This chapter provides a tutorial on how to create a routine, or set of instructions, that results in a custom show command. It also provides a detailed description of all the classes and utility functions used to create custom show commands.
Before creating a show routine, the following is recommended:
To create a show routine, you perform the following high-level steps:
The following is an output example once all steps are completed. As you perform the steps in this section, you will be able to see how this example is built.
Schema nodes describe a data model. Similar to the output of the tree command or the content of a YANG file, they indicate what lists, containers, keys, fields, and leaf-lists can be created.
To build a SchemaNode, start with a FixedSchemaRoot()
then add your top-level list/container using FixedSchemaNode.add_child() as shown in the SchemaNode reference.
For example:
The code above generates a data model for the following YANG model:
Ensure that the filter auto-completes all fields by passing the schema node into the add_command call when you install the plug-in. This ensures that the filter operator (|) can auto-complete all fields. For example:
To retrieve the state, use build_path() to populate a path of the key you need to retrieve, and call get_data.
This returns a Data object pointing to the root of the data returned by the management server:
With the data from the management server and a data model, populate the Data object:
To format the output, assign Formatter instances to the different Data objects. The type of Formatter determines whether the output is formatted using key: value pairs, as a grid-based table, or using a custom format.
A list of all the built-in formatters is in the Formatters section.
The following example shows how to implement the callback method which can then be invoked to complete the routine:
Once you have completed Steps 1 - 5 (in sections 2.1.1 - 2.1.5), the show routine first shown in section 2.1 Create a show routine is now complete.
The following is an example of the complete show routine code:
The previous steps detail how to obtain data, and then separately print a report. With streaming, data is retrieved and begins printing immediately. This is useful for reports that have large amounts of data (for example, route tables) because printing begins immediately instead of waiting for the entire data collection to complete.
Perform the following to implement streaming:
The following is an example of the complete show routine code using streaming.