Search in sources :

Example 1 with OperationResult

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;
}
Also used : OutputModifier(de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier) Reference(io.adminshell.aas.v3.model.Reference) InvokeOperationSyncResponse(de.fraunhofer.iosb.ilt.faaast.service.model.api.response.InvokeOperationSyncResponse) OperationResult(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult) Operation(io.adminshell.aas.v3.model.Operation) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) TimeoutException(java.util.concurrent.TimeoutException) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException)

Example 2 with OperationResult

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());
    }
}
Also used : MessageBus(de.fraunhofer.iosb.ilt.faaast.service.messagebus.MessageBus) Operation(io.adminshell.aas.v3.model.Operation) Arrays(java.util.Arrays) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) Reference(io.adminshell.aas.v3.model.Reference) OutputModifier(de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier) AssetConnectionManager(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionManager) Persistence(de.fraunhofer.iosb.ilt.faaast.service.persistence.Persistence) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) ReferenceHelper(de.fraunhofer.iosb.ilt.faaast.service.util.ReferenceHelper) OperationHandle(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationHandle) List(java.util.List) StatusCode(de.fraunhofer.iosb.ilt.faaast.service.model.api.StatusCode) ExecutionState(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.ExecutionState) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) BiConsumer(java.util.function.BiConsumer) InvokeOperationAsyncRequest(de.fraunhofer.iosb.ilt.faaast.service.model.request.InvokeOperationAsyncRequest) InvokeOperationAsyncResponse(de.fraunhofer.iosb.ilt.faaast.service.model.api.response.InvokeOperationAsyncResponse) OperationResult(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult) AssetOperationProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider) Submodel(io.adminshell.aas.v3.model.Submodel) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) 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 3 with OperationResult

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());
    }
}
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) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with OperationResult

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;
}
Also used : GetOperationAsyncResultResponse(de.fraunhofer.iosb.ilt.faaast.service.model.api.response.GetOperationAsyncResultResponse) OperationResult(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult)

Example 5 with OperationResult

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);
}
Also used : OperationResult(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult) OperationHandle(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationHandle) Test(org.junit.Test)

Aggregations

OperationResult (de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult)6 ResourceNotFoundException (de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException)3 OperationHandle (de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationHandle)3 AssetOperationProvider (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider)2 OutputModifier (de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier)2 Operation (io.adminshell.aas.v3.model.Operation)2 OperationVariable (io.adminshell.aas.v3.model.OperationVariable)2 Reference (io.adminshell.aas.v3.model.Reference)2 TimeoutException (java.util.concurrent.TimeoutException)2 Test (org.junit.Test)2 AssetConnectionException (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException)1 AssetConnectionManager (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionManager)1 MessageBus (de.fraunhofer.iosb.ilt.faaast.service.messagebus.MessageBus)1 StatusCode (de.fraunhofer.iosb.ilt.faaast.service.model.api.StatusCode)1 ExecutionState (de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.ExecutionState)1 GetOperationAsyncResultResponse (de.fraunhofer.iosb.ilt.faaast.service.model.api.response.GetOperationAsyncResultResponse)1 InvokeOperationAsyncResponse (de.fraunhofer.iosb.ilt.faaast.service.model.api.response.InvokeOperationAsyncResponse)1 InvokeOperationSyncResponse (de.fraunhofer.iosb.ilt.faaast.service.model.api.response.InvokeOperationSyncResponse)1 InvokeOperationAsyncRequest (de.fraunhofer.iosb.ilt.faaast.service.model.request.InvokeOperationAsyncRequest)1 Persistence (de.fraunhofer.iosb.ilt.faaast.service.persistence.Persistence)1