2 Script to add bridges/ports/termination points to ovsdb config
8 __author__ = 'Marcus Williams'
9 __copyright__ = "Copyright (c) 2015, Intel Corp Inc., Cisco Systems Inc. and others"
10 __credits__ = ["Jan Medved, Lori Jakab"]
11 __license__ = "New-style BSD"
12 __email__ = "marcus.williams@intel.com"
16 class OvsdbConfigBlaster (object):
17 PUT_HEADERS = {'Content-Type': 'application/json',
18 'Authorization': 'Basic YWRtaW46YWRtaW4=',
19 'Accept': 'application/json'}
20 GET_HEADERS = {'Accept': 'application/json',
21 'Authorization': 'Basic YWRtaW46YWRtaW4='}
22 DELETE_HEADERS = {'Accept': 'application/json',
23 'Authorization': 'Basic YWRtaW46YWRtaW4='}
32 vswitch_remote_ovsdb_port,
39 :param controller_ip: The ODL host ip used to send RPCs
40 :param controller_port: The RESTCONF port on the ODL host
41 :param vswitch_ip: The ip of OpenvSwitch to use
42 :param vswitch_ovsdb_port: The ovsdb port of OpenvSwitch to use
43 :param vswitch_remote_ip: The ip of remote OpenvSwitch to use
44 :param vswitch_remote_ovsdb_port: The ovsdb port of remote OpenvSwitch to use
45 :param vswitch_port_type: Port type to create
46 :param vswitch_lst_del_br: string containing a list of ovs switches on which BR'S should be deleted.
47 :param num_instances: The number of instances (bridges, ports etc)to be added
48 :param delete_ports: The number of ports to be deleted from a bridge
50 logging.basicConfig(level=logging.DEBUG)
51 self.session = requests.Session()
52 self.controller_ip = controller_ip
53 self.controller_port = controller_port
54 self.vswitch_dict = dict()
55 self.add_vswitch_to_dict(
58 vswitch_ovsdb_port, 'ovs-1')
60 self.add_vswitch_to_dict(
63 vswitch_remote_ovsdb_port, 'ovs-2')
64 self.vswitch_port_type = vswitch_port_type
65 self.vswitch_lst_del_br = vswitch_lst_del_br
66 self.num_instances = num_instances
67 self.delete_ports = delete_ports
68 self.connect_vswitch(self.vswitch_dict['ovs-1'])
69 if self.vswitch_dict.get('ovs-2'):
70 self.connect_vswitch(self.vswitch_dict['ovs-2'])
73 def return_ovsdb_url(vswitch_ip, vswitch_ovsdb_port, url_type="config"):
74 """ Return an ovsdb restconf url
76 :param vswitch_ip: The ip of Open vSwitch to use
77 :param vswitch_ovsdb_port: The ovsdb port of Open vSwitch to use
78 :param url_tyep: The type of url 'config' | 'oper'
81 if url_type == "config":
82 url_prefix = 'restconf/config/'
83 elif url_type == "oper":
84 url_prefix = 'restconf/operational/'
85 ovsdb_url = url_prefix \
86 + 'network-topology:' \
87 'network-topology/topology/' \
88 'ovsdb:1/node/ovsdb:%2F%2F' \
94 def add_vswitch_to_dict(self, vswitch_ip, vswitch_remote_ip, vswitch_ovsdb_port, vswitch_name):
96 """ Add details of an Open vSwitch instance to self.vswitch_dict
98 :param vswitch_ip: The ip of Open vSwitch to use
99 :param vswitch_remote_ip: The ip of remote Open vSwitch to use
100 :param vswitch_ovsdb_port: The ovsdb port of Open vSwitch to use
101 :param vswitch_name: The name to label the added Open vSwitch instance
103 urlprefix = 'http://' \
104 + self.controller_ip + \
106 + self.controller_port + \
108 self.vswitch_dict.update({
110 'name': vswitch_name,
112 'remote-ip': vswitch_remote_ip,
113 'ovsdb-port': vswitch_ovsdb_port,
114 'node-id': 'ovsdb://%s:%s'
117 'post-url': urlprefix +
118 OvsdbConfigBlaster.return_ovsdb_url(
121 'get-config-url': urlprefix +
122 OvsdbConfigBlaster.return_ovsdb_url(
125 'get-oper-url': urlprefix +
126 OvsdbConfigBlaster.return_ovsdb_url(
128 vswitch_ovsdb_port)}})
130 def connect_vswitch(self, vswitch_dict):
131 """ Connect ODL to an Open vSwitch instance using restconf
133 :param vswitch_dict: A dictionary detailing
134 an instance of Open vSwitch
137 u'network-topology:node': [
139 u'node-id': unicode(vswitch_dict['node-id']),
140 u'connection-info': {
141 u'ovsdb:remote-port': unicode(vswitch_dict['ovsdb-port']),
142 u'ovsdb:remote-ip': unicode(vswitch_dict['ip'])
147 self.send_rest(self.session,
148 vswitch_dict['post-url'],
151 def add_bridge(self, num_instances, vswitch_name='ovs-1'):
152 """Add num_instances of bridge to ODL config
154 :param num_instances: Number of bridges to create
155 :param vswitch_name: A name describing
156 an instance of Open vSwitch
159 for i in range(num_instances):
160 bridge_name = unicode('br-' + str(i) + '-test')
162 u"network-topology:node": [
164 u"node-id": u"%s/bridge/%s"
165 % (unicode(self.vswitch_dict[vswitch_name]
167 unicode(bridge_name)),
168 u"ovsdb:bridge-name": unicode(bridge_name),
169 u"ovsdb:datapath-id": u"00:00:b2:bf:48:25:f2:4b",
170 u"ovsdb:protocol-entry": [
173 u"ovsdb:ovsdb-bridge-protocol-openflow-13"
175 u"ovsdb:controller-entry": [
177 u"target": u"tcp:%s:%s" % (self.controller_ip, self.controller_port)
179 u"ovsdb:managed-by": u"/network-topology:network-topology/"
180 u"network-topology:topology"
181 u"[network-topology:topology-id"
182 u"='ovsdb:1']/network-topology:node"
183 u"[network-topology:node-id="
185 % unicode(self.vswitch_dict[vswitch_name]
190 self.send_rest(self.session,
191 self.vswitch_dict[vswitch_name]
198 def add_port(self, port_type="ovsdb:interface-type-vxlan"):
199 """Add self.num_instances of port to ODL config
201 :param port_type: The type of port to create
202 default: 'ovsdb:interface-type-vxlan'
204 bridge_name = 'br-0-test'
205 self.add_bridge(1, 'ovs-1')
206 # self.add_bridge(1, 'ovs-2')
208 for instance in range(self.num_instances):
209 for vswitch in self.vswitch_dict.itervalues():
210 if port_type == "ovsdb:interface-type-vxlan":
212 ovsdb_rest_url = vswitch.get('post-url') \
215 + '/termination-point/'
216 body_name = 'tp-body'
218 port_prefix = "port-"
219 ovsdb_rest_url = vswitch.get('post-url') \
223 body_name = 'port-body'
224 port_name = port_prefix + str(instance) + '-test-' + vswitch.get('ip')
226 u"network-topology:termination-point": [
230 u"ovsdb:option": u"remote_ip",
231 u"ovsdb:value": unicode(vswitch.get('remote-ip'))
234 u"ovsdb:name": unicode(port_name),
235 u"ovsdb:interface-type": unicode(port_type),
236 u"tp-id": unicode(port_name),
237 u"vlan-tag": unicode(instance + 1),
243 u"vlan-mode": u"access"
249 self.send_rest(self.session,
250 ovsdb_rest_url + port_name,
255 def delete_bridge(self, vswitch_lst_del_br, num_bridges):
256 """Delete num_instances of bridge in ODL config
258 :param num_bridges: Number of bridges to delete
259 :param vswitch_lst_del_br: A list containing instances of Open vSwitch on which bridges should be deleted.
261 for vswitch_names in vswitch_lst_del_br:
262 for br_num in range(num_bridges):
263 bridge_name = unicode('br-' + str(br_num) + '-test')
264 self.send_rest_del(self.session,
265 self.vswitch_dict[vswitch_names]
271 def delete_port(self, num_ports):
272 """Delete ports from ODL config
274 :param num_ports: Number of ports to delete
276 for port in range(num_ports):
277 bridge_name = 'br-0-test'
278 for vswitch in self.vswitch_dict.itervalues():
280 ovsdb_rest_url = vswitch.get('post-url') \
283 + '/termination-point/'
284 port_name = port_prefix + str(port) + '-test-' + vswitch.get('ip')
285 self.send_rest_del(self.session,
286 ovsdb_rest_url + port_name)
289 def send_rest_del(self, session, rest_url):
290 """Send an HTTP DELETE to the Rest URL and return the status code
292 :param session: The HTTP session handle
293 :return int: status_code - HTTP status code
295 ret = session.delete(rest_url,
296 headers=self.DELETE_HEADERS,
298 timeout=self.TIMEOUT)
300 if ret.status_code is not 200:
301 raise ValueError(ret.text,
304 return ret.status_code
306 def send_rest(self, session, rest_url, json_body):
307 """Send an HTTP PUT to the Rest URL and return the status code
309 :param session: The HTTP session handle
310 :param json_body: the JSON body to be sent
312 :return int: status_code - HTTP status code
314 ret = session.put(rest_url,
316 headers=self.PUT_HEADERS,
318 timeout=self.TIMEOUT)
320 if ret.status_code is not 200:
321 raise ValueError(ret.text,
325 return ret.status_code
328 if __name__ == "__main__":
329 parser = argparse.ArgumentParser(description='Add:delete bridge/port/term-points to OpenDaylight')
331 parser.add_argument('--mode', default='None',
332 help='Operating mode, can be "bridge", "port" or "term" \
333 (default is "bridge")')
334 parser.add_argument('--controller', default='127.0.0.1',
335 help='IP of running ODL controller \
336 (default is 127.0.0.1)')
337 parser.add_argument('--controllerport', default='8181',
338 help='Port of ODL RESTCONF \
340 parser.add_argument('--vswitch', default='127.0.0.1',
341 help='IP of Open vSwitch \
342 (default is 127.0.0.1)')
343 parser.add_argument('--vswitchport', default='6640',
344 help='Port of Open vSwitch OVSDB server \
346 parser.add_argument('--vswitchremote', default=None,
347 help='IP of remote Open vSwitch \
349 parser.add_argument('--vswitchremoteport', default=None,
350 help='Port of remote Open vSwitch OVSDB server \
352 parser.add_argument('--vswitchporttype', default=None,
353 help='Port of remote Open vSwitch OVSDB server \
355 parser.add_argument('--deletebridges', nargs='*', type=str, default=None,
356 help='A list of switches on which to delete bridges, '
357 'uses instances for number of bridges. \
358 Example: "ovs-1 ovs2" \
360 parser.add_argument('--deleteports', type=int, default=1,
361 help='delete ports of remote open vswitch ovsdb server (default 1)')
362 parser.add_argument('--instances', type=int, default=1,
363 help='Number of instances to add/get (default 1)')
365 args = parser.parse_args()
367 ovsdb_config_blaster = OvsdbConfigBlaster(args.controller,
372 args.vswitchremoteport,
373 args.vswitchporttype,
377 if args.mode == "bridge":
378 if args.deletebridges is not None:
379 ovsdb_config_blaster.delete_bridge(ovsdb_config_blaster.
381 ovsdb_config_blaster.
384 ovsdb_config_blaster.add_bridge(ovsdb_config_blaster.num_instances)
385 elif args.mode == "term":
386 if args.deleteports is not None:
387 ovsdb_config_blaster.delete_port(ovsdb_config_blaster.delete_ports)
389 ovsdb_config_blaster.add_port()
391 print "please use: python ovsdbconfigblaster.py --help " \
392 "\nUnsupported mode: ", args.mode