diff --git a/java/lora-ns-chirpstack/src/main/java/lora/ns/chirpstack/GrpcExceptionHandler.java b/java/lora-ns-chirpstack/src/main/java/lora/ns/chirpstack/GrpcExceptionHandler.java new file mode 100644 index 000000000..696ad5fd6 --- /dev/null +++ b/java/lora-ns-chirpstack/src/main/java/lora/ns/chirpstack/GrpcExceptionHandler.java @@ -0,0 +1,33 @@ +package lora.ns.chirpstack; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import com.cumulocity.model.event.CumulocitySeverities; + +import io.grpc.StatusRuntimeException; +import lombok.RequiredArgsConstructor; +import lora.ns.exception.LoraError; +import lora.rest.LoraContextService; + +@RestControllerAdvice +@RequiredArgsConstructor +public class GrpcExceptionHandler { + + private final LoraContextService loraContextService; + + @ExceptionHandler(StatusRuntimeException.class) + private ResponseEntity processGrpcStatusRuntimeException(StatusRuntimeException e) { + loraContextService.error(e.getMessage(), e); + loraContextService.sendAlarm(e.getClass().getSimpleName(), e.getMessage(), CumulocitySeverities.CRITICAL); + StringWriter detailedMessage = new StringWriter(); + e.printStackTrace(new PrintWriter(detailedMessage)); + return new ResponseEntity<>(new LoraError(e.getMessage(), detailedMessage.toString()), + HttpStatus.resolve(e.getStatus().getCode().value())); + } +} diff --git a/java/lora-ns-ms/src/main/java/lora/ns/connector/LNSAbstractConnector.java b/java/lora-ns-ms/src/main/java/lora/ns/connector/LNSAbstractConnector.java index 1aacb71a5..2942ca3ec 100644 --- a/java/lora-ns-ms/src/main/java/lora/ns/connector/LNSAbstractConnector.java +++ b/java/lora-ns-ms/src/main/java/lora/ns/connector/LNSAbstractConnector.java @@ -77,6 +77,8 @@ public void afterPropertiesSet() { for (OptionRepresentation option : tenantOptionApi.getAllOptionsForCategory(this.getId())) { this.properties.setProperty(option.getKey(), option.getValue()); } + mergeProperties(this.getInitProperties()); + log.info("Using following properties: {}", this.properties); init(); } diff --git a/java/lora-ns-ms/src/main/java/lora/ns/integration/LNSIntegrationService.java b/java/lora-ns-ms/src/main/java/lora/ns/integration/LNSIntegrationService.java index 599c9ae4b..46fb9d335 100644 --- a/java/lora-ns-ms/src/main/java/lora/ns/integration/LNSIntegrationService.java +++ b/java/lora-ns-ms/src/main/java/lora/ns/integration/LNSIntegrationService.java @@ -313,25 +313,12 @@ public ManagedObjectRepresentation addLnsConnector(LNSConnectorRepresentation co ManagedObject agentApi = inventoryApi.getManagedObjectApi(agentService.getAgent().getId()); agentApi.addChildDevice(mor.getId()); - LNSConnector instance = getInstance(mor); + String category = mor.getId().getValue(); + savePropertiesAsTenantOptions(connectorRepresentation.getProperties(), category); - Properties allProperties = new Properties(); - allProperties.putAll(instance.getInitProperties()); - allProperties.putAll(connectorRepresentation.getProperties()); - instance.setProperties(allProperties); + LNSConnector instance = getInstance(mor); - String category = mor.getId().getValue(); - allProperties.forEach((k, v) -> { - OptionRepresentation option = new OptionRepresentation(); - option.setCategory(category); - if (isPropertyEncrypted(k.toString())) { - option.setKey("credentials." + k.toString()); - } else { - option.setKey(k.toString()); - } - option.setValue(v.toString()); - tenantOptionApi.save(option); - }); + savePropertiesAsTenantOptions(instance.getInitProperties(), category); lnsConnectorManager.addConnector(instance); Optional credentials = subscriptionsService @@ -346,6 +333,20 @@ public ManagedObjectRepresentation addLnsConnector(LNSConnectorRepresentation co return mor; } + private void savePropertiesAsTenantOptions(Properties properties, String category) { + properties.forEach((k, v) -> { + OptionRepresentation option = new OptionRepresentation(); + option.setCategory(category); + if (isPropertyEncrypted(k.toString())) { + option.setKey("credentials." + k.toString()); + } else { + option.setKey(k.toString()); + } + option.setValue(v.toString()); + tenantOptionApi.save(option); + }); + } + public void removeLnsConnector(String lnsConnectorId) { lnsConnectorManager.getConnector(lnsConnectorId).removeRoutings(); inventoryApi.delete(new GId(lnsConnectorId));