blob: 89edbda410c2f1852b64a44b68ebebbe28dd97c1 [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
106 def set_cell(self, cellname):
107 '''
108 Calls 'cell <name>' to set the environment variables on ONOSbench
109
110 Before issuing any cli commands, set the environment variable first.
111 '''
112 try:
113 if not cellname:
114 main.log.error("Must define cellname")
115 main.cleanup()
116 main.exit()
117 else:
118 self.handle.sendline("cell "+str(cellname))
119 #Expect the cellname in the ONOS_CELL variable.
120 #Note that this variable name is subject to change
121 # and that this driver will have to change accordingly
122 self.handle.expect("ONOS_CELL="+str(cellname))
123 handle_before = self.handle.before
124 handle_after = self.handle.after
125 #Get the rest of the handle
126 self.handle.sendline("")
127 self.handle.expect("\$")
128 handle_more = self.handle.before
129
130 main.log.info("Cell call returned: "+handle_before+
131 handle_after + handle_more)
132
133 return main.TRUE
134
135 except pexpect.EOF:
136 main.log.error(self.name + ": EOF exception found")
137 main.log.error(self.name + ": " + self.handle.before)
138 main.cleanup()
139 main.exit()
140 except:
141 main.log.info(self.name+" ::::::")
142 main.log.error( traceback.print_exc())
143 main.log.info(self.name+" ::::::")
144 main.cleanup()
145 main.exit()
146
andrewonlabc2d05aa2014-10-13 16:51:10 -0400147 def start_onos_cli(self, ONOS_ip):
andrewonlab95ce8322014-10-13 14:12:04 -0400148 try:
149 self.handle.sendline("")
150 self.handle.expect("\$")
151
152 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400153 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400154 i = self.handle.expect([
155 "onos>",
156 pexpect.TIMEOUT],timeout=60)
157
158 if i == 0:
159 main.log.info(str(ONOS_ip)+" CLI Started successfully")
160 return main.TRUE
161 else:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400162 #If failed, send ctrl+c to process and try again
andrewonlabf47993a2014-10-24 17:56:01 -0400163 main.log.info("Starting CLI failed. Retrying...")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400164 self.handle.sendline("\x03")
165 self.handle.sendline("onos -w "+str(ONOS_ip))
166 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
167 timeout=30)
168 if i == 0:
169 return main.TRUE
170 else:
171 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400172 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400173 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400174
175 except pexpect.EOF:
176 main.log.error(self.name + ": EOF exception found")
177 main.log.error(self.name + ": " + self.handle.before)
178 main.cleanup()
179 main.exit()
180 except:
181 main.log.info(self.name+" ::::::")
182 main.log.error( traceback.print_exc())
183 main.log.info(self.name+" ::::::")
184 main.cleanup()
185 main.exit()
186
andrewonlaba18f6bf2014-10-13 19:31:54 -0400187 def sendline(self, cmd_str):
188 '''
189 Send a completely user specified string to
190 the onos> prompt. Use this function if you have
191 a very specific command to send.
192
193 Warning: There are no sanity checking to commands
194 sent using this method.
195 '''
196 try:
197 self.handle.sendline("")
198 self.handle.expect("onos>")
199
200 self.handle.sendline(cmd_str)
201 self.handle.expect("onos>")
202
203 handle = self.handle.before
204
205 self.handle.sendline("")
206 self.handle.expect("onos>")
207
208 handle += self.handle.before
209 handle += self.handle.after
210
211 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400212 ansi_escape = re.compile(r'\x1b[^m]*m')
213 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400214
215 return handle
216 except pexpect.EOF:
217 main.log.error(self.name + ": EOF exception found")
218 main.log.error(self.name + ": " + self.handle.before)
219 main.cleanup()
220 main.exit()
221 except:
222 main.log.info(self.name+" ::::::")
223 main.log.error( traceback.print_exc())
224 main.log.info(self.name+" ::::::")
225 main.cleanup()
226 main.exit()
227
andrewonlab95ce8322014-10-13 14:12:04 -0400228 #IMPORTANT NOTE:
229 #For all cli commands, naming convention should match
230 #the cli command replacing ':' with '_'.
231 #Ex) onos:topology > onos_topology
232 # onos:links > onos_links
233 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400234
235 def add_node(self, node_id, ONOS_ip, tcp_port=""):
236 '''
237 Adds a new cluster node by ID and address information.
238 Required:
239 * node_id
240 * ONOS_ip
241 Optional:
242 * tcp_port
243 '''
244 try:
245 self.handle.sendline("")
246 self.handle.expect("onos>")
247
248 self.handle.sendline("add-node "+
249 str(node_id)+" "+
250 str(ONOS_ip)+" "+
251 str(tcp_port))
252
253 i = self.handle.expect([
254 "Error",
255 "onos>" ])
256
257 #Clear handle to get previous output
258 self.handle.sendline("")
259 self.handle.expect("onos>")
260
261 handle = self.handle.before
262
263 if i == 0:
264 main.log.error("Error in adding node")
265 main.log.error(handle)
266 return main.FALSE
267 else:
268 main.log.info("Node "+str(ONOS_ip)+" added")
269 return main.TRUE
270
271 except pexpect.EOF:
272 main.log.error(self.name + ": EOF exception found")
273 main.log.error(self.name + ": " + self.handle.before)
274 main.cleanup()
275 main.exit()
276 except:
277 main.log.info(self.name+" ::::::")
278 main.log.error( traceback.print_exc())
279 main.log.info(self.name+" ::::::")
280 main.cleanup()
281 main.exit()
282
andrewonlab86dc3082014-10-13 18:18:38 -0400283 def remove_node(self, node_id):
284 '''
285 Removes a cluster by ID
286 Issues command: 'remove-node [<node-id>]'
287 Required:
288 * node_id
289 '''
290 try:
291 self.handle.sendline("")
292 self.handle.expect("onos>")
293
294 self.handle.sendline("remove-node "+str(node_id))
295 self.handle.expect("onos>")
296
297 return main.TRUE
298
299 except pexpect.EOF:
300 main.log.error(self.name + ": EOF exception found")
301 main.log.error(self.name + ": " + self.handle.before)
302 main.cleanup()
303 main.exit()
304 except:
305 main.log.info(self.name+" ::::::")
306 main.log.error( traceback.print_exc())
307 main.log.info(self.name+" ::::::")
308 main.cleanup()
309 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400310
andrewonlab7c211572014-10-15 16:45:20 -0400311 def nodes(self):
312 '''
313 List the nodes currently visible
314 Issues command: 'nodes'
315 Returns: entire handle of list of nodes
316 '''
317 try:
318 self.handle.sendline("")
319 self.handle.expect("onos>")
320
321 self.handle.sendline("nodes")
322 self.handle.expect("onos>")
323
324 self.handle.sendline("")
325 self.handle.expect("onos>")
326
327 handle = self.handle.before
328
329 return handle
330
331 except pexpect.EOF:
332 main.log.error(self.name + ": EOF exception found")
333 main.log.error(self.name + ": " + self.handle.before)
334 main.cleanup()
335 main.exit()
336 except:
337 main.log.info(self.name+" ::::::")
338 main.log.error( traceback.print_exc())
339 main.log.info(self.name+" ::::::")
340 main.cleanup()
341 main.exit()
342
andrewonlab38d6ae22014-10-15 14:23:45 -0400343 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400344 '''
345 Shows the current state of the topology
346 by issusing command: 'onos> onos:topology'
347 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400348 try:
349 self.handle.sendline("")
350 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400351 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400352 self.handle.sendline("onos:topology")
353 self.handle.expect("onos>")
354
355 handle = self.handle.before
356
357 main.log.info("onos:topology returned: " +
358 str(handle))
359
360 return handle
361
362 except pexpect.EOF:
363 main.log.error(self.name + ": EOF exception found")
364 main.log.error(self.name + ": " + self.handle.before)
365 main.cleanup()
366 main.exit()
367 except:
368 main.log.info(self.name+" ::::::")
369 main.log.error( traceback.print_exc())
370 main.log.info(self.name+" ::::::")
371 main.cleanup()
372 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400373
374 def feature_install(self, feature_str):
375 '''
376 Installs a specified feature
377 by issuing command: 'onos> feature:install <feature_str>'
378 '''
379 try:
380 self.handle.sendline("")
381 self.handle.expect("onos>")
382
383 self.handle.sendline("feature:install "+str(feature_str))
384 self.handle.expect("onos>")
385
386 return main.TRUE
387
388 except pexpect.EOF:
389 main.log.error(self.name + ": EOF exception found")
390 main.log.error(self.name + ": " + self.handle.before)
391 main.cleanup()
392 main.exit()
393 except:
394 main.log.info(self.name+" ::::::")
395 main.log.error( traceback.print_exc())
396 main.log.info(self.name+" ::::::")
397 main.cleanup()
398 main.exit()
399
400 def feature_uninstall(self, feature_str):
401 '''
402 Uninstalls a specified feature
403 by issuing command: 'onos> feature:uninstall <feature_str>'
404 '''
405 try:
406 self.handle.sendline("")
407 self.handle.expect("onos>")
408
409 self.handle.sendline("feature:uninstall "+str(feature_str))
410 self.handle.expect("onos>")
411
412 return main.TRUE
413
414 except pexpect.EOF:
415 main.log.error(self.name + ": EOF exception found")
416 main.log.error(self.name + ": " + self.handle.before)
417 main.cleanup()
418 main.exit()
419 except:
420 main.log.info(self.name+" ::::::")
421 main.log.error( traceback.print_exc())
422 main.log.info(self.name+" ::::::")
423 main.cleanup()
424 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400425
Jon Halle8217482014-10-17 13:49:14 -0400426 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400427 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400428 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400429 Optional argument:
430 * grep_str - pass in a string to grep
431 '''
432 try:
433 self.handle.sendline("")
434 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400435
436 if json_format:
437 if not grep_str:
438 self.handle.sendline("devices -j")
439 self.handle.expect("devices -j")
440 self.handle.expect("onos>")
441 else:
442 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400443 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400444 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
445 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400446 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400447 '''
448 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
449 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 -0400450 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 -0400451 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400452 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400453 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400454 '''
455 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400456 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
457 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400458 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400459 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400460 else:
461 if not grep_str:
462 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400463 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400464 else:
465 self.handle.sendline("devices | grep '"+
466 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400467 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400468 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400469 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400470 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400471 except pexpect.EOF:
472 main.log.error(self.name + ": EOF exception found")
473 main.log.error(self.name + ": " + self.handle.before)
474 main.cleanup()
475 main.exit()
476 except:
477 main.log.info(self.name+" ::::::")
478 main.log.error( traceback.print_exc())
479 main.log.info(self.name+" ::::::")
480 main.cleanup()
481 main.exit()
482
Jon Halle8217482014-10-17 13:49:14 -0400483 def links(self, json_format=True, grep_str=""):
484 '''
485 Lists all core links
486 Optional argument:
487 * grep_str - pass in a string to grep
488 '''
489 try:
490 self.handle.sendline("")
491 self.handle.expect("onos>")
492
493 if json_format:
494 if not grep_str:
495 self.handle.sendline("links -j")
496 self.handle.expect("links -j")
497 self.handle.expect("onos>")
498 else:
499 self.handle.sendline("links -j | grep '"+
500 str(grep_str)+"'")
501 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
502 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400503 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400504 '''
505 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
506 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 -0400507 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 -0400508 So we take off that escape sequence using
509 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
510 handle1 = ansi_escape.sub('', handle)
511 '''
512 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400513 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
514 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400515 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400516 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400517 else:
518 if not grep_str:
519 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400520 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400521 else:
522 self.handle.sendline("links | grep '"+
523 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400524 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400525 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400526 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400527 return handle
Jon Halle8217482014-10-17 13:49:14 -0400528 except pexpect.EOF:
529 main.log.error(self.name + ": EOF exception found")
530 main.log.error(self.name + ": " + self.handle.before)
531 main.cleanup()
532 main.exit()
533 except:
534 main.log.info(self.name+" ::::::")
535 main.log.error( traceback.print_exc())
536 main.log.info(self.name+" ::::::")
537 main.cleanup()
538 main.exit()
539
540
541 def ports(self, json_format=True, grep_str=""):
542 '''
543 Lists all ports
544 Optional argument:
545 * grep_str - pass in a string to grep
546 '''
547 try:
548 self.handle.sendline("")
549 self.handle.expect("onos>")
550
551 if json_format:
552 if not grep_str:
553 self.handle.sendline("ports -j")
554 self.handle.expect("ports -j")
555 self.handle.expect("onos>")
556 else:
557 self.handle.sendline("ports -j | grep '"+
558 str(grep_str)+"'")
559 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
560 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400561 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400562 '''
563 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
564 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 -0400565 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 -0400566 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400567 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
568 handle1 = ansi_escape.sub('', handle)
569 '''
570 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400571 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
572 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400573 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400574 return handle1
575
Jon Halle8217482014-10-17 13:49:14 -0400576 else:
577 if not grep_str:
578 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400579 self.handle.expect("onos>")
580 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400581 self.handle.expect("onos>")
582 else:
583 self.handle.sendline("ports | grep '"+
584 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400585 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400586 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400587 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400588 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400589 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400590 return handle
Jon Halle8217482014-10-17 13:49:14 -0400591 except pexpect.EOF:
592 main.log.error(self.name + ": EOF exception found")
593 main.log.error(self.name + ": " + self.handle.before)
594 main.cleanup()
595 main.exit()
596 except:
597 main.log.info(self.name+" ::::::")
598 main.log.error( traceback.print_exc())
599 main.log.info(self.name+" ::::::")
600 main.cleanup()
601 main.exit()
602
603
Jon Hall983a1702014-10-28 18:44:22 -0400604 def roles(self, json_format=True, grep_str=""):
andrewonlab7c211572014-10-15 16:45:20 -0400605 '''
Jon Hall983a1702014-10-28 18:44:22 -0400606 Lists all devices and the controllers with roles assigned to them
607 Optional argument:
608 * grep_str - pass in a string to grep
andrewonlab7c211572014-10-15 16:45:20 -0400609 '''
610 try:
611 self.handle.sendline("")
612 self.handle.expect("onos>")
andrewonlab7c211572014-10-15 16:45:20 -0400613
Jon Hall983a1702014-10-28 18:44:22 -0400614 if json_format:
615 if not grep_str:
616 self.handle.sendline("roles -j")
617 self.handle.expect("roles -j")
618 self.handle.expect("onos>")
619 else:
620 self.handle.sendline("roles -j | grep '"+
621 str(grep_str)+"'")
622 self.handle.expect("roles -j | grep '"+str(grep_str)+"'")
623 self.handle.expect("onos>")
624 handle = self.handle.before
625 '''
626 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
627 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
628 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
629 So we take off that escape sequence using the following commads:
630 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
631 handle1 = ansi_escape.sub('', handle)
632 '''
633 #print "repr(handle) =", repr(handle)
634 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
635 handle1 = ansi_escape.sub('', handle)
636 #print "repr(handle1) = ", repr(handle1)
637 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400638
andrewonlab7c211572014-10-15 16:45:20 -0400639 else:
Jon Hall983a1702014-10-28 18:44:22 -0400640 if not grep_str:
641 self.handle.sendline("roles")
642 self.handle.expect("onos>")
643 self.handle.sendline("")
644 self.handle.expect("onos>")
645 else:
646 self.handle.sendline("roles | grep '"+
647 str(grep_str)+"'")
648 self.handle.expect("onos>")
649 self.handle.sendline("")
650 self.handle.expect("onos>")
651 handle = self.handle.before
652 #print "handle =",handle
653 return handle
654 except pexpect.EOF:
655 main.log.error(self.name + ": EOF exception found")
656 main.log.error(self.name + ": " + self.handle.before)
657 main.cleanup()
658 main.exit()
659 except:
660 main.log.info(self.name+" ::::::")
661 main.log.error( traceback.print_exc())
662 main.log.info(self.name+" ::::::")
663 main.cleanup()
664 main.exit()
665
666 def get_role(self, device_id):
667 '''
668 Given the a string containing the json representation of the "roles" cli command and a
669 partial or whole device id, returns a json object containing the
670 roles output for the first device whose id contains "device_id"
671
672 Returns:
673 Dict of the role assignments for the given device or
674 None if not match
675 '''
676 try:
677 import json
678 if device_id == None:
679 return None
680 else:
681 raw_roles = self.roles()
682 roles_json = json.loads(raw_roles)
683 #search json for the device with id then return the device
684 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400685 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400686 if str(device_id) in device['id']:
687 return device
688 return None
andrewonlab7c211572014-10-15 16:45:20 -0400689
andrewonlab86dc3082014-10-13 18:18:38 -0400690 except pexpect.EOF:
691 main.log.error(self.name + ": EOF exception found")
692 main.log.error(self.name + ": " + self.handle.before)
693 main.cleanup()
694 main.exit()
695 except:
696 main.log.info(self.name+" ::::::")
697 main.log.error( traceback.print_exc())
698 main.log.info(self.name+" ::::::")
699 main.cleanup()
700 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400701
andrewonlab3e15ead2014-10-15 14:21:34 -0400702 def paths(self, src_id, dst_id):
703 '''
704 Returns string of paths, and the cost.
705 Issues command: onos:paths <src> <dst>
706 '''
707 try:
708 self.handle.sendline("")
709 self.handle.expect("onos>")
710
711 self.handle.sendline("onos:paths "+
712 str(src_id) + " " + str(dst_id))
713 i = self.handle.expect([
714 "Error",
715 "onos>"])
716
717 self.handle.sendline("")
718 self.handle.expect("onos>")
719
720 handle = self.handle.before
721
722 if i == 0:
723 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400724 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400725 else:
726 path = handle.split(";")[0]
727 cost = handle.split(";")[1]
728 return (path, cost)
729
730 except pexpect.EOF:
731 main.log.error(self.name + ": EOF exception found")
732 main.log.error(self.name + ": " + self.handle.before)
733 main.cleanup()
734 main.exit()
735 except:
736 main.log.info(self.name+" ::::::")
737 main.log.error( traceback.print_exc())
738 main.log.info(self.name+" ::::::")
739 main.cleanup()
740 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400741
Jon Hall42db6dc2014-10-24 19:03:48 -0400742 def hosts(self, json_format=True, grep_str=""):
743 '''
744 Lists all discovered hosts
745 Optional argument:
746 * grep_str - pass in a string to grep
747 '''
748 try:
749 self.handle.sendline("")
750 self.handle.expect("onos>")
751
752 if json_format:
753 if not grep_str:
754 self.handle.sendline("hosts -j")
755 self.handle.expect("hosts -j")
756 self.handle.expect("onos>")
757 else:
758 self.handle.sendline("hosts -j | grep '"+
759 str(grep_str)+"'")
760 self.handle.expect("hosts -j | grep '"+str(grep_str)+"'")
761 self.handle.expect("onos>")
762 handle = self.handle.before
763 '''
764 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
765 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
766 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
767 So we take off that escape sequence using
768 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
769 handle1 = ansi_escape.sub('', handle)
770 '''
771 #print "repr(handle) =", repr(handle)
772 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
773 handle1 = ansi_escape.sub('', handle)
774 #print "repr(handle1) = ", repr(handle1)
775 return handle1
776 else:
777 if not grep_str:
778 self.handle.sendline("hosts")
779 self.handle.expect("onos>")
780 else:
781 self.handle.sendline("hosts | grep '"+
782 str(grep_str)+"'")
783 self.handle.expect("onos>")
784 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400785 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400786 return handle
787 except pexpect.EOF:
788 main.log.error(self.name + ": EOF exception found")
789 main.log.error(self.name + ": " + self.handle.before)
790 main.cleanup()
791 main.exit()
792 except:
793 main.log.info(self.name+" ::::::")
794 main.log.error( traceback.print_exc())
795 main.log.info(self.name+" ::::::")
796 main.cleanup()
797 main.exit()
798
799 def get_host(self, mac):
800 '''
801 Return the first host from the hosts api whose 'id' contains 'mac'
802 Note: mac must be a colon seperated mac address, but could be a partial mac address
803 Return None if there is no match
804 '''
805 import json
806 try:
807 if mac == None:
808 return None
809 else:
810 mac = mac
811 raw_hosts = self.hosts()
812 hosts_json = json.loads(raw_hosts)
813 #search json for the host with mac then return the device
814 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400815 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400816 if mac in host['id']:
817 return host
818 return None
819 except pexpect.EOF:
820 main.log.error(self.name + ": EOF exception found")
821 main.log.error(self.name + ": " + self.handle.before)
822 main.cleanup()
823 main.exit()
824 except:
825 main.log.info(self.name+" ::::::")
826 main.log.error( traceback.print_exc())
827 main.log.info(self.name+" ::::::")
828 main.cleanup()
829 main.exit()
830
andrewonlab3f0a4af2014-10-17 12:25:14 -0400831
832 def get_hosts_id(self, host_list):
833 '''
834 Obtain list of hosts
835 Issues command: 'onos> hosts'
836
837 Required:
838 * host_list: List of hosts obtained by Mininet
839 IMPORTANT:
840 This function assumes that you started your
841 topology with the option '--mac'.
842 Furthermore, it assumes that value of VLAN is '-1'
843 Description:
844 Converts mininet hosts (h1, h2, h3...) into
845 ONOS format (00:00:00:00:00:01/-1 , ...)
846 '''
847
848 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400849 onos_host_list = []
850
851 for host in host_list:
852 host = host.replace("h", "")
853 host_hex = hex(int(host)).zfill(12)
854 host_hex = str(host_hex).replace('x','0')
855 i = iter(str(host_hex))
856 host_hex = ":".join(a+b for a,b in zip(i,i))
857 host_hex = host_hex + "/-1"
858 onos_host_list.append(host_hex)
859
860 return onos_host_list
861
862 except pexpect.EOF:
863 main.log.error(self.name + ": EOF exception found")
864 main.log.error(self.name + ": " + self.handle.before)
865 main.cleanup()
866 main.exit()
867 except:
868 main.log.info(self.name+" ::::::")
869 main.log.error( traceback.print_exc())
870 main.log.info(self.name+" ::::::")
871 main.cleanup()
872 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400873
andrewonlabe6745342014-10-17 14:29:13 -0400874 def add_host_intent(self, host_id_one, host_id_two):
875 '''
876 Required:
877 * host_id_one: ONOS host id for host1
878 * host_id_two: ONOS host id for host2
879 Description:
880 Adds a host-to-host intent (bidrectional) by
881 specifying the two hosts.
882 '''
883 try:
884 self.handle.sendline("")
885 self.handle.expect("onos>")
886
887 self.handle.sendline("add-host-intent "+
888 str(host_id_one) + " " + str(host_id_two))
889 self.handle.expect("onos>")
890
andrewonlabe6745342014-10-17 14:29:13 -0400891 handle = self.handle.before
Shreya Shah4f25fdf2014-10-29 19:55:35 -0400892 print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400893
894 main.log.info("Intent installed between "+
895 str(host_id_one) + " and " + str(host_id_two))
896
897 return handle
898
899 except pexpect.EOF:
900 main.log.error(self.name + ": EOF exception found")
901 main.log.error(self.name + ": " + self.handle.before)
902 main.cleanup()
903 main.exit()
904 except:
905 main.log.info(self.name+" ::::::")
906 main.log.error( traceback.print_exc())
907 main.log.info(self.name+" ::::::")
908 main.cleanup()
909 main.exit()
910
andrewonlab7b31d232014-10-24 13:31:47 -0400911 def add_optical_intent(self, ingress_device, egress_device):
912 '''
913 Required:
914 * ingress_device: device id of ingress device
915 * egress_device: device id of egress device
916 Optional:
917 TODO: Still needs to be implemented via dev side
918 '''
919 try:
920 self.handle.sendline("add-optical-intent "+
921 str(ingress_device) + " " + str(egress_device))
922 self.handle.expect("add-optical-intent")
923 i = self.handle.expect([
924 "Error",
925 "onos>"])
926
927 handle = self.handle.before
928
929 #If error, return error message
930 if i == 0:
931 return handle
932 else:
933 return main.TRUE
934
935 except pexpect.EOF:
936 main.log.error(self.name + ": EOF exception found")
937 main.log.error(self.name + ": " + self.handle.before)
938 main.cleanup()
939 main.exit()
940 except:
941 main.log.info(self.name+" ::::::")
942 main.log.error( traceback.print_exc())
943 main.log.info(self.name+" ::::::")
944 main.cleanup()
945 main.exit()
946
andrewonlab4dbb4d82014-10-17 18:22:31 -0400947 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400948 egress_device, port_egress, ethType="", ethSrc="",
andrewonlab40ccd8b2014-11-06 16:23:34 -0500949 ethDst="", bandwidth="", lambda_alloc=False):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400950 '''
951 Required:
952 * ingress_device: device id of ingress device
953 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400954 Optional:
955 * ethType: specify ethType
956 * ethSrc: specify ethSrc (i.e. src mac addr)
957 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500958 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -0500959 * lambda_alloc: if True, intent will allocate lambda
960 for the specified intent
andrewonlab4dbb4d82014-10-17 18:22:31 -0400961 Description:
962 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400963 specifying device id's and optional fields
964
andrewonlab4dbb4d82014-10-17 18:22:31 -0400965 NOTE: This function may change depending on the
966 options developers provide for point-to-point
967 intent via cli
968 '''
969 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400970 cmd = ""
971
972 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500973 if not ethType and not ethSrc and not ethDst\
andrewonlab0a400692014-11-06 14:06:41 -0500974 and not bandwidth and not lambda_alloc:
andrewonlab289e4b72014-10-21 21:24:18 -0400975 cmd = "add-point-intent "+\
976 str(ingress_device) + "/" + str(port_ingress) + " " +\
977 str(egress_device) + "/" + str(port_egress)
978
979 else:
andrewonlab9a130be2014-10-22 12:44:56 -0400980 cmd = "add-point-intent "
981
andrewonlab0c0a6772014-10-22 12:31:18 -0400982 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -0400983 cmd += " --ethType " + str(ethType)
984 if ethSrc:
985 cmd += " --ethSrc " + str(ethSrc)
986 if ethDst:
987 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500988 if bandwidth:
989 cmd += " --bandwidth " + str(bandwidth)
990 if lambda_alloc:
andrewonlab40ccd8b2014-11-06 16:23:34 -0500991 cmd += " --lambda "
andrewonlab289e4b72014-10-21 21:24:18 -0400992
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500993 cmd += " "+str(ingress_device) +\
994 "/" + str(port_ingress) + " " +\
995 str(egress_device) + "/" + str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400996
andrewonlab289e4b72014-10-21 21:24:18 -0400997 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400998 i = self.handle.expect([
999 "Error",
1000 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -04001001
Shreya Shah0f01c812014-10-26 20:15:28 -04001002 self.handle.sendline("intents")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001003 self.handle.expect("onos>")
Shreya Shah0f01c812014-10-26 20:15:28 -04001004 Intenthandle = self.handle.before
andrewonlab4dbb4d82014-10-17 18:22:31 -04001005
1006 if i == 0:
1007 main.log.error("Error in adding point-to-point intent")
1008 return handle
1009 else:
1010 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001011
andrewonlab4dbb4d82014-10-17 18:22:31 -04001012 except pexpect.EOF:
1013 main.log.error(self.name + ": EOF exception found")
1014 main.log.error(self.name + ": " + self.handle.before)
1015 main.cleanup()
1016 main.exit()
1017 except:
1018 main.log.info(self.name+" ::::::")
1019 main.log.error( traceback.print_exc())
1020 main.log.info(self.name+" ::::::")
1021 main.cleanup()
1022 main.exit()
1023
andrewonlab9a50dfe2014-10-17 17:22:31 -04001024 def remove_intent(self, intent_id):
1025 '''
1026 Remove intent for specified intent id
1027 '''
1028 try:
1029 self.handle.sendline("")
1030 self.handle.expect("onos>")
1031
1032 self.handle.sendline("remove-intent "+str(intent_id))
1033 i = self.handle.expect([
1034 "Error",
1035 "onos>"])
1036
1037 handle = self.handle.before
1038
1039 if i == 0:
1040 main.log.error("Error in removing intent")
1041 return handle
1042 else:
1043 return handle
1044
1045 except pexpect.EOF:
1046 main.log.error(self.name + ": EOF exception found")
1047 main.log.error(self.name + ": " + self.handle.before)
1048 main.cleanup()
1049 main.exit()
1050 except:
1051 main.log.info(self.name+" ::::::")
1052 main.log.error( traceback.print_exc())
1053 main.log.info(self.name+" ::::::")
1054 main.cleanup()
1055 main.exit()
1056
andrewonlab377693f2014-10-21 16:00:30 -04001057 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001058 '''
andrewonlab377693f2014-10-21 16:00:30 -04001059 Optional:
1060 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001061 Description:
1062 Obtain intents currently installed
1063 '''
1064 try:
andrewonlab377693f2014-10-21 16:00:30 -04001065 if json_format:
1066 self.handle.sendline("intents -j")
1067 self.handle.expect("intents -j")
1068 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001069 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001070
1071 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1072 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001073 else:
1074 self.handle.sendline("")
1075 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001076
andrewonlab377693f2014-10-21 16:00:30 -04001077 self.handle.sendline("intents")
1078 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001079 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001080
1081 return handle
1082
1083 except pexpect.EOF:
1084 main.log.error(self.name + ": EOF exception found")
1085 main.log.error(self.name + ": " + self.handle.before)
1086 main.cleanup()
1087 main.exit()
1088 except:
1089 main.log.info(self.name+" ::::::")
1090 main.log.error( traceback.print_exc())
1091 main.log.info(self.name+" ::::::")
1092 main.cleanup()
1093 main.exit()
1094
Shreya Shah0f01c812014-10-26 20:15:28 -04001095 def flows(self, json_format = False):
1096 '''
1097 Optional:
1098 * json_format: enable output formatting in json
1099 Description:
1100 Obtain flows currently installed
1101 '''
1102 try:
1103 if json_format:
1104 self.handle.sendline("flows -j")
1105 self.handle.expect("flows -j")
1106 self.handle.expect("onos>")
1107 handle = self.handle.before
1108
1109 else:
1110 self.handle.sendline("")
1111 self.handle.expect("onos>")
1112 self.handle.sendline("flows")
1113 self.handle.expect("onos>")
1114 handle = self.handle.before
1115
1116 return handle
1117
1118 except pexpect.EOF:
1119 main.log.error(self.name + ": EOF exception found")
1120 main.log.error(self.name + ": " + self.handle.before)
1121 main.cleanup()
1122 main.exit()
1123 except:
1124 main.log.info(self.name+" ::::::")
1125 main.log.error( traceback.print_exc())
1126 main.log.info(self.name+" ::::::")
1127 main.cleanup()
1128 main.exit()
1129
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001130 def intents_events_metrics(self, json_format=True):
1131 '''
1132 Description:Returns topology metrics
1133 Optional:
1134 * json_format: enable json formatting of output
1135 '''
1136 try:
1137 if json_format:
1138 self.handle.sendline("intents-events-metrics -j")
1139 self.handle.expect("intents-events-metrics -j")
1140 self.handle.expect("onos>")
1141
1142 handle = self.handle.before
1143
1144 #Some color thing that we want to escape
1145 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1146 handle = ansi_escape.sub('', handle)
1147
1148 else:
1149 self.handle.sendline("intents-events-metrics")
1150 self.handle.expect("intents-events-metrics")
1151 self.handle.expect("onos>")
1152
1153 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001154
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001155 return handle
1156
1157 except pexpect.EOF:
1158 main.log.error(self.name + ": EOF exception found")
1159 main.log.error(self.name + ": " + self.handle.before)
1160 main.cleanup()
1161 main.exit()
1162 except:
1163 main.log.info(self.name+" ::::::")
1164 main.log.error( traceback.print_exc())
1165 main.log.info(self.name+" ::::::")
1166 main.cleanup()
1167 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001168
andrewonlab867212a2014-10-22 20:13:38 -04001169 def topology_events_metrics(self, json_format=True):
1170 '''
1171 Description:Returns topology metrics
1172 Optional:
1173 * json_format: enable json formatting of output
1174 '''
1175 try:
1176 if json_format:
1177 self.handle.sendline("topology-events-metrics -j")
1178 self.handle.expect("topology-events-metrics -j")
1179 self.handle.expect("onos>")
1180
1181 handle = self.handle.before
1182
1183 #Some color thing that we want to escape
1184 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1185 handle = ansi_escape.sub('', handle)
1186
1187 else:
1188 self.handle.sendline("topology-events-metrics")
1189 self.handle.expect("topology-events-metrics")
1190 self.handle.expect("onos>")
1191
1192 handle = self.handle.before
1193
1194 return handle
1195
1196 except pexpect.EOF:
1197 main.log.error(self.name + ": EOF exception found")
1198 main.log.error(self.name + ": " + self.handle.before)
1199 main.cleanup()
1200 main.exit()
1201 except:
1202 main.log.info(self.name+" ::::::")
1203 main.log.error( traceback.print_exc())
1204 main.log.info(self.name+" ::::::")
1205 main.cleanup()
1206 main.exit()
1207
andrewonlab3e15ead2014-10-15 14:21:34 -04001208 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001209 #Wrapper functions use existing driver
1210 #functions and extends their use case.
1211 #For example, we may use the output of
1212 #a normal driver function, and parse it
1213 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001214
andrewonlab9a50dfe2014-10-17 17:22:31 -04001215 def get_all_intents_id(self):
1216 '''
1217 Description:
1218 Obtain all intent id's in a list
1219 '''
1220 try:
1221 #Obtain output of intents function
1222 intents_str = self.intents()
1223 all_intent_list = []
1224 intent_id_list = []
1225
1226 #Parse the intents output for ID's
1227 intents_list = [s.strip() for s in intents_str.splitlines()]
1228 for intents in intents_list:
1229 if "onos>" in intents:
1230 continue
1231 elif "intents" in intents:
1232 continue
1233 else:
1234 line_list = intents.split(" ")
1235 all_intent_list.append(line_list[0])
1236
1237 all_intent_list = all_intent_list[1:-2]
1238
1239 for intents in all_intent_list:
1240 if not intents:
1241 continue
1242 else:
1243 intent_id_list.append(intents)
1244
1245 return intent_id_list
1246
1247 except pexpect.EOF:
1248 main.log.error(self.name + ": EOF exception found")
1249 main.log.error(self.name + ": " + self.handle.before)
1250 main.cleanup()
1251 main.exit()
1252 except:
1253 main.log.info(self.name+" ::::::")
1254 main.log.error( traceback.print_exc())
1255 main.log.info(self.name+" ::::::")
1256 main.cleanup()
1257 main.exit()
1258
andrewonlab7e4d2d32014-10-15 13:23:21 -04001259 def get_all_devices_id(self):
1260 '''
1261 Use 'devices' function to obtain list of all devices
1262 and parse the result to obtain a list of all device
1263 id's. Returns this list. Returns empty list if no
1264 devices exist
1265 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001266
1267 This function may be useful if you are not sure of the
1268 device id, and wish to execute other commands using
1269 the ids. By obtaining the list of device ids on the fly,
1270 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001271 '''
1272 try:
1273 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001274 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001275 id_list = []
1276
1277 if not devices_str:
1278 main.log.info("There are no devices to get id from")
1279 return id_list
1280
1281 #Split the string into list by comma
1282 device_list = devices_str.split(",")
1283 #Get temporary list of all arguments with string 'id='
1284 temp_list = [dev for dev in device_list if "id=" in dev]
1285 #Split list further into arguments before and after string
1286 # 'id='. Get the latter portion (the actual device id) and
1287 # append to id_list
1288 for arg in temp_list:
1289 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001290 return id_list
1291
1292 except pexpect.EOF:
1293 main.log.error(self.name + ": EOF exception found")
1294 main.log.error(self.name + ": " + self.handle.before)
1295 main.cleanup()
1296 main.exit()
1297 except:
1298 main.log.info(self.name+" ::::::")
1299 main.log.error( traceback.print_exc())
1300 main.log.info(self.name+" ::::::")
1301 main.cleanup()
1302 main.exit()
1303
andrewonlab7c211572014-10-15 16:45:20 -04001304 def get_all_nodes_id(self):
1305 '''
1306 Uses 'nodes' function to obtain list of all nodes
1307 and parse the result of nodes to obtain just the
1308 node id's.
1309 Returns:
1310 list of node id's
1311 '''
1312 try:
1313 nodes_str = self.nodes()
1314 id_list = []
1315
1316 if not nodes_str:
1317 main.log.info("There are no nodes to get id from")
1318 return id_list
1319
1320 #Sample nodes_str output
1321 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1322
1323 #Split the string into list by comma
1324 nodes_list = nodes_str.split(",")
1325 temp_list = [node for node in nodes_list if "id=" in node]
1326 for arg in temp_list:
1327 id_list.append(arg.split("id=")[1])
1328
1329 return id_list
1330
1331 except pexpect.EOF:
1332 main.log.error(self.name + ": EOF exception found")
1333 main.log.error(self.name + ": " + self.handle.before)
1334 main.cleanup()
1335 main.exit()
1336 except:
1337 main.log.info(self.name+" ::::::")
1338 main.log.error( traceback.print_exc())
1339 main.log.info(self.name+" ::::::")
1340 main.cleanup()
1341 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001342
Jon Halla91c4dc2014-10-22 12:57:04 -04001343 def get_device(self, dpid=None):
1344 '''
1345 Return the first device from the devices api whose 'id' contains 'dpid'
1346 Return None if there is no match
1347 '''
1348 import json
1349 try:
1350 if dpid == None:
1351 return None
1352 else:
1353 dpid = dpid.replace(':', '')
1354 raw_devices = self.devices()
1355 devices_json = json.loads(raw_devices)
1356 #search json for the device with dpid then return the device
1357 for device in devices_json:
1358 #print "%s in %s?" % (dpid, device['id'])
1359 if dpid in device['id']:
1360 return device
1361 return None
1362 except pexpect.EOF:
1363 main.log.error(self.name + ": EOF exception found")
1364 main.log.error(self.name + ": " + self.handle.before)
1365 main.cleanup()
1366 main.exit()
1367 except:
1368 main.log.info(self.name+" ::::::")
1369 main.log.error( traceback.print_exc())
1370 main.log.info(self.name+" ::::::")
1371 main.cleanup()
1372 main.exit()
1373
Jon Hall42db6dc2014-10-24 19:03:48 -04001374 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1375 '''
1376 Checks the number of swithes & links that ONOS sees against the
1377 supplied values. By default this will report to main.log, but the
1378 log level can be specifid.
1379
1380 Params: ip = ip used for the onos cli
1381 numoswitch = expected number of switches
1382 numlink = expected number of links
1383 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1384
1385
1386 log_level can
1387
1388 Returns: main.TRUE if the number of switchs and links are correct,
1389 main.FALSE if the numer of switches and links is incorrect,
1390 and main.ERROR otherwise
1391 '''
1392
1393 try:
1394 topology = self.get_topology(ip)
1395 if topology == {}:
1396 return main.ERROR
1397 output = ""
1398 #Is the number of switches is what we expected
1399 devices = topology.get('devices',False)
1400 links = topology.get('links',False)
1401 if devices == False or links == False:
1402 return main.ERROR
1403 switch_check = ( int(devices) == int(numoswitch) )
1404 #Is the number of links is what we expected
1405 link_check = ( int(links) == int(numolink) )
1406 if (switch_check and link_check):
1407 #We expected the correct numbers
1408 output = output + "The number of links and switches match "\
1409 + "what was expected"
1410 result = main.TRUE
1411 else:
1412 output = output + \
1413 "The number of links and switches does not match what was expected"
1414 result = main.FALSE
1415 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1416 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1417 if log_level == "report":
1418 main.log.report(output)
1419 elif log_level == "warn":
1420 main.log.warn(output)
1421 else:
1422 main.log.info(output)
1423 return result
1424 except pexpect.EOF:
1425 main.log.error(self.name + ": EOF exception found")
1426 main.log.error(self.name + ": " + self.handle.before)
1427 main.cleanup()
1428 main.exit()
1429 except:
1430 main.log.info(self.name+" ::::::")
1431 main.log.error( traceback.print_exc())
1432 main.log.info(self.name+" ::::::")
1433 main.cleanup()
1434 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001435
1436 def device_role(self, device_id, onos_node, role="master"):
1437 '''
1438 Calls the device-role cli command.
1439 device_id must be the id of a device as seen in the onos devices command
1440 onos_node is the ip of one of the onos nodes in the cluster
1441 role must be either master, standby, or none
1442
Jon Hall983a1702014-10-28 18:44:22 -04001443 Returns main.TRUE or main.FALSE based argument varification.
1444 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001445 support that output
1446 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001447 try:
Jon Hall983a1702014-10-28 18:44:22 -04001448 #print "beginning device_role... \n\tdevice_id:" + device_id
1449 #print "\tonos_node: " + onos_node
1450 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001451 if role.lower() == "master" or \
1452 role.lower() == "standby" or \
1453 role.lower() == "none":
1454 self.handle.sendline("")
1455 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001456 self.handle.sendline("device-role " +
1457 str(device_id) + " " +
1458 str(onos_node) + " " +
1459 str(role))
1460 i= self.handle.expect(["Error","onos>"])
1461 if i == 0:
1462 output = str(self.handle.before)
1463 self.handle.expect("onos>")
1464 output = output + str(self.handle.before)
1465 main.log.error(self.name + ": " +
1466 output + '\033[0m')#end color output to escape any colours from the cli
1467 return main.ERROR
1468 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001469 self.handle.expect("onos>")
1470 return main.TRUE
1471 else:
1472 return main.FALSE
1473
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()
1485
1486
andrewonlab7e4d2d32014-10-15 13:23:21 -04001487 #***********************************