blob: 45bb92e35eda287b6dbb96f8ddcae0cbe175f11a [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("\$")
93 self.handle.sendline("\n")
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...")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400197 self.handle.sendline("\x03")
198 self.handle.sendline("onos -w "+str(ONOS_ip))
199 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
200 timeout=30)
201 if i == 0:
202 return main.TRUE
203 else:
204 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400205 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400206 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400207
208 except pexpect.EOF:
209 main.log.error(self.name + ": EOF exception found")
210 main.log.error(self.name + ": " + self.handle.before)
211 main.cleanup()
212 main.exit()
213 except:
214 main.log.info(self.name+" ::::::")
215 main.log.error( traceback.print_exc())
216 main.log.info(self.name+" ::::::")
217 main.cleanup()
218 main.exit()
219
andrewonlaba18f6bf2014-10-13 19:31:54 -0400220 def sendline(self, cmd_str):
221 '''
222 Send a completely user specified string to
223 the onos> prompt. Use this function if you have
224 a very specific command to send.
225
226 Warning: There are no sanity checking to commands
227 sent using this method.
228 '''
229 try:
230 self.handle.sendline("")
231 self.handle.expect("onos>")
232
233 self.handle.sendline(cmd_str)
234 self.handle.expect("onos>")
235
236 handle = self.handle.before
237
238 self.handle.sendline("")
239 self.handle.expect("onos>")
240
241 handle += self.handle.before
242 handle += self.handle.after
243
244 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400245 ansi_escape = re.compile(r'\x1b[^m]*m')
246 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400247
248 return handle
249 except pexpect.EOF:
250 main.log.error(self.name + ": EOF exception found")
251 main.log.error(self.name + ": " + self.handle.before)
252 main.cleanup()
253 main.exit()
254 except:
255 main.log.info(self.name+" ::::::")
256 main.log.error( traceback.print_exc())
257 main.log.info(self.name+" ::::::")
258 main.cleanup()
259 main.exit()
260
andrewonlab95ce8322014-10-13 14:12:04 -0400261 #IMPORTANT NOTE:
262 #For all cli commands, naming convention should match
263 #the cli command replacing ':' with '_'.
264 #Ex) onos:topology > onos_topology
265 # onos:links > onos_links
266 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400267
268 def add_node(self, node_id, ONOS_ip, tcp_port=""):
269 '''
270 Adds a new cluster node by ID and address information.
271 Required:
272 * node_id
273 * ONOS_ip
274 Optional:
275 * tcp_port
276 '''
277 try:
278 self.handle.sendline("")
279 self.handle.expect("onos>")
280
281 self.handle.sendline("add-node "+
282 str(node_id)+" "+
283 str(ONOS_ip)+" "+
284 str(tcp_port))
285
286 i = self.handle.expect([
287 "Error",
288 "onos>" ])
289
290 #Clear handle to get previous output
291 self.handle.sendline("")
292 self.handle.expect("onos>")
293
294 handle = self.handle.before
295
296 if i == 0:
297 main.log.error("Error in adding node")
298 main.log.error(handle)
299 return main.FALSE
300 else:
301 main.log.info("Node "+str(ONOS_ip)+" added")
302 return main.TRUE
303
304 except pexpect.EOF:
305 main.log.error(self.name + ": EOF exception found")
306 main.log.error(self.name + ": " + self.handle.before)
307 main.cleanup()
308 main.exit()
309 except:
310 main.log.info(self.name+" ::::::")
311 main.log.error( traceback.print_exc())
312 main.log.info(self.name+" ::::::")
313 main.cleanup()
314 main.exit()
315
andrewonlab86dc3082014-10-13 18:18:38 -0400316 def remove_node(self, node_id):
317 '''
318 Removes a cluster by ID
319 Issues command: 'remove-node [<node-id>]'
320 Required:
321 * node_id
322 '''
323 try:
324 self.handle.sendline("")
325 self.handle.expect("onos>")
326
327 self.handle.sendline("remove-node "+str(node_id))
328 self.handle.expect("onos>")
329
330 return main.TRUE
331
332 except pexpect.EOF:
333 main.log.error(self.name + ": EOF exception found")
334 main.log.error(self.name + ": " + self.handle.before)
335 main.cleanup()
336 main.exit()
337 except:
338 main.log.info(self.name+" ::::::")
339 main.log.error( traceback.print_exc())
340 main.log.info(self.name+" ::::::")
341 main.cleanup()
342 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400343
andrewonlab7c211572014-10-15 16:45:20 -0400344 def nodes(self):
345 '''
346 List the nodes currently visible
347 Issues command: 'nodes'
348 Returns: entire handle of list of nodes
349 '''
350 try:
351 self.handle.sendline("")
352 self.handle.expect("onos>")
353
354 self.handle.sendline("nodes")
355 self.handle.expect("onos>")
356
357 self.handle.sendline("")
358 self.handle.expect("onos>")
359
360 handle = self.handle.before
361
362 return handle
363
364 except pexpect.EOF:
365 main.log.error(self.name + ": EOF exception found")
366 main.log.error(self.name + ": " + self.handle.before)
367 main.cleanup()
368 main.exit()
369 except:
370 main.log.info(self.name+" ::::::")
371 main.log.error( traceback.print_exc())
372 main.log.info(self.name+" ::::::")
373 main.cleanup()
374 main.exit()
375
andrewonlab38d6ae22014-10-15 14:23:45 -0400376 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400377 '''
378 Shows the current state of the topology
379 by issusing command: 'onos> onos:topology'
380 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400381 try:
382 self.handle.sendline("")
383 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400384 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400385 self.handle.sendline("onos:topology")
386 self.handle.expect("onos>")
387
388 handle = self.handle.before
389
390 main.log.info("onos:topology returned: " +
391 str(handle))
392
393 return handle
394
395 except pexpect.EOF:
396 main.log.error(self.name + ": EOF exception found")
397 main.log.error(self.name + ": " + self.handle.before)
398 main.cleanup()
399 main.exit()
400 except:
401 main.log.info(self.name+" ::::::")
402 main.log.error( traceback.print_exc())
403 main.log.info(self.name+" ::::::")
404 main.cleanup()
405 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400406
407 def feature_install(self, feature_str):
408 '''
409 Installs a specified feature
410 by issuing command: 'onos> feature:install <feature_str>'
411 '''
412 try:
413 self.handle.sendline("")
414 self.handle.expect("onos>")
415
416 self.handle.sendline("feature:install "+str(feature_str))
417 self.handle.expect("onos>")
418
419 return main.TRUE
420
421 except pexpect.EOF:
422 main.log.error(self.name + ": EOF exception found")
423 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500424 main.log.report("Failed to install feature")
425 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400426 main.cleanup()
427 main.exit()
428 except:
429 main.log.info(self.name+" ::::::")
430 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500431 main.log.report("Failed to install feature")
432 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400433 main.log.info(self.name+" ::::::")
434 main.cleanup()
435 main.exit()
436
437 def feature_uninstall(self, feature_str):
438 '''
439 Uninstalls a specified feature
440 by issuing command: 'onos> feature:uninstall <feature_str>'
441 '''
442 try:
443 self.handle.sendline("")
444 self.handle.expect("onos>")
445
446 self.handle.sendline("feature:uninstall "+str(feature_str))
447 self.handle.expect("onos>")
448
449 return main.TRUE
450
451 except pexpect.EOF:
452 main.log.error(self.name + ": EOF exception found")
453 main.log.error(self.name + ": " + self.handle.before)
454 main.cleanup()
455 main.exit()
456 except:
457 main.log.info(self.name+" ::::::")
458 main.log.error( traceback.print_exc())
459 main.log.info(self.name+" ::::::")
460 main.cleanup()
461 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400462
Jon Halle8217482014-10-17 13:49:14 -0400463 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400464 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400465 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400466 Optional argument:
467 * grep_str - pass in a string to grep
468 '''
469 try:
470 self.handle.sendline("")
471 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400472
473 if json_format:
474 if not grep_str:
475 self.handle.sendline("devices -j")
476 self.handle.expect("devices -j")
477 self.handle.expect("onos>")
478 else:
479 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400480 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400481 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
482 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400483 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400484 '''
485 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
486 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 -0400487 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 -0400488 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400489 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400490 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400491 '''
492 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400493 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
494 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400495 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400496 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400497 else:
498 if not grep_str:
499 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400500 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400501 else:
502 self.handle.sendline("devices | grep '"+
503 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400504 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400505 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400506 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400507 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400508 except pexpect.EOF:
509 main.log.error(self.name + ": EOF exception found")
510 main.log.error(self.name + ": " + self.handle.before)
511 main.cleanup()
512 main.exit()
513 except:
514 main.log.info(self.name+" ::::::")
515 main.log.error( traceback.print_exc())
516 main.log.info(self.name+" ::::::")
517 main.cleanup()
518 main.exit()
519
Jon Halle8217482014-10-17 13:49:14 -0400520 def links(self, json_format=True, grep_str=""):
521 '''
522 Lists all core links
523 Optional argument:
524 * grep_str - pass in a string to grep
525 '''
526 try:
527 self.handle.sendline("")
528 self.handle.expect("onos>")
529
530 if json_format:
531 if not grep_str:
532 self.handle.sendline("links -j")
533 self.handle.expect("links -j")
534 self.handle.expect("onos>")
535 else:
536 self.handle.sendline("links -j | grep '"+
537 str(grep_str)+"'")
538 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
539 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400540 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400541 '''
542 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
543 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 -0400544 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Halld815ce42014-10-17 19:52:30 -0400545 So we take off that escape sequence using
546 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
547 handle1 = ansi_escape.sub('', handle)
548 '''
549 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400550 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
551 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400552 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400553 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400554 else:
555 if not grep_str:
556 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400557 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400558 else:
559 self.handle.sendline("links | grep '"+
560 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400561 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400562 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400563 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400564 return handle
Jon Halle8217482014-10-17 13:49:14 -0400565 except pexpect.EOF:
566 main.log.error(self.name + ": EOF exception found")
567 main.log.error(self.name + ": " + self.handle.before)
568 main.cleanup()
569 main.exit()
570 except:
571 main.log.info(self.name+" ::::::")
572 main.log.error( traceback.print_exc())
573 main.log.info(self.name+" ::::::")
574 main.cleanup()
575 main.exit()
576
577
578 def ports(self, json_format=True, grep_str=""):
579 '''
580 Lists all ports
581 Optional argument:
582 * grep_str - pass in a string to grep
583 '''
584 try:
585 self.handle.sendline("")
586 self.handle.expect("onos>")
587
588 if json_format:
589 if not grep_str:
590 self.handle.sendline("ports -j")
591 self.handle.expect("ports -j")
592 self.handle.expect("onos>")
593 else:
594 self.handle.sendline("ports -j | grep '"+
595 str(grep_str)+"'")
596 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
597 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400598 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400599 '''
600 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
601 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 -0400602 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Shreya Shah0c525cc2014-10-17 20:20:24 -0400603 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400604 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
605 handle1 = ansi_escape.sub('', handle)
606 '''
607 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400608 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
609 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400610 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400611 return handle1
612
Jon Halle8217482014-10-17 13:49:14 -0400613 else:
614 if not grep_str:
615 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400616 self.handle.expect("onos>")
617 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400618 self.handle.expect("onos>")
619 else:
620 self.handle.sendline("ports | grep '"+
621 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400622 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400623 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400624 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400625 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400626 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400627 return handle
Jon Halle8217482014-10-17 13:49:14 -0400628 except pexpect.EOF:
629 main.log.error(self.name + ": EOF exception found")
630 main.log.error(self.name + ": " + self.handle.before)
631 main.cleanup()
632 main.exit()
633 except:
634 main.log.info(self.name+" ::::::")
635 main.log.error( traceback.print_exc())
636 main.log.info(self.name+" ::::::")
637 main.cleanup()
638 main.exit()
639
640
Jon Hall983a1702014-10-28 18:44:22 -0400641 def roles(self, json_format=True, grep_str=""):
andrewonlab7c211572014-10-15 16:45:20 -0400642 '''
Jon Hall983a1702014-10-28 18:44:22 -0400643 Lists all devices and the controllers with roles assigned to them
644 Optional argument:
645 * grep_str - pass in a string to grep
andrewonlab7c211572014-10-15 16:45:20 -0400646 '''
647 try:
648 self.handle.sendline("")
649 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500650
Jon Hall983a1702014-10-28 18:44:22 -0400651 if json_format:
Jon Hallb1290e82014-11-18 16:17:48 -0500652 self.handle.sendline("roles -j")
653 self.handle.expect("roles -j")
654 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400655 handle = self.handle.before
656 '''
Jon Hallb1290e82014-11-18 16:17:48 -0500657 handle variable here contains some ANSI escape color code sequences at the
658 end which are invisible in the print command output. To make that escape
659 sequence visible, use repr() function. The repr(handle) output when printed
660 shows the ANSI escape sequences. In json.loads(somestring), this somestring
661 variable is actually repr(somestring) and json.loads would fail with the escape
662 sequence.
663
664 So we take off that escape sequence using the following commads:
Jon Hall983a1702014-10-28 18:44:22 -0400665 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallb1290e82014-11-18 16:17:48 -0500666 handle1 = ansi_escape.sub('', handle)
Jon Hall983a1702014-10-28 18:44:22 -0400667 '''
668 #print "repr(handle) =", repr(handle)
669 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
670 handle1 = ansi_escape.sub('', handle)
671 #print "repr(handle1) = ", repr(handle1)
672 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400673
andrewonlab7c211572014-10-15 16:45:20 -0400674 else:
Jon Hallb1290e82014-11-18 16:17:48 -0500675 self.handle.sendline("roles")
676 self.handle.expect("onos>")
677 self.handle.sendline("")
678 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400679 handle = self.handle.before
680 #print "handle =",handle
681 return handle
682 except pexpect.EOF:
683 main.log.error(self.name + ": EOF exception found")
684 main.log.error(self.name + ": " + self.handle.before)
685 main.cleanup()
686 main.exit()
687 except:
688 main.log.info(self.name+" ::::::")
689 main.log.error( traceback.print_exc())
690 main.log.info(self.name+" ::::::")
691 main.cleanup()
692 main.exit()
693
694 def get_role(self, device_id):
695 '''
696 Given the a string containing the json representation of the "roles" cli command and a
697 partial or whole device id, returns a json object containing the
698 roles output for the first device whose id contains "device_id"
699
700 Returns:
701 Dict of the role assignments for the given device or
702 None if not match
703 '''
704 try:
705 import json
706 if device_id == None:
707 return None
708 else:
709 raw_roles = self.roles()
710 roles_json = json.loads(raw_roles)
711 #search json for the device with id then return the device
712 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400713 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400714 if str(device_id) in device['id']:
715 return device
716 return None
andrewonlab7c211572014-10-15 16:45:20 -0400717
andrewonlab86dc3082014-10-13 18:18:38 -0400718 except pexpect.EOF:
719 main.log.error(self.name + ": EOF exception found")
720 main.log.error(self.name + ": " + self.handle.before)
721 main.cleanup()
722 main.exit()
723 except:
724 main.log.info(self.name+" ::::::")
725 main.log.error( traceback.print_exc())
726 main.log.info(self.name+" ::::::")
727 main.cleanup()
728 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400729
andrewonlab3e15ead2014-10-15 14:21:34 -0400730 def paths(self, src_id, dst_id):
731 '''
732 Returns string of paths, and the cost.
733 Issues command: onos:paths <src> <dst>
734 '''
735 try:
736 self.handle.sendline("")
737 self.handle.expect("onos>")
738
739 self.handle.sendline("onos:paths "+
740 str(src_id) + " " + str(dst_id))
741 i = self.handle.expect([
742 "Error",
743 "onos>"])
744
745 self.handle.sendline("")
746 self.handle.expect("onos>")
747
748 handle = self.handle.before
749
750 if i == 0:
751 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400752 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400753 else:
754 path = handle.split(";")[0]
755 cost = handle.split(";")[1]
756 return (path, cost)
757
758 except pexpect.EOF:
759 main.log.error(self.name + ": EOF exception found")
760 main.log.error(self.name + ": " + self.handle.before)
761 main.cleanup()
762 main.exit()
763 except:
764 main.log.info(self.name+" ::::::")
765 main.log.error( traceback.print_exc())
766 main.log.info(self.name+" ::::::")
767 main.cleanup()
768 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400769
Jon Hall42db6dc2014-10-24 19:03:48 -0400770 def hosts(self, json_format=True, grep_str=""):
771 '''
772 Lists all discovered hosts
773 Optional argument:
774 * grep_str - pass in a string to grep
775 '''
776 try:
777 self.handle.sendline("")
778 self.handle.expect("onos>")
779
780 if json_format:
781 if not grep_str:
782 self.handle.sendline("hosts -j")
783 self.handle.expect("hosts -j")
784 self.handle.expect("onos>")
785 else:
786 self.handle.sendline("hosts -j | grep '"+
787 str(grep_str)+"'")
788 self.handle.expect("hosts -j | grep '"+str(grep_str)+"'")
789 self.handle.expect("onos>")
790 handle = self.handle.before
791 '''
792 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
793 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
794 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
795 So we take off that escape sequence using
796 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
797 handle1 = ansi_escape.sub('', handle)
798 '''
799 #print "repr(handle) =", repr(handle)
800 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
801 handle1 = ansi_escape.sub('', handle)
802 #print "repr(handle1) = ", repr(handle1)
803 return handle1
804 else:
805 if not grep_str:
806 self.handle.sendline("hosts")
807 self.handle.expect("onos>")
808 else:
809 self.handle.sendline("hosts | grep '"+
810 str(grep_str)+"'")
811 self.handle.expect("onos>")
812 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400813 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400814 return handle
815 except pexpect.EOF:
816 main.log.error(self.name + ": EOF exception found")
817 main.log.error(self.name + ": " + self.handle.before)
818 main.cleanup()
819 main.exit()
820 except:
821 main.log.info(self.name+" ::::::")
822 main.log.error( traceback.print_exc())
823 main.log.info(self.name+" ::::::")
824 main.cleanup()
825 main.exit()
826
827 def get_host(self, mac):
828 '''
829 Return the first host from the hosts api whose 'id' contains 'mac'
830 Note: mac must be a colon seperated mac address, but could be a partial mac address
831 Return None if there is no match
832 '''
833 import json
834 try:
835 if mac == None:
836 return None
837 else:
838 mac = mac
839 raw_hosts = self.hosts()
840 hosts_json = json.loads(raw_hosts)
841 #search json for the host with mac then return the device
842 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400843 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400844 if mac in host['id']:
845 return host
846 return None
847 except pexpect.EOF:
848 main.log.error(self.name + ": EOF exception found")
849 main.log.error(self.name + ": " + self.handle.before)
850 main.cleanup()
851 main.exit()
852 except:
853 main.log.info(self.name+" ::::::")
854 main.log.error( traceback.print_exc())
855 main.log.info(self.name+" ::::::")
856 main.cleanup()
857 main.exit()
858
andrewonlab3f0a4af2014-10-17 12:25:14 -0400859
860 def get_hosts_id(self, host_list):
861 '''
862 Obtain list of hosts
863 Issues command: 'onos> hosts'
864
865 Required:
866 * host_list: List of hosts obtained by Mininet
867 IMPORTANT:
868 This function assumes that you started your
869 topology with the option '--mac'.
870 Furthermore, it assumes that value of VLAN is '-1'
871 Description:
872 Converts mininet hosts (h1, h2, h3...) into
873 ONOS format (00:00:00:00:00:01/-1 , ...)
874 '''
875
876 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400877 onos_host_list = []
878
879 for host in host_list:
880 host = host.replace("h", "")
881 host_hex = hex(int(host)).zfill(12)
882 host_hex = str(host_hex).replace('x','0')
883 i = iter(str(host_hex))
884 host_hex = ":".join(a+b for a,b in zip(i,i))
885 host_hex = host_hex + "/-1"
886 onos_host_list.append(host_hex)
887
888 return onos_host_list
889
890 except pexpect.EOF:
891 main.log.error(self.name + ": EOF exception found")
892 main.log.error(self.name + ": " + self.handle.before)
893 main.cleanup()
894 main.exit()
895 except:
896 main.log.info(self.name+" ::::::")
897 main.log.error( traceback.print_exc())
898 main.log.info(self.name+" ::::::")
899 main.cleanup()
900 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400901
andrewonlabe6745342014-10-17 14:29:13 -0400902 def add_host_intent(self, host_id_one, host_id_two):
903 '''
904 Required:
905 * host_id_one: ONOS host id for host1
906 * host_id_two: ONOS host id for host2
907 Description:
908 Adds a host-to-host intent (bidrectional) by
Jon Hallb1290e82014-11-18 16:17:48 -0500909 specifying the two hosts.
andrewonlabe6745342014-10-17 14:29:13 -0400910 '''
911 try:
912 self.handle.sendline("")
913 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500914
andrewonlabe6745342014-10-17 14:29:13 -0400915 self.handle.sendline("add-host-intent "+
916 str(host_id_one) + " " + str(host_id_two))
917 self.handle.expect("onos>")
918
andrewonlabe6745342014-10-17 14:29:13 -0400919 handle = self.handle.before
Shreya Shah4f25fdf2014-10-29 19:55:35 -0400920 print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400921
922 main.log.info("Intent installed between "+
923 str(host_id_one) + " and " + str(host_id_two))
924
925 return handle
Jon Hallb1290e82014-11-18 16:17:48 -0500926
andrewonlabe6745342014-10-17 14:29:13 -0400927 except pexpect.EOF:
928 main.log.error(self.name + ": EOF exception found")
929 main.log.error(self.name + ": " + self.handle.before)
930 main.cleanup()
931 main.exit()
932 except:
933 main.log.info(self.name+" ::::::")
934 main.log.error( traceback.print_exc())
935 main.log.info(self.name+" ::::::")
936 main.cleanup()
937 main.exit()
938
andrewonlab7b31d232014-10-24 13:31:47 -0400939 def add_optical_intent(self, ingress_device, egress_device):
940 '''
941 Required:
942 * ingress_device: device id of ingress device
943 * egress_device: device id of egress device
944 Optional:
945 TODO: Still needs to be implemented via dev side
946 '''
947 try:
948 self.handle.sendline("add-optical-intent "+
949 str(ingress_device) + " " + str(egress_device))
950 self.handle.expect("add-optical-intent")
951 i = self.handle.expect([
952 "Error",
953 "onos>"])
954
955 handle = self.handle.before
956
957 #If error, return error message
958 if i == 0:
959 return handle
960 else:
961 return main.TRUE
962
963 except pexpect.EOF:
964 main.log.error(self.name + ": EOF exception found")
965 main.log.error(self.name + ": " + self.handle.before)
966 main.cleanup()
967 main.exit()
968 except:
969 main.log.info(self.name+" ::::::")
970 main.log.error( traceback.print_exc())
971 main.log.info(self.name+" ::::::")
972 main.cleanup()
973 main.exit()
974
andrewonlab4dbb4d82014-10-17 18:22:31 -0400975 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400976 egress_device, port_egress, ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -0500977 ethDst="", bandwidth="", lambda_alloc=False,
978 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400979 '''
980 Required:
981 * ingress_device: device id of ingress device
982 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400983 Optional:
984 * ethType: specify ethType
985 * ethSrc: specify ethSrc (i.e. src mac addr)
986 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500987 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -0500988 * lambda_alloc: if True, intent will allocate lambda
989 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -0500990 * ipProto: specify ip protocol
991 * ipSrc: specify ip source address
992 * ipDst: specify ip destination address
993 * tcpSrc: specify tcp source port
994 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400995 Description:
996 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400997 specifying device id's and optional fields
998
andrewonlab4dbb4d82014-10-17 18:22:31 -0400999 NOTE: This function may change depending on the
1000 options developers provide for point-to-point
1001 intent via cli
1002 '''
1003 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001004 cmd = ""
1005
1006 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001007 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001008 and not bandwidth and not lambda_alloc \
1009 and not ipProto and not ipSrc and not ipDst \
1010 and not tcpSrc and not tcpDst:
andrewonlab289e4b72014-10-21 21:24:18 -04001011 cmd = "add-point-intent "+\
1012 str(ingress_device) + "/" + str(port_ingress) + " " +\
1013 str(egress_device) + "/" + str(port_egress)
1014
1015 else:
andrewonlab9a130be2014-10-22 12:44:56 -04001016 cmd = "add-point-intent "
1017
andrewonlab0c0a6772014-10-22 12:31:18 -04001018 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001019 cmd += " --ethType " + str(ethType)
1020 if ethSrc:
1021 cmd += " --ethSrc " + str(ethSrc)
1022 if ethDst:
1023 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001024 if bandwidth:
1025 cmd += " --bandwidth " + str(bandwidth)
1026 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001027 cmd += " --lambda "
1028 if ipProto:
1029 cmd += " --ipProto " + str(ipProto)
1030 if ipSrc:
1031 cmd += " --ipSrc " + str(ipSrc)
1032 if ipDst:
1033 cmd += " --ipDst " + str(ipDst)
1034 if tcpSrc:
1035 cmd += " --tcpSrc " + str(tcpSrc)
1036 if tcpDst:
1037 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001038
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001039 cmd += " "+str(ingress_device) +\
1040 "/" + str(port_ingress) + " " +\
1041 str(egress_device) + "/" + str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001042
andrewonlab289e4b72014-10-21 21:24:18 -04001043 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001044 i = self.handle.expect([
1045 "Error",
1046 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -04001047
Shreya Shah0f01c812014-10-26 20:15:28 -04001048 self.handle.sendline("intents")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001049 self.handle.expect("onos>")
Shreya Shah0f01c812014-10-26 20:15:28 -04001050 Intenthandle = self.handle.before
andrewonlab4dbb4d82014-10-17 18:22:31 -04001051
1052 if i == 0:
1053 main.log.error("Error in adding point-to-point intent")
1054 return handle
1055 else:
1056 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001057
andrewonlab4dbb4d82014-10-17 18:22:31 -04001058 except pexpect.EOF:
1059 main.log.error(self.name + ": EOF exception found")
1060 main.log.error(self.name + ": " + self.handle.before)
1061 main.cleanup()
1062 main.exit()
1063 except:
1064 main.log.info(self.name+" ::::::")
1065 main.log.error( traceback.print_exc())
1066 main.log.info(self.name+" ::::::")
1067 main.cleanup()
1068 main.exit()
1069
andrewonlab9a50dfe2014-10-17 17:22:31 -04001070 def remove_intent(self, intent_id):
1071 '''
1072 Remove intent for specified intent id
1073 '''
1074 try:
1075 self.handle.sendline("")
1076 self.handle.expect("onos>")
1077
1078 self.handle.sendline("remove-intent "+str(intent_id))
1079 i = self.handle.expect([
1080 "Error",
1081 "onos>"])
1082
1083 handle = self.handle.before
1084
1085 if i == 0:
1086 main.log.error("Error in removing intent")
1087 return handle
1088 else:
1089 return handle
1090
1091 except pexpect.EOF:
1092 main.log.error(self.name + ": EOF exception found")
1093 main.log.error(self.name + ": " + self.handle.before)
1094 main.cleanup()
1095 main.exit()
1096 except:
1097 main.log.info(self.name+" ::::::")
1098 main.log.error( traceback.print_exc())
1099 main.log.info(self.name+" ::::::")
1100 main.cleanup()
1101 main.exit()
1102
andrewonlab377693f2014-10-21 16:00:30 -04001103 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001104 '''
andrewonlab377693f2014-10-21 16:00:30 -04001105 Optional:
1106 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001107 Description:
1108 Obtain intents currently installed
1109 '''
1110 try:
andrewonlab377693f2014-10-21 16:00:30 -04001111 if json_format:
1112 self.handle.sendline("intents -j")
1113 self.handle.expect("intents -j")
1114 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001115 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001116
1117 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1118 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001119 else:
1120 self.handle.sendline("")
1121 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001122
andrewonlab377693f2014-10-21 16:00:30 -04001123 self.handle.sendline("intents")
1124 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001125 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001126
1127 return handle
1128
1129 except pexpect.EOF:
1130 main.log.error(self.name + ": EOF exception found")
1131 main.log.error(self.name + ": " + self.handle.before)
1132 main.cleanup()
1133 main.exit()
1134 except:
1135 main.log.info(self.name+" ::::::")
1136 main.log.error( traceback.print_exc())
1137 main.log.info(self.name+" ::::::")
1138 main.cleanup()
1139 main.exit()
1140
Shreya Shah0f01c812014-10-26 20:15:28 -04001141 def flows(self, json_format = False):
1142 '''
1143 Optional:
1144 * json_format: enable output formatting in json
1145 Description:
1146 Obtain flows currently installed
1147 '''
1148 try:
1149 if json_format:
1150 self.handle.sendline("flows -j")
1151 self.handle.expect("flows -j")
1152 self.handle.expect("onos>")
1153 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001154 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1155 handle = ansi_escape.sub('', handle)
Shreya Shah0f01c812014-10-26 20:15:28 -04001156
1157 else:
1158 self.handle.sendline("")
1159 self.handle.expect("onos>")
1160 self.handle.sendline("flows")
1161 self.handle.expect("onos>")
1162 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001163 if re.search("Error\sexecuting\scommand:", handle):
1164 main.log.error(self.name + ".flows() response: " + str(handle))
Shreya Shah0f01c812014-10-26 20:15:28 -04001165
1166 return handle
1167
1168 except pexpect.EOF:
1169 main.log.error(self.name + ": EOF exception found")
1170 main.log.error(self.name + ": " + self.handle.before)
1171 main.cleanup()
1172 main.exit()
1173 except:
1174 main.log.info(self.name+" ::::::")
1175 main.log.error( traceback.print_exc())
1176 main.log.info(self.name+" ::::::")
1177 main.cleanup()
1178 main.exit()
1179
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001180 def intents_events_metrics(self, json_format=True):
1181 '''
1182 Description:Returns topology metrics
1183 Optional:
1184 * json_format: enable json formatting of output
1185 '''
1186 try:
1187 if json_format:
1188 self.handle.sendline("intents-events-metrics -j")
1189 self.handle.expect("intents-events-metrics -j")
1190 self.handle.expect("onos>")
1191
1192 handle = self.handle.before
1193
1194 #Some color thing that we want to escape
1195 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1196 handle = ansi_escape.sub('', handle)
1197
1198 else:
1199 self.handle.sendline("intents-events-metrics")
1200 self.handle.expect("intents-events-metrics")
1201 self.handle.expect("onos>")
1202
1203 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001204
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001205 return handle
1206
1207 except pexpect.EOF:
1208 main.log.error(self.name + ": EOF exception found")
1209 main.log.error(self.name + ": " + self.handle.before)
1210 main.cleanup()
1211 main.exit()
1212 except:
1213 main.log.info(self.name+" ::::::")
1214 main.log.error( traceback.print_exc())
1215 main.log.info(self.name+" ::::::")
1216 main.cleanup()
1217 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001218
andrewonlab867212a2014-10-22 20:13:38 -04001219 def topology_events_metrics(self, json_format=True):
1220 '''
1221 Description:Returns topology metrics
1222 Optional:
1223 * json_format: enable json formatting of output
1224 '''
1225 try:
1226 if json_format:
1227 self.handle.sendline("topology-events-metrics -j")
1228 self.handle.expect("topology-events-metrics -j")
1229 self.handle.expect("onos>")
1230
1231 handle = self.handle.before
1232
1233 #Some color thing that we want to escape
1234 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1235 handle = ansi_escape.sub('', handle)
1236
1237 else:
1238 self.handle.sendline("topology-events-metrics")
1239 self.handle.expect("topology-events-metrics")
1240 self.handle.expect("onos>")
1241
1242 handle = self.handle.before
1243
1244 return handle
1245
1246 except pexpect.EOF:
1247 main.log.error(self.name + ": EOF exception found")
1248 main.log.error(self.name + ": " + self.handle.before)
1249 main.cleanup()
1250 main.exit()
1251 except:
1252 main.log.info(self.name+" ::::::")
1253 main.log.error( traceback.print_exc())
1254 main.log.info(self.name+" ::::::")
1255 main.cleanup()
1256 main.exit()
1257
andrewonlab3e15ead2014-10-15 14:21:34 -04001258 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001259 #Wrapper functions use existing driver
1260 #functions and extends their use case.
1261 #For example, we may use the output of
1262 #a normal driver function, and parse it
1263 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001264
andrewonlab9a50dfe2014-10-17 17:22:31 -04001265 def get_all_intents_id(self):
1266 '''
1267 Description:
1268 Obtain all intent id's in a list
1269 '''
1270 try:
1271 #Obtain output of intents function
1272 intents_str = self.intents()
1273 all_intent_list = []
1274 intent_id_list = []
1275
1276 #Parse the intents output for ID's
1277 intents_list = [s.strip() for s in intents_str.splitlines()]
1278 for intents in intents_list:
1279 if "onos>" in intents:
1280 continue
1281 elif "intents" in intents:
1282 continue
1283 else:
1284 line_list = intents.split(" ")
1285 all_intent_list.append(line_list[0])
1286
1287 all_intent_list = all_intent_list[1:-2]
1288
1289 for intents in all_intent_list:
1290 if not intents:
1291 continue
1292 else:
1293 intent_id_list.append(intents)
1294
1295 return intent_id_list
1296
1297 except pexpect.EOF:
1298 main.log.error(self.name + ": EOF exception found")
1299 main.log.error(self.name + ": " + self.handle.before)
1300 main.cleanup()
1301 main.exit()
1302 except:
1303 main.log.info(self.name+" ::::::")
1304 main.log.error( traceback.print_exc())
1305 main.log.info(self.name+" ::::::")
1306 main.cleanup()
1307 main.exit()
1308
andrewonlab7e4d2d32014-10-15 13:23:21 -04001309 def get_all_devices_id(self):
1310 '''
1311 Use 'devices' function to obtain list of all devices
1312 and parse the result to obtain a list of all device
1313 id's. Returns this list. Returns empty list if no
1314 devices exist
1315 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001316
1317 This function may be useful if you are not sure of the
1318 device id, and wish to execute other commands using
1319 the ids. By obtaining the list of device ids on the fly,
1320 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001321 '''
1322 try:
1323 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001324 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001325 id_list = []
1326
1327 if not devices_str:
1328 main.log.info("There are no devices to get id from")
1329 return id_list
1330
1331 #Split the string into list by comma
1332 device_list = devices_str.split(",")
1333 #Get temporary list of all arguments with string 'id='
1334 temp_list = [dev for dev in device_list if "id=" in dev]
1335 #Split list further into arguments before and after string
1336 # 'id='. Get the latter portion (the actual device id) and
1337 # append to id_list
1338 for arg in temp_list:
1339 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001340 return id_list
1341
1342 except pexpect.EOF:
1343 main.log.error(self.name + ": EOF exception found")
1344 main.log.error(self.name + ": " + self.handle.before)
1345 main.cleanup()
1346 main.exit()
1347 except:
1348 main.log.info(self.name+" ::::::")
1349 main.log.error( traceback.print_exc())
1350 main.log.info(self.name+" ::::::")
1351 main.cleanup()
1352 main.exit()
1353
andrewonlab7c211572014-10-15 16:45:20 -04001354 def get_all_nodes_id(self):
1355 '''
1356 Uses 'nodes' function to obtain list of all nodes
1357 and parse the result of nodes to obtain just the
1358 node id's.
1359 Returns:
1360 list of node id's
1361 '''
1362 try:
1363 nodes_str = self.nodes()
1364 id_list = []
1365
1366 if not nodes_str:
1367 main.log.info("There are no nodes to get id from")
1368 return id_list
1369
1370 #Sample nodes_str output
1371 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1372
1373 #Split the string into list by comma
1374 nodes_list = nodes_str.split(",")
1375 temp_list = [node for node in nodes_list if "id=" in node]
1376 for arg in temp_list:
1377 id_list.append(arg.split("id=")[1])
1378
1379 return id_list
1380
1381 except pexpect.EOF:
1382 main.log.error(self.name + ": EOF exception found")
1383 main.log.error(self.name + ": " + self.handle.before)
1384 main.cleanup()
1385 main.exit()
1386 except:
1387 main.log.info(self.name+" ::::::")
1388 main.log.error( traceback.print_exc())
1389 main.log.info(self.name+" ::::::")
1390 main.cleanup()
1391 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001392
Jon Halla91c4dc2014-10-22 12:57:04 -04001393 def get_device(self, dpid=None):
1394 '''
1395 Return the first device from the devices api whose 'id' contains 'dpid'
1396 Return None if there is no match
1397 '''
1398 import json
1399 try:
1400 if dpid == None:
1401 return None
1402 else:
1403 dpid = dpid.replace(':', '')
1404 raw_devices = self.devices()
1405 devices_json = json.loads(raw_devices)
1406 #search json for the device with dpid then return the device
1407 for device in devices_json:
1408 #print "%s in %s?" % (dpid, device['id'])
1409 if dpid in device['id']:
1410 return device
1411 return None
1412 except pexpect.EOF:
1413 main.log.error(self.name + ": EOF exception found")
1414 main.log.error(self.name + ": " + self.handle.before)
1415 main.cleanup()
1416 main.exit()
1417 except:
1418 main.log.info(self.name+" ::::::")
1419 main.log.error( traceback.print_exc())
1420 main.log.info(self.name+" ::::::")
1421 main.cleanup()
1422 main.exit()
1423
Jon Hall42db6dc2014-10-24 19:03:48 -04001424 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1425 '''
1426 Checks the number of swithes & links that ONOS sees against the
1427 supplied values. By default this will report to main.log, but the
1428 log level can be specifid.
1429
1430 Params: ip = ip used for the onos cli
1431 numoswitch = expected number of switches
1432 numlink = expected number of links
1433 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1434
1435
1436 log_level can
1437
1438 Returns: main.TRUE if the number of switchs and links are correct,
1439 main.FALSE if the numer of switches and links is incorrect,
1440 and main.ERROR otherwise
1441 '''
1442
1443 try:
1444 topology = self.get_topology(ip)
1445 if topology == {}:
1446 return main.ERROR
1447 output = ""
1448 #Is the number of switches is what we expected
1449 devices = topology.get('devices',False)
1450 links = topology.get('links',False)
1451 if devices == False or links == False:
1452 return main.ERROR
1453 switch_check = ( int(devices) == int(numoswitch) )
1454 #Is the number of links is what we expected
1455 link_check = ( int(links) == int(numolink) )
1456 if (switch_check and link_check):
1457 #We expected the correct numbers
1458 output = output + "The number of links and switches match "\
1459 + "what was expected"
1460 result = main.TRUE
1461 else:
1462 output = output + \
1463 "The number of links and switches does not match what was expected"
1464 result = main.FALSE
1465 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1466 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1467 if log_level == "report":
1468 main.log.report(output)
1469 elif log_level == "warn":
1470 main.log.warn(output)
1471 else:
1472 main.log.info(output)
1473 return result
1474 except pexpect.EOF:
1475 main.log.error(self.name + ": EOF exception found")
1476 main.log.error(self.name + ": " + self.handle.before)
1477 main.cleanup()
1478 main.exit()
1479 except:
1480 main.log.info(self.name+" ::::::")
1481 main.log.error( traceback.print_exc())
1482 main.log.info(self.name+" ::::::")
1483 main.cleanup()
1484 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001485
1486 def device_role(self, device_id, onos_node, role="master"):
1487 '''
1488 Calls the device-role cli command.
1489 device_id must be the id of a device as seen in the onos devices command
1490 onos_node is the ip of one of the onos nodes in the cluster
1491 role must be either master, standby, or none
1492
Jon Hall983a1702014-10-28 18:44:22 -04001493 Returns main.TRUE or main.FALSE based argument varification.
1494 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001495 support that output
1496 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001497 try:
Jon Hall983a1702014-10-28 18:44:22 -04001498 #print "beginning device_role... \n\tdevice_id:" + device_id
1499 #print "\tonos_node: " + onos_node
1500 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001501 if role.lower() == "master" or \
1502 role.lower() == "standby" or \
1503 role.lower() == "none":
1504 self.handle.sendline("")
1505 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001506 self.handle.sendline("device-role " +
1507 str(device_id) + " " +
1508 str(onos_node) + " " +
1509 str(role))
1510 i= self.handle.expect(["Error","onos>"])
1511 if i == 0:
1512 output = str(self.handle.before)
1513 self.handle.expect("onos>")
1514 output = output + str(self.handle.before)
1515 main.log.error(self.name + ": " +
1516 output + '\033[0m')#end color output to escape any colours from the cli
1517 return main.ERROR
1518 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001519 self.handle.expect("onos>")
1520 return main.TRUE
1521 else:
1522 return main.FALSE
1523
1524 except pexpect.EOF:
1525 main.log.error(self.name + ": EOF exception found")
1526 main.log.error(self.name + ": " + self.handle.before)
1527 main.cleanup()
1528 main.exit()
1529 except:
1530 main.log.info(self.name+" ::::::")
1531 main.log.error( traceback.print_exc())
1532 main.log.info(self.name+" ::::::")
1533 main.cleanup()
1534 main.exit()
1535
1536
andrewonlab7e4d2d32014-10-15 13:23:21 -04001537 #***********************************