Devnet
pyATS
8 Profile the Network

Profile the Network

Taking snapshots of the state of the network before and after testing and comparing the snapshots is a simple and common method of determining and validating test results. Traditionally, network snapshots can be done by taking the output of the necessary show commands before network testing and taking another round of snapshots at some other point during or after testing. pyATS provides the learn feature in the pyATS Library Ops module, simplifying the retrieval of network snapshots.

This topic explains how the learn feature of the pyATS Library Ops module can be used to perform stateful network validation of device elements such as services, protocols, and hardware. The term feature typically refers to a network protocol that the pyATS Library represents as a Python object. The learn function of the pyATS Library Ops module learns and profiles an entire feature’s configuration and operational status. With just a few commands, you can profile your system before and after a configuration change and get a detailed summary of exactly what changed.

Parser versus Learn in pyATS

What is the difference between the parse and learn functions? The parsed output of the parser function for different devices may data structure schema may differ between platforms and operating systems. The learn function, by contrast, results in a consistent set of keys, which means that you can write one script that works on different devices. For example, the schema of the output for learning the OSPF feature should be the same across platforms.

The learn output is stored with the same key-value pair structure across devices. The stored output makes it possible for you to take a snapshot of the network state at different points in time, and then to compare network states. The learn function results in a consistent set of keys that enable you to write one script that works on various devices regardless of their operating system command syntax.

You can learn the operational state by executing a series of commands and parsing the output into a common data structure that will work across various operating systems.

"Learn" Function Examples

There are a few ways that the Learn function can be utilized and executed. The pyATS (Genie) SDK library can run the learn function and then redirect the output to a desired file and location. Another way is using the .learn() method in test scripts to retrieve network state information. Here are a few examples of how the Learn function is utilized:

Single Feature, Single Device

To learn one feature on a single device, you can use the device hostname or the device alias as defined in the testbed YAML file. This example shows the use of the learning function with the pyATS (Genie) SDK library, which is one way of using this function. You could do the same thing in a test script as well. When the command is from the console, it will create 2 files. One is the console output from the device; the second is the structured data.

(py3venv) [developer@devbox ~]$ pyats learn ospf \
> --testbed-file sandbox-testbed-file.yaml \
> --devices dist-rtr01
DeviceFeature LearntOutput Files Generated
dist-rtr01OSPFconnection_dist-rtr01.txt, ospf_iosxe_dist-rtr01_ops.txt, ospf_iosxe_dist-rtr01_console.txt

All Features, Single Device

In the second example, the system learns all features on the device and also stores the output as a Python dictionary in the variable output. The "all" value is used with Learn to have pyATS learn all features on a single device. Be careful with using the "all" value as it may generate a large amount of data which may be unnecessary.

(pyats) $ pyats shell --testbed-file sandbox-testbed-file.yaml
   >>> dev = testbed.devices['dist-rtr01']
   >>> dev.connect()
   >>> output = dev.learn('all')
DeviceFeatures Learnt
dist-rtr01All Features

Multiple Features, Multiple Devices

In the third example, the system learns the BGP and OSPF features on all devices in the testbed. The Python for loop is used query all devices. The system then stores the output as a Python dictionary in the variable learnt.

(pyats) $ pyats shell --testbed-file mock.yaml
   >>> learnt = {}
   >>> for name, dev in testbed.devices.items():
   ...     dev.connect()
   ...     learnt[name] = {}
   ...     learnt[name]['bgp'] = dev.learn('bgp')
   ...     learnt[name]['ospf'] = dev.learn('ospf')
DeviceBGP LearntOSPF Learnt
nx-osv-1BGPOSPF
csr1000v-1BGPOSPF

Note: The capabilities of supported "learn" features available can be found at the following URL: Genie Feature Browser (opens in a new tab)