use of io.adminshell.aas.v3.model.Referable in project FAAAST-Service by FraunhoferIOSB.
the class ReferenceHelper method completeReferenceWithProperKeyElements.
/**
* Browse the keys of a reference and try to find the referenced element in the
* asset administration shell environment to set the right {@link io.adminshell.aas.v3.model.KeyElements}
* of the key.
* All key types must be null or SUBMODEL_ELEMENT.
*
* @param reference with keys which should be completed
* @param env the asset administration shell environment which contains the referenced elements
* @throws ResourceNotFoundException if an element referenced by a key could not be found
*/
public static void completeReferenceWithProperKeyElements(Reference reference, AssetAdministrationShellEnvironment env) throws ResourceNotFoundException {
if (reference == null) {
return;
}
List<Key> keys = reference.getKeys();
if (keys.stream().allMatch(x -> x.getType() != null && x.getType() != KeyElements.SUBMODEL_ELEMENT)) {
return;
}
final Referable[] parent = { null };
for (Key k : keys) {
if (env.getAssetAdministrationShells().stream().anyMatch(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue()))) {
k.setType(KeyElements.ASSET_ADMINISTRATION_SHELL);
continue;
}
env.getSubmodels().forEach(x -> {
if (x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(KeyElements.SUBMODEL);
parent[0] = x;
}
});
if (k.getType() != null && k.getType() != KeyElements.SUBMODEL_ELEMENT) {
continue;
}
if (env.getConceptDescriptions().stream().anyMatch(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue()))) {
k.setType(KeyElements.CONCEPT_DESCRIPTION);
continue;
}
if (env.getAssets().stream().anyMatch(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue()))) {
k.setType(KeyElements.ASSET);
continue;
}
if (parent[0] != null && Submodel.class.isAssignableFrom(parent[0].getClass())) {
Submodel submodel = (Submodel) parent[0];
submodel.getSubmodelElements().forEach(y -> {
if (y.getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(AasUtils.referableToKeyType(y));
parent[0] = y;
}
});
} else if (SubmodelElementCollection.class.isAssignableFrom(parent[0].getClass())) {
((SubmodelElementCollection) parent[0]).getValues().forEach(x -> {
if (x.getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(AasUtils.referableToKeyType(x));
parent[0] = x;
}
});
} else if (Operation.class.isAssignableFrom(parent[0].getClass())) {
Operation operation = (Operation) parent[0];
Stream.concat(Stream.concat(operation.getInoutputVariables().stream(), operation.getInputVariables().stream()), operation.getOutputVariables().stream()).forEach(x -> {
if (x.getValue().getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(AasUtils.referableToKeyType(x.getValue()));
parent[0] = x.getValue();
}
});
}
if (k.getType() == null) {
throw new ResourceNotFoundException("Resource with ID " + k.getValue() + " was not found!");
}
}
}
use of io.adminshell.aas.v3.model.Referable in project FAAAST-Service by FraunhoferIOSB.
the class AasServiceNodeManager method elementCreated.
/**
* Handles an elementCreated event.
*
* @param element Reference to the created element.
* @param value The element that was added.
* @throws StatusException If the operation fails
* @throws ServiceResultException If the operation fails
* @throws ServiceException If the operation fails
* @throws AddressSpaceException If the operation fails
*/
private void elementCreated(Reference element, Referable value) throws StatusException, ServiceResultException, ServiceException, AddressSpaceException {
if (element == null) {
throw new IllegalArgumentException("element is null");
} else if (value == null) {
throw new IllegalArgumentException("value is null");
}
try {
logger.debug("elementCreated called. Reference " + AasUtils.asString(element));
// The element is the parent object where the value is added
ObjectData parent = null;
if (referableMap.containsKey(element)) {
parent = referableMap.get(element);
} else {
logger.info("elementCreated: element not found in referableMap: " + AasUtils.asString(element));
}
if (value instanceof ConceptDescription) {
addConceptDescriptions(List.of((ConceptDescription) value));
} else if (value instanceof Asset) {
addAsset(aasEnvironmentNode, (Asset) value);
} else if (value instanceof Submodel) {
addSubmodel(aasEnvironmentNode, (Submodel) value);
} else if (value instanceof AssetAdministrationShell) {
addAssetAdministrationShell((AssetAdministrationShell) value);
} else if (parent != null) {
if (value instanceof EmbeddedDataSpecification) {
if (parent.getNode() instanceof AASAssetAdministrationShellType) {
addEmbeddedDataSpecifications((AASAssetAdministrationShellType) parent.getNode(), List.of((EmbeddedDataSpecification) value));
} else if (parent.getNode() instanceof AASSubmodelType) {
addEmbeddedDataSpecifications((AASSubmodelType) parent.getNode(), List.of((EmbeddedDataSpecification) value));
} else if (parent.getNode() instanceof AASSubmodelElementType) {
addEmbeddedDataSpecifications((AASSubmodelElementType) parent.getNode(), List.of((EmbeddedDataSpecification) value));
} else if (parent.getNode() instanceof AASAssetType) {
addEmbeddedDataSpecifications((AASAssetType) parent.getNode(), List.of((EmbeddedDataSpecification) value));
} else {
logger.warn("elementCreated: EmbeddedDataSpecification parent class not found");
}
} else if (value instanceof Constraint) {
if (parent.getNode() instanceof AASSubmodelType) {
addQualifiers(((AASSubmodelType) parent.getNode()).getQualifierNode(), List.of((Constraint) value));
} else if (parent.getNode() instanceof AASSubmodelElementType) {
addQualifiers(((AASSubmodelElementType) parent.getNode()).getQualifierNode(), List.of((Constraint) value));
} else {
logger.warn("elementCreated: Constraint parent class not found");
}
} else if (value instanceof SubmodelElement) {
if (parent.getNode() instanceof AASSubmodelType) {
logger.info("elementCreated: call addSubmodelElements");
addSubmodelElements(parent.getNode(), List.of((SubmodelElement) value), (Submodel) parent.getReferable(), element);
} else if (parent.getNode() instanceof AASSubmodelElementType) {
logger.info("elementCreated: call addSubmodelElements");
addSubmodelElements(parent.getNode(), List.of((SubmodelElement) value), parent.getSubmodel(), element);
} else {
logger.warn("elementCreated: SubmodelElement parent class not found: " + parent.getNode().getNodeId().toString() + "; " + parent.getNode());
}
}
} else {
logger.warn("elementCreated: element not found: " + AasUtils.asString(element));
}
} catch (Throwable ex) {
logger.error("elementCreated Exception", ex);
throw ex;
}
}
use of io.adminshell.aas.v3.model.Referable in project FAAAST-Service by FraunhoferIOSB.
the class AasServiceNodeManager method doRemoveFromMaps.
/**
* Removes the given SubmodelElement from the maps.
*
* @param element The desired SubmodelElement
* @param reference The reference to the desired SubmodelElement
* @param referable The corresponding referable
*/
private void doRemoveFromMaps(AASSubmodelElementType element, Reference reference, Referable referable) {
try {
logger.debug("doRemoveFromMaps: remove SubmodelElement " + AasUtils.asString(reference));
if (submodelElementOpcUAMap.containsKey(reference)) {
submodelElementOpcUAMap.remove(reference);
logger.debug("doRemoveFromMaps: remove SubmodelElement from submodelElementOpcUAMap: " + AasUtils.asString(reference));
}
if (element instanceof AASPropertyType) {
AASPropertyType prop = (AASPropertyType) element;
if (submodelElementAasMap.containsKey(prop.getValueNode().getNodeId())) {
submodelElementAasMap.remove(prop.getValueNode().getNodeId());
logger.debug("doRemoveFromMaps: remove Property NodeId " + prop.getValueNode().getNodeId());
}
} else if (element instanceof AASRangeType) {
AASRangeType range = (AASRangeType) element;
if (submodelElementAasMap.containsKey(range.getMinNode().getNodeId())) {
submodelElementAasMap.remove(range.getMinNode().getNodeId());
logger.debug("doRemoveFromMaps: remove Range Min NodeId " + range.getMinNode().getNodeId());
}
if (submodelElementAasMap.containsKey(range.getMaxNode().getNodeId())) {
submodelElementAasMap.remove(range.getMaxNode().getNodeId());
logger.debug("doRemoveFromMaps: remove Range Max NodeId " + range.getMaxNode().getNodeId());
}
} else if (element instanceof AASOperationType) {
AASOperationType oper = (AASOperationType) element;
if (submodelElementAasMap.containsKey(oper.getOperationNode().getNodeId())) {
submodelElementAasMap.remove(oper.getOperationNode().getNodeId());
logger.debug("doRemoveFromMaps: remove Operation NodeId " + oper.getOperationNode().getNodeId());
}
} else if (element instanceof AASBlobType) {
AASBlobType blob = (AASBlobType) element;
if (submodelElementAasMap.containsKey(blob.getValueNode().getNodeId())) {
submodelElementAasMap.remove(blob.getValueNode().getNodeId());
logger.debug("doRemoveFromMaps: remove Blob NodeId " + blob.getValueNode().getNodeId());
}
} else if (element instanceof AASMultiLanguagePropertyType) {
AASMultiLanguagePropertyType mlp = (AASMultiLanguagePropertyType) element;
if (submodelElementAasMap.containsKey(mlp.getValueNode().getNodeId())) {
submodelElementAasMap.remove(mlp.getValueNode().getNodeId());
logger.debug("doRemoveFromMaps: remove AASMultiLanguageProperty NodeId " + mlp.getValueNode().getNodeId());
}
} else if (element instanceof AASReferenceElementType) {
AASReferenceElementType refElem = (AASReferenceElementType) element;
NodeId nid = refElem.getValueNode().getKeysNode().getNodeId();
if (submodelElementAasMap.containsKey(nid)) {
submodelElementAasMap.remove(nid);
logger.debug("doRemoveFromMaps: remove AASReferenceElement NodeId " + nid);
}
} else if (element instanceof AASRelationshipElementType) {
AASRelationshipElementType relElem = (AASRelationshipElementType) element;
NodeId nid = relElem.getFirstNode().getKeysNode().getNodeId();
if (submodelElementAasMap.containsKey(nid)) {
submodelElementAasMap.remove(nid);
logger.debug("doRemoveFromMaps: remove AASRelationshipElement First NodeId " + nid);
}
nid = relElem.getSecondNode().getKeysNode().getNodeId();
if (submodelElementAasMap.containsKey(nid)) {
submodelElementAasMap.remove(nid);
logger.debug("doRemoveFromMaps: remove AASRelationshipElement Second NodeId " + nid);
}
if (relElem instanceof AASAnnotatedRelationshipElementType) {
if (referable instanceof AnnotatedRelationshipElement) {
AnnotatedRelationshipElement annRelElem = (AnnotatedRelationshipElement) referable;
for (DataElement de : annRelElem.getAnnotations()) {
doRemoveFromMaps(reference, de);
}
}
}
} else if (element instanceof AASEntityType) {
AASEntityType ent = (AASEntityType) element;
if ((ent.getGlobalAssetIdNode() != null) && (ent.getGlobalAssetIdNode().getKeysNode() != null)) {
NodeId nid = ent.getGlobalAssetIdNode().getKeysNode().getNodeId();
if (submodelElementAasMap.containsKey(nid)) {
submodelElementAasMap.remove(nid);
logger.debug("doRemoveFromMaps: remove Entity GlobalAssetId NodeId " + nid);
}
}
if (submodelElementAasMap.containsKey(ent.getEntityTypeNode().getNodeId())) {
submodelElementAasMap.remove(ent.getEntityTypeNode().getNodeId());
logger.debug("doRemoveFromMaps: remove Entity EntityType NodeId " + ent.getEntityTypeNode().getNodeId());
}
} else if (referable instanceof SubmodelElementCollection) {
SubmodelElementCollection sec = (SubmodelElementCollection) referable;
for (SubmodelElement se : sec.getValues()) {
doRemoveFromMaps(reference, se);
}
}
// Capability and File are currently not relevant here
} catch (Throwable ex) {
logger.error("doRemoveFromMaps Exception", ex);
throw ex;
}
}
use of io.adminshell.aas.v3.model.Referable in project FAAAST-Service by FraunhoferIOSB.
the class ReferablePersistenceManager method remove.
/**
* Remove a {@link io.adminshell.aas.v3.model.Referable}
*
* @param reference of the referable which should be removed
*/
public void remove(Reference reference) throws ResourceNotFoundException {
if (reference == null) {
return;
}
if (reference.getKeys() != null && reference.getKeys().size() > 0) {
KeyElements lastKeyElementOfReference = reference.getKeys().get(reference.getKeys().size() - 1).getType();
Class clazz = AasUtils.keyTypeToClass(lastKeyElementOfReference);
if (Identifiable.class.isAssignableFrom(clazz)) {
// TODO: Build Identifier and forward remove reuquest to remove(Identifier id)
return;
}
if (SubmodelElement.class.isAssignableFrom(clazz)) {
clazz = SubmodelElement.class;
}
Referable referable = AasUtils.resolve(reference, this.aasEnvironment);
if (referable == null) {
throw new ResourceNotFoundException(String.format("Resource not found with reference {}", AasUtils.asString(reference)));
}
if (reference.getKeys().size() > 1) {
Reference parent = new DefaultReference.Builder().keys(reference.getKeys().subList(0, reference.getKeys().size() - 1)).build();
Referable parentReferable = AasUtils.resolve(parent, this.aasEnvironment);
Method method = EnvironmentHelper.getGetReferableListMethod(clazz, parentReferable);
if (method != null) {
try {
List<Referable> referableList = (List<Referable>) method.invoke(parentReferable);
referableList.remove(referable);
} catch (InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
use of io.adminshell.aas.v3.model.Referable in project FAAAST-Service by FraunhoferIOSB.
the class Util method setUpEventCheck.
public static void setUpEventCheck(Referable expected, Class<? extends EventMessage> clazz, Supplier<?> call) {
AtomicBoolean fired = new AtomicBoolean(false);
SubscriptionId subscriptionId = IntegrationTestHttpEndpoint.messageBus.subscribe(SubscriptionInfo.create(clazz, x -> {
if (ElementReadEventMessage.class.isAssignableFrom(x.getClass())) {
Assert.assertEquals(expected, ((ElementReadEventMessage) x).getValue());
fired.set(true);
}
if (ElementCreateEventMessage.class.isAssignableFrom(x.getClass())) {
Assert.assertEquals(expected, ((ElementCreateEventMessage) x).getValue());
fired.set(true);
}
if (ElementUpdateEventMessage.class.isAssignableFrom(x.getClass())) {
Assert.assertEquals(expected, ((ElementUpdateEventMessage) x).getValue());
fired.set(true);
}
if (ElementDeleteEventMessage.class.isAssignableFrom(x.getClass())) {
Assert.assertEquals(expected, ((ElementDeleteEventMessage) x).getValue());
fired.set(true);
}
}));
call.get();
Assert.assertTrue(fired.get());
IntegrationTestHttpEndpoint.messageBus.unsubscribe(subscriptionId);
}
Aggregations