<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Learning Lab
Step1:  Sample Network Device
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().
Step2:  Note that we only require the name of the sample network device.
Step3:  Introduction
In this lesson, we will
Step4:  Management Interface
Determine which network interface the Controller is using to manage the network device, using function
Step5:  Each network interface belongs to one of the following two categories
Step6:  Additional functions are used to transform the XML into a HTML table to make it pleasant for humans to read. The implementation uses XSLT.
Step7:  For one network interface
A single network interface can be specified by name as the second parameter to function
Step8:  The XML representation is obtained...
Step9:  ... and transformed to HTML. We expect the HTML table to contain exactly one row of data, identical to one row in the table above.
Step10:  Interface Configuration
Step11:  The JSON for a single network interface is reasonably small.
Step12:  The JSON for all network interfaces (of one network device).
Step13:  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.
Step14:  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.
Step15:  Python type InterfaceConfiguration
Configuration of network interfaces is available as a Python named tuple. The functions are
Step16:  The type of the named tuple is a class in Python.
Step17:  A tuple provides access to the fields in a language that is very natural. Several fields are accessed to demonstrate.
Step18:  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.
Step19:  List all field names of the Python class.
Step20:  Note that the tuple field names are not strings, as they are in JSON. Here is the access of field description in JSON.
Step21:  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).
Step22:  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.
Step23:  Interface Properties
The Controller is capable of representing network interface properties in either XML or JSON format. The HTTP Reference shows the actual XML and JSON documents returned from the Controller.
Interface Properties
Step24:  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.
Step25:  A single network interface can be specified by name as the second parameter to function
Step26:  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.
Step27:  Interface Properties
Step28:  The JSON document is displayed in the HTTP Reference. 
The property names and values can be accessed directly from the JSON structure.
Step29:  Python type InterfaceProperties
The properties of a network interface are available as a Python named tuple. The functions are
Step30:  The type of the tuple is a class in Python.
Step31:  List all fields of the Python class.
Step32:  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
Step33:  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.
Step34:  Conclusion
In this lesson we
Step35:  RPC interface_configuration_xml
Step36:  RPC interface_configuration_json
Step37:  RPC interface_configuration_json
Step38:  RPC interface_properties_xml
Step39:  RPC interface_properties_xml
Step40:  RPC interface_properties_json
Step41:  RPC interface_properties_json
<ASSISTANT_TASK:>
Python Code:
import learning_lab
Explanation: Learning Lab: Interface
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|
Table of Contents
Table of Contents
Prologue
Import Learning Lab
Verify Controller
Sample Network Device
Introduction
List of Network Interfaces
Management Interface
Interface Configuration
XML
For all network interfaces
For one network interface
JSON
Python type InterfaceConfiguration
Interface Properties
XML
JSON
Python type InterfaceProperties
Conclusion
HTTP Reference
RPC interface_configuration_xml(device)
RPC interface_configuration_xml(device, interface)
RPC interface_configuration_json(device)
RPC interface_configuration_json(device, interface)
RPC interface_properties_xml(device)
RPC interface_properties_xml(device, interface)
RPC interface_properties_json(device)
RPC interface_properties_json(device, interface)
Prologue
Import Learning Lab
If the Learning Lab team have provided you with either:
* a Python Interactive Shell, or
* an IPython Notebook
then please proceed. Otherwise, please follow the instructions on web page: How to set up your computer
Import the Python module named learning_lab.
End of explanation
%run 01_inventory_mount
from basics.sample import sample_device
device_name = 'xrvr-1'
Explanation: Sample Network Device
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().
End of explanation
device_name
Explanation: Note that we only require the name of the sample network device.
End of explanation
from basics.interface import interface_names
interface_names(device_name)
Explanation: Introduction
In this lesson, we will:
1. List the names of network interfaces on a single network device. 
2. Determine which network interface the Controller is using to manage the network device. 
3. For each network interface, obtain: 
    * configuration
    * properties
4. Configuration and properties will be obtained:
    * in bulk - all network interfaces
    * individually - a single network interface, specified by name
5. The data formats will be:
    * XML - demonstration of transformation to HTML using XSLT
    * JSON - indexing deep into a complex document
    * Python - types InterfaceConfiguration and InterfaceProperties    
List of Network Interfaces
List the network interfaces of the selected network device, using function:
* interface_names(device_name).
End of explanation
from basics.interface import management_interface
management_interface(device_name)
Explanation: Management Interface
Determine which network interface the Controller is using to manage the network device, using function:
* management_interface(device_name)
End of explanation
from basics.interface import interface_configuration_xml
xml = interface_configuration_xml(device_name)
Explanation: Each network interface belongs to one of the following two categories:
* management interface
* data interface
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.
Interface Configuration
Interface Configuration: XML
For all network interfaces
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.
End of explanation
from basics.interface import interface_configuration_to_html
from IPython.core.display import HTML
HTML(interface_configuration_to_html(xml))
Explanation: Additional functions are used to transform the XML into a HTML table to make it pleasant for humans to read. The implementation uses XSLT.
End of explanation
import random
interface_name = random.choice(interface_names(device_name))
Explanation: For one network interface
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.
End of explanation
xml = interface_configuration_xml(device_name, interface_name)
Explanation: The XML representation is obtained...
End of explanation
HTML(interface_configuration_to_html(xml))
Explanation: ... and transformed to HTML. We expect the HTML table to contain exactly one row of data, identical to one row in the table above.
End of explanation
from basics.interface import interface_configuration_json
json = interface_configuration_json(device_name, interface_name)
Explanation: Interface Configuration: JSON
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)
End of explanation
json
Explanation: The JSON for a single network interface is reasonably small.
End of explanation
json = interface_configuration_json(device_name)
Explanation: The JSON for all network interfaces (of one network device).
End of explanation
[(
    item[u'interface-name'], 
    item[u'description']
 ) 
 for item in json
     [u'interface-configurations']
     [u'interface-configuration']]
Explanation: 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.
End of explanation
xml.findtext('//{*}primary/{*}address')
Explanation: 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.
End of explanation
from basics.interface import interface_configuration_tuple
tuple = interface_configuration_tuple(device_name, interface_name)
Explanation: Python type InterfaceConfiguration
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)
End of explanation
type(tuple)
Explanation: The type of the named tuple is a class in Python.
End of explanation
tuple.name
tuple.address
tuple.netmask
tuple.shutdown
Explanation: A tuple provides access to the fields in a language that is very natural. Several fields are accessed to demonstrate.
End of explanation
json = interface_configuration_json(device_name, interface_name)
shutdown = 'shutdown' in json['interface-configuration'][0]
assert shutdown == tuple.shutdown
Explanation: 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.
End of explanation
tuple.__dict__.keys()
Explanation: List all field names of the Python class.
End of explanation
json['interface-configuration'][0]['description']
Explanation: Note that the tuple field names are not strings, as they are in JSON. Here is the access of field description in JSON.
End of explanation
tuple.description
Explanation: 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).
End of explanation
bulk = interface_configuration_tuple(device_name)
[(tuple.name, tuple.address) for tuple in bulk]
Explanation: 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.
End of explanation
from basics.interface import interface_properties_xml
xml = interface_properties_xml(device_name)
Explanation: Interface Properties
The Controller is capable of representing network interface properties in either XML or JSON format. The HTTP Reference shows the actual XML and JSON documents returned from the Controller.
Interface Properties: XML
Obtain the properties of all network interfaces of one network device, using function:
* interface_properties_xml(device_name)
End of explanation
from basics.interface import interface_properties_to_html
HTML(interface_properties_to_html(xml))
Explanation: 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.
End of explanation
xml = interface_properties_xml(device_name, interface_name)
Explanation: 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.
End of explanation
HTML(interface_properties_to_html(xml))
Explanation: 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.
End of explanation
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']
]
json = interface_properties_json(device_name, interface_name)
Explanation: Interface Properties: JSON
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: 
* interface-name
* type
* encapsulation.
End of explanation
[(name, value) for (name,value) in json[u'interface'][0].iteritems()]
Explanation: The JSON document is displayed in the HTTP Reference. 
The property names and values can be accessed directly from the JSON structure.
End of explanation
from basics.interface import interface_properties_tuple
tuple = interface_properties_tuple(device_name, interface_name)
Explanation: Python type InterfaceProperties
The properties of a network interface are available as a Python named tuple. The functions are:
* interface_properties_tuple(device_name)
* interface_properties_tuple(device_name, interface_name)
End of explanation
type(tuple)
Explanation: The type of the tuple is a class in Python.
End of explanation
tuple.__dict__.keys()
Explanation: List all fields of the Python class.
End of explanation
tuple.name
tuple.l2_transport
tuple.mtu
Explanation: 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.
End of explanation
bulk = interface_properties_tuple(device_name)
[(tuple.name, tuple.state) for tuple in bulk]
Explanation: 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.
End of explanation
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)))
Explanation: Conclusion
In this lesson we:
1. Listed the names of network interfaces on a single network device. 
2. Determined which network interface the Controller is using to manage the network device. 
3. For each network interface we obtained: 
    * configuration
    * properties
4. The configuration and properties of network interfaces are available, per network device:
    * in bulk - all network interfaces
    * individually - a single network interface, specified by name
5. The data formats used were:
    * XML - demonstration of transformation to HTML using XSLT
    * JSON - indexing deep into a complex document
    * Python - types InterfaceConfiguration and InterfaceProperties
HTTP Reference
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.
RPC interface_configuration_xml: multiple
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.
End of explanation
http = interface_configuration_xml_http(device_name, interface_name)
HTML(http_to_html(http))
Explanation: RPC interface_configuration_xml: single
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.
End of explanation
from basics.interface import interface_configuration_json_http
HTML(http_to_html(interface_configuration_json_http(device_name)))
Explanation: RPC interface_configuration_json: multiple
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.
End of explanation
HTML(http_to_html(interface_configuration_json_http(device_name, interface_name)))
Explanation: RPC interface_configuration_json: single
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.
End of explanation
from basics.interface import interface_properties_xml_http
HTML(http_to_html(interface_properties_xml_http(device_name)))
Explanation: RPC interface_properties_xml: multiple
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.
End of explanation
HTML(http_to_html(interface_properties_xml_http(device_name, interface_name)))
Explanation: RPC interface_properties_xml: single
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.
End of explanation
from basics.interface import interface_properties_json_http
HTML(http_to_html(interface_properties_json_http(device_name)))
Explanation: RPC interface_properties_json: multiple
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.
End of explanation
HTML(http_to_html(interface_properties_json_http(device_name, interface_name)))
Explanation: RPC interface_properties_json: single
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.
End of explanation
<END_TASK>