Skip to content

Commit

Permalink
Merge pull request #111 from maciej-cz/feature/webhook-url-customization
Browse files Browse the repository at this point in the history
Allow webhook URL customization
  • Loading branch information
cpoder authored Jul 3, 2024
2 parents 9a78587 + 1443725 commit 35102aa
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package lora.ns.actility;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import javax.net.ssl.HostnameVerifier;
Expand Down Expand Up @@ -274,6 +277,13 @@ public void configureRoutings(String url, String tenant, String login, String pa
}
}

@Override
public Optional<String> getCustomRoutingBaseUrl() {
return isNotBlank(properties.getProperty("webhook-url")) ?
Optional.of(properties.getProperty("webhook-url")) :
super.getCustomRoutingBaseUrl();
}

@Override
public void removeRoutings() {
// Don't remove the connections, update them
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
package lora.ns.actility;

import java.util.LinkedList;
import java.util.List;

import lora.ns.connector.LNSConnectorWizardStep;
import lora.ns.connector.PropertyDescription;
import lora.ns.connector.PropertyDescription.PropertyType;

public class ConnectorWizardStep1 implements LNSConnectorWizardStep {

protected LinkedList<PropertyDescription> propertyDescriptions = new LinkedList<>();
{
propertyDescriptions.add(new PropertyDescription("url", "URL", true, null, null, null, null, null, null, null,
PropertyType.TEXT, false));
propertyDescriptions.add(new PropertyDescription("username", "Username", true, null, null, null, null, null,
null, null, PropertyType.TEXT, false));
propertyDescriptions.add(new PropertyDescription("password", "Password", true, null, null, null, null, null,
null, null, PropertyType.PASSWORD, true));
propertyDescriptions.add(new PropertyDescription("domain", "Domain", false, null, null, null, null, null, null,
null, PropertyType.TEXT, false));
propertyDescriptions.add(new PropertyDescription("group", "Group", false, null, null, null, null, null, null,
null, PropertyType.TEXT, false));
}
protected List<PropertyDescription> propertyDescriptions = List.of(
PropertyDescription.text("url", "URL", true),
PropertyDescription.text("username", "Username", true),
PropertyDescription.password("password", "Password"),
PropertyDescription.text("domain", "Domain", false),
PropertyDescription.text("group", "Group", false),
PropertyDescription.text("webhook-url", "Webhook URL", false));

@Override
public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class ConnectorWizardStep1 implements LNSConnectorWizardStep {
private final List<PropertyDescription> propertyDescriptions = List.of(
PropertyDescription.text("apikey", "API Key", true).withEncrypted(true),
PropertyDescription.text("proxy-host", "Proxy Host", false),
PropertyDescription.number("proxy-port", "Proxy Port", false));
PropertyDescription.number("proxy-port", "Proxy Port", false),
PropertyDescription.text("webhook-url", "Webhook URL", false));

public String getName() {
return "step1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ public void configureRoutings(String url, String tenant, String login, String pa
this.setProperty("downlinkRouteId", downlinkActionPolicy.getId());
}

@Override
public Optional<String> getCustomRoutingBaseUrl() {
return isNotBlank(properties.getProperty("webhook-url")) ?
Optional.of(properties.getProperty("webhook-url")) :
super.getCustomRoutingBaseUrl();
}

@Override
public void removeRoutings() {
getProperty("uplinkRouteId").ifPresent(id -> service.deleteActionPolicy(id.toString()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lora.ns.connector;

import java.util.List;
import java.util.Optional;
import java.util.Properties;

import lora.codec.downlink.DownlinkData;
Expand Down Expand Up @@ -47,4 +48,8 @@ public interface LNSConnector {
default Properties getInitProperties() {
return new Properties();
}

default Optional<String> getCustomRoutingBaseUrl() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package lora.ns.integration;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Expand Down Expand Up @@ -128,6 +128,12 @@ public abstract class LNSIntegrationService<I extends LNSConnector> {
@Autowired
protected LoraContextService loraContextService;

@Autowired
private ApplicationContext applicationContext;

@Autowired
private EventApi eventApi;

protected LinkedList<LNSConnectorWizardStep> wizard = new LinkedList<>();

protected LinkedList<PropertyDescription> deviceProvisioningAdditionalProperties = new LinkedList<>();
Expand Down Expand Up @@ -155,9 +161,6 @@ public String getSimulatedPayload(Map<String, Object> fields) {
return mMessageTemplateEngine.process("payload.json", context);
}

@Autowired
private ApplicationContext applicationContext;

protected I getInstance(ManagedObjectRepresentation instance) {
@SuppressWarnings("unchecked")
Class<I> instanceType = (Class<I>) ((ParameterizedType) getClass().getGenericSuperclass())
Expand Down Expand Up @@ -185,13 +188,8 @@ private void init(MicroserviceSubscriptionAddedEvent event) {
log.info("Looking for LNS Connectors in tenant {}", subscriptionsService.getTenant());
InventoryFilter filter = new InventoryFilter().byType(LNS_CONNECTOR_TYPE);
ManagedObjectCollection col = inventoryApi.getManagedObjectsByFilter(filter);
QueryParam queryParam = null;
try {
queryParam = new QueryParam(() -> "query", URLEncoder.encode(
LNS_TYPE + " eq " + this.getType() + " and type eq '" + LNS_CONNECTOR_TYPE + "'", "utf8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
QueryParam queryParam = new QueryParam(() -> "query", URLEncoder.encode(
LNS_TYPE + " eq " + this.getType() + " and type eq '" + LNS_CONNECTOR_TYPE + "'", StandardCharsets.UTF_8));
for (ManagedObjectRepresentation mor : col.get(queryParam).allPages()) {
log.info("Retrieved connector: {} of type {}", mor.getName(), mor.getProperty(LNS_TYPE));
LNSConnector instance = getInstance(mor);
Expand All @@ -202,9 +200,6 @@ private void init(MicroserviceSubscriptionAddedEvent event) {
agentService.registerAgent(this);
}

@Autowired
private EventApi eventApi;

public void mapEventToC8Y(String eventString, String lnsInstanceId) {
loraContextService.log("Following message was received from the LNS: {}", eventString);
EventRepresentation event = new EventRepresentation();
Expand Down Expand Up @@ -274,10 +269,9 @@ public List<EndDevice> getDevices(String lnsInstanceId) {
}

private void configureRoutings(String lnsInstanceId, MicroserviceCredentials credentials) {
String url = "https://" + c8yUtils.getTenantDomain() + "/service/lora-ns-" + this.getType() + "/"
+ lnsInstanceId;
loraContextService.log("Connector URL is {}", url);
var connector = lnsConnectorManager.getConnector(lnsInstanceId);
String url = getRoutingUrl(connector);
loraContextService.log("Connector URL is {}", url);
try {
connector.removeRoutings();
connector.configureRoutings(url, subscriptionsService.getTenant(), credentials.getUsername(),
Expand All @@ -287,6 +281,12 @@ private void configureRoutings(String lnsInstanceId, MicroserviceCredentials cre
}
}

private String getRoutingUrl(LNSConnector connector) {
String routingBaseUrl = connector.getCustomRoutingBaseUrl()
.orElse("https://" + c8yUtils.getTenantDomain());
return routingBaseUrl + "/service/lora-ns-" + this.getType() + "/" + connector.getId();
}

public ManagedObjectRepresentation addLnsConnector(LNSConnectorRepresentation connectorRepresentation) {
loraContextService.log("Adding LNS connector {} with properties {}", connectorRepresentation.getName(),
connectorRepresentation.getProperties());
Expand Down

0 comments on commit 35102aa

Please sign in to comment.