Search in sources :

Example 1 with UaStackClient

use of org.eclipse.milo.opcua.stack.client.UaStackClient in project milo by eclipse.

the class DataTypeTreeBuilder method addChildren.

private static CompletableFuture<Unit> addChildren(Tree<DataTypeTree.DataType> tree, UaStackClient client, OpcUaSession session, NamespaceTable namespaceTable) {
    CompletableFuture<List<ReferenceDescription>> subtypes = browseSafe(client, session, new BrowseDescription(tree.getValue().getNodeId(), BrowseDirection.Forward, Identifiers.HasSubtype, false, uint(NodeClass.DataType.getValue()), uint(BrowseResultMask.All.getValue())));
    CompletableFuture<List<DataTypeTree.DataType>> dataTypesFuture = subtypes.thenCompose(references -> {
        Stream<CompletableFuture<DataTypeTree.DataType>> dataTypeFutures = references.stream().map(dataTypeReference -> {
            NodeId dataTypeId = dataTypeReference.getNodeId().toNodeId(namespaceTable).orElse(NodeId.NULL_VALUE);
            CompletableFuture<List<ReferenceDescription>> encodings = browseSafe(client, session, new BrowseDescription(dataTypeId, BrowseDirection.Forward, Identifiers.HasEncoding, false, uint(NodeClass.Object.getValue()), uint(BrowseResultMask.All.getValue())));
            return encodings.thenApply(encodingReferences -> {
                NodeId binaryEncodingId = null;
                NodeId xmlEncodingId = null;
                for (ReferenceDescription r : encodingReferences) {
                    if (r.getBrowseName().equals(OpcUaDefaultBinaryEncoding.ENCODING_NAME)) {
                        binaryEncodingId = r.getNodeId().toNodeId(namespaceTable).orElse(null);
                    } else if (r.getBrowseName().equals(OpcUaDefaultXmlEncoding.ENCODING_NAME)) {
                        xmlEncodingId = r.getNodeId().toNodeId(namespaceTable).orElse(null);
                    }
                }
                return new DataTypeTree.DataType(dataTypeReference.getBrowseName(), dataTypeId, binaryEncodingId, xmlEncodingId);
            });
        });
        return FutureUtils.sequence(dataTypeFutures);
    });
    return dataTypesFuture.thenCompose(dataTypes -> {
        Stream<CompletableFuture<Unit>> futures = dataTypes.stream().map(tree::addChild).map(childNode -> addChildren(childNode, client, session, namespaceTable));
        return FutureUtils.sequence(futures);
    }).thenApply(v -> Unit.VALUE);
}
Also used : OpcUaDefaultBinaryEncoding(org.eclipse.milo.opcua.stack.core.types.OpcUaDefaultBinaryEncoding) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) CompletableFuture(java.util.concurrent.CompletableFuture) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) Tree(org.eclipse.milo.opcua.stack.core.util.Tree) NamespaceTable(org.eclipse.milo.opcua.stack.core.NamespaceTable) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) ReadResponse(org.eclipse.milo.opcua.stack.core.types.structured.ReadResponse) OpcUaDefaultXmlEncoding(org.eclipse.milo.opcua.stack.core.types.OpcUaDefaultXmlEncoding) DataTypeTree(org.eclipse.milo.opcua.sdk.core.DataTypeTree) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) ReferenceDescription(org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription) BrowseDirection(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) BrowseResultMask(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseResultMask) Stream(java.util.stream.Stream) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) FutureUtils(org.eclipse.milo.opcua.stack.core.util.FutureUtils) UaException(org.eclipse.milo.opcua.stack.core.UaException) Collections(java.util.Collections) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) CompletableFuture(java.util.concurrent.CompletableFuture) DataTypeTree(org.eclipse.milo.opcua.sdk.core.DataTypeTree) ReferenceDescription(org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) List(java.util.List) Stream(java.util.stream.Stream) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription)

Example 2 with UaStackClient

use of org.eclipse.milo.opcua.stack.client.UaStackClient in project milo by eclipse.

the class ClientServerTest method testClientServerRoundTrip_TestStack_Basic128Rsa15_Sign.

@Test(dataProvider = "getVariants")
public void testClientServerRoundTrip_TestStack_Basic128Rsa15_Sign(Variant input) throws Exception {
    EndpointDescription endpoint = endpoints[1];
    logger.info("SecurityPolicy={}, MessageSecurityMode={}, input={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode(), input);
    UaStackClient client = createClient(endpoint);
    connectAndTest(input, client);
}
Also used : UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) Test(org.testng.annotations.Test)

Example 3 with UaStackClient

use of org.eclipse.milo.opcua.stack.client.UaStackClient in project milo by eclipse.

the class ClientServerTest method testClientServerRoundTrip_TestStack_Basic256Sha256_SignAndEncrypt.

@Test(dataProvider = "getVariants")
public void testClientServerRoundTrip_TestStack_Basic256Sha256_SignAndEncrypt(Variant input) throws Exception {
    EndpointDescription endpoint = endpoints[6];
    logger.info("SecurityPolicy={}, MessageSecurityMode={}, input={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode(), input);
    UaStackClient client = createClient(endpoint);
    connectAndTest(input, client);
}
Also used : UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) Test(org.testng.annotations.Test)

Example 4 with UaStackClient

use of org.eclipse.milo.opcua.stack.client.UaStackClient in project milo by eclipse.

the class ClientServerTest method testClientDisconnect.

@Test
public void testClientDisconnect() throws Exception {
    EndpointDescription endpoint = endpoints[0];
    Variant input = new Variant(42);
    logger.info("SecurityPolicy={}, MessageSecurityMode={}, input={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode(), input);
    UaStackClient client = createClient(endpoint);
    client.connect().get();
    RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(0), uint(0), null, DEFAULT_TIMEOUT_HINT, null);
    ReadRequest request = new ReadRequest(header, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(NodeId.NULL_VALUE, AttributeId.Value.uid(), null, null) });
    logger.info("sending request: {}", request);
    UaResponseMessage response0 = client.sendRequest(request).get();
    logger.info("got response: {}", response0);
    client.disconnect().get();
    assertThrows(() -> client.sendRequest(request).get());
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) UaResponseMessage(org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) Test(org.testng.annotations.Test)

Example 5 with UaStackClient

use of org.eclipse.milo.opcua.stack.client.UaStackClient in project milo by eclipse.

the class ClientServerTest method testClientTimeout.

@Test
public void testClientTimeout() throws Exception {
    EndpointDescription endpoint = endpoints[0];
    logger.info("SecurityPolicy={}, MessageSecurityMode={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode());
    UaStackClientConfig config = UaStackClientConfig.builder().setEndpoint(endpoint).setKeyPair(clientKeyPair).setCertificate(clientCertificate).build();
    UaStackClient client = UaStackClient.create(config);
    client.connect().get();
    server.addServiceHandler("/test", ReadRequest.TYPE_ID, service -> {
        // intentionally do nothing so the request can timeout
        logger.info("received {}; ignoring...", service.getRequest());
    });
    RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(0), uint(0), null, uint(1000), null);
    ReadRequest request = new ReadRequest(header, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(NodeId.NULL_VALUE, AttributeId.Value.uid(), null, null) });
    try {
        client.sendRequest(request).get();
        fail("expected response to timeout");
    } catch (Throwable t) {
        StatusCode statusCode = UaException.extractStatusCode(t).orElse(StatusCode.BAD);
        assertEquals(statusCode.getValue(), StatusCodes.Bad_Timeout);
    }
}
Also used : ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) UaStackClient(org.eclipse.milo.opcua.stack.client.UaStackClient) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) UaStackClientConfig(org.eclipse.milo.opcua.stack.client.UaStackClientConfig) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) ReadRequest(org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest) Test(org.testng.annotations.Test)

Aggregations

UaStackClient (org.eclipse.milo.opcua.stack.client.UaStackClient)17 EndpointDescription (org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)15 Test (org.testng.annotations.Test)11 RequestHeader (org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader)9 ReadRequest (org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest)8 ReadValueId (org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId)8 UaException (org.eclipse.milo.opcua.stack.core.UaException)6 CompletableFuture (java.util.concurrent.CompletableFuture)5 CertificateEncodingException (java.security.cert.CertificateEncodingException)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 Stream (java.util.stream.Stream)4 OpcUaSession (org.eclipse.milo.opcua.sdk.client.OpcUaSession)4 SignedIdentityToken (org.eclipse.milo.opcua.sdk.client.api.identity.SignedIdentityToken)4 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)4 Unit (org.eclipse.milo.opcua.stack.core.util.Unit)4 Fsm (com.digitalpetri.strictmachine.Fsm)3 FsmContext (com.digitalpetri.strictmachine.FsmContext)3 ActionContext (com.digitalpetri.strictmachine.dsl.ActionContext)3 FsmBuilder (com.digitalpetri.strictmachine.dsl.FsmBuilder)3