Search and contents

Trying a config against a stand-in target system

Write a config for a target system's OPC UA server before it can be reached: list its nodes, serve them from a stand-in, check the config against the stand-in, and preview what its mapping makes of a sample.

This page runs those four steps in order, with the commands and the output they print. Every command is run by a test, so what is shown is what the tools do. Each tool has a page of its own with every option and exit code; the fixture kit says how they fit together.

A server can't always be reached while a config is written: it is on another network, it is in production, or it is not built yet. A stand-in lets the config be written and checked now, and run against the real thing later, with one line changed.

What is covered and what is not

Three of the commands here are covered by the stability policy (public-api-stability.md): the browse command, the offline-check command and the mapping-preview command. Their exit codes and the names and meanings of their options are covered, and the text they print is not, so do not parse it. The file-driven node server is test tooling and is not covered. Its options, its node file format and its exit codes may change in any release (opcua-node-server.md).

The stand-in is not the target system. It serves single values, directly under the Objects folder, on a security policy of None, and nothing else of a real server. It is for a config to read while the real server is out of reach.

Before you start

The commands run from the repository root with the project's Python environment active (or prefixed with poetry run), and need two programs:

  • The kindred-opcua-sidecar program on PATH, as a Node needs it. Browse and the connection check both use it.
  • The node server, built with sidecar/node_server/build.sh (it needs cmake, a C compiler and the open62541 library that deployment/install_open62541.sh installs). The program is left at sidecar/node_server/build/kindred-opcua-node-server.

Setup and the commands in this document

Every command below runs as written once this block has been run in the same shell. It starts a first server, which plays the target system so there is something to list: on a real project you leave it out, because your config's endpoint names the target system. It also makes a copy of the MES example config that points at that server. The nodes, their types and the order are made up, and the data is synthetic. Servers started here stop when the shell exits. They use ports 4840 and 4841; set TARGET_PORT and STANDIN_PORT first if either is taken.

WORK=$(mktemp -d)
TARGET_PORT=${TARGET_PORT:-4840}
STANDIN_PORT=${STANDIN_PORT:-4841}
SERVER=sidecar/node_server/build/kindred-opcua-node-server
cat > "$WORK/target-nodes.txt" <<'NODES'
ns=1;i=5000             string  ro      Order         MD-2027-0142
ns=2;s=Line1.State      string  ro      State         RUN
ns=2;s=Line1.Setpoint   double  rw      Setpoint      72.5
ns=2;s=Line1.Enabled    boolean ro      Enabled       true
NODES
"$SERVER" "$WORK/target-nodes.txt" --port "$TARGET_PORT" > "$WORK/target-out.txt" &
trap 'kill $(jobs -p) 2>/dev/null' EXIT
for _ in $(seq 50); do grep -q listening "$WORK/target-out.txt" && break; sleep 0.1; done
grep -q listening "$WORK/target-out.txt" || echo "The target server did not start: is port $TARGET_PORT free?" >&2
sed "s/127.0.0.1:4840/127.0.0.1:$TARGET_PORT/" node-types/mes/example_node_config.json > "$WORK/target.json"

Step 1: list what the target offers

The browse command takes the config and a directory of Node Type definitions (node-types in this repository). It connects the way a Node does, and prints each node below the Objects folder with its id, class, data type and browse name. --max-depth 1 lists only what is directly under it, which is all this server has. A target with folders needs a deeper listing (--max-depth goes to 8, and --node-id starts the walk at a folder). The conversion below reads the variables at any depth, and the stand-in serves them all directly under the Objects folder, so the ids a config reads are the same. The listing is kept in a file for the next step.

python -m engine.opcua_browse_cli "$WORK/target.json" node-types --max-depth 1 > "$WORK/listing.txt" \
  && cat "$WORK/listing.txt"
Browsing opc.tcp://127.0.0.1:4840 from the Objects folder, to a depth of 1.
i=2253  Object  0:Server
ns=1;i=5000  Variable  String  1:Order
ns=2;s=Line1.State  Variable  String  2:State
ns=2;s=Line1.Setpoint  Variable  Double  2:Setpoint
ns=2;s=Line1.Enabled  Variable  Boolean  2:Enabled
5 nodes listed.

Each id is exactly what goes in a connector's node_id, so the config does not need them written down first. The first node, i=2253, is in namespace 0: it is the server's own, not one of the target's nodes, and it is not served from a node file. The listing does not read any value and does not say whether a node can be written.

Step 2: make the stand-in

A node file has one node per line: the id, the type in lower case, an access mode, the browse name without its namespace, and an optional value. The listing holds the first, the second (as a name with capitals, such as Double) and the fourth, so this awk command makes the file from it:

  • It keeps the variables in namespace 1 and above, and leaves out the server's own. It also leaves out a variable the listing gave no type for, and one whose id or browse name holds a space, because a node file cannot hold either; the lines it leaves out are the ones to look at by hand.
  • It writes ro, read-only, for each, because a listing does not say what may be written.
  • It writes no value, so each node starts at its type's zero. Add a value to a line to give a node one.

A node of a type the node server does not serve (its page lists ten) has to be left out of the file or given one of those, and a node file is only as faithful as the listing.

awk '$2 == "Variable" && NF == 4 && $1 ~ /^ns=[1-9]/ { split($4, name, ":"); print $1, tolower($3), "ro", name[2] }' \
  "$WORK/listing.txt" > "$WORK/standin-nodes.txt" && cat "$WORK/standin-nodes.txt"
ns=1;i=5000 string ro Order
ns=2;s=Line1.State string ro State
ns=2;s=Line1.Setpoint double ro Setpoint
ns=2;s=Line1.Enabled boolean ro Enabled

The server checks a file before it serves anything:

"$SERVER" "$WORK/standin-nodes.txt" --check
kindred-opcua-node-server: 4 nodes, file is valid

Serve it on a second port, and make the config for it. The config differs from the target's in one line, the endpoint:

"$SERVER" "$WORK/standin-nodes.txt" --port "$STANDIN_PORT" > "$WORK/standin-out.txt" &
for _ in $(seq 50); do grep -q listening "$WORK/standin-out.txt" && break; sleep 0.1; done
cat "$WORK/standin-out.txt"
kindred-opcua-node-server: listening on opc.tcp://0.0.0.0:4841, 4 nodes
sed "s/127.0.0.1:$TARGET_PORT/127.0.0.1:$STANDIN_PORT/" "$WORK/target.json" > "$WORK/stand-in.json"
diff "$WORK/target.json" "$WORK/stand-in.json"
7c7
<       "endpoint": "opc.tcp://127.0.0.1:4840",
---
>       "endpoint": "opc.tcp://127.0.0.1:4841",

diff exits 1 when the files differ, which is what is wanted here. The same config now reads the stand-in; to read the target system later, put its address back in the endpoint and change nothing else. Browsing the stand-in lists the same nodes as the target (the header line, which names the endpoint, is the only one that differs, so it is left out of the comparison):

python -m engine.opcua_browse_cli "$WORK/stand-in.json" node-types --max-depth 1 > "$WORK/stand-in-listing.txt" \
  && diff <(tail -n +2 "$WORK/listing.txt") <(tail -n +2 "$WORK/stand-in-listing.txt") \
  && echo "The two servers list the same nodes."
The two servers list the same nodes.

Step 3: check the config, then try its connection

The offline check validates the config the way a Node does when it loads one. It reaches no target:

python -m engine.offline_check_cli "$WORK/stand-in.json" node-types
Offline check: the config is valid (Node Type MES).

With --connect, a config that passes also has each connector tested against its target. --show-sample prints what the connector read, which is the node the config names, here at its type's zero, because the stand-in was made from a listing that holds no values:

python -m engine.offline_check_cli "$WORK/stand-in.json" node-types --connect --show-sample
Offline check: the config is valid (Node Type MES).
Connection check: passed.
  opcua (opcua) -> opc.tcp://127.0.0.1:4841: passed: connected
    sample: ""
    The config's own secrets were removed. Anything else is shown as the target returned it, cut to a size, and may be sensitive plant data.

The stand-in has to serve every node the config reads. The MES example reads ns=1;i=5000, which the listing held. A config that names a node the stand-in lacks fails its connection check, with exit 3:

sed 's/ns=1;i=5000/ns=2;s=Missing/' "$WORK/stand-in.json" > "$WORK/missing.json"
python -m engine.offline_check_cli "$WORK/missing.json" node-types --connect
Offline check: the config is valid (Node Type MES).
Connection check: failed.
  opcua (opcua) -> opc.tcp://127.0.0.1:4841: failed: the node id was not found on the target (BadNodeIdUnknown)

Step 4: preview the mapping over a sample

The mapping preview runs a file of source records through the config's mapping, offline, with the transform a Node uses, and reports where the mapping leaves gaps. The sample is a JSON array or JSON lines file. The MES example config's mapping reads one source key, value, into order_id; for your own config, write the keys its mapping reads. These records are written by hand, and their values are made up:

cat > "$WORK/sample.json" <<'JSON'
[{"value": "MD-2027-0142"}, {"value": "MD-2027-0143"}]
JSON
python -m engine.mapping_preview_cli "$WORK/stand-in.json" node-types "$WORK/sample.json"
Mapping preview: no gaps found (Node Type MES, section opcua).
  records: 2 (2 matched, 0 unmatched, 0 flagged)

A sample with a field nothing reads, and a record without the field the mapping needs, shows the gaps, with exit 1. The report names fields and counts them, and never prints a value:

cat > "$WORK/sample-gaps.json" <<'JSON'
[{"value": "MD-2027-0142", "temp": 71.2}, {"temp": 70.9}]
JSON
python -m engine.mapping_preview_cli "$WORK/stand-in.json" node-types "$WORK/sample-gaps.json"
Mapping preview: gaps found (Node Type MES, section opcua).
  records: 2 (2 matched, 0 unmatched, 0 flagged)
  fields left empty (records): order_id (1)
  record fields never read (records): temp (2)
  record 0: unread: temp
  record 1: empty: order_id; unread: temp

What a passing run does not tell you

  • A passing check does not prove the target behaves that way. The connection check shows that a node the config names can be read from the stand-in. A node mistyped, or a value the target reports differently, passes here and fails there. Run the same checks against the target system when it can be reached.
  • The stand-in holds what the listing held. It has no folders, methods, history or security, and a config that depends on them is not exercised by it. Its values are zero or made up.
  • The preview shows what the mapping does with the records you give it. A real sample from the target system tells more than a written one.

Where next

Release: v0.11.0