use of de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult in project FAAAST-Service by FraunhoferIOSB.
the class InvokeOperationSyncRequestHandler method process.
@Override
public InvokeOperationSyncResponse process(InvokeOperationSyncRequest request) {
Reference reference = ReferenceHelper.toReference(request.getPath(), request.getId(), Submodel.class);
InvokeOperationSyncResponse response = new InvokeOperationSyncResponse();
try {
// Check if submodelelement does exist
Operation operation = (Operation) persistence.get(reference, new OutputModifier());
publishOperationInvokeEventMessage(reference, toValues(request.getInputArguments()), toValues(request.getInoutputArguments()));
OperationResult operationResult = executeOperationSync(reference, request);
response.setPayload(operationResult);
response.setStatusCode(StatusCode.Success);
} catch (ResourceNotFoundException ex) {
response.setStatusCode(StatusCode.ClientErrorResourceNotFound);
} catch (Exception ex) {
response.setStatusCode(StatusCode.ServerInternalError);
}
publishOperationFinishEventMessage(reference, toValues(response.getPayload().getOutputArguments()), toValues(response.getPayload().getInoutputArguments()));
return response;
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult in project FAAAST-Service by FraunhoferIOSB.
the class InvokeOperationAsyncRequestHandler method executeOperationAsync.
public OperationHandle executeOperationAsync(Reference reference, InvokeOperationAsyncRequest request) {
if (assetConnectionManager.hasOperationProvider(reference)) {
OperationHandle operationHandle = this.persistence.putOperationContext(null, request.getRequestId(), new OperationResult.Builder().requestId(request.getRequestId()).inoutputArguments(request.getInoutputArguments()).executionState(ExecutionState.Running).build());
BiConsumer<OperationVariable[], OperationVariable[]> callback = (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);
publishOperationFinishEventMessage(reference, toValues(Arrays.asList(x)), toValues(Arrays.asList(y)));
};
AssetOperationProvider assetOperationProvider = assetConnectionManager.getOperationProvider(reference);
try {
assetOperationProvider.invokeAsync(request.getInputArguments().toArray(new OperationVariable[0]), request.getInoutputArguments().toArray(new OperationVariable[0]), callback);
} catch (AssetConnectionException ex) {
OperationResult operationResult = persistence.getOperationResult(operationHandle.getHandleId());
operationResult.setExecutionState(ExecutionState.Failed);
operationResult.setInoutputArguments(request.getInoutputArguments());
persistence.putOperationContext(operationHandle.getHandleId(), operationHandle.getRequestId(), operationResult);
publishOperationFinishEventMessage(reference, List.of(), toValues(operationResult.getInoutputArguments()));
}
return operationHandle;
} else {
throw new RuntimeException("No assetconnection available for running operation with request id" + request.getRequestId());
}
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult in project FAAAST-Service by FraunhoferIOSB.
the class InvokeOperationSyncRequestHandler method executeOperationSync.
public OperationResult executeOperationSync(Reference reference, InvokeOperationSyncRequest request) {
if (assetConnectionManager.hasOperationProvider(reference)) {
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 ex) {
future.cancel(true);
result = OperationResult.builder().requestId(request.getRequestId()).inoutputArguments(request.getInoutputArguments()).executionState(ExecutionState.Timeout).build();
} catch (Exception ex) {
result = OperationResult.builder().requestId(request.getRequestId()).inoutputArguments(request.getInoutputArguments()).executionState(ExecutionState.Failed).build();
} finally {
executor.shutdown();
}
return result;
} else {
throw new RuntimeException("No assetconnection available for running operation with request id" + request.getRequestId());
}
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult in project FAAAST-Service by FraunhoferIOSB.
the class GetOperationAsyncResultRequestHandler method process.
@Override
public GetOperationAsyncResultResponse process(GetOperationAsyncResultRequest request) {
GetOperationAsyncResultResponse response = new GetOperationAsyncResultResponse();
try {
OperationResult operationResult = persistence.getOperationResult(request.getHandleId());
response.setPayload(operationResult);
response.setStatusCode(StatusCode.Success);
} catch (Exception ex) {
response.setStatusCode(StatusCode.ServerInternalError);
}
return response;
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult in project FAAAST-Service by FraunhoferIOSB.
the class PersistenceInMemoryTest method testUpdateOperationResult.
@Test
public void testUpdateOperationResult() {
OperationResult expectedResult = new OperationResult.Builder().requestId("Test").executionState(ExecutionState.Initiated).build();
OperationHandle actualOperationHandle = this.persistence.putOperationContext(null, "Test", expectedResult);
expectedResult.setExecutionState(ExecutionState.Completed);
expectedResult.setExecutionResult(new Result.Builder().message(new Message.Builder().code("test").build()).success(true).build());
this.persistence.putOperationContext(actualOperationHandle.getHandleId(), null, expectedResult);
OperationResult actualResult = this.persistence.getOperationResult(actualOperationHandle.getHandleId());
Assert.assertEquals(expectedResult, actualResult);
}
Aggregations