Skip to content

Commit

Permalink
Added test class bodies and renamed consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
nalim2 authored and JulianFeinauer committed May 19, 2019
1 parent b8c09b5 commit 2b98afa
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.api.PlcConnection;
import org.apache.plc4x.java.api.authentication.PlcAuthentication;
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
import org.apache.plc4x.java.opcua.connection.OPCUAConnectionFactory;
import org.apache.plc4x.java.opcua.connection.OpcuaConnectionFactory;
import org.apache.plc4x.java.spi.PlcDriver;

import java.net.InetAddress;
Expand All @@ -37,20 +37,20 @@ Licensed to the Apache Software Foundation (ASF) under one
* Implementation of the OPC UA protocol, based on:
* - Eclipse Milo (https://github.com/eclipse/milo)
*/
public class OPCUAPlcDriver implements PlcDriver {
public class OpcuaPlcDriver implements PlcDriver {



public static final Pattern INET_ADDRESS_PATTERN = Pattern.compile("tcp:https://(?<host>[\\w.-]+)(:(?<port>\\d*))?");
public static final Pattern OPCUA_URI_PATTERN = Pattern.compile("^opcua:(" + INET_ADDRESS_PATTERN + ")?" + "(?<params>/[\\w/]+)?");
private static final int requestTimeout = 10000;
private OPCUAConnectionFactory opcuaConnectionFactory;
private OpcuaConnectionFactory opcuaConnectionFactory;

public OPCUAPlcDriver() {
this.opcuaConnectionFactory = new OPCUAConnectionFactory();
public OpcuaPlcDriver() {
this.opcuaConnectionFactory = new OpcuaConnectionFactory();
}

public OPCUAPlcDriver(OPCUAConnectionFactory opcuaConnectionFactory) {
public OpcuaPlcDriver(OpcuaConnectionFactory opcuaConnectionFactory) {
this.opcuaConnectionFactory = opcuaConnectionFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.api.messages.PlcWriteRequest;
import org.apache.plc4x.java.base.connection.AbstractPlcConnection;
import org.apache.plc4x.java.base.messages.*;
import org.apache.plc4x.java.opcua.protocol.model.OpcuaPlcFieldHandler;
import org.apache.plc4x.java.opcua.protocol.OpcuaPlcFieldHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class BaseOPCUAPlcConnection extends AbstractPlcConnection implements PlcReader, PlcWriter, PlcSubscriber {
public abstract class BaseOpcuaPlcConnection extends AbstractPlcConnection implements PlcReader, PlcWriter, PlcSubscriber {

private static final Logger logger = LoggerFactory.getLogger(BaseOPCUAPlcConnection.class);
private static final Logger logger = LoggerFactory.getLogger(BaseOpcuaPlcConnection.class);

BaseOPCUAPlcConnection(String params) {
BaseOpcuaPlcConnection(String params) {

if (!StringUtils.isEmpty(params)) {
for (String param : params.split("&")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.net.InetAddress;
import java.util.Objects;

public class OPCUAConnectionFactory {
public class OpcuaConnectionFactory {

public OPCUATcpPlcConnection opcuaTcpPlcConnectionOf(InetAddress address, Integer port, String params, int requestTimeout) {
public OpcuaTcpPlcConnection opcuaTcpPlcConnectionOf(InetAddress address, Integer port, String params, int requestTimeout) {
Objects.requireNonNull(address);

if (port == null) {
return OPCUATcpPlcConnection.of(address, params, requestTimeout);
return OpcuaTcpPlcConnection.of(address, params, requestTimeout);
} else {
return OPCUATcpPlcConnection.of(address, port, params, requestTimeout);
return OpcuaTcpPlcConnection.of(address, port, params, requestTimeout);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.base.messages.items.*;
import org.apache.plc4x.java.base.model.SubscriptionPlcField;
import org.apache.plc4x.java.opcua.protocol.OpcuaField;
import org.apache.plc4x.java.opcua.protocol.model.OpcuaSubsriptionHandle;
import org.apache.plc4x.java.opcua.protocol.OpcuaSubsriptionHandle;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
Expand Down Expand Up @@ -70,41 +70,42 @@ Licensed to the Apache Software Foundation (ASF) under one

import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint;

public class OPCUATcpPlcConnection extends BaseOPCUAPlcConnection {
public class OpcuaTcpPlcConnection extends BaseOpcuaPlcConnection {

private static final int OPCUA_DEFAULT_TCP_PORT = 4840;

private static final Logger logger = LoggerFactory.getLogger(OPCUATcpPlcConnection.class);
private static final Logger logger = LoggerFactory.getLogger(OpcuaTcpPlcConnection.class);
private InetAddress address;
private int requestTimeout = 5000;
private int port;
private String params;
private OpcUaClient client;
private boolean isConnected = false;
private final AtomicLong clientHandles = new AtomicLong(1L);
private OPCUATcpPlcConnection(InetAddress address, String params, int requestTimeout) {

private OpcuaTcpPlcConnection(InetAddress address, String params, int requestTimeout) {
this( address, OPCUA_DEFAULT_TCP_PORT, params, requestTimeout);
logger.info("Configured OPCUATcpPlcConnection with: host-name {}", address.getHostAddress());
logger.info("Configured OpcuaTcpPlcConnection with: host-name {}", address.getHostAddress());
}

public OPCUATcpPlcConnection(InetAddress address, int port, String params, int requestTimeout) {
public OpcuaTcpPlcConnection(InetAddress address, int port, String params, int requestTimeout) {
this(params);
logger.info("Configured OPCUATcpPlcConnection with: host-name {}", address.getHostAddress());
logger.info("Configured OpcuaTcpPlcConnection with: host-name {}", address.getHostAddress());
this.address = address;
this.port = port;
this.params = params;
}

public OPCUATcpPlcConnection(String params) {
public OpcuaTcpPlcConnection(String params) {
super(params);
}

public static OPCUATcpPlcConnection of(InetAddress address, String params, int requestTimeout) {
return new OPCUATcpPlcConnection(address, params, requestTimeout);
public static OpcuaTcpPlcConnection of(InetAddress address, String params, int requestTimeout) {
return new OpcuaTcpPlcConnection(address, params, requestTimeout);
}

public static OPCUATcpPlcConnection of(InetAddress address, int port, String params, int requestTimeout) {
return new OPCUATcpPlcConnection(address, port, params, requestTimeout);
public static OpcuaTcpPlcConnection of(InetAddress address, int port, String params, int requestTimeout) {
return new OpcuaTcpPlcConnection(address, port, params, requestTimeout);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one

public class OpcuaField implements PlcField {

private static final Pattern ADDRESS_PATTERN = Pattern.compile("^ns=(?<namespace>\\d+);(?<identifierType>[isgb])=((?<identifier>\\w+))?");
public static final Pattern ADDRESS_PATTERN = Pattern.compile("^ns=(?<namespace>\\d+);(?<identifierType>[isgb])=((?<identifier>\\w+))?");

private final OpcuaIdentifierType identifierType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ Licensed to the Apache Software Foundation (ASF) under one
* @author Matthias Milan Stlrljic
* Created by Matthias Milan Stlrljic on 10.05.2019
*/
package org.apache.plc4x.java.opcua.protocol.model;
package org.apache.plc4x.java.opcua.protocol;


import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
import org.apache.plc4x.java.api.model.PlcField;
import org.apache.plc4x.java.base.connection.DefaultPlcFieldHandler;
import org.apache.plc4x.java.base.messages.items.*;
import org.apache.plc4x.java.opcua.protocol.OpcuaField;

import java.math.BigInteger;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
* @author Matthias Milan Stlrljic
* Created by Matthias Milan Stlrljic on 10.05.2019
*/
package org.apache.plc4x.java.opcua.protocol.model;
package org.apache.plc4x.java.opcua.protocol;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
Expand All @@ -28,7 +28,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.base.messages.DefaultPlcSubscriptionEvent;
import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
import org.apache.plc4x.java.opcua.connection.OPCUATcpPlcConnection;
import org.apache.plc4x.java.opcua.connection.OpcuaTcpPlcConnection;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void onSubscriptionValue(UaMonitoredItem item, DataValue value) {
if(value.getStatusCode() != StatusCode.GOOD){
resultCode = PlcResponseCode.NOT_FOUND;
}else{
stringItem = OPCUATcpPlcConnection.encodeFieldItem(value);
stringItem = OpcuaTcpPlcConnection.encodeFieldItem(value);

}
Map<String, Pair<PlcResponseCode, BaseDefaultFieldItem>> fields = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
# specific language governing permissions and limitations
# under the License.
#
org.apache.plc4x.java.opcua.OPCUAPlcDriver
org.apache.plc4x.java.opcua.OpcuaPlcDriver
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
* @author Matthias Milan Stlrljic
* Created by Matthias Milan Stlrljic on 10.05.2019
*/
package org.apache.plc4x.java.opcua.connection;
package org.apache.plc4x.java.opcua;

import org.apache.plc4x.java.PlcDriverManager;
import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
Expand All @@ -28,8 +28,9 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.api.types.PlcSubscriptionType;
import org.apache.plc4x.java.base.messages.DefaultPlcSubscriptionRequest;
import org.apache.plc4x.java.base.model.SubscriptionPlcField;
import org.apache.plc4x.java.opcua.connection.OpcuaTcpPlcConnection;
import org.apache.plc4x.java.opcua.protocol.OpcuaField;
import org.apache.plc4x.java.opcua.protocol.model.OpcuaPlcFieldHandler;
import org.apache.plc4x.java.opcua.protocol.OpcuaPlcFieldHandler;

import java.math.BigInteger;
import java.time.Duration;
Expand All @@ -39,17 +40,15 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.util.LinkedHashMap;
import java.util.function.Consumer;

public class ManualPLC4XOPCUA {
public class ManualPLC4XOpcua {
public static void main(String args[]){




OPCUATcpPlcConnection opcuaConnection = null;
OpcuaTcpPlcConnection opcuaConnection = null;
OpcuaPlcFieldHandler fieldH = new OpcuaPlcFieldHandler();
PlcField field = fieldH.createField("ns=2;i=10855");
try {
opcuaConnection = (OPCUATcpPlcConnection)
opcuaConnection = (OpcuaTcpPlcConnection)
new PlcDriverManager().getConnection("opcua:tcp:https://opcua.demo-this.com:51210/UA/SampleServer");

} catch (PlcConnectionException e) {
Expand Down Expand Up @@ -82,7 +81,6 @@ public static void main(String args[]){

PlcWriteRequest.Builder wBuilder = opcuaConnection.writeRequestBuilder();
wBuilder.addItem("w-Bool", "ns=2;i=11012", true);
/*
wBuilder.addItem("w-ByteString", "ns=2;i=10858", "TEST".getBytes());
wBuilder.addItem("w-Byte", "ns=2;i=10846", (byte)1);
wBuilder.addItem("w-Double", "ns=2;i=10854", (double)0.25);
Expand All @@ -97,7 +95,6 @@ public static void main(String args[]){
wBuilder.addItem("w-UInt32", "ns=2;i=10850", (long)21412);
wBuilder.addItem("w-UInt64", "ns=2;i=10852", new BigInteger("1245152"));
wBuilder.addItem("w-UInteger", "ns=2;i=10870", new BigInteger("1245152"));
*/
PlcWriteRequest writeRequest = wBuilder.build();
PlcWriteResponse wResponse = opcuaConnection.write(writeRequest).get();

Expand All @@ -113,7 +110,7 @@ public static void main(String args[]){
PlcConsumerRegistration registration = opcuaConnection.register(consumer, subResp.getSubscriptionHandles());
Thread.sleep(7000);
registration.unregister();
Thread.sleep(200000);
Thread.sleep(20000);
opcuaConnection.close();
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
* @author Matthias Milan Stlrljic
* Created by Matthias Milan Stlrljic on 10.05.2019
*/
package org.apache.plc4x.java.opcua;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.apache.plc4x.java.opcua.OpcuaPlcDriver.INET_ADDRESS_PATTERN;
import static org.apache.plc4x.java.opcua.OpcuaPlcDriver.OPCUA_URI_PATTERN;
import static org.apache.plc4x.java.opcua.UtilsTest.assertMatching;

public class OpcuaPlcDriverTest {
@BeforeEach
public void before() {
}

@AfterEach
public void after() {

}

@Test
public void testOpcuaAddressPattern() {

assertMatching(INET_ADDRESS_PATTERN, "tcp:https://localhost");
assertMatching(INET_ADDRESS_PATTERN, "tcp:https://localhost:3131");
assertMatching(INET_ADDRESS_PATTERN, "tcp:https://www.google.de");
assertMatching(INET_ADDRESS_PATTERN, "tcp:https://www.google.de:443");
assertMatching(INET_ADDRESS_PATTERN, "tcp:https://127.0.0.1");
assertMatching(INET_ADDRESS_PATTERN, "tcp:https://127.0.0.1:251");
assertMatching(INET_ADDRESS_PATTERN, "tcp:https://254.254.254.254:1337");
assertMatching(INET_ADDRESS_PATTERN, "tcp:https://254.254.254.254");


assertMatching(OPCUA_URI_PATTERN, "opcua:tcp:https://localhost");
assertMatching(OPCUA_URI_PATTERN, "opcua:tcp:https://localhost:3131");
assertMatching(OPCUA_URI_PATTERN, "opcua:tcp:https://www.google.de");
assertMatching(OPCUA_URI_PATTERN, "opcua:tcp:https://www.google.de:443");
assertMatching(OPCUA_URI_PATTERN, "opcua:tcp:https://127.0.0.1");
assertMatching(OPCUA_URI_PATTERN, "opcua:tcp:https://127.0.0.1:251");
assertMatching(OPCUA_URI_PATTERN, "opcua:tcp:https://254.254.254.254:1337");
assertMatching(OPCUA_URI_PATTERN, "opcua:tcp:https://254.254.254.254");


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
* @author Matthias Milan Stlrljic
* Created by Matthias Milan Stlrljic on 10.05.2019
*/
package org.apache.plc4x.java.opcua;

import java.util.regex.Pattern;

import static org.junit.Assert.fail;

public class UtilsTest {
public static void assertMatching(Pattern pattern, String match) {
if (!pattern.matcher(match).matches()) {
fail(pattern + "doesn't match " + match);
}
}

public static void assertNoMatching(Pattern pattern, String match) {
if (pattern.matcher(match).matches()) {
fail(pattern + "does match " + match + " but should not");
}
}
}
Loading

0 comments on commit 2b98afa

Please sign in to comment.