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();
}
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);
}
}
}
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);
}
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;
}
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;
}
Aggregations