Blog Viewer

Scripting How-To: Use views for automation tasks

By Erdem posted 06-02-2015 21:56


Use Views for Automation Tasks


A View defines the mapping between the Junos OS or XML and the field names by which you want to retrieve the data. The purpose of Junos PyEZ is to abstract, or insulate, the user of the Table/View from this information. The View widgets are defined using YAML in a similar process as the Tables.


The purpose of a View is to allow the user access of specific fields in a native Python way, which is to say as variables with properties. The Junos PyEZ micro-framework handles both the extraction of the Junos OS or XML into native Python, as well as any type conversion or data normalization.


A View Is a Selected Table Item

 You obtain a View by selecting a specific Table item. Using the RouteTable as an example:


1 >>> from jnpr.junos.op.routes import RouteTable
2 >>> routes = RouteTable(dev)
3 >>> routes.get()
4 RouteTable:jnpr-dc-fw: 148 items
5 >>> this = routes['']
6 >>> this
7 RouteTableView:


The view to a specific route is made on line 5, in the assignment of this.


View Item Name

All View items have a name property, which provides the unique name for this item in the associated Table.


1 >>>
2 ''


You can also obtain a reference to the associated Table using the T property:


1 >>> this.T
2 RouteTable:jnpr-dc-fw: 148 items


View Item Keys (Properties)

You can obtain a list of the available field names, or "keys" or "properties":


1 >>> pp( this.keys() )
2 ['age', 'via', 'protocol']


So we can see that we can access the three properties by names: age, via, and protocol.


Accessing a Specific View Item

You access a view item as a variable property:


1 >>> print this.via
2 ge-0/0/0.0
3 >>> print this.protocol
4 Direct
5 >>> print this.age
6 172775


View Items

If you need to access the complete list of view items (keys or values) you can do so using the items() method: 
1 >>> this.items()
2 [('age', 173447), ('via', 'ge-0/0/0.0'), ('protocol', 'Direct')]


Note here how age is a numeric value; the Junos PyEZ automatically converted the value to an integer based on the View definition. The YAML for the RouteTable is illustrated in the next section.


Again, the purpose of the Junos PyEZ is to abstract this YAML/Junos OS/XML information from the user of the Table or View. The person that defines the Table or View widgets (in YAML) might or might not be the same person using the widgets for their automation tasks.


Applying a Different View

 You can apply a different View to a table item. There are two ways to do this.The first way is to change the default table view. This would apply to any future table item selections. The default View is defined in the YAML file. Here is the YAML for the RouteTable for illustration purposes:


01 RouteTable:
02   rpc: get-route-information
03   args_key: destination
04   item: route-table/rt
05   key: rt-destination
06   view: RouteTableView
07 RouteTableView:
08   groups:
09     entry: rt-entry
10   fields_entry:
11     # fields taken from the group 'entry'
12     protocol: protocol-name
13     via: nh/via | nh/nh-local-interface
14     age: { age/@seconds : int }

If you create another View, perhaps called "MyRouteTableView", and you want to apply that to an existing table, you would assign it to the table view property:


1 >>> routes.view = MyRouteTableView


You can also read the view property should you want to store or reference this View later.


The second approach to changing a View is to apply a new View to an existing View instance. You do this using the asview() method:


1 new_view = this.asview(MyRouteTableView)

Creating Your Own Views

 It is very easy to define your own Views using a YAML file. This process is described here.