use of de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationHandle in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManagerTest method testInvokeOperationAsyncRequest.
@Test
public void testInvokeOperationAsyncRequest() {
CoreConfig coreConfig = CoreConfig.builder().build();
Persistence persistence = mock(Persistence.class);
MessageBus messageBus = mock(MessageBus.class);
AssetConnectionManager assetConnectionManager = mock(AssetConnectionManager.class);
AssetOperationProvider assetOperationProvider = mock(AssetOperationProvider.class);
RequestHandlerManager manager = new RequestHandlerManager(coreConfig, persistence, messageBus, assetConnectionManager);
Operation operation = getTestOperation();
OperationHandle expectedOperationHandle = new OperationHandle.Builder().handleId("1").requestId("1").build();
when(persistence.putOperationContext(any(), any(), any())).thenReturn(expectedOperationHandle);
when(persistence.getOperationResult(any())).thenReturn(new OperationResult.Builder().requestId("1").build());
when(assetConnectionManager.hasOperationProvider(any())).thenReturn(true);
when(assetConnectionManager.getOperationProvider(any())).thenReturn(assetOperationProvider);
InvokeOperationAsyncRequest invokeOperationAsyncRequest = new InvokeOperationAsyncRequest.Builder().requestId("1").id(new DefaultIdentifier.Builder().idType(IdentifierType.IRI).identifier("http://example.org").build()).inoutputArguments(operation.getInoutputVariables()).inputArguments(operation.getInputVariables()).build();
InvokeOperationAsyncResponse response = manager.execute(invokeOperationAsyncRequest);
OperationHandle actualOperationHandle = response.getPayload();
Assert.assertEquals(expectedOperationHandle, actualOperationHandle);
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationHandle 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.OperationHandle in project FAAAST-Service by FraunhoferIOSB.
the class InvokeOperationAsyncRequestHandler method process.
@Override
public InvokeOperationAsyncResponse process(InvokeOperationAsyncRequest request) {
InvokeOperationAsyncResponse response = new InvokeOperationAsyncResponse();
try {
Reference reference = ReferenceHelper.toReference(request.getPath(), request.getId(), Submodel.class);
// Check if submodelelement does exist
Operation operation = (Operation) persistence.get(reference, new OutputModifier());
OperationHandle operationHandle = executeOperationAsync(reference, request);
response.setPayload(operationHandle);
response.setStatusCode(StatusCode.Success);
publishOperationInvokeEventMessage(reference, toValues(request.getInputArguments()), toValues(request.getInoutputArguments()));
} catch (ResourceNotFoundException ex) {
response.setStatusCode(StatusCode.ClientErrorResourceNotFound);
} catch (Exception ex) {
response.setStatusCode(StatusCode.ServerInternalError);
}
return response;
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationHandle in project FAAAST-Service by FraunhoferIOSB.
the class PersistenceInMemory method putOperationContext.
@Override
public OperationHandle putOperationContext(String handleId, String requestId, OperationResult operationResult) {
if (operationResult == null) {
operationResult = new OperationResult.Builder().executionState(ExecutionState.Initiated).requestId(requestId).build();
}
if (StringUtils.isBlank(handleId)) {
OperationHandle operationHandle = new OperationHandle.Builder().requestId(requestId).handleId(UUID.randomUUID().toString()).build();
operationHandleMap.put(operationHandle.getHandleId(), operationHandle);
operationResultMap.putIfAbsent(operationHandle.getHandleId(), operationResult);
return operationHandle;
} else {
operationResultMap.put(handleId, operationResult);
}
return null;
}
use of de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationHandle 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