blob: 1002698086d81d45c89a6c282eb5b663c590e4f0 [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 Halld815ce42014-10-17 19:52:30 -0400451 So we take off that escape sequence using
452 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
453 handle1 = ansi_escape.sub('', handle)
454 '''
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
469 print "handle =",handle
470 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
530 print "handle =",handle
531 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
593 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
andrewonlab7c211572014-10-15 16:45:20 -0400608 def device_role(self, device_id, node_id, role):
609 '''
610 Set device role for specified device and node with role
611 Required:
612 * device_id : may be obtained by function get_all_devices_id
613 * node_id : may be obtained by function get_all_nodes_id
614 * role: specify one of the following roles:
615 - master
616 - standby
617 - none
618 '''
619 try:
620 self.handle.sendline("")
621 self.handle.expect("onos>")
622
623 self.handle.sendline("device-role "+
624 str(device_id) + " " +
625 str(node_id) + " " +
626 str(role))
627 i = self.handle.expect([
628 "Error",
629 "onos>"])
630
631 self.handle.sendline("")
632 self.handle.expect("onos>")
633
634 handle = self.handle.before
635
636 if i == 0:
637 main.log.error("device-role command returned error")
638 return handle
639 else:
640 return main.TRUE
641
andrewonlab86dc3082014-10-13 18:18:38 -0400642 except pexpect.EOF:
643 main.log.error(self.name + ": EOF exception found")
644 main.log.error(self.name + ": " + self.handle.before)
645 main.cleanup()
646 main.exit()
647 except:
648 main.log.info(self.name+" ::::::")
649 main.log.error( traceback.print_exc())
650 main.log.info(self.name+" ::::::")
651 main.cleanup()
652 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400653
andrewonlab3e15ead2014-10-15 14:21:34 -0400654 def paths(self, src_id, dst_id):
655 '''
656 Returns string of paths, and the cost.
657 Issues command: onos:paths <src> <dst>
658 '''
659 try:
660 self.handle.sendline("")
661 self.handle.expect("onos>")
662
663 self.handle.sendline("onos:paths "+
664 str(src_id) + " " + str(dst_id))
665 i = self.handle.expect([
666 "Error",
667 "onos>"])
668
669 self.handle.sendline("")
670 self.handle.expect("onos>")
671
672 handle = self.handle.before
673
674 if i == 0:
675 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400676 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400677 else:
678 path = handle.split(";")[0]
679 cost = handle.split(";")[1]
680 return (path, cost)
681
682 except pexpect.EOF:
683 main.log.error(self.name + ": EOF exception found")
684 main.log.error(self.name + ": " + self.handle.before)
685 main.cleanup()
686 main.exit()
687 except:
688 main.log.info(self.name+" ::::::")
689 main.log.error( traceback.print_exc())
690 main.log.info(self.name+" ::::::")
691 main.cleanup()
692 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400693
Jon Hall42db6dc2014-10-24 19:03:48 -0400694 def hosts(self, json_format=True, grep_str=""):
695 '''
696 Lists all discovered hosts
697 Optional argument:
698 * grep_str - pass in a string to grep
699 '''
700 try:
701 self.handle.sendline("")
702 self.handle.expect("onos>")
703
704 if json_format:
705 if not grep_str:
706 self.handle.sendline("hosts -j")
707 self.handle.expect("hosts -j")
708 self.handle.expect("onos>")
709 else:
710 self.handle.sendline("hosts -j | grep '"+
711 str(grep_str)+"'")
712 self.handle.expect("hosts -j | grep '"+str(grep_str)+"'")
713 self.handle.expect("onos>")
714 handle = self.handle.before
715 '''
716 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
717 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
718 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
719 So we take off that escape sequence using
720 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
721 handle1 = ansi_escape.sub('', handle)
722 '''
723 #print "repr(handle) =", repr(handle)
724 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
725 handle1 = ansi_escape.sub('', handle)
726 #print "repr(handle1) = ", repr(handle1)
727 return handle1
728 else:
729 if not grep_str:
730 self.handle.sendline("hosts")
731 self.handle.expect("onos>")
732 else:
733 self.handle.sendline("hosts | grep '"+
734 str(grep_str)+"'")
735 self.handle.expect("onos>")
736 handle = self.handle.before
737 print "handle =",handle
738 return handle
739 except pexpect.EOF:
740 main.log.error(self.name + ": EOF exception found")
741 main.log.error(self.name + ": " + self.handle.before)
742 main.cleanup()
743 main.exit()
744 except:
745 main.log.info(self.name+" ::::::")
746 main.log.error( traceback.print_exc())
747 main.log.info(self.name+" ::::::")
748 main.cleanup()
749 main.exit()
750
751 def get_host(self, mac):
752 '''
753 Return the first host from the hosts api whose 'id' contains 'mac'
754 Note: mac must be a colon seperated mac address, but could be a partial mac address
755 Return None if there is no match
756 '''
757 import json
758 try:
759 if mac == None:
760 return None
761 else:
762 mac = mac
763 raw_hosts = self.hosts()
764 hosts_json = json.loads(raw_hosts)
765 #search json for the host with mac then return the device
766 for host in hosts_json:
767 print "%s in %s?" % (mac, host['id'])
768 if mac in host['id']:
769 return host
770 return None
771 except pexpect.EOF:
772 main.log.error(self.name + ": EOF exception found")
773 main.log.error(self.name + ": " + self.handle.before)
774 main.cleanup()
775 main.exit()
776 except:
777 main.log.info(self.name+" ::::::")
778 main.log.error( traceback.print_exc())
779 main.log.info(self.name+" ::::::")
780 main.cleanup()
781 main.exit()
782
andrewonlab3f0a4af2014-10-17 12:25:14 -0400783
784 def get_hosts_id(self, host_list):
785 '''
786 Obtain list of hosts
787 Issues command: 'onos> hosts'
788
789 Required:
790 * host_list: List of hosts obtained by Mininet
791 IMPORTANT:
792 This function assumes that you started your
793 topology with the option '--mac'.
794 Furthermore, it assumes that value of VLAN is '-1'
795 Description:
796 Converts mininet hosts (h1, h2, h3...) into
797 ONOS format (00:00:00:00:00:01/-1 , ...)
798 '''
799
800 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400801 onos_host_list = []
802
803 for host in host_list:
804 host = host.replace("h", "")
805 host_hex = hex(int(host)).zfill(12)
806 host_hex = str(host_hex).replace('x','0')
807 i = iter(str(host_hex))
808 host_hex = ":".join(a+b for a,b in zip(i,i))
809 host_hex = host_hex + "/-1"
810 onos_host_list.append(host_hex)
811
812 return onos_host_list
813
814 except pexpect.EOF:
815 main.log.error(self.name + ": EOF exception found")
816 main.log.error(self.name + ": " + self.handle.before)
817 main.cleanup()
818 main.exit()
819 except:
820 main.log.info(self.name+" ::::::")
821 main.log.error( traceback.print_exc())
822 main.log.info(self.name+" ::::::")
823 main.cleanup()
824 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400825
andrewonlabe6745342014-10-17 14:29:13 -0400826 def add_host_intent(self, host_id_one, host_id_two):
827 '''
828 Required:
829 * host_id_one: ONOS host id for host1
830 * host_id_two: ONOS host id for host2
831 Description:
832 Adds a host-to-host intent (bidrectional) by
833 specifying the two hosts.
834 '''
835 try:
836 self.handle.sendline("")
837 self.handle.expect("onos>")
838
839 self.handle.sendline("add-host-intent "+
840 str(host_id_one) + " " + str(host_id_two))
841 self.handle.expect("onos>")
842
andrewonlabe6745342014-10-17 14:29:13 -0400843 handle = self.handle.before
Shreya Shah4f25fdf2014-10-29 19:55:35 -0400844 print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400845
846 main.log.info("Intent installed between "+
847 str(host_id_one) + " and " + str(host_id_two))
848
849 return handle
850
851 except pexpect.EOF:
852 main.log.error(self.name + ": EOF exception found")
853 main.log.error(self.name + ": " + self.handle.before)
854 main.cleanup()
855 main.exit()
856 except:
857 main.log.info(self.name+" ::::::")
858 main.log.error( traceback.print_exc())
859 main.log.info(self.name+" ::::::")
860 main.cleanup()
861 main.exit()
862
andrewonlab7b31d232014-10-24 13:31:47 -0400863 def add_optical_intent(self, ingress_device, egress_device):
864 '''
865 Required:
866 * ingress_device: device id of ingress device
867 * egress_device: device id of egress device
868 Optional:
869 TODO: Still needs to be implemented via dev side
870 '''
871 try:
872 self.handle.sendline("add-optical-intent "+
873 str(ingress_device) + " " + str(egress_device))
874 self.handle.expect("add-optical-intent")
875 i = self.handle.expect([
876 "Error",
877 "onos>"])
878
879 handle = self.handle.before
880
881 #If error, return error message
882 if i == 0:
883 return handle
884 else:
885 return main.TRUE
886
887 except pexpect.EOF:
888 main.log.error(self.name + ": EOF exception found")
889 main.log.error(self.name + ": " + self.handle.before)
890 main.cleanup()
891 main.exit()
892 except:
893 main.log.info(self.name+" ::::::")
894 main.log.error( traceback.print_exc())
895 main.log.info(self.name+" ::::::")
896 main.cleanup()
897 main.exit()
898
andrewonlab4dbb4d82014-10-17 18:22:31 -0400899 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400900 egress_device, port_egress, ethType="", ethSrc="",
901 ethDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400902 '''
903 Required:
904 * ingress_device: device id of ingress device
905 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400906 Optional:
907 * ethType: specify ethType
908 * ethSrc: specify ethSrc (i.e. src mac addr)
909 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400910 Description:
911 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400912 specifying device id's and optional fields
913
andrewonlab4dbb4d82014-10-17 18:22:31 -0400914 NOTE: This function may change depending on the
915 options developers provide for point-to-point
916 intent via cli
917 '''
918 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400919 cmd = ""
920
921 #If there are no optional arguments
922 if not ethType and not ethSrc and not ethDst:
923 cmd = "add-point-intent "+\
924 str(ingress_device) + "/" + str(port_ingress) + " " +\
925 str(egress_device) + "/" + str(port_egress)
926
927 else:
andrewonlab9a130be2014-10-22 12:44:56 -0400928 cmd = "add-point-intent "
929
andrewonlab0c0a6772014-10-22 12:31:18 -0400930 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -0400931 cmd += " --ethType " + str(ethType)
932 if ethSrc:
933 cmd += " --ethSrc " + str(ethSrc)
934 if ethDst:
935 cmd += " --ethDst " + str(ethDst)
andrewonlab9a130be2014-10-22 12:44:56 -0400936
937 cmd += " "+str(ingress_device) + "/" + str(port_ingress) + " " +\
938 str(egress_device) + "/" + str(port_egress)
andrewonlab289e4b72014-10-21 21:24:18 -0400939
Shreya Shah0f01c812014-10-26 20:15:28 -0400940 print "cmd = ", cmd
941 #self.handle.sendline("")
942 #self.handle.expect("onos>")
andrewonlab4dbb4d82014-10-17 18:22:31 -0400943
andrewonlab289e4b72014-10-21 21:24:18 -0400944 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400945 i = self.handle.expect([
946 "Error",
947 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -0400948
Shreya Shah0f01c812014-10-26 20:15:28 -0400949 self.handle.sendline("intents")
andrewonlab4dbb4d82014-10-17 18:22:31 -0400950 self.handle.expect("onos>")
Shreya Shah0f01c812014-10-26 20:15:28 -0400951 Intenthandle = self.handle.before
952 #print "Intenthandle = ", Intenthandle
andrewonlab4dbb4d82014-10-17 18:22:31 -0400953
Shreya Shah0f01c812014-10-26 20:15:28 -0400954 #self.handle.sendline("flows")
955 #self.handle.expect("onos>")
956 #Flowhandle = self.handle.before
957 #print "Flowhandle = ", Flowhandle
andrewonlab4dbb4d82014-10-17 18:22:31 -0400958
959 if i == 0:
960 main.log.error("Error in adding point-to-point intent")
961 return handle
962 else:
963 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -0400964
andrewonlab4dbb4d82014-10-17 18:22:31 -0400965 except pexpect.EOF:
966 main.log.error(self.name + ": EOF exception found")
967 main.log.error(self.name + ": " + self.handle.before)
968 main.cleanup()
969 main.exit()
970 except:
971 main.log.info(self.name+" ::::::")
972 main.log.error( traceback.print_exc())
973 main.log.info(self.name+" ::::::")
974 main.cleanup()
975 main.exit()
976
andrewonlab9a50dfe2014-10-17 17:22:31 -0400977 def remove_intent(self, intent_id):
978 '''
979 Remove intent for specified intent id
980 '''
981 try:
982 self.handle.sendline("")
983 self.handle.expect("onos>")
984
985 self.handle.sendline("remove-intent "+str(intent_id))
986 i = self.handle.expect([
987 "Error",
988 "onos>"])
989
990 handle = self.handle.before
991
992 if i == 0:
993 main.log.error("Error in removing intent")
994 return handle
995 else:
996 return handle
997
998 except pexpect.EOF:
999 main.log.error(self.name + ": EOF exception found")
1000 main.log.error(self.name + ": " + self.handle.before)
1001 main.cleanup()
1002 main.exit()
1003 except:
1004 main.log.info(self.name+" ::::::")
1005 main.log.error( traceback.print_exc())
1006 main.log.info(self.name+" ::::::")
1007 main.cleanup()
1008 main.exit()
1009
andrewonlab377693f2014-10-21 16:00:30 -04001010 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001011 '''
andrewonlab377693f2014-10-21 16:00:30 -04001012 Optional:
1013 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001014 Description:
1015 Obtain intents currently installed
1016 '''
1017 try:
andrewonlab377693f2014-10-21 16:00:30 -04001018 if json_format:
1019 self.handle.sendline("intents -j")
1020 self.handle.expect("intents -j")
1021 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001022 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001023
andrewonlab377693f2014-10-21 16:00:30 -04001024 else:
1025 self.handle.sendline("")
1026 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001027
andrewonlab377693f2014-10-21 16:00:30 -04001028 self.handle.sendline("intents")
1029 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001030 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001031
1032 return handle
1033
1034 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
Shreya Shah0f01c812014-10-26 20:15:28 -04001046 def flows(self, json_format = False):
1047 '''
1048 Optional:
1049 * json_format: enable output formatting in json
1050 Description:
1051 Obtain flows currently installed
1052 '''
1053 try:
1054 if json_format:
1055 self.handle.sendline("flows -j")
1056 self.handle.expect("flows -j")
1057 self.handle.expect("onos>")
1058 handle = self.handle.before
1059
1060 else:
1061 self.handle.sendline("")
1062 self.handle.expect("onos>")
1063 self.handle.sendline("flows")
1064 self.handle.expect("onos>")
1065 handle = self.handle.before
1066
1067 return handle
1068
1069 except pexpect.EOF:
1070 main.log.error(self.name + ": EOF exception found")
1071 main.log.error(self.name + ": " + self.handle.before)
1072 main.cleanup()
1073 main.exit()
1074 except:
1075 main.log.info(self.name+" ::::::")
1076 main.log.error( traceback.print_exc())
1077 main.log.info(self.name+" ::::::")
1078 main.cleanup()
1079 main.exit()
1080
1081
1082
andrewonlab867212a2014-10-22 20:13:38 -04001083 def topology_events_metrics(self, json_format=True):
1084 '''
1085 Description:Returns topology metrics
1086 Optional:
1087 * json_format: enable json formatting of output
1088 '''
1089 try:
1090 if json_format:
1091 self.handle.sendline("topology-events-metrics -j")
1092 self.handle.expect("topology-events-metrics -j")
1093 self.handle.expect("onos>")
1094
1095 handle = self.handle.before
1096
1097 #Some color thing that we want to escape
1098 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1099 handle = ansi_escape.sub('', handle)
1100
1101 else:
1102 self.handle.sendline("topology-events-metrics")
1103 self.handle.expect("topology-events-metrics")
1104 self.handle.expect("onos>")
1105
1106 handle = self.handle.before
1107
1108 return handle
1109
1110 except pexpect.EOF:
1111 main.log.error(self.name + ": EOF exception found")
1112 main.log.error(self.name + ": " + self.handle.before)
1113 main.cleanup()
1114 main.exit()
1115 except:
1116 main.log.info(self.name+" ::::::")
1117 main.log.error( traceback.print_exc())
1118 main.log.info(self.name+" ::::::")
1119 main.cleanup()
1120 main.exit()
1121
andrewonlab3e15ead2014-10-15 14:21:34 -04001122 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001123 #Wrapper functions use existing driver
1124 #functions and extends their use case.
1125 #For example, we may use the output of
1126 #a normal driver function, and parse it
1127 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001128
andrewonlab9a50dfe2014-10-17 17:22:31 -04001129 def get_all_intents_id(self):
1130 '''
1131 Description:
1132 Obtain all intent id's in a list
1133 '''
1134 try:
1135 #Obtain output of intents function
1136 intents_str = self.intents()
1137 all_intent_list = []
1138 intent_id_list = []
1139
1140 #Parse the intents output for ID's
1141 intents_list = [s.strip() for s in intents_str.splitlines()]
1142 for intents in intents_list:
1143 if "onos>" in intents:
1144 continue
1145 elif "intents" in intents:
1146 continue
1147 else:
1148 line_list = intents.split(" ")
1149 all_intent_list.append(line_list[0])
1150
1151 all_intent_list = all_intent_list[1:-2]
1152
1153 for intents in all_intent_list:
1154 if not intents:
1155 continue
1156 else:
1157 intent_id_list.append(intents)
1158
1159 return intent_id_list
1160
1161 except pexpect.EOF:
1162 main.log.error(self.name + ": EOF exception found")
1163 main.log.error(self.name + ": " + self.handle.before)
1164 main.cleanup()
1165 main.exit()
1166 except:
1167 main.log.info(self.name+" ::::::")
1168 main.log.error( traceback.print_exc())
1169 main.log.info(self.name+" ::::::")
1170 main.cleanup()
1171 main.exit()
1172
andrewonlab7e4d2d32014-10-15 13:23:21 -04001173 def get_all_devices_id(self):
1174 '''
1175 Use 'devices' function to obtain list of all devices
1176 and parse the result to obtain a list of all device
1177 id's. Returns this list. Returns empty list if no
1178 devices exist
1179 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001180
1181 This function may be useful if you are not sure of the
1182 device id, and wish to execute other commands using
1183 the ids. By obtaining the list of device ids on the fly,
1184 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001185 '''
1186 try:
1187 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001188 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001189 id_list = []
1190
1191 if not devices_str:
1192 main.log.info("There are no devices to get id from")
1193 return id_list
1194
1195 #Split the string into list by comma
1196 device_list = devices_str.split(",")
1197 #Get temporary list of all arguments with string 'id='
1198 temp_list = [dev for dev in device_list if "id=" in dev]
1199 #Split list further into arguments before and after string
1200 # 'id='. Get the latter portion (the actual device id) and
1201 # append to id_list
1202 for arg in temp_list:
1203 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001204 return id_list
1205
1206 except pexpect.EOF:
1207 main.log.error(self.name + ": EOF exception found")
1208 main.log.error(self.name + ": " + self.handle.before)
1209 main.cleanup()
1210 main.exit()
1211 except:
1212 main.log.info(self.name+" ::::::")
1213 main.log.error( traceback.print_exc())
1214 main.log.info(self.name+" ::::::")
1215 main.cleanup()
1216 main.exit()
1217
andrewonlab7c211572014-10-15 16:45:20 -04001218 def get_all_nodes_id(self):
1219 '''
1220 Uses 'nodes' function to obtain list of all nodes
1221 and parse the result of nodes to obtain just the
1222 node id's.
1223 Returns:
1224 list of node id's
1225 '''
1226 try:
1227 nodes_str = self.nodes()
1228 id_list = []
1229
1230 if not nodes_str:
1231 main.log.info("There are no nodes to get id from")
1232 return id_list
1233
1234 #Sample nodes_str output
1235 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1236
1237 #Split the string into list by comma
1238 nodes_list = nodes_str.split(",")
1239 temp_list = [node for node in nodes_list if "id=" in node]
1240 for arg in temp_list:
1241 id_list.append(arg.split("id=")[1])
1242
1243 return id_list
1244
1245 except pexpect.EOF:
1246 main.log.error(self.name + ": EOF exception found")
1247 main.log.error(self.name + ": " + self.handle.before)
1248 main.cleanup()
1249 main.exit()
1250 except:
1251 main.log.info(self.name+" ::::::")
1252 main.log.error( traceback.print_exc())
1253 main.log.info(self.name+" ::::::")
1254 main.cleanup()
1255 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001256
Jon Halla91c4dc2014-10-22 12:57:04 -04001257 def get_device(self, dpid=None):
1258 '''
1259 Return the first device from the devices api whose 'id' contains 'dpid'
1260 Return None if there is no match
1261 '''
1262 import json
1263 try:
1264 if dpid == None:
1265 return None
1266 else:
1267 dpid = dpid.replace(':', '')
1268 raw_devices = self.devices()
1269 devices_json = json.loads(raw_devices)
1270 #search json for the device with dpid then return the device
1271 for device in devices_json:
1272 #print "%s in %s?" % (dpid, device['id'])
1273 if dpid in device['id']:
1274 return device
1275 return None
1276 except pexpect.EOF:
1277 main.log.error(self.name + ": EOF exception found")
1278 main.log.error(self.name + ": " + self.handle.before)
1279 main.cleanup()
1280 main.exit()
1281 except:
1282 main.log.info(self.name+" ::::::")
1283 main.log.error( traceback.print_exc())
1284 main.log.info(self.name+" ::::::")
1285 main.cleanup()
1286 main.exit()
1287
Jon Hall42db6dc2014-10-24 19:03:48 -04001288 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1289 '''
1290 Checks the number of swithes & links that ONOS sees against the
1291 supplied values. By default this will report to main.log, but the
1292 log level can be specifid.
1293
1294 Params: ip = ip used for the onos cli
1295 numoswitch = expected number of switches
1296 numlink = expected number of links
1297 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1298
1299
1300 log_level can
1301
1302 Returns: main.TRUE if the number of switchs and links are correct,
1303 main.FALSE if the numer of switches and links is incorrect,
1304 and main.ERROR otherwise
1305 '''
1306
1307 try:
1308 topology = self.get_topology(ip)
1309 if topology == {}:
1310 return main.ERROR
1311 output = ""
1312 #Is the number of switches is what we expected
1313 devices = topology.get('devices',False)
1314 links = topology.get('links',False)
1315 if devices == False or links == False:
1316 return main.ERROR
1317 switch_check = ( int(devices) == int(numoswitch) )
1318 #Is the number of links is what we expected
1319 link_check = ( int(links) == int(numolink) )
1320 if (switch_check and link_check):
1321 #We expected the correct numbers
1322 output = output + "The number of links and switches match "\
1323 + "what was expected"
1324 result = main.TRUE
1325 else:
1326 output = output + \
1327 "The number of links and switches does not match what was expected"
1328 result = main.FALSE
1329 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1330 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1331 if log_level == "report":
1332 main.log.report(output)
1333 elif log_level == "warn":
1334 main.log.warn(output)
1335 else:
1336 main.log.info(output)
1337 return result
1338 except pexpect.EOF:
1339 main.log.error(self.name + ": EOF exception found")
1340 main.log.error(self.name + ": " + self.handle.before)
1341 main.cleanup()
1342 main.exit()
1343 except:
1344 main.log.info(self.name+" ::::::")
1345 main.log.error( traceback.print_exc())
1346 main.log.info(self.name+" ::::::")
1347 main.cleanup()
1348 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001349
1350 def device_role(self, device_id, onos_node, role="master"):
1351 '''
1352 Calls the device-role cli command.
1353 device_id must be the id of a device as seen in the onos devices command
1354 onos_node is the ip of one of the onos nodes in the cluster
1355 role must be either master, standby, or none
1356
1357 Returns main.TRUE or main.FALSE based argument varification.
1358 When device-role supports errors this should be extended to
1359 support that output
1360 '''
1361 #TODO: handle error messages from device-role
1362 try:
1363 print "beginning device_role... \n\tdevice_id:" + device_id
1364 print "\tonos_node: " + onos_node
1365 print "\trole: "+ role
1366 if role.lower() == "master" or \
1367 role.lower() == "standby" or \
1368 role.lower() == "none":
1369 self.handle.sendline("")
1370 self.handle.expect("onos>")
1371 self.handle.sendline("device-role " + device_id + " " + onos_node + " " + role)
1372 self.handle.expect("onos>")
1373 return main.TRUE
1374 else:
1375 return main.FALSE
1376
1377 except pexpect.EOF:
1378 main.log.error(self.name + ": EOF exception found")
1379 main.log.error(self.name + ": " + self.handle.before)
1380 main.cleanup()
1381 main.exit()
1382 except:
1383 main.log.info(self.name+" ::::::")
1384 main.log.error( traceback.print_exc())
1385 main.log.info(self.name+" ::::::")
1386 main.cleanup()
1387 main.exit()
1388
1389
andrewonlab7e4d2d32014-10-15 13:23:21 -04001390 #***********************************