blob: e2e08bdd24f718ffe014c6d0a0d1e30b11bcd04a [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:
74 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
75 main.log.error( traceback.print_exc() )
76 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
77 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:
andrewonlab38d2b4a2014-11-13 16:28:47 -0500111 self.handle.sendline("logout")
112 self.handle.expect("\$")
113
114 except pexpect.EOF:
115 main.log.error(self.name + ": eof exception found")
116 main.log.error(self.name + ": " + self.handle.before)
117 main.cleanup()
118 main.exit()
119 except:
120 main.log.info(self.name+" ::::::")
121 main.log.error( traceback.print_exc())
122 main.log.info(self.name+" ::::::")
123 main.cleanup()
124 main.exit()
125
andrewonlab95ce8322014-10-13 14:12:04 -0400126 def set_cell(self, cellname):
127 '''
128 Calls 'cell <name>' to set the environment variables on ONOSbench
129
130 Before issuing any cli commands, set the environment variable first.
131 '''
132 try:
133 if not cellname:
134 main.log.error("Must define cellname")
135 main.cleanup()
136 main.exit()
137 else:
138 self.handle.sendline("cell "+str(cellname))
139 #Expect the cellname in the ONOS_CELL variable.
140 #Note that this variable name is subject to change
141 # and that this driver will have to change accordingly
142 self.handle.expect("ONOS_CELL="+str(cellname))
143 handle_before = self.handle.before
144 handle_after = self.handle.after
145 #Get the rest of the handle
146 self.handle.sendline("")
147 self.handle.expect("\$")
148 handle_more = self.handle.before
149
150 main.log.info("Cell call returned: "+handle_before+
151 handle_after + handle_more)
152
153 return main.TRUE
154
155 except pexpect.EOF:
andrewonlab38d2b4a2014-11-13 16:28:47 -0500156 main.log.error(self.name + ": eof exception found")
andrewonlab95ce8322014-10-13 14:12:04 -0400157 main.log.error(self.name + ": " + self.handle.before)
158 main.cleanup()
159 main.exit()
160 except:
161 main.log.info(self.name+" ::::::")
162 main.log.error( traceback.print_exc())
163 main.log.info(self.name+" ::::::")
164 main.cleanup()
165 main.exit()
166
andrewonlabc2d05aa2014-10-13 16:51:10 -0400167 def start_onos_cli(self, ONOS_ip):
andrewonlab95ce8322014-10-13 14:12:04 -0400168 try:
169 self.handle.sendline("")
170 self.handle.expect("\$")
171
172 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400173 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400174 i = self.handle.expect([
175 "onos>",
176 pexpect.TIMEOUT],timeout=60)
177
178 if i == 0:
179 main.log.info(str(ONOS_ip)+" CLI Started successfully")
180 return main.TRUE
181 else:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400182 #If failed, send ctrl+c to process and try again
andrewonlabf47993a2014-10-24 17:56:01 -0400183 main.log.info("Starting CLI failed. Retrying...")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400184 self.handle.sendline("\x03")
185 self.handle.sendline("onos -w "+str(ONOS_ip))
186 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
187 timeout=30)
188 if i == 0:
189 return main.TRUE
190 else:
191 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400192 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400193 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400194
195 except pexpect.EOF:
196 main.log.error(self.name + ": EOF exception found")
197 main.log.error(self.name + ": " + self.handle.before)
198 main.cleanup()
199 main.exit()
200 except:
201 main.log.info(self.name+" ::::::")
202 main.log.error( traceback.print_exc())
203 main.log.info(self.name+" ::::::")
204 main.cleanup()
205 main.exit()
206
andrewonlaba18f6bf2014-10-13 19:31:54 -0400207 def sendline(self, cmd_str):
208 '''
209 Send a completely user specified string to
210 the onos> prompt. Use this function if you have
211 a very specific command to send.
212
213 Warning: There are no sanity checking to commands
214 sent using this method.
215 '''
216 try:
217 self.handle.sendline("")
218 self.handle.expect("onos>")
219
220 self.handle.sendline(cmd_str)
221 self.handle.expect("onos>")
222
223 handle = self.handle.before
224
225 self.handle.sendline("")
226 self.handle.expect("onos>")
227
228 handle += self.handle.before
229 handle += self.handle.after
230
231 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400232 ansi_escape = re.compile(r'\x1b[^m]*m')
233 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400234
235 return handle
236 except pexpect.EOF:
237 main.log.error(self.name + ": EOF exception found")
238 main.log.error(self.name + ": " + self.handle.before)
239 main.cleanup()
240 main.exit()
241 except:
242 main.log.info(self.name+" ::::::")
243 main.log.error( traceback.print_exc())
244 main.log.info(self.name+" ::::::")
245 main.cleanup()
246 main.exit()
247
andrewonlab95ce8322014-10-13 14:12:04 -0400248 #IMPORTANT NOTE:
249 #For all cli commands, naming convention should match
250 #the cli command replacing ':' with '_'.
251 #Ex) onos:topology > onos_topology
252 # onos:links > onos_links
253 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400254
255 def add_node(self, node_id, ONOS_ip, tcp_port=""):
256 '''
257 Adds a new cluster node by ID and address information.
258 Required:
259 * node_id
260 * ONOS_ip
261 Optional:
262 * tcp_port
263 '''
264 try:
265 self.handle.sendline("")
266 self.handle.expect("onos>")
267
268 self.handle.sendline("add-node "+
269 str(node_id)+" "+
270 str(ONOS_ip)+" "+
271 str(tcp_port))
272
273 i = self.handle.expect([
274 "Error",
275 "onos>" ])
276
277 #Clear handle to get previous output
278 self.handle.sendline("")
279 self.handle.expect("onos>")
280
281 handle = self.handle.before
282
283 if i == 0:
284 main.log.error("Error in adding node")
285 main.log.error(handle)
286 return main.FALSE
287 else:
288 main.log.info("Node "+str(ONOS_ip)+" added")
289 return main.TRUE
290
291 except pexpect.EOF:
292 main.log.error(self.name + ": EOF exception found")
293 main.log.error(self.name + ": " + self.handle.before)
294 main.cleanup()
295 main.exit()
296 except:
297 main.log.info(self.name+" ::::::")
298 main.log.error( traceback.print_exc())
299 main.log.info(self.name+" ::::::")
300 main.cleanup()
301 main.exit()
302
andrewonlab86dc3082014-10-13 18:18:38 -0400303 def remove_node(self, node_id):
304 '''
305 Removes a cluster by ID
306 Issues command: 'remove-node [<node-id>]'
307 Required:
308 * node_id
309 '''
310 try:
311 self.handle.sendline("")
312 self.handle.expect("onos>")
313
314 self.handle.sendline("remove-node "+str(node_id))
315 self.handle.expect("onos>")
316
317 return main.TRUE
318
319 except pexpect.EOF:
320 main.log.error(self.name + ": EOF exception found")
321 main.log.error(self.name + ": " + self.handle.before)
322 main.cleanup()
323 main.exit()
324 except:
325 main.log.info(self.name+" ::::::")
326 main.log.error( traceback.print_exc())
327 main.log.info(self.name+" ::::::")
328 main.cleanup()
329 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400330
andrewonlab7c211572014-10-15 16:45:20 -0400331 def nodes(self):
332 '''
333 List the nodes currently visible
334 Issues command: 'nodes'
335 Returns: entire handle of list of nodes
336 '''
337 try:
338 self.handle.sendline("")
339 self.handle.expect("onos>")
340
341 self.handle.sendline("nodes")
342 self.handle.expect("onos>")
343
344 self.handle.sendline("")
345 self.handle.expect("onos>")
346
347 handle = self.handle.before
348
349 return handle
350
351 except pexpect.EOF:
352 main.log.error(self.name + ": EOF exception found")
353 main.log.error(self.name + ": " + self.handle.before)
354 main.cleanup()
355 main.exit()
356 except:
357 main.log.info(self.name+" ::::::")
358 main.log.error( traceback.print_exc())
359 main.log.info(self.name+" ::::::")
360 main.cleanup()
361 main.exit()
362
andrewonlab38d6ae22014-10-15 14:23:45 -0400363 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400364 '''
365 Shows the current state of the topology
366 by issusing command: 'onos> onos:topology'
367 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400368 try:
369 self.handle.sendline("")
370 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400371 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400372 self.handle.sendline("onos:topology")
373 self.handle.expect("onos>")
374
375 handle = self.handle.before
376
377 main.log.info("onos:topology returned: " +
378 str(handle))
379
380 return handle
381
382 except pexpect.EOF:
383 main.log.error(self.name + ": EOF exception found")
384 main.log.error(self.name + ": " + self.handle.before)
385 main.cleanup()
386 main.exit()
387 except:
388 main.log.info(self.name+" ::::::")
389 main.log.error( traceback.print_exc())
390 main.log.info(self.name+" ::::::")
391 main.cleanup()
392 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400393
394 def feature_install(self, feature_str):
395 '''
396 Installs a specified feature
397 by issuing command: 'onos> feature:install <feature_str>'
398 '''
399 try:
400 self.handle.sendline("")
401 self.handle.expect("onos>")
402
403 self.handle.sendline("feature:install "+str(feature_str))
404 self.handle.expect("onos>")
405
406 return main.TRUE
407
408 except pexpect.EOF:
409 main.log.error(self.name + ": EOF exception found")
410 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500411 main.log.report("Failed to install feature")
412 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400413 main.cleanup()
414 main.exit()
415 except:
416 main.log.info(self.name+" ::::::")
417 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500418 main.log.report("Failed to install feature")
419 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400420 main.log.info(self.name+" ::::::")
421 main.cleanup()
422 main.exit()
423
424 def feature_uninstall(self, feature_str):
425 '''
426 Uninstalls a specified feature
427 by issuing command: 'onos> feature:uninstall <feature_str>'
428 '''
429 try:
430 self.handle.sendline("")
431 self.handle.expect("onos>")
432
433 self.handle.sendline("feature:uninstall "+str(feature_str))
434 self.handle.expect("onos>")
435
436 return main.TRUE
437
438 except pexpect.EOF:
439 main.log.error(self.name + ": EOF exception found")
440 main.log.error(self.name + ": " + self.handle.before)
441 main.cleanup()
442 main.exit()
443 except:
444 main.log.info(self.name+" ::::::")
445 main.log.error( traceback.print_exc())
446 main.log.info(self.name+" ::::::")
447 main.cleanup()
448 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400449
Jon Halle8217482014-10-17 13:49:14 -0400450 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400451 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400452 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400453 Optional argument:
454 * grep_str - pass in a string to grep
455 '''
456 try:
457 self.handle.sendline("")
458 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400459
460 if json_format:
461 if not grep_str:
462 self.handle.sendline("devices -j")
463 self.handle.expect("devices -j")
464 self.handle.expect("onos>")
465 else:
466 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400467 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400468 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
469 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400470 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400471 '''
472 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
473 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 -0400474 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 -0400475 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400476 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400477 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400478 '''
479 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400480 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
481 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400482 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400483 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400484 else:
485 if not grep_str:
486 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400487 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400488 else:
489 self.handle.sendline("devices | grep '"+
490 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400491 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400492 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400493 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400494 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400495 except pexpect.EOF:
496 main.log.error(self.name + ": EOF exception found")
497 main.log.error(self.name + ": " + self.handle.before)
498 main.cleanup()
499 main.exit()
500 except:
501 main.log.info(self.name+" ::::::")
502 main.log.error( traceback.print_exc())
503 main.log.info(self.name+" ::::::")
504 main.cleanup()
505 main.exit()
506
Jon Halle8217482014-10-17 13:49:14 -0400507 def links(self, json_format=True, grep_str=""):
508 '''
509 Lists all core links
510 Optional argument:
511 * grep_str - pass in a string to grep
512 '''
513 try:
514 self.handle.sendline("")
515 self.handle.expect("onos>")
516
517 if json_format:
518 if not grep_str:
519 self.handle.sendline("links -j")
520 self.handle.expect("links -j")
521 self.handle.expect("onos>")
522 else:
523 self.handle.sendline("links -j | grep '"+
524 str(grep_str)+"'")
525 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
526 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400527 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400528 '''
529 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
530 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 -0400531 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 -0400532 So we take off that escape sequence using
533 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
534 handle1 = ansi_escape.sub('', handle)
535 '''
536 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400537 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
538 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400539 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400540 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400541 else:
542 if not grep_str:
543 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400544 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400545 else:
546 self.handle.sendline("links | grep '"+
547 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400548 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400549 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400550 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400551 return handle
Jon Halle8217482014-10-17 13:49:14 -0400552 except pexpect.EOF:
553 main.log.error(self.name + ": EOF exception found")
554 main.log.error(self.name + ": " + self.handle.before)
555 main.cleanup()
556 main.exit()
557 except:
558 main.log.info(self.name+" ::::::")
559 main.log.error( traceback.print_exc())
560 main.log.info(self.name+" ::::::")
561 main.cleanup()
562 main.exit()
563
564
565 def ports(self, json_format=True, grep_str=""):
566 '''
567 Lists all ports
568 Optional argument:
569 * grep_str - pass in a string to grep
570 '''
571 try:
572 self.handle.sendline("")
573 self.handle.expect("onos>")
574
575 if json_format:
576 if not grep_str:
577 self.handle.sendline("ports -j")
578 self.handle.expect("ports -j")
579 self.handle.expect("onos>")
580 else:
581 self.handle.sendline("ports -j | grep '"+
582 str(grep_str)+"'")
583 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
584 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400585 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400586 '''
587 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
588 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 -0400589 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 -0400590 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400591 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
592 handle1 = ansi_escape.sub('', handle)
593 '''
594 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400595 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
596 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400597 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400598 return handle1
599
Jon Halle8217482014-10-17 13:49:14 -0400600 else:
601 if not grep_str:
602 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400603 self.handle.expect("onos>")
604 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400605 self.handle.expect("onos>")
606 else:
607 self.handle.sendline("ports | grep '"+
608 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400609 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400610 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400611 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400612 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400613 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400614 return handle
Jon Halle8217482014-10-17 13:49:14 -0400615 except pexpect.EOF:
616 main.log.error(self.name + ": EOF exception found")
617 main.log.error(self.name + ": " + self.handle.before)
618 main.cleanup()
619 main.exit()
620 except:
621 main.log.info(self.name+" ::::::")
622 main.log.error( traceback.print_exc())
623 main.log.info(self.name+" ::::::")
624 main.cleanup()
625 main.exit()
626
627
Jon Hall983a1702014-10-28 18:44:22 -0400628 def roles(self, json_format=True, grep_str=""):
andrewonlab7c211572014-10-15 16:45:20 -0400629 '''
Jon Hall983a1702014-10-28 18:44:22 -0400630 Lists all devices and the controllers with roles assigned to them
631 Optional argument:
632 * grep_str - pass in a string to grep
andrewonlab7c211572014-10-15 16:45:20 -0400633 '''
634 try:
635 self.handle.sendline("")
636 self.handle.expect("onos>")
andrewonlab7c211572014-10-15 16:45:20 -0400637
Jon Hall983a1702014-10-28 18:44:22 -0400638 if json_format:
639 if not grep_str:
640 self.handle.sendline("roles -j")
641 self.handle.expect("roles -j")
642 self.handle.expect("onos>")
643 else:
644 self.handle.sendline("roles -j | grep '"+
645 str(grep_str)+"'")
646 self.handle.expect("roles -j | grep '"+str(grep_str)+"'")
647 self.handle.expect("onos>")
648 handle = self.handle.before
649 '''
650 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
651 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
652 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
653 So we take off that escape sequence using the following commads:
654 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
655 handle1 = ansi_escape.sub('', handle)
656 '''
657 #print "repr(handle) =", repr(handle)
658 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
659 handle1 = ansi_escape.sub('', handle)
660 #print "repr(handle1) = ", repr(handle1)
661 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400662
andrewonlab7c211572014-10-15 16:45:20 -0400663 else:
Jon Hall983a1702014-10-28 18:44:22 -0400664 if not grep_str:
665 self.handle.sendline("roles")
666 self.handle.expect("onos>")
667 self.handle.sendline("")
668 self.handle.expect("onos>")
669 else:
670 self.handle.sendline("roles | grep '"+
671 str(grep_str)+"'")
672 self.handle.expect("onos>")
673 self.handle.sendline("")
674 self.handle.expect("onos>")
675 handle = self.handle.before
676 #print "handle =",handle
677 return handle
678 except pexpect.EOF:
679 main.log.error(self.name + ": EOF exception found")
680 main.log.error(self.name + ": " + self.handle.before)
681 main.cleanup()
682 main.exit()
683 except:
684 main.log.info(self.name+" ::::::")
685 main.log.error( traceback.print_exc())
686 main.log.info(self.name+" ::::::")
687 main.cleanup()
688 main.exit()
689
690 def get_role(self, device_id):
691 '''
692 Given the a string containing the json representation of the "roles" cli command and a
693 partial or whole device id, returns a json object containing the
694 roles output for the first device whose id contains "device_id"
695
696 Returns:
697 Dict of the role assignments for the given device or
698 None if not match
699 '''
700 try:
701 import json
702 if device_id == None:
703 return None
704 else:
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:
Jon Hall720653a2014-10-28 19:02:37 -0400709 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400710 if str(device_id) in device['id']:
711 return device
712 return None
andrewonlab7c211572014-10-15 16:45:20 -0400713
andrewonlab86dc3082014-10-13 18:18:38 -0400714 except pexpect.EOF:
715 main.log.error(self.name + ": EOF exception found")
716 main.log.error(self.name + ": " + self.handle.before)
717 main.cleanup()
718 main.exit()
719 except:
720 main.log.info(self.name+" ::::::")
721 main.log.error( traceback.print_exc())
722 main.log.info(self.name+" ::::::")
723 main.cleanup()
724 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400725
andrewonlab3e15ead2014-10-15 14:21:34 -0400726 def paths(self, src_id, dst_id):
727 '''
728 Returns string of paths, and the cost.
729 Issues command: onos:paths <src> <dst>
730 '''
731 try:
732 self.handle.sendline("")
733 self.handle.expect("onos>")
734
735 self.handle.sendline("onos:paths "+
736 str(src_id) + " " + str(dst_id))
737 i = self.handle.expect([
738 "Error",
739 "onos>"])
740
741 self.handle.sendline("")
742 self.handle.expect("onos>")
743
744 handle = self.handle.before
745
746 if i == 0:
747 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400748 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400749 else:
750 path = handle.split(";")[0]
751 cost = handle.split(";")[1]
752 return (path, cost)
753
754 except pexpect.EOF:
755 main.log.error(self.name + ": EOF exception found")
756 main.log.error(self.name + ": " + self.handle.before)
757 main.cleanup()
758 main.exit()
759 except:
760 main.log.info(self.name+" ::::::")
761 main.log.error( traceback.print_exc())
762 main.log.info(self.name+" ::::::")
763 main.cleanup()
764 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400765
Jon Hall42db6dc2014-10-24 19:03:48 -0400766 def hosts(self, json_format=True, grep_str=""):
767 '''
768 Lists all discovered hosts
769 Optional argument:
770 * grep_str - pass in a string to grep
771 '''
772 try:
773 self.handle.sendline("")
774 self.handle.expect("onos>")
775
776 if json_format:
777 if not grep_str:
778 self.handle.sendline("hosts -j")
779 self.handle.expect("hosts -j")
780 self.handle.expect("onos>")
781 else:
782 self.handle.sendline("hosts -j | grep '"+
783 str(grep_str)+"'")
784 self.handle.expect("hosts -j | grep '"+str(grep_str)+"'")
785 self.handle.expect("onos>")
786 handle = self.handle.before
787 '''
788 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
789 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
790 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
791 So we take off that escape sequence using
792 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
793 handle1 = ansi_escape.sub('', handle)
794 '''
795 #print "repr(handle) =", repr(handle)
796 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
797 handle1 = ansi_escape.sub('', handle)
798 #print "repr(handle1) = ", repr(handle1)
799 return handle1
800 else:
801 if not grep_str:
802 self.handle.sendline("hosts")
803 self.handle.expect("onos>")
804 else:
805 self.handle.sendline("hosts | grep '"+
806 str(grep_str)+"'")
807 self.handle.expect("onos>")
808 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400809 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400810 return handle
811 except pexpect.EOF:
812 main.log.error(self.name + ": EOF exception found")
813 main.log.error(self.name + ": " + self.handle.before)
814 main.cleanup()
815 main.exit()
816 except:
817 main.log.info(self.name+" ::::::")
818 main.log.error( traceback.print_exc())
819 main.log.info(self.name+" ::::::")
820 main.cleanup()
821 main.exit()
822
823 def get_host(self, mac):
824 '''
825 Return the first host from the hosts api whose 'id' contains 'mac'
826 Note: mac must be a colon seperated mac address, but could be a partial mac address
827 Return None if there is no match
828 '''
829 import json
830 try:
831 if mac == None:
832 return None
833 else:
834 mac = mac
835 raw_hosts = self.hosts()
836 hosts_json = json.loads(raw_hosts)
837 #search json for the host with mac then return the device
838 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400839 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400840 if mac in host['id']:
841 return host
842 return None
843 except pexpect.EOF:
844 main.log.error(self.name + ": EOF exception found")
845 main.log.error(self.name + ": " + self.handle.before)
846 main.cleanup()
847 main.exit()
848 except:
849 main.log.info(self.name+" ::::::")
850 main.log.error( traceback.print_exc())
851 main.log.info(self.name+" ::::::")
852 main.cleanup()
853 main.exit()
854
andrewonlab3f0a4af2014-10-17 12:25:14 -0400855
856 def get_hosts_id(self, host_list):
857 '''
858 Obtain list of hosts
859 Issues command: 'onos> hosts'
860
861 Required:
862 * host_list: List of hosts obtained by Mininet
863 IMPORTANT:
864 This function assumes that you started your
865 topology with the option '--mac'.
866 Furthermore, it assumes that value of VLAN is '-1'
867 Description:
868 Converts mininet hosts (h1, h2, h3...) into
869 ONOS format (00:00:00:00:00:01/-1 , ...)
870 '''
871
872 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400873 onos_host_list = []
874
875 for host in host_list:
876 host = host.replace("h", "")
877 host_hex = hex(int(host)).zfill(12)
878 host_hex = str(host_hex).replace('x','0')
879 i = iter(str(host_hex))
880 host_hex = ":".join(a+b for a,b in zip(i,i))
881 host_hex = host_hex + "/-1"
882 onos_host_list.append(host_hex)
883
884 return onos_host_list
885
886 except pexpect.EOF:
887 main.log.error(self.name + ": EOF exception found")
888 main.log.error(self.name + ": " + self.handle.before)
889 main.cleanup()
890 main.exit()
891 except:
892 main.log.info(self.name+" ::::::")
893 main.log.error( traceback.print_exc())
894 main.log.info(self.name+" ::::::")
895 main.cleanup()
896 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400897
andrewonlabe6745342014-10-17 14:29:13 -0400898 def add_host_intent(self, host_id_one, host_id_two):
899 '''
900 Required:
901 * host_id_one: ONOS host id for host1
902 * host_id_two: ONOS host id for host2
903 Description:
904 Adds a host-to-host intent (bidrectional) by
905 specifying the two hosts.
906 '''
907 try:
908 self.handle.sendline("")
909 self.handle.expect("onos>")
910
911 self.handle.sendline("add-host-intent "+
912 str(host_id_one) + " " + str(host_id_two))
913 self.handle.expect("onos>")
914
andrewonlabe6745342014-10-17 14:29:13 -0400915 handle = self.handle.before
Shreya Shah4f25fdf2014-10-29 19:55:35 -0400916 print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400917
918 main.log.info("Intent installed between "+
919 str(host_id_one) + " and " + str(host_id_two))
920
921 return handle
922
923 except pexpect.EOF:
924 main.log.error(self.name + ": EOF exception found")
925 main.log.error(self.name + ": " + self.handle.before)
926 main.cleanup()
927 main.exit()
928 except:
929 main.log.info(self.name+" ::::::")
930 main.log.error( traceback.print_exc())
931 main.log.info(self.name+" ::::::")
932 main.cleanup()
933 main.exit()
934
andrewonlab7b31d232014-10-24 13:31:47 -0400935 def add_optical_intent(self, ingress_device, egress_device):
936 '''
937 Required:
938 * ingress_device: device id of ingress device
939 * egress_device: device id of egress device
940 Optional:
941 TODO: Still needs to be implemented via dev side
942 '''
943 try:
944 self.handle.sendline("add-optical-intent "+
945 str(ingress_device) + " " + str(egress_device))
946 self.handle.expect("add-optical-intent")
947 i = self.handle.expect([
948 "Error",
949 "onos>"])
950
951 handle = self.handle.before
952
953 #If error, return error message
954 if i == 0:
955 return handle
956 else:
957 return main.TRUE
958
959 except pexpect.EOF:
960 main.log.error(self.name + ": EOF exception found")
961 main.log.error(self.name + ": " + self.handle.before)
962 main.cleanup()
963 main.exit()
964 except:
965 main.log.info(self.name+" ::::::")
966 main.log.error( traceback.print_exc())
967 main.log.info(self.name+" ::::::")
968 main.cleanup()
969 main.exit()
970
andrewonlab4dbb4d82014-10-17 18:22:31 -0400971 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400972 egress_device, port_egress, ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -0500973 ethDst="", bandwidth="", lambda_alloc=False,
974 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400975 '''
976 Required:
977 * ingress_device: device id of ingress device
978 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400979 Optional:
980 * ethType: specify ethType
981 * ethSrc: specify ethSrc (i.e. src mac addr)
982 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500983 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -0500984 * lambda_alloc: if True, intent will allocate lambda
985 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -0500986 * ipProto: specify ip protocol
987 * ipSrc: specify ip source address
988 * ipDst: specify ip destination address
989 * tcpSrc: specify tcp source port
990 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400991 Description:
992 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400993 specifying device id's and optional fields
994
andrewonlab4dbb4d82014-10-17 18:22:31 -0400995 NOTE: This function may change depending on the
996 options developers provide for point-to-point
997 intent via cli
998 '''
999 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001000 cmd = ""
1001
1002 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001003 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001004 and not bandwidth and not lambda_alloc \
1005 and not ipProto and not ipSrc and not ipDst \
1006 and not tcpSrc and not tcpDst:
andrewonlab289e4b72014-10-21 21:24:18 -04001007 cmd = "add-point-intent "+\
1008 str(ingress_device) + "/" + str(port_ingress) + " " +\
1009 str(egress_device) + "/" + str(port_egress)
1010
1011 else:
andrewonlab9a130be2014-10-22 12:44:56 -04001012 cmd = "add-point-intent "
1013
andrewonlab0c0a6772014-10-22 12:31:18 -04001014 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001015 cmd += " --ethType " + str(ethType)
1016 if ethSrc:
1017 cmd += " --ethSrc " + str(ethSrc)
1018 if ethDst:
1019 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001020 if bandwidth:
1021 cmd += " --bandwidth " + str(bandwidth)
1022 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001023 cmd += " --lambda "
1024 if ipProto:
1025 cmd += " --ipProto " + str(ipProto)
1026 if ipSrc:
1027 cmd += " --ipSrc " + str(ipSrc)
1028 if ipDst:
1029 cmd += " --ipDst " + str(ipDst)
1030 if tcpSrc:
1031 cmd += " --tcpSrc " + str(tcpSrc)
1032 if tcpDst:
1033 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001034
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001035 cmd += " "+str(ingress_device) +\
1036 "/" + str(port_ingress) + " " +\
1037 str(egress_device) + "/" + str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001038
andrewonlab289e4b72014-10-21 21:24:18 -04001039 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001040 i = self.handle.expect([
1041 "Error",
1042 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -04001043
Shreya Shah0f01c812014-10-26 20:15:28 -04001044 self.handle.sendline("intents")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001045 self.handle.expect("onos>")
Shreya Shah0f01c812014-10-26 20:15:28 -04001046 Intenthandle = self.handle.before
andrewonlab4dbb4d82014-10-17 18:22:31 -04001047
1048 if i == 0:
1049 main.log.error("Error in adding point-to-point intent")
1050 return handle
1051 else:
1052 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001053
andrewonlab4dbb4d82014-10-17 18:22:31 -04001054 except pexpect.EOF:
1055 main.log.error(self.name + ": EOF exception found")
1056 main.log.error(self.name + ": " + self.handle.before)
1057 main.cleanup()
1058 main.exit()
1059 except:
1060 main.log.info(self.name+" ::::::")
1061 main.log.error( traceback.print_exc())
1062 main.log.info(self.name+" ::::::")
1063 main.cleanup()
1064 main.exit()
1065
andrewonlab9a50dfe2014-10-17 17:22:31 -04001066 def remove_intent(self, intent_id):
1067 '''
1068 Remove intent for specified intent id
1069 '''
1070 try:
1071 self.handle.sendline("")
1072 self.handle.expect("onos>")
1073
1074 self.handle.sendline("remove-intent "+str(intent_id))
1075 i = self.handle.expect([
1076 "Error",
1077 "onos>"])
1078
1079 handle = self.handle.before
1080
1081 if i == 0:
1082 main.log.error("Error in removing intent")
1083 return handle
1084 else:
1085 return handle
1086
1087 except pexpect.EOF:
1088 main.log.error(self.name + ": EOF exception found")
1089 main.log.error(self.name + ": " + self.handle.before)
1090 main.cleanup()
1091 main.exit()
1092 except:
1093 main.log.info(self.name+" ::::::")
1094 main.log.error( traceback.print_exc())
1095 main.log.info(self.name+" ::::::")
1096 main.cleanup()
1097 main.exit()
1098
andrewonlab377693f2014-10-21 16:00:30 -04001099 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001100 '''
andrewonlab377693f2014-10-21 16:00:30 -04001101 Optional:
1102 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001103 Description:
1104 Obtain intents currently installed
1105 '''
1106 try:
andrewonlab377693f2014-10-21 16:00:30 -04001107 if json_format:
1108 self.handle.sendline("intents -j")
1109 self.handle.expect("intents -j")
1110 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001111 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001112
1113 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1114 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001115 else:
1116 self.handle.sendline("")
1117 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001118
andrewonlab377693f2014-10-21 16:00:30 -04001119 self.handle.sendline("intents")
1120 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001121 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001122
1123 return handle
1124
1125 except pexpect.EOF:
1126 main.log.error(self.name + ": EOF exception found")
1127 main.log.error(self.name + ": " + self.handle.before)
1128 main.cleanup()
1129 main.exit()
1130 except:
1131 main.log.info(self.name+" ::::::")
1132 main.log.error( traceback.print_exc())
1133 main.log.info(self.name+" ::::::")
1134 main.cleanup()
1135 main.exit()
1136
Shreya Shah0f01c812014-10-26 20:15:28 -04001137 def flows(self, json_format = False):
1138 '''
1139 Optional:
1140 * json_format: enable output formatting in json
1141 Description:
1142 Obtain flows currently installed
1143 '''
1144 try:
1145 if json_format:
1146 self.handle.sendline("flows -j")
1147 self.handle.expect("flows -j")
1148 self.handle.expect("onos>")
1149 handle = self.handle.before
1150
1151 else:
1152 self.handle.sendline("")
1153 self.handle.expect("onos>")
1154 self.handle.sendline("flows")
1155 self.handle.expect("onos>")
1156 handle = self.handle.before
1157
1158 return handle
1159
1160 except pexpect.EOF:
1161 main.log.error(self.name + ": EOF exception found")
1162 main.log.error(self.name + ": " + self.handle.before)
1163 main.cleanup()
1164 main.exit()
1165 except:
1166 main.log.info(self.name+" ::::::")
1167 main.log.error( traceback.print_exc())
1168 main.log.info(self.name+" ::::::")
1169 main.cleanup()
1170 main.exit()
1171
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001172 def intents_events_metrics(self, json_format=True):
1173 '''
1174 Description:Returns topology metrics
1175 Optional:
1176 * json_format: enable json formatting of output
1177 '''
1178 try:
1179 if json_format:
1180 self.handle.sendline("intents-events-metrics -j")
1181 self.handle.expect("intents-events-metrics -j")
1182 self.handle.expect("onos>")
1183
1184 handle = self.handle.before
1185
1186 #Some color thing that we want to escape
1187 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1188 handle = ansi_escape.sub('', handle)
1189
1190 else:
1191 self.handle.sendline("intents-events-metrics")
1192 self.handle.expect("intents-events-metrics")
1193 self.handle.expect("onos>")
1194
1195 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001196
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001197 return handle
1198
1199 except pexpect.EOF:
1200 main.log.error(self.name + ": EOF exception found")
1201 main.log.error(self.name + ": " + self.handle.before)
1202 main.cleanup()
1203 main.exit()
1204 except:
1205 main.log.info(self.name+" ::::::")
1206 main.log.error( traceback.print_exc())
1207 main.log.info(self.name+" ::::::")
1208 main.cleanup()
1209 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001210
andrewonlab867212a2014-10-22 20:13:38 -04001211 def topology_events_metrics(self, json_format=True):
1212 '''
1213 Description:Returns topology metrics
1214 Optional:
1215 * json_format: enable json formatting of output
1216 '''
1217 try:
1218 if json_format:
1219 self.handle.sendline("topology-events-metrics -j")
1220 self.handle.expect("topology-events-metrics -j")
1221 self.handle.expect("onos>")
1222
1223 handle = self.handle.before
1224
1225 #Some color thing that we want to escape
1226 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1227 handle = ansi_escape.sub('', handle)
1228
1229 else:
1230 self.handle.sendline("topology-events-metrics")
1231 self.handle.expect("topology-events-metrics")
1232 self.handle.expect("onos>")
1233
1234 handle = self.handle.before
1235
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
andrewonlab3e15ead2014-10-15 14:21:34 -04001250 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001251 #Wrapper functions use existing driver
1252 #functions and extends their use case.
1253 #For example, we may use the output of
1254 #a normal driver function, and parse it
1255 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001256
andrewonlab9a50dfe2014-10-17 17:22:31 -04001257 def get_all_intents_id(self):
1258 '''
1259 Description:
1260 Obtain all intent id's in a list
1261 '''
1262 try:
1263 #Obtain output of intents function
1264 intents_str = self.intents()
1265 all_intent_list = []
1266 intent_id_list = []
1267
1268 #Parse the intents output for ID's
1269 intents_list = [s.strip() for s in intents_str.splitlines()]
1270 for intents in intents_list:
1271 if "onos>" in intents:
1272 continue
1273 elif "intents" in intents:
1274 continue
1275 else:
1276 line_list = intents.split(" ")
1277 all_intent_list.append(line_list[0])
1278
1279 all_intent_list = all_intent_list[1:-2]
1280
1281 for intents in all_intent_list:
1282 if not intents:
1283 continue
1284 else:
1285 intent_id_list.append(intents)
1286
1287 return intent_id_list
1288
1289 except pexpect.EOF:
1290 main.log.error(self.name + ": EOF exception found")
1291 main.log.error(self.name + ": " + self.handle.before)
1292 main.cleanup()
1293 main.exit()
1294 except:
1295 main.log.info(self.name+" ::::::")
1296 main.log.error( traceback.print_exc())
1297 main.log.info(self.name+" ::::::")
1298 main.cleanup()
1299 main.exit()
1300
andrewonlab7e4d2d32014-10-15 13:23:21 -04001301 def get_all_devices_id(self):
1302 '''
1303 Use 'devices' function to obtain list of all devices
1304 and parse the result to obtain a list of all device
1305 id's. Returns this list. Returns empty list if no
1306 devices exist
1307 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001308
1309 This function may be useful if you are not sure of the
1310 device id, and wish to execute other commands using
1311 the ids. By obtaining the list of device ids on the fly,
1312 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001313 '''
1314 try:
1315 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001316 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001317 id_list = []
1318
1319 if not devices_str:
1320 main.log.info("There are no devices to get id from")
1321 return id_list
1322
1323 #Split the string into list by comma
1324 device_list = devices_str.split(",")
1325 #Get temporary list of all arguments with string 'id='
1326 temp_list = [dev for dev in device_list if "id=" in dev]
1327 #Split list further into arguments before and after string
1328 # 'id='. Get the latter portion (the actual device id) and
1329 # append to id_list
1330 for arg in temp_list:
1331 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001332 return id_list
1333
1334 except pexpect.EOF:
1335 main.log.error(self.name + ": EOF exception found")
1336 main.log.error(self.name + ": " + self.handle.before)
1337 main.cleanup()
1338 main.exit()
1339 except:
1340 main.log.info(self.name+" ::::::")
1341 main.log.error( traceback.print_exc())
1342 main.log.info(self.name+" ::::::")
1343 main.cleanup()
1344 main.exit()
1345
andrewonlab7c211572014-10-15 16:45:20 -04001346 def get_all_nodes_id(self):
1347 '''
1348 Uses 'nodes' function to obtain list of all nodes
1349 and parse the result of nodes to obtain just the
1350 node id's.
1351 Returns:
1352 list of node id's
1353 '''
1354 try:
1355 nodes_str = self.nodes()
1356 id_list = []
1357
1358 if not nodes_str:
1359 main.log.info("There are no nodes to get id from")
1360 return id_list
1361
1362 #Sample nodes_str output
1363 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1364
1365 #Split the string into list by comma
1366 nodes_list = nodes_str.split(",")
1367 temp_list = [node for node in nodes_list if "id=" in node]
1368 for arg in temp_list:
1369 id_list.append(arg.split("id=")[1])
1370
1371 return id_list
1372
1373 except pexpect.EOF:
1374 main.log.error(self.name + ": EOF exception found")
1375 main.log.error(self.name + ": " + self.handle.before)
1376 main.cleanup()
1377 main.exit()
1378 except:
1379 main.log.info(self.name+" ::::::")
1380 main.log.error( traceback.print_exc())
1381 main.log.info(self.name+" ::::::")
1382 main.cleanup()
1383 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001384
Jon Halla91c4dc2014-10-22 12:57:04 -04001385 def get_device(self, dpid=None):
1386 '''
1387 Return the first device from the devices api whose 'id' contains 'dpid'
1388 Return None if there is no match
1389 '''
1390 import json
1391 try:
1392 if dpid == None:
1393 return None
1394 else:
1395 dpid = dpid.replace(':', '')
1396 raw_devices = self.devices()
1397 devices_json = json.loads(raw_devices)
1398 #search json for the device with dpid then return the device
1399 for device in devices_json:
1400 #print "%s in %s?" % (dpid, device['id'])
1401 if dpid in device['id']:
1402 return device
1403 return None
1404 except pexpect.EOF:
1405 main.log.error(self.name + ": EOF exception found")
1406 main.log.error(self.name + ": " + self.handle.before)
1407 main.cleanup()
1408 main.exit()
1409 except:
1410 main.log.info(self.name+" ::::::")
1411 main.log.error( traceback.print_exc())
1412 main.log.info(self.name+" ::::::")
1413 main.cleanup()
1414 main.exit()
1415
Jon Hall42db6dc2014-10-24 19:03:48 -04001416 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1417 '''
1418 Checks the number of swithes & links that ONOS sees against the
1419 supplied values. By default this will report to main.log, but the
1420 log level can be specifid.
1421
1422 Params: ip = ip used for the onos cli
1423 numoswitch = expected number of switches
1424 numlink = expected number of links
1425 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1426
1427
1428 log_level can
1429
1430 Returns: main.TRUE if the number of switchs and links are correct,
1431 main.FALSE if the numer of switches and links is incorrect,
1432 and main.ERROR otherwise
1433 '''
1434
1435 try:
1436 topology = self.get_topology(ip)
1437 if topology == {}:
1438 return main.ERROR
1439 output = ""
1440 #Is the number of switches is what we expected
1441 devices = topology.get('devices',False)
1442 links = topology.get('links',False)
1443 if devices == False or links == False:
1444 return main.ERROR
1445 switch_check = ( int(devices) == int(numoswitch) )
1446 #Is the number of links is what we expected
1447 link_check = ( int(links) == int(numolink) )
1448 if (switch_check and link_check):
1449 #We expected the correct numbers
1450 output = output + "The number of links and switches match "\
1451 + "what was expected"
1452 result = main.TRUE
1453 else:
1454 output = output + \
1455 "The number of links and switches does not match what was expected"
1456 result = main.FALSE
1457 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1458 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1459 if log_level == "report":
1460 main.log.report(output)
1461 elif log_level == "warn":
1462 main.log.warn(output)
1463 else:
1464 main.log.info(output)
1465 return result
1466 except pexpect.EOF:
1467 main.log.error(self.name + ": EOF exception found")
1468 main.log.error(self.name + ": " + self.handle.before)
1469 main.cleanup()
1470 main.exit()
1471 except:
1472 main.log.info(self.name+" ::::::")
1473 main.log.error( traceback.print_exc())
1474 main.log.info(self.name+" ::::::")
1475 main.cleanup()
1476 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001477
1478 def device_role(self, device_id, onos_node, role="master"):
1479 '''
1480 Calls the device-role cli command.
1481 device_id must be the id of a device as seen in the onos devices command
1482 onos_node is the ip of one of the onos nodes in the cluster
1483 role must be either master, standby, or none
1484
Jon Hall983a1702014-10-28 18:44:22 -04001485 Returns main.TRUE or main.FALSE based argument varification.
1486 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001487 support that output
1488 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001489 try:
Jon Hall983a1702014-10-28 18:44:22 -04001490 #print "beginning device_role... \n\tdevice_id:" + device_id
1491 #print "\tonos_node: " + onos_node
1492 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001493 if role.lower() == "master" or \
1494 role.lower() == "standby" or \
1495 role.lower() == "none":
1496 self.handle.sendline("")
1497 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001498 self.handle.sendline("device-role " +
1499 str(device_id) + " " +
1500 str(onos_node) + " " +
1501 str(role))
1502 i= self.handle.expect(["Error","onos>"])
1503 if i == 0:
1504 output = str(self.handle.before)
1505 self.handle.expect("onos>")
1506 output = output + str(self.handle.before)
1507 main.log.error(self.name + ": " +
1508 output + '\033[0m')#end color output to escape any colours from the cli
1509 return main.ERROR
1510 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001511 self.handle.expect("onos>")
1512 return main.TRUE
1513 else:
1514 return main.FALSE
1515
1516 except pexpect.EOF:
1517 main.log.error(self.name + ": EOF exception found")
1518 main.log.error(self.name + ": " + self.handle.before)
1519 main.cleanup()
1520 main.exit()
1521 except:
1522 main.log.info(self.name+" ::::::")
1523 main.log.error( traceback.print_exc())
1524 main.log.info(self.name+" ::::::")
1525 main.cleanup()
1526 main.exit()
1527
1528
andrewonlab7e4d2d32014-10-15 13:23:21 -04001529 #***********************************