Search in sources :

Example 1 with EndpointDescription

use of org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription in project milo by eclipse.

the class OpcUaClient method create.

/**
 * Create and configure an {@link OpcUaClient} by selecting an {@link EndpointDescription} from a list of endpoints
 * retrieved via the GetEndpoints service from the server at {@code endpointUrl} and building an
 * {@link OpcUaClientConfig} using that endpoint.
 *
 * @param endpointUrl    the endpoint URL of the server to connect to and retrieve endpoints from.
 * @param selectEndpoint a function that selects the {@link EndpointDescription} to connect to from the list of
 *                       endpoints from the server.
 * @param buildConfig    a function that configures an {@link OpcUaClientConfigBuilder} and then builds and returns
 *                       an {@link OpcUaClientConfig}.
 * @return a configured {@link OpcUaClient}.
 * @throws UaException if the endpoints could not be retrieved or the client could not be created.
 */
public static OpcUaClient create(String endpointUrl, Function<List<EndpointDescription>, Optional<EndpointDescription>> selectEndpoint, Function<OpcUaClientConfigBuilder, OpcUaClientConfig> buildConfig) throws UaException {
    try {
        List<EndpointDescription> endpoints = DiscoveryClient.getEndpoints(endpointUrl).get();
        EndpointDescription endpoint = selectEndpoint.apply(endpoints).orElseThrow(() -> new UaException(StatusCodes.Bad_ConfigurationError, "no endpoint selected"));
        OpcUaClientConfigBuilder builder = OpcUaClientConfig.builder().setEndpoint(endpoint);
        return create(buildConfig.apply(builder));
    } catch (InterruptedException | ExecutionException e) {
        if (!endpointUrl.endsWith("/discovery")) {
            StringBuilder discoveryUrl = new StringBuilder(endpointUrl);
            if (!endpointUrl.endsWith("/")) {
                discoveryUrl.append("/");
            }
            discoveryUrl.append("discovery");
            return create(discoveryUrl.toString(), selectEndpoint, buildConfig);
        } else {
            throw UaException.extract(e).orElseGet(() -> new UaException(e));
        }
    }
}
Also used : OpcUaClientConfigBuilder(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder) UaException(org.eclipse.milo.opcua.stack.core.UaException) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with EndpointDescription

use of org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription in project milo by eclipse.

the class AnonymousProviderTest method testGetIdentityToken_EmptyPolicyId.

@Test
public void testGetIdentityToken_EmptyPolicyId() throws Exception {
    EndpointDescription endpoint = new EndpointDescription(null, null, null, null, null, new UserTokenPolicy[] { new UserTokenPolicy("", UserTokenType.Anonymous, null, null, null) }, null, null);
    AnonymousProvider p = new AnonymousProvider();
    SignedIdentityToken signedIdentityToken = p.getIdentityToken(endpoint, ByteString.NULL_VALUE);
    assertEquals(signedIdentityToken.getToken().getPolicyId(), "");
    assertTrue(signedIdentityToken.getToken() instanceof AnonymousIdentityToken);
}
Also used : AnonymousIdentityToken(org.eclipse.milo.opcua.stack.core.types.structured.AnonymousIdentityToken) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) UserTokenPolicy(org.eclipse.milo.opcua.stack.core.types.structured.UserTokenPolicy) Test(org.testng.annotations.Test)

Example 3 with EndpointDescription

use of org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription in project milo by eclipse.

the class AnonymousProviderTest method testGetIdentityToken.

@Test
public void testGetIdentityToken() throws Exception {
    EndpointDescription endpoint = new EndpointDescription(null, null, null, null, null, new UserTokenPolicy[] { new UserTokenPolicy("anonymous", UserTokenType.Anonymous, null, null, null) }, null, null);
    AnonymousProvider p = new AnonymousProvider();
    SignedIdentityToken signedIdentityToken = p.getIdentityToken(endpoint, ByteString.NULL_VALUE);
    assertEquals(signedIdentityToken.getToken().getPolicyId(), "anonymous");
    assertTrue(signedIdentityToken.getToken() instanceof AnonymousIdentityToken);
}
Also used : AnonymousIdentityToken(org.eclipse.milo.opcua.stack.core.types.structured.AnonymousIdentityToken) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) UserTokenPolicy(org.eclipse.milo.opcua.stack.core.types.structured.UserTokenPolicy) Test(org.testng.annotations.Test)

Example 4 with EndpointDescription

use of org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription in project milo by eclipse.

the class DiscoveryClient method getEndpoints.

private static CompletableFuture<List<EndpointDescription>> getEndpoints(String endpointUrl, String profileUri, Consumer<UaStackClientConfigBuilder> customizer) {
    EndpointDescription endpoint = new EndpointDescription(endpointUrl, null, null, MessageSecurityMode.None, SecurityPolicy.None.getUri(), null, profileUri, ubyte(0));
    UaStackClientConfigBuilder builder = UaStackClientConfig.builder();
    builder.setEndpoint(endpoint);
    customizer.accept(builder);
    UaStackClientConfig config = builder.build();
    try {
        UaStackClient stackClient = UaStackClient.create(config);
        DiscoveryClient discoveryClient = new DiscoveryClient(stackClient);
        return discoveryClient.connect().thenCompose(c -> c.getEndpoints(endpointUrl, new String[0], new String[] { profileUri })).whenComplete((e, ex) -> discoveryClient.disconnect()).thenApply(response -> l(response.getEndpoints()));
    } catch (UaException e) {
        return failedFuture(e);
    }
}
Also used : StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) FindServersResponse(org.eclipse.milo.opcua.stack.core.types.structured.FindServersResponse) CompletableFuture(java.util.concurrent.CompletableFuture) Unsigned.ubyte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ubyte) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) Consumer(java.util.function.Consumer) Strings(com.google.common.base.Strings) GetEndpointsRequest(org.eclipse.milo.opcua.stack.core.types.structured.GetEndpointsRequest) GetEndpointsResponse(org.eclipse.milo.opcua.stack.core.types.structured.GetEndpointsResponse) List(java.util.List) Stack(org.eclipse.milo.opcua.stack.core.Stack) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) ApplicationDescription(org.eclipse.milo.opcua.stack.core.types.structured.ApplicationDescription) MessageSecurityMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode) FutureUtils.failedFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedFuture) UaException(org.eclipse.milo.opcua.stack.core.UaException) FindServersRequest(org.eclipse.milo.opcua.stack.core.types.structured.FindServersRequest) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) EndpointUtil(org.eclipse.milo.opcua.stack.core.util.EndpointUtil) UaException(org.eclipse.milo.opcua.stack.core.UaException) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)

Example 5 with EndpointDescription

use of org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription in project milo by eclipse.

the class DiscoveryClient method findServers.

/**
 * Query the FindServers service at the {@code endpointUrl}.
 * <p>
 * The discovery URL(s) for each server {@link ApplicationDescription} in the response can then be used in a
 * {@link #getEndpoints(String)} call to discover the endpoints for that server.
 *
 * @param endpointUrl the endpoint URL to find servers at.
 * @param customizer  a {@link Consumer} that accepts a {@link UaStackClientConfigBuilder} for customization.
 * @return a List of {@link ApplicationDescription}s returned by the FindServers service.
 */
public static CompletableFuture<List<ApplicationDescription>> findServers(String endpointUrl, Consumer<UaStackClientConfigBuilder> customizer) {
    EndpointDescription endpoint = new EndpointDescription(endpointUrl, null, null, MessageSecurityMode.None, SecurityPolicy.None.getUri(), null, Stack.TCP_UASC_UABINARY_TRANSPORT_URI, ubyte(0));
    UaStackClientConfigBuilder builder = UaStackClientConfig.builder();
    builder.setEndpoint(endpoint);
    customizer.accept(builder);
    UaStackClientConfig config = builder.build();
    try {
        UaStackClient stackClient = UaStackClient.create(config);
        DiscoveryClient discoveryClient = new DiscoveryClient(stackClient);
        return discoveryClient.connect().thenCompose(c -> c.findServers(endpointUrl, new String[0], new String[0])).whenComplete((e, ex) -> discoveryClient.disconnect()).thenApply(response -> l(response.getServers()));
    } catch (UaException e) {
        return failedFuture(e);
    }
}
Also used : StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) FindServersResponse(org.eclipse.milo.opcua.stack.core.types.structured.FindServersResponse) CompletableFuture(java.util.concurrent.CompletableFuture) Unsigned.ubyte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ubyte) RequestHeader(org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader) Consumer(java.util.function.Consumer) Strings(com.google.common.base.Strings) GetEndpointsRequest(org.eclipse.milo.opcua.stack.core.types.structured.GetEndpointsRequest) GetEndpointsResponse(org.eclipse.milo.opcua.stack.core.types.structured.GetEndpointsResponse) List(java.util.List) Stack(org.eclipse.milo.opcua.stack.core.Stack) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) ApplicationDescription(org.eclipse.milo.opcua.stack.core.types.structured.ApplicationDescription) MessageSecurityMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode) FutureUtils.failedFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedFuture) UaException(org.eclipse.milo.opcua.stack.core.UaException) FindServersRequest(org.eclipse.milo.opcua.stack.core.types.structured.FindServersRequest) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) EndpointUtil(org.eclipse.milo.opcua.stack.core.util.EndpointUtil) UaException(org.eclipse.milo.opcua.stack.core.UaException) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)

Aggregations

EndpointDescription (org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)41 UaException (org.eclipse.milo.opcua.stack.core.UaException)16 Test (org.testng.annotations.Test)16 SecurityPolicy (org.eclipse.milo.opcua.stack.core.security.SecurityPolicy)15 UaStackClient (org.eclipse.milo.opcua.stack.client.UaStackClient)13 ByteString (org.eclipse.milo.opcua.stack.core.types.builtin.ByteString)9 MessageSecurityMode (org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode)9 X509Certificate (java.security.cert.X509Certificate)8 List (java.util.List)7 KeyPair (java.security.KeyPair)6 ExecutionException (java.util.concurrent.ExecutionException)6 StatusCodes (org.eclipse.milo.opcua.stack.core.StatusCodes)6 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)6 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)6 RequestHeader (org.eclipse.milo.opcua.stack.core.types.structured.RequestHeader)6 UserTokenPolicy (org.eclipse.milo.opcua.stack.core.types.structured.UserTokenPolicy)6 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)5 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)5 ApplicationDescription (org.eclipse.milo.opcua.stack.core.types.structured.ApplicationDescription)5 ReadValueId (org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId)5