use of io.adminshell.aas.v3.model.File in project FAAAST-Service by FraunhoferIOSB.
the class AASEnvironmentFactory method guessDeserializerAndTryToParse.
private AssetAdministrationShellEnvironment guessDeserializerAndTryToParse(String envFilePath, String env) {
Deserializer approxDeserializer = null;
String fileEnding = envFilePath.split("\\.")[1];
if (!envFilePath.equalsIgnoreCase(Application.DEFAULT_AASENV_PATH)) {
LOGGER.debug("Looking for Deserializer for file ending '" + fileEnding + "'");
approxDeserializer = deserializer.getOrDefault(deserializer.keySet().stream().filter(x -> x.equalsIgnoreCase(fileEnding)).findFirst().orElseGet(null), null);
}
if (approxDeserializer != null) {
try {
LOGGER.debug("Try resolving with '" + approxDeserializer.getClass().getSimpleName() + "'");
AssetAdministrationShellEnvironment environment = approxDeserializer.read(env);
// but returns an empty AASEnvironment
if (!Objects.equals(environment, new DefaultAssetAdministrationShellEnvironment())) {
return environment;
}
} catch (Exception ignored) {
LOGGER.debug("Resolving with '" + approxDeserializer.getClass().getSimpleName() + "' was not successfull. Try other Deserializers.");
}
}
return null;
}
use of io.adminshell.aas.v3.model.File in project FAAAST-Service by FraunhoferIOSB.
the class AASEnvironmentFactory method getAASEnvironment.
/**
* Parses the content in the given file path to an
* {@link io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment}.
* Supported formats of the file:
* <p>
* <ul>
* <li>json
* <li>aml
* <li>xml
* <li>opcua nodeset (also as .xml)
* <li>rdf
* <li>json-ld
* </ul>
* The method retrieves the right deserializer and parses the content to an
* {@link io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment}.
*
* @param envFilePath of the file which contains the Asset Administration Shell Environment
* @return the parsed Asset Administration Shell Environment object
* @throws Exception
*/
public AssetAdministrationShellEnvironment getAASEnvironment(String envFilePath) throws Exception {
initDeserializer();
String env = getFileContent(envFilePath);
LOGGER.info("Try to resolve Asset Administration Shell Environment from file '" + envFilePath + "'");
AssetAdministrationShellEnvironment parsedEnvironment = guessDeserializerAndTryToParse(envFilePath, env);
if (parsedEnvironment != null) {
return parsedEnvironment;
}
// else try every deserializer
String formats = "";
for (Map.Entry<String, Deserializer> deserializer : deserializer.entrySet()) {
try {
LOGGER.debug("Try resolving with '" + deserializer.getValue().getClass().getSimpleName() + "'");
formats += "\t" + deserializer.getKey() + "\n";
return deserializer.getValue().read(env);
} catch (DeserializationException ex) {
}
}
throw new Exception("Could not deserialize content to an Asset Administration Shell Environment. Used File: " + envFilePath + "\nSupported Formats:\n" + formats);
}
use of io.adminshell.aas.v3.model.File 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.File in project FAAAST-Service by FraunhoferIOSB.
the class JsonDeserializerTest method compareValueList.
private void compareValueList(Map<SubmodelElement, File> input) throws DeserializationException, IOException {
List<Object> expected = input.keySet().stream().map(x -> ElementValueMapper.toValue(x)).collect(Collectors.toList());
TypeInfo typeInfo = TypeExtractor.extractTypeInfo(input.keySet());
List<ElementValue> actual = deserializer.readValueList(filesAsJsonArray(input), typeInfo);
Assert.assertEquals(expected, actual);
}
use of io.adminshell.aas.v3.model.File in project FAAAST-Service by FraunhoferIOSB.
the class AasServiceNodeManager method addAasFile.
/**
* Adds an AAS file to the given node.
*
* @param node The desired UA node
* @param aasFile The AAS file object
* @param submodel The corresponding Submodel as parent object of the data element
* @param parentRef The AAS reference to the parent node
* @param ordered Specifies whether the file should be added ordered (true) or unordered (false)
* @param nodeName The desired Name of the node. If this value is not set,
* the IdShort of the file is used.
* @throws StatusException If the operation fails
* @throws ServiceException If the operation fails
* @throws AddressSpaceException If the operation fails
* @throws ServiceResultException If the operation fails
*/
private void addAasFile(UaNode node, File aasFile, Submodel submodel, Reference parentRef, boolean ordered, String nodeName) throws StatusException, ServiceException, AddressSpaceException, ServiceResultException {
try {
if ((node != null) && (aasFile != null)) {
String name = aasFile.getIdShort();
if ((nodeName != null) && (!nodeName.isEmpty())) {
name = nodeName;
}
QualifiedName browseName = UaQualifiedName.from(opc.i4aas.ObjectTypeIds.AASFileType.getNamespaceUri(), name).toQualifiedName(getNamespaceTable());
NodeId nid = getDefaultNodeId();
AASFileType fileNode = createInstance(AASFileType.class, nid, browseName, LocalizedText.english(name));
addSubmodelElementBaseData(fileNode, aasFile);
// MimeType
if (!aasFile.getMimeType().isEmpty()) {
fileNode.setMimeType(aasFile.getMimeType());
}
// Value
if (aasFile.getValue() != null) {
if (fileNode.getValueNode() == null) {
addFileValueNode(fileNode);
}
fileNode.setValue(aasFile.getValue());
if (!aasFile.getValue().isEmpty()) {
java.io.File f = new java.io.File(aasFile.getValue());
if (!f.exists()) {
logger.warn("addAasFile: File '" + f.getAbsolutePath() + "' does not exist!");
} else {
// File Object: include only when the file exists
QualifiedName fileBrowseName = UaQualifiedName.from(opc.i4aas.ObjectTypeIds.AASFileType.getNamespaceUri(), AASFileType.FILE).toQualifiedName(getNamespaceTable());
NodeId fileId = new NodeId(getNamespaceIndex(), fileNode.getNodeId().getValue().toString() + "." + AASFileType.FILE);
FileTypeNode fileType = createInstance(FileTypeNode.class, fileId, fileBrowseName, LocalizedText.english(AASFileType.FILE));
fileType.setFile(new java.io.File(aasFile.getValue()));
fileType.setWritable(false);
fileType.setUserWritable(false);
if (fileType.getNodeVersion() != null) {
fileType.getNodeVersion().setDescription(new LocalizedText("", ""));
}
fileNode.addReference(fileType, Identifiers.HasAddIn, false);
}
}
}
if (ordered) {
node.addReference(fileNode, Identifiers.HasOrderedComponent, false);
} else {
node.addComponent(fileNode);
}
if (parentRef != null) {
Reference fileRef = AasUtils.toReference(parentRef, aasFile);
referableMap.put(fileRef, new ObjectData(aasFile, fileNode, submodel));
}
}
} catch (Throwable ex) {
logger.error("addAasFile Exception", ex);
throw ex;
}
}
Aggregations