blob: 009b785ff8926c54387f6c4b3e9a1ea9627bb52a [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>")
527 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400528 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400529 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400530 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400531 return handle
Jon Halle8217482014-10-17 13:49:14 -0400532 except pexpect.EOF:
533 main.log.error(self.name + ": EOF exception found")
534 main.log.error(self.name + ": " + self.handle.before)
535 main.cleanup()
536 main.exit()
537 except:
538 main.log.info(self.name+" ::::::")
539 main.log.error( traceback.print_exc())
540 main.log.info(self.name+" ::::::")
541 main.cleanup()
542 main.exit()
543
544
545 def ports(self, json_format=True, grep_str=""):
546 '''
547 Lists all ports
548 Optional argument:
549 * grep_str - pass in a string to grep
550 '''
551 try:
552 self.handle.sendline("")
553 self.handle.expect("onos>")
554
555 if json_format:
556 if not grep_str:
557 self.handle.sendline("ports -j")
558 self.handle.expect("ports -j")
559 self.handle.expect("onos>")
560 else:
561 self.handle.sendline("ports -j | grep '"+
562 str(grep_str)+"'")
563 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
564 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400565 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400566 '''
567 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
568 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 -0400569 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 -0400570 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400571 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
572 handle1 = ansi_escape.sub('', handle)
573 '''
574 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400575 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
576 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400577 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400578 return handle1
579
Jon Halle8217482014-10-17 13:49:14 -0400580 else:
581 if not grep_str:
582 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400583 self.handle.expect("onos>")
584 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400585 self.handle.expect("onos>")
586 else:
587 self.handle.sendline("ports | grep '"+
588 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400589 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400590 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400591 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400592 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400593 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400594 return handle
Jon Halle8217482014-10-17 13:49:14 -0400595 except pexpect.EOF:
596 main.log.error(self.name + ": EOF exception found")
597 main.log.error(self.name + ": " + self.handle.before)
598 main.cleanup()
599 main.exit()
600 except:
601 main.log.info(self.name+" ::::::")
602 main.log.error( traceback.print_exc())
603 main.log.info(self.name+" ::::::")
604 main.cleanup()
605 main.exit()
606
607
Jon Hall983a1702014-10-28 18:44:22 -0400608 def roles(self, json_format=True, grep_str=""):
andrewonlab7c211572014-10-15 16:45:20 -0400609 '''
Jon Hall983a1702014-10-28 18:44:22 -0400610 Lists all devices and the controllers with roles assigned to them
611 Optional argument:
612 * grep_str - pass in a string to grep
andrewonlab7c211572014-10-15 16:45:20 -0400613 '''
614 try:
615 self.handle.sendline("")
616 self.handle.expect("onos>")
andrewonlab7c211572014-10-15 16:45:20 -0400617
Jon Hall983a1702014-10-28 18:44:22 -0400618 if json_format:
619 if not grep_str:
620 self.handle.sendline("roles -j")
621 self.handle.expect("roles -j")
622 self.handle.expect("onos>")
623 else:
624 self.handle.sendline("roles -j | grep '"+
625 str(grep_str)+"'")
626 self.handle.expect("roles -j | grep '"+str(grep_str)+"'")
627 self.handle.expect("onos>")
628 handle = self.handle.before
629 '''
630 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
631 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
632 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
633 So we take off that escape sequence using the following commads:
634 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
635 handle1 = ansi_escape.sub('', handle)
636 '''
637 #print "repr(handle) =", repr(handle)
638 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
639 handle1 = ansi_escape.sub('', handle)
640 #print "repr(handle1) = ", repr(handle1)
641 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400642
andrewonlab7c211572014-10-15 16:45:20 -0400643 else:
Jon Hall983a1702014-10-28 18:44:22 -0400644 if not grep_str:
645 self.handle.sendline("roles")
646 self.handle.expect("onos>")
647 self.handle.sendline("")
648 self.handle.expect("onos>")
649 else:
650 self.handle.sendline("roles | grep '"+
651 str(grep_str)+"'")
652 self.handle.expect("onos>")
653 self.handle.sendline("")
654 self.handle.expect("onos>")
655 handle = self.handle.before
656 #print "handle =",handle
657 return handle
658 except pexpect.EOF:
659 main.log.error(self.name + ": EOF exception found")
660 main.log.error(self.name + ": " + self.handle.before)
661 main.cleanup()
662 main.exit()
663 except:
664 main.log.info(self.name+" ::::::")
665 main.log.error( traceback.print_exc())
666 main.log.info(self.name+" ::::::")
667 main.cleanup()
668 main.exit()
669
670 def get_role(self, device_id):
671 '''
672 Given the a string containing the json representation of the "roles" cli command and a
673 partial or whole device id, returns a json object containing the
674 roles output for the first device whose id contains "device_id"
675
676 Returns:
677 Dict of the role assignments for the given device or
678 None if not match
679 '''
680 try:
681 import json
682 if device_id == None:
683 return None
684 else:
685 raw_roles = self.roles()
686 roles_json = json.loads(raw_roles)
687 #search json for the device with id then return the device
688 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400689 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400690 if str(device_id) in device['id']:
691 return device
692 return None
andrewonlab7c211572014-10-15 16:45:20 -0400693
andrewonlab86dc3082014-10-13 18:18:38 -0400694 except pexpect.EOF:
695 main.log.error(self.name + ": EOF exception found")
696 main.log.error(self.name + ": " + self.handle.before)
697 main.cleanup()
698 main.exit()
699 except:
700 main.log.info(self.name+" ::::::")
701 main.log.error( traceback.print_exc())
702 main.log.info(self.name+" ::::::")
703 main.cleanup()
704 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400705
andrewonlab3e15ead2014-10-15 14:21:34 -0400706 def paths(self, src_id, dst_id):
707 '''
708 Returns string of paths, and the cost.
709 Issues command: onos:paths <src> <dst>
710 '''
711 try:
712 self.handle.sendline("")
713 self.handle.expect("onos>")
714
715 self.handle.sendline("onos:paths "+
716 str(src_id) + " " + str(dst_id))
717 i = self.handle.expect([
718 "Error",
719 "onos>"])
720
721 self.handle.sendline("")
722 self.handle.expect("onos>")
723
724 handle = self.handle.before
725
726 if i == 0:
727 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400728 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400729 else:
730 path = handle.split(";")[0]
731 cost = handle.split(";")[1]
732 return (path, cost)
733
734 except pexpect.EOF:
735 main.log.error(self.name + ": EOF exception found")
736 main.log.error(self.name + ": " + self.handle.before)
737 main.cleanup()
738 main.exit()
739 except:
740 main.log.info(self.name+" ::::::")
741 main.log.error( traceback.print_exc())
742 main.log.info(self.name+" ::::::")
743 main.cleanup()
744 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400745
Jon Hall42db6dc2014-10-24 19:03:48 -0400746 def hosts(self, json_format=True, grep_str=""):
747 '''
748 Lists all discovered hosts
749 Optional argument:
750 * grep_str - pass in a string to grep
751 '''
752 try:
753 self.handle.sendline("")
754 self.handle.expect("onos>")
755
756 if json_format:
757 if not grep_str:
758 self.handle.sendline("hosts -j")
759 self.handle.expect("hosts -j")
760 self.handle.expect("onos>")
761 else:
762 self.handle.sendline("hosts -j | grep '"+
763 str(grep_str)+"'")
764 self.handle.expect("hosts -j | grep '"+str(grep_str)+"'")
765 self.handle.expect("onos>")
766 handle = self.handle.before
767 '''
768 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
769 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
770 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
771 So we take off that escape sequence using
772 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
773 handle1 = ansi_escape.sub('', handle)
774 '''
775 #print "repr(handle) =", repr(handle)
776 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
777 handle1 = ansi_escape.sub('', handle)
778 #print "repr(handle1) = ", repr(handle1)
779 return handle1
780 else:
781 if not grep_str:
782 self.handle.sendline("hosts")
783 self.handle.expect("onos>")
784 else:
785 self.handle.sendline("hosts | grep '"+
786 str(grep_str)+"'")
787 self.handle.expect("onos>")
788 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400789 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400790 return handle
791 except pexpect.EOF:
792 main.log.error(self.name + ": EOF exception found")
793 main.log.error(self.name + ": " + self.handle.before)
794 main.cleanup()
795 main.exit()
796 except:
797 main.log.info(self.name+" ::::::")
798 main.log.error( traceback.print_exc())
799 main.log.info(self.name+" ::::::")
800 main.cleanup()
801 main.exit()
802
803 def get_host(self, mac):
804 '''
805 Return the first host from the hosts api whose 'id' contains 'mac'
806 Note: mac must be a colon seperated mac address, but could be a partial mac address
807 Return None if there is no match
808 '''
809 import json
810 try:
811 if mac == None:
812 return None
813 else:
814 mac = mac
815 raw_hosts = self.hosts()
816 hosts_json = json.loads(raw_hosts)
817 #search json for the host with mac then return the device
818 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400819 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400820 if mac in host['id']:
821 return host
822 return None
823 except pexpect.EOF:
824 main.log.error(self.name + ": EOF exception found")
825 main.log.error(self.name + ": " + self.handle.before)
826 main.cleanup()
827 main.exit()
828 except:
829 main.log.info(self.name+" ::::::")
830 main.log.error( traceback.print_exc())
831 main.log.info(self.name+" ::::::")
832 main.cleanup()
833 main.exit()
834
andrewonlab3f0a4af2014-10-17 12:25:14 -0400835
836 def get_hosts_id(self, host_list):
837 '''
838 Obtain list of hosts
839 Issues command: 'onos> hosts'
840
841 Required:
842 * host_list: List of hosts obtained by Mininet
843 IMPORTANT:
844 This function assumes that you started your
845 topology with the option '--mac'.
846 Furthermore, it assumes that value of VLAN is '-1'
847 Description:
848 Converts mininet hosts (h1, h2, h3...) into
849 ONOS format (00:00:00:00:00:01/-1 , ...)
850 '''
851
852 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400853 onos_host_list = []
854
855 for host in host_list:
856 host = host.replace("h", "")
857 host_hex = hex(int(host)).zfill(12)
858 host_hex = str(host_hex).replace('x','0')
859 i = iter(str(host_hex))
860 host_hex = ":".join(a+b for a,b in zip(i,i))
861 host_hex = host_hex + "/-1"
862 onos_host_list.append(host_hex)
863
864 return onos_host_list
865
866 except pexpect.EOF:
867 main.log.error(self.name + ": EOF exception found")
868 main.log.error(self.name + ": " + self.handle.before)
869 main.cleanup()
870 main.exit()
871 except:
872 main.log.info(self.name+" ::::::")
873 main.log.error( traceback.print_exc())
874 main.log.info(self.name+" ::::::")
875 main.cleanup()
876 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400877
andrewonlabe6745342014-10-17 14:29:13 -0400878 def add_host_intent(self, host_id_one, host_id_two):
879 '''
880 Required:
881 * host_id_one: ONOS host id for host1
882 * host_id_two: ONOS host id for host2
883 Description:
884 Adds a host-to-host intent (bidrectional) by
885 specifying the two hosts.
886 '''
887 try:
888 self.handle.sendline("")
889 self.handle.expect("onos>")
890
891 self.handle.sendline("add-host-intent "+
892 str(host_id_one) + " " + str(host_id_two))
893 self.handle.expect("onos>")
894
andrewonlabe6745342014-10-17 14:29:13 -0400895 handle = self.handle.before
Shreya Shah4f25fdf2014-10-29 19:55:35 -0400896 print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400897
898 main.log.info("Intent installed between "+
899 str(host_id_one) + " and " + str(host_id_two))
900
901 return handle
902
903 except pexpect.EOF:
904 main.log.error(self.name + ": EOF exception found")
905 main.log.error(self.name + ": " + self.handle.before)
906 main.cleanup()
907 main.exit()
908 except:
909 main.log.info(self.name+" ::::::")
910 main.log.error( traceback.print_exc())
911 main.log.info(self.name+" ::::::")
912 main.cleanup()
913 main.exit()
914
andrewonlab7b31d232014-10-24 13:31:47 -0400915 def add_optical_intent(self, ingress_device, egress_device):
916 '''
917 Required:
918 * ingress_device: device id of ingress device
919 * egress_device: device id of egress device
920 Optional:
921 TODO: Still needs to be implemented via dev side
922 '''
923 try:
924 self.handle.sendline("add-optical-intent "+
925 str(ingress_device) + " " + str(egress_device))
926 self.handle.expect("add-optical-intent")
927 i = self.handle.expect([
928 "Error",
929 "onos>"])
930
931 handle = self.handle.before
932
933 #If error, return error message
934 if i == 0:
935 return handle
936 else:
937 return main.TRUE
938
939 except pexpect.EOF:
940 main.log.error(self.name + ": EOF exception found")
941 main.log.error(self.name + ": " + self.handle.before)
942 main.cleanup()
943 main.exit()
944 except:
945 main.log.info(self.name+" ::::::")
946 main.log.error( traceback.print_exc())
947 main.log.info(self.name+" ::::::")
948 main.cleanup()
949 main.exit()
950
andrewonlab4dbb4d82014-10-17 18:22:31 -0400951 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400952 egress_device, port_egress, ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -0500953 ethDst="", bandwidth="", lambda_alloc=False,
954 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400955 '''
956 Required:
957 * ingress_device: device id of ingress device
958 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400959 Optional:
960 * ethType: specify ethType
961 * ethSrc: specify ethSrc (i.e. src mac addr)
962 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500963 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -0500964 * lambda_alloc: if True, intent will allocate lambda
965 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -0500966 * ipProto: specify ip protocol
967 * ipSrc: specify ip source address
968 * ipDst: specify ip destination address
969 * tcpSrc: specify tcp source port
970 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400971 Description:
972 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400973 specifying device id's and optional fields
974
andrewonlab4dbb4d82014-10-17 18:22:31 -0400975 NOTE: This function may change depending on the
976 options developers provide for point-to-point
977 intent via cli
978 '''
979 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400980 cmd = ""
981
982 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500983 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -0500984 and not bandwidth and not lambda_alloc \
985 and not ipProto and not ipSrc and not ipDst \
986 and not tcpSrc and not tcpDst:
andrewonlab289e4b72014-10-21 21:24:18 -0400987 cmd = "add-point-intent "+\
988 str(ingress_device) + "/" + str(port_ingress) + " " +\
989 str(egress_device) + "/" + str(port_egress)
990
991 else:
andrewonlab9a130be2014-10-22 12:44:56 -0400992 cmd = "add-point-intent "
993
andrewonlab0c0a6772014-10-22 12:31:18 -0400994 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -0400995 cmd += " --ethType " + str(ethType)
996 if ethSrc:
997 cmd += " --ethSrc " + str(ethSrc)
998 if ethDst:
999 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001000 if bandwidth:
1001 cmd += " --bandwidth " + str(bandwidth)
1002 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001003 cmd += " --lambda "
1004 if ipProto:
1005 cmd += " --ipProto " + str(ipProto)
1006 if ipSrc:
1007 cmd += " --ipSrc " + str(ipSrc)
1008 if ipDst:
1009 cmd += " --ipDst " + str(ipDst)
1010 if tcpSrc:
1011 cmd += " --tcpSrc " + str(tcpSrc)
1012 if tcpDst:
1013 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001014
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001015 cmd += " "+str(ingress_device) +\
1016 "/" + str(port_ingress) + " " +\
1017 str(egress_device) + "/" + str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001018
andrewonlab289e4b72014-10-21 21:24:18 -04001019 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001020 i = self.handle.expect([
1021 "Error",
1022 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -04001023
Shreya Shah0f01c812014-10-26 20:15:28 -04001024 self.handle.sendline("intents")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001025 self.handle.expect("onos>")
Shreya Shah0f01c812014-10-26 20:15:28 -04001026 Intenthandle = self.handle.before
andrewonlab4dbb4d82014-10-17 18:22:31 -04001027
1028 if i == 0:
1029 main.log.error("Error in adding point-to-point intent")
1030 return handle
1031 else:
1032 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001033
andrewonlab4dbb4d82014-10-17 18:22:31 -04001034 except pexpect.EOF:
1035 main.log.error(self.name + ": EOF exception found")
1036 main.log.error(self.name + ": " + self.handle.before)
1037 main.cleanup()
1038 main.exit()
1039 except:
1040 main.log.info(self.name+" ::::::")
1041 main.log.error( traceback.print_exc())
1042 main.log.info(self.name+" ::::::")
1043 main.cleanup()
1044 main.exit()
1045
andrewonlab9a50dfe2014-10-17 17:22:31 -04001046 def remove_intent(self, intent_id):
1047 '''
1048 Remove intent for specified intent id
1049 '''
1050 try:
1051 self.handle.sendline("")
1052 self.handle.expect("onos>")
1053
1054 self.handle.sendline("remove-intent "+str(intent_id))
1055 i = self.handle.expect([
1056 "Error",
1057 "onos>"])
1058
1059 handle = self.handle.before
1060
1061 if i == 0:
1062 main.log.error("Error in removing intent")
1063 return handle
1064 else:
1065 return handle
1066
1067 except pexpect.EOF:
1068 main.log.error(self.name + ": EOF exception found")
1069 main.log.error(self.name + ": " + self.handle.before)
1070 main.cleanup()
1071 main.exit()
1072 except:
1073 main.log.info(self.name+" ::::::")
1074 main.log.error( traceback.print_exc())
1075 main.log.info(self.name+" ::::::")
1076 main.cleanup()
1077 main.exit()
1078
andrewonlab377693f2014-10-21 16:00:30 -04001079 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001080 '''
andrewonlab377693f2014-10-21 16:00:30 -04001081 Optional:
1082 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001083 Description:
1084 Obtain intents currently installed
1085 '''
1086 try:
andrewonlab377693f2014-10-21 16:00:30 -04001087 if json_format:
1088 self.handle.sendline("intents -j")
1089 self.handle.expect("intents -j")
1090 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001091 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001092
1093 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1094 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001095 else:
1096 self.handle.sendline("")
1097 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001098
andrewonlab377693f2014-10-21 16:00:30 -04001099 self.handle.sendline("intents")
1100 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001101 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001102
1103 return handle
1104
1105 except pexpect.EOF:
1106 main.log.error(self.name + ": EOF exception found")
1107 main.log.error(self.name + ": " + self.handle.before)
1108 main.cleanup()
1109 main.exit()
1110 except:
1111 main.log.info(self.name+" ::::::")
1112 main.log.error( traceback.print_exc())
1113 main.log.info(self.name+" ::::::")
1114 main.cleanup()
1115 main.exit()
1116
Shreya Shah0f01c812014-10-26 20:15:28 -04001117 def flows(self, json_format = False):
1118 '''
1119 Optional:
1120 * json_format: enable output formatting in json
1121 Description:
1122 Obtain flows currently installed
1123 '''
1124 try:
1125 if json_format:
1126 self.handle.sendline("flows -j")
1127 self.handle.expect("flows -j")
1128 self.handle.expect("onos>")
1129 handle = self.handle.before
1130
1131 else:
1132 self.handle.sendline("")
1133 self.handle.expect("onos>")
1134 self.handle.sendline("flows")
1135 self.handle.expect("onos>")
1136 handle = self.handle.before
1137
1138 return handle
1139
1140 except pexpect.EOF:
1141 main.log.error(self.name + ": EOF exception found")
1142 main.log.error(self.name + ": " + self.handle.before)
1143 main.cleanup()
1144 main.exit()
1145 except:
1146 main.log.info(self.name+" ::::::")
1147 main.log.error( traceback.print_exc())
1148 main.log.info(self.name+" ::::::")
1149 main.cleanup()
1150 main.exit()
1151
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001152 def intents_events_metrics(self, json_format=True):
1153 '''
1154 Description:Returns topology metrics
1155 Optional:
1156 * json_format: enable json formatting of output
1157 '''
1158 try:
1159 if json_format:
1160 self.handle.sendline("intents-events-metrics -j")
1161 self.handle.expect("intents-events-metrics -j")
1162 self.handle.expect("onos>")
1163
1164 handle = self.handle.before
1165
1166 #Some color thing that we want to escape
1167 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1168 handle = ansi_escape.sub('', handle)
1169
1170 else:
1171 self.handle.sendline("intents-events-metrics")
1172 self.handle.expect("intents-events-metrics")
1173 self.handle.expect("onos>")
1174
1175 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001176
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001177 return handle
1178
1179 except pexpect.EOF:
1180 main.log.error(self.name + ": EOF exception found")
1181 main.log.error(self.name + ": " + self.handle.before)
1182 main.cleanup()
1183 main.exit()
1184 except:
1185 main.log.info(self.name+" ::::::")
1186 main.log.error( traceback.print_exc())
1187 main.log.info(self.name+" ::::::")
1188 main.cleanup()
1189 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001190
andrewonlab867212a2014-10-22 20:13:38 -04001191 def topology_events_metrics(self, json_format=True):
1192 '''
1193 Description:Returns topology metrics
1194 Optional:
1195 * json_format: enable json formatting of output
1196 '''
1197 try:
1198 if json_format:
1199 self.handle.sendline("topology-events-metrics -j")
1200 self.handle.expect("topology-events-metrics -j")
1201 self.handle.expect("onos>")
1202
1203 handle = self.handle.before
1204
1205 #Some color thing that we want to escape
1206 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1207 handle = ansi_escape.sub('', handle)
1208
1209 else:
1210 self.handle.sendline("topology-events-metrics")
1211 self.handle.expect("topology-events-metrics")
1212 self.handle.expect("onos>")
1213
1214 handle = self.handle.before
1215
1216 return handle
1217
1218 except pexpect.EOF:
1219 main.log.error(self.name + ": EOF exception found")
1220 main.log.error(self.name + ": " + self.handle.before)
1221 main.cleanup()
1222 main.exit()
1223 except:
1224 main.log.info(self.name+" ::::::")
1225 main.log.error( traceback.print_exc())
1226 main.log.info(self.name+" ::::::")
1227 main.cleanup()
1228 main.exit()
1229
andrewonlab3e15ead2014-10-15 14:21:34 -04001230 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001231 #Wrapper functions use existing driver
1232 #functions and extends their use case.
1233 #For example, we may use the output of
1234 #a normal driver function, and parse it
1235 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001236
andrewonlab9a50dfe2014-10-17 17:22:31 -04001237 def get_all_intents_id(self):
1238 '''
1239 Description:
1240 Obtain all intent id's in a list
1241 '''
1242 try:
1243 #Obtain output of intents function
1244 intents_str = self.intents()
1245 all_intent_list = []
1246 intent_id_list = []
1247
1248 #Parse the intents output for ID's
1249 intents_list = [s.strip() for s in intents_str.splitlines()]
1250 for intents in intents_list:
1251 if "onos>" in intents:
1252 continue
1253 elif "intents" in intents:
1254 continue
1255 else:
1256 line_list = intents.split(" ")
1257 all_intent_list.append(line_list[0])
1258
1259 all_intent_list = all_intent_list[1:-2]
1260
1261 for intents in all_intent_list:
1262 if not intents:
1263 continue
1264 else:
1265 intent_id_list.append(intents)
1266
1267 return intent_id_list
1268
1269 except pexpect.EOF:
1270 main.log.error(self.name + ": EOF exception found")
1271 main.log.error(self.name + ": " + self.handle.before)
1272 main.cleanup()
1273 main.exit()
1274 except:
1275 main.log.info(self.name+" ::::::")
1276 main.log.error( traceback.print_exc())
1277 main.log.info(self.name+" ::::::")
1278 main.cleanup()
1279 main.exit()
1280
andrewonlab7e4d2d32014-10-15 13:23:21 -04001281 def get_all_devices_id(self):
1282 '''
1283 Use 'devices' function to obtain list of all devices
1284 and parse the result to obtain a list of all device
1285 id's. Returns this list. Returns empty list if no
1286 devices exist
1287 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001288
1289 This function may be useful if you are not sure of the
1290 device id, and wish to execute other commands using
1291 the ids. By obtaining the list of device ids on the fly,
1292 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001293 '''
1294 try:
1295 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001296 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001297 id_list = []
1298
1299 if not devices_str:
1300 main.log.info("There are no devices to get id from")
1301 return id_list
1302
1303 #Split the string into list by comma
1304 device_list = devices_str.split(",")
1305 #Get temporary list of all arguments with string 'id='
1306 temp_list = [dev for dev in device_list if "id=" in dev]
1307 #Split list further into arguments before and after string
1308 # 'id='. Get the latter portion (the actual device id) and
1309 # append to id_list
1310 for arg in temp_list:
1311 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001312 return id_list
1313
1314 except pexpect.EOF:
1315 main.log.error(self.name + ": EOF exception found")
1316 main.log.error(self.name + ": " + self.handle.before)
1317 main.cleanup()
1318 main.exit()
1319 except:
1320 main.log.info(self.name+" ::::::")
1321 main.log.error( traceback.print_exc())
1322 main.log.info(self.name+" ::::::")
1323 main.cleanup()
1324 main.exit()
1325
andrewonlab7c211572014-10-15 16:45:20 -04001326 def get_all_nodes_id(self):
1327 '''
1328 Uses 'nodes' function to obtain list of all nodes
1329 and parse the result of nodes to obtain just the
1330 node id's.
1331 Returns:
1332 list of node id's
1333 '''
1334 try:
1335 nodes_str = self.nodes()
1336 id_list = []
1337
1338 if not nodes_str:
1339 main.log.info("There are no nodes to get id from")
1340 return id_list
1341
1342 #Sample nodes_str output
1343 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1344
1345 #Split the string into list by comma
1346 nodes_list = nodes_str.split(",")
1347 temp_list = [node for node in nodes_list if "id=" in node]
1348 for arg in temp_list:
1349 id_list.append(arg.split("id=")[1])
1350
1351 return id_list
1352
1353 except pexpect.EOF:
1354 main.log.error(self.name + ": EOF exception found")
1355 main.log.error(self.name + ": " + self.handle.before)
1356 main.cleanup()
1357 main.exit()
1358 except:
1359 main.log.info(self.name+" ::::::")
1360 main.log.error( traceback.print_exc())
1361 main.log.info(self.name+" ::::::")
1362 main.cleanup()
1363 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001364
Jon Halla91c4dc2014-10-22 12:57:04 -04001365 def get_device(self, dpid=None):
1366 '''
1367 Return the first device from the devices api whose 'id' contains 'dpid'
1368 Return None if there is no match
1369 '''
1370 import json
1371 try:
1372 if dpid == None:
1373 return None
1374 else:
1375 dpid = dpid.replace(':', '')
1376 raw_devices = self.devices()
1377 devices_json = json.loads(raw_devices)
1378 #search json for the device with dpid then return the device
1379 for device in devices_json:
1380 #print "%s in %s?" % (dpid, device['id'])
1381 if dpid in device['id']:
1382 return device
1383 return None
1384 except pexpect.EOF:
1385 main.log.error(self.name + ": EOF exception found")
1386 main.log.error(self.name + ": " + self.handle.before)
1387 main.cleanup()
1388 main.exit()
1389 except:
1390 main.log.info(self.name+" ::::::")
1391 main.log.error( traceback.print_exc())
1392 main.log.info(self.name+" ::::::")
1393 main.cleanup()
1394 main.exit()
1395
Jon Hall42db6dc2014-10-24 19:03:48 -04001396 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1397 '''
1398 Checks the number of swithes & links that ONOS sees against the
1399 supplied values. By default this will report to main.log, but the
1400 log level can be specifid.
1401
1402 Params: ip = ip used for the onos cli
1403 numoswitch = expected number of switches
1404 numlink = expected number of links
1405 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1406
1407
1408 log_level can
1409
1410 Returns: main.TRUE if the number of switchs and links are correct,
1411 main.FALSE if the numer of switches and links is incorrect,
1412 and main.ERROR otherwise
1413 '''
1414
1415 try:
1416 topology = self.get_topology(ip)
1417 if topology == {}:
1418 return main.ERROR
1419 output = ""
1420 #Is the number of switches is what we expected
1421 devices = topology.get('devices',False)
1422 links = topology.get('links',False)
1423 if devices == False or links == False:
1424 return main.ERROR
1425 switch_check = ( int(devices) == int(numoswitch) )
1426 #Is the number of links is what we expected
1427 link_check = ( int(links) == int(numolink) )
1428 if (switch_check and link_check):
1429 #We expected the correct numbers
1430 output = output + "The number of links and switches match "\
1431 + "what was expected"
1432 result = main.TRUE
1433 else:
1434 output = output + \
1435 "The number of links and switches does not match what was expected"
1436 result = main.FALSE
1437 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1438 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1439 if log_level == "report":
1440 main.log.report(output)
1441 elif log_level == "warn":
1442 main.log.warn(output)
1443 else:
1444 main.log.info(output)
1445 return result
1446 except pexpect.EOF:
1447 main.log.error(self.name + ": EOF exception found")
1448 main.log.error(self.name + ": " + self.handle.before)
1449 main.cleanup()
1450 main.exit()
1451 except:
1452 main.log.info(self.name+" ::::::")
1453 main.log.error( traceback.print_exc())
1454 main.log.info(self.name+" ::::::")
1455 main.cleanup()
1456 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001457
1458 def device_role(self, device_id, onos_node, role="master"):
1459 '''
1460 Calls the device-role cli command.
1461 device_id must be the id of a device as seen in the onos devices command
1462 onos_node is the ip of one of the onos nodes in the cluster
1463 role must be either master, standby, or none
1464
Jon Hall983a1702014-10-28 18:44:22 -04001465 Returns main.TRUE or main.FALSE based argument varification.
1466 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001467 support that output
1468 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001469 try:
Jon Hall983a1702014-10-28 18:44:22 -04001470 #print "beginning device_role... \n\tdevice_id:" + device_id
1471 #print "\tonos_node: " + onos_node
1472 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001473 if role.lower() == "master" or \
1474 role.lower() == "standby" or \
1475 role.lower() == "none":
1476 self.handle.sendline("")
1477 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001478 self.handle.sendline("device-role " +
1479 str(device_id) + " " +
1480 str(onos_node) + " " +
1481 str(role))
1482 i= self.handle.expect(["Error","onos>"])
1483 if i == 0:
1484 output = str(self.handle.before)
1485 self.handle.expect("onos>")
1486 output = output + str(self.handle.before)
1487 main.log.error(self.name + ": " +
1488 output + '\033[0m')#end color output to escape any colours from the cli
1489 return main.ERROR
1490 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001491 self.handle.expect("onos>")
1492 return main.TRUE
1493 else:
1494 return main.FALSE
1495
1496 except pexpect.EOF:
1497 main.log.error(self.name + ": EOF exception found")
1498 main.log.error(self.name + ": " + self.handle.before)
1499 main.cleanup()
1500 main.exit()
1501 except:
1502 main.log.info(self.name+" ::::::")
1503 main.log.error( traceback.print_exc())
1504 main.log.info(self.name+" ::::::")
1505 main.cleanup()
1506 main.exit()
1507
1508
andrewonlab7e4d2d32014-10-15 13:23:21 -04001509 #***********************************