Skip to content

Commit e70c288

Browse files
Add submodelReference eventing to MqttAasRepository PR (#515)
* feat: Events + Tests * fix: remove extra line * style: bump header date --------- Co-authored-by: Mateus Molina <[email protected]>
1 parent b3f7507 commit e70c288

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

basyx.aasrepository/basyx.aasrepository-feature-mqtt/Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ This feature provides hierarchical MQTT eventing for a multitude of events:
55
| ----------- | ----------- | --- |
66
| AAS Created | aas-repository/\$repoId/shells/created| Created AAS JSON |
77
| AAS Updated | aas-repository/\$repoId/shells/updated| Updated AAS JSON|
8-
| AAS Deleted | aas-repository/\$repoId/shells/deleted| Deleted AAS JSON|
8+
| AAS Deleted | aas-repository/\$repoId/shells/deleted| Deleted AAS JSON|
9+
| Submodel Reference Created | aas-repository/\$repoId/shells/submodels/\$submodelId/created| Created AAS JSON |
10+
| Submodel Reference Deleted | aas-repository/\$repoId/shells/submodels/\$submodelId/deleted| Deleted AAS JSON|

basyx.aasrepository/basyx.aasrepository-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/mqtt/MqttAasRepository.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (C) 2021 the Eclipse BaSyx Authors
2+
* Copyright (C) 2024 the Eclipse BaSyx Authors
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining
55
* a copy of this software and associated documentation files (the
@@ -107,12 +107,19 @@ public CursorResult<List<Reference>> getSubmodelReferences(String aasId, Paginat
107107

108108
@Override
109109
public void addSubmodelReference(String aasId, Reference submodelReference) {
110+
AssetAdministrationShell shell = decorated.getAas(aasId);
110111
decorated.addSubmodelReference(aasId, submodelReference);
112+
113+
String referenceId = submodelReference.getKeys().get(0).getValue();
114+
115+
submodelReferenceCreated(shell, getName(), referenceId);
111116
}
112117

113118
@Override
114119
public void removeSubmodelReference(String aasId, String submodelId) {
120+
AssetAdministrationShell shell = decorated.getAas(aasId);
115121
decorated.removeSubmodelReference(aasId, submodelId);
122+
submodelReferenceDeleted(shell, getName(), submodelId);
116123
}
117124

118125
@Override
@@ -136,6 +143,14 @@ private void aasUpdated(AssetAdministrationShell shell, String repoId) {
136143
private void aasDeleted(AssetAdministrationShell shell, String repoId) {
137144
sendMqttMessage(topicFactory.createDeleteAASTopic(repoId), serializePayload(shell));
138145
}
146+
147+
private void submodelReferenceCreated(AssetAdministrationShell shell, String repoId, String referenceId) {
148+
sendMqttMessage(topicFactory.createCreateAASSubmodelReferenceTopic(repoId, referenceId), serializePayload(shell));
149+
}
150+
151+
private void submodelReferenceDeleted(AssetAdministrationShell shell, String repoId, String referenceId) {
152+
sendMqttMessage(topicFactory.createDeleteAASSubmodelReferenceTopic(repoId, referenceId), serializePayload(shell));
153+
}
139154

140155
private String serializePayload(AssetAdministrationShell shell) {
141156
try {

basyx.aasrepository/basyx.aasrepository-feature-mqtt/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/mqtt/MqttAasRepositoryTopicFactory.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (C) 2021 the Eclipse BaSyx Authors
2+
* Copyright (C) 2024 the Eclipse BaSyx Authors
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining
55
* a copy of this software and associated documentation files (the
@@ -37,6 +37,7 @@
3737
public class MqttAasRepositoryTopicFactory extends AbstractMqttTopicFactory {
3838
private static final String AASREPOSITORY = "aas-repository";
3939
private static final String SHELLS = "shells";
40+
private static final String SUBMODELS = "submodels";
4041
private static final String CREATED = "created";
4142
private static final String UPDATED = "updated";
4243
private static final String DELETED = "deleted";
@@ -93,4 +94,40 @@ public String createDeleteAASTopic(String repoId) {
9394
.add(DELETED)
9495
.toString();
9596
}
97+
98+
/**
99+
* Creates the hierarchical topic for the submodel reference create event
100+
*
101+
* @param repoId
102+
* @param referenceId
103+
* @return
104+
*/
105+
public String createCreateAASSubmodelReferenceTopic(String repoId, String referenceId) {
106+
return new StringJoiner("/", "", "")
107+
.add(AASREPOSITORY)
108+
.add(repoId)
109+
.add(SHELLS)
110+
.add(SUBMODELS)
111+
.add(referenceId)
112+
.add(CREATED)
113+
.toString();
114+
}
115+
116+
/**
117+
* Creates the hierarchical topic for the submodel reference delete event
118+
*
119+
* @param repoId
120+
* @param referenceId
121+
* @return
122+
*/
123+
public String createDeleteAASSubmodelReferenceTopic(String repoId, String referenceId) {
124+
return new StringJoiner("/", "", "")
125+
.add(AASREPOSITORY)
126+
.add(repoId)
127+
.add(SHELLS)
128+
.add(SUBMODELS)
129+
.add(referenceId)
130+
.add(DELETED)
131+
.toString();
132+
}
96133
}

basyx.aasrepository/basyx.aasrepository-feature-mqtt/src/test/java/org/eclipse/digitaltwin/basyx/aasrepository/feature/mqtt/TestMqttV2AASAggregatorObserver.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
package org.eclipse.digitaltwin.basyx.aasrepository.feature.mqtt;
2-
31
/*******************************************************************************
4-
* Copyright (C) 2021 the Eclipse BaSyx Authors
2+
* Copyright (C) 2024 the Eclipse BaSyx Authors
53
*
64
* Permission is hereby granted, free of charge, to any person obtaining
75
* a copy of this software and associated documentation files (the
@@ -24,10 +22,12 @@
2422
*
2523
* SPDX-License-Identifier: MIT
2624
******************************************************************************/
25+
package org.eclipse.digitaltwin.basyx.aasrepository.feature.mqtt;
2726

2827
import static org.junit.Assert.assertEquals;
2928

3029
import java.io.IOException;
30+
import java.util.ArrayList;
3131
import java.util.Arrays;
3232
import java.util.List;
3333

@@ -94,7 +94,7 @@ public static void tearDownClass() {
9494
public void createAasEvent() throws DeserializationException {
9595
AssetAdministrationShell shell = createAasDummy("createAasEventId");
9696
aasRepository.createAas(shell);
97-
97+
9898
assertEquals(topicFactory.createCreateAASTopic(aasRepository.getName()), listener.lastTopic);
9999
assertEquals(shell, deserializePayload(listener.lastPayload));
100100
}
@@ -121,6 +121,29 @@ public void deleteAasEvent() throws DeserializationException {
121121
assertEquals(topicFactory.createDeleteAASTopic(aasRepository.getName()), listener.lastTopic);
122122
assertEquals(shell, deserializePayload(listener.lastPayload));
123123
}
124+
125+
@Test
126+
public void addSubmodelReferenceEvent() throws DeserializationException {
127+
AssetAdministrationShell shell = createAasDummy("createAasSubmodelRefEventId");
128+
aasRepository.createAas(shell);
129+
130+
Reference submodelReference = DummyAasFactory.createDummyReference(DummyAasFactory.DUMMY_SUBMODEL_ID);
131+
aasRepository.addSubmodelReference(shell.getId(), submodelReference);
132+
133+
assertEquals(topicFactory.createCreateAASSubmodelReferenceTopic(aasRepository.getName(), DummyAasFactory.DUMMY_SUBMODEL_ID), listener.lastTopic);
134+
assertEquals(shell, deserializePayload(listener.lastPayload));
135+
}
136+
137+
@Test
138+
public void removeSubmodelReferenceEvent() throws DeserializationException {
139+
AssetAdministrationShell shell = createAasWithSubmodelReference("removeAasSubmodelRefEventId");
140+
aasRepository.createAas(shell);
141+
142+
aasRepository.removeSubmodelReference(shell.getId(), DummyAasFactory.DUMMY_SUBMODEL_ID);
143+
144+
assertEquals(topicFactory.createDeleteAASSubmodelReferenceTopic(aasRepository.getName(), DummyAasFactory.DUMMY_SUBMODEL_ID), listener.lastTopic);
145+
assertEquals(shell, deserializePayload(listener.lastPayload));
146+
}
124147

125148
private AssetAdministrationShell deserializePayload(String payload) throws DeserializationException {
126149
return new JsonDeserializer().read(payload, AssetAdministrationShell.class);
@@ -135,6 +158,16 @@ private AssetAdministrationShell createAasDummy(String aasId) {
135158
return new DefaultAssetAdministrationShell.Builder().id(aasId)
136159
.build();
137160
}
161+
162+
private AssetAdministrationShell createAasWithSubmodelReference(String aasId) {
163+
Reference submodelReference = DummyAasFactory.createDummyReference(DummyAasFactory.DUMMY_SUBMODEL_ID);
164+
165+
List<Reference> submodelReferences = new ArrayList<Reference>();
166+
submodelReferences.add(submodelReference);
167+
168+
return new DefaultAssetAdministrationShell.Builder().id(aasId).submodels(submodelReferences)
169+
.build();
170+
}
138171

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

0 commit comments

Comments
 (0)