Search in sources :

Example 1 with ServiceContext

use of de.fraunhofer.iosb.ilt.faaast.service.ServiceContext in project FAAAST-Service by FraunhoferIOSB.

the class OpcUaAssetConnectionTest method testWriteReadValue.

private void testWriteReadValue(String nodeId, PropertyValue expected) throws AssetConnectionException, InterruptedException {
    Reference reference = AasUtils.parseReference("(Property)[ID_SHORT]Temperature");
    ServiceContext serviceContext = mock(ServiceContext.class);
    doReturn(ElementValueTypeInfo.builder().type(expected.getClass()).datatype(expected.getValue().getDataType()).build()).when(serviceContext).getTypeInfo(reference);
    OpcUaAssetConnection connection = new OpcUaAssetConnection(CoreConfig.builder().build(), OpcUaAssetConnectionConfig.builder().valueProvider(reference, OpcUaValueProviderConfig.builder().nodeId(nodeId).build()).host(serverUrl).build(), serviceContext);
    connection.getValueProviders().get(reference).setValue(expected);
    DataElementValue actual = connection.getValueProviders().get(reference).getValue();
    connection.close();
    Assert.assertEquals(expected, actual);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Reference(io.adminshell.aas.v3.model.Reference) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)

Example 2 with ServiceContext

use of de.fraunhofer.iosb.ilt.faaast.service.ServiceContext in project FAAAST-Service by FraunhoferIOSB.

the class HttpEndpointTest method init.

@BeforeClass
public static void init() throws Exception {
    try (ServerSocket serverSocket = new ServerSocket(0)) {
        Assert.assertNotNull(serverSocket);
        Assert.assertTrue(serverSocket.getLocalPort() > 0);
        port = serverSocket.getLocalPort();
    }
    deserializer = new HttpJsonDeserializer();
    HttpEndpointConfig endpointConfig = new HttpEndpointConfig();
    endpointConfig.setPort(port);
    serviceContext = mock(ServiceContext.class);
    endpoint = new HttpEndpoint();
    endpoint.init(CoreConfig.builder().build(), endpointConfig, serviceContext);
    endpoint.start();
    client = new HttpClient();
    client.start();
}
Also used : ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) HttpClient(org.eclipse.jetty.client.HttpClient) ServerSocket(java.net.ServerSocket) HttpJsonDeserializer(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.serialization.HttpJsonDeserializer) BeforeClass(org.junit.BeforeClass)

Example 3 with ServiceContext

use of de.fraunhofer.iosb.ilt.faaast.service.ServiceContext in project FAAAST-Service by FraunhoferIOSB.

the class RequestMappingManager method init.

private void init() {
    try (ScanResult scanResult = new ClassGraph().enableAllInfo().acceptPackages(getClass().getPackageName()).scan()) {
        mappers = scanResult.getSubclasses(RequestMapper.class.getName()).filter(x -> !x.isAbstract() && !x.isInterface()).loadClasses(RequestMapper.class).stream().map(x -> {
            try {
                Constructor<RequestMapper> constructor = x.getConstructor(ServiceContext.class);
                return constructor.newInstance(serviceContext);
            } catch (NoSuchMethodException | SecurityException ex) {
                logger.warn("request mapper implementation could not be loaded, " + "reason: missing constructor (implementation class: {}, required constructor signature: {}", x.getName());
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                logger.warn("request mapper implementation could not be loaded, " + "reason: calling constructor failed (implementation class: {}, constructor arguments: {}", x.getName());
            }
            logger.debug("unable to instantiate class {}", x.getName());
            return null;
        }).filter(x -> x != null).collect(Collectors.toList());
        mappers.stream().filter(x -> x == null).collect(Collectors.toList());
    }
}
Also used : ClassGraph(io.github.classgraph.ClassGraph) Logger(org.slf4j.Logger) HttpRequest(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.model.HttpRequest) LoggerFactory(org.slf4j.LoggerFactory) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) InvalidRequestException(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.exception.InvalidRequestException) RequestMapper(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.request.mapper.RequestMapper) Request(de.fraunhofer.iosb.ilt.faaast.service.model.api.Request) Optional(java.util.Optional) ScanResult(io.github.classgraph.ScanResult) ScanResult(io.github.classgraph.ScanResult) Constructor(java.lang.reflect.Constructor) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) ClassGraph(io.github.classgraph.ClassGraph) RequestMapper(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.request.mapper.RequestMapper)

Example 4 with ServiceContext

use of de.fraunhofer.iosb.ilt.faaast.service.ServiceContext in project FAAAST-Service by FraunhoferIOSB.

the class OpcUaAssetConnectionTest method testSubscribe.

private void testSubscribe(String nodeId, PropertyValue expected) throws AssetConnectionException, InterruptedException, ExecutionException, UaException {
    Reference reference = AasUtils.parseReference("(Property)[ID_SHORT]Temperature");
    long interval = 1000;
    ServiceContext serviceContext = mock(ServiceContext.class);
    TypeInfo infoExample = ElementValueTypeInfo.builder().type(PropertyValue.class).datatype(expected.getValue().getDataType()).build();
    doReturn(infoExample).when(serviceContext).getTypeInfo(reference);
    OpcUaAssetConnection connection = new OpcUaAssetConnection();
    connection.init(CoreConfig.builder().build(), OpcUaAssetConnectionConfig.builder().host(serverUrl).subscriptionProvider(reference, OpcUaSubscriptionProviderConfig.builder().nodeId(nodeId).interval(interval).build()).valueProvider(reference, OpcUaValueProviderConfig.builder().nodeId(nodeId).build()).build(), serviceContext);
    final AtomicReference<DataElementValue> response = new AtomicReference<>();
    CountDownLatch condition = new CountDownLatch(1);
    connection.getSubscriptionProviders().get(reference).addNewDataListener(new NewDataListener() {

        @Override
        public void newDataReceived(DataElementValue data) {
            response.set(data);
            condition.countDown();
        }
    });
    connection.getValueProviders().get(reference).setValue(expected);
    long waitTime = 5 * interval;
    TimeUnit waitTimeUnit = isDebugging() ? TimeUnit.SECONDS : TimeUnit.MILLISECONDS;
    Assert.assertTrue(String.format("test failed because there was no response within defined time (%d %s)", waitTime, waitTimeUnit), condition.await(waitTime, waitTimeUnit));
    Assert.assertEquals(expected, response.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Reference(io.adminshell.aas.v3.model.Reference) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) NewDataListener(de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener) TimeUnit(java.util.concurrent.TimeUnit) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ElementValueTypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo) TypeInfo(de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo) DataElementValue(de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)

Example 5 with ServiceContext

use of de.fraunhofer.iosb.ilt.faaast.service.ServiceContext 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 {
    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);
}
Also used : DefaultOperationVariable(io.adminshell.aas.v3.model.impl.DefaultOperationVariable) OperationVariable(io.adminshell.aas.v3.model.OperationVariable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Reference(io.adminshell.aas.v3.model.Reference) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultProperty(io.adminshell.aas.v3.model.impl.DefaultProperty) Property(io.adminshell.aas.v3.model.Property)

Aggregations

ServiceContext (de.fraunhofer.iosb.ilt.faaast.service.ServiceContext)6 Reference (io.adminshell.aas.v3.model.Reference)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 DataElementValue (de.fraunhofer.iosb.ilt.faaast.service.model.value.DataElementValue)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 NewDataListener (de.fraunhofer.iosb.ilt.faaast.service.assetconnection.NewDataListener)1 InvalidRequestException (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.exception.InvalidRequestException)1 HttpRequest (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.model.HttpRequest)1 RequestMapper (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.request.mapper.RequestMapper)1 HttpJsonDeserializer (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.serialization.HttpJsonDeserializer)1 Request (de.fraunhofer.iosb.ilt.faaast.service.model.api.Request)1 ElementValueTypeInfo (de.fraunhofer.iosb.ilt.faaast.service.typing.ElementValueTypeInfo)1 TypeInfo (de.fraunhofer.iosb.ilt.faaast.service.typing.TypeInfo)1 OperationVariable (io.adminshell.aas.v3.model.OperationVariable)1 Property (io.adminshell.aas.v3.model.Property)1 DefaultOperationVariable (io.adminshell.aas.v3.model.impl.DefaultOperationVariable)1 DefaultProperty (io.adminshell.aas.v3.model.impl.DefaultProperty)1 ClassGraph (io.github.classgraph.ClassGraph)1 ScanResult (io.github.classgraph.ScanResult)1 Constructor (java.lang.reflect.Constructor)1