Properties

Python Path


In [3]:
import sys
sys.path.append("..")

Select Network Topology


In [4]:
from os import environ
environ["NETWORK_PROFILE"] = "giles"

Select Device

The following code selects one device from the inventory that is connected and has more than one interface.


In [5]:
from basics.inventory import inventory_connected
from basics.interface_names import interface_names
from basics.interface_properties import interface_properties

for device_name in inventory_connected():
    interface_names_list = interface_names(device_name)
    if len(interface_names_list) > 1:
        for interface_name in interface_names_list:
            print device_name,
            print interface_properties(device_name, interface_name)
        break
device_name


network profile detected as: giles
http://172.23.29.120:8181/restconf/operational/opendaylight-inventory:nodes/node/controller-config/yang-ext:mount/Cisco-IOS-XR-ifmgr-oper:interface-properties
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-5-77939718843b> in <module>()
      4 
      5 for device_name in inventory_connected():
----> 6     interface_names_list = interface_names(device_name)
      7     if len(interface_names_list) > 1:
      8         for interface_name in interface_names_list:

/Users/kjarrad/git/python-odl-hackathon/src/basics/interface_names.pyc in interface_names(device_name)
     20     'Return a list of interface names, given the name of a mounted, connected device.'
     21     url_suffix = _url_template % urllib.quote_plus(device_name)
---> 22     response = odl_http_get(url_suffix, 'application/xml', expected_status_code=[200, 400])
     23     if response.status_code == 400:
     24         return []  # The inventory item does not have interfaces.

/Users/kjarrad/git/python-odl-hackathon/src/basics/odl_http.pyc in odl_http_get(url_suffix, accept, expected_status_code, contentType, content)
     66 ):
     67     'Get a response from the ODL server.'
---> 68     return odl_http_request('get', url_suffix, contentType, content, accept, expected_status_code)
     69 
     70 def odl_http_post(

/Users/kjarrad/git/python-odl-hackathon/src/basics/odl_http.pyc in odl_http_request(method, url_suffix, contentType, content, accept, expected_status_code)
     48             print content
     49         msg = 'Expected HTTP status code %s, got %d, response: %s' % (expected_status_code, response.status_code, response.text)
---> 50         raise Exception(msg)
     51     else:
     52         return response

Exception: Expected HTTP status code [200, 400], got 500, response: <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 500 Internal Server Error</title>
</head>
<body><h2>HTTP ERROR 500</h2>
<p>Problem accessing /restconf/operational/opendaylight-inventory:nodes/node/controller-config/yang-ext:mount/Cisco-IOS-XR-ifmgr-oper:interface-properties. Reason:
<pre>    Internal Server Error</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                

</body>
</html>

Select Interface


In [ ]:
from basics.inventory import inventory_not_connected
inventory_not_connected()

Inventory JSON

The JSON response from CDL is accessible via function inventory_json().
Here is a code sample that aggregates the JSON data into a table.


In [ ]:
from basics.inventory import inventory_json

name_field = u'id'
connected_field = u'netconf-node-inventory:connected'
capability_field = u'netconf-node-inventory:initial-capability'

from ipy_table import *
items_table = [(
    item[name_field],
    item[connected_field] if connected_field in item else '',
    len(item[capability_field]) if capability_field in item else '')
    for item in inventory_json()]
items_table.insert(0, ('name','connected','capabilities'))
make_table(items_table)
apply_theme('basic')

Configured devices

List of devices specified in the settings:


In [ ]:
from settings.config import config
config['network_device'].keys()

Outstanding devices

List of devices specified in the settings but not connected:


In [ ]:
set(config['network_device'].keys()) - set(inventory_connected())

Mount all devices from settings

Attempt to connect the outstanding devices.


In [ ]:
from basics.mount_device import mount_device
from settings.config import config

for (device_name, device) in config['network_device'].items():
    mount_device(
        device_name,
        device['address'],
        device['port'],
        device['username'],
        device['password'])

Refresh inventory

The outstanding devices are visible in the inventory below. It takes a few seconds to establish a connection. Execute this code repeatedly until all expected connections appear. The connection status is the second column.


In [ ]:
from basics.inventory import inventory_summary
inventory_summary()

Generate markdown output


In [ ]:
from basics.inventory import inventory_json

name_field = u'id'
connected_field = u'netconf-node-inventory:connected'
capability_field = u'netconf-node-inventory:initial-capability'

table_md = '''
    |Name|Is Connected|Number of Capabilities|
    |:---|:---:|---:|
'''

for item in inventory_json():
    table_md += '|%s|%s|%s|\n' % (
        item[name_field],
        item[connected_field] if connected_field in item else '',
        len(item[capability_field]) if capability_field in item else '')
    
# from basics.markdown import normtable
# print normtable(table_md)