Try it in the lab
The lab is a terminal on this site, with a simulated NETCONF device behind it, all running in your browser: nothing is installed and nothing leaves your browser. Its TypeScript tab runs this library, and the guide's examples with a Run button run there. This page opens it; anywhere else, the Lab button in the header does, or Ctrl+`, and it keeps its session as you move between pages. The lab device is a small switch with the IETF interface and system models and a set of OpenConfig models: see Try nnc for what it offers, and for the lab's other tab, the nnc command line.
A first example
An example of one statement has a Run button above it, which types it at the prompt for Enter to run (Shift+click runs it at once). An example of several statements has a Run button beside each statement, which types that statement, and Run all above it, which runs them one after another, each showing its result. In the lab, lab connects to the lab device, in place of ssh({ … }), though an example calling ssh() runs too, against the lab device:
import { connect } from '@node-net/netconf';
// lab: the lab device, in place of ssh({ … })
const labClient = await connect({ transport: lab });
const IF = '/ietf-interfaces:interfaces/interface[name="eth0"]';
const data = await labClient.content.read(IF);
console.log(data);
await labClient.close();The lab also has a client already connected to the device, which the runnable examples on the other guide pages use.
Types
The lab type-checks each input before it runs, as the TypeScript compiler would in your own project. An input with type errors shows them and doesn't run, so a misspelt option is caught before anything reaches the device. Tab completes from the library's types too, including option names inside a call: try client.ops.getConfig({ then Tab. The compiler loads the first time you type. Each input is checked as a file of its own, so declare an interface, enum or namespace whose parts belong together in one input: a later one replaces it rather than adding to it. .types off runs inputs without checking them, and .help lists the lab's other commands.
Protocol observation
To see the NETCONF messages behind each call, listen for the session's 'message' event. It fires for every message sent or received, with its direction ('send' or 'receive') and its xml. The XML has been through the same redaction as trace logging, so passwords and keys are masked. It is a compact string, as it went to the device. Parse it with parseXml and log the element rather than the string: console.log() prints an element indented, one element per line, and the lab colours it too:
import { parseXml } from '@node-net/netconf';
const stopWatching = client.session.on('message', ({ direction, xml }) => {
const arrow = direction === 'send' ? '>>' : '<<';
console.log(`${arrow} ${new Date().toISOString()}`);
console.log(parseXml(xml));
});
await client.ops.lock('running');
await client.ops.unlock('running');
stopWatching();Each call now prints the <rpc> it sent and the <rpc-reply> it got back. on() returns a function that removes the listener. Until you call it, every call prints its messages, so you can leave it running while you try the examples on other pages, and run stopWatching() when you've seen enough.
Only a logged element is laid out and coloured. A string is printed as it is, so the indented XML from serializeXml(element, { indent: 2 }) comes out plain, as does an element joined into a string with + or a template, which is compact as well. In Node.js, console.log(element) indents it but doesn't colour it.
Setting the logger to 'trace' logs the same messages, but each one is compact, on a single line, which suits a log file better than a screen. The event lets you print them however you like. The hello exchange is over by the time connect() resolves, so a listener added afterwards never sees it. Logging and tracing has more, and in the lab's nnc tab, trace xml does the same thing as this listener.
Next
- Concepts: how the client, session and profile fit together
- Operations and Content: runnable examples of both layers