Search in sources :

Example 26 with Operation

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

the class OpcUaEndpointTest method testPropertyChangeFromMessageBus.

/**
 * Test method to check whether the new value message from the MessageBus is
 * processed correctly
 *
 * @throws SecureIdentityException If the operation fails
 * @throws IOException If the operation fails
 * @throws ServiceException If the operation fails
 * @throws Exception If the operation fails
 */
@Test
public void testPropertyChangeFromMessageBus() throws SecureIdentityException, IOException, ServiceException, Exception {
    UaClient client = new UaClient(ENDPOINT_URL);
    client.setSecurityMode(SecurityMode.NONE);
    TestUtils.initialize(client);
    client.connect();
    System.out.println("testPropertyChangeFromMessageBus: client connected");
    aasns = client.getAddressSpace().getNamespaceTable().getIndex(VariableIds.AASAssetAdministrationShellType_AssetInformation_AssetKind.getNamespaceUri());
    List<RelativePath> relPath = new ArrayList<>();
    List<RelativePathElement> browsePath = new ArrayList<>();
    browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.AAS_ENVIRONMENT_NAME)));
    browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.SUBMODEL_TECH_DATA_NODE_NAME)));
    browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.MAX_ROTATION_SPEED_NAME)));
    browsePath.add(new RelativePathElement(Identifiers.HasProperty, false, true, new QualifiedName(aasns, TestDefines.PROPERTY_VALUE_NAME)));
    relPath.add(new RelativePath(browsePath.toArray(RelativePathElement[]::new)));
    BrowsePathResult[] bpres = client.getAddressSpace().translateBrowsePathsToNodeIds(Identifiers.ObjectsFolder, relPath.toArray(RelativePath[]::new));
    Assert.assertNotNull("testPropertyChangeFromMessageBus Browse Result Null", bpres);
    Assert.assertTrue("testPropertyChangeFromMessageBus Browse Result: size doesn't match", bpres.length == 1);
    Assert.assertTrue("testPropertyChangeFromMessageBus Browse Result Good", bpres[0].getStatusCode().isGood());
    BrowsePathTarget[] targets = bpres[0].getTargets();
    Assert.assertNotNull("testPropertyChangeFromMessageBus ValueType Null", targets);
    Assert.assertTrue("testPropertyChangeFromMessageBus ValueType empty", targets.length > 0);
    List<Key> keys = new ArrayList<>();
    keys.add(new DefaultKey.Builder().idType(KeyType.IRI).type(KeyElements.SUBMODEL).value(TestDefines.SUBMODEL_TECH_DATA_NAME).build());
    keys.add(new DefaultKey.Builder().idType(KeyType.ID_SHORT).type(KeyElements.PROPERTY).value(TestDefines.MAX_ROTATION_SPEED_NAME).build());
    Reference propRef = new DefaultReference.Builder().keys(keys).build();
    ValueChangeEventMessage valueChangeMessage = new ValueChangeEventMessage();
    valueChangeMessage.setElement(propRef);
    valueChangeMessage.setOldValue(PropertyValue.of(Datatype.INT, "5000"));
    Integer newValue = 5005;
    valueChangeMessage.setNewValue(PropertyValue.of(Datatype.INT, newValue.toString()));
    service.getMessageBus().publish(valueChangeMessage);
    Thread.sleep(DEFAULT_TIMEOUT);
    // read new value
    DataValue value = client.readValue(targets[0].getTargetId());
    Assert.assertEquals(StatusCode.GOOD, value.getStatusCode());
    Assert.assertEquals("new value not equal", newValue, value.getValue().getValue());
    System.out.println("disconnect client");
    client.disconnect();
}
Also used : RelativePath(com.prosysopc.ua.stack.core.RelativePath) BrowsePathResult(com.prosysopc.ua.stack.core.BrowsePathResult) DataValue(com.prosysopc.ua.stack.builtintypes.DataValue) Reference(io.adminshell.aas.v3.model.Reference) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) QualifiedName(com.prosysopc.ua.stack.builtintypes.QualifiedName) ArrayList(java.util.ArrayList) UaClient(com.prosysopc.ua.client.UaClient) RelativePathElement(com.prosysopc.ua.stack.core.RelativePathElement) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) Key(io.adminshell.aas.v3.model.Key) ValueChangeEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ValueChangeEventMessage) BrowsePathTarget(com.prosysopc.ua.stack.core.BrowsePathTarget) Test(org.junit.Test)

Example 27 with Operation

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

the class OpcUaEndpointTest method testAddProperty.

/**
 * Test method for adding a new property to an existing submodel.
 *
 * @throws SecureIdentityException If the operation fails
 * @throws IOException If the operation fails
 * @throws ServiceException If the operation fails
 * @throws Exception If the operation fails
 */
@Test
public void testAddProperty() throws SecureIdentityException, IOException, ServiceException, Exception {
    UaClient client = new UaClient(ENDPOINT_URL);
    client.setSecurityMode(SecurityMode.NONE);
    TestUtils.initialize(client);
    client.connect();
    System.out.println("testAddProperty: client connected");
    aasns = client.getAddressSpace().getNamespaceTable().getIndex(VariableIds.AASAssetAdministrationShellType_AssetInformation_AssetKind.getNamespaceUri());
    String propName = "NewProperty99";
    // make sure the element doesn't exist yet
    List<RelativePath> relPath = new ArrayList<>();
    List<RelativePathElement> browsePath = new ArrayList<>();
    browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.AAS_ENVIRONMENT_NAME)));
    browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, TestDefines.SUBMODEL_TECH_DATA_NODE_NAME)));
    browsePath.add(new RelativePathElement(Identifiers.HierarchicalReferences, false, true, new QualifiedName(aasns, propName)));
    browsePath.add(new RelativePathElement(Identifiers.HasProperty, false, true, new QualifiedName(aasns, TestDefines.PROPERTY_VALUE_NAME)));
    relPath.add(new RelativePath(browsePath.toArray(RelativePathElement[]::new)));
    BrowsePathResult[] bpres = client.getAddressSpace().translateBrowsePathsToNodeIds(Identifiers.ObjectsFolder, relPath.toArray(RelativePath[]::new));
    Assert.assertNotNull("testAddProperty Browse Result Null", bpres);
    Assert.assertTrue("testAddProperty Browse Result: size doesn't match", bpres.length == 1);
    Assert.assertTrue("testAddProperty Browse Result Bad", bpres[0].getStatusCode().isBad());
    // Send event to MessageBus
    ElementCreateEventMessage msg = new ElementCreateEventMessage();
    msg.setElement(new DefaultReference.Builder().key(new DefaultKey.Builder().idType(KeyType.IRI).type(KeyElements.SUBMODEL).value("http://i40.customer.com/type/1/1/7A7104BDAB57E184").build()).build());
    msg.setValue(new DefaultProperty.Builder().kind(ModelingKind.INSTANCE).idShort(propName).category("Variable").value("AZF45").valueType("string").build());
    service.getMessageBus().publish(msg);
    Thread.sleep(DEFAULT_TIMEOUT);
    // check that the element is there now
    bpres = client.getAddressSpace().translateBrowsePathsToNodeIds(Identifiers.ObjectsFolder, relPath.toArray(RelativePath[]::new));
    Assert.assertNotNull("testAddProperty Browse Result Null", bpres);
    Assert.assertTrue("testAddProperty Browse Result: size doesn't match", bpres.length == 1);
    Assert.assertTrue("testAddProperty Browse Result Good", bpres[0].getStatusCode().isGood());
    System.out.println("disconnect client");
    client.disconnect();
}
Also used : RelativePath(com.prosysopc.ua.stack.core.RelativePath) BrowsePathResult(com.prosysopc.ua.stack.core.BrowsePathResult) QualifiedName(com.prosysopc.ua.stack.builtintypes.QualifiedName) ArrayList(java.util.ArrayList) ByteString(com.prosysopc.ua.stack.builtintypes.ByteString) LangString(io.adminshell.aas.v3.model.LangString) DefaultProperty(io.adminshell.aas.v3.model.impl.DefaultProperty) UaClient(com.prosysopc.ua.client.UaClient) ElementCreateEventMessage(de.fraunhofer.iosb.ilt.faaast.service.model.messagebus.event.change.ElementCreateEventMessage) RelativePathElement(com.prosysopc.ua.stack.core.RelativePathElement) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) Test(org.junit.Test)

Example 28 with Operation

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

the class OpcUaEndpointTest method testSubmodelDoc.

/**
 * Tests the given Submodel Documentation.
 *
 * @param client The OPC UA Client.
 * @param submodelNode The desired Submodel
 * @throws ServiceException If the operation fails
 * @throws AddressSpaceException If the operation fails
 * @throws StatusException If the operation fails
 * @throws ServiceResultException If the operation fails
 * @throws InterruptedException Id a wait was interrupted
 */
private void testSubmodelDoc(UaClient client, NodeId submodelNode) throws ServiceException, AddressSpaceException, StatusException, ServiceResultException, InterruptedException {
    TestUtils.checkDisplayName(client, submodelNode, "Submodel:" + TestDefines.SUBMODEL_DOC_NODE_NAME);
    TestUtils.checkType(client, submodelNode, new NodeId(aasns, TestDefines.AAS_SUBMODEL_TYPE_ID));
    String submodelName = "SubmodelOperationalData";
    NodeId operatingManualNode = null;
    List<ReferenceDescription> refs = client.getAddressSpace().browse(submodelNode);
    Assert.assertNotNull("Browse " + submodelName + " Refs Null", refs);
    Assert.assertFalse("Browse " + submodelName + " Refs empty", refs.isEmpty());
    for (ReferenceDescription ref : refs) {
        NodeId nid = client.getAddressSpace().getNamespaceTable().toNodeId(ref.getNodeId());
        switch(ref.getBrowseName().getName()) {
            case TestDefines.OPERATING_MANUAL_NAME:
                operatingManualNode = nid;
                break;
            default:
                break;
        }
    }
    Assert.assertNotNull(submodelName + " OperatingManual Node not found", operatingManualNode);
    TestUtils.checkIdentificationNode(client, submodelNode, aasns, AASIdentifierTypeDataType.IRI, TestDefines.SUBMODEL_DOC_NAME);
    TestUtils.checkAdministrationNode(client, submodelNode, aasns, "11", "159");
    TestUtils.checkModelingKindNode(client, submodelNode, aasns, AASModelingKindDataType.Instance);
    TestUtils.checkCategoryNode(client, submodelNode, aasns, "");
    TestUtils.checkDataSpecificationNode(client, submodelNode, aasns);
    TestUtils.checkQualifierNode(client, submodelNode, aasns, new ArrayList<>());
    testOperatingManual(client, operatingManualNode);
}
Also used : ReferenceDescription(com.prosysopc.ua.stack.core.ReferenceDescription) NodeId(com.prosysopc.ua.stack.builtintypes.NodeId) ByteString(com.prosysopc.ua.stack.builtintypes.ByteString) LangString(io.adminshell.aas.v3.model.LangString)

Example 29 with Operation

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

the class OpcUaAssetConnection method registerOperationProvider.

/**
 * {@inheritDoc}
 *
 * @throws AssetConnectionException if nodeId could not be parsed
 * @throws AssetConnectionException if nodeId does not refer to a method
 *             node
 * @throws AssetConnectionException if parent node of nodeId could not be
 *             resolved
 * @throws AssetConnectionException if output variables are null or do
 *             contain any other type than
 *             {@link de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue}
 */
@Override
public void registerOperationProvider(Reference reference, OpcUaOperationProviderConfig providerConfig) throws AssetConnectionException {
    String baseErrorMessage = "error registering operation provider";
    final NodeId nodeId = parseNodeId(providerConfig.getNodeId());
    final UaNode node;
    try {
        node = client.getAddressSpace().getNode(nodeId);
    } catch (UaException e) {
        throw new AssetConnectionException(String.format("%s - could not resolve nodeId (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
    }
    if (!UaMethodNode.class.isAssignableFrom(node.getClass())) {
        throw new AssetConnectionException(String.format("%s - provided node must be a method (nodeId: %s", baseErrorMessage, providerConfig.getNodeId()));
    }
    final UaMethodNode methodNode = (UaMethodNode) node;
    final NodeId parentNodeId;
    try {
        parentNodeId = client.getAddressSpace().getNode(nodeId).browseNodes(AddressSpace.BrowseOptions.builder().setBrowseDirection(BrowseDirection.Inverse).build()).get(0).getNodeId();
    } catch (UaException e) {
        throw new AssetConnectionException(String.format("%s - could not resolve parent node (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
    }
    final Argument[] methodArguments;
    try {
        methodArguments = methodNode.readInputArgumentsAsync().get() != null ? methodNode.readInputArgumentsAsync().get() : new Argument[0];
    } catch (InterruptedException | ExecutionException e) {
        Thread.currentThread().interrupt();
        throw new AssetConnectionException(String.format("%s - could not read input arguments (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
    }
    final Argument[] methodOutputArguments;
    try {
        methodOutputArguments = methodNode.readOutputArgumentsAsync().get() != null ? methodNode.readOutputArgumentsAsync().get() : new Argument[0];
    } catch (InterruptedException | ExecutionException e) {
        Thread.currentThread().interrupt();
        throw new AssetConnectionException(String.format("%s - could not read ouput arguments (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
    }
    final OperationVariable[] outputVariables = serviceContext.getOperationOutputVariables(reference) != null ? serviceContext.getOperationOutputVariables(reference) : new OperationVariable[0];
    for (var outputVariable : outputVariables) {
        if (outputVariable == null) {
            throw new AssetConnectionException(String.format("%s - output variable must be non-null (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()));
        }
        SubmodelElement submodelElement = outputVariable.getValue();
        if (submodelElement == null) {
            throw new AssetConnectionException(String.format("%s - output variable must contain non-null submodel element (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()));
        }
        if (!Property.class.isAssignableFrom(submodelElement.getClass())) {
            throw new AssetConnectionException(String.format("%s - unsupported element type (nodeId: %s, element type: %s)", baseErrorMessage, submodelElement.getClass(), providerConfig.getNodeId()));
        }
    }
    this.operationProviders.put(reference, new AssetOperationProvider() {

        @Override
        public OperationVariable[] invoke(OperationVariable[] input, OperationVariable[] inoutput) throws AssetConnectionException {
            String baseErrorMessage = "error invoking operation on asset connection";
            Map<String, ElementValue> inputParameter = new HashMap<>();
            if (input != null) {
                try {
                    inputParameter = Stream.of(input).collect(Collectors.toMap(x -> x.getValue().getIdShort(), LambdaExceptionHelper.rethrowFunction(x -> ElementValueMapper.toValue(x.getValue()))));
                } catch (ValueMappingException e) {
                    throw new AssetConnectionException(String.format("%s - could not exract value of input parameters", baseErrorMessage), e);
                }
            }
            Map<String, ElementValue> inoutputParameter = new HashMap<>();
            if (inoutput != null) {
                try {
                    inoutputParameter = Stream.of(inoutput).collect(Collectors.toMap(x -> x.getValue().getIdShort(), LambdaExceptionHelper.rethrowFunction(x -> ElementValueMapper.toValue(x.getValue()))));
                } catch (ValueMappingException e) {
                    throw new AssetConnectionException(String.format("%s - could not exract value of inoutput parameters", baseErrorMessage), e);
                }
            }
            if (methodArguments.length != (inputParameter.size() + inoutputParameter.size())) {
                throw new AssetConnectionException(String.format("%s - argument count mismatch (expected: %d, provided input arguments: %d, provided inoutput arguments: %d)", baseErrorMessage, methodArguments.length, inputParameter.size(), inoutputParameter.size()));
            }
            Variant[] actualParameters = new Variant[methodArguments.length];
            for (int i = 0; i < methodArguments.length; i++) {
                String argumentName = methodArguments[i].getName();
                ElementValue parameterValue;
                if (inputParameter.containsKey(argumentName)) {
                    parameterValue = inputParameter.get(argumentName);
                } else if (inoutputParameter.containsKey(argumentName)) {
                    parameterValue = inoutputParameter.get(argumentName);
                } else {
                    throw new AssetConnectionException(String.format("%s - missing argument (argument name: %s)", baseErrorMessage, argumentName));
                }
                if (parameterValue == null) {
                    throw new AssetConnectionException(String.format("%s - parameter value must be non-null (argument name: %s)", baseErrorMessage, argumentName));
                }
                if (!PropertyValue.class.isAssignableFrom(parameterValue.getClass())) {
                    throw new AssetConnectionException(String.format("%s - currently only parameters of the Property are supported (argument name: %s, provided type: %s)", baseErrorMessage, argumentName, parameterValue.getClass()));
                }
                actualParameters[i] = valueConverter.convert(((PropertyValue) parameterValue).getValue(), methodArguments[i].getDataType());
            }
            CallMethodResult methodResult;
            try {
                methodResult = client.call(new CallMethodRequest(parentNodeId, nodeId, actualParameters)).get();
            } catch (InterruptedException | ExecutionException e) {
                Thread.currentThread().interrupt();
                throw new AssetConnectionException(String.format("%s - executing OPC UA method failed (nodeId: %s)", baseErrorMessage, providerConfig.getNodeId()), e);
            }
            OperationVariable[] result = new OperationVariable[outputVariables.length];
            for (int i = 0; i < methodOutputArguments.length; i++) {
                String argumentName = methodArguments[i].getName();
                for (int j = 0; j < outputVariables.length; j++) {
                    if (Objects.equals(argumentName, outputVariables[j].getValue().getIdShort())) {
                        SubmodelElement element = outputVariables[j].getValue();
                        Datatype targetType;
                        try {
                            targetType = ((PropertyValue) ElementValueMapper.toValue(element)).getValue().getDataType();
                        } catch (ValueMappingException e) {
                            throw new AssetConnectionException(String.format("%s - could not exract value of results variable with idShort '%s'", baseErrorMessage, element.getIdShort()), e);
                        }
                        TypedValue<?> newValue = valueConverter.convert(methodResult.getOutputArguments()[i], targetType);
                        Property newProperty = DeepCopyHelper.deepCopy(element, Property.class);
                        ElementValueMapper.setValue(newProperty, PropertyValue.builder().value(newValue).build());
                        result[j] = new DefaultOperationVariable.Builder().value(newProperty).build();
                    }
                }
                // update inoutput variable values
                if (inoutputParameter.containsKey(argumentName) && inoutput != null) {
                    // find in original array and set there
                    for (int j = 0; j < inoutput.length; j++) {
                        if (Objects.equals(argumentName, inoutput[j].getValue().getIdShort())) {
                            ElementValueMapper.setValue(inoutput[j].getValue(), new PropertyValue(valueConverter.convert(methodResult.getOutputArguments()[i], ((PropertyValue) inoutputParameter.get(argumentName)).getValue().getDataType())));
                        }
                    }
                }
            }
            return result;
        }
    });
}
Also used : SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) ValueConversionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.opcua.conversion.ValueConversionException) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue) VariableNode(org.eclipse.milo.opcua.sdk.core.nodes.VariableNode) LoggerFactory(org.slf4j.LoggerFactory) CallMethodResult(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodResult) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) ValueMappingException(de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException) NewDataListener(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener) Argument(org.eclipse.milo.opcua.stack.core.types.structured.Argument) ManagedDataItem(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) Property(io.adminshell.aas.v3.model.Property) Map(java.util.Map) AssetConnection(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnection) UaMethodNode(org.eclipse.milo.opcua.sdk.client.nodes.UaMethodNode) ManagedSubscription(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) BrowseDirection(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection) Reference(io.adminshell.aas.v3.model.Reference) Collectors(java.util.stream.Collectors) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) ConfigurationInitializationException(de.fraunhofer.iosb.ilt.faaast.service.exception.ConfigurationInitializationException) AasUtils(io.adminshell.aas.v3.dataformat.core.util.AasUtils) TypedValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.TypedValue) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) CoreConfig(de.fraunhofer.iosb.ilt.faaast.service.config.CoreConfig) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) Optional(java.util.Optional) ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) CallMethodRequest(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodRequest) DefaultOperationVariable(io.adminshell.aas.v3.model.impl.DefaultOperationVariable) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DeepCopyHelper(de.fraunhofer.iosb.ilt.faaast.service.util.DeepCopyHelper) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) AssetOperationProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) Logger(org.slf4j.Logger) ElementValueMapper(de.fraunhofer.iosb.ilt.faaast.service.model.value.mapper.ElementValueMapper) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) ValueConverter(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.opcua.conversion.ValueConverter) AssetSubscriptionProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetSubscriptionProvider) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException) LambdaExceptionHelper(de.fraunhofer.iosb.ilt.faaast.service.util.LambdaExceptionHelper) AssetValueProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetValueProvider) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) DefaultOperationVariable(io.adminshell.aas.v3.model.impl.DefaultOperationVariable) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) Argument(org.eclipse.milo.opcua.stack.core.types.structured.Argument) UaException(org.eclipse.milo.opcua.stack.core.UaException) ValueMappingException(de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) Datatype(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.Datatype) UaMethodNode(org.eclipse.milo.opcua.sdk.client.nodes.UaMethodNode) CallMethodResult(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodResult) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) ExecutionException(java.util.concurrent.ExecutionException) Property(io.adminshell.aas.v3.model.Property) TypedValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.primitive.TypedValue) PropertyValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.PropertyValue) CallMethodRequest(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodRequest) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue) ElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.ElementValue) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) AssetOperationProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider) Map(java.util.Map) HashMap(java.util.HashMap)

Example 30 with Operation

use of io.adminshell.aas.v3.model.Operation 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)

Aggregations

StatusException (com.prosysopc.ua.StatusException)39 ServiceException (com.prosysopc.ua.ServiceException)36 AddressSpaceException (com.prosysopc.ua.client.AddressSpaceException)36 ServiceResultException (com.prosysopc.ua.stack.common.ServiceResultException)36 MessageBusException (de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException)36 UaNodeFactoryException (com.prosysopc.ua.nodes.UaNodeFactoryException)35 DefaultReference (io.adminshell.aas.v3.model.impl.DefaultReference)34 QualifiedName (com.prosysopc.ua.stack.builtintypes.QualifiedName)29 Reference (io.adminshell.aas.v3.model.Reference)29 LangString (io.adminshell.aas.v3.model.LangString)28 ByteString (com.prosysopc.ua.stack.builtintypes.ByteString)26 NodeId (com.prosysopc.ua.stack.builtintypes.NodeId)26 ArrayList (java.util.ArrayList)22 UaQualifiedName (com.prosysopc.ua.UaQualifiedName)19 Test (org.junit.Test)19 ObjectData (de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.data.ObjectData)16 IOException (java.io.IOException)15 DefaultKey (io.adminshell.aas.v3.model.impl.DefaultKey)12 UaClient (com.prosysopc.ua.client.UaClient)10 BrowsePathResult (com.prosysopc.ua.stack.core.BrowsePathResult)10