Search in sources :

Example 1 with OpcUaClientConfig

use of org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig in project milo by eclipse.

the class OpcUaClientConfigTest method testCopy.

@Test
public void testCopy() {
    OpcUaClientConfig original = OpcUaClientConfig.builder().setEndpoint(endpoint).setSessionName(() -> "testSessionName").setSessionTimeout(uint(60000 * 60)).setRequestTimeout(uint(120000)).setMaxResponseMessageSize(UInteger.MAX).setMaxPendingPublishRequests(uint(2)).setIdentityProvider(new AnonymousProvider()).setSessionLocaleIds(new String[] { "en", "es" }).build();
    OpcUaClientConfig copy = OpcUaClientConfig.copy(original).build();
    assertEquals(copy.getSessionName(), original.getSessionName());
    assertEquals(copy.getSessionTimeout(), original.getSessionTimeout());
    assertEquals(copy.getRequestTimeout(), original.getRequestTimeout());
    assertEquals(copy.getMaxResponseMessageSize(), original.getMaxResponseMessageSize());
    assertEquals(copy.getMaxPendingPublishRequests(), original.getMaxPendingPublishRequests());
    assertEquals(copy.getIdentityProvider(), original.getIdentityProvider());
    assertEquals(copy.getKeepAliveFailuresAllowed(), original.getKeepAliveFailuresAllowed());
    assertEquals(copy.getKeepAliveInterval(), original.getKeepAliveInterval());
    assertEquals(copy.getKeepAliveTimeout(), original.getKeepAliveTimeout());
    assertEquals(copy.getSessionLocaleIds(), original.getSessionLocaleIds());
}
Also used : AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) OpcUaClientConfig(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig) Test(org.testng.annotations.Test)

Example 2 with OpcUaClientConfig

use of org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig in project milo by eclipse.

the class OpcUaClientConfigTest method testCopyAndModify.

@Test
public void testCopyAndModify() {
    OpcUaClientConfig original = OpcUaClientConfig.builder().setEndpoint(endpoint).setSessionName(() -> "testSessionName").setSessionTimeout(uint(60000 * 60)).setRequestTimeout(uint(120000)).setMaxResponseMessageSize(UInteger.MAX).setMaxPendingPublishRequests(uint(2)).setIdentityProvider(new AnonymousProvider()).build();
    OpcUaClientConfig copy = OpcUaClientConfig.copy(original, builder -> builder.setSessionName(() -> "foo").setSessionTimeout(uint(0)).setRequestTimeout(uint(0)).setMaxResponseMessageSize(uint(0)).setMaxPendingPublishRequests(uint(0)).setIdentityProvider(new AnonymousProvider()).setKeepAliveFailuresAllowed(uint(2)).setKeepAliveInterval(uint(10000)).setKeepAliveTimeout(uint(15000)).setSessionLocaleIds(new String[] { "en", "es" }));
    assertNotEquals(copy.getSessionName(), original.getSessionName());
    assertNotEquals(copy.getIdentityProvider(), original.getIdentityProvider());
    assertNotEquals(copy.getSessionLocaleIds(), original.getSessionLocaleIds());
    assertEquals(copy.getSessionTimeout(), uint(0));
    assertEquals(copy.getRequestTimeout(), uint(0));
    assertEquals(copy.getMaxResponseMessageSize(), uint(0));
    assertEquals(copy.getMaxPendingPublishRequests(), uint(0));
    assertEquals(copy.getKeepAliveFailuresAllowed(), uint(2));
    assertEquals(copy.getKeepAliveInterval(), uint(10000));
    assertEquals(copy.getKeepAliveTimeout(), uint(15000));
    assertEquals(copy.getSessionLocaleIds(), new String[] { "en", "es" });
}
Also used : AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) OpcUaClientConfig(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig) Test(org.testng.annotations.Test)

Example 3 with OpcUaClientConfig

use of org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig in project vantiq-extension-sources by Vantiq.

the class OpcUaESClient method createClient.

@SuppressWarnings({ "PMD.CognitiveComplexity", "PMD.MethodLengthCheck" })
private OpcUaClient createClient(Map<String, Object> config) throws Exception {
    if (storageDirectory == null) {
        throw new OpcExtConfigException(ERROR_PREFIX + ".missingStorageDirectory: No storage directory specified.");
    }
    SecurityPolicy securityPolicy = determineSecurityPolicy(config);
    MessageSecurityMode msgSecMode = determineMessageSecurityMode(config);
    File securityDir = new File(storageDirectory, SECURITY_DIRECTORY);
    if (!securityDir.exists() && !securityDir.mkdirs()) {
        throw new OpcExtConfigException(ERROR_PREFIX + ".invalidStorageDirectory: unable to create security dir: " + securityDir);
    }
    log.info("security temp dir: {}", securityDir.getAbsolutePath());
    keyStoreManager = new KeyStoreManager().load(securityDir);
    IdentityProvider idProvider = constructIdentityProvider(config);
    List<EndpointDescription> endpoints;
    discoveryEndpoint = (String) config.get(OpcConstants.CONFIG_DISCOVERY_ENDPOINT);
    serverEndpoint = (String) config.get(OpcConstants.CONFIG_SERVER_ENDPOINT);
    if (discoveryEndpoint == null && serverEndpoint == null) {
        String errorMsg = ERROR_PREFIX + ".noDiscoveryEndpoint: No discovery or server endpoint was provided in the configuration.";
        log.error(errorMsg);
        throw new OpcExtConfigException(errorMsg);
    }
    OpcUaClientConfig opcConfig;
    try {
        endpoints = DiscoveryClient.getEndpoints(discoveryEndpoint).get();
    } catch (Throwable ex) {
        try {
            // try the explicit discovery endpoint as well
            String discoveryUrl = discoveryEndpoint + "/discovery";
            log.info("Trying explicit discovery URL: {}", discoveryUrl);
            endpoints = DiscoveryClient.getEndpoints(discoveryUrl).get();
        } catch (ExecutionException e) {
            String errMsg = ERROR_PREFIX + ".discoveryError: Could not discover OPC Endpoints:" + e.getClass().getName() + "::" + e.getMessage();
            log.error(ERROR_PREFIX + ".discoveryError: Could not discover OPC Endpoints: {} :: {}", e.getClass().getName(), e.getMessage());
            throw new OpcExtConfigException(errMsg, e);
        }
    }
    if (log.isDebugEnabled()) {
        logDiscoveredEndpoints(endpoints);
    }
    List<EndpointDescription> validEndpoints = endpoints.stream().filter(e -> (e.getSecurityPolicyUri().equals(securityPolicy.getUri()) && e.getSecurityMode().equals(msgSecMode))).collect(Collectors.toList());
    if (log.isDebugEnabled()) {
        logAcceptableEndpoints(validEndpoints, securityPolicy, msgSecMode);
        // The following code is here for testing only.  It allows us to fake a poorly configured
        // server that reports invalid or unreachable endpoints as part of discovery.  This is, for
        // reasons I'm sure i don't agree with, part of the protocol, so we must tolerate it.  The
        // purportedly proper response is to substitute the address used for discovery for any
        // unreachable addresses.  This, of course, makes little sense since the whole point of discovery
        // is to allow these to be spread across different nodes.  But I didn't write the spec.
        Boolean fakeBadAddress = (Boolean) config.get(OpcConstants.CONFIG_TEST_DISCOVERY_UNREACHABLE);
        if (fakeBadAddress != null && fakeBadAddress) {
            List<EndpointDescription> newValidEndpoints = new ArrayList<>();
            for (EndpointDescription e : validEndpoints) {
                URI url = new URI(e.getEndpointUrl());
                URI borkedUri = new URI(url.getScheme(), null, "utterlyWorthlessHostThatShouldNeverResolve", url.getPort(), url.getPath(), null, null);
                EndpointDescription borkedEd = new EndpointDescription(borkedUri.toString(), e.getServer(), e.getServerCertificate(), e.getSecurityMode(), e.getSecurityPolicyUri(), e.getUserIdentityTokens(), e.getTransportProfileUri(), e.getSecurityLevel());
                newValidEndpoints.add(borkedEd);
            }
            validEndpoints = newValidEndpoints;
        }
    }
    // First, we'll look for an endpoint that doesn't contain localhost.  This is, generally,
    // a not too useful configuration since localhost is always a relative address.
    EndpointDescription endpoint = validEndpoints.stream().filter(e -> {
        try {
            // Note:  Must use URI here.  If you use URL, it will fail with
            // a MailformedURLException because the generic system doesn't
            // understand opc.tcp: as a scheme/protocol.
            URI url = new URI(e.getEndpointUrl());
            InetAddress ina = InetAddress.getByName(url.getHost());
            if (!ina.isLoopbackAddress() || ina.isReachable(3000)) {
                return true;
            }
        } catch (UnknownHostException | URISyntaxException ex) {
            log.warn("Recoverable error during discovered server URL validation:" + ex.getClass().getName() + "::" + ex.getMessage() + "-->" + e.getEndpointUrl());
        } catch (Exception ex) {
            // This means that we have some non-optimal addresses returned by discovery.
            // In these cases, we'll leave it up to the SDK & network stack to figure out how to get there.
            log.debug("Recoverable error during discovered server URL validation. Left to network stack to resolve:" + ex.getClass().getName() + "::" + ex.getMessage() + "-->" + e.getEndpointUrl());
        }
        return false;
    }).findFirst().orElse(null);
    if (endpoint == null) {
        // Discovery server returned either no reasonable endpoints or none that weren't a loopback.
        log.warn("No servers at reachable, non-loopback addresses found via discovery. " + "Fixing up addresses to match discovery server.");
        endpoint = validEndpoints.stream().findFirst().orElse(null);
        if (endpoint != null) {
            endpoint = fixLookbackAddress(endpoint);
        }
    }
    if (endpoint == null) {
        throw new Exception("No acceptable endpoints returned for security policy: " + securityPolicy.getUri() + " and security mode " + msgSecMode);
    }
    if (serverEndpoint != null) {
        // Then we'll override the endpoint provided but otherwise use the endpoint descriptor returned.
        // The SDK seems to have an issue when no EndpointDescriptor is provided.
        EndpointDescription newEndpoint = new EndpointDescription(serverEndpoint, endpoint.getServer(), endpoint.getServerCertificate(), endpoint.getSecurityMode(), endpoint.getSecurityPolicyUri(), endpoint.getUserIdentityTokens(), endpoint.getTransportProfileUri(), endpoint.getSecurityLevel());
        log.debug("Replacing endpoint address with provided serverEndpoint: {} --> {}", endpoint.getEndpointUrl(), newEndpoint.getEndpointUrl());
        endpoint = newEndpoint;
    }
    log.info("Using discovered endpoint: {} [{}, {}]", endpoint.getEndpointUrl(), securityPolicy, msgSecMode.toString());
    opcConfig = OpcUaClientConfig.builder().setApplicationName(LocalizedText.english("VANTIQ OPC-UA Source")).setApplicationUri("urn:io:vantiq:extsrc:opcua:client").setCertificate(keyStoreManager.getClientCertificate()).setKeyPair(keyStoreManager.getClientKeyPair()).setEndpoint(endpoint).setIdentityProvider(idProvider).setRequestTimeout(uint(5000)).build();
    return OpcUaClient.create(opcConfig);
}
Also used : MonitoringParameters(org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters) X509Certificate(java.security.cert.X509Certificate) URISyntaxException(java.net.URISyntaxException) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) InetAddress(java.net.InetAddress) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) Map(java.util.Map) URI(java.net.URI) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) MessageFormatter(org.slf4j.helpers.MessageFormatter) X509IdentityProvider(org.eclipse.milo.opcua.sdk.client.api.identity.X509IdentityProvider) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) IdentityProvider(org.eclipse.milo.opcua.sdk.client.api.identity.IdentityProvider) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) PrivateKey(java.security.PrivateKey) Optional(java.util.Optional) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) MonitoringMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) MessageSecurityMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode) BiConsumer(java.util.function.BiConsumer) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) OpcUaClientConfig(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) UnknownHostException(java.net.UnknownHostException) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) UaException(org.eclipse.milo.opcua.stack.core.UaException) DiscoveryClient(org.eclipse.milo.opcua.stack.client.DiscoveryClient) UsernameProvider(org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider) MessageSecurityMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MessageSecurityMode) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) X509IdentityProvider(org.eclipse.milo.opcua.sdk.client.api.identity.X509IdentityProvider) IdentityProvider(org.eclipse.milo.opcua.sdk.client.api.identity.IdentityProvider) OpcUaClientConfig(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException) SecurityPolicy(org.eclipse.milo.opcua.stack.core.security.SecurityPolicy) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) InetAddress(java.net.InetAddress)

Example 4 with OpcUaClientConfig

use of org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig in project milo by eclipse.

the class SessionFsmTest method testCloseSessionWhileInactive.

@Test
public void testCloseSessionWhileInactive() throws Exception {
    OpcUaClientConfig clientConfig = OpcUaClientConfig.builder().setEndpoint(new EndpointDescription("opc.tcp://localhost:12685", null, null, MessageSecurityMode.None, SecurityPolicy.None.getUri(), null, TransportProfile.TCP_UASC_UABINARY.getUri(), null)).setApplicationName(LocalizedText.english("Eclipse Milo Test Client")).setApplicationUri("urn:eclipse:milo:examples:client").setRequestTimeout(uint(60000)).build();
    OpcUaClient client = OpcUaClient.create(clientConfig);
    SessionFsm sessionFsm = SessionFsmFactory.newSessionFsm(client);
    assertNotNull(sessionFsm.closeSession().get());
}
Also used : OpcUaClientConfig(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) Test(org.testng.annotations.Test)

Aggregations

OpcUaClientConfig (org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig)4 AnonymousProvider (org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider)3 Test (org.testng.annotations.Test)3 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)2 ImmutableList (com.google.common.collect.ImmutableList)1 File (java.io.File)1 InetAddress (java.net.InetAddress)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 PrivateKey (java.security.PrivateKey)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1