blob: 84c4b7f555016e0fad19a4145ebd5489670f8038 [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
3'''
4This driver enters the onos> prompt to issue commands.
5
6Please follow the coding style demonstrated by existing
7functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040014shreya@onlab.us
andrewonlab95ce8322014-10-13 14:12:04 -040015
16OCT 13 2014
17
18'''
19
20import sys
21import time
22import pexpect
23import re
24import traceback
25import os.path
26import pydoc
Jon Halla001c392014-10-17 18:50:59 -040027import re
andrewonlab95ce8322014-10-13 14:12:04 -040028sys.path.append("../")
29from drivers.common.clidriver import CLI
30
31class OnosCliDriver(CLI):
32
33 def __init__(self):
34 '''
35 Initialize client
36 '''
37 super(CLI, self).__init__()
38
39 def connect(self,**connectargs):
40 '''
41 Creates ssh handle for ONOS cli.
42 '''
43 try:
44 for key in connectargs:
45 vars(self)[key] = connectargs[key]
46 self.home = "~/ONOS"
47 for key in self.options:
48 if key == "home":
49 self.home = self.options['home']
50 break
51
52
53 self.name = self.options['name']
54 self.handle = super(OnosCliDriver,self).connect(
55 user_name = self.user_name,
56 ip_address = self.ip_address,
57 port = self.port,
58 pwd = self.pwd,
59 home = self.home)
60
61 self.handle.sendline("cd "+ self.home)
62 self.handle.expect("\$")
63 if self.handle:
64 return self.handle
65 else :
66 main.log.info("NO ONOS HANDLE")
67 return main.FALSE
68 except pexpect.EOF:
69 main.log.error(self.name + ": EOF exception found")
70 main.log.error(self.name + ": " + self.handle.before)
71 main.cleanup()
72 main.exit()
73 except:
andrewonlab9627f432014-11-14 12:45:10 -050074 main.log.info(self.name + ":::::::::::::::::::::::")
andrewonlab95ce8322014-10-13 14:12:04 -040075 main.log.error( traceback.print_exc() )
andrewonlab9627f432014-11-14 12:45:10 -050076 main.log.info(":::::::::::::::::::::::")
andrewonlab95ce8322014-10-13 14:12:04 -040077 main.cleanup()
78 main.exit()
79
80 def disconnect(self):
81 '''
82 Called when Test is complete to disconnect the ONOS handle.
83 '''
84 response = ''
85 try:
andrewonlab2a6c9342014-10-16 13:40:15 -040086 self.handle.sendline("")
Jon Hall7e5b9172014-10-22 12:32:47 -040087 i = self.handle.expect(["onos>","\$"])
88 if i == 0:
89 self.handle.sendline("system:shutdown")
90 self.handle.expect("Confirm")
91 self.handle.sendline("yes")
92 self.handle.expect("\$")
Jon Hallffb386d2014-11-21 13:43:38 -080093 self.handle.sendline("")
andrewonlabc2d05aa2014-10-13 16:51:10 -040094 self.handle.expect("\$")
Jon Hall7e5b9172014-10-22 12:32:47 -040095 self.handle.sendline("exit")
96 self.handle.expect("closed")
andrewonlabc2d05aa2014-10-13 16:51:10 -040097
andrewonlab95ce8322014-10-13 14:12:04 -040098 except pexpect.EOF:
99 main.log.error(self.name + ": EOF exception found")
100 main.log.error(self.name + ": " + self.handle.before)
101 except:
102 main.log.error(self.name + ": Connection failed to the host")
103 response = main.FALSE
104 return response
105
andrewonlab38d2b4a2014-11-13 16:28:47 -0500106 def logout(self):
107 '''
108 Sends 'logout' command to ONOS cli
109 '''
110 try:
andrewonlab9627f432014-11-14 12:45:10 -0500111 self.handle.sendline("")
112 i = self.handle.expect([
113 "onos>",
114 "\$"], timeout=10)
115 if i == 0:
116 self.handle.sendline("logout")
117 self.handle.expect("\$")
118 elif i == 1:
119 return main.TRUE
120
andrewonlab38d2b4a2014-11-13 16:28:47 -0500121 except pexpect.EOF:
122 main.log.error(self.name + ": eof exception found")
andrewonlab9627f432014-11-14 12:45:10 -0500123 main.log.error(self.name + ": " +
124 self.handle.before)
andrewonlab38d2b4a2014-11-13 16:28:47 -0500125 main.cleanup()
126 main.exit()
127 except:
128 main.log.info(self.name+" ::::::")
129 main.log.error( traceback.print_exc())
130 main.log.info(self.name+" ::::::")
131 main.cleanup()
132 main.exit()
133
andrewonlab95ce8322014-10-13 14:12:04 -0400134 def set_cell(self, cellname):
135 '''
136 Calls 'cell <name>' to set the environment variables on ONOSbench
137
138 Before issuing any cli commands, set the environment variable first.
139 '''
140 try:
141 if not cellname:
142 main.log.error("Must define cellname")
143 main.cleanup()
144 main.exit()
145 else:
146 self.handle.sendline("cell "+str(cellname))
147 #Expect the cellname in the ONOS_CELL variable.
148 #Note that this variable name is subject to change
149 # and that this driver will have to change accordingly
150 self.handle.expect("ONOS_CELL="+str(cellname))
151 handle_before = self.handle.before
152 handle_after = self.handle.after
153 #Get the rest of the handle
154 self.handle.sendline("")
155 self.handle.expect("\$")
156 handle_more = self.handle.before
157
158 main.log.info("Cell call returned: "+handle_before+
159 handle_after + handle_more)
160
161 return main.TRUE
162
163 except pexpect.EOF:
andrewonlab38d2b4a2014-11-13 16:28:47 -0500164 main.log.error(self.name + ": eof exception found")
andrewonlab95ce8322014-10-13 14:12:04 -0400165 main.log.error(self.name + ": " + self.handle.before)
166 main.cleanup()
167 main.exit()
168 except:
169 main.log.info(self.name+" ::::::")
170 main.log.error( traceback.print_exc())
171 main.log.info(self.name+" ::::::")
172 main.cleanup()
173 main.exit()
174
andrewonlabc2d05aa2014-10-13 16:51:10 -0400175 def start_onos_cli(self, ONOS_ip):
andrewonlab95ce8322014-10-13 14:12:04 -0400176 try:
177 self.handle.sendline("")
andrewonlab48829f62014-11-17 13:49:01 -0500178 x = self.handle.expect([
179 "\$", "onos>"], timeout=10)
180
181 if x == 1:
182 main.log.info("ONOS cli is already running")
183 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400184
185 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400186 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400187 i = self.handle.expect([
188 "onos>",
189 pexpect.TIMEOUT],timeout=60)
190
191 if i == 0:
192 main.log.info(str(ONOS_ip)+" CLI Started successfully")
193 return main.TRUE
194 else:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400195 #If failed, send ctrl+c to process and try again
andrewonlabf47993a2014-10-24 17:56:01 -0400196 main.log.info("Starting CLI failed. Retrying...")
Jon Hallffb386d2014-11-21 13:43:38 -0800197 self.handle.send("\x03")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400198 self.handle.sendline("onos -w "+str(ONOS_ip))
199 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
200 timeout=30)
201 if i == 0:
andrewonlab28ca4b22014-11-20 13:15:59 -0500202 main.log.info(str(ONOS_ip)+" CLI Started "+
203 "successfully after retry attempt")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400204 return main.TRUE
205 else:
206 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400207 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400208 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400209
210 except pexpect.EOF:
211 main.log.error(self.name + ": EOF exception found")
212 main.log.error(self.name + ": " + self.handle.before)
213 main.cleanup()
214 main.exit()
215 except:
216 main.log.info(self.name+" ::::::")
217 main.log.error( traceback.print_exc())
218 main.log.info(self.name+" ::::::")
219 main.cleanup()
220 main.exit()
221
andrewonlaba18f6bf2014-10-13 19:31:54 -0400222 def sendline(self, cmd_str):
223 '''
224 Send a completely user specified string to
225 the onos> prompt. Use this function if you have
226 a very specific command to send.
227
228 Warning: There are no sanity checking to commands
229 sent using this method.
230 '''
231 try:
232 self.handle.sendline("")
233 self.handle.expect("onos>")
234
235 self.handle.sendline(cmd_str)
236 self.handle.expect("onos>")
237
238 handle = self.handle.before
239
240 self.handle.sendline("")
241 self.handle.expect("onos>")
242
243 handle += self.handle.before
244 handle += self.handle.after
245
246 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400247 ansi_escape = re.compile(r'\x1b[^m]*m')
248 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400249
250 return handle
251 except pexpect.EOF:
252 main.log.error(self.name + ": EOF exception found")
253 main.log.error(self.name + ": " + self.handle.before)
254 main.cleanup()
255 main.exit()
256 except:
257 main.log.info(self.name+" ::::::")
258 main.log.error( traceback.print_exc())
259 main.log.info(self.name+" ::::::")
260 main.cleanup()
261 main.exit()
262
andrewonlab95ce8322014-10-13 14:12:04 -0400263 #IMPORTANT NOTE:
264 #For all cli commands, naming convention should match
265 #the cli command replacing ':' with '_'.
266 #Ex) onos:topology > onos_topology
267 # onos:links > onos_links
268 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400269
270 def add_node(self, node_id, ONOS_ip, tcp_port=""):
271 '''
272 Adds a new cluster node by ID and address information.
273 Required:
274 * node_id
275 * ONOS_ip
276 Optional:
277 * tcp_port
278 '''
279 try:
280 self.handle.sendline("")
281 self.handle.expect("onos>")
282
283 self.handle.sendline("add-node "+
284 str(node_id)+" "+
285 str(ONOS_ip)+" "+
286 str(tcp_port))
287
288 i = self.handle.expect([
289 "Error",
290 "onos>" ])
291
292 #Clear handle to get previous output
293 self.handle.sendline("")
294 self.handle.expect("onos>")
295
296 handle = self.handle.before
297
298 if i == 0:
299 main.log.error("Error in adding node")
300 main.log.error(handle)
301 return main.FALSE
302 else:
303 main.log.info("Node "+str(ONOS_ip)+" added")
304 return main.TRUE
305
306 except pexpect.EOF:
307 main.log.error(self.name + ": EOF exception found")
308 main.log.error(self.name + ": " + self.handle.before)
309 main.cleanup()
310 main.exit()
311 except:
312 main.log.info(self.name+" ::::::")
313 main.log.error( traceback.print_exc())
314 main.log.info(self.name+" ::::::")
315 main.cleanup()
316 main.exit()
317
andrewonlab86dc3082014-10-13 18:18:38 -0400318 def remove_node(self, node_id):
319 '''
320 Removes a cluster by ID
321 Issues command: 'remove-node [<node-id>]'
322 Required:
323 * node_id
324 '''
325 try:
326 self.handle.sendline("")
327 self.handle.expect("onos>")
328
329 self.handle.sendline("remove-node "+str(node_id))
330 self.handle.expect("onos>")
331
332 return main.TRUE
333
334 except pexpect.EOF:
335 main.log.error(self.name + ": EOF exception found")
336 main.log.error(self.name + ": " + self.handle.before)
337 main.cleanup()
338 main.exit()
339 except:
340 main.log.info(self.name+" ::::::")
341 main.log.error( traceback.print_exc())
342 main.log.info(self.name+" ::::::")
343 main.cleanup()
344 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400345
andrewonlab7c211572014-10-15 16:45:20 -0400346 def nodes(self):
347 '''
348 List the nodes currently visible
349 Issues command: 'nodes'
350 Returns: entire handle of list of nodes
351 '''
352 try:
353 self.handle.sendline("")
354 self.handle.expect("onos>")
355
356 self.handle.sendline("nodes")
357 self.handle.expect("onos>")
358
359 self.handle.sendline("")
360 self.handle.expect("onos>")
361
362 handle = self.handle.before
363
364 return handle
365
366 except pexpect.EOF:
367 main.log.error(self.name + ": EOF exception found")
368 main.log.error(self.name + ": " + self.handle.before)
369 main.cleanup()
370 main.exit()
371 except:
372 main.log.info(self.name+" ::::::")
373 main.log.error( traceback.print_exc())
374 main.log.info(self.name+" ::::::")
375 main.cleanup()
376 main.exit()
377
andrewonlab38d6ae22014-10-15 14:23:45 -0400378 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400379 '''
380 Shows the current state of the topology
381 by issusing command: 'onos> onos:topology'
382 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400383 try:
384 self.handle.sendline("")
385 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400386 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400387 self.handle.sendline("onos:topology")
388 self.handle.expect("onos>")
389
390 handle = self.handle.before
391
392 main.log.info("onos:topology returned: " +
393 str(handle))
394
395 return handle
396
397 except pexpect.EOF:
398 main.log.error(self.name + ": EOF exception found")
399 main.log.error(self.name + ": " + self.handle.before)
400 main.cleanup()
401 main.exit()
402 except:
403 main.log.info(self.name+" ::::::")
404 main.log.error( traceback.print_exc())
405 main.log.info(self.name+" ::::::")
406 main.cleanup()
407 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400408
409 def feature_install(self, feature_str):
410 '''
411 Installs a specified feature
412 by issuing command: 'onos> feature:install <feature_str>'
413 '''
414 try:
415 self.handle.sendline("")
416 self.handle.expect("onos>")
417
418 self.handle.sendline("feature:install "+str(feature_str))
419 self.handle.expect("onos>")
420
421 return main.TRUE
422
423 except pexpect.EOF:
424 main.log.error(self.name + ": EOF exception found")
425 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500426 main.log.report("Failed to install feature")
427 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400428 main.cleanup()
429 main.exit()
430 except:
431 main.log.info(self.name+" ::::::")
432 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500433 main.log.report("Failed to install feature")
434 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400435 main.log.info(self.name+" ::::::")
436 main.cleanup()
437 main.exit()
438
439 def feature_uninstall(self, feature_str):
440 '''
441 Uninstalls a specified feature
442 by issuing command: 'onos> feature:uninstall <feature_str>'
443 '''
444 try:
445 self.handle.sendline("")
446 self.handle.expect("onos>")
447
448 self.handle.sendline("feature:uninstall "+str(feature_str))
449 self.handle.expect("onos>")
450
451 return main.TRUE
452
453 except pexpect.EOF:
454 main.log.error(self.name + ": EOF exception found")
455 main.log.error(self.name + ": " + self.handle.before)
456 main.cleanup()
457 main.exit()
458 except:
459 main.log.info(self.name+" ::::::")
460 main.log.error( traceback.print_exc())
461 main.log.info(self.name+" ::::::")
462 main.cleanup()
463 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800464
465 def devices(self, json_format=True):
andrewonlab86dc3082014-10-13 18:18:38 -0400466 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400467 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400468 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800469 * json_format - boolean indicating if you want output in json
andrewonlab86dc3082014-10-13 18:18:38 -0400470 '''
471 try:
472 self.handle.sendline("")
473 self.handle.expect("onos>")
andrewonlabb66dfa12014-12-02 15:51:10 -0500474
Jon Halle8217482014-10-17 13:49:14 -0400475 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800476 self.handle.sendline("devices -j")
477 self.handle.expect("devices -j")
478 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400479 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400480 '''
481 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
482 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
Jon Hall5227ce82014-10-17 20:09:51 -0400483 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Hall983a1702014-10-28 18:44:22 -0400484 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400485 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400486 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400487 '''
488 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400489 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
490 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400491 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400492 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400493 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800494 self.handle.sendline("devices")
495 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400496 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400497 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400498 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400499 except pexpect.EOF:
500 main.log.error(self.name + ": EOF exception found")
501 main.log.error(self.name + ": " + self.handle.before)
502 main.cleanup()
503 main.exit()
504 except:
505 main.log.info(self.name+" ::::::")
506 main.log.error( traceback.print_exc())
507 main.log.info(self.name+" ::::::")
508 main.cleanup()
509 main.exit()
510
Jon Hallffb386d2014-11-21 13:43:38 -0800511 def links(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400512 '''
513 Lists all core links
514 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800515 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400516 '''
517 try:
518 self.handle.sendline("")
519 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800520
Jon Halle8217482014-10-17 13:49:14 -0400521 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800522 self.handle.sendline("links -j")
523 self.handle.expect("links -j")
524 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400525 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400526 '''
527 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
528 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
Jon Hall5227ce82014-10-17 20:09:51 -0400529 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Hallffb386d2014-11-21 13:43:38 -0800530 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400531 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800532 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400533 '''
534 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400535 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
536 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400537 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400538 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400539 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800540 self.handle.sendline("links")
541 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400542 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400543 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400544 return handle
Jon Halle8217482014-10-17 13:49:14 -0400545 except pexpect.EOF:
546 main.log.error(self.name + ": EOF exception found")
547 main.log.error(self.name + ": " + self.handle.before)
548 main.cleanup()
549 main.exit()
550 except:
551 main.log.info(self.name+" ::::::")
552 main.log.error( traceback.print_exc())
553 main.log.info(self.name+" ::::::")
554 main.cleanup()
555 main.exit()
556
557
Jon Hallffb386d2014-11-21 13:43:38 -0800558 def ports(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400559 '''
560 Lists all ports
561 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800562 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400563 '''
564 try:
565 self.handle.sendline("")
566 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800567
Jon Halle8217482014-10-17 13:49:14 -0400568 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800569 self.handle.sendline("ports -j")
570 self.handle.expect("ports -j")
571 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400572 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400573 '''
574 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
575 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
Jon Hall5227ce82014-10-17 20:09:51 -0400576 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Hallffb386d2014-11-21 13:43:38 -0800577 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400578 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800579 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400580 '''
581 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400582 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
583 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400584 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400585 return handle1
586
Jon Halle8217482014-10-17 13:49:14 -0400587 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800588 self.handle.sendline("ports")
589 self.handle.expect("onos>")
590 self.handle.sendline("")
591 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400592 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400593 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800594 return handle
Jon Halle8217482014-10-17 13:49:14 -0400595 except pexpect.EOF:
596 main.log.error(self.name + ": EOF exception found")
597 main.log.error(self.name + ": " + self.handle.before)
598 main.cleanup()
599 main.exit()
600 except:
601 main.log.info(self.name+" ::::::")
602 main.log.error( traceback.print_exc())
603 main.log.info(self.name+" ::::::")
604 main.cleanup()
605 main.exit()
606
607
Jon Hallffb386d2014-11-21 13:43:38 -0800608 def roles(self, json_format=True):
andrewonlab7c211572014-10-15 16:45:20 -0400609 '''
Jon Hall983a1702014-10-28 18:44:22 -0400610 Lists all devices and the controllers with roles assigned to them
611 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800612 * json_format - boolean indicating if you want output in json
andrewonlab7c211572014-10-15 16:45:20 -0400613 '''
614 try:
615 self.handle.sendline("")
616 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500617
Jon Hall983a1702014-10-28 18:44:22 -0400618 if json_format:
Jon Hallb1290e82014-11-18 16:17:48 -0500619 self.handle.sendline("roles -j")
620 self.handle.expect("roles -j")
621 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400622 handle = self.handle.before
623 '''
Jon Hallb1290e82014-11-18 16:17:48 -0500624 handle variable here contains some ANSI escape color code sequences at the
625 end which are invisible in the print command output. To make that escape
626 sequence visible, use repr() function. The repr(handle) output when printed
627 shows the ANSI escape sequences. In json.loads(somestring), this somestring
628 variable is actually repr(somestring) and json.loads would fail with the escape
629 sequence.
630
631 So we take off that escape sequence using the following commads:
Jon Hall983a1702014-10-28 18:44:22 -0400632 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallb1290e82014-11-18 16:17:48 -0500633 handle1 = ansi_escape.sub('', handle)
Jon Hall983a1702014-10-28 18:44:22 -0400634 '''
635 #print "repr(handle) =", repr(handle)
636 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
637 handle1 = ansi_escape.sub('', handle)
638 #print "repr(handle1) = ", repr(handle1)
639 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400640
andrewonlab7c211572014-10-15 16:45:20 -0400641 else:
Jon Hallb1290e82014-11-18 16:17:48 -0500642 self.handle.sendline("roles")
643 self.handle.expect("onos>")
644 self.handle.sendline("")
645 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400646 handle = self.handle.before
647 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800648 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400649 except pexpect.EOF:
650 main.log.error(self.name + ": EOF exception found")
651 main.log.error(self.name + ": " + self.handle.before)
652 main.cleanup()
653 main.exit()
654 except:
655 main.log.info(self.name+" ::::::")
656 main.log.error( traceback.print_exc())
657 main.log.info(self.name+" ::::::")
658 main.cleanup()
659 main.exit()
660
661 def get_role(self, device_id):
662 '''
663 Given the a string containing the json representation of the "roles" cli command and a
664 partial or whole device id, returns a json object containing the
665 roles output for the first device whose id contains "device_id"
666
667 Returns:
668 Dict of the role assignments for the given device or
669 None if not match
670 '''
671 try:
672 import json
673 if device_id == None:
674 return None
675 else:
676 raw_roles = self.roles()
677 roles_json = json.loads(raw_roles)
678 #search json for the device with id then return the device
679 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400680 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400681 if str(device_id) in device['id']:
682 return device
683 return None
andrewonlab7c211572014-10-15 16:45:20 -0400684
andrewonlab86dc3082014-10-13 18:18:38 -0400685 except pexpect.EOF:
686 main.log.error(self.name + ": EOF exception found")
687 main.log.error(self.name + ": " + self.handle.before)
688 main.cleanup()
689 main.exit()
690 except:
691 main.log.info(self.name+" ::::::")
692 main.log.error( traceback.print_exc())
693 main.log.info(self.name+" ::::::")
694 main.cleanup()
695 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800696
697 def roles_not_null(self):
698 '''
699 Iterates through each device and checks if there is a master assigned
700 Returns: main.TRUE if each device has a master
701 main.FALSE any device has no master
702 '''
703 try:
704 import json
705 raw_roles = self.roles()
706 roles_json = json.loads(raw_roles)
707 #search json for the device with id then return the device
708 for device in roles_json:
709 #print device
710 if device['master'] == "none":
711 main.log.warn("Device has no master: " + str(device) )
712 return main.FALSE
713 return main.TRUE
714
715 except pexpect.EOF:
716 main.log.error(self.name + ": EOF exception found")
717 main.log.error(self.name + ": " + self.handle.before)
718 main.cleanup()
719 main.exit()
720 except:
721 main.log.info(self.name+" ::::::")
722 main.log.error( traceback.print_exc())
723 main.log.info(self.name+" ::::::")
724 main.cleanup()
725 main.exit()
726
727
andrewonlab3e15ead2014-10-15 14:21:34 -0400728 def paths(self, src_id, dst_id):
729 '''
730 Returns string of paths, and the cost.
731 Issues command: onos:paths <src> <dst>
732 '''
733 try:
734 self.handle.sendline("")
735 self.handle.expect("onos>")
736
737 self.handle.sendline("onos:paths "+
738 str(src_id) + " " + str(dst_id))
739 i = self.handle.expect([
740 "Error",
741 "onos>"])
742
743 self.handle.sendline("")
744 self.handle.expect("onos>")
745
746 handle = self.handle.before
747
748 if i == 0:
749 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400750 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400751 else:
752 path = handle.split(";")[0]
753 cost = handle.split(";")[1]
754 return (path, cost)
755
756 except pexpect.EOF:
757 main.log.error(self.name + ": EOF exception found")
758 main.log.error(self.name + ": " + self.handle.before)
759 main.cleanup()
760 main.exit()
761 except:
762 main.log.info(self.name+" ::::::")
763 main.log.error( traceback.print_exc())
764 main.log.info(self.name+" ::::::")
765 main.cleanup()
766 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800767
768 def hosts(self, json_format=True):
Jon Hall42db6dc2014-10-24 19:03:48 -0400769 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800770 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400771 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800772 * json_format - boolean indicating if you want output in json
Jon Hall42db6dc2014-10-24 19:03:48 -0400773 '''
774 try:
775 self.handle.sendline("")
776 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800777
Jon Hall42db6dc2014-10-24 19:03:48 -0400778 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800779 self.handle.sendline("hosts -j")
780 self.handle.expect("hosts -j")
781 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400782 handle = self.handle.before
783 '''
784 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
785 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
786 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Hallffb386d2014-11-21 13:43:38 -0800787 So we take off that escape sequence using
Jon Hall42db6dc2014-10-24 19:03:48 -0400788 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800789 handle1 = ansi_escape.sub('', handle)
Jon Hall42db6dc2014-10-24 19:03:48 -0400790 '''
791 #print "repr(handle) =", repr(handle)
792 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
793 handle1 = ansi_escape.sub('', handle)
794 #print "repr(handle1) = ", repr(handle1)
795 return handle1
796 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800797 self.handle.sendline("hosts")
798 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400799 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400800 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400801 return handle
802 except pexpect.EOF:
803 main.log.error(self.name + ": EOF exception found")
804 main.log.error(self.name + ": " + self.handle.before)
805 main.cleanup()
806 main.exit()
807 except:
808 main.log.info(self.name+" ::::::")
809 main.log.error( traceback.print_exc())
810 main.log.info(self.name+" ::::::")
811 main.cleanup()
812 main.exit()
813
814 def get_host(self, mac):
815 '''
816 Return the first host from the hosts api whose 'id' contains 'mac'
817 Note: mac must be a colon seperated mac address, but could be a partial mac address
818 Return None if there is no match
819 '''
820 import json
821 try:
822 if mac == None:
823 return None
824 else:
825 mac = mac
826 raw_hosts = self.hosts()
827 hosts_json = json.loads(raw_hosts)
828 #search json for the host with mac then return the device
829 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400830 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400831 if mac in host['id']:
832 return host
833 return None
834 except pexpect.EOF:
835 main.log.error(self.name + ": EOF exception found")
836 main.log.error(self.name + ": " + self.handle.before)
837 main.cleanup()
838 main.exit()
839 except:
840 main.log.info(self.name+" ::::::")
841 main.log.error( traceback.print_exc())
842 main.log.info(self.name+" ::::::")
843 main.cleanup()
844 main.exit()
845
andrewonlab3f0a4af2014-10-17 12:25:14 -0400846
847 def get_hosts_id(self, host_list):
848 '''
849 Obtain list of hosts
850 Issues command: 'onos> hosts'
851
852 Required:
853 * host_list: List of hosts obtained by Mininet
854 IMPORTANT:
855 This function assumes that you started your
856 topology with the option '--mac'.
857 Furthermore, it assumes that value of VLAN is '-1'
858 Description:
859 Converts mininet hosts (h1, h2, h3...) into
860 ONOS format (00:00:00:00:00:01/-1 , ...)
861 '''
862
863 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400864 onos_host_list = []
865
866 for host in host_list:
867 host = host.replace("h", "")
868 host_hex = hex(int(host)).zfill(12)
869 host_hex = str(host_hex).replace('x','0')
870 i = iter(str(host_hex))
871 host_hex = ":".join(a+b for a,b in zip(i,i))
872 host_hex = host_hex + "/-1"
873 onos_host_list.append(host_hex)
874
875 return onos_host_list
876
877 except pexpect.EOF:
878 main.log.error(self.name + ": EOF exception found")
879 main.log.error(self.name + ": " + self.handle.before)
880 main.cleanup()
881 main.exit()
882 except:
883 main.log.info(self.name+" ::::::")
884 main.log.error( traceback.print_exc())
885 main.log.info(self.name+" ::::::")
886 main.cleanup()
887 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400888
andrewonlabe6745342014-10-17 14:29:13 -0400889 def add_host_intent(self, host_id_one, host_id_two):
890 '''
891 Required:
892 * host_id_one: ONOS host id for host1
893 * host_id_two: ONOS host id for host2
894 Description:
895 Adds a host-to-host intent (bidrectional) by
Jon Hallb1290e82014-11-18 16:17:48 -0500896 specifying the two hosts.
andrewonlabe6745342014-10-17 14:29:13 -0400897 '''
898 try:
899 self.handle.sendline("")
900 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500901
andrewonlabe6745342014-10-17 14:29:13 -0400902 self.handle.sendline("add-host-intent "+
903 str(host_id_one) + " " + str(host_id_two))
904 self.handle.expect("onos>")
905
andrewonlabe6745342014-10-17 14:29:13 -0400906 handle = self.handle.before
Jon Hallffb386d2014-11-21 13:43:38 -0800907 #print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400908
909 main.log.info("Intent installed between "+
910 str(host_id_one) + " and " + str(host_id_two))
911
912 return handle
Jon Hallb1290e82014-11-18 16:17:48 -0500913
andrewonlabe6745342014-10-17 14:29:13 -0400914 except pexpect.EOF:
915 main.log.error(self.name + ": EOF exception found")
916 main.log.error(self.name + ": " + self.handle.before)
917 main.cleanup()
918 main.exit()
919 except:
920 main.log.info(self.name+" ::::::")
921 main.log.error( traceback.print_exc())
922 main.log.info(self.name+" ::::::")
923 main.cleanup()
924 main.exit()
925
andrewonlab7b31d232014-10-24 13:31:47 -0400926 def add_optical_intent(self, ingress_device, egress_device):
927 '''
928 Required:
929 * ingress_device: device id of ingress device
930 * egress_device: device id of egress device
931 Optional:
932 TODO: Still needs to be implemented via dev side
933 '''
934 try:
935 self.handle.sendline("add-optical-intent "+
936 str(ingress_device) + " " + str(egress_device))
937 self.handle.expect("add-optical-intent")
938 i = self.handle.expect([
939 "Error",
940 "onos>"])
941
942 handle = self.handle.before
943
944 #If error, return error message
945 if i == 0:
946 return handle
947 else:
948 return main.TRUE
949
950 except pexpect.EOF:
951 main.log.error(self.name + ": EOF exception found")
952 main.log.error(self.name + ": " + self.handle.before)
953 main.cleanup()
954 main.exit()
955 except:
956 main.log.info(self.name+" ::::::")
957 main.log.error( traceback.print_exc())
958 main.log.info(self.name+" ::::::")
959 main.cleanup()
960 main.exit()
961
andrewonlab36af3822014-11-18 17:48:18 -0500962 def add_point_intent(self, ingress_device, egress_device,
963 port_ingress="", port_egress="", ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -0500964 ethDst="", bandwidth="", lambda_alloc=False,
965 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400966 '''
967 Required:
968 * ingress_device: device id of ingress device
969 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400970 Optional:
971 * ethType: specify ethType
972 * ethSrc: specify ethSrc (i.e. src mac addr)
973 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500974 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -0500975 * lambda_alloc: if True, intent will allocate lambda
976 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -0500977 * ipProto: specify ip protocol
978 * ipSrc: specify ip source address
979 * ipDst: specify ip destination address
980 * tcpSrc: specify tcp source port
981 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400982 Description:
983 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400984 specifying device id's and optional fields
985
andrewonlab4dbb4d82014-10-17 18:22:31 -0400986 NOTE: This function may change depending on the
987 options developers provide for point-to-point
988 intent via cli
989 '''
990 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400991 cmd = ""
992
993 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500994 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -0500995 and not bandwidth and not lambda_alloc \
996 and not ipProto and not ipSrc and not ipDst \
997 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -0500998 cmd = "add-point-intent"
999
1000
andrewonlab289e4b72014-10-21 21:24:18 -04001001 else:
andrewonlab36af3822014-11-18 17:48:18 -05001002 cmd = "add-point-intent"
andrewonlab9a130be2014-10-22 12:44:56 -04001003
andrewonlab0c0a6772014-10-22 12:31:18 -04001004 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001005 cmd += " --ethType " + str(ethType)
1006 if ethSrc:
1007 cmd += " --ethSrc " + str(ethSrc)
1008 if ethDst:
1009 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001010 if bandwidth:
1011 cmd += " --bandwidth " + str(bandwidth)
1012 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001013 cmd += " --lambda "
1014 if ipProto:
1015 cmd += " --ipProto " + str(ipProto)
1016 if ipSrc:
1017 cmd += " --ipSrc " + str(ipSrc)
1018 if ipDst:
1019 cmd += " --ipDst " + str(ipDst)
1020 if tcpSrc:
1021 cmd += " --tcpSrc " + str(tcpSrc)
1022 if tcpDst:
1023 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001024
andrewonlab36af3822014-11-18 17:48:18 -05001025 #Check whether the user appended the port
1026 #or provided it as an input
1027 if "/" in ingress_device:
1028 cmd += " "+str(ingress_device)
1029 else:
1030 if not port_ingress:
1031 main.log.error("You must specify "+
1032 "the ingress port")
1033 #TODO: perhaps more meaningful return
1034 return main.FALSE
1035
1036 cmd += " "+ \
1037 str(ingress_device) + "/" +\
1038 str(port_ingress) + " "
1039
1040 if "/" in egress_device:
1041 cmd += " "+str(egress_device)
1042 else:
1043 if not port_egress:
1044 main.log.error("You must specify "+
1045 "the egress port")
1046 return main.FALSE
1047
1048 cmd += " "+\
1049 str(egress_device) + "/" +\
1050 str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001051
andrewonlab289e4b72014-10-21 21:24:18 -04001052 self.handle.sendline(cmd)
andrewonlab36af3822014-11-18 17:48:18 -05001053
1054 main.log.info(cmd + " sent")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001055 i = self.handle.expect([
1056 "Error",
1057 "onos>"])
andrewonlab4dbb4d82014-10-17 18:22:31 -04001058
1059 if i == 0:
1060 main.log.error("Error in adding point-to-point intent")
1061 return handle
1062 else:
1063 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001064
andrewonlab4dbb4d82014-10-17 18:22:31 -04001065 except pexpect.EOF:
1066 main.log.error(self.name + ": EOF exception found")
1067 main.log.error(self.name + ": " + self.handle.before)
1068 main.cleanup()
1069 main.exit()
1070 except:
1071 main.log.info(self.name+" ::::::")
1072 main.log.error( traceback.print_exc())
1073 main.log.info(self.name+" ::::::")
1074 main.cleanup()
1075 main.exit()
1076
shahshreyad0c80432014-12-04 16:56:05 -08001077
1078 def add_multipoint_to_singlepoint_intent(self, ingress_device1, ingress_device2,egress_device,
1079 port_ingress="", port_egress="", ethType="", ethSrc="",
1080 ethDst="", bandwidth="", lambda_alloc=False,
1081 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="", setEthDst=""):
1082 '''
1083 Note:
1084 This function assumes that there would be 2 ingress devices and one egress device
1085 For more number of ingress devices, this function needs to be modified
1086 Required:
1087 * ingress_device1: device id of ingress device1
1088 * ingress_device2: device id of ingress device2
1089 * egress_device: device id of egress device
1090 Optional:
1091 * ethType: specify ethType
1092 * ethSrc: specify ethSrc (i.e. src mac addr)
1093 * ethDst: specify ethDst (i.e. dst mac addr)
1094 * bandwidth: specify bandwidth capacity of link
1095 * lambda_alloc: if True, intent will allocate lambda
1096 for the specified intent
1097 * ipProto: specify ip protocol
1098 * ipSrc: specify ip source address
1099 * ipDst: specify ip destination address
1100 * tcpSrc: specify tcp source port
1101 * tcpDst: specify tcp destination port
1102 * setEthSrc: action to Rewrite Source MAC Address
1103 * setEthDst: action to Rewrite Destination MAC Address
1104 Description:
1105 Adds a multipoint-to-singlepoint intent (uni-directional) by
1106 specifying device id's and optional fields
1107
1108 NOTE: This function may change depending on the
1109 options developers provide for multipointpoint-to-singlepoint
1110 intent via cli
1111 '''
1112 try:
1113 cmd = ""
1114
1115 #If there are no optional arguments
1116 if not ethType and not ethSrc and not ethDst\
1117 and not bandwidth and not lambda_alloc \
1118 and not ipProto and not ipSrc and not ipDst \
1119 and not tcpSrc and not tcpDst and not setEthSrc and not setEthDst:
1120 cmd = "add-multi-to-single-intent"
1121
1122
1123 else:
1124 cmd = "add-multi-to-single-intent"
1125
1126 if ethType:
1127 cmd += " --ethType " + str(ethType)
1128 if ethSrc:
1129 cmd += " --ethSrc " + str(ethSrc)
1130 if ethDst:
1131 cmd += " --ethDst " + str(ethDst)
1132 if bandwidth:
1133 cmd += " --bandwidth " + str(bandwidth)
1134 if lambda_alloc:
1135 cmd += " --lambda "
1136 if ipProto:
1137 cmd += " --ipProto " + str(ipProto)
1138 if ipSrc:
1139 cmd += " --ipSrc " + str(ipSrc)
1140 if ipDst:
1141 cmd += " --ipDst " + str(ipDst)
1142 if tcpSrc:
1143 cmd += " --tcpSrc " + str(tcpSrc)
1144 if tcpDst:
1145 cmd += " --tcpDst " + str(tcpDst)
1146 if setEthSrc:
1147 cmd += " --setEthSrc "+ str(setEthSrc)
1148 if setEthDst:
1149 cmd += " --setEthDst "+ str(setEthDst)
1150
1151 #Check whether the user appended the port
1152 #or provided it as an input
1153 if "/" in ingress_device1:
1154 cmd += " "+str(ingress_device1)
1155 else:
1156 if not port_ingress1:
1157 main.log.error("You must specify "+
1158 "the ingress port1")
1159 #TODO: perhaps more meaningful return
1160 return main.FALSE
1161
1162 cmd += " "+ \
1163 str(ingress_device1) + "/" +\
1164 str(port_ingress1) + " "
1165
1166 if "/" in ingress_device2:
1167 cmd += " "+str(ingress_device2)
1168 else:
1169 if not port_ingress2:
1170 main.log.error("You must specify "+
1171 "the ingress port2")
1172 #TODO: perhaps more meaningful return
1173 return main.FALSE
1174
1175 cmd += " "+ \
1176 str(ingress_device2) + "/" +\
1177 str(port_ingress2) + " "
1178
1179 if "/" in egress_device:
1180 cmd += " "+str(egress_device)
1181 else:
1182 if not port_egress:
1183 main.log.error("You must specify "+
1184 "the egress port")
1185 return main.FALSE
1186
1187 cmd += " "+\
1188 str(egress_device) + "/" +\
1189 str(port_egress)
1190 print "cmd= ",cmd
1191 self.handle.sendline(cmd)
1192
1193 main.log.info(cmd + " sent")
1194 i = self.handle.expect([
1195 "Error",
1196 "onos>"])
1197
1198 if i == 0:
1199 main.log.error("Error in adding point-to-point intent")
1200 return self.handle
1201 else:
1202 return main.TRUE
1203
1204 except pexpect.EOF:
1205 main.log.error(self.name + ": EOF exception found")
1206 main.log.error(self.name + ": " + self.handle.before)
1207 main.cleanup()
1208 main.exit()
1209 except:
1210 main.log.info(self.name+" ::::::")
1211 main.log.error( traceback.print_exc())
1212 main.log.info(self.name+" ::::::")
1213 main.cleanup()
1214 main.exit()
1215
1216
andrewonlab9a50dfe2014-10-17 17:22:31 -04001217 def remove_intent(self, intent_id):
1218 '''
1219 Remove intent for specified intent id
1220 '''
1221 try:
1222 self.handle.sendline("")
1223 self.handle.expect("onos>")
1224
1225 self.handle.sendline("remove-intent "+str(intent_id))
1226 i = self.handle.expect([
1227 "Error",
1228 "onos>"])
1229
1230 handle = self.handle.before
1231
1232 if i == 0:
1233 main.log.error("Error in removing intent")
1234 return handle
1235 else:
1236 return handle
1237
1238 except pexpect.EOF:
1239 main.log.error(self.name + ": EOF exception found")
1240 main.log.error(self.name + ": " + self.handle.before)
1241 main.cleanup()
1242 main.exit()
1243 except:
1244 main.log.info(self.name+" ::::::")
1245 main.log.error( traceback.print_exc())
1246 main.log.info(self.name+" ::::::")
1247 main.cleanup()
1248 main.exit()
1249
pingping-lindabe7972014-11-17 19:29:44 -08001250 # This method should be used after installing application: onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001251 def routes(self, json_format=False):
1252 '''
1253 Optional:
1254 * json_format: enable output formatting in json
1255 Description:
1256 Obtain all routes in the system
1257 '''
1258 try:
1259 if json_format:
1260 self.handle.sendline("routes -j")
1261 self.handle.expect("routes -j")
1262 self.handle.expect("onos>")
1263 handle_tmp = self.handle.before
1264
1265 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1266 handle = ansi_escape.sub('', handle_tmp)
1267
1268 else:
1269 self.handle.sendline("")
1270 self.handle.expect("onos>")
1271
1272 self.handle.sendline("routes")
1273 self.handle.expect("onos>")
1274 handle = self.handle.before
1275
1276 return handle
1277
1278 except pexpect.EOF:
1279 main.log.error(self.name + ": EOF exception found")
1280 main.log.error(self.name + ": " + self.handle.before)
1281 main.cleanup()
1282 main.exit()
1283 except:
1284 main.log.info(self.name + " ::::::")
1285 main.log.error(traceback.print_exc())
1286 main.log.info(self.name + " ::::::")
1287 main.cleanup()
1288 main.exit()
1289
Jon Hallffb386d2014-11-21 13:43:38 -08001290 def intents(self, json_format = True):
andrewonlabe6745342014-10-17 14:29:13 -04001291 '''
andrewonlab377693f2014-10-21 16:00:30 -04001292 Optional:
1293 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001294 Description:
1295 Obtain intents currently installed
1296 '''
1297 try:
andrewonlab377693f2014-10-21 16:00:30 -04001298 if json_format:
1299 self.handle.sendline("intents -j")
1300 self.handle.expect("intents -j")
1301 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001302 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001303
1304 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1305 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001306 else:
1307 self.handle.sendline("")
1308 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001309
andrewonlab377693f2014-10-21 16:00:30 -04001310 self.handle.sendline("intents")
1311 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001312 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001313
1314 return handle
1315
1316 except pexpect.EOF:
1317 main.log.error(self.name + ": EOF exception found")
1318 main.log.error(self.name + ": " + self.handle.before)
1319 main.cleanup()
1320 main.exit()
1321 except:
1322 main.log.info(self.name+" ::::::")
1323 main.log.error( traceback.print_exc())
1324 main.log.info(self.name+" ::::::")
1325 main.cleanup()
1326 main.exit()
1327
Jon Hallffb386d2014-11-21 13:43:38 -08001328 def flows(self, json_format = True):
Shreya Shah0f01c812014-10-26 20:15:28 -04001329 '''
1330 Optional:
1331 * json_format: enable output formatting in json
1332 Description:
1333 Obtain flows currently installed
1334 '''
1335 try:
1336 if json_format:
1337 self.handle.sendline("flows -j")
1338 self.handle.expect("flows -j")
1339 self.handle.expect("onos>")
1340 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001341 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1342 handle = ansi_escape.sub('', handle)
Shreya Shah0f01c812014-10-26 20:15:28 -04001343
1344 else:
1345 self.handle.sendline("")
1346 self.handle.expect("onos>")
1347 self.handle.sendline("flows")
1348 self.handle.expect("onos>")
1349 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001350 if re.search("Error\sexecuting\scommand:", handle):
1351 main.log.error(self.name + ".flows() response: " + str(handle))
Shreya Shah0f01c812014-10-26 20:15:28 -04001352
1353 return handle
1354
1355 except pexpect.EOF:
1356 main.log.error(self.name + ": EOF exception found")
1357 main.log.error(self.name + ": " + self.handle.before)
1358 main.cleanup()
1359 main.exit()
1360 except:
1361 main.log.info(self.name+" ::::::")
1362 main.log.error( traceback.print_exc())
1363 main.log.info(self.name+" ::::::")
1364 main.cleanup()
1365 main.exit()
1366
andrewonlabb66dfa12014-12-02 15:51:10 -05001367 def push_test_intents(self, dpid_src, dpid_dst, num_intents,
1368 num_mult="", app_id="", report=True):
andrewonlab87852b02014-11-19 18:44:19 -05001369 '''
1370 Description:
1371 Push a number of intents in a batch format to
1372 a specific point-to-point intent definition
1373 Required:
1374 * dpid_src: specify source dpid
1375 * dpid_dst: specify destination dpid
1376 * num_intents: specify number of intents to push
1377 Optional:
andrewonlabb66dfa12014-12-02 15:51:10 -05001378 * num_mult: number multiplier for multiplying
1379 the number of intents specified
1380 * app_id: specify the application id init to further
1381 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001382 * report: default True, returns latency information
1383 '''
1384 try:
1385 cmd = "push-test-intents "+\
1386 str(dpid_src)+" "+str(dpid_dst)+" "+\
1387 str(num_intents)
andrewonlabb66dfa12014-12-02 15:51:10 -05001388
1389 if num_mult:
1390 cmd += " " + str(num_mult)
andrewonlab042b3912014-12-10 16:40:50 -05001391 #If app id is specified, then num_mult
1392 #must exist because of the way this command
1393 #takes in arguments
andrewonlabb66dfa12014-12-02 15:51:10 -05001394 if app_id:
1395 cmd += " " + str(app_id)
1396
andrewonlab87852b02014-11-19 18:44:19 -05001397 self.handle.sendline(cmd)
1398 self.handle.expect(cmd)
1399 self.handle.expect("onos>")
1400
1401 handle = self.handle.before
1402
1403 #Some color thing that we want to escape
1404 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1405 handle = ansi_escape.sub('', handle)
1406
1407 if report:
andrewonlabb66dfa12014-12-02 15:51:10 -05001408 lat_result = []
andrewonlab87852b02014-11-19 18:44:19 -05001409 main.log.info(handle)
andrewonlabb66dfa12014-12-02 15:51:10 -05001410 #Split result by newline
1411 newline = handle.split("\r\r\n")
1412 #Ignore the first object of list, which is empty
1413 newline = newline[1:]
1414 #Some sloppy parsing method to get the latency
1415 for result in newline:
1416 result = result.split(": ")
1417 #Append the first result of second parse
1418 lat_result.append(result[1].split(" ")[0])
1419
andrewonlab042b3912014-12-10 16:40:50 -05001420 main.log.info(lat_result)
andrewonlabb66dfa12014-12-02 15:51:10 -05001421 return lat_result
andrewonlab87852b02014-11-19 18:44:19 -05001422 else:
1423 return main.TRUE
1424
1425 except pexpect.EOF:
1426 main.log.error(self.name + ": EOF exception found")
1427 main.log.error(self.name + ": " + self.handle.before)
1428 main.cleanup()
1429 main.exit()
1430 except:
1431 main.log.info(self.name+" ::::::")
1432 main.log.error( traceback.print_exc())
1433 main.log.info(self.name+" ::::::")
1434 main.cleanup()
1435 main.exit()
1436
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001437 def intents_events_metrics(self, json_format=True):
1438 '''
1439 Description:Returns topology metrics
1440 Optional:
1441 * json_format: enable json formatting of output
1442 '''
1443 try:
1444 if json_format:
1445 self.handle.sendline("intents-events-metrics -j")
1446 self.handle.expect("intents-events-metrics -j")
1447 self.handle.expect("onos>")
1448
1449 handle = self.handle.before
1450
1451 #Some color thing that we want to escape
1452 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1453 handle = ansi_escape.sub('', handle)
1454
1455 else:
1456 self.handle.sendline("intents-events-metrics")
1457 self.handle.expect("intents-events-metrics")
1458 self.handle.expect("onos>")
1459
1460 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001461
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001462 return handle
1463
1464 except pexpect.EOF:
1465 main.log.error(self.name + ": EOF exception found")
1466 main.log.error(self.name + ": " + self.handle.before)
1467 main.cleanup()
1468 main.exit()
1469 except:
1470 main.log.info(self.name+" ::::::")
1471 main.log.error( traceback.print_exc())
1472 main.log.info(self.name+" ::::::")
1473 main.cleanup()
1474 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001475
andrewonlab867212a2014-10-22 20:13:38 -04001476 def topology_events_metrics(self, json_format=True):
1477 '''
1478 Description:Returns topology metrics
1479 Optional:
1480 * json_format: enable json formatting of output
1481 '''
1482 try:
1483 if json_format:
1484 self.handle.sendline("topology-events-metrics -j")
1485 self.handle.expect("topology-events-metrics -j")
1486 self.handle.expect("onos>")
1487
1488 handle = self.handle.before
1489
1490 #Some color thing that we want to escape
1491 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1492 handle = ansi_escape.sub('', handle)
1493
1494 else:
1495 self.handle.sendline("topology-events-metrics")
1496 self.handle.expect("topology-events-metrics")
1497 self.handle.expect("onos>")
1498
1499 handle = self.handle.before
1500
1501 return handle
1502
1503 except pexpect.EOF:
1504 main.log.error(self.name + ": EOF exception found")
1505 main.log.error(self.name + ": " + self.handle.before)
1506 main.cleanup()
1507 main.exit()
1508 except:
1509 main.log.info(self.name+" ::::::")
1510 main.log.error( traceback.print_exc())
1511 main.log.info(self.name+" ::::::")
1512 main.cleanup()
1513 main.exit()
1514
andrewonlab3e15ead2014-10-15 14:21:34 -04001515 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001516 #Wrapper functions use existing driver
1517 #functions and extends their use case.
1518 #For example, we may use the output of
1519 #a normal driver function, and parse it
1520 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001521
andrewonlab9a50dfe2014-10-17 17:22:31 -04001522 def get_all_intents_id(self):
1523 '''
1524 Description:
1525 Obtain all intent id's in a list
1526 '''
1527 try:
1528 #Obtain output of intents function
1529 intents_str = self.intents()
1530 all_intent_list = []
1531 intent_id_list = []
1532
1533 #Parse the intents output for ID's
1534 intents_list = [s.strip() for s in intents_str.splitlines()]
1535 for intents in intents_list:
1536 if "onos>" in intents:
1537 continue
1538 elif "intents" in intents:
1539 continue
1540 else:
1541 line_list = intents.split(" ")
1542 all_intent_list.append(line_list[0])
1543
1544 all_intent_list = all_intent_list[1:-2]
1545
1546 for intents in all_intent_list:
1547 if not intents:
1548 continue
1549 else:
1550 intent_id_list.append(intents)
1551
1552 return intent_id_list
1553
1554 except pexpect.EOF:
1555 main.log.error(self.name + ": EOF exception found")
1556 main.log.error(self.name + ": " + self.handle.before)
1557 main.cleanup()
1558 main.exit()
1559 except:
1560 main.log.info(self.name+" ::::::")
1561 main.log.error( traceback.print_exc())
1562 main.log.info(self.name+" ::::::")
1563 main.cleanup()
1564 main.exit()
1565
andrewonlab7e4d2d32014-10-15 13:23:21 -04001566 def get_all_devices_id(self):
1567 '''
1568 Use 'devices' function to obtain list of all devices
1569 and parse the result to obtain a list of all device
1570 id's. Returns this list. Returns empty list if no
1571 devices exist
1572 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001573
1574 This function may be useful if you are not sure of the
1575 device id, and wish to execute other commands using
1576 the ids. By obtaining the list of device ids on the fly,
1577 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001578 '''
1579 try:
1580 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001581 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001582 id_list = []
1583
1584 if not devices_str:
1585 main.log.info("There are no devices to get id from")
1586 return id_list
1587
1588 #Split the string into list by comma
1589 device_list = devices_str.split(",")
1590 #Get temporary list of all arguments with string 'id='
1591 temp_list = [dev for dev in device_list if "id=" in dev]
1592 #Split list further into arguments before and after string
1593 # 'id='. Get the latter portion (the actual device id) and
1594 # append to id_list
1595 for arg in temp_list:
1596 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001597 return id_list
1598
1599 except pexpect.EOF:
1600 main.log.error(self.name + ": EOF exception found")
1601 main.log.error(self.name + ": " + self.handle.before)
1602 main.cleanup()
1603 main.exit()
1604 except:
1605 main.log.info(self.name+" ::::::")
1606 main.log.error( traceback.print_exc())
1607 main.log.info(self.name+" ::::::")
1608 main.cleanup()
1609 main.exit()
1610
andrewonlab7c211572014-10-15 16:45:20 -04001611 def get_all_nodes_id(self):
1612 '''
1613 Uses 'nodes' function to obtain list of all nodes
1614 and parse the result of nodes to obtain just the
1615 node id's.
1616 Returns:
1617 list of node id's
1618 '''
1619 try:
1620 nodes_str = self.nodes()
1621 id_list = []
1622
1623 if not nodes_str:
1624 main.log.info("There are no nodes to get id from")
1625 return id_list
1626
1627 #Sample nodes_str output
1628 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1629
1630 #Split the string into list by comma
1631 nodes_list = nodes_str.split(",")
1632 temp_list = [node for node in nodes_list if "id=" in node]
1633 for arg in temp_list:
1634 id_list.append(arg.split("id=")[1])
1635
1636 return id_list
1637
1638 except pexpect.EOF:
1639 main.log.error(self.name + ": EOF exception found")
1640 main.log.error(self.name + ": " + self.handle.before)
1641 main.cleanup()
1642 main.exit()
1643 except:
1644 main.log.info(self.name+" ::::::")
1645 main.log.error( traceback.print_exc())
1646 main.log.info(self.name+" ::::::")
1647 main.cleanup()
1648 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001649
Jon Halla91c4dc2014-10-22 12:57:04 -04001650 def get_device(self, dpid=None):
1651 '''
1652 Return the first device from the devices api whose 'id' contains 'dpid'
1653 Return None if there is no match
1654 '''
1655 import json
1656 try:
1657 if dpid == None:
1658 return None
1659 else:
1660 dpid = dpid.replace(':', '')
1661 raw_devices = self.devices()
1662 devices_json = json.loads(raw_devices)
1663 #search json for the device with dpid then return the device
1664 for device in devices_json:
1665 #print "%s in %s?" % (dpid, device['id'])
1666 if dpid in device['id']:
1667 return device
1668 return None
1669 except pexpect.EOF:
1670 main.log.error(self.name + ": EOF exception found")
1671 main.log.error(self.name + ": " + self.handle.before)
1672 main.cleanup()
1673 main.exit()
1674 except:
1675 main.log.info(self.name+" ::::::")
1676 main.log.error( traceback.print_exc())
1677 main.log.info(self.name+" ::::::")
1678 main.cleanup()
1679 main.exit()
1680
Jon Hall42db6dc2014-10-24 19:03:48 -04001681 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1682 '''
1683 Checks the number of swithes & links that ONOS sees against the
1684 supplied values. By default this will report to main.log, but the
1685 log level can be specifid.
1686
1687 Params: ip = ip used for the onos cli
1688 numoswitch = expected number of switches
1689 numlink = expected number of links
1690 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1691
1692
1693 log_level can
1694
1695 Returns: main.TRUE if the number of switchs and links are correct,
1696 main.FALSE if the numer of switches and links is incorrect,
1697 and main.ERROR otherwise
1698 '''
1699
1700 try:
1701 topology = self.get_topology(ip)
1702 if topology == {}:
1703 return main.ERROR
1704 output = ""
1705 #Is the number of switches is what we expected
1706 devices = topology.get('devices',False)
1707 links = topology.get('links',False)
1708 if devices == False or links == False:
1709 return main.ERROR
1710 switch_check = ( int(devices) == int(numoswitch) )
1711 #Is the number of links is what we expected
1712 link_check = ( int(links) == int(numolink) )
1713 if (switch_check and link_check):
1714 #We expected the correct numbers
1715 output = output + "The number of links and switches match "\
1716 + "what was expected"
1717 result = main.TRUE
1718 else:
1719 output = output + \
1720 "The number of links and switches does not match what was expected"
1721 result = main.FALSE
1722 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1723 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1724 if log_level == "report":
1725 main.log.report(output)
1726 elif log_level == "warn":
1727 main.log.warn(output)
1728 else:
1729 main.log.info(output)
1730 return result
1731 except pexpect.EOF:
1732 main.log.error(self.name + ": EOF exception found")
1733 main.log.error(self.name + ": " + self.handle.before)
1734 main.cleanup()
1735 main.exit()
1736 except:
1737 main.log.info(self.name+" ::::::")
1738 main.log.error( traceback.print_exc())
1739 main.log.info(self.name+" ::::::")
1740 main.cleanup()
1741 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001742
1743 def device_role(self, device_id, onos_node, role="master"):
1744 '''
1745 Calls the device-role cli command.
1746 device_id must be the id of a device as seen in the onos devices command
1747 onos_node is the ip of one of the onos nodes in the cluster
1748 role must be either master, standby, or none
1749
Jon Hall94fd0472014-12-08 11:52:42 -08001750 Returns main.TRUE or main.FALSE based on argument verification.
Jon Hall983a1702014-10-28 18:44:22 -04001751 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001752 support that output
1753 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001754 try:
Jon Hall983a1702014-10-28 18:44:22 -04001755 #print "beginning device_role... \n\tdevice_id:" + device_id
1756 #print "\tonos_node: " + onos_node
1757 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001758 if role.lower() == "master" or \
1759 role.lower() == "standby" or \
1760 role.lower() == "none":
1761 self.handle.sendline("")
1762 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001763 self.handle.sendline("device-role " +
1764 str(device_id) + " " +
1765 str(onos_node) + " " +
1766 str(role))
1767 i= self.handle.expect(["Error","onos>"])
1768 if i == 0:
1769 output = str(self.handle.before)
1770 self.handle.expect("onos>")
1771 output = output + str(self.handle.before)
1772 main.log.error(self.name + ": " +
1773 output + '\033[0m')#end color output to escape any colours from the cli
1774 return main.ERROR
1775 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001776 self.handle.expect("onos>")
1777 return main.TRUE
1778 else:
1779 return main.FALSE
1780
1781 except pexpect.EOF:
1782 main.log.error(self.name + ": EOF exception found")
1783 main.log.error(self.name + ": " + self.handle.before)
1784 main.cleanup()
1785 main.exit()
1786 except:
1787 main.log.info(self.name+" ::::::")
1788 main.log.error( traceback.print_exc())
1789 main.log.info(self.name+" ::::::")
1790 main.cleanup()
1791 main.exit()
1792
Jon Hall73cf9cc2014-11-20 22:28:38 -08001793 def clusters(self, json_format=True):
1794 '''
1795 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001796 Optional argument:
1797 * json_format - boolean indicating if you want output in json
Jon Hall73cf9cc2014-11-20 22:28:38 -08001798 '''
1799 try:
1800 self.handle.sendline("")
1801 self.handle.expect("onos>")
1802
1803 if json_format:
1804 self.handle.sendline("clusters -j")
1805 self.handle.expect("clusters -j")
1806 self.handle.expect("onos>")
1807 handle = self.handle.before
1808 '''
1809 handle variable here contains some ANSI escape color code
1810 sequences at the end which are invisible in the print command
1811 output. To make that escape sequence visible, use repr() function.
1812 The repr(handle) output when printed shows the ANSI escape sequences.
1813 In json.loads(somestring), this somestring variable is actually
1814 repr(somestring) and json.loads would fail with the escape sequence.
1815 So we take off that escape sequence using
1816 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1817 handle1 = ansi_escape.sub('', handle)
1818 '''
1819 #print "repr(handle) =", repr(handle)
1820 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1821 handle1 = ansi_escape.sub('', handle)
1822 #print "repr(handle1) = ", repr(handle1)
1823 return handle1
1824 else:
Jon Hallffb386d2014-11-21 13:43:38 -08001825 self.handle.sendline("clusters")
Jon Hall73cf9cc2014-11-20 22:28:38 -08001826 self.handle.expect("onos>")
1827 handle = self.handle.before
1828 #print "handle =",handle
1829 return handle
1830 except pexpect.EOF:
1831 main.log.error(self.name + ": EOF exception found")
1832 main.log.error(self.name + ": " + self.handle.before)
1833 main.cleanup()
1834 main.exit()
1835 except:
1836 main.log.info(self.name+" ::::::")
1837 main.log.error( traceback.print_exc())
1838 main.log.info(self.name+" ::::::")
1839 main.cleanup()
1840 main.exit()
1841
Jon Hall94fd0472014-12-08 11:52:42 -08001842 def election_test_leader(self):
1843 '''
1844 * CLI command to get the current leader for the Election test application.
1845 #NOTE: Requires installation of the onos-app-election feature
1846 Returns: Node IP of the leader if one exists
1847 None if none exists
1848 Main.FALSE on error
1849 '''
1850 try:
1851 self.handle.sendline("election-test-leader")
1852 self.handle.expect("election-test-leader")
1853 self.handle.expect("onos>")
1854 response = self.handle.before
1855 #Leader
1856 node_search = re.search("The\scurrent\sleader\sfor\sthe\sElection\sapp\sis\s(?P<node>.+)\.", response)
1857 if node_search:
1858 node = node_search.group('node')
Jon Hall669173b2014-12-17 11:36:30 -08001859 main.log.info("Election-test-leader on "+str(self.name)+" found " + node + " as the leader")
Jon Hall94fd0472014-12-08 11:52:42 -08001860 return node
1861 #no leader
1862 null_search = re.search("There\sis\scurrently\sno\sleader\selected\sfor\sthe\sElection\sapp", response)
1863 if null_search:
Jon Hall669173b2014-12-17 11:36:30 -08001864 main.log.info("Election-test-leader found no leader on " + self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001865 return None
1866 #error
Jon Hall669173b2014-12-17 11:36:30 -08001867 if re.search("Command\snot\sfound", response):
1868 main.log.error("Election app is not loaded on " + self.name)
1869 return main.FALSE
1870 else:
1871 main.log.error("Error in election_test_leader: unexpected response")
1872 main.log.error( repr(response) )
1873 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001874
1875 except pexpect.EOF:
1876 main.log.error(self.name + ": EOF exception found")
1877 main.log.error(self.name + ": " + self.handle.before)
1878 main.cleanup()
1879 main.exit()
1880 except:
1881 main.log.info(self.name+" ::::::")
1882 main.log.error( traceback.print_exc())
1883 main.log.info(self.name+" ::::::")
1884 main.cleanup()
1885 main.exit()
1886
1887 def election_test_run(self):
1888 '''
1889 * CLI command to run for leadership of the Election test application.
1890 #NOTE: Requires installation of the onos-app-election feature
1891 Returns: Main.TRUE on success
1892 Main.FALSE on error
1893 '''
1894 try:
1895 self.handle.sendline("election-test-run")
1896 self.handle.expect("election-test-run")
1897 self.handle.expect("onos>")
1898 response = self.handle.before
1899 #success
1900 search = re.search("Entering\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1901 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001902 main.log.info(self.name + " entering leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001903 return main.TRUE
1904 #error
Jon Hall669173b2014-12-17 11:36:30 -08001905 if re.search("Command\snot\sfound", response):
1906 main.log.error("Election app is not loaded on " + self.name)
1907 return main.FALSE
1908 else:
1909 main.log.error("Error in election_test_run: unexpected response")
1910 main.log.error( repr(response) )
1911 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001912
1913 except pexpect.EOF:
1914 main.log.error(self.name + ": EOF exception found")
1915 main.log.error(self.name + ": " + self.handle.before)
1916 main.cleanup()
1917 main.exit()
1918 except:
1919 main.log.info(self.name+" ::::::")
1920 main.log.error( traceback.print_exc())
1921 main.log.info(self.name+" ::::::")
1922 main.cleanup()
1923 main.exit()
1924
1925 def election_test_withdraw(self):
1926 '''
1927 * CLI command to withdraw the local node from leadership election for
1928 * the Election test application.
1929 #NOTE: Requires installation of the onos-app-election feature
1930 Returns: Main.TRUE on success
1931 Main.FALSE on error
1932 '''
1933 try:
1934 self.handle.sendline("election-test-withdraw")
1935 self.handle.expect("election-test-withdraw")
1936 self.handle.expect("onos>")
1937 response = self.handle.before
1938 #success
1939 search = re.search("Withdrawing\sfrom\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1940 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001941 main.log.info(self.name + " withdrawing from leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001942 return main.TRUE
1943 #error
Jon Hall669173b2014-12-17 11:36:30 -08001944 if re.search("Command\snot\sfound", response):
1945 main.log.error("Election app is not loaded on " + self.name)
1946 return main.FALSE
1947 else:
1948 main.log.error("Error in election_test_withdraw: unexpected response")
1949 main.log.error( repr(response) )
1950 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001951
1952
1953 except pexpect.EOF:
1954 main.log.error(self.name + ": EOF exception found")
1955 main.log.error(self.name + ": " + self.handle.before)
1956 main.cleanup()
1957 main.exit()
1958 except:
1959 main.log.info(self.name+" ::::::")
1960 main.log.error( traceback.print_exc())
1961 main.log.info(self.name+" ::::::")
1962 main.cleanup()
1963 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001964
andrewonlab7e4d2d32014-10-15 13:23:21 -04001965 #***********************************