Search in sources :

Example 1 with OperationHandle

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);
}
Also used : CoreConfig(de.fraunhofer.iosb.ilt.faaast.service.config.CoreConfig) OperationResult(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationResult) Operation(io.adminshell.aas.v3.model.Operation) DefaultOperation(io.adminshell.aas.v3.model.impl.DefaultOperation) OperationHandle(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationHandle) Persistence(de.fraunhofer.iosb.ilt.faaast.service.persistence.Persistence) MessageBus(de.fraunhofer.iosb.ilt.faaast.service.messagebus.MessageBus) AssetConnectionManager(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionManager) RequestHandlerManager(de.fraunhofer.iosb.ilt.faaast.service.request.RequestHandlerManager) InvokeOperationAsyncRequest(de.fraunhofer.iosb.ilt.faaast.service.model.request.InvokeOperationAsyncRequest) AssetOperationProvider(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetOperationProvider) DefaultIdentifier(io.adminshell.aas.v3.model.impl.DefaultIdentifier) InvokeOperationAsyncResponse(de.fraunhofer.iosb.ilt.faaast.service.model.api.response.InvokeOperationAsyncResponse) Test(org.junit.Test)

Example 2 with OperationHandle

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

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;
}
Also used : OutputModifier(de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier) Reference(io.adminshell.aas.v3.model.Reference) Operation(io.adminshell.aas.v3.model.Operation) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException) InvokeOperationAsyncResponse(de.fraunhofer.iosb.ilt.faaast.service.model.api.response.InvokeOperationAsyncResponse) OperationHandle(de.fraunhofer.iosb.ilt.faaast.service.model.api.operation.OperationHandle) AssetConnectionException(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.AssetConnectionException) ResourceNotFoundException(de.fraunhofer.iosb.ilt.faaast.service.exception.ResourceNotFoundException)

Example 4 with OperationHandle

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

Example 5 with OperationHandle

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

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