blob: 4d4a417730a545d649dac4b6e1dfb0d18d3e3e81 [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
843 self.handle.sendline("")
844 self.handle.expect("onos>")
845
846 handle = self.handle.before
847
848 main.log.info("Intent installed between "+
849 str(host_id_one) + " and " + str(host_id_two))
850
851 return handle
852
853 except pexpect.EOF:
854 main.log.error(self.name + ": EOF exception found")
855 main.log.error(self.name + ": " + self.handle.before)
856 main.cleanup()
857 main.exit()
858 except:
859 main.log.info(self.name+" ::::::")
860 main.log.error( traceback.print_exc())
861 main.log.info(self.name+" ::::::")
862 main.cleanup()
863 main.exit()
864
andrewonlab7b31d232014-10-24 13:31:47 -0400865 def add_optical_intent(self, ingress_device, egress_device):
866 '''
867 Required:
868 * ingress_device: device id of ingress device
869 * egress_device: device id of egress device
870 Optional:
871 TODO: Still needs to be implemented via dev side
872 '''
873 try:
874 self.handle.sendline("add-optical-intent "+
875 str(ingress_device) + " " + str(egress_device))
876 self.handle.expect("add-optical-intent")
877 i = self.handle.expect([
878 "Error",
879 "onos>"])
880
881 handle = self.handle.before
882
883 #If error, return error message
884 if i == 0:
885 return handle
886 else:
887 return main.TRUE
888
889 except pexpect.EOF:
890 main.log.error(self.name + ": EOF exception found")
891 main.log.error(self.name + ": " + self.handle.before)
892 main.cleanup()
893 main.exit()
894 except:
895 main.log.info(self.name+" ::::::")
896 main.log.error( traceback.print_exc())
897 main.log.info(self.name+" ::::::")
898 main.cleanup()
899 main.exit()
900
andrewonlab4dbb4d82014-10-17 18:22:31 -0400901 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400902 egress_device, port_egress, ethType="", ethSrc="",
903 ethDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400904 '''
905 Required:
906 * ingress_device: device id of ingress device
907 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400908 Optional:
909 * ethType: specify ethType
910 * ethSrc: specify ethSrc (i.e. src mac addr)
911 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400912 Description:
913 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400914 specifying device id's and optional fields
915
andrewonlab4dbb4d82014-10-17 18:22:31 -0400916 NOTE: This function may change depending on the
917 options developers provide for point-to-point
918 intent via cli
919 '''
920 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400921 cmd = ""
922
923 #If there are no optional arguments
924 if not ethType and not ethSrc and not ethDst:
925 cmd = "add-point-intent "+\
926 str(ingress_device) + "/" + str(port_ingress) + " " +\
927 str(egress_device) + "/" + str(port_egress)
928
929 else:
andrewonlab9a130be2014-10-22 12:44:56 -0400930 cmd = "add-point-intent "
931
andrewonlab0c0a6772014-10-22 12:31:18 -0400932 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -0400933 cmd += " --ethType " + str(ethType)
934 if ethSrc:
935 cmd += " --ethSrc " + str(ethSrc)
936 if ethDst:
937 cmd += " --ethDst " + str(ethDst)
andrewonlab9a130be2014-10-22 12:44:56 -0400938
939 cmd += " "+str(ingress_device) + "/" + str(port_ingress) + " " +\
940 str(egress_device) + "/" + str(port_egress)
andrewonlab289e4b72014-10-21 21:24:18 -0400941
Shreya Shah0f01c812014-10-26 20:15:28 -0400942 print "cmd = ", cmd
943 #self.handle.sendline("")
944 #self.handle.expect("onos>")
andrewonlab4dbb4d82014-10-17 18:22:31 -0400945
andrewonlab289e4b72014-10-21 21:24:18 -0400946 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400947 i = self.handle.expect([
948 "Error",
949 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -0400950
Shreya Shah0f01c812014-10-26 20:15:28 -0400951 self.handle.sendline("intents")
andrewonlab4dbb4d82014-10-17 18:22:31 -0400952 self.handle.expect("onos>")
Shreya Shah0f01c812014-10-26 20:15:28 -0400953 Intenthandle = self.handle.before
954 #print "Intenthandle = ", Intenthandle
andrewonlab4dbb4d82014-10-17 18:22:31 -0400955
Shreya Shah0f01c812014-10-26 20:15:28 -0400956 #self.handle.sendline("flows")
957 #self.handle.expect("onos>")
958 #Flowhandle = self.handle.before
959 #print "Flowhandle = ", Flowhandle
andrewonlab4dbb4d82014-10-17 18:22:31 -0400960
961 if i == 0:
962 main.log.error("Error in adding point-to-point intent")
963 return handle
964 else:
965 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -0400966
andrewonlab4dbb4d82014-10-17 18:22:31 -0400967 except pexpect.EOF:
968 main.log.error(self.name + ": EOF exception found")
969 main.log.error(self.name + ": " + self.handle.before)
970 main.cleanup()
971 main.exit()
972 except:
973 main.log.info(self.name+" ::::::")
974 main.log.error( traceback.print_exc())
975 main.log.info(self.name+" ::::::")
976 main.cleanup()
977 main.exit()
978
andrewonlab9a50dfe2014-10-17 17:22:31 -0400979 def remove_intent(self, intent_id):
980 '''
981 Remove intent for specified intent id
982 '''
983 try:
984 self.handle.sendline("")
985 self.handle.expect("onos>")
986
987 self.handle.sendline("remove-intent "+str(intent_id))
988 i = self.handle.expect([
989 "Error",
990 "onos>"])
991
992 handle = self.handle.before
993
994 if i == 0:
995 main.log.error("Error in removing intent")
996 return handle
997 else:
998 return handle
999
1000 except pexpect.EOF:
1001 main.log.error(self.name + ": EOF exception found")
1002 main.log.error(self.name + ": " + self.handle.before)
1003 main.cleanup()
1004 main.exit()
1005 except:
1006 main.log.info(self.name+" ::::::")
1007 main.log.error( traceback.print_exc())
1008 main.log.info(self.name+" ::::::")
1009 main.cleanup()
1010 main.exit()
1011
andrewonlab377693f2014-10-21 16:00:30 -04001012 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001013 '''
andrewonlab377693f2014-10-21 16:00:30 -04001014 Optional:
1015 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001016 Description:
1017 Obtain intents currently installed
1018 '''
1019 try:
andrewonlab377693f2014-10-21 16:00:30 -04001020 if json_format:
1021 self.handle.sendline("intents -j")
1022 self.handle.expect("intents -j")
1023 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001024 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001025
andrewonlab377693f2014-10-21 16:00:30 -04001026 else:
1027 self.handle.sendline("")
1028 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001029
andrewonlab377693f2014-10-21 16:00:30 -04001030 self.handle.sendline("intents")
1031 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001032 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001033
1034 return handle
1035
1036 except pexpect.EOF:
1037 main.log.error(self.name + ": EOF exception found")
1038 main.log.error(self.name + ": " + self.handle.before)
1039 main.cleanup()
1040 main.exit()
1041 except:
1042 main.log.info(self.name+" ::::::")
1043 main.log.error( traceback.print_exc())
1044 main.log.info(self.name+" ::::::")
1045 main.cleanup()
1046 main.exit()
1047
Shreya Shah0f01c812014-10-26 20:15:28 -04001048 def flows(self, json_format = False):
1049 '''
1050 Optional:
1051 * json_format: enable output formatting in json
1052 Description:
1053 Obtain flows currently installed
1054 '''
1055 try:
1056 if json_format:
1057 self.handle.sendline("flows -j")
1058 self.handle.expect("flows -j")
1059 self.handle.expect("onos>")
1060 handle = self.handle.before
1061
1062 else:
1063 self.handle.sendline("")
1064 self.handle.expect("onos>")
1065 self.handle.sendline("flows")
1066 self.handle.expect("onos>")
1067 handle = self.handle.before
1068
1069 return handle
1070
1071 except pexpect.EOF:
1072 main.log.error(self.name + ": EOF exception found")
1073 main.log.error(self.name + ": " + self.handle.before)
1074 main.cleanup()
1075 main.exit()
1076 except:
1077 main.log.info(self.name+" ::::::")
1078 main.log.error( traceback.print_exc())
1079 main.log.info(self.name+" ::::::")
1080 main.cleanup()
1081 main.exit()
1082
1083
1084
andrewonlab867212a2014-10-22 20:13:38 -04001085 def topology_events_metrics(self, json_format=True):
1086 '''
1087 Description:Returns topology metrics
1088 Optional:
1089 * json_format: enable json formatting of output
1090 '''
1091 try:
1092 if json_format:
1093 self.handle.sendline("topology-events-metrics -j")
1094 self.handle.expect("topology-events-metrics -j")
1095 self.handle.expect("onos>")
1096
1097 handle = self.handle.before
1098
1099 #Some color thing that we want to escape
1100 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1101 handle = ansi_escape.sub('', handle)
1102
1103 else:
1104 self.handle.sendline("topology-events-metrics")
1105 self.handle.expect("topology-events-metrics")
1106 self.handle.expect("onos>")
1107
1108 handle = self.handle.before
1109
1110 return handle
1111
1112 except pexpect.EOF:
1113 main.log.error(self.name + ": EOF exception found")
1114 main.log.error(self.name + ": " + self.handle.before)
1115 main.cleanup()
1116 main.exit()
1117 except:
1118 main.log.info(self.name+" ::::::")
1119 main.log.error( traceback.print_exc())
1120 main.log.info(self.name+" ::::::")
1121 main.cleanup()
1122 main.exit()
1123
andrewonlab3e15ead2014-10-15 14:21:34 -04001124 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001125 #Wrapper functions use existing driver
1126 #functions and extends their use case.
1127 #For example, we may use the output of
1128 #a normal driver function, and parse it
1129 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001130
andrewonlab9a50dfe2014-10-17 17:22:31 -04001131 def get_all_intents_id(self):
1132 '''
1133 Description:
1134 Obtain all intent id's in a list
1135 '''
1136 try:
1137 #Obtain output of intents function
1138 intents_str = self.intents()
1139 all_intent_list = []
1140 intent_id_list = []
1141
1142 #Parse the intents output for ID's
1143 intents_list = [s.strip() for s in intents_str.splitlines()]
1144 for intents in intents_list:
1145 if "onos>" in intents:
1146 continue
1147 elif "intents" in intents:
1148 continue
1149 else:
1150 line_list = intents.split(" ")
1151 all_intent_list.append(line_list[0])
1152
1153 all_intent_list = all_intent_list[1:-2]
1154
1155 for intents in all_intent_list:
1156 if not intents:
1157 continue
1158 else:
1159 intent_id_list.append(intents)
1160
1161 return intent_id_list
1162
1163 except pexpect.EOF:
1164 main.log.error(self.name + ": EOF exception found")
1165 main.log.error(self.name + ": " + self.handle.before)
1166 main.cleanup()
1167 main.exit()
1168 except:
1169 main.log.info(self.name+" ::::::")
1170 main.log.error( traceback.print_exc())
1171 main.log.info(self.name+" ::::::")
1172 main.cleanup()
1173 main.exit()
1174
andrewonlab7e4d2d32014-10-15 13:23:21 -04001175 def get_all_devices_id(self):
1176 '''
1177 Use 'devices' function to obtain list of all devices
1178 and parse the result to obtain a list of all device
1179 id's. Returns this list. Returns empty list if no
1180 devices exist
1181 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001182
1183 This function may be useful if you are not sure of the
1184 device id, and wish to execute other commands using
1185 the ids. By obtaining the list of device ids on the fly,
1186 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001187 '''
1188 try:
1189 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001190 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001191 id_list = []
1192
1193 if not devices_str:
1194 main.log.info("There are no devices to get id from")
1195 return id_list
1196
1197 #Split the string into list by comma
1198 device_list = devices_str.split(",")
1199 #Get temporary list of all arguments with string 'id='
1200 temp_list = [dev for dev in device_list if "id=" in dev]
1201 #Split list further into arguments before and after string
1202 # 'id='. Get the latter portion (the actual device id) and
1203 # append to id_list
1204 for arg in temp_list:
1205 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001206 return id_list
1207
1208 except pexpect.EOF:
1209 main.log.error(self.name + ": EOF exception found")
1210 main.log.error(self.name + ": " + self.handle.before)
1211 main.cleanup()
1212 main.exit()
1213 except:
1214 main.log.info(self.name+" ::::::")
1215 main.log.error( traceback.print_exc())
1216 main.log.info(self.name+" ::::::")
1217 main.cleanup()
1218 main.exit()
1219
andrewonlab7c211572014-10-15 16:45:20 -04001220 def get_all_nodes_id(self):
1221 '''
1222 Uses 'nodes' function to obtain list of all nodes
1223 and parse the result of nodes to obtain just the
1224 node id's.
1225 Returns:
1226 list of node id's
1227 '''
1228 try:
1229 nodes_str = self.nodes()
1230 id_list = []
1231
1232 if not nodes_str:
1233 main.log.info("There are no nodes to get id from")
1234 return id_list
1235
1236 #Sample nodes_str output
1237 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1238
1239 #Split the string into list by comma
1240 nodes_list = nodes_str.split(",")
1241 temp_list = [node for node in nodes_list if "id=" in node]
1242 for arg in temp_list:
1243 id_list.append(arg.split("id=")[1])
1244
1245 return id_list
1246
1247 except pexpect.EOF:
1248 main.log.error(self.name + ": EOF exception found")
1249 main.log.error(self.name + ": " + self.handle.before)
1250 main.cleanup()
1251 main.exit()
1252 except:
1253 main.log.info(self.name+" ::::::")
1254 main.log.error( traceback.print_exc())
1255 main.log.info(self.name+" ::::::")
1256 main.cleanup()
1257 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001258
Jon Halla91c4dc2014-10-22 12:57:04 -04001259 def get_device(self, dpid=None):
1260 '''
1261 Return the first device from the devices api whose 'id' contains 'dpid'
1262 Return None if there is no match
1263 '''
1264 import json
1265 try:
1266 if dpid == None:
1267 return None
1268 else:
1269 dpid = dpid.replace(':', '')
1270 raw_devices = self.devices()
1271 devices_json = json.loads(raw_devices)
1272 #search json for the device with dpid then return the device
1273 for device in devices_json:
1274 #print "%s in %s?" % (dpid, device['id'])
1275 if dpid in device['id']:
1276 return device
1277 return None
1278 except pexpect.EOF:
1279 main.log.error(self.name + ": EOF exception found")
1280 main.log.error(self.name + ": " + self.handle.before)
1281 main.cleanup()
1282 main.exit()
1283 except:
1284 main.log.info(self.name+" ::::::")
1285 main.log.error( traceback.print_exc())
1286 main.log.info(self.name+" ::::::")
1287 main.cleanup()
1288 main.exit()
1289
Jon Hall42db6dc2014-10-24 19:03:48 -04001290 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1291 '''
1292 Checks the number of swithes & links that ONOS sees against the
1293 supplied values. By default this will report to main.log, but the
1294 log level can be specifid.
1295
1296 Params: ip = ip used for the onos cli
1297 numoswitch = expected number of switches
1298 numlink = expected number of links
1299 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1300
1301
1302 log_level can
1303
1304 Returns: main.TRUE if the number of switchs and links are correct,
1305 main.FALSE if the numer of switches and links is incorrect,
1306 and main.ERROR otherwise
1307 '''
1308
1309 try:
1310 topology = self.get_topology(ip)
1311 if topology == {}:
1312 return main.ERROR
1313 output = ""
1314 #Is the number of switches is what we expected
1315 devices = topology.get('devices',False)
1316 links = topology.get('links',False)
1317 if devices == False or links == False:
1318 return main.ERROR
1319 switch_check = ( int(devices) == int(numoswitch) )
1320 #Is the number of links is what we expected
1321 link_check = ( int(links) == int(numolink) )
1322 if (switch_check and link_check):
1323 #We expected the correct numbers
1324 output = output + "The number of links and switches match "\
1325 + "what was expected"
1326 result = main.TRUE
1327 else:
1328 output = output + \
1329 "The number of links and switches does not match what was expected"
1330 result = main.FALSE
1331 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1332 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1333 if log_level == "report":
1334 main.log.report(output)
1335 elif log_level == "warn":
1336 main.log.warn(output)
1337 else:
1338 main.log.info(output)
1339 return result
1340 except pexpect.EOF:
1341 main.log.error(self.name + ": EOF exception found")
1342 main.log.error(self.name + ": " + self.handle.before)
1343 main.cleanup()
1344 main.exit()
1345 except:
1346 main.log.info(self.name+" ::::::")
1347 main.log.error( traceback.print_exc())
1348 main.log.info(self.name+" ::::::")
1349 main.cleanup()
1350 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001351
1352 def device_role(self, device_id, onos_node, role="master"):
1353 '''
1354 Calls the device-role cli command.
1355 device_id must be the id of a device as seen in the onos devices command
1356 onos_node is the ip of one of the onos nodes in the cluster
1357 role must be either master, standby, or none
1358
1359 Returns main.TRUE or main.FALSE based argument varification.
1360 When device-role supports errors this should be extended to
1361 support that output
1362 '''
1363 #TODO: handle error messages from device-role
1364 try:
1365 print "beginning device_role... \n\tdevice_id:" + device_id
1366 print "\tonos_node: " + onos_node
1367 print "\trole: "+ role
1368 if role.lower() == "master" or \
1369 role.lower() == "standby" or \
1370 role.lower() == "none":
1371 self.handle.sendline("")
1372 self.handle.expect("onos>")
1373 self.handle.sendline("device-role " + device_id + " " + onos_node + " " + role)
1374 self.handle.expect("onos>")
1375 return main.TRUE
1376 else:
1377 return main.FALSE
1378
1379 except pexpect.EOF:
1380 main.log.error(self.name + ": EOF exception found")
1381 main.log.error(self.name + ": " + self.handle.before)
1382 main.cleanup()
1383 main.exit()
1384 except:
1385 main.log.info(self.name+" ::::::")
1386 main.log.error( traceback.print_exc())
1387 main.log.info(self.name+" ::::::")
1388 main.cleanup()
1389 main.exit()
1390
1391
andrewonlab7e4d2d32014-10-15 13:23:21 -04001392 #***********************************