Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Remaining MQTTSubmodelRepo Methods #544

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

package org.eclipse.digitaltwin.basyx.common.mqttcore.serializer;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.eclipse.digitaltwin.aas4j.v3.dataformat.core.DeserializationException;
Expand Down Expand Up @@ -72,6 +74,34 @@ public static String serializeSubmodelElement(SubmodelElement submodelElement) {
throw new RuntimeException(e);
}
}

/**
* Serializer to create a JSON String for the given submodel elements.
*
* @param submodelElement
* @return serialized list of submodelElements as JSON String
*/
public static String serializeSubmodelElements(List<SubmodelElement> submodelElements) {
try {
List<SubmodelElement> updatedSubmodelElements = new ArrayList<>();

for(int i = 0; i < submodelElements.size(); i++) {
SubmodelElement elem = submodelElements.get(i);
SubmodelElement localElement;
if (shouldSendEmptyValueEvent(elem)) {
localElement = getSubmodelElementWithoutValue(elem);
} else {
localElement = elem;
}

updatedSubmodelElements.add(localElement);
}

return new JsonSerializer().writeList(updatedSubmodelElements);
} catch (SerializationException | DeserializationException e) {
throw new RuntimeException(e);
}
}

/**
* Generator to create a copy of a submodelElement without its value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ This feature provides hierarchical MQTT eventing for a multitude of events:
| SubmodelElement Created | sm-repository/$repoId/submodels/$submodelIdBase64URLEncoded/submodelElements/$idShortPath/created | Created SubmodelElement JSON |
| SubmodelElement Updated | sm-repository/$repoId/submodels/$submodelIdBase64URLEncoded/submodelElements/$idShortPath/updated | Updated SubmodelElement JSON |
| SubmodelElement Deleted | sm-repository/$repoId/submodels/$submodelIdBase64URLEncoded/submodelElements/$idShortPath/deleted | Deleted SubmodelElement JSON |
| SubmodelElements Patched | sm-repository/$repoId/submodels/$submodelIdBase64URLEncoded/submodelElements/patched | Patched SubmodelElements JSON |
| FileValue Updated | sm-repository/$repoId/submodels/$submodelIdBase64URLEncoded/submodelElements/$idShortPath/fileValue/$fileName/updated | Updated SubmodelElement JSON |
Copy link
Member

@mateusmolina-iese mateusmolina-iese Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please change the topic to match the spec [1, 2] (.../$idShortPath/attachment/updated and /deleted)? Also, no need for the $fileName in the topic itself.

[1] https://app.swaggerhub.com/apis/Plattform_i40/SubmodelServiceSpecification/V3.0.1_SSP-001#/Submodel%20API/PutFileByPath
[2] https://app.swaggerhub.com/apis/Plattform_i40/SubmodelServiceSpecification/V3.0.1_SSP-001#/Submodel%20API/DeleteFileByPath

| FileValue Deleted | sm-repository/$repoId/submodels/$submodelIdBase64URLEncoded/submodelElements/$idShortPath/fileValue/deleted | Deleted SubmodelElement JSON |

Per default, the SubmodelElement topic payloads include the SubmodelElement's value. If this is not desired, the SubmodelElement can be annotated with a Qualifier of type *emptyValueUpdateEvent* and value *true*
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.eclipse.digitaltwin.basyx.submodelrepository.feature.mqtt;

import java.io.File;
import java.io.InputStream;
import java.util.List;

Expand Down Expand Up @@ -126,6 +127,13 @@ public void deleteSubmodelElement(String submodelId, String idShortPath) throws
submodelElementDeleted(submodelElement, getName(), submodelId, idShortPath);
}

@Override
public void patchSubmodelElements(String submodelId, List<SubmodelElement> submodelElementList) {
decorated.patchSubmodelElements(submodelId, submodelElementList);
List<SubmodelElement> patchedSubmodelElements = decorated.getSubmodelElements(submodelId, PaginationInfo.NO_LIMIT).getResult();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to use the NO_LIMIT request to get the patched submodel elements again? They are already being passed as parameter.

submodelElementsPatched(patchedSubmodelElements, getName(), submodelId);
}

@Override
public String getName() {
return decorated.getName();
Expand All @@ -141,6 +149,35 @@ public Submodel getSubmodelByIdMetadata(String submodelId) {
return decorated.getSubmodelByIdMetadata(submodelId);
}

@Override
public OperationVariable[] invokeOperation(String submodelId, String idShortPath, OperationVariable[] input) throws ElementDoesNotExistException {
return decorated.invokeOperation(submodelId, idShortPath, input);
}

@Override
public java.io.File getFileByPathSubmodel(String submodelId, String idShortPath) {
return decorated.getFileByPathSubmodel(submodelId, idShortPath);
}

@Override
public void deleteFileValue(String identifier, String idShortPath) {
SubmodelElement submodelElement = decorated.getSubmodelElement(identifier, idShortPath);
decorated.deleteFileValue(identifier, idShortPath);
fileValueDeleted(submodelElement, getName(), identifier, idShortPath);
}

@Override
public void setFileValue(String submodelId, String idShortPath, String fileName, InputStream inputStream){
decorated.setFileValue(submodelId, idShortPath, fileName, inputStream);
SubmodelElement submodelElement = decorated.getSubmodelElement(submodelId, idShortPath);
fileValueUpdated(submodelElement, getName(), submodelId, idShortPath, fileName);
}

@Override
public InputStream getFileByFilePath(String submodelId, String filePath) {
return decorated.getFileByFilePath(submodelId, filePath);
}

private void submodelCreated(Submodel submodel, String repoId) {
sendMqttMessage(topicFactory.createCreateSubmodelTopic(repoId), SubmodelSerializer.serializeSubmodel(submodel));
}
Expand All @@ -164,6 +201,18 @@ private void submodelElementUpdated(SubmodelElement submodelElement, String repo
private void submodelElementDeleted(SubmodelElement submodelElement, String repoId, String submodelId, String submodelElementId) {
sendMqttMessage(topicFactory.createDeleteSubmodelElementTopic(repoId, submodelId, submodelElementId), SubmodelElementSerializer.serializeSubmodelElement(submodelElement));
}

private void submodelElementsPatched(List<SubmodelElement> submodelElements, String repoId, String submodelId) {
sendMqttMessage(topicFactory.createPatchSubmodelElementsTopic(repoId, submodelId), SubmodelElementSerializer.serializeSubmodelElements(submodelElements));
}

private void fileValueDeleted(SubmodelElement submodelElement, String repoId, String submodelId, String submodelElementId) {
sendMqttMessage(topicFactory.createDeleteFileValueTopic(repoId, submodelId, submodelElementId), SubmodelElementSerializer.serializeSubmodelElement(submodelElement));
}

private void fileValueUpdated(SubmodelElement submodelElement, String repoId, String submodelId, String submodelElementId, String fileName) {
sendMqttMessage(topicFactory.createUpdateFileValueTopic(repoId, submodelId, submodelElementId, fileName), SubmodelElementSerializer.serializeSubmodelElement(submodelElement));
}

/**
* Sends MQTT message to connected broker
Expand Down Expand Up @@ -194,37 +243,4 @@ private MqttMessage createMqttMessage(String payload) {
}
}

@Override
public OperationVariable[] invokeOperation(String submodelId, String idShortPath, OperationVariable[] input) throws ElementDoesNotExistException {
return decorated.invokeOperation(submodelId, idShortPath, input);
}

@Override
public java.io.File getFileByPathSubmodel(String submodelId, String idShortPath) {
return decorated.getFileByPathSubmodel(submodelId, idShortPath);
}

@Override
public void deleteFileValue(String identifier, String idShortPath) {
// TODO: Eventing
decorated.deleteFileValue(identifier, idShortPath);
}

@Override
public void setFileValue(String submodelId, String idShortPath, String fileName, InputStream inputStream){
// TODO: Eventing
decorated.setFileValue(submodelId, idShortPath, fileName, inputStream);
}

@Override
public void patchSubmodelElements(String submodelId, List<SubmodelElement> submodelElementList) {
// TODO: Eventing
decorated.patchSubmodelElements(submodelId, submodelElementList);
}

@Override
public InputStream getFileByFilePath(String submodelId, String filePath) {
return decorated.getFileByFilePath(submodelId, filePath);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public class MqttSubmodelRepositoryTopicFactory extends AbstractMqttTopicFactory
private static final String CREATED = "created";
private static final String UPDATED = "updated";
private static final String DELETED = "deleted";
private static final String PATCHED = "patched";
private static final String SUBMODELELEMENTS = "submodelElements";
private static final String FILEVALUE = "fileValue";

/**
* @param encoder
Expand Down Expand Up @@ -104,4 +106,34 @@ public String createUpdateSubmodelElementTopic(String repoId, String submodelId,
public String createDeleteSubmodelElementTopic(String repoId, String submodelId, String submodelElementId) {
return new StringJoiner("/", "", "").add(SUBMODELREPOSITORY).add(repoId).add(SUBMODELS).add(encodeId(submodelId)).add(SUBMODELELEMENTS).add(submodelElementId).add(DELETED).toString();
}

/**
* Creates the hierarchical topic for the patch event of submodelElements
*
* @param repoId
* @param submodelId
*/
public String createPatchSubmodelElementsTopic(String repoId, String submodelId) {
return new StringJoiner("/", "", "").add(SUBMODELREPOSITORY).add(repoId).add(SUBMODELS).add(encodeId(submodelId)).add(SUBMODELELEMENTS).add(PATCHED).toString();
}

/**
* Creates the hierarchical topic for the delete event of a file of a file element
*
* @param repoId
*
*/
public String createDeleteFileValueTopic(String repoId, String submodelId, String submodelElementId) {
return new StringJoiner("/", "", "").add(SUBMODELREPOSITORY).add(repoId).add(SUBMODELS).add(encodeId(submodelId)).add(SUBMODELELEMENTS).add(submodelElementId).add(FILEVALUE).add(DELETED).toString();
}

/**
* Creates the hierarchical topic for the update event of a file of a file element
*
* @param repoId
*
*/
public String createUpdateFileValueTopic(String repoId, String submodelId, String submodelElementId, String fileName) {
return new StringJoiner("/", "", "").add(SUBMODELREPOSITORY).add(repoId).add(SUBMODELS).add(encodeId(submodelId)).add(SUBMODELELEMENTS).add(submodelElementId).add(FILEVALUE).add(fileName).add(UPDATED).toString();
}
}
Loading