You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2026/04/07 12:19:03 UTC

(streampipes) branch dev updated: fix: normalize OPC UA unsigned values (#4356)

This is an automated email from the ASF dual-hosted git repository.

zehnder pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to refs/heads/dev by this push:
     new dcbad78f38 fix: normalize OPC UA unsigned values (#4356)
dcbad78f38 is described below

commit dcbad78f38f6916911f3d702c65bb24f4cdb592f
Author: Philipp Zehnder <te...@users.noreply.github.com>
AuthorDate: Tue Apr 7 14:18:57 2026 +0200

    fix: normalize OPC UA unsigned values (#4356)
---
 .../opcua/model/node/OpcUaNumberNormalizer.java    |  50 ++++++++
 .../opcua/model/node/ScalarOpcUaNode.java          |   2 +-
 .../opcua/model/node/StructuredOpcUaNode.java      |   2 +
 .../connectors/opcua/utils/OpcUaTypes.java         |  91 -------------
 .../node/OpcUaNodeUnsignedNormalizationTest.java   | 141 +++++++++++++++++++++
 .../adapters/OpcUaAdapterIntegrationTest.java      |  18 +--
 6 files changed, 203 insertions(+), 101 deletions(-)

diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/OpcUaNumberNormalizer.java b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/OpcUaNumberNormalizer.java
new file mode 100644
index 0000000000..e6708b30d7
--- /dev/null
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/OpcUaNumberNormalizer.java
@@ -0,0 +1,50 @@
+/*
+ * 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
+ *
+ *    http://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.
+ *
+ */
+
+package org.apache.streampipes.extensions.connectors.opcua.model.node;
+
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
+
+final class OpcUaNumberNormalizer {
+
+  private OpcUaNumberNormalizer() {
+  }
+
+  static Object normalize(Object value) {
+    if (value instanceof UByte uByte) {
+      return uByte.intValue();
+    }
+
+    if (value instanceof UShort uShort) {
+      return uShort.intValue();
+    }
+
+    if (value instanceof UInteger uInteger) {
+      return uInteger.longValue();
+    }
+
+    if (value instanceof ULong uLong) {
+      return uLong.longValue();
+    }
+
+    return value;
+  }
+}
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/ScalarOpcUaNode.java b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/ScalarOpcUaNode.java
index 9b85d21bba..0a4ff3c6f0 100644
--- a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/ScalarOpcUaNode.java
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/ScalarOpcUaNode.java
@@ -99,7 +99,7 @@ public class ScalarOpcUaNode implements OpcUaNode {
       return ((XmlElement) rawValue).getFragment();
     }
 
-    return rawValue;
+    return OpcUaNumberNormalizer.normalize(rawValue);
   }
 
   private boolean isByteStringArray(Object value) {
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/StructuredOpcUaNode.java b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/StructuredOpcUaNode.java
index 8197658c07..696cdf6322 100644
--- a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/StructuredOpcUaNode.java
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/model/node/StructuredOpcUaNode.java
@@ -176,6 +176,8 @@ public class StructuredOpcUaNode implements OpcUaNode {
       return uaEnumeratedType.getName() != null ? uaEnumeratedType.getName() : uaEnumeratedType.getValue();
     }
 
+    value = OpcUaNumberNormalizer.normalize(value);
+
     if (isScalar(value)) {
       return value;
     }
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaTypes.java b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaTypes.java
deleted file mode 100644
index 1a891f40d4..0000000000
--- a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaTypes.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * 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
- *
- *    http://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.
- *
- */
-
-package org.apache.streampipes.extensions.connectors.opcua.utils;
-
-import org.apache.streampipes.sdk.utils.Datatypes;
-
-import org.eclipse.milo.opcua.sdk.core.nodes.VariableNode;
-import org.eclipse.milo.opcua.stack.core.Identifiers;
-import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
-
-import java.util.Objects;
-
-public class OpcUaTypes {
-
-  /**
-   * Maps OPC UA data types to internal StreamPipes data types
-   *
-   * @param o data type id as UInteger
-   * @return StreamPipes internal data type
-   */
-  public static Datatypes getType(UInteger o) {
-    if (UInteger.valueOf(4).equals(o)
-        | UInteger.valueOf(5).equals(o)
-        | UInteger.valueOf(6).equals(o)
-        | UInteger.valueOf(7).equals(o)
-        | UInteger.valueOf(8).equals(o)
-        | UInteger.valueOf(9).equals(o)
-        | UInteger.valueOf(27).equals(o)) {
-      return Datatypes.Integer;
-    } else if (UInteger.valueOf(8).equals(o)) {
-      return Datatypes.Long;
-    } else if (UInteger.valueOf(11).equals(o)) {
-      return Datatypes.Double;
-    } else if (UInteger.valueOf(10).equals(o) | UInteger.valueOf(26).equals(o) | UInteger.valueOf(50).equals(o)) {
-      return Datatypes.Float;
-    } else if (UInteger.valueOf(1).equals(o)) {
-      return Datatypes.Boolean;
-    } else if (UInteger.valueOf(12).equals(o)) {
-      return Datatypes.String;
-    } else if (UInteger.valueOf(13).equals(o)) {
-      return Datatypes.Long;
-    }
-
-    return Datatypes.String;
-  }
-
-  public static Datatypes getTypeFromValue(Object value) {
-    if (value instanceof Boolean) {
-      return Datatypes.Boolean;
-    } else if (value instanceof Integer) {
-      return Datatypes.Integer;
-    } else if (value instanceof Long) {
-      return Datatypes.Long;
-    } else if (value instanceof Float) {
-      return Datatypes.Float;
-    } else if (value instanceof Double) {
-      return Datatypes.Double;
-    } else {
-      return Datatypes.String;
-    }
-  }
-
-  /**
-   * Determines if the node is an extension data type or a custom data type
-   *
-   * @param node a data variable node
-   * @return true if the node is an ExtensionObject or custom data type
-   */
-  public static boolean isExtensionOrCustom(VariableNode node) {
-    var dataType = node.getDataType();
-    return Objects.equals(dataType, Identifiers.Structure)
-        || (dataType != null && dataType.getNamespaceIndex().intValue() != 0);
-  }
-
-}
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/model/node/OpcUaNodeUnsignedNormalizationTest.java b/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/model/node/OpcUaNodeUnsignedNormalizationTest.java
new file mode 100644
index 0000000000..e98c125c99
--- /dev/null
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/model/node/OpcUaNodeUnsignedNormalizationTest.java
@@ -0,0 +1,141 @@
+/*
+ * 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
+ *
+ *    http://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.
+ *
+ */
+
+package org.apache.streampipes.extensions.connectors.opcua.model.node;
+
+import org.eclipse.milo.opcua.stack.core.types.builtin.Matrix;
+import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.ULong;
+import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class OpcUaNodeUnsignedNormalizationTest {
+
+  @Test
+  void scalarNodeNormalizesUnsignedByte() {
+    assertScalarValue(UByte.valueOf(255), 255);
+  }
+
+  @Test
+  void scalarNodeNormalizesUnsignedShort() {
+    assertScalarValue(UShort.valueOf(65535), 65535);
+  }
+
+  @Test
+  void scalarNodeNormalizesUnsignedInteger() {
+    assertScalarValue(UInteger.valueOf(4294967295L), 4294967295L);
+  }
+
+  @Test
+  void scalarNodeNormalizesUnsignedLongToBigInteger() {
+    assertScalarValue(ULong.MAX, -1L);
+  }
+
+  @Test
+  void scalarNodeNormalizesUnsignedValuesInMatrix() {
+    var event = new HashMap<String, Object>();
+
+    new ScalarOpcUaNode(mockNodeInfo()).addToEvent(
+        null,
+        event,
+        new Variant(new Matrix(new ULong[][] {{ULong.MAX}}))
+    );
+
+    assertEquals(
+        Map.of(
+            "testNode",
+            Map.of(
+                "elements",
+                List.of(-1L),
+                "dimensions",
+                List.of(1, 1)
+            )
+        ),
+        event
+    );
+  }
+
+  @Test
+  void structuredNodeNormalizesUnsignedByte() {
+    assertStructuredValue(UByte.valueOf(255), 255);
+  }
+
+  @Test
+  void structuredNodeNormalizesUnsignedShort() {
+    assertStructuredValue(UShort.valueOf(65535), 65535);
+  }
+
+  @Test
+  void structuredNodeNormalizesUnsignedInteger() {
+    assertStructuredValue(UInteger.valueOf(4294967295L), 4294967295L);
+  }
+
+  @Test
+  void structuredNodeNormalizesUnsignedLongToBigInteger() {
+    assertStructuredValue(ULong.MAX, -1L);
+  }
+
+  @Test
+  void structuredNodeNormalizesUnsignedValuesInArray() {
+    var event = new HashMap<String, Object>();
+
+    new StructuredOpcUaNode(mockNodeInfo()).addToEvent(
+        null,
+        event,
+        new Variant(new ULong[] {ULong.MAX, ULong.valueOf(1L)})
+    );
+
+    assertEquals(
+        List.of(-1L, 1L),
+        event.get("testNode")
+    );
+  }
+
+  private void assertScalarValue(Object inputValue, Object expectedValue) {
+    var event = new HashMap<String, Object>();
+
+    new ScalarOpcUaNode(mockNodeInfo()).addToEvent(null, event, new Variant(inputValue));
+
+    assertEquals(expectedValue, event.get("testNode"));
+  }
+
+  private void assertStructuredValue(Object inputValue, Object expectedValue) {
+    var event = new HashMap<String, Object>();
+
+    new StructuredOpcUaNode(mockNodeInfo()).addToEvent(null, event, new Variant(inputValue));
+
+    assertEquals(expectedValue, event.get("testNode"));
+  }
+
+  private BasicVariableNodeInfo mockNodeInfo() {
+    var nodeInfo = mock(BasicVariableNodeInfo.class);
+    when(nodeInfo.getDesiredName("")).thenReturn("testNode");
+    when(nodeInfo.getBaseNodeName()).thenReturn("testNode");
+    return nodeInfo;
+  }
+}
diff --git a/streampipes-integration-tests/src/test/java/org/apache/streampipes/integration/adapters/OpcUaAdapterIntegrationTest.java b/streampipes-integration-tests/src/test/java/org/apache/streampipes/integration/adapters/OpcUaAdapterIntegrationTest.java
index c702b86a0a..216e4e0809 100644
--- a/streampipes-integration-tests/src/test/java/org/apache/streampipes/integration/adapters/OpcUaAdapterIntegrationTest.java
+++ b/streampipes-integration-tests/src/test/java/org/apache/streampipes/integration/adapters/OpcUaAdapterIntegrationTest.java
@@ -89,7 +89,7 @@ public class OpcUaAdapterIntegrationTest {
   public void testScalarByteNode() throws Exception {
     assertSingleNodeEvent(
         "ns=2;s=CTT.Static.AllProfiles.Scalar.Byte",
-        Map.of("Byte", UByte.valueOf(0))
+        Map.of("Byte", 0)
     );
   }
 
@@ -283,7 +283,7 @@ public class OpcUaAdapterIntegrationTest {
   public void testScalarUInt16Node() throws Exception {
     assertSingleNodeEvent(
         "ns=2;s=CTT.Static.AllProfiles.Scalar.UInt16",
-        Map.of("UInt16", UShort.valueOf(0))
+        Map.of("UInt16", 0)
     );
   }
 
@@ -291,7 +291,7 @@ public class OpcUaAdapterIntegrationTest {
   public void testScalarUInt32Node() throws Exception {
     assertSingleNodeEvent(
         "ns=2;s=CTT.Static.AllProfiles.Scalar.UInt32",
-        Map.of("UInt32", UInteger.valueOf(0L))
+        Map.of("UInt32", 0L)
     );
   }
 
@@ -299,7 +299,7 @@ public class OpcUaAdapterIntegrationTest {
   public void testScalarUInt64Node() throws Exception {
     assertSingleNodeEvent(
         "ns=2;s=CTT.Static.AllProfiles.Scalar.UInt64",
-        Map.of("UInt64", ULong.valueOf(0L))
+        Map.of("UInt64", 0L)
     );
   }
 
@@ -584,7 +584,7 @@ public class OpcUaAdapterIntegrationTest {
     assertSingleNodeMatrixEvent(
         "ns=2;s=CTT.Static.AllProfiles.Matrix.ByteMatrix",
         "ByteMatrix",
-        repeatedMatrix(UByte.valueOf(0))
+        repeatedMatrix(0)
     );
   }
 
@@ -781,7 +781,7 @@ public class OpcUaAdapterIntegrationTest {
     assertSingleNodeMatrixEvent(
         "ns=2;s=CTT.Static.AllProfiles.Matrix.UInt16Matrix",
         "UInt16Matrix",
-        repeatedMatrix(UShort.valueOf(0))
+        repeatedMatrix(0)
     );
   }
 
@@ -790,7 +790,7 @@ public class OpcUaAdapterIntegrationTest {
     assertSingleNodeMatrixEvent(
         "ns=2;s=CTT.Static.AllProfiles.Matrix.UInt32Matrix",
         "UInt32Matrix",
-        repeatedMatrix(UInteger.valueOf(0L))
+        repeatedMatrix(0L)
     );
   }
 
@@ -799,7 +799,7 @@ public class OpcUaAdapterIntegrationTest {
     assertSingleNodeMatrixEvent(
         "ns=2;s=CTT.Static.AllProfiles.Matrix.UInt64Matrix",
         "UInt64Matrix",
-        repeatedMatrix(ULong.valueOf(0L))
+        repeatedMatrix(0L)
     );
   }
 
@@ -854,7 +854,7 @@ public class OpcUaAdapterIntegrationTest {
                     "DoubleField", 1.0d,
                     "StringField", "two",
                     "BooleanField", true,
-                    "UInt32Field", UInteger.valueOf(42L)
+                    "UInt32Field",42L
                 )
             )
         ),