Search in sources :

Example 1 with File

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;
}
Also used : DefaultAssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment) JsonDeserializer(io.adminshell.aas.v3.dataformat.json.JsonDeserializer) AmlDeserializer(io.adminshell.aas.v3.dataformat.aml.AmlDeserializer) Deserializer(io.adminshell.aas.v3.dataformat.Deserializer) I4AASDeserializer(io.adminshell.aas.v3.dataformat.i4aas.I4AASDeserializer) XmlDeserializer(io.adminshell.aas.v3.dataformat.xml.XmlDeserializer) AssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment) DefaultAssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment) DeserializationException(io.adminshell.aas.v3.dataformat.DeserializationException) IOException(java.io.IOException)

Example 2 with File

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);
}
Also used : JsonDeserializer(io.adminshell.aas.v3.dataformat.json.JsonDeserializer) AmlDeserializer(io.adminshell.aas.v3.dataformat.aml.AmlDeserializer) Deserializer(io.adminshell.aas.v3.dataformat.Deserializer) I4AASDeserializer(io.adminshell.aas.v3.dataformat.i4aas.I4AASDeserializer) XmlDeserializer(io.adminshell.aas.v3.dataformat.xml.XmlDeserializer) HashMap(java.util.HashMap) Map(java.util.Map) AssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment) DefaultAssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment) DeserializationException(io.adminshell.aas.v3.dataformat.DeserializationException) DeserializationException(io.adminshell.aas.v3.dataformat.DeserializationException) IOException(java.io.IOException)

Example 3 with File

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;
    }
}
Also used : AASEntityType(opc.i4aas.AASEntityType) AASRangeType(opc.i4aas.AASRangeType) AASBlobType(opc.i4aas.AASBlobType) AASPropertyType(opc.i4aas.AASPropertyType) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) AASOperationType(opc.i4aas.AASOperationType) AnnotatedRelationshipElement(io.adminshell.aas.v3.model.AnnotatedRelationshipElement) AASRelationshipElementType(opc.i4aas.AASRelationshipElementType) DataElement(io.adminshell.aas.v3.model.DataElement) AASMultiLanguagePropertyType(opc.i4aas.AASMultiLanguagePropertyType) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) AASAnnotatedRelationshipElementType(opc.i4aas.AASAnnotatedRelationshipElementType) NodeId(com.prosysopc.ua.stack.builtintypes.NodeId) AASReferenceElementType(opc.i4aas.AASReferenceElementType)

Example 4 with File

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);
}
Also used : SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) TypeExtractor(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeExtractor) ElementValueMapper(de.fraunhofer.iosb.ilt.faaast.service.model.value.mapper.ElementValueMapper) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) IOException(java.io.IOException) Test(org.junit.Test) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Level(java.util.logging.Level) List(java.util.List) DeserializationException(de.fraunhofer.iosb.ilt.faaast.service.dataformat.DeserializationException) RangeValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.RangeValue) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) PropertyValues(de.fraunhofer.iosb.ilt.faaast.service.serialization.json.fixture.PropertyValues) Map(java.util.Map) JsonDeserializer(de.fraunhofer.iosb.ilt.faaast.service.dataformat.json.JsonDeserializer) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) Assert(org.junit.Assert) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo)

Example 5 with File

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;
    }
}
Also used : AASFileType(opc.i4aas.AASFileType) Reference(io.adminshell.aas.v3.model.Reference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) UaQualifiedName(com.prosysopc.ua.UaQualifiedName) QualifiedName(com.prosysopc.ua.stack.builtintypes.QualifiedName) ObjectData(de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.data.ObjectData) ByteString(com.prosysopc.ua.stack.builtintypes.ByteString) LangString(io.adminshell.aas.v3.model.LangString) LocalizedText(com.prosysopc.ua.stack.builtintypes.LocalizedText) NodeId(com.prosysopc.ua.stack.builtintypes.NodeId) FileTypeNode(com.prosysopc.ua.types.opcua.server.FileTypeNode) File(io.adminshell.aas.v3.model.File)

Aggregations

IOException (java.io.IOException)5 NodeId (com.prosysopc.ua.stack.builtintypes.NodeId)4 SubmodelElement (io.adminshell.aas.v3.model.SubmodelElement)4 File (java.io.File)4 Map (java.util.Map)4 ByteString (com.prosysopc.ua.stack.builtintypes.ByteString)3 DeserializationException (de.fraunhofer.iosb.ilt.faaast.service.dataformat.DeserializationException)3 JsonDeserializer (de.fraunhofer.iosb.ilt.faaast.service.dataformat.json.JsonDeserializer)3 ElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue)3 PropertyValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue)3 RangeValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.RangeValue)3 ElementValueMapper (de.fraunhofer.iosb.ilt.faaast.service.model.value.mapper.ElementValueMapper)3 Datatype (de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype)3 PropertyValues (de.fraunhofer.iosb.ilt.faaast.service.serialization.json.fixture.PropertyValues)3 TypeExtractor (de.fraunhofer.iosb.ilt.faaast.service.typing.TypeExtractor)3 TypeInfo (de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo)3 AssetAdministrationShellEnvironment (io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment)3 LangString (io.adminshell.aas.v3.model.LangString)3 DefaultAssetAdministrationShellEnvironment (io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShellEnvironment)3 FileNotFoundException (java.io.FileNotFoundException)3