Search in sources :

Example 6 with OperationVariable

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

the class OpcUaEndpointFullTest method startTest.

/**
 * Initialize and start the test.
 *
 * @throws Exception If the operation fails
 */
@BeforeClass
public static void startTest() throws Exception {
    CoreConfig coreConfig = new CoreConfig();
    OpcUaEndpointConfig config = new OpcUaEndpointConfig();
    config.setTcpPort(OPC_TCP_PORT);
    config.setSecondsTillShutdown(0);
    AssetConnection assetConnection = new TestAssetConnection();
    // register Test Operation
    List<Key> keys = new ArrayList<>();
    keys.add(new DefaultKey.Builder().type(KeyElements.SUBMODEL).idType(KeyType.IRI).value("https://acplt.org/Test_Submodel3").build());
    keys.add(new DefaultKey.Builder().type(KeyElements.OPERATION).idType(KeyType.ID_SHORT).value("ExampleOperation").build());
    Reference ref = new DefaultReference.Builder().keys(keys).build();
    List<OperationVariable> outputArgs = new ArrayList<>();
    outputArgs.add(new DefaultOperationVariable.Builder().value(new DefaultProperty.Builder().idShort("Test Output 1").valueType("string").value("XYZ1").build()).build());
    assetConnection.registerOperationProvider(ref, new TestOperationProviderConfig(outputArgs));
    endpoint = new OpcUaEndpoint();
    service = new TestService(endpoint, assetConnection, true);
    endpoint.init(coreConfig, config, service);
    service.start();
}
Also used : TestAssetConnection(de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.helper.assetconnection.TestAssetConnection) DefaultOperationVariable(io.adminshell.aas.v3.model.impl.DefaultOperationVariable) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) TestOperationProviderConfig(de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.helper.assetconnection.TestOperationProviderConfig) CoreConfig(de.fraunhofer.iosb.ilt.faaast.service.config.CoreConfig) TestService(de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.helper.TestService) Reference(io.adminshell.aas.v3.model.Reference) DefaultReference(io.adminshell.aas.v3.model.impl.DefaultReference) ArrayList(java.util.ArrayList) DefaultProperty(io.adminshell.aas.v3.model.impl.DefaultProperty) AssetConnection(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnection) TestAssetConnection(de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.helper.assetconnection.TestAssetConnection) DefaultOperationVariable(io.adminshell.aas.v3.model.impl.DefaultOperationVariable) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) DefaultKey(io.adminshell.aas.v3.model.impl.DefaultKey) Key(io.adminshell.aas.v3.model.Key) BeforeClass(org.junit.BeforeClass)

Example 7 with OperationVariable

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

the class ValueConverter method setOutputArguments.

/**
 * Sets the output arguments for an operation from the given output variables.
 *
 * @param outputVariables The desired output variables
 * @param outputArguments The desired output arguments
 * @throws StatusException If the operation fails
 */
public static void setOutputArguments(List<OperationVariable> outputVariables, Variant[] outputArguments) throws StatusException {
    if (outputArguments.length != outputVariables.size()) {
        throw new StatusException(StatusCodes.Bad_InvalidArgument);
    } else {
        for (int i = 0; i < outputVariables.size(); i++) {
            SubmodelElement smelem = outputVariables.get(i).getValue();
            SubmodelElementData.Type type;
            if (smelem instanceof Property) {
                type = SubmodelElementData.Type.PROPERTY_VALUE;
            } else {
                throw new StatusException(StatusCodes.Bad_InvalidArgument);
            }
            outputArguments[i] = getSubmodelElementValue(smelem, type);
        }
    }
}
Also used : StatusException(com.prosysopc.ua.StatusException) SubmodelElement(io.adminshell.aas.v3.model.SubmodelElement) Property(io.adminshell.aas.v3.model.Property) MultiLanguageProperty(io.adminshell.aas.v3.model.MultiLanguageProperty) SubmodelElementData(de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.data.SubmodelElementData)

Example 8 with OperationVariable

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

the class OpcUaAssetConnectionTest method testInvokeOperation.

private void testInvokeOperation(String nodeId, boolean sync, Map<String, PropertyValue> input, Map<String, PropertyValue> inoutput, Map<String, PropertyValue> expectedInoutput, Map<String, PropertyValue> expectedOutput) throws AssetConnectionException, InterruptedException, ConfigurationInitializationException {
    Reference reference = AasUtils.parseReference("(Property)[ID_SHORT]Temperature");
    OpcUaAssetConnectionConfig config = OpcUaAssetConnectionConfig.builder().host(serverUrl).operationProvider(reference, OpcUaOperationProviderConfig.builder().nodeId(nodeId).build()).build();
    OperationVariable[] inputVariables = input == null ? new OperationVariable[0] : input.entrySet().stream().map(x -> {
        Property property = new DefaultProperty.Builder().idShort(x.getKey()).build();
        ElementValueMapper.setValue(property, x.getValue());
        return new DefaultOperationVariable.Builder().value(property).build();
    }).toArray(OperationVariable[]::new);
    OperationVariable[] inoutputVariables = inoutput == null ? new OperationVariable[0] : inoutput.entrySet().stream().map(x -> {
        Property property = new DefaultProperty.Builder().idShort(x.getKey()).build();
        ElementValueMapper.setValue(property, x.getValue());
        return new DefaultOperationVariable.Builder().value(property).build();
    }).toArray(OperationVariable[]::new);
    OperationVariable[] expectedInOut = expectedInoutput == null ? new OperationVariable[0] : expectedInoutput.entrySet().stream().map(x -> {
        Property property = new DefaultProperty.Builder().idShort(x.getKey()).build();
        ElementValueMapper.setValue(property, x.getValue());
        return new DefaultOperationVariable.Builder().value(property).build();
    }).toArray(OperationVariable[]::new);
    OperationVariable[] expectedOut = expectedOutput == null ? new OperationVariable[0] : expectedOutput.entrySet().stream().map(x -> {
        Property property = new DefaultProperty.Builder().idShort(x.getKey()).build();
        ElementValueMapper.setValue(property, x.getValue());
        return new DefaultOperationVariable.Builder().value(property).build();
    }).toArray(OperationVariable[]::new);
    ServiceContext serviceContext = mock(ServiceContext.class);
    doReturn(expectedOut).when(serviceContext).getOperationOutputVariables(reference);
    OpcUaAssetConnection connection = new OpcUaAssetConnection(CoreConfig.builder().build(), config, serviceContext);
    OperationVariable[] actual;
    if (sync) {
        actual = connection.getOperationProviders().get(reference).invoke(inputVariables, inoutputVariables);
    } else {
        final AtomicReference<OperationVariable[]> operationResult = new AtomicReference<>();
        final AtomicReference<OperationVariable[]> operationInout = new AtomicReference<>();
        CountDownLatch condition = new CountDownLatch(1);
        connection.getOperationProviders().get(reference).invokeAsync(inputVariables, inoutputVariables, (res, inout) -> {
            operationResult.set(res);
            operationInout.set(inout);
            condition.countDown();
        });
        condition.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
        actual = operationResult.get();
        inoutputVariables = operationInout.get();
    }
    connection.close();
    Assert.assertArrayEquals(expectedOut, actual);
    Assert.assertArrayEquals(expectedInOut, inoutputVariables);
}
Also used : DefaultOperationVariable(io.adminshell.aas.v3.model.impl.DefaultOperationVariable) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Reference(io.adminshell.aas.v3.model.Reference) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultProperty(io.adminshell.aas.v3.model.impl.DefaultProperty) Property(io.adminshell.aas.v3.model.Property)

Example 9 with OperationVariable

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

the class InvokeOperationAsyncRequestHandler method executeOperationAsync.

public OperationHandle executeOperationAsync(Reference reference, InvokeOperationAsyncRequest request) throws MessageBusException, Exception {
    if (!assetConnectionManager.hasOperationProvider(reference)) {
        throw new IllegalArgumentException(String.format("error executing operation - no operation provider defined for reference '%s' (requestId: %s)", AasUtils.asString(reference), request.getRequestId()));
    }
    OperationHandle operationHandle = this.persistence.putOperationContext(null, request.getRequestId(), new OperationResult.Builder().requestId(request.getRequestId()).inoutputArguments(request.getInoutputArguments()).executionState(ExecutionState.RUNNING).build());
    try {
        BiConsumer<OperationVariable[], OperationVariable[]> callback = LambdaExceptionHelper.rethrowBiConsumer((x, y) -> {
            OperationResult operationResult = persistence.getOperationResult(operationHandle.getHandleId());
            operationResult.setExecutionState(ExecutionState.COMPLETED);
            operationResult.setOutputArguments(Arrays.asList(x));
            operationResult.setInoutputArguments(Arrays.asList(y));
            persistence.putOperationContext(operationHandle.getHandleId(), operationHandle.getRequestId(), operationResult);
            messageBus.publish(OperationFinishEventMessage.builder().element(reference).inoutput(ElementValueHelper.toValues(Arrays.asList(x))).output(ElementValueHelper.toValues(Arrays.asList(y))).build());
        });
        AssetOperationProvider assetOperationProvider = assetConnectionManager.getOperationProvider(reference);
        assetOperationProvider.invokeAsync(request.getInputArguments().toArray(new OperationVariable[0]), request.getInoutputArguments().toArray(new OperationVariable[0]), callback);
    } catch (AssetConnectionException | ValueMappingException e) {
        OperationResult operationResult = persistence.getOperationResult(operationHandle.getHandleId());
        operationResult.setExecutionState(ExecutionState.FAILED);
        operationResult.setInoutputArguments(request.getInoutputArguments());
        persistence.putOperationContext(operationHandle.getHandleId(), operationHandle.getRequestId(), operationResult);
        try {
            messageBus.publish(OperationFinishEventMessage.builder().element(reference).inoutput(ElementValueHelper.toValues(operationResult.getInoutputArguments())).build());
        } catch (ValueMappingException e2) {
            LOGGER.warn("could not publish operation finished event message because mapping result to value objects failed", e2);
        }
    }
    return operationHandle;
}
Also used : OperationVariable(io.adminshell.aas.v3.model.OperationVariable) ValueMappingException(de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException) OperationResult(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult) AssetOperationProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) OperationHandle(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationHandle)

Example 10 with OperationVariable

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

the class InvokeOperationSyncRequestHandler method executeOperationSync.

public OperationResult executeOperationSync(Reference reference, InvokeOperationSyncRequest request) {
    if (!assetConnectionManager.hasOperationProvider(reference)) {
        throw new IllegalArgumentException(String.format("error executing operation - no operation provider defined for reference '%s' (requestId: %s)", AasUtils.asString(reference), request.getRequestId()));
    }
    AssetOperationProvider assetOperationProvider = assetConnectionManager.getOperationProvider(reference);
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<OperationVariable[]> future = executor.submit(new Callable<OperationVariable[]>() {

        @Override
        public OperationVariable[] call() throws Exception {
            return assetOperationProvider.invoke(request.getInputArguments().toArray(new OperationVariable[0]), request.getInoutputArguments().toArray(new OperationVariable[0]));
        }
    });
    OperationResult result;
    try {
        OperationVariable[] outputVariables = future.get(request.getTimeout(), TimeUnit.MILLISECONDS);
        result = OperationResult.builder().requestId(request.getRequestId()).executionState(ExecutionState.COMPLETED).inoutputArguments(request.getInoutputArguments()).outputArguments(Arrays.asList(outputVariables)).build();
    } catch (TimeoutException e) {
        future.cancel(true);
        result = OperationResult.builder().requestId(request.getRequestId()).inoutputArguments(request.getInoutputArguments()).executionState(ExecutionState.TIMEOUT).build();
        Thread.currentThread().interrupt();
    } catch (Exception e) {
        result = OperationResult.builder().requestId(request.getRequestId()).inoutputArguments(request.getInoutputArguments()).executionState(ExecutionState.FAILED).build();
        Thread.currentThread().interrupt();
    } finally {
        executor.shutdown();
    }
    return result;
}
Also used : OperationVariable(io.adminshell.aas.v3.model.OperationVariable) ExecutorService(java.util.concurrent.ExecutorService) AssetOperationProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider) OperationResult(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult) TimeoutException(java.util.concurrent.TimeoutException) ValueMappingException(de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException) MessageBusException(de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

OperationVariable (io.adminshell.aas.v3.model.OperationVariable)9 StatusException (com.prosysopc.ua.StatusException)5 Property (io.adminshell.aas.v3.model.Property)4 Reference (io.adminshell.aas.v3.model.Reference)4 AssetOperationProvider (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider)3 SubmodelElementData (de.fraunhofer.iosb.ilt.faaast.service.endpoint.opcua.data.SubmodelElementData)3 MessageBusException (de.fraunhofer.iosb.ilt.faaast.service.exception.MessageBusException)3 ValueMappingException (de.fraunhofer.iosb.ilt.faaast.service.model.exception.ValueMappingException)3 ArrayList (java.util.ArrayList)3 ServiceException (com.prosysopc.ua.ServiceException)2 AddressSpaceException (com.prosysopc.ua.client.AddressSpaceException)2 UaNodeFactoryException (com.prosysopc.ua.nodes.UaNodeFactoryException)2 NodeId (com.prosysopc.ua.stack.builtintypes.NodeId)2 ServiceResultException (com.prosysopc.ua.stack.common.ServiceResultException)2 ServiceContext (de.fraunhofer.iosb.ilt.faaast.service.ServiceContext)2 AssetConnection (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnection)2 AssetConnectionException (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException)2 CoreConfig (de.fraunhofer.iosb.ilt.faaast.service.config.CoreConfig)2 OperationResult (de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult)2 DefaultOperationVariable (io.adminshell.aas.v3.model.impl.DefaultOperationVariable)2