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

10. PyEZ python library and OpenConfig integration

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

PyEZ

source code and installation instruction: https://github.com/Juniper/py-junos-eznc

PyEZ enhancements to better support openconfig and yang

With PyEZ 2.1:

  • we enhanced the get_config method support
  • we added the get method support

Demo

check your pyez version

for the following demo you need to use PyEZ version > 2.1

$ pip list | grep junos-eznc
junos-eznc (2.1.1)
import what you need
$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> from jnpr.junos import Device
>>> from pprint import pprint
>>> from lxml import etree
>>> from json import dumps
open a connection to your juniper openconfig device
>>> dev=Device(host="172.30.52.85",user="lab",passwd="m0naco")
>>> dev.open()
Device(172.30.52.85)
>>> dev
Device(172.30.52.85)
>>> dev.connected
True
>>> 
how to use PyEZ to get the junos configuration

The below examples use the PyEZ method get_config (get-config netconf rpc).

display the junos configuration only (i.e the openconfig configuration is not displayed)
data = dev.rpc.get_config()
print (etree.tostring(data))
getting part of the configuration

you can use filter xml to display only a part of the configuration.
Here's an example to display only the junos configuration for bgp (the bgp openconfig configuration is not displayed)

data = dev.rpc.get_config(filter_xml='protocols/bgp')
print (etree.tostring(data))

filter xml can be provided in diff manners. These examples provide the same output.

>>> data = dev.rpc.get_config(filter_xml=etree.XML('<system><services/></system>'))
>>> print etree.tostring(data)
<configuration changed-seconds="1494491390" changed-localtime="2017-05-11 08:29:50 UTC">
    <system>
        <services>
            <ftp>
            </ftp>
            <ssh>
            </ssh>
            <telnet>
            </telnet>
            <netconf>
                <ssh>
                </ssh>
            </netconf>
        </services>
    </system>
</configuration>

>>> 
>>> data = dev.rpc.get_config(filter_xml='<system><services/></system>')
>>> print etree.tostring(data)
<configuration changed-seconds="1494491467" changed-localtime="2017-05-11 08:31:07 UTC">
    <system>
        <services>
            <ftp>
            </ftp>
            <ssh>
            </ssh>
            <telnet>
            </telnet>
            <netconf>
                <ssh>
                </ssh>
            </netconf>
        </services>
    </system>
</configuration>

>>> 
>>> data = dev.rpc.get_config(filter_xml='system/services')
>>> print etree.tostring(data)
<configuration changed-seconds="1494491467" changed-localtime="2017-05-11 08:31:07 UTC">
    <system>
        <services>
            <ftp>
            </ftp>
            <ssh>
            </ssh>
            <telnet>
            </telnet>
            <netconf>
                <ssh>
                </ssh>
            </netconf>
        </services>
    </system>
</configuration>

>>> 
display the junos configuration in json
>>> data = dev.rpc.get_config(filter_xml='<system><services/></system>', options={'format': 'json'})
>>> print data
{u'configuration': {u'@': {u'junos:changed-localtime': u'2017-05-11 08:31:07 UTC', u'junos:changed-seconds': u'1494491467', u'xmlns': u'http://xml.juniper.net/xnm/1.1/xnm'}, u'system': {u'services': {u'netconf': {u'ssh': [None]}, u'ftp': [None], u'ssh': [None], u'telnet': [None]}}}}
>>> pprint(data)
{u'configuration': {u'@': {u'junos:changed-localtime': u'2017-05-11 08:31:07 UTC',
                           u'junos:changed-seconds': u'1494491467',
                           u'xmlns': u'http://xml.juniper.net/xnm/1.1/xnm'},
                    u'system': {u'services': {u'ftp': [None],
                                              u'netconf': {u'ssh': [None]},
                                              u'ssh': [None],
                                              u'telnet': [None]}}}}
>>> print dumps(data, indent=4)
{
    "configuration": {
        "@": {
            "junos:changed-localtime": "2017-05-11 08:31:07 UTC", 
            "junos:changed-seconds": "1494491467", 
            "xmlns": "http://xml.juniper.net/xnm/1.1/xnm"
        }, 
        "system": {
            "services": {
                "netconf": {
                    "ssh": [
                        null
                    ]
                }, 
                "ftp": [
                    null
                ], 
                "ssh": [
                    null
                ], 
                "telnet": [
                    null
                ]
            }
        }
    }
}
>>> 
PyEZ and Openconfig integration

The below examples use the PyEZ method get_config (get-config netconf rpc).

display both the junos and the openconfig configuration
data = dev.rpc.get_config(model=True)
print (etree.tostring(data))
display the bgp openconfig configuration
>>> data = dev.rpc.get_config(filter_xml='bgp', model='openconfig')
>>> print (etree.tostring(data))
<bgp>
    <neighbors>
        <neighbor>
            <neighbor-address>192.168.178.11</neighbor-address>
            <config>
                <peer-as>110</peer-as>
                <peer-group>OC</peer-group>
            </config>
        </neighbor>
    </neighbors>
    <peer-groups>
        <peer-group>
            <peer-group-name>OC</peer-group-name>
            <config>
                <local-as>104</local-as>
                <peer-type>EXTERNAL</peer-type>
            </config>
            <apply-policy>
                <config>
                    <import-policy>bgp-in</import-policy>
                    <export-policy>bgp-out</export-policy>
                </config>
            </apply-policy>
        </peer-group>
    </peer-groups>
</bgp>

>>> 
display the bgp openconfig configuration and also the namespace in the response so that same xml can be reapplied to device
>>> data = dev.rpc.get_config(filter_xml='bgp', model='openconfig', remove_ns=False)
>>> print etree.tostring(data)
<bgp xmlns="http://openconfig.net/yang/bgp">
    <neighbors>
        <neighbor>
            <neighbor-address>192.168.178.11</neighbor-address>
            <config>
                <peer-as>110</peer-as>
                <peer-group>OC</peer-group>
            </config>
        </neighbor>
    </neighbors>
    <peer-groups>
        <peer-group>
            <peer-group-name>OC</peer-group-name>
            <config>
                <local-as>104</local-as>
                <peer-type>EXTERNAL</peer-type>
            </config>
            <apply-policy>
                <config>
                    <import-policy>bgp-in</import-policy>
                    <export-policy>bgp-out</export-policy>
                </config>
            </apply-policy>
        </peer-group>
    </peer-groups>
</bgp>

>>> 

display the bgp neighbors openconfig configuration (but not the whole bgp openconfig configuration)
>>> data = dev.rpc.get_config(filter_xml='<bgp><neighbors></neighbors></bgp>', model='openconfig')
>>> print etree.tostring(data)
<bgp>
    <neighbors>
        <neighbor>
            <neighbor-address>192.168.178.11</neighbor-address>
            <config>
                <peer-as>110</peer-as>
                <peer-group>OC</peer-group>
            </config>
        </neighbor>
    </neighbors>
</bgp>

>>> 
display the bgp neighbors openconfig configuration (but not the whole bgp openconfig configuration)
>>> data = dev.rpc.get_config(filter_xml='bgp/neighbors', model='openconfig')
>>> print (etree.tostring(data))
<bgp>
    <neighbors>
        <neighbor>
            <neighbor-address>192.168.178.11</neighbor-address>
            <config>
                <peer-as>110</peer-as>
                <peer-group>OC</peer-group>
            </config>
        </neighbor>
    </neighbors>
</bgp>

>>> 

display the bgp openconfig configuration in json
>>> data = dev.rpc.get_config(filter_xml='<bgp/>', model='openconfig', options={'format': 'json'})
>>> print data 
{u'openconfig-bgp:bgp': {u'neighbors': {u'neighbor': [{u'neighbor-address': u'192.168.178.11', u'config': {u'peer-as': 110, u'peer-group': u'OC'}}]}, u'peer-groups': {u'peer-group': [{u'apply-policy': {u'config': {u'import-policy': [u'bgp-in'], u'export-policy': [u'bgp-out']}}, u'config': {u'peer-type': u'EXTERNAL', u'local-as': 104}, u'peer-group-name': u'OC'}]}}}
>>> pprint (data) 
{u'openconfig-bgp:bgp': {u'neighbors': {u'neighbor': [{u'config': {u'peer-as': 110,
                                                                   u'peer-group': u'OC'},
                                                       u'neighbor-address': u'192.168.178.11'}]},
                         u'peer-groups': {u'peer-group': [{u'apply-policy': {u'config': {u'export-policy': [u'bgp-out'],
                                                                                         u'import-policy': [u'bgp-in']}},
                                                           u'config': {u'local-as': 104,
                                                                       u'peer-type': u'EXTERNAL'},
                                                           u'peer-group-name': u'OC'}]}}}
>>> print dumps(data, indent=4)
{
    "openconfig-bgp:bgp": {
        "neighbors": {
            "neighbor": [
                {
                    "neighbor-address": "192.168.178.11", 
                    "config": {
                        "peer-as": 110, 
                        "peer-group": "OC"
                    }
                }
            ]
        }, 
        "peer-groups": {
            "peer-group": [
                {
                    "apply-policy": {
                        "config": {
                            "import-policy": [
                                "bgp-in"
                            ], 
                            "export-policy": [
                                "bgp-out"
                            ]
                        }
                    }, 
                    "config": {
                        "peer-type": "EXTERNAL", 
                        "local-as": 104
                    }, 
                    "peer-group-name": "OC"
                }
            ]
        }
    }
}
>>> 

equivalent of this netconf <rpc><get-config><source><running/></source><filter type="subtree"><bgp xmlns="http://openconfig.net/yang/bgp"><neighbors/></bgp></filter></get-config></rpc>

>>> data = dev.rpc.get_config(filter_xml='<bgp><neighbors></neighbors></bgp>', model='openconfig', options={'database': 'committed'})
>>> print etree.tostring(data)
<bgp>
    <neighbors>
        <neighbor>
            <neighbor-address>192.168.178.11</neighbor-address>
            <config>
                <peer-as>110</peer-as>
                <peer-group>OC</peer-group>
            </config>
        </neighbor>
    </neighbors>
</bgp>

>>> 

equivalent of this netconf <rpc><get-config><source><candidate/></source><filter type="subtree"><bgp xmlns="http://openconfig.net/yang/bgp"><neighbors/></bgp></filter></get-config></rpc>

>>> data = dev.rpc.get_config(filter_xml='<bgp><neighbors></neighbors></bgp>', model='openconfig', options={'database': 'candidate'})
>>> print etree.tostring(data)
<bgp>
    <neighbors>
        <neighbor>
            <neighbor-address>192.168.178.11</neighbor-address>
            <config>
                <peer-as>110</peer-as>
                <peer-group>OC</peer-group>
            </config>
        </neighbor>
    </neighbors>
</bgp>

>>> 

equivalent of this netconf <rpc><get-config><source><candidate/></source><filter type="subtree"><bgp xmlns="http://openconfig.net/yang/bgp"/></filter></get-config></rpc>

>>> data = dev.rpc.get_config(filter_xml='<bgp/>', model='openconfig', remove_ns=False)
>>> print etree.tostring(data)
<bgp xmlns="http://openconfig.net/yang/bgp">
    <neighbors>
        <neighbor>
            <neighbor-address>192.168.178.11</neighbor-address>
            <config>
                <peer-as>110</peer-as>
                <peer-group>OC</peer-group>
            </config>
        </neighbor>
    </neighbors>
    <peer-groups>
        <peer-group>
            <peer-group-name>OC</peer-group-name>
            <config>
                <local-as>104</local-as>
                <peer-type>EXTERNAL</peer-type>
            </config>
            <apply-policy>
                <config>
                    <import-policy>bgp-in</import-policy>
                    <export-policy>bgp-out</export-policy>
                </config>
            </apply-policy>
        </peer-group>
    </peer-groups>
</bgp>

>>> 
display in json the openconfig bgp running configuration, with the namespace
>>> data = dev.rpc.get_config(filter_xml='<bgp/>', model='openconfig', remove_ns=False, options={'format': 'json', 'database': 'committed'})
>>> print data
{u'openconfig-bgp:bgp': {u'neighbors': {u'neighbor': [{u'neighbor-address': u'192.168.178.11', u'config': {u'peer-as': 110, u'peer-group': u'OC'}}]}, u'peer-groups': {u'peer-group': [{u'apply-policy': {u'config': {u'import-policy': [u'bgp-in'], u'export-policy': [u'bgp-out']}}, u'config': {u'peer-type': u'EXTERNAL', u'local-as': 104}, u'peer-group-name': u'OC'}]}}}
>>> pprint(data)
{u'openconfig-bgp:bgp': {u'neighbors': {u'neighbor': [{u'config': {u'peer-as': 110,
                                                                   u'peer-group': u'OC'},
                                                       u'neighbor-address': u'192.168.178.11'}]},
                         u'peer-groups': {u'peer-group': [{u'apply-policy': {u'config': {u'export-policy': [u'bgp-out'],
                                                                                         u'import-policy': [u'bgp-in']}},
                                                           u'config': {u'local-as': 104,
                                                                       u'peer-type': u'EXTERNAL'},
                                                           u'peer-group-name': u'OC'}]}}}
>>> print(dumps(data, indent=4))
{
    "openconfig-bgp:bgp": {
        "neighbors": {
            "neighbor": [
                {
                    "neighbor-address": "192.168.178.11", 
                    "config": {
                        "peer-as": 110, 
                        "peer-group": "OC"
                    }
                }
            ]
        }, 
        "peer-groups": {
            "peer-group": [
                {
                    "apply-policy": {
                        "config": {
                            "import-policy": [
                                "bgp-in"
                            ], 
                            "export-policy": [
                                "bgp-out"
                            ]
                        }
                    }, 
                    "config": {
                        "peer-type": "EXTERNAL", 
                        "local-as": 104
                    }, 
                    "peer-group-name": "OC"
                }
            ]
        }
    }
}
>>> 
how to use PyEZ to get the junos operationnal states

The below examples use the PyEZ method get (get netconf rpc).

equivalent of <rpc><get><filter type="xpath" select="bgp"/></get></rpc>
>>> data = dev.rpc.get('bgp')
>>> print (etree.tostring(data))
<data>
<bgp>
  <global>
    <state>
      <as>0</as>
      <router-id>172.30.52.85</router-id>
      <total-paths>3</total-paths>
      <total-prefixes>3</total-prefixes>
    </state>
    <route-selection-options>
      <state>
        <always-compare-med>false</always-compare-med>
        <ignore-as-path-length>false</ignore-as-path-length>
        <external-compare-router-id>false</external-compare-router-id>
        <advertise-inactive-routes>false</advertise-inactive-routes>
      </state>
    </route-selection-options>
    <confederation>
      <state>
        <enabled>false</enabled>
        <identifier>0</identifier>
      </state>
    </confederation>
    <graceful-restart>
      <state>
        <enabled>false</enabled>
        <restart-time>0</restart-time>
        <stale-routes-time>0</stale-routes-time>
        <helper-only>true</helper-only>
      </state>
    </graceful-restart>
  </global>
  <neighbors>
    <neighbor>
      <neighbor-address>192.168.178.11</neighbor-address>
      <state>
        <peer-as>110</peer-as>
        <local-as>104</local-as>
        <peer-type>EXTERNAL</peer-type>
        <auth-password>(null)</auth-password>
        <remove-private-as>0</remove-private-as>
        <route-flap-damping>false</route-flap-damping>
        <description>(null)</description>
        <session-state>ESTABLISHED</session-state>
        <supported-capabilities>
          MPBGP
        </supported-capabilities>
        <supported-capabilities>
          ROUTE_REFRESH
        </supported-capabilities>
        <supported-capabilities>
          GRACEFUL_RESTART
        </supported-capabilities>
        <peer-group>OC</peer-group>
        <session-status>RUNNING</session-status>
        <session-admin-status>START</session-admin-status>
        <session-established-transitions>1</session-established-transitions>
        <interface-error>false</interface-error>
        <neighbor-address>192.168.178.11</neighbor-address>
        <enabled>true</enabled>
        <messages>
          <sent>
            <UPDATE>1</UPDATE>
            <NOTIFICATION>0</NOTIFICATION>
          </sent>
          <received>
            <UPDATE>2</UPDATE>
            <NOTIFICATION>0</NOTIFICATION>
          </received>
        </messages>
        <queues>
          <input>0</input>
          <output>0</output>
        </queues>
      </state>
      <timers>
        <state>
          <connect-retry>0</connect-retry>
          <hold-time>90</hold-time>
          <keepalive-interval>30</keepalive-interval>
          <minimum-advertisement-interval>0</minimum-advertisement-interval>
          <uptime>195099900</uptime>
          <negotiated-hold-time>90</negotiated-hold-time>
        </state>
      </timers>
      <transport>
        <state>
          <tcp-mss>0</tcp-mss>
          <mtu-discovery>false</mtu-discovery>
          <passive-mode>false</passive-mode>
          <local-address>192.168.178.10</local-address>
          <local-port>45824</local-port>
          <remote-address>192.168.178.11</remote-address>
          <remote-port>179</remote-port>
        </state>
      </transport>
      <error-handling>
        <state>
          <treat-as-withdraw>false</treat-as-withdraw>
          <erroneous-update-messages>0</erroneous-update-messages>
        </state>
      </error-handling>
      <logging-options>
        <state>
          <log-neighbor-state-changes>false</log-neighbor-state-changes>
        </state>
      </logging-options>
      <ebgp-multihop>
        <state>
          <enabled>false</enabled>
          <multihop-ttl>0</multihop-ttl>
        </state>
      </ebgp-multihop>
      <route-reflector>
        <state>
          <route-reflector-cluster-id>zero-len</route-reflector-cluster-id>
          <route-reflector-client>false</route-reflector-client>
        </state>
      </route-reflector>
      <as-path-options>
        <state>
          <allow-own-as>1</allow-own-as>
          <replace-peer-as>false</replace-peer-as>
        </state>
      </as-path-options>
      <use-multiple-paths>
        <state>
          <enabled>false</enabled>
        </state>
        <ebgp>
          <state>
            <allow-multiple-as>false</allow-multiple-as>
            <maximum-paths>16</maximum-paths>
          </state>
        </ebgp>
        <ibgp>
          <state>
            <maximum-paths>16</maximum-paths>
          </state>
        </ibgp>
      </use-multiple-paths>
      <afi-safis>
        <afi-safi>
          <afi-safi-name>IPV4_UNICAST</afi-safi-name>
          <graceful-restart>
            <state>
              <enabled>true</enabled>
              <received>true</received>
              <advertised>true</advertised>
            </state>
          </graceful-restart>
          <state>
            <afi-safi-name>IPV4_UNICAST</afi-safi-name>
            <enabled>false</enabled>
            <active>true</active>
            <prefixes>
              <received>3</received>
              <sent>3</sent>
              <installed>0</installed>
            </prefixes>
            <prefix-limit-exceeded>false</prefix-limit-exceeded>
            <total-paths>3</total-paths>
            <total-prefixes>3</total-prefixes>
            <prefix-limit>
              <state>
                <max-prefixes>0</max-prefixes>
                <shutdown-threshold-pct>0</shutdown-threshold-pct>
                <restart-timer>0</restart-timer>
              </state>
            </prefix-limit>
          </state>
          <add-paths>
            <receive>false</receive>
            <send-max>0</send-max>
          </add-paths>
        </afi-safi>
      </afi-safis>
      <graceful-restart>
        <state>
          <enabled>false</enabled>
          <restart-time>120</restart-time>
          <stale-routes-time>300</stale-routes-time>
          <helper-only>true</helper-only>
          <peer-restart-time>120</peer-restart-time>
          <peer-restarting>false</peer-restarting>
          <local-restarting>false</local-restarting>
          <mode>HELPER_ONLY</mode>
        </state>
      </graceful-restart>
      <apply-policy>
        <state>
          <import-policy>
            bgp-in
          </import-policy>
          <export-policy>
            bgp-out
          </export-policy>
        </state>
      </apply-policy>
    </neighbor>
  </neighbors>
  <peer-groups>
    <peer-group>
      <peer-group-name>OC</peer-group-name>
      <state>
        <peer-as>110</peer-as>
        <local-as>104</local-as>
        <peer-type>EXTERNAL</peer-type>
        <auth-password>(null)</auth-password>
        <remove-private-as>0</remove-private-as>
        <route-flap-damping>false</route-flap-damping>
        <description>(null)</description>
        <peer-group-name>OC</peer-group-name>
        <total-paths>3</total-paths>
        <total-prefixes>3</total-prefixes>
      </state>
      <timers>
        <state>
          <connect-retry>0</connect-retry>
          <hold-time>90</hold-time>
          <keepalive-interval>30</keepalive-interval>
          <minimum-advertisement-interval>0</minimum-advertisement-interval>
        </state>
      </timers>
      <transport>
        <state>
          <tcp-mss>0</tcp-mss>
          <mtu-discovery>false</mtu-discovery>
          <passive-mode>false</passive-mode>
          <local-address>192.168.178.10</local-address>
        </state>
      </transport>
      <error-handling>
        <state>
          <treat-as-withdraw>false</treat-as-withdraw>
        </state>
      </error-handling>
      <logging-options>
        <state>
          <log-neighbor-state-changes>false</log-neighbor-state-changes>
        </state>
      </logging-options>
      <ebgp-multihop>
        <state>
          <enabled>false</enabled>
          <multihop-ttl>0</multihop-ttl>
        </state>
      </ebgp-multihop>
      <route-reflector>
        <state>
          <route-reflector-cluster-id>zero-len</route-reflector-cluster-id>
          <route-reflector-client>false</route-reflector-client>
        </state>
      </route-reflector>
      <as-path-options>
        <state>
          <allow-own-as>1</allow-own-as>
          <replace-peer-as>false</replace-peer-as>
        </state>
      </as-path-options>
      <use-multiple-paths>
        <state>
          <enabled>false</enabled>
        </state>
        <ebgp>
          <state>
            <allow-multiple-as>false</allow-multiple-as>
            <maximum-paths>16</maximum-paths>
          </state>
        </ebgp>
        <ibgp>
          <state>
            <maximum-paths>16</maximum-paths>
          </state>
        </ibgp>
      </use-multiple-paths>
      <afi-safis>
        <afi-safi>
          <afi-safi-name>IPV4_UNICAST</afi-safi-name>
          <graceful-restart>
            <state>
              <enabled>true</enabled>
            </state>
          </graceful-restart>
          <state>
            <afi-safi-name>IPV4_UNICAST</afi-safi-name>
            <enabled>false</enabled>
            <prefix-limit>
              <state>
                <max-prefixes>0</max-prefixes>
                <shutdown-threshold-pct>0</shutdown-threshold-pct>
                <restart-timer>0</restart-timer>
              </state>
            </prefix-limit>
          </state>
          <add-paths>
            <receive>false</receive>
            <send-max>0</send-max>
          </add-paths>
        </afi-safi>
      </afi-safis>
      <graceful-restart>
        <state>
          <enabled>false</enabled>
          <restart-time>120</restart-time>
          <stale-routes-time>300</stale-routes-time>
          <helper-only>true</helper-only>
        </state>
      </graceful-restart>
      <apply-policy>
        <state>
          <import-policy>
            bgp-in
          </import-policy>
          <export-policy>
            bgp-out
          </export-policy>
        </state>
      </apply-policy>
    </peer-group>
  </peer-groups>
</bgp>
<database-status-information>
</database-status-information>
</data>

>>> 

display bgp states for bgp neighbors configured using junos data model as well as bgp neighbors configured using openconfig data model
>>> data = dev.rpc.get("/bgp/neighbors")
>>> print etree.tostring(data)
<data>
<bgp>
  <neighbors>
    <neighbor>
      <neighbor-address>192.168.178.11</neighbor-address>
      <state>
        <peer-as>110</peer-as>
        <local-as>104</local-as>
        <peer-type>EXTERNAL</peer-type>
        <auth-password>(null)</auth-password>
        <remove-private-as>0</remove-private-as>
        <route-flap-damping>false</route-flap-damping>
        <description>(null)</description>
        <session-state>ESTABLISHED</session-state>
        <supported-capabilities>
          MPBGP
        </supported-capabilities>
        <supported-capabilities>
          ROUTE_REFRESH
        </supported-capabilities>
        <supported-capabilities>
          GRACEFUL_RESTART
        </supported-capabilities>
        <peer-group>OC</peer-group>
        <session-status>RUNNING</session-status>
        <session-admin-status>START</session-admin-status>
        <session-established-transitions>1</session-established-transitions>
        <interface-error>false</interface-error>
        <neighbor-address>192.168.178.11</neighbor-address>
        <enabled>true</enabled>
        <messages>
          <sent>
            <UPDATE>1</UPDATE>
            <NOTIFICATION>0</NOTIFICATION>
          </sent>
          <received>
            <UPDATE>2</UPDATE>
            <NOTIFICATION>0</NOTIFICATION>
          </received>
        </messages>
        <queues>
          <input>0</input>
          <output>0</output>
        </queues>
      </state>
      <timers>
        <state>
          <connect-retry>0</connect-retry>
          <hold-time>90</hold-time>
          <keepalive-interval>30</keepalive-interval>
          <minimum-advertisement-interval>0</minimum-advertisement-interval>
          <uptime>195109000</uptime>
          <negotiated-hold-time>90</negotiated-hold-time>
        </state>
      </timers>
      <transport>
        <state>
          <tcp-mss>0</tcp-mss>
          <mtu-discovery>false</mtu-discovery>
          <passive-mode>false</passive-mode>
          <local-address>192.168.178.10</local-address>
          <local-port>45824</local-port>
          <remote-address>192.168.178.11</remote-address>
          <remote-port>179</remote-port>
        </state>
      </transport>
      <error-handling>
        <state>
          <treat-as-withdraw>false</treat-as-withdraw>
          <erroneous-update-messages>0</erroneous-update-messages>
        </state>
      </error-handling>
      <logging-options>
        <state>
          <log-neighbor-state-changes>false</log-neighbor-state-changes>
        </state>
      </logging-options>
      <ebgp-multihop>
        <state>
          <enabled>false</enabled>
          <multihop-ttl>0</multihop-ttl>
        </state>
      </ebgp-multihop>
      <route-reflector>
        <state>
          <route-reflector-cluster-id>zero-len</route-reflector-cluster-id>
          <route-reflector-client>false</route-reflector-client>
        </state>
      </route-reflector>
      <as-path-options>
        <state>
          <allow-own-as>1</allow-own-as>
          <replace-peer-as>false</replace-peer-as>
        </state>
      </as-path-options>
      <use-multiple-paths>
        <state>
          <enabled>false</enabled>
        </state>
        <ebgp>
          <state>
            <allow-multiple-as>false</allow-multiple-as>
            <maximum-paths>16</maximum-paths>
          </state>
        </ebgp>
        <ibgp>
          <state>
            <maximum-paths>16</maximum-paths>
          </state>
        </ibgp>
      </use-multiple-paths>
      <afi-safis>
        <afi-safi>
          <afi-safi-name>IPV4_UNICAST</afi-safi-name>
          <graceful-restart>
            <state>
              <enabled>true</enabled>
              <received>true</received>
              <advertised>true</advertised>
            </state>
          </graceful-restart>
          <state>
            <afi-safi-name>IPV4_UNICAST</afi-safi-name>
            <enabled>false</enabled>
            <active>true</active>
            <prefixes>
              <received>3</received>
              <sent>3</sent>
              <installed>0</installed>
            </prefixes>
            <prefix-limit-exceeded>false</prefix-limit-exceeded>
            <total-paths>3</total-paths>
            <total-prefixes>3</total-prefixes>
            <prefix-limit>
              <state>
                <max-prefixes>0</max-prefixes>
                <shutdown-threshold-pct>0</shutdown-threshold-pct>
                <restart-timer>0</restart-timer>
              </state>
            </prefix-limit>
          </state>
          <add-paths>
            <receive>false</receive>
            <send-max>0</send-max>
          </add-paths>
        </afi-safi>
      </afi-safis>
      <graceful-restart>
        <state>
          <enabled>false</enabled>
          <restart-time>120</restart-time>
          <stale-routes-time>300</stale-routes-time>
          <helper-only>true</helper-only>
          <peer-restart-time>120</peer-restart-time>
          <peer-restarting>false</peer-restarting>
          <local-restarting>false</local-restarting>
          <mode>HELPER_ONLY</mode>
        </state>
      </graceful-restart>
      <apply-policy>
        <state>
          <import-policy>
            bgp-in
          </import-policy>
          <export-policy>
            bgp-out
          </export-policy>
        </state>
      </apply-policy>
    </neighbor>
  </neighbors>
</bgp>
<database-status-information>
</database-status-information>
</data>

>>> 
display bgp states for bgp neighbor 192.168.178.11
>>> 
>>> data = dev.rpc.get("/bgp/neighbors/neighbor[neighbor-address='192.168.178.11']/state")
>>> print etree.tostring(data)
<data>
<bgp>
  <neighbors>
    <neighbor>
      <neighbor-address>192.168.178.11</neighbor-address>
      <state>
        <peer-as>110</peer-as>
        <local-as>104</local-as>
        <peer-type>EXTERNAL</peer-type>
        <auth-password>(null)</auth-password>
        <remove-private-as>0</remove-private-as>
        <route-flap-damping>false</route-flap-damping>
        <description>(null)</description>
        <session-state>ESTABLISHED</session-state>
        <supported-capabilities>
          MPBGP
        </supported-capabilities>
        <supported-capabilities>
          ROUTE_REFRESH
        </supported-capabilities>
        <supported-capabilities>
          GRACEFUL_RESTART
        </supported-capabilities>
        <peer-group>OC</peer-group>
        <session-status>RUNNING</session-status>
        <session-admin-status>START</session-admin-status>
        <session-established-transitions>1</session-established-transitions>
        <interface-error>false</interface-error>
        <neighbor-address>192.168.178.11</neighbor-address>
        <enabled>true</enabled>
        <messages>
          <sent>
            <UPDATE>1</UPDATE>
            <NOTIFICATION>0</NOTIFICATION>
          </sent>
          <received>
            <UPDATE>2</UPDATE>
            <NOTIFICATION>0</NOTIFICATION>
          </received>
        </messages>
        <queues>
          <input>0</input>
          <output>0</output>
        </queues>
      </state>
    </neighbor>
  </neighbors>
</bgp>
<database-status-information>
</database-status-information>
</data>

>>> 
display some bgp states only for bgp neighbor 192.168.178.11
>>> data = dev.rpc.get("/bgp/neighbors/neighbor[neighbor-address='192.168.178.11']/timers/state/hold-time")
>>> print etree.tostring(data)
<data>
<bgp>
  <neighbors>
    <neighbor>
      <neighbor-address>192.168.178.11</neighbor-address>
      <timers>
        <state>
          <hold-time>90</hold-time>
        </state>
      </timers>
    </neighbor>
  </neighbors>
</bgp>
<database-status-information>
</database-status-information>
</data>

>>> 

How to display openconfig configuration using the PyEZ cli method

This is FYI only. We should not use the cli method. We should always use the equivalent RPC only (see examples above).

display bgp openconfig configuration
>>> x = dev.rpc.cli(command="show configuration openconfig-bgp:bgp")
>>> type(x)
<type 'lxml.etree._Element'>
>>> print etree.tostring(x)
<configuration-information>
<configuration-output>
neighbors {
    neighbor 192.168.178.11 {
        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;
            }
        }
    }
}
</configuration-output>
</configuration-information>

>>> 
display bgp openconfig configuration
>>> x = dev.cli("show configuration openconfig-bgp:bgp")
>>> type(x)
<type 'str'>
>>> print x

neighbors {
    neighbor 192.168.178.11 {
        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;
            }
        }
    }
}

>>> 

display bgp openconfig configuration with set format
>>> x = dev.rpc.cli(command="show configuration openconfig-bgp:bgp| display set")
>>> type(x)
<type 'lxml.etree._Element'>
>>> print etree.tostring(x)
<configuration-information>
<configuration-output>
set openconfig-bgp:bgp neighbors neighbor 192.168.178.11 config peer-as 110
set openconfig-bgp:bgp neighbors neighbor 192.168.178.11 config peer-group OC
set openconfig-bgp:bgp peer-groups peer-group OC config local-as 104
set openconfig-bgp:bgp peer-groups peer-group OC config peer-type EXTERNAL
set openconfig-bgp:bgp peer-groups peer-group OC apply-policy config import-policy bgp-in
set openconfig-bgp:bgp peer-groups peer-group OC apply-policy config export-policy bgp-out
</configuration-output>
</configuration-information>
>>> 
display bgp openconfig configuration with set format
>>> x = dev.cli("show configuration openconfig-bgp:bgp| display set")
>>> type(x)
<type 'str'>
>>> print x
set openconfig-bgp:bgp neighbors neighbor 192.168.178.11 config peer-as 110
set openconfig-bgp:bgp neighbors neighbor 192.168.178.11 config peer-group OC
set openconfig-bgp:bgp peer-groups peer-group OC config local-as 104
set openconfig-bgp:bgp peer-groups peer-group OC config peer-type EXTERNAL
set openconfig-bgp:bgp peer-groups peer-group OC apply-policy config import-policy bgp-in
set openconfig-bgp:bgp peer-groups peer-group OC apply-policy config export-policy bgp-out
>>> 
display openconfig configuration with xml format
>>> x = dev.rpc.cli(command="show configuration openconfig-bgp:bgp| display xml")
>>> print etree.tostring(x)
<bgp>
    <neighbors>
        <neighbor>
            <neighbor-address>192.168.178.11</neighbor-address>
            <config>
                <peer-as>110</peer-as>
                <peer-group>OC</peer-group>
            </config>
        </neighbor>
    </neighbors>
    <peer-groups>
        <peer-group>
            <peer-group-name>OC</peer-group-name>
            <config>
                <local-as>104</local-as>
                <peer-type>EXTERNAL</peer-type>
            </config>
            <apply-policy>
                <config>
                    <import-policy>bgp-in</import-policy>
                    <export-policy>bgp-out</export-policy>
                </config>
            </apply-policy>
        </peer-group>
    </peer-groups>
</bgp>

>>> 
display openconfig configuration with xml format
>>> x = dev.cli("show configuration openconfig-bgp:bgp| display xml")
>>> type(x)
<type 'lxml.etree._Element'>
>>> print etree.tostring(x)
<bgp>
    <neighbors>
        <neighbor>
            <neighbor-address>192.168.178.11</neighbor-address>
            <config>
                <peer-as>110</peer-as>
                <peer-group>OC</peer-group>
            </config>
        </neighbor>
    </neighbors>
    <peer-groups>
        <peer-group>
            <peer-group-name>OC</peer-group-name>
            <config>
                <local-as>104</local-as>
                <peer-type>EXTERNAL</peer-type>
            </config>
            <apply-policy>
                <config>
                    <import-policy>bgp-in</import-policy>
                    <export-policy>bgp-out</export-policy>
                </config>
            </apply-policy>
        </peer-group>
    </peer-groups>
</bgp>
display openconfig configuration with json format
>>> x = dev.rpc.cli(command="show configuration openconfig-bgp:bgp| display json")
>>> type (x)
<type 'lxml.etree._Element'>
>>> print etree.tostring(x)
<configuration-information>
<json-output>
{
    "openconfig-bgp:bgp" : {
        "neighbors" : {
            "neighbor" : [
            {
                "neighbor-address" : "192.168.178.11", 
                "config" : {
                    "peer-as" : 110, 
                    "peer-group" : "OC"
                }
            }
            ]
        }, 
        "peer-groups" : {
            "peer-group" : [
            {
                "peer-group-name" : "OC", 
                "config" : {
                    "local-as" : 104, 
                    "peer-type" : "EXTERNAL"
                }, 
                "apply-policy" : {
                    "config" : {
                        "import-policy" : ["bgp-in"],
                        "export-policy" : ["bgp-out"]
                    }
                }
            }
            ]
        }
    }
}
</json-output>
</configuration-information>

>>> 
display openconfig configuration translated in the junos data model using cli method
>>> cnf = dev.cli("show configuration groups junos-yang")
>>> print cnf

protocols {
    bgp {
        group OC {
            type external;
            import bgp-in;
            export bgp-out;
            local-as 104;
            neighbor 192.168.178.11 {
                peer-as 110;
            }
        }
    }
}

>>> 
display openconfig configuration translated in the junos data model with set format
>>> cnf = dev.cli("show configuration groups junos-yang | display set")
>>> print cnf

set groups junos-yang protocols bgp group OC type external
set groups junos-yang protocols bgp group OC import bgp-in
set groups junos-yang protocols bgp group OC export bgp-out
set groups junos-yang protocols bgp group OC local-as 104
set groups junos-yang protocols bgp group OC neighbor 192.168.178.11 peer-as 110

>>> 
display openconfig configuration translated in the junos data model with xml format
>>> cnf = dev.cli("show configuration groups junos-yang | display xml")
>>> print etree.tostring(cnf)
<configuration commit-seconds="1494111804" commit-localtime="2017-05-06 23:03:24 UTC" commit-user="ansible">
    <groups>
        <name>junos-yang</name>
        <protocols>
            <bgp>
                <group>
                    <name>OC</name>
                    <type>external</type>
                    <import>bgp-in</import>
                    <export>bgp-out</export>
                    <local-as>
                        <as-number>104</as-number>
                    </local-as>
                    <neighbor>
                        <name>192.168.178.11</name>
                        <peer-as>110</peer-as>
                    </neighbor>
                </group>
            </bgp>
        </protocols>
    </groups>
</configuration>
>>>
display openconfig configuration translated in the junos data model with json format
>>> x = dev.rpc.cli(command="show configuration groups junos-yang| display json")
>>> type(x)
<type 'lxml.etree._Element'>
>>> print etree.tostring(x)
<configuration-information>
<json-output>
{
    "configuration" : {
        "@" : {
            "xmlns" : "http://xml.juniper.net/xnm/1.1/xnm", 
            "junos:commit-seconds" : "1494111804", 
            "junos:commit-localtime" : "2017-05-06 23:03:24 UTC", 
            "junos:commit-user" : "ansible"
        }, 
        "groups" : [
        {
            "name" : "junos-yang"
            "protocols" : [null]
                "bgp" : [null]
                    {
                        "name" : "OC"
                        "type" : "external"
                        "import" : ["bgp-in"]
                        "export" : ["bgp-out"]
                        "local-as" : {
                            "as-number" : "104"
                        }
                        {
                            "name" : "192.168.178.11"
                            "peer-as" : "110"
                        }
                        ]
                    }
                    ]
        }
        ]
    }
}
</json-output>
</configuration-information>

>>> 
Clone this wiki locally