use of de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException in project FAAAST-Service by FraunhoferIOSB.
the class PersistenceInMemoryTest method getSubmodelElementsWithSemanticIdTest.
@Test
public void getSubmodelElementsWithSemanticIdTest() throws ResourceNotFoundException {
String AAS_IDSHORT = "TestAssetAdministrationShell";
String SUBMODEL_IRI = "http://acplt.org/Submodels/Assets/TestAsset/Identification";
Reference submodelReference = Util.createReference(AAS_IDSHORT, SUBMODEL_IRI);
Reference semanticIdReference = new DefaultReference.Builder().key(new DefaultKey.Builder().type(KeyElements.GLOBAL_REFERENCE).value("0173-1#02-AAO677#002").idType(KeyType.IRI).build()).build();
List<SubmodelElement> actualSubmodelElements = persistence.getSubmodelElements(submodelReference, semanticIdReference, new QueryModifier());
List<SubmodelElement> expectedSubmodelElements = this.environment.getSubmodels().stream().filter(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(SUBMODEL_IRI)).findFirst().get().getSubmodelElements().stream().filter(x -> x.getSemanticId().equals(semanticIdReference)).collect(Collectors.toList());
Assert.assertEquals(expectedSubmodelElements, actualSubmodelElements);
}
use of de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException in project FAAAST-Service by FraunhoferIOSB.
the class PersistenceInMemoryTest method testQueryModifierLevel.
@Test
public void testQueryModifierLevel() throws ResourceNotFoundException {
String SUBMODEL_IDENTIFIER = "https://acplt.org/Test_Submodel_Mandatory";
QueryModifier queryModifier = new QueryModifier.Builder().level(Level.Deep).build();
Identifier submodelId = new DefaultIdentifier.Builder().idType(IdentifierType.IRI).identifier(SUBMODEL_IDENTIFIER).build();
Submodel expected = this.environment.getSubmodels().stream().filter(x -> x.getIdentification().equals(submodelId)).findFirst().get();
Submodel actual = (Submodel) this.persistence.get(submodelId, queryModifier);
Assert.assertEquals(expected, actual);
queryModifier = new QueryModifier.Builder().level(Level.Core).build();
actual = (Submodel) this.persistence.get(submodelId, queryModifier);
List<SubmodelElement> submodelElementCollections = actual.getSubmodelElements().stream().filter(x -> SubmodelElementCollection.class.isAssignableFrom(x.getClass())).collect(Collectors.toList());
Assert.assertTrue(submodelElementCollections.stream().allMatch(x -> ((SubmodelElementCollection) x).getValues() == null));
}
use of de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException in project FAAAST-Service by FraunhoferIOSB.
the class ReferablePersistenceManager method getSubmodelElements.
/**
* Get the submodel elements associated to the reference.
* Supported are two possible parents of submodel elements:
* <p>
* <ul>
* <li>{@link io.adminshell.aas.v3.model.Submodel}
* <li>{@link io.adminshell.aas.v3.model.SubmodelElementCollection}
* </ul>
* <p>
* If the semanticId is not null the submodel element list filtered by the semantic id
*
* @param reference to the submodel or submodel element collection
* @param semanticId of the submodel elements
* @return a list of the submodel elements associated to the parent reference
*/
public List<SubmodelElement> getSubmodelElements(Reference reference, Reference semanticId) throws ResourceNotFoundException {
if (reference == null) {
return null;
}
if (reference.getKeys() != null && reference.getKeys().size() > 0) {
List<SubmodelElement> submodelElements = null;
KeyElements lastKeyElementOfReference = reference.getKeys().get(reference.getKeys().size() - 1).getType();
if (lastKeyElementOfReference == KeyElements.SUBMODEL) {
Submodel submodel = AasUtils.resolve(reference, this.aasEnvironment, Submodel.class);
if (submodel == null) {
throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(reference)));
}
Submodel deepCopiedSubmodel = DeepCopyHelper.deepCopy(submodel, submodel.getClass());
submodelElements = deepCopiedSubmodel.getSubmodelElements();
} else if (lastKeyElementOfReference == KeyElements.SUBMODEL_ELEMENT_COLLECTION) {
SubmodelElementCollection submodelElementCollection = AasUtils.resolve(reference, this.aasEnvironment, SubmodelElementCollection.class);
if (submodelElementCollection == null) {
throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(reference)));
}
SubmodelElementCollection deepCopiedSubmodelElementCollection = DeepCopyHelper.deepCopy(submodelElementCollection, submodelElementCollection.getClass());
submodelElements = new ArrayList<>(deepCopiedSubmodelElementCollection.getValues());
}
if (semanticId != null) {
assert submodelElements != null;
submodelElements = submodelElements.stream().filter(x -> x.getSemanticId() != null && x.getSemanticId().equals(semanticId)).collect(Collectors.toList());
}
return submodelElements;
}
return null;
}
use of de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException in project FAAAST-Service by FraunhoferIOSB.
the class GetSubmodelElementByPathRequestHandler method process.
@Override
public GetSubmodelElementByPathResponse process(GetSubmodelElementByPathRequest request) {
GetSubmodelElementByPathResponse response = new GetSubmodelElementByPathResponse();
try {
Reference reference = ReferenceHelper.toReference(request.getPath(), request.getId(), Submodel.class);
SubmodelElement submodelElement = persistence.get(reference, request.getOutputModifier());
ElementValue oldValue = ElementValueMapper.toValue(submodelElement);
// read value from AssetConnection if one exist
// and update value in persistence if differs
ElementValue valueFromAssetConnection = readDataElementValueFromAssetConnection(reference);
if (valueFromAssetConnection != null && !Objects.equals(valueFromAssetConnection, oldValue)) {
submodelElement = ElementValueMapper.setValue(submodelElement, valueFromAssetConnection);
persistence.put(null, reference, submodelElement);
// TODO @Jens
// better publishValueChangeEventMessage(reference, oldValue, oldValue) ???
publishElementUpdateEventMessage(reference, submodelElement);
}
response.setPayload(submodelElement);
response.setStatusCode(StatusCode.Success);
publishElementReadEventMessage(reference, submodelElement);
} catch (ResourceNotFoundException ex) {
response.setStatusCode(StatusCode.ClientErrorResourceNotFound);
} catch (Exception ex) {
response.setStatusCode(StatusCode.ServerInternalError);
}
return response;
}
use of de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException in project FAAAST-Service by FraunhoferIOSB.
the class GetAllSubmodelElementsRequestHandler method process.
@Override
public GetAllSubmodelElementsResponse process(GetAllSubmodelElementsRequest request) {
GetAllSubmodelElementsResponse response = new GetAllSubmodelElementsResponse();
try {
Reference reference = ReferenceHelper.toReference(request.getId(), Submodel.class);
List<SubmodelElement> submodelElements = persistence.getSubmodelElements(reference, null, request.getOutputModifier());
readValueFromAssetConnectionAndUpdatePersistence(reference, submodelElements);
response.setPayload(submodelElements);
response.setStatusCode(StatusCode.Success);
if (submodelElements != null) {
submodelElements.forEach(x -> publishElementReadEventMessage(AasUtils.toReference(reference, x), x));
}
} catch (ResourceNotFoundException ex) {
response.setStatusCode(StatusCode.ClientErrorResourceNotFound);
} catch (Exception ex) {
response.setStatusCode(StatusCode.ServerInternalError);
}
return response;
}
Aggregations