blob: facd23e4aa7bc7d09291f81c1b694a2893c1d8b3 [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
andrewonlab38d2b4a2014-11-13 16:28:47 -0500106 def logout(self):
107 '''
108 Sends 'logout' command to ONOS cli
109 '''
110 try:
111 self.handle.sendline("")
112 self.handle.expect("onos>")
113 self.handle.sendline("logout")
114 self.handle.expect("\$")
115
116 except pexpect.EOF:
117 main.log.error(self.name + ": eof exception found")
118 main.log.error(self.name + ": " + self.handle.before)
119 main.cleanup()
120 main.exit()
121 except:
122 main.log.info(self.name+" ::::::")
123 main.log.error( traceback.print_exc())
124 main.log.info(self.name+" ::::::")
125 main.cleanup()
126 main.exit()
127
andrewonlab95ce8322014-10-13 14:12:04 -0400128 def set_cell(self, cellname):
129 '''
130 Calls 'cell <name>' to set the environment variables on ONOSbench
131
132 Before issuing any cli commands, set the environment variable first.
133 '''
134 try:
135 if not cellname:
136 main.log.error("Must define cellname")
137 main.cleanup()
138 main.exit()
139 else:
140 self.handle.sendline("cell "+str(cellname))
141 #Expect the cellname in the ONOS_CELL variable.
142 #Note that this variable name is subject to change
143 # and that this driver will have to change accordingly
144 self.handle.expect("ONOS_CELL="+str(cellname))
145 handle_before = self.handle.before
146 handle_after = self.handle.after
147 #Get the rest of the handle
148 self.handle.sendline("")
149 self.handle.expect("\$")
150 handle_more = self.handle.before
151
152 main.log.info("Cell call returned: "+handle_before+
153 handle_after + handle_more)
154
155 return main.TRUE
156
157 except pexpect.EOF:
andrewonlab38d2b4a2014-11-13 16:28:47 -0500158 main.log.error(self.name + ": eof exception found")
andrewonlab95ce8322014-10-13 14:12:04 -0400159 main.log.error(self.name + ": " + self.handle.before)
160 main.cleanup()
161 main.exit()
162 except:
163 main.log.info(self.name+" ::::::")
164 main.log.error( traceback.print_exc())
165 main.log.info(self.name+" ::::::")
166 main.cleanup()
167 main.exit()
168
andrewonlabc2d05aa2014-10-13 16:51:10 -0400169 def start_onos_cli(self, ONOS_ip):
andrewonlab95ce8322014-10-13 14:12:04 -0400170 try:
171 self.handle.sendline("")
172 self.handle.expect("\$")
173
174 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400175 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400176 i = self.handle.expect([
177 "onos>",
178 pexpect.TIMEOUT],timeout=60)
179
180 if i == 0:
181 main.log.info(str(ONOS_ip)+" CLI Started successfully")
182 return main.TRUE
183 else:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400184 #If failed, send ctrl+c to process and try again
andrewonlabf47993a2014-10-24 17:56:01 -0400185 main.log.info("Starting CLI failed. Retrying...")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400186 self.handle.sendline("\x03")
187 self.handle.sendline("onos -w "+str(ONOS_ip))
188 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
189 timeout=30)
190 if i == 0:
191 return main.TRUE
192 else:
193 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400194 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400195 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400196
197 except pexpect.EOF:
198 main.log.error(self.name + ": EOF exception found")
199 main.log.error(self.name + ": " + self.handle.before)
200 main.cleanup()
201 main.exit()
202 except:
203 main.log.info(self.name+" ::::::")
204 main.log.error( traceback.print_exc())
205 main.log.info(self.name+" ::::::")
206 main.cleanup()
207 main.exit()
208
andrewonlaba18f6bf2014-10-13 19:31:54 -0400209 def sendline(self, cmd_str):
210 '''
211 Send a completely user specified string to
212 the onos> prompt. Use this function if you have
213 a very specific command to send.
214
215 Warning: There are no sanity checking to commands
216 sent using this method.
217 '''
218 try:
219 self.handle.sendline("")
220 self.handle.expect("onos>")
221
222 self.handle.sendline(cmd_str)
223 self.handle.expect("onos>")
224
225 handle = self.handle.before
226
227 self.handle.sendline("")
228 self.handle.expect("onos>")
229
230 handle += self.handle.before
231 handle += self.handle.after
232
233 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400234 ansi_escape = re.compile(r'\x1b[^m]*m')
235 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400236
237 return handle
238 except pexpect.EOF:
239 main.log.error(self.name + ": EOF exception found")
240 main.log.error(self.name + ": " + self.handle.before)
241 main.cleanup()
242 main.exit()
243 except:
244 main.log.info(self.name+" ::::::")
245 main.log.error( traceback.print_exc())
246 main.log.info(self.name+" ::::::")
247 main.cleanup()
248 main.exit()
249
andrewonlab95ce8322014-10-13 14:12:04 -0400250 #IMPORTANT NOTE:
251 #For all cli commands, naming convention should match
252 #the cli command replacing ':' with '_'.
253 #Ex) onos:topology > onos_topology
254 # onos:links > onos_links
255 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400256
257 def add_node(self, node_id, ONOS_ip, tcp_port=""):
258 '''
259 Adds a new cluster node by ID and address information.
260 Required:
261 * node_id
262 * ONOS_ip
263 Optional:
264 * tcp_port
265 '''
266 try:
267 self.handle.sendline("")
268 self.handle.expect("onos>")
269
270 self.handle.sendline("add-node "+
271 str(node_id)+" "+
272 str(ONOS_ip)+" "+
273 str(tcp_port))
274
275 i = self.handle.expect([
276 "Error",
277 "onos>" ])
278
279 #Clear handle to get previous output
280 self.handle.sendline("")
281 self.handle.expect("onos>")
282
283 handle = self.handle.before
284
285 if i == 0:
286 main.log.error("Error in adding node")
287 main.log.error(handle)
288 return main.FALSE
289 else:
290 main.log.info("Node "+str(ONOS_ip)+" added")
291 return main.TRUE
292
293 except pexpect.EOF:
294 main.log.error(self.name + ": EOF exception found")
295 main.log.error(self.name + ": " + self.handle.before)
296 main.cleanup()
297 main.exit()
298 except:
299 main.log.info(self.name+" ::::::")
300 main.log.error( traceback.print_exc())
301 main.log.info(self.name+" ::::::")
302 main.cleanup()
303 main.exit()
304
andrewonlab86dc3082014-10-13 18:18:38 -0400305 def remove_node(self, node_id):
306 '''
307 Removes a cluster by ID
308 Issues command: 'remove-node [<node-id>]'
309 Required:
310 * node_id
311 '''
312 try:
313 self.handle.sendline("")
314 self.handle.expect("onos>")
315
316 self.handle.sendline("remove-node "+str(node_id))
317 self.handle.expect("onos>")
318
319 return main.TRUE
320
321 except pexpect.EOF:
322 main.log.error(self.name + ": EOF exception found")
323 main.log.error(self.name + ": " + self.handle.before)
324 main.cleanup()
325 main.exit()
326 except:
327 main.log.info(self.name+" ::::::")
328 main.log.error( traceback.print_exc())
329 main.log.info(self.name+" ::::::")
330 main.cleanup()
331 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400332
andrewonlab7c211572014-10-15 16:45:20 -0400333 def nodes(self):
334 '''
335 List the nodes currently visible
336 Issues command: 'nodes'
337 Returns: entire handle of list of nodes
338 '''
339 try:
340 self.handle.sendline("")
341 self.handle.expect("onos>")
342
343 self.handle.sendline("nodes")
344 self.handle.expect("onos>")
345
346 self.handle.sendline("")
347 self.handle.expect("onos>")
348
349 handle = self.handle.before
350
351 return handle
352
353 except pexpect.EOF:
354 main.log.error(self.name + ": EOF exception found")
355 main.log.error(self.name + ": " + self.handle.before)
356 main.cleanup()
357 main.exit()
358 except:
359 main.log.info(self.name+" ::::::")
360 main.log.error( traceback.print_exc())
361 main.log.info(self.name+" ::::::")
362 main.cleanup()
363 main.exit()
364
andrewonlab38d6ae22014-10-15 14:23:45 -0400365 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400366 '''
367 Shows the current state of the topology
368 by issusing command: 'onos> onos:topology'
369 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400370 try:
371 self.handle.sendline("")
372 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400373 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400374 self.handle.sendline("onos:topology")
375 self.handle.expect("onos>")
376
377 handle = self.handle.before
378
379 main.log.info("onos:topology returned: " +
380 str(handle))
381
382 return handle
383
384 except pexpect.EOF:
385 main.log.error(self.name + ": EOF exception found")
386 main.log.error(self.name + ": " + self.handle.before)
387 main.cleanup()
388 main.exit()
389 except:
390 main.log.info(self.name+" ::::::")
391 main.log.error( traceback.print_exc())
392 main.log.info(self.name+" ::::::")
393 main.cleanup()
394 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400395
396 def feature_install(self, feature_str):
397 '''
398 Installs a specified feature
399 by issuing command: 'onos> feature:install <feature_str>'
400 '''
401 try:
402 self.handle.sendline("")
403 self.handle.expect("onos>")
404
405 self.handle.sendline("feature:install "+str(feature_str))
406 self.handle.expect("onos>")
407
408 return main.TRUE
409
410 except pexpect.EOF:
411 main.log.error(self.name + ": EOF exception found")
412 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500413 main.log.report("Failed to install feature")
414 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400415 main.cleanup()
416 main.exit()
417 except:
418 main.log.info(self.name+" ::::::")
419 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500420 main.log.report("Failed to install feature")
421 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400422 main.log.info(self.name+" ::::::")
423 main.cleanup()
424 main.exit()
425
426 def feature_uninstall(self, feature_str):
427 '''
428 Uninstalls a specified feature
429 by issuing command: 'onos> feature:uninstall <feature_str>'
430 '''
431 try:
432 self.handle.sendline("")
433 self.handle.expect("onos>")
434
435 self.handle.sendline("feature:uninstall "+str(feature_str))
436 self.handle.expect("onos>")
437
438 return main.TRUE
439
440 except pexpect.EOF:
441 main.log.error(self.name + ": EOF exception found")
442 main.log.error(self.name + ": " + self.handle.before)
443 main.cleanup()
444 main.exit()
445 except:
446 main.log.info(self.name+" ::::::")
447 main.log.error( traceback.print_exc())
448 main.log.info(self.name+" ::::::")
449 main.cleanup()
450 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400451
Jon Halle8217482014-10-17 13:49:14 -0400452 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400453 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400454 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400455 Optional argument:
456 * grep_str - pass in a string to grep
457 '''
458 try:
459 self.handle.sendline("")
460 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400461
462 if json_format:
463 if not grep_str:
464 self.handle.sendline("devices -j")
465 self.handle.expect("devices -j")
466 self.handle.expect("onos>")
467 else:
468 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400469 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400470 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
471 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400472 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400473 '''
474 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
475 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 -0400476 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Hall983a1702014-10-28 18:44:22 -0400477 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400478 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400479 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400480 '''
481 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400482 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
483 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400484 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400485 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400486 else:
487 if not grep_str:
488 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400489 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400490 else:
491 self.handle.sendline("devices | grep '"+
492 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400493 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400494 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400495 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400496 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400497 except pexpect.EOF:
498 main.log.error(self.name + ": EOF exception found")
499 main.log.error(self.name + ": " + self.handle.before)
500 main.cleanup()
501 main.exit()
502 except:
503 main.log.info(self.name+" ::::::")
504 main.log.error( traceback.print_exc())
505 main.log.info(self.name+" ::::::")
506 main.cleanup()
507 main.exit()
508
Jon Halle8217482014-10-17 13:49:14 -0400509 def links(self, json_format=True, grep_str=""):
510 '''
511 Lists all core links
512 Optional argument:
513 * grep_str - pass in a string to grep
514 '''
515 try:
516 self.handle.sendline("")
517 self.handle.expect("onos>")
518
519 if json_format:
520 if not grep_str:
521 self.handle.sendline("links -j")
522 self.handle.expect("links -j")
523 self.handle.expect("onos>")
524 else:
525 self.handle.sendline("links -j | grep '"+
526 str(grep_str)+"'")
527 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
528 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400529 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400530 '''
531 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
532 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 -0400533 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 -0400534 So we take off that escape sequence using
535 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
536 handle1 = ansi_escape.sub('', handle)
537 '''
538 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400539 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
540 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400541 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400542 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400543 else:
544 if not grep_str:
545 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400546 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400547 else:
548 self.handle.sendline("links | grep '"+
549 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400550 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400551 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400552 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400553 return handle
Jon Halle8217482014-10-17 13:49:14 -0400554 except pexpect.EOF:
555 main.log.error(self.name + ": EOF exception found")
556 main.log.error(self.name + ": " + self.handle.before)
557 main.cleanup()
558 main.exit()
559 except:
560 main.log.info(self.name+" ::::::")
561 main.log.error( traceback.print_exc())
562 main.log.info(self.name+" ::::::")
563 main.cleanup()
564 main.exit()
565
566
567 def ports(self, json_format=True, grep_str=""):
568 '''
569 Lists all ports
570 Optional argument:
571 * grep_str - pass in a string to grep
572 '''
573 try:
574 self.handle.sendline("")
575 self.handle.expect("onos>")
576
577 if json_format:
578 if not grep_str:
579 self.handle.sendline("ports -j")
580 self.handle.expect("ports -j")
581 self.handle.expect("onos>")
582 else:
583 self.handle.sendline("ports -j | grep '"+
584 str(grep_str)+"'")
585 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
586 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400587 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400588 '''
589 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
590 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 -0400591 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 -0400592 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400593 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
594 handle1 = ansi_escape.sub('', handle)
595 '''
596 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400597 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
598 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400599 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400600 return handle1
601
Jon Halle8217482014-10-17 13:49:14 -0400602 else:
603 if not grep_str:
604 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400605 self.handle.expect("onos>")
606 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400607 self.handle.expect("onos>")
608 else:
609 self.handle.sendline("ports | grep '"+
610 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400611 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400612 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400613 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400614 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400615 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400616 return handle
Jon Halle8217482014-10-17 13:49:14 -0400617 except pexpect.EOF:
618 main.log.error(self.name + ": EOF exception found")
619 main.log.error(self.name + ": " + self.handle.before)
620 main.cleanup()
621 main.exit()
622 except:
623 main.log.info(self.name+" ::::::")
624 main.log.error( traceback.print_exc())
625 main.log.info(self.name+" ::::::")
626 main.cleanup()
627 main.exit()
628
629
Jon Hall983a1702014-10-28 18:44:22 -0400630 def roles(self, json_format=True, grep_str=""):
andrewonlab7c211572014-10-15 16:45:20 -0400631 '''
Jon Hall983a1702014-10-28 18:44:22 -0400632 Lists all devices and the controllers with roles assigned to them
633 Optional argument:
634 * grep_str - pass in a string to grep
andrewonlab7c211572014-10-15 16:45:20 -0400635 '''
636 try:
637 self.handle.sendline("")
638 self.handle.expect("onos>")
andrewonlab7c211572014-10-15 16:45:20 -0400639
Jon Hall983a1702014-10-28 18:44:22 -0400640 if json_format:
641 if not grep_str:
642 self.handle.sendline("roles -j")
643 self.handle.expect("roles -j")
644 self.handle.expect("onos>")
645 else:
646 self.handle.sendline("roles -j | grep '"+
647 str(grep_str)+"'")
648 self.handle.expect("roles -j | grep '"+str(grep_str)+"'")
649 self.handle.expect("onos>")
650 handle = self.handle.before
651 '''
652 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
653 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
654 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
655 So we take off that escape sequence using the following commads:
656 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
657 handle1 = ansi_escape.sub('', handle)
658 '''
659 #print "repr(handle) =", repr(handle)
660 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
661 handle1 = ansi_escape.sub('', handle)
662 #print "repr(handle1) = ", repr(handle1)
663 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400664
andrewonlab7c211572014-10-15 16:45:20 -0400665 else:
Jon Hall983a1702014-10-28 18:44:22 -0400666 if not grep_str:
667 self.handle.sendline("roles")
668 self.handle.expect("onos>")
669 self.handle.sendline("")
670 self.handle.expect("onos>")
671 else:
672 self.handle.sendline("roles | grep '"+
673 str(grep_str)+"'")
674 self.handle.expect("onos>")
675 self.handle.sendline("")
676 self.handle.expect("onos>")
677 handle = self.handle.before
678 #print "handle =",handle
679 return handle
680 except pexpect.EOF:
681 main.log.error(self.name + ": EOF exception found")
682 main.log.error(self.name + ": " + self.handle.before)
683 main.cleanup()
684 main.exit()
685 except:
686 main.log.info(self.name+" ::::::")
687 main.log.error( traceback.print_exc())
688 main.log.info(self.name+" ::::::")
689 main.cleanup()
690 main.exit()
691
692 def get_role(self, device_id):
693 '''
694 Given the a string containing the json representation of the "roles" cli command and a
695 partial or whole device id, returns a json object containing the
696 roles output for the first device whose id contains "device_id"
697
698 Returns:
699 Dict of the role assignments for the given device or
700 None if not match
701 '''
702 try:
703 import json
704 if device_id == None:
705 return None
706 else:
707 raw_roles = self.roles()
708 roles_json = json.loads(raw_roles)
709 #search json for the device with id then return the device
710 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400711 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400712 if str(device_id) in device['id']:
713 return device
714 return None
andrewonlab7c211572014-10-15 16:45:20 -0400715
andrewonlab86dc3082014-10-13 18:18:38 -0400716 except pexpect.EOF:
717 main.log.error(self.name + ": EOF exception found")
718 main.log.error(self.name + ": " + self.handle.before)
719 main.cleanup()
720 main.exit()
721 except:
722 main.log.info(self.name+" ::::::")
723 main.log.error( traceback.print_exc())
724 main.log.info(self.name+" ::::::")
725 main.cleanup()
726 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400727
andrewonlab3e15ead2014-10-15 14:21:34 -0400728 def paths(self, src_id, dst_id):
729 '''
730 Returns string of paths, and the cost.
731 Issues command: onos:paths <src> <dst>
732 '''
733 try:
734 self.handle.sendline("")
735 self.handle.expect("onos>")
736
737 self.handle.sendline("onos:paths "+
738 str(src_id) + " " + str(dst_id))
739 i = self.handle.expect([
740 "Error",
741 "onos>"])
742
743 self.handle.sendline("")
744 self.handle.expect("onos>")
745
746 handle = self.handle.before
747
748 if i == 0:
749 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400750 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400751 else:
752 path = handle.split(";")[0]
753 cost = handle.split(";")[1]
754 return (path, cost)
755
756 except pexpect.EOF:
757 main.log.error(self.name + ": EOF exception found")
758 main.log.error(self.name + ": " + self.handle.before)
759 main.cleanup()
760 main.exit()
761 except:
762 main.log.info(self.name+" ::::::")
763 main.log.error( traceback.print_exc())
764 main.log.info(self.name+" ::::::")
765 main.cleanup()
766 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400767
Jon Hall42db6dc2014-10-24 19:03:48 -0400768 def hosts(self, json_format=True, grep_str=""):
769 '''
770 Lists all discovered hosts
771 Optional argument:
772 * grep_str - pass in a string to grep
773 '''
774 try:
775 self.handle.sendline("")
776 self.handle.expect("onos>")
777
778 if json_format:
779 if not grep_str:
780 self.handle.sendline("hosts -j")
781 self.handle.expect("hosts -j")
782 self.handle.expect("onos>")
783 else:
784 self.handle.sendline("hosts -j | grep '"+
785 str(grep_str)+"'")
786 self.handle.expect("hosts -j | grep '"+str(grep_str)+"'")
787 self.handle.expect("onos>")
788 handle = self.handle.before
789 '''
790 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
791 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
792 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
793 So we take off that escape sequence using
794 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
795 handle1 = ansi_escape.sub('', handle)
796 '''
797 #print "repr(handle) =", repr(handle)
798 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
799 handle1 = ansi_escape.sub('', handle)
800 #print "repr(handle1) = ", repr(handle1)
801 return handle1
802 else:
803 if not grep_str:
804 self.handle.sendline("hosts")
805 self.handle.expect("onos>")
806 else:
807 self.handle.sendline("hosts | grep '"+
808 str(grep_str)+"'")
809 self.handle.expect("onos>")
810 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400811 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400812 return handle
813 except pexpect.EOF:
814 main.log.error(self.name + ": EOF exception found")
815 main.log.error(self.name + ": " + self.handle.before)
816 main.cleanup()
817 main.exit()
818 except:
819 main.log.info(self.name+" ::::::")
820 main.log.error( traceback.print_exc())
821 main.log.info(self.name+" ::::::")
822 main.cleanup()
823 main.exit()
824
825 def get_host(self, mac):
826 '''
827 Return the first host from the hosts api whose 'id' contains 'mac'
828 Note: mac must be a colon seperated mac address, but could be a partial mac address
829 Return None if there is no match
830 '''
831 import json
832 try:
833 if mac == None:
834 return None
835 else:
836 mac = mac
837 raw_hosts = self.hosts()
838 hosts_json = json.loads(raw_hosts)
839 #search json for the host with mac then return the device
840 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400841 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400842 if mac in host['id']:
843 return host
844 return None
845 except pexpect.EOF:
846 main.log.error(self.name + ": EOF exception found")
847 main.log.error(self.name + ": " + self.handle.before)
848 main.cleanup()
849 main.exit()
850 except:
851 main.log.info(self.name+" ::::::")
852 main.log.error( traceback.print_exc())
853 main.log.info(self.name+" ::::::")
854 main.cleanup()
855 main.exit()
856
andrewonlab3f0a4af2014-10-17 12:25:14 -0400857
858 def get_hosts_id(self, host_list):
859 '''
860 Obtain list of hosts
861 Issues command: 'onos> hosts'
862
863 Required:
864 * host_list: List of hosts obtained by Mininet
865 IMPORTANT:
866 This function assumes that you started your
867 topology with the option '--mac'.
868 Furthermore, it assumes that value of VLAN is '-1'
869 Description:
870 Converts mininet hosts (h1, h2, h3...) into
871 ONOS format (00:00:00:00:00:01/-1 , ...)
872 '''
873
874 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400875 onos_host_list = []
876
877 for host in host_list:
878 host = host.replace("h", "")
879 host_hex = hex(int(host)).zfill(12)
880 host_hex = str(host_hex).replace('x','0')
881 i = iter(str(host_hex))
882 host_hex = ":".join(a+b for a,b in zip(i,i))
883 host_hex = host_hex + "/-1"
884 onos_host_list.append(host_hex)
885
886 return onos_host_list
887
888 except pexpect.EOF:
889 main.log.error(self.name + ": EOF exception found")
890 main.log.error(self.name + ": " + self.handle.before)
891 main.cleanup()
892 main.exit()
893 except:
894 main.log.info(self.name+" ::::::")
895 main.log.error( traceback.print_exc())
896 main.log.info(self.name+" ::::::")
897 main.cleanup()
898 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400899
andrewonlabe6745342014-10-17 14:29:13 -0400900 def add_host_intent(self, host_id_one, host_id_two):
901 '''
902 Required:
903 * host_id_one: ONOS host id for host1
904 * host_id_two: ONOS host id for host2
905 Description:
906 Adds a host-to-host intent (bidrectional) by
907 specifying the two hosts.
908 '''
909 try:
910 self.handle.sendline("")
911 self.handle.expect("onos>")
912
913 self.handle.sendline("add-host-intent "+
914 str(host_id_one) + " " + str(host_id_two))
915 self.handle.expect("onos>")
916
andrewonlabe6745342014-10-17 14:29:13 -0400917 handle = self.handle.before
Shreya Shah4f25fdf2014-10-29 19:55:35 -0400918 print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400919
920 main.log.info("Intent installed between "+
921 str(host_id_one) + " and " + str(host_id_two))
922
923 return handle
924
925 except pexpect.EOF:
926 main.log.error(self.name + ": EOF exception found")
927 main.log.error(self.name + ": " + self.handle.before)
928 main.cleanup()
929 main.exit()
930 except:
931 main.log.info(self.name+" ::::::")
932 main.log.error( traceback.print_exc())
933 main.log.info(self.name+" ::::::")
934 main.cleanup()
935 main.exit()
936
andrewonlab7b31d232014-10-24 13:31:47 -0400937 def add_optical_intent(self, ingress_device, egress_device):
938 '''
939 Required:
940 * ingress_device: device id of ingress device
941 * egress_device: device id of egress device
942 Optional:
943 TODO: Still needs to be implemented via dev side
944 '''
945 try:
946 self.handle.sendline("add-optical-intent "+
947 str(ingress_device) + " " + str(egress_device))
948 self.handle.expect("add-optical-intent")
949 i = self.handle.expect([
950 "Error",
951 "onos>"])
952
953 handle = self.handle.before
954
955 #If error, return error message
956 if i == 0:
957 return handle
958 else:
959 return main.TRUE
960
961 except pexpect.EOF:
962 main.log.error(self.name + ": EOF exception found")
963 main.log.error(self.name + ": " + self.handle.before)
964 main.cleanup()
965 main.exit()
966 except:
967 main.log.info(self.name+" ::::::")
968 main.log.error( traceback.print_exc())
969 main.log.info(self.name+" ::::::")
970 main.cleanup()
971 main.exit()
972
andrewonlab4dbb4d82014-10-17 18:22:31 -0400973 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400974 egress_device, port_egress, ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -0500975 ethDst="", bandwidth="", lambda_alloc=False,
976 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400977 '''
978 Required:
979 * ingress_device: device id of ingress device
980 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400981 Optional:
982 * ethType: specify ethType
983 * ethSrc: specify ethSrc (i.e. src mac addr)
984 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500985 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -0500986 * lambda_alloc: if True, intent will allocate lambda
987 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -0500988 * ipProto: specify ip protocol
989 * ipSrc: specify ip source address
990 * ipDst: specify ip destination address
991 * tcpSrc: specify tcp source port
992 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400993 Description:
994 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400995 specifying device id's and optional fields
996
andrewonlab4dbb4d82014-10-17 18:22:31 -0400997 NOTE: This function may change depending on the
998 options developers provide for point-to-point
999 intent via cli
1000 '''
1001 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001002 cmd = ""
1003
1004 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001005 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001006 and not bandwidth and not lambda_alloc \
1007 and not ipProto and not ipSrc and not ipDst \
1008 and not tcpSrc and not tcpDst:
andrewonlab289e4b72014-10-21 21:24:18 -04001009 cmd = "add-point-intent "+\
1010 str(ingress_device) + "/" + str(port_ingress) + " " +\
1011 str(egress_device) + "/" + str(port_egress)
1012
1013 else:
andrewonlab9a130be2014-10-22 12:44:56 -04001014 cmd = "add-point-intent "
1015
andrewonlab0c0a6772014-10-22 12:31:18 -04001016 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001017 cmd += " --ethType " + str(ethType)
1018 if ethSrc:
1019 cmd += " --ethSrc " + str(ethSrc)
1020 if ethDst:
1021 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001022 if bandwidth:
1023 cmd += " --bandwidth " + str(bandwidth)
1024 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001025 cmd += " --lambda "
1026 if ipProto:
1027 cmd += " --ipProto " + str(ipProto)
1028 if ipSrc:
1029 cmd += " --ipSrc " + str(ipSrc)
1030 if ipDst:
1031 cmd += " --ipDst " + str(ipDst)
1032 if tcpSrc:
1033 cmd += " --tcpSrc " + str(tcpSrc)
1034 if tcpDst:
1035 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001036
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001037 cmd += " "+str(ingress_device) +\
1038 "/" + str(port_ingress) + " " +\
1039 str(egress_device) + "/" + str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001040
andrewonlab289e4b72014-10-21 21:24:18 -04001041 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001042 i = self.handle.expect([
1043 "Error",
1044 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -04001045
Shreya Shah0f01c812014-10-26 20:15:28 -04001046 self.handle.sendline("intents")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001047 self.handle.expect("onos>")
Shreya Shah0f01c812014-10-26 20:15:28 -04001048 Intenthandle = self.handle.before
andrewonlab4dbb4d82014-10-17 18:22:31 -04001049
1050 if i == 0:
1051 main.log.error("Error in adding point-to-point intent")
1052 return handle
1053 else:
1054 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001055
andrewonlab4dbb4d82014-10-17 18:22:31 -04001056 except pexpect.EOF:
1057 main.log.error(self.name + ": EOF exception found")
1058 main.log.error(self.name + ": " + self.handle.before)
1059 main.cleanup()
1060 main.exit()
1061 except:
1062 main.log.info(self.name+" ::::::")
1063 main.log.error( traceback.print_exc())
1064 main.log.info(self.name+" ::::::")
1065 main.cleanup()
1066 main.exit()
1067
andrewonlab9a50dfe2014-10-17 17:22:31 -04001068 def remove_intent(self, intent_id):
1069 '''
1070 Remove intent for specified intent id
1071 '''
1072 try:
1073 self.handle.sendline("")
1074 self.handle.expect("onos>")
1075
1076 self.handle.sendline("remove-intent "+str(intent_id))
1077 i = self.handle.expect([
1078 "Error",
1079 "onos>"])
1080
1081 handle = self.handle.before
1082
1083 if i == 0:
1084 main.log.error("Error in removing intent")
1085 return handle
1086 else:
1087 return handle
1088
1089 except pexpect.EOF:
1090 main.log.error(self.name + ": EOF exception found")
1091 main.log.error(self.name + ": " + self.handle.before)
1092 main.cleanup()
1093 main.exit()
1094 except:
1095 main.log.info(self.name+" ::::::")
1096 main.log.error( traceback.print_exc())
1097 main.log.info(self.name+" ::::::")
1098 main.cleanup()
1099 main.exit()
1100
andrewonlab377693f2014-10-21 16:00:30 -04001101 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001102 '''
andrewonlab377693f2014-10-21 16:00:30 -04001103 Optional:
1104 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001105 Description:
1106 Obtain intents currently installed
1107 '''
1108 try:
andrewonlab377693f2014-10-21 16:00:30 -04001109 if json_format:
1110 self.handle.sendline("intents -j")
1111 self.handle.expect("intents -j")
1112 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001113 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001114
1115 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1116 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001117 else:
1118 self.handle.sendline("")
1119 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001120
andrewonlab377693f2014-10-21 16:00:30 -04001121 self.handle.sendline("intents")
1122 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001123 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001124
1125 return handle
1126
1127 except pexpect.EOF:
1128 main.log.error(self.name + ": EOF exception found")
1129 main.log.error(self.name + ": " + self.handle.before)
1130 main.cleanup()
1131 main.exit()
1132 except:
1133 main.log.info(self.name+" ::::::")
1134 main.log.error( traceback.print_exc())
1135 main.log.info(self.name+" ::::::")
1136 main.cleanup()
1137 main.exit()
1138
Shreya Shah0f01c812014-10-26 20:15:28 -04001139 def flows(self, json_format = False):
1140 '''
1141 Optional:
1142 * json_format: enable output formatting in json
1143 Description:
1144 Obtain flows currently installed
1145 '''
1146 try:
1147 if json_format:
1148 self.handle.sendline("flows -j")
1149 self.handle.expect("flows -j")
1150 self.handle.expect("onos>")
1151 handle = self.handle.before
1152
1153 else:
1154 self.handle.sendline("")
1155 self.handle.expect("onos>")
1156 self.handle.sendline("flows")
1157 self.handle.expect("onos>")
1158 handle = self.handle.before
1159
1160 return handle
1161
1162 except pexpect.EOF:
1163 main.log.error(self.name + ": EOF exception found")
1164 main.log.error(self.name + ": " + self.handle.before)
1165 main.cleanup()
1166 main.exit()
1167 except:
1168 main.log.info(self.name+" ::::::")
1169 main.log.error( traceback.print_exc())
1170 main.log.info(self.name+" ::::::")
1171 main.cleanup()
1172 main.exit()
1173
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001174 def intents_events_metrics(self, json_format=True):
1175 '''
1176 Description:Returns topology metrics
1177 Optional:
1178 * json_format: enable json formatting of output
1179 '''
1180 try:
1181 if json_format:
1182 self.handle.sendline("intents-events-metrics -j")
1183 self.handle.expect("intents-events-metrics -j")
1184 self.handle.expect("onos>")
1185
1186 handle = self.handle.before
1187
1188 #Some color thing that we want to escape
1189 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1190 handle = ansi_escape.sub('', handle)
1191
1192 else:
1193 self.handle.sendline("intents-events-metrics")
1194 self.handle.expect("intents-events-metrics")
1195 self.handle.expect("onos>")
1196
1197 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001198
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001199 return handle
1200
1201 except pexpect.EOF:
1202 main.log.error(self.name + ": EOF exception found")
1203 main.log.error(self.name + ": " + self.handle.before)
1204 main.cleanup()
1205 main.exit()
1206 except:
1207 main.log.info(self.name+" ::::::")
1208 main.log.error( traceback.print_exc())
1209 main.log.info(self.name+" ::::::")
1210 main.cleanup()
1211 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001212
andrewonlab867212a2014-10-22 20:13:38 -04001213 def topology_events_metrics(self, json_format=True):
1214 '''
1215 Description:Returns topology metrics
1216 Optional:
1217 * json_format: enable json formatting of output
1218 '''
1219 try:
1220 if json_format:
1221 self.handle.sendline("topology-events-metrics -j")
1222 self.handle.expect("topology-events-metrics -j")
1223 self.handle.expect("onos>")
1224
1225 handle = self.handle.before
1226
1227 #Some color thing that we want to escape
1228 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1229 handle = ansi_escape.sub('', handle)
1230
1231 else:
1232 self.handle.sendline("topology-events-metrics")
1233 self.handle.expect("topology-events-metrics")
1234 self.handle.expect("onos>")
1235
1236 handle = self.handle.before
1237
1238 return handle
1239
1240 except pexpect.EOF:
1241 main.log.error(self.name + ": EOF exception found")
1242 main.log.error(self.name + ": " + self.handle.before)
1243 main.cleanup()
1244 main.exit()
1245 except:
1246 main.log.info(self.name+" ::::::")
1247 main.log.error( traceback.print_exc())
1248 main.log.info(self.name+" ::::::")
1249 main.cleanup()
1250 main.exit()
1251
andrewonlab3e15ead2014-10-15 14:21:34 -04001252 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001253 #Wrapper functions use existing driver
1254 #functions and extends their use case.
1255 #For example, we may use the output of
1256 #a normal driver function, and parse it
1257 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001258
andrewonlab9a50dfe2014-10-17 17:22:31 -04001259 def get_all_intents_id(self):
1260 '''
1261 Description:
1262 Obtain all intent id's in a list
1263 '''
1264 try:
1265 #Obtain output of intents function
1266 intents_str = self.intents()
1267 all_intent_list = []
1268 intent_id_list = []
1269
1270 #Parse the intents output for ID's
1271 intents_list = [s.strip() for s in intents_str.splitlines()]
1272 for intents in intents_list:
1273 if "onos>" in intents:
1274 continue
1275 elif "intents" in intents:
1276 continue
1277 else:
1278 line_list = intents.split(" ")
1279 all_intent_list.append(line_list[0])
1280
1281 all_intent_list = all_intent_list[1:-2]
1282
1283 for intents in all_intent_list:
1284 if not intents:
1285 continue
1286 else:
1287 intent_id_list.append(intents)
1288
1289 return intent_id_list
1290
1291 except pexpect.EOF:
1292 main.log.error(self.name + ": EOF exception found")
1293 main.log.error(self.name + ": " + self.handle.before)
1294 main.cleanup()
1295 main.exit()
1296 except:
1297 main.log.info(self.name+" ::::::")
1298 main.log.error( traceback.print_exc())
1299 main.log.info(self.name+" ::::::")
1300 main.cleanup()
1301 main.exit()
1302
andrewonlab7e4d2d32014-10-15 13:23:21 -04001303 def get_all_devices_id(self):
1304 '''
1305 Use 'devices' function to obtain list of all devices
1306 and parse the result to obtain a list of all device
1307 id's. Returns this list. Returns empty list if no
1308 devices exist
1309 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001310
1311 This function may be useful if you are not sure of the
1312 device id, and wish to execute other commands using
1313 the ids. By obtaining the list of device ids on the fly,
1314 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001315 '''
1316 try:
1317 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001318 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001319 id_list = []
1320
1321 if not devices_str:
1322 main.log.info("There are no devices to get id from")
1323 return id_list
1324
1325 #Split the string into list by comma
1326 device_list = devices_str.split(",")
1327 #Get temporary list of all arguments with string 'id='
1328 temp_list = [dev for dev in device_list if "id=" in dev]
1329 #Split list further into arguments before and after string
1330 # 'id='. Get the latter portion (the actual device id) and
1331 # append to id_list
1332 for arg in temp_list:
1333 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001334 return id_list
1335
1336 except pexpect.EOF:
1337 main.log.error(self.name + ": EOF exception found")
1338 main.log.error(self.name + ": " + self.handle.before)
1339 main.cleanup()
1340 main.exit()
1341 except:
1342 main.log.info(self.name+" ::::::")
1343 main.log.error( traceback.print_exc())
1344 main.log.info(self.name+" ::::::")
1345 main.cleanup()
1346 main.exit()
1347
andrewonlab7c211572014-10-15 16:45:20 -04001348 def get_all_nodes_id(self):
1349 '''
1350 Uses 'nodes' function to obtain list of all nodes
1351 and parse the result of nodes to obtain just the
1352 node id's.
1353 Returns:
1354 list of node id's
1355 '''
1356 try:
1357 nodes_str = self.nodes()
1358 id_list = []
1359
1360 if not nodes_str:
1361 main.log.info("There are no nodes to get id from")
1362 return id_list
1363
1364 #Sample nodes_str output
1365 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1366
1367 #Split the string into list by comma
1368 nodes_list = nodes_str.split(",")
1369 temp_list = [node for node in nodes_list if "id=" in node]
1370 for arg in temp_list:
1371 id_list.append(arg.split("id=")[1])
1372
1373 return id_list
1374
1375 except pexpect.EOF:
1376 main.log.error(self.name + ": EOF exception found")
1377 main.log.error(self.name + ": " + self.handle.before)
1378 main.cleanup()
1379 main.exit()
1380 except:
1381 main.log.info(self.name+" ::::::")
1382 main.log.error( traceback.print_exc())
1383 main.log.info(self.name+" ::::::")
1384 main.cleanup()
1385 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001386
Jon Halla91c4dc2014-10-22 12:57:04 -04001387 def get_device(self, dpid=None):
1388 '''
1389 Return the first device from the devices api whose 'id' contains 'dpid'
1390 Return None if there is no match
1391 '''
1392 import json
1393 try:
1394 if dpid == None:
1395 return None
1396 else:
1397 dpid = dpid.replace(':', '')
1398 raw_devices = self.devices()
1399 devices_json = json.loads(raw_devices)
1400 #search json for the device with dpid then return the device
1401 for device in devices_json:
1402 #print "%s in %s?" % (dpid, device['id'])
1403 if dpid in device['id']:
1404 return device
1405 return None
1406 except pexpect.EOF:
1407 main.log.error(self.name + ": EOF exception found")
1408 main.log.error(self.name + ": " + self.handle.before)
1409 main.cleanup()
1410 main.exit()
1411 except:
1412 main.log.info(self.name+" ::::::")
1413 main.log.error( traceback.print_exc())
1414 main.log.info(self.name+" ::::::")
1415 main.cleanup()
1416 main.exit()
1417
Jon Hall42db6dc2014-10-24 19:03:48 -04001418 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1419 '''
1420 Checks the number of swithes & links that ONOS sees against the
1421 supplied values. By default this will report to main.log, but the
1422 log level can be specifid.
1423
1424 Params: ip = ip used for the onos cli
1425 numoswitch = expected number of switches
1426 numlink = expected number of links
1427 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1428
1429
1430 log_level can
1431
1432 Returns: main.TRUE if the number of switchs and links are correct,
1433 main.FALSE if the numer of switches and links is incorrect,
1434 and main.ERROR otherwise
1435 '''
1436
1437 try:
1438 topology = self.get_topology(ip)
1439 if topology == {}:
1440 return main.ERROR
1441 output = ""
1442 #Is the number of switches is what we expected
1443 devices = topology.get('devices',False)
1444 links = topology.get('links',False)
1445 if devices == False or links == False:
1446 return main.ERROR
1447 switch_check = ( int(devices) == int(numoswitch) )
1448 #Is the number of links is what we expected
1449 link_check = ( int(links) == int(numolink) )
1450 if (switch_check and link_check):
1451 #We expected the correct numbers
1452 output = output + "The number of links and switches match "\
1453 + "what was expected"
1454 result = main.TRUE
1455 else:
1456 output = output + \
1457 "The number of links and switches does not match what was expected"
1458 result = main.FALSE
1459 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1460 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1461 if log_level == "report":
1462 main.log.report(output)
1463 elif log_level == "warn":
1464 main.log.warn(output)
1465 else:
1466 main.log.info(output)
1467 return result
1468 except pexpect.EOF:
1469 main.log.error(self.name + ": EOF exception found")
1470 main.log.error(self.name + ": " + self.handle.before)
1471 main.cleanup()
1472 main.exit()
1473 except:
1474 main.log.info(self.name+" ::::::")
1475 main.log.error( traceback.print_exc())
1476 main.log.info(self.name+" ::::::")
1477 main.cleanup()
1478 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001479
1480 def device_role(self, device_id, onos_node, role="master"):
1481 '''
1482 Calls the device-role cli command.
1483 device_id must be the id of a device as seen in the onos devices command
1484 onos_node is the ip of one of the onos nodes in the cluster
1485 role must be either master, standby, or none
1486
Jon Hall983a1702014-10-28 18:44:22 -04001487 Returns main.TRUE or main.FALSE based argument varification.
1488 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001489 support that output
1490 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001491 try:
Jon Hall983a1702014-10-28 18:44:22 -04001492 #print "beginning device_role... \n\tdevice_id:" + device_id
1493 #print "\tonos_node: " + onos_node
1494 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001495 if role.lower() == "master" or \
1496 role.lower() == "standby" or \
1497 role.lower() == "none":
1498 self.handle.sendline("")
1499 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001500 self.handle.sendline("device-role " +
1501 str(device_id) + " " +
1502 str(onos_node) + " " +
1503 str(role))
1504 i= self.handle.expect(["Error","onos>"])
1505 if i == 0:
1506 output = str(self.handle.before)
1507 self.handle.expect("onos>")
1508 output = output + str(self.handle.before)
1509 main.log.error(self.name + ": " +
1510 output + '\033[0m')#end color output to escape any colours from the cli
1511 return main.ERROR
1512 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001513 self.handle.expect("onos>")
1514 return main.TRUE
1515 else:
1516 return main.FALSE
1517
1518 except pexpect.EOF:
1519 main.log.error(self.name + ": EOF exception found")
1520 main.log.error(self.name + ": " + self.handle.before)
1521 main.cleanup()
1522 main.exit()
1523 except:
1524 main.log.info(self.name+" ::::::")
1525 main.log.error( traceback.print_exc())
1526 main.log.info(self.name+" ::::::")
1527 main.cleanup()
1528 main.exit()
1529
1530
andrewonlab7e4d2d32014-10-15 13:23:21 -04001531 #***********************************