1. 程式人生 > >SDN實戰:Build a VXLAN Tunnel by Making Python-based API Calls for DCI

SDN實戰:Build a VXLAN Tunnel by Making Python-based API Calls for DCI

python restful api vxlan ovsdb

SDN IN ACTION: Build a VXLAN tunnel by making Python-based API Calls on OpenDaylight and Provide the DCI service

薛國鋒 [email protected]

今天做了個小實驗,通過Python程序調用OpenDaylight的北向API,在兩個DC之間建立VXLAN隧道,實現DCIDC內部采用Mininet c0控制器和OpenFlow協議,跨DC采用OpenDaylightOVSDB協議實現DC互聯。通過本次實驗深刻領悟到,

SDN控制器實際上就是一個API Gateway ,提供北向RESTful API以及南向控制協議(OpenFlowOVSDBNetConfBGP-LS等)之間的轉換,實現網絡動態可編程和業務自動化部署J


Today we will play with Python, making Python-based API calls on OpenDaylight and building a VXLAN tunnel between two DCs. Inside each DC, the Mininet C0 controller and OpenFlow are deployed; and with OVSDB, OpenDaylight manages both DCs and provides the interconnect between them. After this test, we would be deeply impressed that the SDN controller is a natural API gateway providing the conversion between NB RESTful API and SB control protocols, such asOpenFlow, OVSDB, NetConf and BGP-LS, etc; and to provide the programmability for network automation and service agility. Below is the physical and logicaldesign and topology:

技術分享


技術分享


1 Run OpenDaylight and install the necessary features


技術分享


[email protected]>feature:installodl-aaa-authn

[email protected]>feature:installodl-restconf-all

[email protected]>feature:installodl-dlux-core

[email protected]>feature:installodl-dluxapps-yangman

[email protected]>feature:installodl-dluxapps-topology

[email protected]>feature:installodl-l2switch-all

[email protected]>feature:installwebconsole

[email protected]>feature:installodl-mdsal-apidocs

[email protected]>feature:installodl-ovsdb-southbound-api

Please check the below web link for OVSDB user guide:

http://docs.opendaylight.org/en/stable-carbon/user-guide/ovsdb-user-guide.html

2 Set up DC1 – Mininet

Create the custom topology file – “dc1.py”:

from mininet.topo import Topo

class MyTopo( Topo ):

def__init__( self ):

#initilaize topology

Topo.__init__( self )

# addhosts and switches

h11 =self.addHost( ‘h11‘,ip= ‘10.0.0.1‘)

h12 =self.addHost( ‘h12‘,ip= ‘10.0.0.2‘)

s11 =self.addSwitch( ‘s11‘ )

s12 =self.addSwitch( ‘s12‘ )

s10 =self.addSwitch( ‘s10‘ )

# addlinks

self.addLink(h11,s11)

self.addLink(h12,s12)

self.addLink(s12,s10)

self.addLink(s10,s11)

topos = { ‘mytopo‘: ( lambda: MyTopo() ) }

Create the virtual DC1 with “dc1.py”::

[email protected]:~$sudo mn --custom dc1.py --topo mytopo

Configure OVS to listen for the connection initiated by the OVSDB Southbound Plugin of OpenDaylight:

mininet>sh ovs-vsctl set-manager ptcp:6640

3 Set up DC2 – Mininet

Create the custom topology file – “dc2.py”:

from mininet.topo import Topo

class MyTopo( Topo ):

def__init__( self ):

#initilaize topology

Topo.__init__( self )

# addhosts and switches

h21 =self.addHost( ‘h21‘,ip= ‘10.0.0.3‘)

h22 =self.addHost( ‘h22‘,ip= ‘10.0.0.4‘)

s21 =self.addSwitch( ‘s21‘ )

s22 =self.addSwitch( ‘s22‘ )

s20 =self.addSwitch( ‘s20‘ )

# addlinks

self.addLink(h21,s21)

self.addLink(h22,s22)

self.addLink(s22,s20)

self.addLink(s20,s21)

topos = { ‘mytopo‘: ( lambda: MyTopo() ) }

Create the virtual DC2 with “dc2.py”:

[email protected]:~$sudo mn --custom dc2.py --topo mytopo

Configure OVS to listen for the connection initiated by the OVSDB Southbound Plugin of OpenDaylight:

mininet>sh ovs-vsctlset-manager ptcp:6640

4 Python Programming to MakeAPI Calls

Install Geany IDE and the Requests Library in Python:

[email protected]:~$sudo apt-getinstall geany

[email protected]:~$geany

[email protected]:~ $pip installrequests

Set Build Commands of Geany IDE:

技術分享


Configure the OVSDB Southbound Plugin in OpenDaylight toactively connect to DC1 and DC2:

import requests

import json

header = {‘content-type‘: ‘application/json‘}

commamd10 = {"network-topology:node": [{

"node-id":"ovsdb://DC1",

"connection-info":

{

"ovsdb:remote-port":"6640", "ovsdb:remote-ip": "192.168.100.144" }}]}

commamd20 = {"network-topology:node": [{

"node-id":"ovsdb://DC2",

"connection-info":

{

"ovsdb:remote-port":"6640", "ovsdb:remote-ip": "192.168.100.145" }}]}


# Making the API Call on OpenDaylight toconnect to DC1

r1 = requests.put(url=‘http://192.168.100.129:8181/restconf/config/network-topology:network-topology/topology/ovsdb:1/node/ovsdb:%2F%2FDC1‘,

data=json.dumps(commamd10),

headers=header,

auth=(‘admin‘,‘admin‘)

)

# Making the API Call on OpenDaylight to connect to DC2

r2 = requests.put(url=‘http://192.168.100.129:8181/restconf/config/network-topology:network-topology/topology/ovsdb:1/node/ovsdb:%2F%2FDC2‘,

data=json.dumps(commamd20),

headers=header,

auth=(‘admin‘,‘admin‘)

)

Configure and manage the vxlan ports of DC1 and DC2:


import requests

import json

header = {‘content-type‘: ‘application/json‘}

commamd11 = { "termination-point":

[{

"tp-id":"vxlanport",

"ovsdb:name":"vxlanport",

"ovsdb:interface-type":"ovsdb:interface-type-vxlan",

"ovsdb:options":

[{

"option":"remote_ip", "value": "192.168.100.145"

}]}]}

commamd21 = { "termination-point":

[{

"tp-id":"vxlanport",

"ovsdb:name":"vxlanport",

"ovsdb:interface-type":"ovsdb:interface-type-vxlan",

"ovsdb:options":

[{

"option":"remote_ip", "value": "192.168.100.144"

}]}]}


# Making the API Call on OpenDaylight to create the vxlan port on s10 of DC1

r1 = requests.put(url=‘http://192.168.100.129:8181/restconf/config/network-topology:network-topology/topology/ovsdb:1/node/ovsdb:%2F%2FDC1%2Fbridge%2Fs10/termination-point/vxlanport‘,

data=json.dumps(commamd11),

headers=header,

auth=(‘admin‘,‘admin‘)

)

# Making the API Call on OpenDaylight to create the vxlan port on s20 of DC2

r2 = requests.put(url=‘http://192.168.100.129:8181/restconf/config/network-topology:network-topology/topology/ovsdb:1/node/ovsdb:%2F%2FDC1%2Fbridge%2Fs20/termination-point/vxlanport‘,

data=json.dumps(commamd21),

headers=header,

auth=(‘admin‘,‘admin‘)

)

5 Check the Network

Check DC1

mininet>sh netstat –an | grep 6640

mininet>sh ovs-vsctl show

技術分享

技術分享

Check DC2

mininet>sh netstat –an | grep 6640

mininet>sh ovs-vsctl show

技術分享

技術分享


Read the network topology from OpenDaylight by Python making API calls:

import requests

import json

header = {‘content-type‘: ‘application/json‘}

r = requests.get( url=‘http://192.168.100.129:8181/restconf/operational/network-topology:network-topology‘,

headers=header,

auth=(‘admin‘,‘admin‘)

)

print(r.status_code)

print(r.headers[‘content-type‘])

print(r.url)

rj = r.json()

print(‘\n‘+rj[‘network-topology‘][‘topology‘][0][‘topology-id‘])

print(‘\n‘+rj[‘network-topology‘][‘topology‘][1][‘topology-id‘])

for node inrj[‘network-topology‘][‘topology‘][0][‘node‘]:

print(‘\n‘+node[‘node-id‘])

if‘termination-point‘ in node:

forport in node[‘termination-point‘]:

print(‘----‘+ port[‘tp-id‘])


技術分享

Make the API calls by Yangman:

技術分享











SDN實戰:Build a VXLAN Tunnel by Making Python-based API Calls for DCI