Search in sources :

Example 6 with Asset

use of io.adminshell.aas.v3.model.Asset 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!");
        }
    }
}
Also used : Submodel(io.adminshell.aas.v3.model.Submodel) KeyElements(io.adminshell.aas.v3.model.KeyElements) Operation(io.adminshell.aas.v3.model.Operation) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) Reference(io.adminshell.aas.v3.model.Reference) AssetAdministrationShellEnvironment(io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) Collectors(java.util.stream.Collectors) AasUtils(io.adminshell.aas.v3.dataformat.core.util.AasUtils) Identifier(io.adminshell.aas.v3.model.Identifier) ArrayList(java.util.ArrayList) ReflectionHelper(io.adminshell.aas.v3.dataformat.core.ReflectionHelper) Key(io.adminshell.aas.v3.model.Key) List(java.util.List) Stream(java.util.stream.Stream) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) Referable(io.adminshell.aas.v3.model.Referable) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) KeyType(io.adminshell.aas.v3.model.KeyType) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) Submodel(io.adminshell.aas.v3.model.Submodel) Referable(io.adminshell.aas.v3.model.Referable) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) Operation(io.adminshell.aas.v3.model.Operation) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) Key(io.adminshell.aas.v3.model.Key) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey)

Example 7 with Asset

use of io.adminshell.aas.v3.model.Asset in project FAAAST-Service by FraunhoferIOSB.

the class IdentifiablePersistenceManager method remove.

/**
 * Remove an identifiable by its identifier.
 * Following identifiables are supported:
 * <ul>
 * <li>{@link io.adminshell.aas.v3.model.AssetAdministrationShell}
 * <li>{@link io.adminshell.aas.v3.model.Submodel}
 * <li>{@link io.adminshell.aas.v3.model.ConceptDescription}
 * <li>{@link io.adminshell.aas.v3.model.Asset}
 * </ul>
 *
 * @param id of the indetifiable which should be removed
 * @throws ResourceNotFoundException if there is no identifiable with such an identifer
 */
public void remove(Identifier id) throws ResourceNotFoundException {
    if (id == null || this.aasEnvironment == null) {
        return;
    }
    Predicate<Identifiable> removeFilter = x -> !x.getIdentification().getIdentifier().equalsIgnoreCase(id.getIdentifier());
    Identifiable identifiable = getIdentifiableById(id);
    if (identifiable == null) {
        throw new ResourceNotFoundException(String.format(ERROR_MSG_RESOURCE_NOT_FOUND_BY_ID, IdentifierHelper.asString(id)));
    }
    // TODO: use reflection?
    if (AssetAdministrationShell.class.isAssignableFrom(identifiable.getClass())) {
        List<AssetAdministrationShell> newAASList;
        newAASList = this.aasEnvironment.getAssetAdministrationShells().stream().filter(removeFilter).collect(Collectors.toList());
        this.aasEnvironment.setAssetAdministrationShells(newAASList);
    } else if (Submodel.class.isAssignableFrom(identifiable.getClass())) {
        List<Submodel> newSubmodelList;
        newSubmodelList = this.aasEnvironment.getSubmodels().stream().filter(removeFilter).collect(Collectors.toList());
        this.aasEnvironment.setSubmodels(newSubmodelList);
        Reference referenceOfIdentifiable = AasUtils.toReference(identifiable);
        this.aasEnvironment.getAssetAdministrationShells().forEach(x -> x.getSubmodels().remove(referenceOfIdentifiable));
    } else if (ConceptDescription.class.isAssignableFrom(identifiable.getClass())) {
        List<ConceptDescription> newConceptDescriptionList;
        newConceptDescriptionList = this.aasEnvironment.getConceptDescriptions().stream().filter(removeFilter).collect(Collectors.toList());
        this.aasEnvironment.setConceptDescriptions(newConceptDescriptionList);
    } else if (Asset.class.isAssignableFrom(identifiable.getClass())) {
        List<Asset> newAssetList;
        newAssetList = this.aasEnvironment.getAssets().stream().filter(removeFilter).collect(Collectors.toList());
        this.aasEnvironment.setAssets(newAssetList);
    }
}
Also used : GlobalAssetIdentification(de.fraunhofer.iosb.ilt.faaast.service.model.asset.GlobalAssetIdentification) ConceptDescription(io.adminshell.aas.v3.model.ConceptDescription) Predicate(java.util.function.Predicate) Reference(io.adminshell.aas.v3.model.Reference) SpecificAssetIdentification(de.fraunhofer.iosb.ilt.faaast.service.model.asset.SpecificAssetIdentification) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) AasUtils(io.adminshell.aas.v3.dataformat.core.util.AasUtils) Identifier(io.adminshell.aas.v3.model.Identifier) ArrayList(java.util.ArrayList) IdentifierHelper(de.fraunhofer.iosb.ilt.faaast.service.util.IdentifierHelper) ReferenceHelper(de.fraunhofer.iosb.ilt.faaast.service.util.ReferenceHelper) AssetIdentification(de.fraunhofer.iosb.ilt.faaast.service.model.asset.AssetIdentification) Asset(io.adminshell.aas.v3.model.Asset) Identifiable(io.adminshell.aas.v3.model.Identifiable) List(java.util.List) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) DeepCopyHelper(de.fraunhofer.iosb.ilt.faaast.service.util.DeepCopyHelper) EnvironmentHelper(de.fraunhofer.iosb.ilt.faaast.service.persistence.memory.util.EnvironmentHelper) AssetAdministrationShell(io.adminshell.aas.v3.model.AssetAdministrationShell) Submodel(io.adminshell.aas.v3.model.Submodel) Submodel(io.adminshell.aas.v3.model.Submodel) AssetAdministrationShell(io.adminshell.aas.v3.model.AssetAdministrationShell) Reference(io.adminshell.aas.v3.model.Reference) Asset(io.adminshell.aas.v3.model.Asset) ArrayList(java.util.ArrayList) List(java.util.List) ConceptDescription(io.adminshell.aas.v3.model.ConceptDescription) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) Identifiable(io.adminshell.aas.v3.model.Identifiable)

Example 8 with Asset

use of io.adminshell.aas.v3.model.Asset in project FAAAST-Service by FraunhoferIOSB.

the class RequestHandler method syncWithAsset.

/**
 * Check for each SubmodelElement if there is an AssetConnection.If yes read
 * the value from it and compare it to the current value.If they differ from
 * each other update the submodelelement with the value from the
 * AssetConnection.
 *
 * @param parent of the SubmodelElement List
 * @param submodelElements List of SubmodelElements which should be
 *            considered and updated
 * @throws ResourceNotFoundException if reference does not point to valid
 *             element
 * @throws AssetConnectionException if reading value from asset connection
 *             fails
 * @throws
 * de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException
 *             if mapping value read from asset connection fails
 * @throws
 * de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException if
 *             publishing fails
 */
protected void syncWithAsset(Reference parent, Collection<SubmodelElement> submodelElements) throws ResourceNotFoundException, AssetConnectionException, ValueMappingException, MessageBusException {
    if (parent == null || submodelElements == null) {
        return;
    }
    for (SubmodelElement submodelElement : submodelElements) {
        Reference reference = AasUtils.toReference(parent, submodelElement);
        Optional<DataElementValue> newValue = assetConnectionManager.readValue(reference);
        if (newValue.isPresent()) {
            ElementValue oldValue = ElementValueMapper.toValue(submodelElement);
            if (!Objects.equals(oldValue, newValue)) {
                submodelElement = persistence.put(null, reference, ElementValueMapper.setValue(submodelElement, newValue.get()));
                messageBus.publish(ElementUpdateEventMessage.builder().element(AasUtils.toReference(parent, submodelElement)).value(submodelElement).build());
            }
        } else if (SubmodelElementCollection.class.isAssignableFrom(submodelElement.getClass())) {
            syncWithAsset(reference, ((SubmodelElementCollection) submodelElement).getValues());
        }
    }
}
Also used : SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) Reference(io.adminshell.aas.v3.model.Reference) SubmodelElementCollection(io.adminshell.aas.v3.model.SubmodelElementCollection) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)

Example 9 with Asset

use of io.adminshell.aas.v3.model.Asset in project FAAAST-Service by FraunhoferIOSB.

the class AssetOperationProvider method invoke.

/**
 * Invokes as operation synchronously
 *
 * @param input input parameters
 * @param inoutput inoutput parameters, i.e. parameters that are passed as
 *            input to the operation but can be modified while execution
 * @return output variables of the operation
 * @throws
 * de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException
 *             when invoking operation on asset connection fails
 * @throws IllegalArgumentException if provided inoutput arguments do not
 *             match actual inoutput arguments
 */
public default OperationVariable[] invoke(OperationVariable[] input, OperationVariable[] inoutput) throws AssetConnectionException {
    final String BASE_ERROR_MSG = "inoutput argument mismatch";
    final AtomicReference<OperationVariable[]> result = new AtomicReference<>();
    final AtomicReference<OperationVariable[]> modifiedInoutput = new AtomicReference<>();
    CountDownLatch condition = new CountDownLatch(1);
    invokeAsync(input, inoutput, (x, y) -> {
        result.set(x);
        modifiedInoutput.set(y);
        condition.countDown();
    });
    try {
        condition.await();
    } catch (InterruptedException x) {
        Thread.currentThread().interrupt();
        throw new AssetConnectionException("invoking operation failed because of timeout", x);
    }
    if (inoutput == null && result.get() != null && result.get().length > 0) {
        throw new IllegalArgumentException(String.format("%s - provided: none, actual: %d", BASE_ERROR_MSG, result.get().length));
    }
    if (result.get() == null && inoutput != null && inoutput.length > 0) {
        throw new IllegalArgumentException(String.format("%s - provided: %d, actual: none", BASE_ERROR_MSG, inoutput.length));
    }
    if (inoutput != null) {
        if (inoutput.length != modifiedInoutput.get().length) {
            throw new IllegalArgumentException(String.format("%s - provided: %d, actual: %d", BASE_ERROR_MSG, inoutput.length, result.get().length));
        }
        for (int i = 0; i < inoutput.length; i++) {
            final OperationVariable variable = inoutput[i];
            inoutput[i] = Stream.of(result.get()).filter(x -> Objects.equals(x.getValue().getIdShort(), variable.getValue().getIdShort())).findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("%s - variable provided but not found in actual ouput (idShort: %s)", BASE_ERROR_MSG, variable.getValue().getIdShort())));
        }
    }
    return result.get();
}
Also used : Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) BiConsumer(java.util.function.BiConsumer) LambdaExceptionHelper(de.fraunhofer.iosb.ilt.faaast.service.util.LambdaExceptionHelper) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 10 with Asset

use of io.adminshell.aas.v3.model.Asset in project FAAAST-Service by FraunhoferIOSB.

the class AasServiceNodeManager method addAsset.

/**
 * Adds an Asset to the given Node.
 *
 * @param node The UA node in which the Asset should be created
 * @param asset The desired Asset
 * @throws StatusException If the operation fails
 */
private void addAsset(UaNode node, Asset asset) throws StatusException {
    if (node == null) {
        throw new IllegalArgumentException(NODE_NULL);
    } else if (asset == null) {
        throw new IllegalArgumentException("asset = null");
    }
    try {
        String name = asset.getIdShort();
        String displayName = "Asset:" + name;
        LOG.info("addAsset {}; to Node: {}", name, node);
        QualifiedName browseName = UaQualifiedName.from(opc.i4aas.ObjectTypeIds.AASAssetType.getNamespaceUri(), name).toQualifiedName(getNamespaceTable());
        NodeId nid = createNodeId(node, browseName);
        AASAssetType assetNode = createInstance(AASAssetType.class, nid, browseName, LocalizedText.english(displayName));
        addIdentifiable(assetNode, asset.getIdentification(), asset.getAdministration(), asset.getCategory());
        // DataSpecifications
        addEmbeddedDataSpecifications(assetNode, asset.getEmbeddedDataSpecifications());
        node.addComponent(assetNode);
        referableMap.put(AasUtils.toReference(asset), new ObjectData(asset, assetNode));
    } catch (Exception ex) {
        LOG.error("addAsset Exception", ex);
        throw ex;
    }
}
Also used : AASAssetType(opc.i4aas.AASAssetType) UaQualifiedName(com.prosysopc.ua.UaQualifiedName) QualifiedName(com.prosysopc.ua.stack.builtintypes.QualifiedName) NodeId(com.prosysopc.ua.stack.builtintypes.NodeId) 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) ServiceResultException(com.prosysopc.ua.stack.common.ServiceResultException) StatusException(com.prosysopc.ua.StatusException) MessageBusException(de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException) ServiceException(com.prosysopc.ua.ServiceException) UaNodeFactoryException(com.prosysopc.ua.nodes.UaNodeFactoryException) AddressSpaceException(com.prosysopc.ua.client.AddressSpaceException)

Aggregations

AssetAdministrationShell (io.adminshell.aas.v3.model.AssetAdministrationShell)6 Reference (io.adminshell.aas.v3.model.Reference)4 Submodel (io.adminshell.aas.v3.model.Submodel)4 SubmodelElement (io.adminshell.aas.v3.model.SubmodelElement)4 ServiceException (com.prosysopc.ua.ServiceException)3 StatusException (com.prosysopc.ua.StatusException)3 AddressSpaceException (com.prosysopc.ua.client.AddressSpaceException)3 UaNodeFactoryException (com.prosysopc.ua.nodes.UaNodeFactoryException)3 ServiceResultException (com.prosysopc.ua.stack.common.ServiceResultException)3 MessageBusException (de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException)3 AasUtils (io.adminshell.aas.v3.dataformat.core.util.AasUtils)3 AssetAdministrationShellEnvironment (io.adminshell.aas.v3.model.AssetAdministrationShellEnvironment)3 LangString (io.adminshell.aas.v3.model.LangString)3 DefaultAssetAdministrationShell (io.adminshell.aas.v3.model.impl.DefaultAssetAdministrationShell)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Test (org.junit.Test)3 ObjectData (de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.data.ObjectData)2 ResourceNotFoundException (de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException)2 DataElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)2