Search and contents

Listing the nodes of an OPC UA server

When a plant's OPC UA server is reachable but its node ids are not written down, one command lists them. It connects the way the Node's own OPC UA connector does, using the connection settings, credentials and certificates of a config's OPC UA section, and prints the nodes below a start node: one line each, indented by depth, with each id in the form a connector's node_id takes.

python -m engine.opcua_browse_cli CONFIG NODE_TYPE_DIR [options]

This command is covered by the stability policy (public-api-stability.md): its exit codes and the names and meanings of its options. The text it prints is for a person to read and may change between releases; do not parse it.

There is no option for a credential or a key, so none ever appears on a command line or in the shell's history. They come from the config's OPC UA section, which is checked the way the Node would check it, and an encrypted config is decrypted the way the Node would (see offline-check.md). Nothing the command prints carries a value of the config, and the sidecar's own error output, which can quote the endpoint, is discarded.

It reads from the server and writes nothing. It runs from the repository root with the project's Python environment active (or prefixed with poetry run), and it needs the kindred-opcua-sidecar program on PATH, as a Node does.

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 the file-driven test server (built with sidecar/node_server/build.sh) on a node file, and makes a copy of the MES example config that points at it, one that points at a port where nothing listens, an invalid one and an encrypted one. The server stops when the shell exits.

WORK=$(mktemp -d)
PORT=${PORT:-4840}
SERVER=sidecar/node_server/build/kindred-opcua-node-server
cat > "$WORK/nodes.txt" <<'NODES'
ns=2;s=Line1.State      string  ro      State         RUN
ns=2;s=Line1.Count      uint32  tick    Count         0
ns=2;s=Line1.Setpoint   double  rw      Setpoint      72.5
ns=2;s=Line1.Enabled    boolean ro      Enabled       true
ns=3;i=1001             int16   ro      Offset        -40
NODES
"$SERVER" "$WORK/nodes.txt" --port "$PORT" > "$WORK/out.txt" &
SERVER_PID=$!
trap 'kill "$SERVER_PID" 2>/dev/null' EXIT
for _ in $(seq 50); do grep -q listening "$WORK/out.txt" && break; sleep 0.1; done
sed "s/127.0.0.1:4840/127.0.0.1:$PORT/" node-types/mes/example_node_config.json > "$WORK/config.json"
sed 's/127.0.0.1:4840/127.0.0.1:1/' node-types/mes/example_node_config.json > "$WORK/unreachable.json"
echo '{"node_type": "MES", "node_type_version": "0.7.0", "config_version": 1, "config": {}}' > "$WORK/invalid.json"
echo '{"sops": {}, "node_type": "ENC[ciphertext]"}' > "$WORK/secret.enc.json"

Listing a server

The command takes the config file and a directory of Node Type definitions (node-types in this repository). --max-depth 1 lists only what is directly under the start node:

python -m engine.opcua_browse_cli "$WORK/config.json" node-types --max-depth 1

It prints:

Browsing opc.tcp://127.0.0.1:4840 from the Objects folder, to a depth of 1.
i=2253  Object  0:Server
ns=2;s=Line1.State  Variable  String  2:State
ns=2;s=Line1.Count  Variable  UInt32  2:Count
ns=2;s=Line1.Setpoint  Variable  Double  2:Setpoint
ns=2;s=Line1.Enabled  Variable  Boolean  2:Enabled
ns=3;i=1001  Variable  Int16  3:Offset
6 nodes listed.

Each line is the node's id, its class, for a Variable its data type, and its browse name (namespace:name). The id is exactly what goes in a connector's node_id. Notes on reading it:

  • Nodes in namespace 0 (i=2253 above, and the rest of the server's own tree) belong to the server itself. The test server's node file takes namespaces 1 to 100 and ids with no space in them, so an id outside that is a node to read, not a line to copy into a node file.
  • A data type is the standard type's name (Int32) or, for any other type, the type's node id. A server that declines to give types still has its variables listed, without one, and the summary says how many.
  • Variables and Methods are listed but not opened. Objects are opened down to --max-depth.
  • A browse name is shown with any byte that is not part of a printable character as ?.

Options

OptionMeaning
--section NAME The config section to connect with. The default is the Node Type's only OPC UA section; MES and PLC / Generic Device have one, opcua. A section that is not an OPC UA section of the Node Type is refused (exit 2).
--node-id ID The node to list below, written as a connector's node_id is (ns=2;s=Line1). The default is the server's Objects folder, not the section's own node_id. The start node itself is not listed.
--max-depth N Levels below the start node, 1 to 8. The default is 2.
--max-nodes N Nodes to list, 1 to 10000. The default is 1000.
--timeout SECONDS How long the walk may take, 0.1 to 20. The default is 20. Connecting is not counted in it, and has its own wait of about 10 seconds.
--shared-dir DIR The shared fragments directory, if the config uses any.

A value outside its range is a usage error (exit 2), and the command never prints back what was typed. A list that stops at --max-nodes says so, and exits 4:

python -m engine.opcua_browse_cli "$WORK/config.json" node-types --max-nodes 2

Exit codes

A script should act on the exit code alone.

CodeMeaningWhat a script should do
0 The listing is complete. Go on.
1 The config is invalid: not JSON, a schema or rule failure, or an unknown Node Type. Fix the config; offline_check_cli names the rule.
2 The command could not run: a usage error, a config that cannot be read or decrypted, a Node Type directory that cannot be used, a section that is not one of the Node Type's OPC UA sections, or a sidecar program that could not be started. Fix the command or the environment. Nothing was browsed.
3 The target could not be browsed: it could not be reached, the start node was refused, it did not answer in time, or it answered in a way the command could not read. The OPC UA status name is printed when there is one (BadNodeIdUnknown, BadUserAccessDenied, BadTimeout, ...). Look at the status: a wrong address, credential or node id, or a target not reachable from this machine.
4 The listing is partial: the node limit was reached, nodes were left out, or the server refused to list the children of a node. What was found is printed. Raise --max-nodes, or narrow the start node, or accept the gap; the summary lines say which it was.
5 No verdict. The config is encrypted and no age key is available. Make the key available and run again.

A variable shown without a data type does not change the exit code, which is about nodes, not about what is known of each.

A server that cannot be reached is exit 3:

python -m engine.opcua_browse_cli "$WORK/unreachable.json" node-types

A start node the server does not know is exit 3 with the status name:

python -m engine.opcua_browse_cli "$WORK/config.json" node-types --node-id "ns=2;s=Nothing.Here"

A section that is not an OPC UA section:

python -m engine.opcua_browse_cli "$WORK/config.json" node-types --section mqtt

An invalid config:

python -m engine.opcua_browse_cli "$WORK/invalid.json" node-types

An encrypted config on a workstation with no age key. The env prefix makes this the case whatever the host has; on a real workstation without a key, the plain command is enough:

env -u SOPS_AGE_KEY -u SOPS_AGE_KEY_CMD -u SOPS_AGE_KEY_FILE XDG_CONFIG_HOME="$WORK/no-keys" \
  python -m engine.opcua_browse_cli "$WORK/secret.enc.json" node-types

Limits

  • A list is a walk, not a snapshot. It is fetched in pages of 200 nodes, and each page repeats the walk from the start and passes over what an earlier page returned. The work is quadratic in the number of pages, and a server that reorders its nodes between pages can show a node twice or miss one. Keep --max-nodes to what you need.
  • Folders of over 200 children are read from the server in pages, and the walk holds that folder's continuation point while it lists what is below. A server that allows only a few per session can refuse a deep walk through several such folders; the refusal is shown on the node as an error, and the exit code is 4.
  • Variables and Methods are not opened, and a node's value is never read.
  • A node whose id cannot be sent back faithfully (it holds a control character, bytes that are not UTF-8, or is longer than 255 bytes) is left out, with what is below it, and counted in the summary.
  • The sidecar is one process, and one connection. A server that allows few sessions may refuse the browse while a Node holds its own.

Release: v0.11.0