Skip to content

Commit

Permalink
Update OPC-UA driver test suites.
Browse files Browse the repository at this point in the history
Signed-off-by: Łukasz Dywicki <[email protected]>
  • Loading branch information
splatch committed Nov 5, 2024
1 parent 3751517 commit 4fb0caf
Show file tree
Hide file tree
Showing 24 changed files with 2,142 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
public class BinaryPayload extends Payload implements Message {

// Accessors for discriminator values.
public Boolean getExtensible() {
return (boolean) false;
public Boolean getBinary() {
return (boolean) true;
}

// Properties.
Expand Down Expand Up @@ -86,7 +86,7 @@ public int getLengthInBits() {
}

public static PayloadBuilder staticParsePayloadBuilder(
ReadBuffer readBuffer, Boolean extensible, Long byteCount) throws ParseException {
ReadBuffer readBuffer, Boolean binary, Long byteCount) throws ParseException {
readBuffer.pullContext("BinaryPayload");
PositionAware positionAware = readBuffer;
boolean _lastItem = ThreadLocalHelper.lastItemThreadLocal.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
public class ExtensiblePayload extends Payload implements Message {

// Accessors for discriminator values.
public Boolean getExtensible() {
return (boolean) true;
public Boolean getBinary() {
return (boolean) false;
}

// Properties.
Expand Down Expand Up @@ -84,7 +84,7 @@ public int getLengthInBits() {
}

public static PayloadBuilder staticParsePayloadBuilder(
ReadBuffer readBuffer, Boolean extensible, Long byteCount) throws ParseException {
ReadBuffer readBuffer, Boolean binary, Long byteCount) throws ParseException {
readBuffer.pullContext("ExtensiblePayload");
PositionAware positionAware = readBuffer;
boolean _lastItem = ThreadLocalHelper.lastItemThreadLocal.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public int getLengthInBits() {
return lengthInBits;
}

public static MessagePDU staticParse(ReadBuffer readBuffer, Boolean response)
public static MessagePDU staticParse(ReadBuffer readBuffer, Boolean response, Boolean binary)
throws ParseException {
readBuffer.pullContext("MessagePDU");
PositionAware positionAware = readBuffer;
Expand All @@ -126,29 +126,34 @@ public static MessagePDU staticParse(ReadBuffer readBuffer, Boolean response)
MessagePDUBuilder builder = null;
if (EvaluationHelper.equals(messageType, (String) "HEL")
&& EvaluationHelper.equals(response, (boolean) false)) {
builder = OpcuaHelloRequest.staticParseMessagePDUBuilder(readBuffer, response);
builder = OpcuaHelloRequest.staticParseMessagePDUBuilder(readBuffer, response, binary);
} else if (EvaluationHelper.equals(messageType, (String) "ACK")
&& EvaluationHelper.equals(response, (boolean) true)) {
builder = OpcuaAcknowledgeResponse.staticParseMessagePDUBuilder(readBuffer, response);
builder = OpcuaAcknowledgeResponse.staticParseMessagePDUBuilder(readBuffer, response, binary);
} else if (EvaluationHelper.equals(messageType, (String) "OPN")
&& EvaluationHelper.equals(response, (boolean) false)) {
builder = OpcuaOpenRequest.staticParseMessagePDUBuilder(readBuffer, totalLength, response);
builder =
OpcuaOpenRequest.staticParseMessagePDUBuilder(readBuffer, totalLength, response, binary);
} else if (EvaluationHelper.equals(messageType, (String) "OPN")
&& EvaluationHelper.equals(response, (boolean) true)) {
builder = OpcuaOpenResponse.staticParseMessagePDUBuilder(readBuffer, totalLength, response);
builder =
OpcuaOpenResponse.staticParseMessagePDUBuilder(readBuffer, totalLength, response, binary);
} else if (EvaluationHelper.equals(messageType, (String) "CLO")
&& EvaluationHelper.equals(response, (boolean) false)) {
builder = OpcuaCloseRequest.staticParseMessagePDUBuilder(readBuffer, response);
builder = OpcuaCloseRequest.staticParseMessagePDUBuilder(readBuffer, response, binary);
} else if (EvaluationHelper.equals(messageType, (String) "MSG")
&& EvaluationHelper.equals(response, (boolean) false)) {
builder = OpcuaMessageRequest.staticParseMessagePDUBuilder(readBuffer, totalLength, response);
builder =
OpcuaMessageRequest.staticParseMessagePDUBuilder(
readBuffer, totalLength, response, binary);
} else if (EvaluationHelper.equals(messageType, (String) "MSG")
&& EvaluationHelper.equals(response, (boolean) true)) {
builder =
OpcuaMessageResponse.staticParseMessagePDUBuilder(readBuffer, totalLength, response);
OpcuaMessageResponse.staticParseMessagePDUBuilder(
readBuffer, totalLength, response, binary);
} else if (EvaluationHelper.equals(messageType, (String) "ERR")
&& EvaluationHelper.equals(response, (boolean) true)) {
builder = OpcuaMessageError.staticParseMessagePDUBuilder(readBuffer, response);
builder = OpcuaMessageError.staticParseMessagePDUBuilder(readBuffer, response, binary);
}
if (builder == null) {
throw new ParseException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public int getLengthInBits() {
return lengthInBits;
}

public static OpcuaAPU staticParse(ReadBuffer readBuffer, Boolean response)
public static OpcuaAPU staticParse(ReadBuffer readBuffer, Boolean response, Boolean binary)
throws ParseException {
readBuffer.pullContext("OpcuaAPU");
PositionAware positionAware = readBuffer;
Expand All @@ -90,7 +90,9 @@ public static OpcuaAPU staticParse(ReadBuffer readBuffer, Boolean response)
MessagePDU message =
readSimpleField(
"message",
readComplex(() -> MessagePDU.staticParse(readBuffer, (boolean) (response)), readBuffer),
readComplex(
() -> MessagePDU.staticParse(readBuffer, (boolean) (response), (boolean) (binary)),
readBuffer),
WithOption.WithByteOrder(ByteOrder.LITTLE_ENDIAN));

readBuffer.closeContext("OpcuaAPU");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public int getLengthInBits() {
}

public static MessagePDUBuilder staticParseMessagePDUBuilder(
ReadBuffer readBuffer, Boolean response) throws ParseException {
ReadBuffer readBuffer, Boolean response, Boolean binary) throws ParseException {
readBuffer.pullContext("OpcuaAcknowledgeResponse");
PositionAware positionAware = readBuffer;
boolean _lastItem = ThreadLocalHelper.lastItemThreadLocal.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public int getLengthInBits() {
}

public static MessagePDUBuilder staticParseMessagePDUBuilder(
ReadBuffer readBuffer, Boolean response) throws ParseException {
ReadBuffer readBuffer, Boolean response, Boolean binary) throws ParseException {
readBuffer.pullContext("OpcuaCloseRequest");
PositionAware positionAware = readBuffer;
boolean _lastItem = ThreadLocalHelper.lastItemThreadLocal.get();
Expand All @@ -114,7 +114,8 @@ public static MessagePDUBuilder staticParseMessagePDUBuilder(
readSimpleField(
"message",
readComplex(
() -> Payload.staticParse(readBuffer, (boolean) (false), (long) (0L)), readBuffer));
() -> Payload.staticParse(readBuffer, (boolean) (binary), (long) (0L)),
readBuffer));

readBuffer.closeContext("OpcuaCloseRequest");
// Create the instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public int getLengthInBits() {
}

public static MessagePDUBuilder staticParseMessagePDUBuilder(
ReadBuffer readBuffer, Boolean response) throws ParseException {
ReadBuffer readBuffer, Boolean response, Boolean binary) throws ParseException {
readBuffer.pullContext("OpcuaHelloRequest");
PositionAware positionAware = readBuffer;
boolean _lastItem = ThreadLocalHelper.lastItemThreadLocal.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public int getLengthInBits() {
}

public static MessagePDUBuilder staticParseMessagePDUBuilder(
ReadBuffer readBuffer, Boolean response) throws ParseException {
ReadBuffer readBuffer, Boolean response, Boolean binary) throws ParseException {
readBuffer.pullContext("OpcuaMessageError");
PositionAware positionAware = readBuffer;
boolean _lastItem = ThreadLocalHelper.lastItemThreadLocal.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public int getLengthInBits() {
}

public static MessagePDUBuilder staticParseMessagePDUBuilder(
ReadBuffer readBuffer, Long totalLength, Boolean response) throws ParseException {
ReadBuffer readBuffer, Long totalLength, Boolean response, Boolean binary)
throws ParseException {
readBuffer.pullContext("OpcuaMessageRequest");
PositionAware positionAware = readBuffer;
boolean _lastItem = ThreadLocalHelper.lastItemThreadLocal.get();
Expand All @@ -117,7 +118,7 @@ public static MessagePDUBuilder staticParseMessagePDUBuilder(
() ->
Payload.staticParse(
readBuffer,
(boolean) (false),
(boolean) (binary),
(long) (((totalLength) - (securityHeader.getLengthInBytes())) - (16L))),
readBuffer));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public int getLengthInBits() {
}

public static MessagePDUBuilder staticParseMessagePDUBuilder(
ReadBuffer readBuffer, Long totalLength, Boolean response) throws ParseException {
ReadBuffer readBuffer, Long totalLength, Boolean response, Boolean binary)
throws ParseException {
readBuffer.pullContext("OpcuaMessageResponse");
PositionAware positionAware = readBuffer;
boolean _lastItem = ThreadLocalHelper.lastItemThreadLocal.get();
Expand All @@ -117,7 +118,7 @@ public static MessagePDUBuilder staticParseMessagePDUBuilder(
() ->
Payload.staticParse(
readBuffer,
(boolean) (false),
(boolean) (binary),
(long) (((totalLength) - (securityHeader.getLengthInBytes())) - (16L))),
readBuffer));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public int getLengthInBits() {
}

public static MessagePDUBuilder staticParseMessagePDUBuilder(
ReadBuffer readBuffer, Long totalLength, Boolean response) throws ParseException {
ReadBuffer readBuffer, Long totalLength, Boolean response, Boolean binary)
throws ParseException {
readBuffer.pullContext("OpcuaOpenRequest");
PositionAware positionAware = readBuffer;
boolean _lastItem = ThreadLocalHelper.lastItemThreadLocal.get();
Expand All @@ -119,7 +120,7 @@ public static MessagePDUBuilder staticParseMessagePDUBuilder(
() ->
Payload.staticParse(
readBuffer,
(boolean) (false),
(boolean) (binary),
(long) (((totalLength) - (openRequest.getLengthInBytes())) - (16L))),
readBuffer));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public int getLengthInBits() {
}

public static MessagePDUBuilder staticParseMessagePDUBuilder(
ReadBuffer readBuffer, Long totalLength, Boolean response) throws ParseException {
ReadBuffer readBuffer, Long totalLength, Boolean response, Boolean binary)
throws ParseException {
readBuffer.pullContext("OpcuaOpenResponse");
PositionAware positionAware = readBuffer;
boolean _lastItem = ThreadLocalHelper.lastItemThreadLocal.get();
Expand All @@ -119,7 +120,7 @@ public static MessagePDUBuilder staticParseMessagePDUBuilder(
() ->
Payload.staticParse(
readBuffer,
(boolean) (false),
(boolean) (binary),
(long) (((totalLength) - (openResponse.getLengthInBytes())) - (16L))),
readBuffer));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public abstract class Payload implements Message {

// Abstract accessors for discriminator values.
public abstract Boolean getExtensible();
public abstract Boolean getBinary();

// Properties.
protected final SequenceHeader sequenceHeader;
Expand Down Expand Up @@ -88,7 +88,7 @@ public int getLengthInBits() {
return lengthInBits;
}

public static Payload staticParse(ReadBuffer readBuffer, Boolean extensible, Long byteCount)
public static Payload staticParse(ReadBuffer readBuffer, Boolean binary, Long byteCount)
throws ParseException {
readBuffer.pullContext("Payload");
PositionAware positionAware = readBuffer;
Expand All @@ -101,18 +101,14 @@ public static Payload staticParse(ReadBuffer readBuffer, Boolean extensible, Lon

// Switch Field (Depending on the discriminator values, passes the instantiation to a sub-type)
PayloadBuilder builder = null;
if (EvaluationHelper.equals(extensible, (boolean) true)) {
builder = ExtensiblePayload.staticParsePayloadBuilder(readBuffer, extensible, byteCount);
} else if (EvaluationHelper.equals(extensible, (boolean) false)) {
builder = BinaryPayload.staticParsePayloadBuilder(readBuffer, extensible, byteCount);
if (EvaluationHelper.equals(binary, (boolean) false)) {
builder = ExtensiblePayload.staticParsePayloadBuilder(readBuffer, binary, byteCount);
} else if (EvaluationHelper.equals(binary, (boolean) true)) {
builder = BinaryPayload.staticParsePayloadBuilder(readBuffer, binary, byteCount);
}
if (builder == null) {
throw new ParseException(
"Unsupported case for discriminated type"
+ " parameters ["
+ "extensible="
+ extensible
+ "]");
"Unsupported case for discriminated type" + " parameters [" + "binary=" + binary + "]");
}

readBuffer.closeContext("Payload");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected boolean awaitDiscoverComplete() {

@Override
protected ProtocolStackConfigurer<OpcuaAPU> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(OpcuaAPU.class, io -> OpcuaAPU.staticParse(io, true))
return SingleProtocolStackConfigurer.builder(OpcuaAPU.class, io -> OpcuaAPU.staticParse(io, true, true))
.withProtocol(OpcuaProtocolLogic.class)
.withPacketSizeEstimator(ByteLengthEstimator.class)
.withDriverContext(OpcuaDriverContext.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,19 +315,22 @@ private CompletableFuture<ActivateSessionResponse> onConnectActivateSessionReque
conversation.setRemoteNonce(sessionResponse.getServerNonce().getStringValue());

List<String> contactPoints = new ArrayList<>(3);
String port = driverContext.getPort() == null ? "" : ":" + driverContext.getPort();
try {
InetAddress address = InetAddress.getByName(driverContext.getHost());
contactPoints.add("opc.tcp://" + address.getHostAddress() + ":" + driverContext.getPort() + driverContext.getTransportEndpoint());
contactPoints.add("opc.tcp://" + address.getHostName() + ":" + driverContext.getPort() + driverContext.getTransportEndpoint());
contactPoints.add("opc.tcp://" + address.getCanonicalHostName() + ":" + driverContext.getPort() + driverContext.getTransportEndpoint());
contactPoints.add("opc.tcp://" + address.getHostAddress() + port + driverContext.getTransportEndpoint());
contactPoints.add("opc.tcp://" + address.getHostName() + port + driverContext.getTransportEndpoint());
contactPoints.add("opc.tcp://" + address.getCanonicalHostName() + port + driverContext.getTransportEndpoint());
} catch (UnknownHostException e) {
LOGGER.debug("error getting host", e);
// fall back to declared host
contactPoints.add("opc.tcp://" + driverContext.getHost() + port + driverContext.getTransportEndpoint());
LOGGER.warn("Could not reach host {}, possible network failure", driverContext.getHost(), e);
}

Entry<EndpointDescription, UserTokenPolicy> selectedEndpoint = selectEndpoint(sessionResponse.getServerEndpoints(), contactPoints,
configuration.getSecurityPolicy(), configuration.getMessageSecurity());
if (selectedEndpoint == null) {
throw new PlcRuntimeException("Unable to find endpoint matching - " + contactPoints.get(1));
throw new PlcRuntimeException("Unable to find endpoint matching " + contactPoints.get(0));
}

PascalString policyId = selectedEndpoint.getValue().getPolicyId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void onConnect(ConversationContext<OpcuaAPU> context) {
LOGGER.error("Failed to establish connection", error);
return;
}
LOGGER.info("Established connection to server", error);
LOGGER.info("Established connection to server");
context.fireConnected();
});
}
Expand Down Expand Up @@ -236,7 +236,7 @@ public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
ReadRequest opcuaReadRequest = new ReadRequest(
requestHeader,
0.0d,
TimestampsToReturn.timestampsToReturnNeither,
TimestampsToReturn.timestampsToReturnBoth,
readValueArray
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ private static byte[] serialize(Message message) throws SerializationException {
return buffer.getBytes();
}

public static Payload fromStream(byte[] payload, boolean extensible) throws ParseException {
public static Payload fromStream(byte[] payload, boolean binary) throws ParseException {
ReadBufferByteBased buffer = new ReadBufferByteBased(payload, ByteOrder.LITTLE_ENDIAN);
return Payload.staticParse(buffer, extensible, (long) (extensible ? -1 : payload.length - 8));
return Payload.staticParse(buffer, binary, (long) (binary ? payload.length - 8 : -1));
}

public static MessagePDU fromStream(ByteBuffer chunkBuffer, boolean response) throws ParseException {
ReadBufferByteBased buffer = new ReadBufferByteBased(chunkBuffer.array(), ByteOrder.LITTLE_ENDIAN);
return MessagePDU.staticParse(buffer, response);
return MessagePDU.staticParse(buffer, response, true);
}

public static MessagePDU pduFromStream(byte[] message, boolean response) throws ParseException {
ReadBufferByteBased buffer = new ReadBufferByteBased(message, ByteOrder.LITTLE_ENDIAN);
return MessagePDU.staticParse(buffer, response);
return MessagePDU.staticParse(buffer, response, true);
}
}
Loading

0 comments on commit 4fb0caf

Please sign in to comment.