Skip to content

Commit

Permalink
more review
Browse files Browse the repository at this point in the history
we probably need to discuss how to handle exceptions for registration
  • Loading branch information
fvolz committed Sep 21, 2023
1 parent 829276b commit fba72c9
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,16 @@ public RegistryHandler(MessageBus messageBus, Persistence persistence, CoreConfi
this.persistence = persistence;
this.coreConfig = coreConfig;
httpClient = HttpClient.newBuilder().build();
messageBus.subscribe(SubscriptionInfo.create(ElementCreateEventMessage.class, LambdaExceptionHelper.wrap(m -> handleCreateEvent(m))));
messageBus.subscribe(SubscriptionInfo.create(ElementUpdateEventMessage.class, LambdaExceptionHelper.wrap(m -> handleChangeEvent(m))));
messageBus.subscribe(SubscriptionInfo.create(ElementDeleteEventMessage.class, LambdaExceptionHelper.wrap(m -> handleDeleteEvent(m))));
messageBus.subscribe(SubscriptionInfo.create(ElementCreateEventMessage.class, LambdaExceptionHelper.wrap(this::handleCreateEvent)));
messageBus.subscribe(SubscriptionInfo.create(ElementUpdateEventMessage.class, LambdaExceptionHelper.wrap(this::handleChangeEvent)));
messageBus.subscribe(SubscriptionInfo.create(ElementDeleteEventMessage.class, LambdaExceptionHelper.wrap(this::handleDeleteEvent)));
environment = persistence.getEnvironment();
try {
if (Objects.isNull(environment) || environment.getAssetAdministrationShells().isEmpty())
return;
for (AssetAdministrationShell aas: environment.getAssetAdministrationShells()) {
createIdentifiableInRegistry(getAasDescriptor(aas), coreConfig.getAasRegistryBasePath());
}
LOGGER.info("Registration of FA³ST Service in Registry successful.");
}
catch (RegistryException | InterruptedException e) {
LOGGER.error(String.format(SYNC_EXCEPTION, e.getMessage()), e);
Expand Down Expand Up @@ -133,9 +132,8 @@ else if (referenceIsKeyElement(eventMessage.getElement(), KeyElements.SUBMODEL))
* Sends the request for updating an aas or submodel in the registry.
*
* @param eventMessage Event that signals the update of an element.
* @throws RegistryException
*/
protected void handleChangeEvent(ElementUpdateEventMessage eventMessage) throws RegistryException, InterruptedException {
protected void handleChangeEvent(ElementUpdateEventMessage eventMessage) throws InterruptedException {
String identifier = eventMessage.getElement().getKeys().get(0).getValue();
if (referenceIsKeyElement(eventMessage.getElement(), KeyElements.ASSET_ADMINISTRATION_SHELL)) {
updateIdentifiableInRegistry(identifier, getAasDescriptor(getAasFromIdentifier(identifier)), coreConfig.getAasRegistryBasePath());
Expand All @@ -153,9 +151,8 @@ else if (referenceIsKeyElement(eventMessage.getElement(), KeyElements.SUBMODEL))
* Sends the request for deleting an aas or submodel in the registry.
*
* @param eventMessage Event that signals the deletion of an element.
* @throws RegistryException
*/
protected void handleDeleteEvent(ElementDeleteEventMessage eventMessage) throws RegistryException, InterruptedException {
protected void handleDeleteEvent(ElementDeleteEventMessage eventMessage) throws InterruptedException {
String identifier = eventMessage.getElement().getKeys().get(0).getValue();
if (referenceIsKeyElement(eventMessage.getElement(), KeyElements.ASSET_ADMINISTRATION_SHELL)) {
deleteIdentifiableInRegistry(identifier, coreConfig.getAasRegistryBasePath());
Expand Down Expand Up @@ -185,17 +182,17 @@ private void createIdentifiableInRegistry(AbstractIdentifiableDescriptor descrip
HttpRequest.BodyPublishers.ofString(mapper.writeValueAsString(descriptor)),
HttpResponse.BodyHandlers.ofString(),
null);
if (!is2xxSuccessful(response.statusCode())) {
if (Objects.isNull(response) || !is2xxSuccessful(response.statusCode())) {
LOGGER.warn(String.format(SYNC_EVENT_ERROR));
}
}
catch (Exception e) {
catch (URISyntaxException | IOException e) {
LOGGER.error(String.format(SYNC_EXCEPTION, e.getMessage()), e);
}
}


private void updateIdentifiableInRegistry(String identifier, AbstractIdentifiableDescriptor descriptor, String basePath) throws RegistryException, InterruptedException {
private void updateIdentifiableInRegistry(String identifier, AbstractIdentifiableDescriptor descriptor, String basePath) throws InterruptedException {
try {
HttpResponse<String> response = execute(
new URL("HTTP", coreConfig.getRegistryHost(), coreConfig.getRegistryPort(),
Expand All @@ -205,17 +202,17 @@ private void updateIdentifiableInRegistry(String identifier, AbstractIdentifiabl
HttpRequest.BodyPublishers.ofString(mapper.writeValueAsString(descriptor)),
HttpResponse.BodyHandlers.ofString(),
null);
if (!is2xxSuccessful(response.statusCode())) {
if (Objects.isNull(response) || !is2xxSuccessful(response.statusCode())) {
LOGGER.warn(String.format(SYNC_EVENT_ERROR));
}
}
catch (Exception e) {
catch (URISyntaxException | IOException e) {
LOGGER.error(String.format(SYNC_EXCEPTION, e.getMessage()), e);
}
}


private void deleteIdentifiableInRegistry(String identifier, String basePath) throws RegistryException, InterruptedException {
private void deleteIdentifiableInRegistry(String identifier, String basePath) throws InterruptedException {
try {
HttpResponse<String> response = execute(
new URL("HTTP", coreConfig.getRegistryHost(), coreConfig.getRegistryPort(),
Expand All @@ -225,11 +222,11 @@ private void deleteIdentifiableInRegistry(String identifier, String basePath) th
HttpRequest.BodyPublishers.noBody(),
HttpResponse.BodyHandlers.ofString(),
null);
if (!is2xxSuccessful(response.statusCode())) {
if (Objects.isNull(response) || !is2xxSuccessful(response.statusCode())) {
LOGGER.warn(String.format(SYNC_EVENT_ERROR));
}
}
catch (Exception e) {
catch (URISyntaxException | IOException e) {
LOGGER.error(String.format(SYNC_EXCEPTION, e.getMessage()), e);
}
}
Expand Down Expand Up @@ -258,7 +255,13 @@ private <T> HttpResponse<T> execute(
builder = builder.header(header.getKey(), header.getValue());
}
}
return httpClient.send(builder.method(method, bodyPublisher).build(), bodyHandler);
try {
return httpClient.send(builder.method(method, bodyPublisher).build(), bodyHandler);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return null;
}


Expand Down

0 comments on commit fba72c9

Please sign in to comment.