blob: d93567f7e8d40d85a25bd70c220683fb266d7871 [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>")
521 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400522 self.handle.expect("onos>")
523 else:
524 self.handle.sendline("links | grep '"+
525 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400526 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400527 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400528 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400529 return handle
Jon Halle8217482014-10-17 13:49:14 -0400530 except pexpect.EOF:
531 main.log.error(self.name + ": EOF exception found")
532 main.log.error(self.name + ": " + self.handle.before)
533 main.cleanup()
534 main.exit()
535 except:
536 main.log.info(self.name+" ::::::")
537 main.log.error( traceback.print_exc())
538 main.log.info(self.name+" ::::::")
539 main.cleanup()
540 main.exit()
541
542
543 def ports(self, json_format=True, grep_str=""):
544 '''
545 Lists all ports
546 Optional argument:
547 * grep_str - pass in a string to grep
548 '''
549 try:
550 self.handle.sendline("")
551 self.handle.expect("onos>")
552
553 if json_format:
554 if not grep_str:
555 self.handle.sendline("ports -j")
556 self.handle.expect("ports -j")
557 self.handle.expect("onos>")
558 else:
559 self.handle.sendline("ports -j | grep '"+
560 str(grep_str)+"'")
561 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
562 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400563 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400564 '''
565 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
566 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 -0400567 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 -0400568 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400569 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
570 handle1 = ansi_escape.sub('', handle)
571 '''
572 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400573 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
574 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400575 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400576 return handle1
577
Jon Halle8217482014-10-17 13:49:14 -0400578 else:
579 if not grep_str:
580 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400581 self.handle.expect("onos>")
582 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400583 self.handle.expect("onos>")
584 else:
585 self.handle.sendline("ports | grep '"+
586 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400587 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400588 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400589 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400590 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400591 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400592 return handle
Jon Halle8217482014-10-17 13:49:14 -0400593 except pexpect.EOF:
594 main.log.error(self.name + ": EOF exception found")
595 main.log.error(self.name + ": " + self.handle.before)
596 main.cleanup()
597 main.exit()
598 except:
599 main.log.info(self.name+" ::::::")
600 main.log.error( traceback.print_exc())
601 main.log.info(self.name+" ::::::")
602 main.cleanup()
603 main.exit()
604
605
Jon Hall983a1702014-10-28 18:44:22 -0400606 def roles(self, json_format=True, grep_str=""):
andrewonlab7c211572014-10-15 16:45:20 -0400607 '''
Jon Hall983a1702014-10-28 18:44:22 -0400608 Lists all devices and the controllers with roles assigned to them
609 Optional argument:
610 * grep_str - pass in a string to grep
andrewonlab7c211572014-10-15 16:45:20 -0400611 '''
612 try:
613 self.handle.sendline("")
614 self.handle.expect("onos>")
andrewonlab7c211572014-10-15 16:45:20 -0400615
Jon Hall983a1702014-10-28 18:44:22 -0400616 if json_format:
617 if not grep_str:
618 self.handle.sendline("roles -j")
619 self.handle.expect("roles -j")
620 self.handle.expect("onos>")
621 else:
622 self.handle.sendline("roles -j | grep '"+
623 str(grep_str)+"'")
624 self.handle.expect("roles -j | grep '"+str(grep_str)+"'")
625 self.handle.expect("onos>")
626 handle = self.handle.before
627 '''
628 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
629 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
630 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
631 So we take off that escape sequence using the following commads:
632 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
633 handle1 = ansi_escape.sub('', handle)
634 '''
635 #print "repr(handle) =", repr(handle)
636 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
637 handle1 = ansi_escape.sub('', handle)
638 #print "repr(handle1) = ", repr(handle1)
639 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400640
andrewonlab7c211572014-10-15 16:45:20 -0400641 else:
Jon Hall983a1702014-10-28 18:44:22 -0400642 if not grep_str:
643 self.handle.sendline("roles")
644 self.handle.expect("onos>")
645 self.handle.sendline("")
646 self.handle.expect("onos>")
647 else:
648 self.handle.sendline("roles | grep '"+
649 str(grep_str)+"'")
650 self.handle.expect("onos>")
651 self.handle.sendline("")
652 self.handle.expect("onos>")
653 handle = self.handle.before
654 #print "handle =",handle
655 return handle
656 except pexpect.EOF:
657 main.log.error(self.name + ": EOF exception found")
658 main.log.error(self.name + ": " + self.handle.before)
659 main.cleanup()
660 main.exit()
661 except:
662 main.log.info(self.name+" ::::::")
663 main.log.error( traceback.print_exc())
664 main.log.info(self.name+" ::::::")
665 main.cleanup()
666 main.exit()
667
668 def get_role(self, device_id):
669 '''
670 Given the a string containing the json representation of the "roles" cli command and a
671 partial or whole device id, returns a json object containing the
672 roles output for the first device whose id contains "device_id"
673
674 Returns:
675 Dict of the role assignments for the given device or
676 None if not match
677 '''
678 try:
679 import json
680 if device_id == None:
681 return None
682 else:
683 raw_roles = self.roles()
684 roles_json = json.loads(raw_roles)
685 #search json for the device with id then return the device
686 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400687 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400688 if str(device_id) in device['id']:
689 return device
690 return None
andrewonlab7c211572014-10-15 16:45:20 -0400691
andrewonlab86dc3082014-10-13 18:18:38 -0400692 except pexpect.EOF:
693 main.log.error(self.name + ": EOF exception found")
694 main.log.error(self.name + ": " + self.handle.before)
695 main.cleanup()
696 main.exit()
697 except:
698 main.log.info(self.name+" ::::::")
699 main.log.error( traceback.print_exc())
700 main.log.info(self.name+" ::::::")
701 main.cleanup()
702 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400703
andrewonlab3e15ead2014-10-15 14:21:34 -0400704 def paths(self, src_id, dst_id):
705 '''
706 Returns string of paths, and the cost.
707 Issues command: onos:paths <src> <dst>
708 '''
709 try:
710 self.handle.sendline("")
711 self.handle.expect("onos>")
712
713 self.handle.sendline("onos:paths "+
714 str(src_id) + " " + str(dst_id))
715 i = self.handle.expect([
716 "Error",
717 "onos>"])
718
719 self.handle.sendline("")
720 self.handle.expect("onos>")
721
722 handle = self.handle.before
723
724 if i == 0:
725 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400726 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400727 else:
728 path = handle.split(";")[0]
729 cost = handle.split(";")[1]
730 return (path, cost)
731
732 except pexpect.EOF:
733 main.log.error(self.name + ": EOF exception found")
734 main.log.error(self.name + ": " + self.handle.before)
735 main.cleanup()
736 main.exit()
737 except:
738 main.log.info(self.name+" ::::::")
739 main.log.error( traceback.print_exc())
740 main.log.info(self.name+" ::::::")
741 main.cleanup()
742 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400743
Jon Hall42db6dc2014-10-24 19:03:48 -0400744 def hosts(self, json_format=True, grep_str=""):
745 '''
746 Lists all discovered hosts
747 Optional argument:
748 * grep_str - pass in a string to grep
749 '''
750 try:
751 self.handle.sendline("")
752 self.handle.expect("onos>")
753
754 if json_format:
755 if not grep_str:
756 self.handle.sendline("hosts -j")
757 self.handle.expect("hosts -j")
758 self.handle.expect("onos>")
759 else:
760 self.handle.sendline("hosts -j | grep '"+
761 str(grep_str)+"'")
762 self.handle.expect("hosts -j | grep '"+str(grep_str)+"'")
763 self.handle.expect("onos>")
764 handle = self.handle.before
765 '''
766 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
767 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
768 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
769 So we take off that escape sequence using
770 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
771 handle1 = ansi_escape.sub('', handle)
772 '''
773 #print "repr(handle) =", repr(handle)
774 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
775 handle1 = ansi_escape.sub('', handle)
776 #print "repr(handle1) = ", repr(handle1)
777 return handle1
778 else:
779 if not grep_str:
780 self.handle.sendline("hosts")
781 self.handle.expect("onos>")
782 else:
783 self.handle.sendline("hosts | grep '"+
784 str(grep_str)+"'")
785 self.handle.expect("onos>")
786 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400787 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400788 return handle
789 except pexpect.EOF:
790 main.log.error(self.name + ": EOF exception found")
791 main.log.error(self.name + ": " + self.handle.before)
792 main.cleanup()
793 main.exit()
794 except:
795 main.log.info(self.name+" ::::::")
796 main.log.error( traceback.print_exc())
797 main.log.info(self.name+" ::::::")
798 main.cleanup()
799 main.exit()
800
801 def get_host(self, mac):
802 '''
803 Return the first host from the hosts api whose 'id' contains 'mac'
804 Note: mac must be a colon seperated mac address, but could be a partial mac address
805 Return None if there is no match
806 '''
807 import json
808 try:
809 if mac == None:
810 return None
811 else:
812 mac = mac
813 raw_hosts = self.hosts()
814 hosts_json = json.loads(raw_hosts)
815 #search json for the host with mac then return the device
816 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400817 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400818 if mac in host['id']:
819 return host
820 return None
821 except pexpect.EOF:
822 main.log.error(self.name + ": EOF exception found")
823 main.log.error(self.name + ": " + self.handle.before)
824 main.cleanup()
825 main.exit()
826 except:
827 main.log.info(self.name+" ::::::")
828 main.log.error( traceback.print_exc())
829 main.log.info(self.name+" ::::::")
830 main.cleanup()
831 main.exit()
832
andrewonlab3f0a4af2014-10-17 12:25:14 -0400833
834 def get_hosts_id(self, host_list):
835 '''
836 Obtain list of hosts
837 Issues command: 'onos> hosts'
838
839 Required:
840 * host_list: List of hosts obtained by Mininet
841 IMPORTANT:
842 This function assumes that you started your
843 topology with the option '--mac'.
844 Furthermore, it assumes that value of VLAN is '-1'
845 Description:
846 Converts mininet hosts (h1, h2, h3...) into
847 ONOS format (00:00:00:00:00:01/-1 , ...)
848 '''
849
850 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400851 onos_host_list = []
852
853 for host in host_list:
854 host = host.replace("h", "")
855 host_hex = hex(int(host)).zfill(12)
856 host_hex = str(host_hex).replace('x','0')
857 i = iter(str(host_hex))
858 host_hex = ":".join(a+b for a,b in zip(i,i))
859 host_hex = host_hex + "/-1"
860 onos_host_list.append(host_hex)
861
862 return onos_host_list
863
864 except pexpect.EOF:
865 main.log.error(self.name + ": EOF exception found")
866 main.log.error(self.name + ": " + self.handle.before)
867 main.cleanup()
868 main.exit()
869 except:
870 main.log.info(self.name+" ::::::")
871 main.log.error( traceback.print_exc())
872 main.log.info(self.name+" ::::::")
873 main.cleanup()
874 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400875
andrewonlabe6745342014-10-17 14:29:13 -0400876 def add_host_intent(self, host_id_one, host_id_two):
877 '''
878 Required:
879 * host_id_one: ONOS host id for host1
880 * host_id_two: ONOS host id for host2
881 Description:
882 Adds a host-to-host intent (bidrectional) by
883 specifying the two hosts.
884 '''
885 try:
886 self.handle.sendline("")
887 self.handle.expect("onos>")
888
889 self.handle.sendline("add-host-intent "+
890 str(host_id_one) + " " + str(host_id_two))
891 self.handle.expect("onos>")
892
andrewonlabe6745342014-10-17 14:29:13 -0400893 handle = self.handle.before
Shreya Shah4f25fdf2014-10-29 19:55:35 -0400894 print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400895
896 main.log.info("Intent installed between "+
897 str(host_id_one) + " and " + str(host_id_two))
898
899 return handle
900
901 except pexpect.EOF:
902 main.log.error(self.name + ": EOF exception found")
903 main.log.error(self.name + ": " + self.handle.before)
904 main.cleanup()
905 main.exit()
906 except:
907 main.log.info(self.name+" ::::::")
908 main.log.error( traceback.print_exc())
909 main.log.info(self.name+" ::::::")
910 main.cleanup()
911 main.exit()
912
andrewonlab7b31d232014-10-24 13:31:47 -0400913 def add_optical_intent(self, ingress_device, egress_device):
914 '''
915 Required:
916 * ingress_device: device id of ingress device
917 * egress_device: device id of egress device
918 Optional:
919 TODO: Still needs to be implemented via dev side
920 '''
921 try:
922 self.handle.sendline("add-optical-intent "+
923 str(ingress_device) + " " + str(egress_device))
924 self.handle.expect("add-optical-intent")
925 i = self.handle.expect([
926 "Error",
927 "onos>"])
928
929 handle = self.handle.before
930
931 #If error, return error message
932 if i == 0:
933 return handle
934 else:
935 return main.TRUE
936
937 except pexpect.EOF:
938 main.log.error(self.name + ": EOF exception found")
939 main.log.error(self.name + ": " + self.handle.before)
940 main.cleanup()
941 main.exit()
942 except:
943 main.log.info(self.name+" ::::::")
944 main.log.error( traceback.print_exc())
945 main.log.info(self.name+" ::::::")
946 main.cleanup()
947 main.exit()
948
andrewonlab4dbb4d82014-10-17 18:22:31 -0400949 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400950 egress_device, port_egress, ethType="", ethSrc="",
andrewonlab40ccd8b2014-11-06 16:23:34 -0500951 ethDst="", bandwidth="", lambda_alloc=False):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400952 '''
953 Required:
954 * ingress_device: device id of ingress device
955 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400956 Optional:
957 * ethType: specify ethType
958 * ethSrc: specify ethSrc (i.e. src mac addr)
959 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500960 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -0500961 * lambda_alloc: if True, intent will allocate lambda
962 for the specified intent
andrewonlab4dbb4d82014-10-17 18:22:31 -0400963 Description:
964 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400965 specifying device id's and optional fields
966
andrewonlab4dbb4d82014-10-17 18:22:31 -0400967 NOTE: This function may change depending on the
968 options developers provide for point-to-point
969 intent via cli
970 '''
971 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400972 cmd = ""
973
974 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500975 if not ethType and not ethSrc and not ethDst\
andrewonlab0a400692014-11-06 14:06:41 -0500976 and not bandwidth and not lambda_alloc:
andrewonlab289e4b72014-10-21 21:24:18 -0400977 cmd = "add-point-intent "+\
978 str(ingress_device) + "/" + str(port_ingress) + " " +\
979 str(egress_device) + "/" + str(port_egress)
980
981 else:
andrewonlab9a130be2014-10-22 12:44:56 -0400982 cmd = "add-point-intent "
983
andrewonlab0c0a6772014-10-22 12:31:18 -0400984 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -0400985 cmd += " --ethType " + str(ethType)
986 if ethSrc:
987 cmd += " --ethSrc " + str(ethSrc)
988 if ethDst:
989 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500990 if bandwidth:
991 cmd += " --bandwidth " + str(bandwidth)
992 if lambda_alloc:
andrewonlab40ccd8b2014-11-06 16:23:34 -0500993 cmd += " --lambda "
andrewonlab289e4b72014-10-21 21:24:18 -0400994
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500995 cmd += " "+str(ingress_device) +\
996 "/" + str(port_ingress) + " " +\
997 str(egress_device) + "/" + str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400998
andrewonlab289e4b72014-10-21 21:24:18 -0400999 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001000 i = self.handle.expect([
1001 "Error",
1002 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -04001003
Shreya Shah0f01c812014-10-26 20:15:28 -04001004 self.handle.sendline("intents")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001005 self.handle.expect("onos>")
Shreya Shah0f01c812014-10-26 20:15:28 -04001006 Intenthandle = self.handle.before
andrewonlab4dbb4d82014-10-17 18:22:31 -04001007
1008 if i == 0:
1009 main.log.error("Error in adding point-to-point intent")
1010 return handle
1011 else:
1012 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001013
andrewonlab4dbb4d82014-10-17 18:22:31 -04001014 except pexpect.EOF:
1015 main.log.error(self.name + ": EOF exception found")
1016 main.log.error(self.name + ": " + self.handle.before)
1017 main.cleanup()
1018 main.exit()
1019 except:
1020 main.log.info(self.name+" ::::::")
1021 main.log.error( traceback.print_exc())
1022 main.log.info(self.name+" ::::::")
1023 main.cleanup()
1024 main.exit()
1025
andrewonlab9a50dfe2014-10-17 17:22:31 -04001026 def remove_intent(self, intent_id):
1027 '''
1028 Remove intent for specified intent id
1029 '''
1030 try:
1031 self.handle.sendline("")
1032 self.handle.expect("onos>")
1033
1034 self.handle.sendline("remove-intent "+str(intent_id))
1035 i = self.handle.expect([
1036 "Error",
1037 "onos>"])
1038
1039 handle = self.handle.before
1040
1041 if i == 0:
1042 main.log.error("Error in removing intent")
1043 return handle
1044 else:
1045 return handle
1046
1047 except pexpect.EOF:
1048 main.log.error(self.name + ": EOF exception found")
1049 main.log.error(self.name + ": " + self.handle.before)
1050 main.cleanup()
1051 main.exit()
1052 except:
1053 main.log.info(self.name+" ::::::")
1054 main.log.error( traceback.print_exc())
1055 main.log.info(self.name+" ::::::")
1056 main.cleanup()
1057 main.exit()
1058
andrewonlab377693f2014-10-21 16:00:30 -04001059 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001060 '''
andrewonlab377693f2014-10-21 16:00:30 -04001061 Optional:
1062 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001063 Description:
1064 Obtain intents currently installed
1065 '''
1066 try:
andrewonlab377693f2014-10-21 16:00:30 -04001067 if json_format:
1068 self.handle.sendline("intents -j")
1069 self.handle.expect("intents -j")
1070 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001071 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001072
1073 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1074 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001075 else:
1076 self.handle.sendline("")
1077 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001078
andrewonlab377693f2014-10-21 16:00:30 -04001079 self.handle.sendline("intents")
1080 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001081 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001082
1083 return handle
1084
1085 except pexpect.EOF:
1086 main.log.error(self.name + ": EOF exception found")
1087 main.log.error(self.name + ": " + self.handle.before)
1088 main.cleanup()
1089 main.exit()
1090 except:
1091 main.log.info(self.name+" ::::::")
1092 main.log.error( traceback.print_exc())
1093 main.log.info(self.name+" ::::::")
1094 main.cleanup()
1095 main.exit()
1096
Shreya Shah0f01c812014-10-26 20:15:28 -04001097 def flows(self, json_format = False):
1098 '''
1099 Optional:
1100 * json_format: enable output formatting in json
1101 Description:
1102 Obtain flows currently installed
1103 '''
1104 try:
1105 if json_format:
1106 self.handle.sendline("flows -j")
1107 self.handle.expect("flows -j")
1108 self.handle.expect("onos>")
1109 handle = self.handle.before
1110
1111 else:
1112 self.handle.sendline("")
1113 self.handle.expect("onos>")
1114 self.handle.sendline("flows")
1115 self.handle.expect("onos>")
1116 handle = self.handle.before
1117
1118 return handle
1119
1120 except pexpect.EOF:
1121 main.log.error(self.name + ": EOF exception found")
1122 main.log.error(self.name + ": " + self.handle.before)
1123 main.cleanup()
1124 main.exit()
1125 except:
1126 main.log.info(self.name+" ::::::")
1127 main.log.error( traceback.print_exc())
1128 main.log.info(self.name+" ::::::")
1129 main.cleanup()
1130 main.exit()
1131
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001132 def intents_events_metrics(self, json_format=True):
1133 '''
1134 Description:Returns topology metrics
1135 Optional:
1136 * json_format: enable json formatting of output
1137 '''
1138 try:
1139 if json_format:
1140 self.handle.sendline("intents-events-metrics -j")
1141 self.handle.expect("intents-events-metrics -j")
1142 self.handle.expect("onos>")
1143
1144 handle = self.handle.before
1145
1146 #Some color thing that we want to escape
1147 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1148 handle = ansi_escape.sub('', handle)
1149
1150 else:
1151 self.handle.sendline("intents-events-metrics")
1152 self.handle.expect("intents-events-metrics")
1153 self.handle.expect("onos>")
1154
1155 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001156
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001157 return handle
1158
1159 except pexpect.EOF:
1160 main.log.error(self.name + ": EOF exception found")
1161 main.log.error(self.name + ": " + self.handle.before)
1162 main.cleanup()
1163 main.exit()
1164 except:
1165 main.log.info(self.name+" ::::::")
1166 main.log.error( traceback.print_exc())
1167 main.log.info(self.name+" ::::::")
1168 main.cleanup()
1169 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001170
andrewonlab867212a2014-10-22 20:13:38 -04001171 def topology_events_metrics(self, json_format=True):
1172 '''
1173 Description:Returns topology metrics
1174 Optional:
1175 * json_format: enable json formatting of output
1176 '''
1177 try:
1178 if json_format:
1179 self.handle.sendline("topology-events-metrics -j")
1180 self.handle.expect("topology-events-metrics -j")
1181 self.handle.expect("onos>")
1182
1183 handle = self.handle.before
1184
1185 #Some color thing that we want to escape
1186 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1187 handle = ansi_escape.sub('', handle)
1188
1189 else:
1190 self.handle.sendline("topology-events-metrics")
1191 self.handle.expect("topology-events-metrics")
1192 self.handle.expect("onos>")
1193
1194 handle = self.handle.before
1195
1196 return handle
1197
1198 except pexpect.EOF:
1199 main.log.error(self.name + ": EOF exception found")
1200 main.log.error(self.name + ": " + self.handle.before)
1201 main.cleanup()
1202 main.exit()
1203 except:
1204 main.log.info(self.name+" ::::::")
1205 main.log.error( traceback.print_exc())
1206 main.log.info(self.name+" ::::::")
1207 main.cleanup()
1208 main.exit()
1209
andrewonlab3e15ead2014-10-15 14:21:34 -04001210 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001211 #Wrapper functions use existing driver
1212 #functions and extends their use case.
1213 #For example, we may use the output of
1214 #a normal driver function, and parse it
1215 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001216
andrewonlab9a50dfe2014-10-17 17:22:31 -04001217 def get_all_intents_id(self):
1218 '''
1219 Description:
1220 Obtain all intent id's in a list
1221 '''
1222 try:
1223 #Obtain output of intents function
1224 intents_str = self.intents()
1225 all_intent_list = []
1226 intent_id_list = []
1227
1228 #Parse the intents output for ID's
1229 intents_list = [s.strip() for s in intents_str.splitlines()]
1230 for intents in intents_list:
1231 if "onos>" in intents:
1232 continue
1233 elif "intents" in intents:
1234 continue
1235 else:
1236 line_list = intents.split(" ")
1237 all_intent_list.append(line_list[0])
1238
1239 all_intent_list = all_intent_list[1:-2]
1240
1241 for intents in all_intent_list:
1242 if not intents:
1243 continue
1244 else:
1245 intent_id_list.append(intents)
1246
1247 return intent_id_list
1248
1249 except pexpect.EOF:
1250 main.log.error(self.name + ": EOF exception found")
1251 main.log.error(self.name + ": " + self.handle.before)
1252 main.cleanup()
1253 main.exit()
1254 except:
1255 main.log.info(self.name+" ::::::")
1256 main.log.error( traceback.print_exc())
1257 main.log.info(self.name+" ::::::")
1258 main.cleanup()
1259 main.exit()
1260
andrewonlab7e4d2d32014-10-15 13:23:21 -04001261 def get_all_devices_id(self):
1262 '''
1263 Use 'devices' function to obtain list of all devices
1264 and parse the result to obtain a list of all device
1265 id's. Returns this list. Returns empty list if no
1266 devices exist
1267 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001268
1269 This function may be useful if you are not sure of the
1270 device id, and wish to execute other commands using
1271 the ids. By obtaining the list of device ids on the fly,
1272 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001273 '''
1274 try:
1275 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001276 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001277 id_list = []
1278
1279 if not devices_str:
1280 main.log.info("There are no devices to get id from")
1281 return id_list
1282
1283 #Split the string into list by comma
1284 device_list = devices_str.split(",")
1285 #Get temporary list of all arguments with string 'id='
1286 temp_list = [dev for dev in device_list if "id=" in dev]
1287 #Split list further into arguments before and after string
1288 # 'id='. Get the latter portion (the actual device id) and
1289 # append to id_list
1290 for arg in temp_list:
1291 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001292 return id_list
1293
1294 except pexpect.EOF:
1295 main.log.error(self.name + ": EOF exception found")
1296 main.log.error(self.name + ": " + self.handle.before)
1297 main.cleanup()
1298 main.exit()
1299 except:
1300 main.log.info(self.name+" ::::::")
1301 main.log.error( traceback.print_exc())
1302 main.log.info(self.name+" ::::::")
1303 main.cleanup()
1304 main.exit()
1305
andrewonlab7c211572014-10-15 16:45:20 -04001306 def get_all_nodes_id(self):
1307 '''
1308 Uses 'nodes' function to obtain list of all nodes
1309 and parse the result of nodes to obtain just the
1310 node id's.
1311 Returns:
1312 list of node id's
1313 '''
1314 try:
1315 nodes_str = self.nodes()
1316 id_list = []
1317
1318 if not nodes_str:
1319 main.log.info("There are no nodes to get id from")
1320 return id_list
1321
1322 #Sample nodes_str output
1323 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1324
1325 #Split the string into list by comma
1326 nodes_list = nodes_str.split(",")
1327 temp_list = [node for node in nodes_list if "id=" in node]
1328 for arg in temp_list:
1329 id_list.append(arg.split("id=")[1])
1330
1331 return id_list
1332
1333 except pexpect.EOF:
1334 main.log.error(self.name + ": EOF exception found")
1335 main.log.error(self.name + ": " + self.handle.before)
1336 main.cleanup()
1337 main.exit()
1338 except:
1339 main.log.info(self.name+" ::::::")
1340 main.log.error( traceback.print_exc())
1341 main.log.info(self.name+" ::::::")
1342 main.cleanup()
1343 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001344
Jon Halla91c4dc2014-10-22 12:57:04 -04001345 def get_device(self, dpid=None):
1346 '''
1347 Return the first device from the devices api whose 'id' contains 'dpid'
1348 Return None if there is no match
1349 '''
1350 import json
1351 try:
1352 if dpid == None:
1353 return None
1354 else:
1355 dpid = dpid.replace(':', '')
1356 raw_devices = self.devices()
1357 devices_json = json.loads(raw_devices)
1358 #search json for the device with dpid then return the device
1359 for device in devices_json:
1360 #print "%s in %s?" % (dpid, device['id'])
1361 if dpid in device['id']:
1362 return device
1363 return None
1364 except pexpect.EOF:
1365 main.log.error(self.name + ": EOF exception found")
1366 main.log.error(self.name + ": " + self.handle.before)
1367 main.cleanup()
1368 main.exit()
1369 except:
1370 main.log.info(self.name+" ::::::")
1371 main.log.error( traceback.print_exc())
1372 main.log.info(self.name+" ::::::")
1373 main.cleanup()
1374 main.exit()
1375
Jon Hall42db6dc2014-10-24 19:03:48 -04001376 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1377 '''
1378 Checks the number of swithes & links that ONOS sees against the
1379 supplied values. By default this will report to main.log, but the
1380 log level can be specifid.
1381
1382 Params: ip = ip used for the onos cli
1383 numoswitch = expected number of switches
1384 numlink = expected number of links
1385 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1386
1387
1388 log_level can
1389
1390 Returns: main.TRUE if the number of switchs and links are correct,
1391 main.FALSE if the numer of switches and links is incorrect,
1392 and main.ERROR otherwise
1393 '''
1394
1395 try:
1396 topology = self.get_topology(ip)
1397 if topology == {}:
1398 return main.ERROR
1399 output = ""
1400 #Is the number of switches is what we expected
1401 devices = topology.get('devices',False)
1402 links = topology.get('links',False)
1403 if devices == False or links == False:
1404 return main.ERROR
1405 switch_check = ( int(devices) == int(numoswitch) )
1406 #Is the number of links is what we expected
1407 link_check = ( int(links) == int(numolink) )
1408 if (switch_check and link_check):
1409 #We expected the correct numbers
1410 output = output + "The number of links and switches match "\
1411 + "what was expected"
1412 result = main.TRUE
1413 else:
1414 output = output + \
1415 "The number of links and switches does not match what was expected"
1416 result = main.FALSE
1417 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1418 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1419 if log_level == "report":
1420 main.log.report(output)
1421 elif log_level == "warn":
1422 main.log.warn(output)
1423 else:
1424 main.log.info(output)
1425 return result
1426 except pexpect.EOF:
1427 main.log.error(self.name + ": EOF exception found")
1428 main.log.error(self.name + ": " + self.handle.before)
1429 main.cleanup()
1430 main.exit()
1431 except:
1432 main.log.info(self.name+" ::::::")
1433 main.log.error( traceback.print_exc())
1434 main.log.info(self.name+" ::::::")
1435 main.cleanup()
1436 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001437
1438 def device_role(self, device_id, onos_node, role="master"):
1439 '''
1440 Calls the device-role cli command.
1441 device_id must be the id of a device as seen in the onos devices command
1442 onos_node is the ip of one of the onos nodes in the cluster
1443 role must be either master, standby, or none
1444
Jon Hall983a1702014-10-28 18:44:22 -04001445 Returns main.TRUE or main.FALSE based argument varification.
1446 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001447 support that output
1448 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001449 try:
Jon Hall983a1702014-10-28 18:44:22 -04001450 #print "beginning device_role... \n\tdevice_id:" + device_id
1451 #print "\tonos_node: " + onos_node
1452 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001453 if role.lower() == "master" or \
1454 role.lower() == "standby" or \
1455 role.lower() == "none":
1456 self.handle.sendline("")
1457 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001458 self.handle.sendline("device-role " +
1459 str(device_id) + " " +
1460 str(onos_node) + " " +
1461 str(role))
1462 i= self.handle.expect(["Error","onos>"])
1463 if i == 0:
1464 output = str(self.handle.before)
1465 self.handle.expect("onos>")
1466 output = output + str(self.handle.before)
1467 main.log.error(self.name + ": " +
1468 output + '\033[0m')#end color output to escape any colours from the cli
1469 return main.ERROR
1470 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001471 self.handle.expect("onos>")
1472 return main.TRUE
1473 else:
1474 return main.FALSE
1475
1476 except pexpect.EOF:
1477 main.log.error(self.name + ": EOF exception found")
1478 main.log.error(self.name + ": " + self.handle.before)
1479 main.cleanup()
1480 main.exit()
1481 except:
1482 main.log.info(self.name+" ::::::")
1483 main.log.error( traceback.print_exc())
1484 main.log.info(self.name+" ::::::")
1485 main.cleanup()
1486 main.exit()
1487
1488
andrewonlab7e4d2d32014-10-15 13:23:21 -04001489 #***********************************