Skip to content

Commit

Permalink
Add submodelReference eventing to MqttAasRepository PR (#515)
Browse files Browse the repository at this point in the history
* feat: Events + Tests

* fix: remove extra line

* style: bump header date

---------

Co-authored-by: Mateus Molina <[email protected]>
  • Loading branch information
1 parent b3f7507 commit e70c288
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ This feature provides hierarchical MQTT eventing for a multitude of events:
| ----------- | ----------- | --- |
| AAS Created | aas-repository/\$repoId/shells/created| Created AAS JSON |
| AAS Updated | aas-repository/\$repoId/shells/updated| Updated AAS JSON|
| AAS Deleted | aas-repository/\$repoId/shells/deleted| Deleted AAS JSON|
| AAS Deleted | aas-repository/\$repoId/shells/deleted| Deleted AAS JSON|
| Submodel Reference Created | aas-repository/\$repoId/shells/submodels/\$submodelId/created| Created AAS JSON |
| Submodel Reference Deleted | aas-repository/\$repoId/shells/submodels/\$submodelId/deleted| Deleted AAS JSON|
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2021 the Eclipse BaSyx Authors
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -107,12 +107,19 @@ public CursorResult<List<Reference>> getSubmodelReferences(String aasId, Paginat

@Override
public void addSubmodelReference(String aasId, Reference submodelReference) {
AssetAdministrationShell shell = decorated.getAas(aasId);
decorated.addSubmodelReference(aasId, submodelReference);

String referenceId = submodelReference.getKeys().get(0).getValue();

submodelReferenceCreated(shell, getName(), referenceId);
}

@Override
public void removeSubmodelReference(String aasId, String submodelId) {
AssetAdministrationShell shell = decorated.getAas(aasId);
decorated.removeSubmodelReference(aasId, submodelId);
submodelReferenceDeleted(shell, getName(), submodelId);
}

@Override
Expand All @@ -136,6 +143,14 @@ private void aasUpdated(AssetAdministrationShell shell, String repoId) {
private void aasDeleted(AssetAdministrationShell shell, String repoId) {
sendMqttMessage(topicFactory.createDeleteAASTopic(repoId), serializePayload(shell));
}

private void submodelReferenceCreated(AssetAdministrationShell shell, String repoId, String referenceId) {
sendMqttMessage(topicFactory.createCreateAASSubmodelReferenceTopic(repoId, referenceId), serializePayload(shell));
}

private void submodelReferenceDeleted(AssetAdministrationShell shell, String repoId, String referenceId) {
sendMqttMessage(topicFactory.createDeleteAASSubmodelReferenceTopic(repoId, referenceId), serializePayload(shell));
}

private String serializePayload(AssetAdministrationShell shell) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2021 the Eclipse BaSyx Authors
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -37,6 +37,7 @@
public class MqttAasRepositoryTopicFactory extends AbstractMqttTopicFactory {
private static final String AASREPOSITORY = "aas-repository";
private static final String SHELLS = "shells";
private static final String SUBMODELS = "submodels";
private static final String CREATED = "created";
private static final String UPDATED = "updated";
private static final String DELETED = "deleted";
Expand Down Expand Up @@ -93,4 +94,40 @@ public String createDeleteAASTopic(String repoId) {
.add(DELETED)
.toString();
}

/**
* Creates the hierarchical topic for the submodel reference create event
*
* @param repoId
* @param referenceId
* @return
*/
public String createCreateAASSubmodelReferenceTopic(String repoId, String referenceId) {
return new StringJoiner("/", "", "")
.add(AASREPOSITORY)
.add(repoId)
.add(SHELLS)
.add(SUBMODELS)
.add(referenceId)
.add(CREATED)
.toString();
}

/**
* Creates the hierarchical topic for the submodel reference delete event
*
* @param repoId
* @param referenceId
* @return
*/
public String createDeleteAASSubmodelReferenceTopic(String repoId, String referenceId) {
return new StringJoiner("/", "", "")
.add(AASREPOSITORY)
.add(repoId)
.add(SHELLS)
.add(SUBMODELS)
.add(referenceId)
.add(DELETED)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.eclipse.digitaltwin.basyx.aasrepository.feature.mqtt;

/*******************************************************************************
* Copyright (C) 2021 the Eclipse BaSyx Authors
* Copyright (C) 2024 the Eclipse BaSyx Authors
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand All @@ -24,10 +22,12 @@
*
* SPDX-License-Identifier: MIT
******************************************************************************/
package org.eclipse.digitaltwin.basyx.aasrepository.feature.mqtt;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -94,7 +94,7 @@ public static void tearDownClass() {
public void createAasEvent() throws DeserializationException {
AssetAdministrationShell shell = createAasDummy("createAasEventId");
aasRepository.createAas(shell);

assertEquals(topicFactory.createCreateAASTopic(aasRepository.getName()), listener.lastTopic);
assertEquals(shell, deserializePayload(listener.lastPayload));
}
Expand All @@ -121,6 +121,29 @@ public void deleteAasEvent() throws DeserializationException {
assertEquals(topicFactory.createDeleteAASTopic(aasRepository.getName()), listener.lastTopic);
assertEquals(shell, deserializePayload(listener.lastPayload));
}

@Test
public void addSubmodelReferenceEvent() throws DeserializationException {
AssetAdministrationShell shell = createAasDummy("createAasSubmodelRefEventId");
aasRepository.createAas(shell);

Reference submodelReference = DummyAasFactory.createDummyReference(DummyAasFactory.DUMMY_SUBMODEL_ID);
aasRepository.addSubmodelReference(shell.getId(), submodelReference);

assertEquals(topicFactory.createCreateAASSubmodelReferenceTopic(aasRepository.getName(), DummyAasFactory.DUMMY_SUBMODEL_ID), listener.lastTopic);
assertEquals(shell, deserializePayload(listener.lastPayload));
}

@Test
public void removeSubmodelReferenceEvent() throws DeserializationException {
AssetAdministrationShell shell = createAasWithSubmodelReference("removeAasSubmodelRefEventId");
aasRepository.createAas(shell);

aasRepository.removeSubmodelReference(shell.getId(), DummyAasFactory.DUMMY_SUBMODEL_ID);

assertEquals(topicFactory.createDeleteAASSubmodelReferenceTopic(aasRepository.getName(), DummyAasFactory.DUMMY_SUBMODEL_ID), listener.lastTopic);
assertEquals(shell, deserializePayload(listener.lastPayload));
}

private AssetAdministrationShell deserializePayload(String payload) throws DeserializationException {
return new JsonDeserializer().read(payload, AssetAdministrationShell.class);
Expand All @@ -135,6 +158,16 @@ private AssetAdministrationShell createAasDummy(String aasId) {
return new DefaultAssetAdministrationShell.Builder().id(aasId)
.build();
}

private AssetAdministrationShell createAasWithSubmodelReference(String aasId) {
Reference submodelReference = DummyAasFactory.createDummyReference(DummyAasFactory.DUMMY_SUBMODEL_ID);

List<Reference> submodelReferences = new ArrayList<Reference>();
submodelReferences.add(submodelReference);

return new DefaultAssetAdministrationShell.Builder().id(aasId).submodels(submodelReferences)
.build();
}

private static AasRepository createMqttAasRepository(MqttClient client) {
AasRepositoryFactory repoFactory = new SimpleAasRepositoryFactory(new AasInMemoryBackendProvider(), new InMemoryAasServiceFactory(new InMemoryFileRepository()));
Expand Down

0 comments on commit e70c288

Please sign in to comment.