YAML – Now A Part of the Junos EZ Library!

Python Logo

Jeremy Schulman has been working hard developing the Junos EZ library for Python, and one of the changes I mentioned that he had proposed before was to use YAML to define data tables, to make it simpler for us non-programmers to customize the data we want to use.

He moved so fast, in fact, that by the time I had published “Getting Busy With the Junos EZ Library” where I mentioned YAML, Jeremy had already updated some of the modules to use YAML!

This means two things:

  1. The format of the table data files has changed
  2. You may need to install some more software so that the updated files will work!

Read on; I’ll keep it reasonably short!

Installing YAML in Ubuntu

I’m assuming that you have already completed all the necessary steps to install Python and the Junos EZ library on Ubuntu. If so, there’s one more simple command to run.

sudo pip-2.7 install pyyaml

You’ll likely see an error when this is installed:

 build/temp.linux-i686-2.7/check_libyaml.c:2:18: fatal error: yaml.h: No such file or directory
 compilation terminated.

 libyaml is not found or a compiler error: forcing --without-libyaml

But it’s ok; libyaml is not actually required and the installation script handles the error. All you really care about is the end result:

Successfully installed pyyaml

Before and After

What difference does YAML make? Well, less than you’d think, but let’s compare the format of the “old” Python file (ethport.py) to the new “YAML” file (ethport.yml).

EthPortTable

EthPortTable, to recap, tells Python what information needs to be queried from the Juniper device. The original ethport.py is relatively simple, but has to have correct syntax:

EthPortTable in Python (ethport.py)
EthPortTable = RSM.GetTable('get-interface-information',
 args = {'media': True, 'interface_name': '[fgx]e*' },
 args_key = 'interface_name',
 item = 'physical-interface',
 view = EthPortView
)

The YAML version is laid out in a hierarchical fashion using keys and values. The levels of indentation define the levels of the hierarchy – e.g. media and interface_name are elements within args. YAML can make laying data out really quite simple, and the key: value format is consistent and straightforward. This latest version also report on aggregated-ethernet (ae) interfaces:

EthPortTable in YAML (ethport.yml)
EthPortTable:
 rpc: get-interface-information
 args:
   media: True
   interface_name: '[afgx]e*'
 args_key: interface_name
 item: physical-interface
 view: EthPortView

EthPortView

EthPortView defines the data that’s extracted from EthPortTable and presented to the user. Here’s the old version in Python:

EthPortView in Python (ethport.py)
EthPortView = RSM.View(RSM.Fields()
 .str('oper', 'oper-status')
 .str('admin','admin-status')
 .str('link_mode','link-mode')
 .str('speed')
 .int('rx_bytes', 'input-bytes', group='mac_stats')
 .int('tx_bytes', 'output-bytes', group='mac_stats')
 .end,
 groups = {
  'mac_stats':'ethernet-mac-statistics'
 }
)

And now here’s the much-simplified version in YAML. Note that in additional to the format change, some additional fields are being grabbed, and the data is reorganized slightly:

EthPortView in YAML (ethport.yml)
EthPortView:
 groups:
   mac_stats: ethernet-mac-statistics
   flags: if-device-flags
 fields:
   oper: oper-status
   admin: admin-status
   mtu: { mtu : int }
   link_mode: link-mode
   macaddr: current-physical-address
 fields_mac_stats:
   rx_bytes: input-bytes
   rx_packets: input-packets
   tx_bytes: output-bytes
   tx_packets: output-packets
 fields_flags:
   running: { ifdf-running: flag }
   present: { ifdf-present: flag }

I do think that the YAML version is significantly simpler to read and create, so I’m looking forward to playing with this.

For reference, the ethport.py module is still there, and is now a static placeholder to pull in and interpret the ethport.yml YAML file; you don’t need to mess with ethport.py any more.

Keep on coding!

 

See also: Installing the Junos EZ Library – Easy SDN Part 1

1 Comment on YAML – Now A Part of the Junos EZ Library!

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.