One of the objectives of this Learning Lab is to demonstrate how to communicate directly with the Controller using HTTP. In each lesson of this Learning Lab, the sample code invokes functions in the Python library. The library functions communicate with the Controller by sending and receiving HTTP messages. Thus, the library is communicating with the Controller directly and the sample code is communicating with the library.
The structure is thus:
COSC Learning Lab -> lesson -> sample script in Python -> library layer in Python -> HTTP layer -> Controller
The data flow through the HTTP layer is excluded from the lessons but links are provided to pages that contain HTTP Reference Tables. These examples of actual HTTP requests and responses can be used to fulfil the stated objective. Here is an example of a HTTP Reference Table.
The following demonstrations are provided of direct communication with the Controller by HTTP:
curl
and wget
from the command lineIf 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
from basics.odl_http import http_history, http_history_clear
http_history_clear()
from basics.http import http_history_to_html
from IPython.core.display import HTML
Produce a reference table describing a HTTP request and response. This table is typical of the HTTP usage throughout this Learning Lab. This particular message was chosen because it is not too lengthy.
See also:
Python code is used to produce the table. An explanation of this code is not relevant. The goal is just to produce the table.
In [2]:
run 01_inventory_mounted
In [3]:
HTML(http_history_to_html(http_history()))
Out[3]:
In the HTML above there are two tables. The first table is titled 'Request' and the second table is titled 'Response'. The column at the left contains the names of HTTP fields. To the right of each HTTP field name is the field value.
Demonstrate how to use the information displayed in a reference table to reproduce a HTTP request and response.
The Controller is a HTTP server. To communicate with a HTTP server requires a HTTP Client. Some HTTP Client tools are:
A demonstration will be given of the last two tools in the list.
The field values of the HTTP request are assigned to variables.
In [4]:
from settings import config
address = config['odl_server']['address']
port = config['odl_server']['port']
url = 'http://%s:%s/restconf/config/opendaylight-inventory:nodes' % (address, port)
method = 'GET'
headers = {'Accept' : 'application/xml'}
content = None
A HTTP request is delivered, using the variables from the previous step.
In [5]:
from requests import request
response = request(method, url, headers=headers, data=content, auth=('admin', 'admin'))
The response has been received and can be examined. The value of each HTTP field matches the 'Response' table (above).
In [6]:
response.status_code
Out[6]:
In [7]:
response.headers
Out[7]:
In [8]:
response.content
Out[8]:
A command line alternative to Python using curl
.
In [9]:
!curl -i {url}
Notice that the HTTP response content (above) is JSON, not XML, because the 'Accept' header was not specified.
The same URL is requested using wget
. This time an 'Accept' header requests a XML response.
In [10]:
!wget -O - --header='Accept: application/xml' --progress=dot {url}
Achievements:
curl
and wget
to reproduce the HTTP request and reponse.