Skip to content

Commit

Permalink
Fixed bad workflow for connector creation
Browse files Browse the repository at this point in the history
  • Loading branch information
cpoder committed Nov 20, 2024
1 parent 15f2eaa commit e50b671
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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<LoraError> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MicroserviceCredentials> credentials = subscriptionsService
Expand All @@ -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));
Expand Down

0 comments on commit e50b671

Please sign in to comment.