Skip to content
This repository has been archived by the owner on Oct 11, 2020. It is now read-only.

11. OpenConfig demo with PyEZ python library

Khelil Sator edited this page May 31, 2017 · 1 revision

openconfig-demo repo content

bgp.j2 - This is a jinja2 template to render an Openconfig BGP configuration file.

interfaces.j2 - This is a jinja2 template to render an interfaces configuration file.

host_vars directory - it has the YAML files bgp.yml and topology.yml that have the variables definition for each device (BGP, interfaces ...)

pyez.oc.bgp.py - This python script uses the templates bgp.j2 and interfaces.j2 to build the configuration file for each the device. For each device, it uses the YAML files bgp.yml and topology.yml under the directory host_vars in order to get the variables definition for BGP and interfaces.
It then push and commit the configuration on the devices It then print the BGP states.
Usage:

python pyez.oc.bgp.py my_list_of_devices.yml  

my_list_of_devices.yml - This is a yaml file with the list of devices. it is uses as an argument of the python script. This is our inventory.

PyEZ Tables and Views for bgp - The PyEZ subpackage jnpr.junos.op doesn’t provide table and view for bgp. It is very easy to add other PyEZ Tables and Views to your package jnpr.junos.op:

  • bgp.yml is a yaml file to define PyEZ BGP tables and views. You need to add it to the PyEZ subpackage jnpr.junos.op.
  • You also need to create a python module bgp.py your the package jnpr.junos.op. This module is a "pythonifier": It loads the bgp.yml file from the package jnpr.junos.op and creates the classes 'BGPNeighborTable' and 'BGPneighborView'. All the “pythonifier” modules in the PyEZ subpackage jnpr.junos.op have the same code (only the name change). So you just need to copy and past an existing one and renamed it bgp.py
    Then you can use the class "BGPNeighborTable": its method get send the junos rpc get-bgp-neighbor-information to get “show bgp neighbor | display xml”, then parses the XML response (so you don’t need to worry about this) and build a Python data structure (a list of dictionaries) that you can easily use by Python: For each bgp-peer in the XML response, PyEZ builds a BGPneighborView with the peer-address, the peer-state, and other details ...
  • Note: In addition to this (PyEZ Tables and Views for bgp), the script python also audit the BGP states pulling state data using NetConf get as per standard (Junos render the state data in Openconfig format)
$ more /usr/local/lib/python2.7/dist-packages/jnpr/junos/op/bgp.yml
BGPNeighborTable:
  rpc: get-bgp-neighbor-information
  item: bgp-peer
  view: BGPneighborView
  key: peer-address

BGPneighborView:
  fields:
    neighbor: peer-address
    state: peer-state
    type: peer-type
    flap_count: flap-count

$ more /usr/local/lib/python2.7/dist-packages/jnpr/junos/op/bgp.py
"""
Pythonifier for BGP Table/View
"""
from jnpr.junos.factory import loadyaml
from os.path import splitext
_YAML_ = splitext(__file__)[0] + '.yml'
globals().update(loadyaml(_YAML_))

Demo

$ python pyez.oc.bgp.py my_list_of_devices.yml

Building the configuration file for each device from a jinja2 template:
fabric-01.oc.bgp.conf
fabric-02.oc.bgp.conf
Done for all the devices!

Pushing and committing the configuration files to the devices ...
Configuration commited successfully on fabric-01
Configuration commited successfully on fabric-02
Done for all the devices!

Waiting 20 seconds before starting to audit the BGP sessions state ...

Auditing the BGP states using PyEZ Tables and Views:
Status of BGP sessions for device fabric-01:
192.168.1.2+54253 is Established
Status of BGP sessions for device fabric-02:
192.168.1.1+179 is Established
Done for all the devices!

Auditing the BGP states pulling state data using NetConf get as per standard (Junos render the state data in Openconfig format):
Status of BGP sessions for device fabric-01:
Neighbor 192.168.1.2 is ESTABLISHED
Status of BGP sessions for device fabric-02:
Neighbor 192.168.1.1 is ESTABLISHED
Done for all the devices!

$  ls -l *.conf
-rw-rw-r-- 1 administrator administrator 793 Nov 14 18:48 fabric-01.oc.bgp.conf
-rw-rw-r-- 1 administrator administrator 793 Nov 14 18:48 fabric-02.oc.bgp.conf
$ more  fabric-01.oc.bgp.conf
interfaces {

    xe-0/0/0 {
        unit 0 {
            family inet {
                address 192.168.1.1/24;
            }
       }
    }

}
openconfig-bgp:bgp {
    neighbors {

        neighbor 192.168.1.2 {
            config {
                peer-as 110;
                peer-group OC ;
            }
        }

    }
    peer-groups {

        peer-group OC {
            config {
                local-as 104;
                peer-type EXTERNAL;
            }
            apply-policy {
                config {
                    import-policy bgp-in;
                    export-policy bgp-out;
                }
            }
        }

    }
}
policy-options {

    policy-statement bgp-in {
        then accept;
    }
    policy-statement bgp-out {
        then accept;
    }

}
$ more  fabric-02.oc.bgp.conf
interfaces {

    xe-0/0/0 {
        unit 0 {
            family inet {
                address 192.168.1.2/24;
            }
       }
    }

}
openconfig-bgp:bgp {
    neighbors {

        neighbor 192.168.1.1 {
            config {
                peer-as 104;
                peer-group OC ;
            }
        }

    }
    peer-groups {

        peer-group OC {
            config {
                local-as 110;
                peer-type EXTERNAL;
            }
            apply-policy {
                config {
                    import-policy bgp-in;
                    export-policy bgp-out;
                }
            }
        }

    }
}
policy-options {

    policy-statement bgp-in {
        then accept;
    }
    policy-statement bgp-out {
        then accept;
    }

}