fixing pep8 problems for test verify tox job
[integration/test.git] / tools / odl-ovsdb-performance-tests / ovsdbconfigblaster.py
1 """
2 Script to add bridges/ports/termination points to ovsdb config
3 """
4 import argparse
5 import logging
6 import requests
7
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"
13 __version__ = "0.0.1"
14
15
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='}
24     TIMEOUT = 10
25
26     def __init__(self,
27                  controller_ip,
28                  controller_port,
29                  vswitch_ip,
30                  vswitch_ovsdb_port,
31                  vswitch_remote_ip,
32                  vswitch_remote_ovsdb_port,
33                  vswitch_port_type,
34                  vswitch_lst_del_br,
35                  delete_ports,
36                  num_instances):
37         """
38         Args:
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
49         """
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(
56             vswitch_ip,
57             vswitch_remote_ip,
58             vswitch_ovsdb_port, 'ovs-1')
59         if vswitch_remote_ip:
60             self.add_vswitch_to_dict(
61                 vswitch_remote_ip,
62                 vswitch_ip,
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'])
71
72     @staticmethod
73     def return_ovsdb_url(vswitch_ip, vswitch_ovsdb_port, url_type="config"):
74         """ Return an ovsdb restconf url
75         Args:
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'
79         """
80         url_prefix = None
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' \
89             + vswitch_ip\
90             + ':' \
91             + vswitch_ovsdb_port
92         return ovsdb_url
93
94     def add_vswitch_to_dict(self, vswitch_ip, vswitch_remote_ip, vswitch_ovsdb_port, vswitch_name):
95
96         """ Add details of an Open vSwitch instance to self.vswitch_dict
97         Args:
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
102         """
103         urlprefix = 'http://' \
104                     + self.controller_ip + \
105                     ':' \
106                     + self.controller_port + \
107                     '/'
108         self.vswitch_dict.update({
109             vswitch_name: {
110                 'name': vswitch_name,
111                 'ip': vswitch_ip,
112                 'remote-ip': vswitch_remote_ip,
113                 'ovsdb-port': vswitch_ovsdb_port,
114                 'node-id': 'ovsdb://%s:%s'
115                            % (vswitch_ip,
116                               vswitch_ovsdb_port),
117                 'post-url': urlprefix +
118                 OvsdbConfigBlaster.return_ovsdb_url(
119                     vswitch_ip,
120                     vswitch_ovsdb_port),
121                 'get-config-url': urlprefix +
122                 OvsdbConfigBlaster.return_ovsdb_url(
123                     vswitch_ip,
124                     vswitch_ovsdb_port),
125                 'get-oper-url': urlprefix +
126                 OvsdbConfigBlaster.return_ovsdb_url(
127                     vswitch_ip,
128                     vswitch_ovsdb_port)}})
129
130     def connect_vswitch(self, vswitch_dict):
131         """ Connect ODL to an Open vSwitch instance using restconf
132         Args:
133             :param vswitch_dict: A dictionary detailing
134                                  an instance of Open vSwitch
135         """
136         connect_ovs_body = {
137             u'network-topology:node': [
138                 {
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'])
143                     }
144                 }
145             ]
146         }
147         self.send_rest(self.session,
148                        vswitch_dict['post-url'],
149                        connect_ovs_body)
150
151     def add_bridge(self, num_instances, vswitch_name='ovs-1'):
152         """Add num_instances of bridge to ODL config
153         Args:
154             :param num_instances: Number of bridges to create
155             :param vswitch_name: A name describing
156                                  an instance of Open vSwitch
157         """
158
159         for i in range(num_instances):
160             bridge_name = unicode('br-' + str(i) + '-test')
161             add_bridge_body = {
162                 u"network-topology:node": [
163                     {
164                         u"node-id": u"%s/bridge/%s"
165                                     % (unicode(self.vswitch_dict[vswitch_name]
166                                                .get('node-id')),
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": [
171                             {
172                                 u"protocol":
173                                     u"ovsdb:ovsdb-bridge-protocol-openflow-13"
174                             }],
175                         u"ovsdb:controller-entry": [
176                             {
177                                 u"target": u"tcp:%s:%s" % (self.controller_ip, self.controller_port)
178                             }],
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="
184                                              u"'%s']"
185                                              % unicode(self.vswitch_dict[vswitch_name]
186                                                            .get('node-id'))
187                     }
188                 ]
189             }
190             self.send_rest(self.session,
191                            self.vswitch_dict[vswitch_name]
192                            .get('post-url') +
193                            '%2Fbridge%2F' +
194                            bridge_name,
195                            add_bridge_body)
196         self.session.close()
197
198     def add_port(self, port_type="ovsdb:interface-type-vxlan"):
199         """Add self.num_instances of port to ODL config
200         Args:
201             :param port_type: The type of port to create
202                                 default: 'ovsdb:interface-type-vxlan'
203         """
204         bridge_name = 'br-0-test'
205         self.add_bridge(1, 'ovs-1')
206 #        self.add_bridge(1, 'ovs-2')
207
208         for instance in range(self.num_instances):
209             for vswitch in self.vswitch_dict.itervalues():
210                 if port_type == "ovsdb:interface-type-vxlan":
211                     port_prefix = "tp-"
212                     ovsdb_rest_url = vswitch.get('post-url') \
213                         + '%2Fbridge%2F'\
214                         + bridge_name\
215                         + '/termination-point/'
216                     body_name = 'tp-body'
217                 else:
218                     port_prefix = "port-"
219                     ovsdb_rest_url = vswitch.get('post-url') \
220                         + '%2Fbridge%2F' \
221                         + bridge_name\
222                         + '/port/'
223                     body_name = 'port-body'
224                 port_name = port_prefix + str(instance) + '-test-' + vswitch.get('ip')
225                 body = {'tp-body': {
226                     u"network-topology:termination-point": [
227                         {
228                             u"ovsdb:options": [
229                                 {
230                                     u"ovsdb:option": u"remote_ip",
231                                     u"ovsdb:value": unicode(vswitch.get('remote-ip'))
232                                 }
233                             ],
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),
238                             u"trunks": [
239                                 {
240                                     u"trunk": u"5"
241                                 }
242                             ],
243                             u"vlan-mode": u"access"
244                         }
245                     ]
246                 },
247                     # TODO add port-body
248                     'port-body': {}}
249                 self.send_rest(self.session,
250                                ovsdb_rest_url + port_name,
251                                body.get(body_name))
252
253         self.session.close()
254
255     def delete_bridge(self, vswitch_lst_del_br, num_bridges):
256         """Delete num_instances of bridge in ODL config
257         Args:
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.
260         """
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]
266                                    .get('post-url') +
267                                    '%2Fbridge%2F' +
268                                    bridge_name)
269             self.session.close()
270
271     def delete_port(self, num_ports):
272         """Delete ports from ODL config
273          Args:
274             :param num_ports: Number of ports to delete
275         """
276         for port in range(num_ports):
277             bridge_name = 'br-0-test'
278             for vswitch in self.vswitch_dict.itervalues():
279                 port_prefix = "tp-"
280                 ovsdb_rest_url = vswitch.get('post-url') \
281                     + '%2Fbridge%2F' \
282                     + bridge_name \
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)
287                 self.session.close()
288
289     def send_rest_del(self, session, rest_url):
290         """Send an HTTP DELETE to the Rest URL and return the status code
291         Args:
292             :param session: The HTTP session handle
293             :return int: status_code - HTTP status code
294         """
295         ret = session.delete(rest_url,
296                              headers=self.DELETE_HEADERS,
297                              stream=False,
298                              timeout=self.TIMEOUT)
299
300         if ret.status_code is not 200:
301             raise ValueError(ret.text,
302                              ret.status_code,
303                              rest_url)
304         return ret.status_code
305
306     def send_rest(self, session, rest_url, json_body):
307         """Send an HTTP PUT to the Rest URL and return the status code
308         Args:
309             :param session: The HTTP session handle
310             :param json_body: the JSON body to be sent
311         Returns:
312             :return int: status_code - HTTP status code
313         """
314         ret = session.put(rest_url,
315                           json=json_body,
316                           headers=self.PUT_HEADERS,
317                           stream=False,
318                           timeout=self.TIMEOUT)
319
320         if ret.status_code is not 200:
321             raise ValueError(ret.text,
322                              ret.status_code,
323                              rest_url,
324                              json_body)
325         return ret.status_code
326
327
328 if __name__ == "__main__":
329     parser = argparse.ArgumentParser(description='Add:delete bridge/port/term-points to OpenDaylight')
330
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 \
339                             (default is 8181)')
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 \
345                             (default is 6640)')
346     parser.add_argument('--vswitchremote', default=None,
347                         help='IP of remote Open vSwitch \
348                             (default is none)')
349     parser.add_argument('--vswitchremoteport', default=None,
350                         help='Port of remote Open vSwitch OVSDB server \
351                             (default is none)')
352     parser.add_argument('--vswitchporttype', default=None,
353                         help='Port of remote Open vSwitch OVSDB server \
354                             (default is none)')
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" \
359                             (default is none)')
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)')
364
365     args = parser.parse_args()
366
367     ovsdb_config_blaster = OvsdbConfigBlaster(args.controller,
368                                               args.controllerport,
369                                               args.vswitch,
370                                               args.vswitchport,
371                                               args.vswitchremote,
372                                               args.vswitchremoteport,
373                                               args.vswitchporttype,
374                                               args.deletebridges,
375                                               args.deleteports,
376                                               args.instances)
377     if args.mode == "bridge":
378         if args.deletebridges is not None:
379             ovsdb_config_blaster.delete_bridge(ovsdb_config_blaster.
380                                                vswitch_lst_del_br,
381                                                ovsdb_config_blaster.
382                                                num_instances)
383         else:
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)
388         else:
389             ovsdb_config_blaster.add_port()
390     else:
391         print "please use: python ovsdbconfigblaster.py --help " \
392               "\nUnsupported mode: ", args.mode