This Learning Lab explores the network interfaces of a network device.
Pre-requisite Learning Lab: Inventory
Keyword | Definition |
---|---|
Network Interface | Connector from one network device to a network interface on one or more other network devices. |
Network Device | Routers, switches, etc. |
Controller | Service that intermediates between client applications and network devices. E.g. ODL, COSC |
ODL | OpenDayLight |
COSC | Cisco Open Source Controller, enhanced ODL |
interface_configuration_xml(device)
interface_configuration_xml(device, interface)
interface_configuration_json(device)
interface_configuration_json(device, interface)
interface_properties_xml(device)
interface_properties_xml(device, interface)
interface_properties_json(device)
interface_properties_json(device, interface)
If the Learning Lab team have provided you with either:
then please proceed. Otherwise, please follow the instructions on web page: How to set up your computer
Import the Python module named learning_lab
.
In [1]:
import learning_lab
We need one network device to explore. Any network device is satisfactory, provided it is connected to the Controller and has multiple network interfaces.
The following code selects a suitable network device from the inventory using convenience function sample_device()
.
In [2]:
%run 01_inventory_mount
from basics.sample import sample_device
device_name = 'xrvr-1'
Note that we only require the name of the sample network device.
In [3]:
device_name
Out[3]:
In this lesson, we will:
InterfaceConfiguration
and InterfaceProperties
List the network interfaces of the selected network device, using function:
interface_names(device_name)
.
In [4]:
from basics.interface import interface_names
interface_names(device_name)
Out[4]:
Determine which network interface the Controller is using to manage the network device, using function:
management_interface(device_name)
In [5]:
from basics.interface import management_interface
management_interface(device_name)
Out[5]:
Each network interface belongs to one of the following two categories:
Normal network traffic flows between data interfaces. A management interface is for monitoring and modifying the data interfaces on the same network device. For example, you can shutdown/startup a data interface via a management interface.
Note: if you shutdown a management interface then the connection terminates and you will not be able to send it the startup command.
Obtain the configuration of all network interfaces on one network device, using function:
interface_configuration_xml(device_name)
The XML representation of data is popular in client applications. The Controller is capable of representing the configuration data in either XML or JSON format. The HTTP Reference shows the actual XML and JSON documents returned from the Controller.
In [6]:
from basics.interface import interface_configuration_xml
xml = interface_configuration_xml(device_name)
Additional functions are used to transform the XML into a HTML table to make it pleasant for humans to read. The implementation uses XSLT.
In [7]:
from basics.interface import interface_configuration_to_html
from IPython.core.display import HTML
HTML(interface_configuration_to_html(xml))
Out[7]:
A single network interface can be specified by name as the second parameter to function:
interface_configuration_xml(device_name, interface_name)
A single network interface is chosen, randomly, from the list of interface names.
In [8]:
import random
interface_name = random.choice(interface_names(device_name))
The XML representation is obtained...
In [9]:
xml = interface_configuration_xml(device_name, interface_name)
... and transformed to HTML. We expect the HTML table to contain exactly one row of data, identical to one row in the table above.
In [10]:
HTML(interface_configuration_to_html(xml))
Out[10]:
XML is ideal for transformation to another markup language, such as HTML, as shown above. JSON is an alternative to XML that is popular. Network interface configuration is available in JSON from functions:
interface_configuration_json(device_name)
interface_configuration_json(device_name, interface_name)
In [11]:
from basics.interface import interface_configuration_json
json = interface_configuration_json(device_name, interface_name)
The JSON for a single network interface is reasonably small.
In [12]:
json
Out[12]:
The JSON for all network interfaces (of one network device).
In [13]:
json = interface_configuration_json(device_name)
This JSON data structure is shown in the HTTP Reference due to its complexity.
Typically usage of JSON is to index into the data structure. This example accesses the interface name and description.
In [14]:
[(
item[u'interface-name'],
item[u'description']
)
for item in json
[u'interface-configurations']
[u'interface-configuration']]
Out[14]:
It is more difficult to access the IP address because the path depends on IPv4 versus IPv6. Note that in XML the IP address is accessed using a XPATH expression with a wildcard matching either IPv4 or IPv6.
In [15]:
xml.findtext('//{*}primary/{*}address')
Out[15]:
Configuration of network interfaces is available as a Python named tuple
. The functions are:
interface_configuration_tuple(device_name)
interface_configuration_tuple(device_name, interface_name)
In [16]:
from basics.interface import interface_configuration_tuple
tuple = interface_configuration_tuple(device_name, interface_name)
The type of the named tuple is a class in Python.
In [17]:
type(tuple)
Out[17]:
A tuple provides access to the fields in a language that is very natural. Several fields are accessed to demonstrate.
In [18]:
tuple.name
Out[18]:
In [19]:
tuple.address
Out[19]:
In [20]:
tuple.netmask
Out[20]:
In [21]:
tuple.shutdown
Out[21]:
The value of field shutdown
, above, is implied by the existence or absence of a specific element in XML (or field in JSON). For this reason, the Python tuple is more convenient to use in a client application than a test for the presence of fields in XML and JSON.
In [22]:
json = interface_configuration_json(device_name, interface_name)
shutdown = 'shutdown' in json['interface-configuration'][0]
assert shutdown == tuple.shutdown
List all field names of the Python class.
In [23]:
tuple.__dict__.keys()
Out[23]:
Note that the tuple field names are not strings, as they are in JSON. Here is the access of field description
in JSON.
In [24]:
json['interface-configuration'][0]['description']
Out[24]:
The JSON field access, above, can only be verified at run-time, whereas the tuple field access, below, can be checked by the editor and can participate in code completion, because the field is pre-defined. This is another way the Python tuple adds value to the underlying XML or JSON (from which the tuple is built).
In [25]:
tuple.description
Out[25]:
All examples of the Python tuple, above, have been for a single network interface. The configuration of multiple network interfaces (of one network device) is available as a list of tuples.
In [26]:
bulk = interface_configuration_tuple(device_name)
[(tuple.name, tuple.address) for tuple in bulk]
Out[26]:
Obtain the properties of all network interfaces of one network device, using function:
interface_properties_xml(device_name)
In [27]:
from basics.interface import interface_properties_xml
xml = interface_properties_xml(device_name)
Additional functions are employed to transform the XML into two HTML tables. The reason for two HTML tables is simply to fit all the properties into the width of this document. The XML is transformed to HTML using a XSLT stylesheet.
In [28]:
from basics.interface import interface_properties_to_html
HTML(interface_properties_to_html(xml))
Out[28]:
A single network interface can be specified by name as the second parameter to function:
interface_properties_xml(device_name, interface_name)
The specific interface was chosen randomly.
In [29]:
xml = interface_properties_xml(device_name, interface_name)
The XML document is displayed in the HTTP Reference. The same XSLT stylesheet used above can be applied to the XML to produce HTML tables. We expect the two HTML tables to contain exactly one row of data, identical to one row in each of the two tables above.
In [30]:
HTML(interface_properties_to_html(xml))
Out[30]:
The interface properties are returned in JSON representation from function:
interface_properties_json(device_name)
The HTTP Reference shows the actual JSON document returned from the Controller.
The sample code below extracts three of the many properties available for each network interface:
In [31]:
from basics.interface import interface_properties_json
[(
item[u'interface-name'],
item[u'type'],
item[u'encapsulation']
)
for item in interface_properties_json(device_name)
[u'interface-properties']
[u'data-nodes']
[u'data-node']
[0]
[u'system-view']
[u'interfaces']
[u'interface']
]
Out[31]:
In [32]:
json = interface_properties_json(device_name, interface_name)
The JSON document is displayed in the HTTP Reference.
The property names and values can be accessed directly from the JSON structure.
In [33]:
[(name, value) for (name,value) in json[u'interface'][0].iteritems()]
Out[33]:
The properties of a network interface are available as a Python named tuple
. The functions are:
In [34]:
from basics.interface import interface_properties_tuple
tuple = interface_properties_tuple(device_name, interface_name)
The type of the tuple is a class in Python.
In [35]:
type(tuple)
Out[35]:
List all fields of the Python class.
In [36]:
tuple.__dict__.keys()
Out[36]:
A tuple provides access to the fields in a language that is very natural. Several fields are accessed to demonstrate. Note that the property values below have different types: string, boolean and integer.
In [37]:
tuple.name
Out[37]:
In [38]:
tuple.l2_transport
Out[38]:
In [39]:
tuple.mtu
Out[39]:
All examples of the Python tuple, above, have been for a single network interface. The properties of multiple network interfaces (of one network device) are available as a list of tuples.
In [40]:
bulk = interface_properties_tuple(device_name)
[(tuple.name, tuple.state) for tuple in bulk]
Out[40]:
In this lesson we:
InterfaceConfiguration
and InterfaceProperties
The HTTP request and response are shown for each remote procedure call (RPC) to the Controller.
See the Learning Lab document HTTP for a demonstration of how to use the tables below.
Function:
interface_configuration_xml(device_name)
invokes function:
interface_configuration_xml_http(device_name)
which returns the HTTP request and response, as shown in the table below.
The response from the Controller is very large because it describes multiple network interfaces. The next RPC is for a single network interface and is thus shorter. To help you navigate this document, here is a link to the next section and the TOC.
In [41]:
from basics.interface import interface_configuration_xml_http
from basics.http import http_to_html
HTML(http_to_html(interface_configuration_xml_http(device_name)))
Out[41]:
The function:
interface_configuration_xml(device_name, interface_name)
invokes function:
interface_configuration_xml_http(device_name, interface_name)
which returns the HTTP request and response, as shown in the table below. The specific interface was chosen randomly.
To help you navigate around the large tables in this document, here are links to the previous section, next section and the TOC.
In [42]:
http = interface_configuration_xml_http(device_name, interface_name)
HTML(http_to_html(http))
Out[42]:
Function:
interface_configuration_json(device_name)
invokes function:
interface_configuration_json_http(device_name)
which returns the HTTP request and response, as shown in the table below.
The response from the Controller is very large because it describes multiple network interfaces. The next RPC is for a single network interface and is thus shorter. To help you navigate this document, here is a link to the previous section, next section and the TOC.
In [43]:
from basics.interface import interface_configuration_json_http
HTML(http_to_html(interface_configuration_json_http(device_name)))
Out[43]:
The function:
interface_configuration_json(device_name, interface_name)
invokes function:
interface_configuration_json_http(device_name, interface_name)
which returns the HTTP request and response, as shown in the table below. The specific interface was chosen randomly.
To help you navigate around the large tables in this document, here are links to the previous section, next section and the TOC.
In [44]:
HTML(http_to_html(interface_configuration_json_http(device_name, interface_name)))
Out[44]:
The function:
interface_properties_xml(device_name)
invokes function:
interface_properties_xml_http(device_name)
which returns the HTTP request and response, as shown in the table below.
To help you navigate around the large tables in this document, here are links to the previous section, next section and the TOC.
In [45]:
from basics.interface import interface_properties_xml_http
HTML(http_to_html(interface_properties_xml_http(device_name)))
Out[45]:
The function:
interface_properties_json(device_name, interface_name)
invokes function:
interface_properties_json_http(device_name, interface_name)
which returns the HTTP request and response, as shown in the table below. The specific interface was chosen randomly.
To help you navigate around the large tables in this document, here are links to the previous section, next section and the TOC.
In [46]:
HTML(http_to_html(interface_properties_xml_http(device_name, interface_name)))
Out[46]:
The function:
interface_properties_json(device_name)
invokes function:
interface_properties_json_http(device_name)
which returns the HTTP request and response, as shown in the table below.
To help you navigate around the large tables in this document, here are links to the previous section, next section and the TOC.
In [47]:
from basics.interface import interface_properties_json_http
HTML(http_to_html(interface_properties_json_http(device_name)))
Out[47]:
The function:
interface_properties_json(device_name, interface_name)
invokes function:
interface_properties_json_http(device_name, interface_name)
which returns the HTTP request and response, as shown in the table below. The specific interface was chosen randomly.
To help you navigate around the large tables in this document, here are links to the previous section and the TOC.
In [48]:
HTML(http_to_html(interface_properties_json_http(device_name, interface_name)))
Out[48]: