blob: da3777c67ae2d430bb82e4965c60015ab901126b [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:
andrewonlab9627f432014-11-14 12:45:10 -050074 main.log.info(self.name + ":::::::::::::::::::::::")
andrewonlab95ce8322014-10-13 14:12:04 -040075 main.log.error( traceback.print_exc() )
andrewonlab9627f432014-11-14 12:45:10 -050076 main.log.info(":::::::::::::::::::::::")
andrewonlab95ce8322014-10-13 14:12:04 -040077 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:
andrewonlab9627f432014-11-14 12:45:10 -0500111 self.handle.sendline("")
112 i = self.handle.expect([
113 "onos>",
114 "\$"], timeout=10)
115 if i == 0:
116 self.handle.sendline("logout")
117 self.handle.expect("\$")
118 elif i == 1:
119 return main.TRUE
120
andrewonlab38d2b4a2014-11-13 16:28:47 -0500121 except pexpect.EOF:
122 main.log.error(self.name + ": eof exception found")
andrewonlab9627f432014-11-14 12:45:10 -0500123 main.log.error(self.name + ": " +
124 self.handle.before)
andrewonlab38d2b4a2014-11-13 16:28:47 -0500125 main.cleanup()
126 main.exit()
127 except:
128 main.log.info(self.name+" ::::::")
129 main.log.error( traceback.print_exc())
130 main.log.info(self.name+" ::::::")
131 main.cleanup()
132 main.exit()
133
andrewonlab95ce8322014-10-13 14:12:04 -0400134 def set_cell(self, cellname):
135 '''
136 Calls 'cell <name>' to set the environment variables on ONOSbench
137
138 Before issuing any cli commands, set the environment variable first.
139 '''
140 try:
141 if not cellname:
142 main.log.error("Must define cellname")
143 main.cleanup()
144 main.exit()
145 else:
146 self.handle.sendline("cell "+str(cellname))
147 #Expect the cellname in the ONOS_CELL variable.
148 #Note that this variable name is subject to change
149 # and that this driver will have to change accordingly
150 self.handle.expect("ONOS_CELL="+str(cellname))
151 handle_before = self.handle.before
152 handle_after = self.handle.after
153 #Get the rest of the handle
154 self.handle.sendline("")
155 self.handle.expect("\$")
156 handle_more = self.handle.before
157
158 main.log.info("Cell call returned: "+handle_before+
159 handle_after + handle_more)
160
161 return main.TRUE
162
163 except pexpect.EOF:
andrewonlab38d2b4a2014-11-13 16:28:47 -0500164 main.log.error(self.name + ": eof exception found")
andrewonlab95ce8322014-10-13 14:12:04 -0400165 main.log.error(self.name + ": " + self.handle.before)
166 main.cleanup()
167 main.exit()
168 except:
169 main.log.info(self.name+" ::::::")
170 main.log.error( traceback.print_exc())
171 main.log.info(self.name+" ::::::")
172 main.cleanup()
173 main.exit()
174
andrewonlabc2d05aa2014-10-13 16:51:10 -0400175 def start_onos_cli(self, ONOS_ip):
andrewonlab95ce8322014-10-13 14:12:04 -0400176 try:
177 self.handle.sendline("")
andrewonlab48829f62014-11-17 13:49:01 -0500178 x = self.handle.expect([
179 "\$", "onos>"], timeout=10)
180
181 if x == 1:
182 main.log.info("ONOS cli is already running")
183 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400184
185 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400186 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400187 i = self.handle.expect([
188 "onos>",
189 pexpect.TIMEOUT],timeout=60)
190
191 if i == 0:
192 main.log.info(str(ONOS_ip)+" CLI Started successfully")
193 return main.TRUE
194 else:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400195 #If failed, send ctrl+c to process and try again
andrewonlabf47993a2014-10-24 17:56:01 -0400196 main.log.info("Starting CLI failed. Retrying...")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400197 self.handle.sendline("\x03")
198 self.handle.sendline("onos -w "+str(ONOS_ip))
199 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
200 timeout=30)
201 if i == 0:
andrewonlab28ca4b22014-11-20 13:15:59 -0500202 main.log.info(str(ONOS_ip)+" CLI Started "+
203 "successfully after retry attempt")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400204 return main.TRUE
205 else:
206 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400207 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400208 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400209
210 except pexpect.EOF:
211 main.log.error(self.name + ": EOF exception found")
212 main.log.error(self.name + ": " + self.handle.before)
213 main.cleanup()
214 main.exit()
215 except:
216 main.log.info(self.name+" ::::::")
217 main.log.error( traceback.print_exc())
218 main.log.info(self.name+" ::::::")
219 main.cleanup()
220 main.exit()
221
andrewonlaba18f6bf2014-10-13 19:31:54 -0400222 def sendline(self, cmd_str):
223 '''
224 Send a completely user specified string to
225 the onos> prompt. Use this function if you have
226 a very specific command to send.
227
228 Warning: There are no sanity checking to commands
229 sent using this method.
230 '''
231 try:
232 self.handle.sendline("")
233 self.handle.expect("onos>")
234
235 self.handle.sendline(cmd_str)
236 self.handle.expect("onos>")
237
238 handle = self.handle.before
239
240 self.handle.sendline("")
241 self.handle.expect("onos>")
242
243 handle += self.handle.before
244 handle += self.handle.after
245
246 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400247 ansi_escape = re.compile(r'\x1b[^m]*m')
248 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400249
250 return handle
251 except pexpect.EOF:
252 main.log.error(self.name + ": EOF exception found")
253 main.log.error(self.name + ": " + self.handle.before)
254 main.cleanup()
255 main.exit()
256 except:
257 main.log.info(self.name+" ::::::")
258 main.log.error( traceback.print_exc())
259 main.log.info(self.name+" ::::::")
260 main.cleanup()
261 main.exit()
262
andrewonlab95ce8322014-10-13 14:12:04 -0400263 #IMPORTANT NOTE:
264 #For all cli commands, naming convention should match
265 #the cli command replacing ':' with '_'.
266 #Ex) onos:topology > onos_topology
267 # onos:links > onos_links
268 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400269
270 def add_node(self, node_id, ONOS_ip, tcp_port=""):
271 '''
272 Adds a new cluster node by ID and address information.
273 Required:
274 * node_id
275 * ONOS_ip
276 Optional:
277 * tcp_port
278 '''
279 try:
280 self.handle.sendline("")
281 self.handle.expect("onos>")
282
283 self.handle.sendline("add-node "+
284 str(node_id)+" "+
285 str(ONOS_ip)+" "+
286 str(tcp_port))
287
288 i = self.handle.expect([
289 "Error",
290 "onos>" ])
291
292 #Clear handle to get previous output
293 self.handle.sendline("")
294 self.handle.expect("onos>")
295
296 handle = self.handle.before
297
298 if i == 0:
299 main.log.error("Error in adding node")
300 main.log.error(handle)
301 return main.FALSE
302 else:
303 main.log.info("Node "+str(ONOS_ip)+" added")
304 return main.TRUE
305
306 except pexpect.EOF:
307 main.log.error(self.name + ": EOF exception found")
308 main.log.error(self.name + ": " + self.handle.before)
309 main.cleanup()
310 main.exit()
311 except:
312 main.log.info(self.name+" ::::::")
313 main.log.error( traceback.print_exc())
314 main.log.info(self.name+" ::::::")
315 main.cleanup()
316 main.exit()
317
andrewonlab86dc3082014-10-13 18:18:38 -0400318 def remove_node(self, node_id):
319 '''
320 Removes a cluster by ID
321 Issues command: 'remove-node [<node-id>]'
322 Required:
323 * node_id
324 '''
325 try:
326 self.handle.sendline("")
327 self.handle.expect("onos>")
328
329 self.handle.sendline("remove-node "+str(node_id))
330 self.handle.expect("onos>")
331
332 return main.TRUE
333
334 except pexpect.EOF:
335 main.log.error(self.name + ": EOF exception found")
336 main.log.error(self.name + ": " + self.handle.before)
337 main.cleanup()
338 main.exit()
339 except:
340 main.log.info(self.name+" ::::::")
341 main.log.error( traceback.print_exc())
342 main.log.info(self.name+" ::::::")
343 main.cleanup()
344 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400345
andrewonlab7c211572014-10-15 16:45:20 -0400346 def nodes(self):
347 '''
348 List the nodes currently visible
349 Issues command: 'nodes'
350 Returns: entire handle of list of nodes
351 '''
352 try:
353 self.handle.sendline("")
354 self.handle.expect("onos>")
355
356 self.handle.sendline("nodes")
357 self.handle.expect("onos>")
358
359 self.handle.sendline("")
360 self.handle.expect("onos>")
361
362 handle = self.handle.before
363
364 return handle
365
366 except pexpect.EOF:
367 main.log.error(self.name + ": EOF exception found")
368 main.log.error(self.name + ": " + self.handle.before)
369 main.cleanup()
370 main.exit()
371 except:
372 main.log.info(self.name+" ::::::")
373 main.log.error( traceback.print_exc())
374 main.log.info(self.name+" ::::::")
375 main.cleanup()
376 main.exit()
377
andrewonlab38d6ae22014-10-15 14:23:45 -0400378 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400379 '''
380 Shows the current state of the topology
381 by issusing command: 'onos> onos:topology'
382 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400383 try:
384 self.handle.sendline("")
385 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400386 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400387 self.handle.sendline("onos:topology")
388 self.handle.expect("onos>")
389
390 handle = self.handle.before
391
392 main.log.info("onos:topology returned: " +
393 str(handle))
394
395 return handle
396
397 except pexpect.EOF:
398 main.log.error(self.name + ": EOF exception found")
399 main.log.error(self.name + ": " + self.handle.before)
400 main.cleanup()
401 main.exit()
402 except:
403 main.log.info(self.name+" ::::::")
404 main.log.error( traceback.print_exc())
405 main.log.info(self.name+" ::::::")
406 main.cleanup()
407 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400408
409 def feature_install(self, feature_str):
410 '''
411 Installs a specified feature
412 by issuing command: 'onos> feature:install <feature_str>'
413 '''
414 try:
415 self.handle.sendline("")
416 self.handle.expect("onos>")
417
418 self.handle.sendline("feature:install "+str(feature_str))
419 self.handle.expect("onos>")
420
421 return main.TRUE
422
423 except pexpect.EOF:
424 main.log.error(self.name + ": EOF exception found")
425 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500426 main.log.report("Failed to install feature")
427 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400428 main.cleanup()
429 main.exit()
430 except:
431 main.log.info(self.name+" ::::::")
432 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500433 main.log.report("Failed to install feature")
434 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400435 main.log.info(self.name+" ::::::")
436 main.cleanup()
437 main.exit()
438
439 def feature_uninstall(self, feature_str):
440 '''
441 Uninstalls a specified feature
442 by issuing command: 'onos> feature:uninstall <feature_str>'
443 '''
444 try:
445 self.handle.sendline("")
446 self.handle.expect("onos>")
447
448 self.handle.sendline("feature:uninstall "+str(feature_str))
449 self.handle.expect("onos>")
450
451 return main.TRUE
452
453 except pexpect.EOF:
454 main.log.error(self.name + ": EOF exception found")
455 main.log.error(self.name + ": " + self.handle.before)
456 main.cleanup()
457 main.exit()
458 except:
459 main.log.info(self.name+" ::::::")
460 main.log.error( traceback.print_exc())
461 main.log.info(self.name+" ::::::")
462 main.cleanup()
463 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400464
Jon Halle8217482014-10-17 13:49:14 -0400465 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400466 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400467 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400468 Optional argument:
469 * grep_str - pass in a string to grep
470 '''
471 try:
472 self.handle.sendline("")
473 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400474
475 if json_format:
476 if not grep_str:
477 self.handle.sendline("devices -j")
478 self.handle.expect("devices -j")
479 self.handle.expect("onos>")
480 else:
481 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400482 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400483 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
484 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400485 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400486 '''
487 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
488 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 -0400489 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 -0400490 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400491 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400492 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400493 '''
494 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400495 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
496 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400497 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400498 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400499 else:
500 if not grep_str:
501 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400502 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400503 else:
504 self.handle.sendline("devices | grep '"+
505 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400506 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400507 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400508 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400509 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400510 except pexpect.EOF:
511 main.log.error(self.name + ": EOF exception found")
512 main.log.error(self.name + ": " + self.handle.before)
513 main.cleanup()
514 main.exit()
515 except:
516 main.log.info(self.name+" ::::::")
517 main.log.error( traceback.print_exc())
518 main.log.info(self.name+" ::::::")
519 main.cleanup()
520 main.exit()
521
Jon Halle8217482014-10-17 13:49:14 -0400522 def links(self, json_format=True, grep_str=""):
523 '''
524 Lists all core links
525 Optional argument:
526 * grep_str - pass in a string to grep
527 '''
528 try:
529 self.handle.sendline("")
530 self.handle.expect("onos>")
531
532 if json_format:
533 if not grep_str:
534 self.handle.sendline("links -j")
535 self.handle.expect("links -j")
536 self.handle.expect("onos>")
537 else:
538 self.handle.sendline("links -j | grep '"+
539 str(grep_str)+"'")
540 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
541 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400542 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400543 '''
544 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
545 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 -0400546 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 -0400547 So we take off that escape sequence using
548 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
549 handle1 = ansi_escape.sub('', handle)
550 '''
551 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400552 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
553 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400554 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400555 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400556 else:
557 if not grep_str:
558 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400559 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400560 else:
561 self.handle.sendline("links | grep '"+
562 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400563 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400564 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400565 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400566 return handle
Jon Halle8217482014-10-17 13:49:14 -0400567 except pexpect.EOF:
568 main.log.error(self.name + ": EOF exception found")
569 main.log.error(self.name + ": " + self.handle.before)
570 main.cleanup()
571 main.exit()
572 except:
573 main.log.info(self.name+" ::::::")
574 main.log.error( traceback.print_exc())
575 main.log.info(self.name+" ::::::")
576 main.cleanup()
577 main.exit()
578
579
580 def ports(self, json_format=True, grep_str=""):
581 '''
582 Lists all ports
583 Optional argument:
584 * grep_str - pass in a string to grep
585 '''
586 try:
587 self.handle.sendline("")
588 self.handle.expect("onos>")
589
590 if json_format:
591 if not grep_str:
592 self.handle.sendline("ports -j")
593 self.handle.expect("ports -j")
594 self.handle.expect("onos>")
595 else:
596 self.handle.sendline("ports -j | grep '"+
597 str(grep_str)+"'")
598 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
599 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400600 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400601 '''
602 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
603 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 -0400604 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 -0400605 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400606 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
607 handle1 = ansi_escape.sub('', handle)
608 '''
609 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400610 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
611 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400612 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400613 return handle1
614
Jon Halle8217482014-10-17 13:49:14 -0400615 else:
616 if not grep_str:
617 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400618 self.handle.expect("onos>")
619 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400620 self.handle.expect("onos>")
621 else:
622 self.handle.sendline("ports | grep '"+
623 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400624 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400625 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400626 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400627 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400628 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400629 return handle
Jon Halle8217482014-10-17 13:49:14 -0400630 except pexpect.EOF:
631 main.log.error(self.name + ": EOF exception found")
632 main.log.error(self.name + ": " + self.handle.before)
633 main.cleanup()
634 main.exit()
635 except:
636 main.log.info(self.name+" ::::::")
637 main.log.error( traceback.print_exc())
638 main.log.info(self.name+" ::::::")
639 main.cleanup()
640 main.exit()
641
642
Jon Hall983a1702014-10-28 18:44:22 -0400643 def roles(self, json_format=True, grep_str=""):
andrewonlab7c211572014-10-15 16:45:20 -0400644 '''
Jon Hall983a1702014-10-28 18:44:22 -0400645 Lists all devices and the controllers with roles assigned to them
646 Optional argument:
647 * grep_str - pass in a string to grep
andrewonlab7c211572014-10-15 16:45:20 -0400648 '''
649 try:
650 self.handle.sendline("")
651 self.handle.expect("onos>")
andrewonlab7c211572014-10-15 16:45:20 -0400652
Jon Hall983a1702014-10-28 18:44:22 -0400653 if json_format:
654 if not grep_str:
655 self.handle.sendline("roles -j")
656 self.handle.expect("roles -j")
657 self.handle.expect("onos>")
658 else:
659 self.handle.sendline("roles -j | grep '"+
660 str(grep_str)+"'")
661 self.handle.expect("roles -j | grep '"+str(grep_str)+"'")
662 self.handle.expect("onos>")
663 handle = self.handle.before
664 '''
665 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
666 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
667 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
668 So we take off that escape sequence using the following commads:
669 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
670 handle1 = ansi_escape.sub('', handle)
671 '''
672 #print "repr(handle) =", repr(handle)
673 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
674 handle1 = ansi_escape.sub('', handle)
675 #print "repr(handle1) = ", repr(handle1)
676 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400677
andrewonlab7c211572014-10-15 16:45:20 -0400678 else:
Jon Hall983a1702014-10-28 18:44:22 -0400679 if not grep_str:
680 self.handle.sendline("roles")
681 self.handle.expect("onos>")
682 self.handle.sendline("")
683 self.handle.expect("onos>")
684 else:
685 self.handle.sendline("roles | grep '"+
686 str(grep_str)+"'")
687 self.handle.expect("onos>")
688 self.handle.sendline("")
689 self.handle.expect("onos>")
690 handle = self.handle.before
691 #print "handle =",handle
692 return handle
693 except pexpect.EOF:
694 main.log.error(self.name + ": EOF exception found")
695 main.log.error(self.name + ": " + self.handle.before)
696 main.cleanup()
697 main.exit()
698 except:
699 main.log.info(self.name+" ::::::")
700 main.log.error( traceback.print_exc())
701 main.log.info(self.name+" ::::::")
702 main.cleanup()
703 main.exit()
704
705 def get_role(self, device_id):
706 '''
707 Given the a string containing the json representation of the "roles" cli command and a
708 partial or whole device id, returns a json object containing the
709 roles output for the first device whose id contains "device_id"
710
711 Returns:
712 Dict of the role assignments for the given device or
713 None if not match
714 '''
715 try:
716 import json
717 if device_id == None:
718 return None
719 else:
720 raw_roles = self.roles()
721 roles_json = json.loads(raw_roles)
722 #search json for the device with id then return the device
723 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400724 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400725 if str(device_id) in device['id']:
726 return device
727 return None
andrewonlab7c211572014-10-15 16:45:20 -0400728
andrewonlab86dc3082014-10-13 18:18:38 -0400729 except pexpect.EOF:
730 main.log.error(self.name + ": EOF exception found")
731 main.log.error(self.name + ": " + self.handle.before)
732 main.cleanup()
733 main.exit()
734 except:
735 main.log.info(self.name+" ::::::")
736 main.log.error( traceback.print_exc())
737 main.log.info(self.name+" ::::::")
738 main.cleanup()
739 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400740
andrewonlab3e15ead2014-10-15 14:21:34 -0400741 def paths(self, src_id, dst_id):
742 '''
743 Returns string of paths, and the cost.
744 Issues command: onos:paths <src> <dst>
745 '''
746 try:
747 self.handle.sendline("")
748 self.handle.expect("onos>")
749
750 self.handle.sendline("onos:paths "+
751 str(src_id) + " " + str(dst_id))
752 i = self.handle.expect([
753 "Error",
754 "onos>"])
755
756 self.handle.sendline("")
757 self.handle.expect("onos>")
758
759 handle = self.handle.before
760
761 if i == 0:
762 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400763 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400764 else:
765 path = handle.split(";")[0]
766 cost = handle.split(";")[1]
767 return (path, cost)
768
769 except pexpect.EOF:
770 main.log.error(self.name + ": EOF exception found")
771 main.log.error(self.name + ": " + self.handle.before)
772 main.cleanup()
773 main.exit()
774 except:
775 main.log.info(self.name+" ::::::")
776 main.log.error( traceback.print_exc())
777 main.log.info(self.name+" ::::::")
778 main.cleanup()
779 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400780
Jon Hall42db6dc2014-10-24 19:03:48 -0400781 def hosts(self, json_format=True, grep_str=""):
782 '''
783 Lists all discovered hosts
784 Optional argument:
785 * grep_str - pass in a string to grep
786 '''
787 try:
788 self.handle.sendline("")
789 self.handle.expect("onos>")
790
791 if json_format:
792 if not grep_str:
793 self.handle.sendline("hosts -j")
794 self.handle.expect("hosts -j")
795 self.handle.expect("onos>")
796 else:
797 self.handle.sendline("hosts -j | grep '"+
798 str(grep_str)+"'")
799 self.handle.expect("hosts -j | grep '"+str(grep_str)+"'")
800 self.handle.expect("onos>")
801 handle = self.handle.before
802 '''
803 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
804 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
805 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
806 So we take off that escape sequence using
807 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
808 handle1 = ansi_escape.sub('', handle)
809 '''
810 #print "repr(handle) =", repr(handle)
811 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
812 handle1 = ansi_escape.sub('', handle)
813 #print "repr(handle1) = ", repr(handle1)
814 return handle1
815 else:
816 if not grep_str:
817 self.handle.sendline("hosts")
818 self.handle.expect("onos>")
819 else:
820 self.handle.sendline("hosts | grep '"+
821 str(grep_str)+"'")
822 self.handle.expect("onos>")
823 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400824 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400825 return handle
826 except pexpect.EOF:
827 main.log.error(self.name + ": EOF exception found")
828 main.log.error(self.name + ": " + self.handle.before)
829 main.cleanup()
830 main.exit()
831 except:
832 main.log.info(self.name+" ::::::")
833 main.log.error( traceback.print_exc())
834 main.log.info(self.name+" ::::::")
835 main.cleanup()
836 main.exit()
837
838 def get_host(self, mac):
839 '''
840 Return the first host from the hosts api whose 'id' contains 'mac'
841 Note: mac must be a colon seperated mac address, but could be a partial mac address
842 Return None if there is no match
843 '''
844 import json
845 try:
846 if mac == None:
847 return None
848 else:
849 mac = mac
850 raw_hosts = self.hosts()
851 hosts_json = json.loads(raw_hosts)
852 #search json for the host with mac then return the device
853 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400854 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400855 if mac in host['id']:
856 return host
857 return None
858 except pexpect.EOF:
859 main.log.error(self.name + ": EOF exception found")
860 main.log.error(self.name + ": " + self.handle.before)
861 main.cleanup()
862 main.exit()
863 except:
864 main.log.info(self.name+" ::::::")
865 main.log.error( traceback.print_exc())
866 main.log.info(self.name+" ::::::")
867 main.cleanup()
868 main.exit()
869
andrewonlab3f0a4af2014-10-17 12:25:14 -0400870
871 def get_hosts_id(self, host_list):
872 '''
873 Obtain list of hosts
874 Issues command: 'onos> hosts'
875
876 Required:
877 * host_list: List of hosts obtained by Mininet
878 IMPORTANT:
879 This function assumes that you started your
880 topology with the option '--mac'.
881 Furthermore, it assumes that value of VLAN is '-1'
882 Description:
883 Converts mininet hosts (h1, h2, h3...) into
884 ONOS format (00:00:00:00:00:01/-1 , ...)
885 '''
886
887 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400888 onos_host_list = []
889
890 for host in host_list:
891 host = host.replace("h", "")
892 host_hex = hex(int(host)).zfill(12)
893 host_hex = str(host_hex).replace('x','0')
894 i = iter(str(host_hex))
895 host_hex = ":".join(a+b for a,b in zip(i,i))
896 host_hex = host_hex + "/-1"
897 onos_host_list.append(host_hex)
898
899 return onos_host_list
900
901 except pexpect.EOF:
902 main.log.error(self.name + ": EOF exception found")
903 main.log.error(self.name + ": " + self.handle.before)
904 main.cleanup()
905 main.exit()
906 except:
907 main.log.info(self.name+" ::::::")
908 main.log.error( traceback.print_exc())
909 main.log.info(self.name+" ::::::")
910 main.cleanup()
911 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400912
andrewonlabe6745342014-10-17 14:29:13 -0400913 def add_host_intent(self, host_id_one, host_id_two):
914 '''
915 Required:
916 * host_id_one: ONOS host id for host1
917 * host_id_two: ONOS host id for host2
918 Description:
919 Adds a host-to-host intent (bidrectional) by
920 specifying the two hosts.
921 '''
922 try:
923 self.handle.sendline("")
924 self.handle.expect("onos>")
925
926 self.handle.sendline("add-host-intent "+
927 str(host_id_one) + " " + str(host_id_two))
928 self.handle.expect("onos>")
929
andrewonlabe6745342014-10-17 14:29:13 -0400930 handle = self.handle.before
Shreya Shah4f25fdf2014-10-29 19:55:35 -0400931 print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400932
933 main.log.info("Intent installed between "+
934 str(host_id_one) + " and " + str(host_id_two))
935
936 return handle
937
938 except pexpect.EOF:
939 main.log.error(self.name + ": EOF exception found")
940 main.log.error(self.name + ": " + self.handle.before)
941 main.cleanup()
942 main.exit()
943 except:
944 main.log.info(self.name+" ::::::")
945 main.log.error( traceback.print_exc())
946 main.log.info(self.name+" ::::::")
947 main.cleanup()
948 main.exit()
949
andrewonlab7b31d232014-10-24 13:31:47 -0400950 def add_optical_intent(self, ingress_device, egress_device):
951 '''
952 Required:
953 * ingress_device: device id of ingress device
954 * egress_device: device id of egress device
955 Optional:
956 TODO: Still needs to be implemented via dev side
957 '''
958 try:
959 self.handle.sendline("add-optical-intent "+
960 str(ingress_device) + " " + str(egress_device))
961 self.handle.expect("add-optical-intent")
962 i = self.handle.expect([
963 "Error",
964 "onos>"])
965
966 handle = self.handle.before
967
968 #If error, return error message
969 if i == 0:
970 return handle
971 else:
972 return main.TRUE
973
974 except pexpect.EOF:
975 main.log.error(self.name + ": EOF exception found")
976 main.log.error(self.name + ": " + self.handle.before)
977 main.cleanup()
978 main.exit()
979 except:
980 main.log.info(self.name+" ::::::")
981 main.log.error( traceback.print_exc())
982 main.log.info(self.name+" ::::::")
983 main.cleanup()
984 main.exit()
985
andrewonlab36af3822014-11-18 17:48:18 -0500986 def add_point_intent(self, ingress_device, egress_device,
987 port_ingress="", port_egress="", ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -0500988 ethDst="", bandwidth="", lambda_alloc=False,
989 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400990 '''
991 Required:
992 * ingress_device: device id of ingress device
993 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400994 Optional:
995 * ethType: specify ethType
996 * ethSrc: specify ethSrc (i.e. src mac addr)
997 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500998 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -0500999 * lambda_alloc: if True, intent will allocate lambda
1000 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -05001001 * ipProto: specify ip protocol
1002 * ipSrc: specify ip source address
1003 * ipDst: specify ip destination address
1004 * tcpSrc: specify tcp source port
1005 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001006 Description:
1007 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -04001008 specifying device id's and optional fields
1009
andrewonlab4dbb4d82014-10-17 18:22:31 -04001010 NOTE: This function may change depending on the
1011 options developers provide for point-to-point
1012 intent via cli
1013 '''
1014 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001015 cmd = ""
1016
1017 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001018 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001019 and not bandwidth and not lambda_alloc \
1020 and not ipProto and not ipSrc and not ipDst \
1021 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001022 cmd = "add-point-intent"
1023
1024
andrewonlab289e4b72014-10-21 21:24:18 -04001025 else:
andrewonlab36af3822014-11-18 17:48:18 -05001026 cmd = "add-point-intent"
andrewonlab9a130be2014-10-22 12:44:56 -04001027
andrewonlab0c0a6772014-10-22 12:31:18 -04001028 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001029 cmd += " --ethType " + str(ethType)
1030 if ethSrc:
1031 cmd += " --ethSrc " + str(ethSrc)
1032 if ethDst:
1033 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001034 if bandwidth:
1035 cmd += " --bandwidth " + str(bandwidth)
1036 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001037 cmd += " --lambda "
1038 if ipProto:
1039 cmd += " --ipProto " + str(ipProto)
1040 if ipSrc:
1041 cmd += " --ipSrc " + str(ipSrc)
1042 if ipDst:
1043 cmd += " --ipDst " + str(ipDst)
1044 if tcpSrc:
1045 cmd += " --tcpSrc " + str(tcpSrc)
1046 if tcpDst:
1047 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001048
andrewonlab36af3822014-11-18 17:48:18 -05001049 #Check whether the user appended the port
1050 #or provided it as an input
1051 if "/" in ingress_device:
1052 cmd += " "+str(ingress_device)
1053 else:
1054 if not port_ingress:
1055 main.log.error("You must specify "+
1056 "the ingress port")
1057 #TODO: perhaps more meaningful return
1058 return main.FALSE
1059
1060 cmd += " "+ \
1061 str(ingress_device) + "/" +\
1062 str(port_ingress) + " "
1063
1064 if "/" in egress_device:
1065 cmd += " "+str(egress_device)
1066 else:
1067 if not port_egress:
1068 main.log.error("You must specify "+
1069 "the egress port")
1070 return main.FALSE
1071
1072 cmd += " "+\
1073 str(egress_device) + "/" +\
1074 str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001075
andrewonlab289e4b72014-10-21 21:24:18 -04001076 self.handle.sendline(cmd)
andrewonlab36af3822014-11-18 17:48:18 -05001077
1078 main.log.info(cmd + " sent")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001079 i = self.handle.expect([
1080 "Error",
1081 "onos>"])
andrewonlab4dbb4d82014-10-17 18:22:31 -04001082
1083 if i == 0:
1084 main.log.error("Error in adding point-to-point intent")
1085 return handle
1086 else:
1087 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001088
andrewonlab4dbb4d82014-10-17 18:22:31 -04001089 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
andrewonlab9a50dfe2014-10-17 17:22:31 -04001101 def remove_intent(self, intent_id):
1102 '''
1103 Remove intent for specified intent id
1104 '''
1105 try:
1106 self.handle.sendline("")
1107 self.handle.expect("onos>")
1108
1109 self.handle.sendline("remove-intent "+str(intent_id))
1110 i = self.handle.expect([
1111 "Error",
1112 "onos>"])
1113
1114 handle = self.handle.before
1115
1116 if i == 0:
1117 main.log.error("Error in removing intent")
1118 return handle
1119 else:
1120 return handle
1121
1122 except pexpect.EOF:
1123 main.log.error(self.name + ": EOF exception found")
1124 main.log.error(self.name + ": " + self.handle.before)
1125 main.cleanup()
1126 main.exit()
1127 except:
1128 main.log.info(self.name+" ::::::")
1129 main.log.error( traceback.print_exc())
1130 main.log.info(self.name+" ::::::")
1131 main.cleanup()
1132 main.exit()
1133
pingping-lindabe7972014-11-17 19:29:44 -08001134 # This method should be used after installing application: onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001135 def routes(self, json_format=False):
1136 '''
1137 Optional:
1138 * json_format: enable output formatting in json
1139 Description:
1140 Obtain all routes in the system
1141 '''
1142 try:
1143 if json_format:
1144 self.handle.sendline("routes -j")
1145 self.handle.expect("routes -j")
1146 self.handle.expect("onos>")
1147 handle_tmp = self.handle.before
1148
1149 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1150 handle = ansi_escape.sub('', handle_tmp)
1151
1152 else:
1153 self.handle.sendline("")
1154 self.handle.expect("onos>")
1155
1156 self.handle.sendline("routes")
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
andrewonlab377693f2014-10-21 16:00:30 -04001174 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001175 '''
andrewonlab377693f2014-10-21 16:00:30 -04001176 Optional:
1177 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001178 Description:
1179 Obtain intents currently installed
1180 '''
1181 try:
andrewonlab377693f2014-10-21 16:00:30 -04001182 if json_format:
1183 self.handle.sendline("intents -j")
1184 self.handle.expect("intents -j")
1185 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001186 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001187
1188 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1189 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001190 else:
1191 self.handle.sendline("")
1192 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001193
andrewonlab377693f2014-10-21 16:00:30 -04001194 self.handle.sendline("intents")
1195 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001196 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001197
1198 return handle
1199
1200 except pexpect.EOF:
1201 main.log.error(self.name + ": EOF exception found")
1202 main.log.error(self.name + ": " + self.handle.before)
1203 main.cleanup()
1204 main.exit()
1205 except:
1206 main.log.info(self.name+" ::::::")
1207 main.log.error( traceback.print_exc())
1208 main.log.info(self.name+" ::::::")
1209 main.cleanup()
1210 main.exit()
1211
Shreya Shah0f01c812014-10-26 20:15:28 -04001212 def flows(self, json_format = False):
1213 '''
1214 Optional:
1215 * json_format: enable output formatting in json
1216 Description:
1217 Obtain flows currently installed
1218 '''
1219 try:
1220 if json_format:
1221 self.handle.sendline("flows -j")
1222 self.handle.expect("flows -j")
1223 self.handle.expect("onos>")
1224 handle = self.handle.before
1225
1226 else:
1227 self.handle.sendline("")
1228 self.handle.expect("onos>")
1229 self.handle.sendline("flows")
1230 self.handle.expect("onos>")
1231 handle = self.handle.before
1232
1233 return handle
1234
1235 except pexpect.EOF:
1236 main.log.error(self.name + ": EOF exception found")
1237 main.log.error(self.name + ": " + self.handle.before)
1238 main.cleanup()
1239 main.exit()
1240 except:
1241 main.log.info(self.name+" ::::::")
1242 main.log.error( traceback.print_exc())
1243 main.log.info(self.name+" ::::::")
1244 main.cleanup()
1245 main.exit()
1246
andrewonlab87852b02014-11-19 18:44:19 -05001247 def push_test_intents(self, dpid_src, dpid_dst, num_intents,
1248 report=True):
1249 '''
1250 Description:
1251 Push a number of intents in a batch format to
1252 a specific point-to-point intent definition
1253 Required:
1254 * dpid_src: specify source dpid
1255 * dpid_dst: specify destination dpid
1256 * num_intents: specify number of intents to push
1257 Optional:
1258 * report: default True, returns latency information
1259 '''
1260 try:
1261 cmd = "push-test-intents "+\
1262 str(dpid_src)+" "+str(dpid_dst)+" "+\
1263 str(num_intents)
1264 self.handle.sendline(cmd)
1265 self.handle.expect(cmd)
1266 self.handle.expect("onos>")
1267
1268 handle = self.handle.before
1269
1270 #Some color thing that we want to escape
1271 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1272 handle = ansi_escape.sub('', handle)
1273
1274 if report:
1275 main.log.info(handle)
1276 return handle
1277 else:
1278 return main.TRUE
1279
1280 except pexpect.EOF:
1281 main.log.error(self.name + ": EOF exception found")
1282 main.log.error(self.name + ": " + self.handle.before)
1283 main.cleanup()
1284 main.exit()
1285 except:
1286 main.log.info(self.name+" ::::::")
1287 main.log.error( traceback.print_exc())
1288 main.log.info(self.name+" ::::::")
1289 main.cleanup()
1290 main.exit()
1291
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001292 def intents_events_metrics(self, json_format=True):
1293 '''
1294 Description:Returns topology metrics
1295 Optional:
1296 * json_format: enable json formatting of output
1297 '''
1298 try:
1299 if json_format:
1300 self.handle.sendline("intents-events-metrics -j")
1301 self.handle.expect("intents-events-metrics -j")
1302 self.handle.expect("onos>")
1303
1304 handle = self.handle.before
1305
1306 #Some color thing that we want to escape
1307 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1308 handle = ansi_escape.sub('', handle)
1309
1310 else:
1311 self.handle.sendline("intents-events-metrics")
1312 self.handle.expect("intents-events-metrics")
1313 self.handle.expect("onos>")
1314
1315 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001316
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001317 return handle
1318
1319 except pexpect.EOF:
1320 main.log.error(self.name + ": EOF exception found")
1321 main.log.error(self.name + ": " + self.handle.before)
1322 main.cleanup()
1323 main.exit()
1324 except:
1325 main.log.info(self.name+" ::::::")
1326 main.log.error( traceback.print_exc())
1327 main.log.info(self.name+" ::::::")
1328 main.cleanup()
1329 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001330
andrewonlab867212a2014-10-22 20:13:38 -04001331 def topology_events_metrics(self, json_format=True):
1332 '''
1333 Description:Returns topology metrics
1334 Optional:
1335 * json_format: enable json formatting of output
1336 '''
1337 try:
1338 if json_format:
1339 self.handle.sendline("topology-events-metrics -j")
1340 self.handle.expect("topology-events-metrics -j")
1341 self.handle.expect("onos>")
1342
1343 handle = self.handle.before
1344
1345 #Some color thing that we want to escape
1346 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1347 handle = ansi_escape.sub('', handle)
1348
1349 else:
1350 self.handle.sendline("topology-events-metrics")
1351 self.handle.expect("topology-events-metrics")
1352 self.handle.expect("onos>")
1353
1354 handle = self.handle.before
1355
1356 return handle
1357
1358 except pexpect.EOF:
1359 main.log.error(self.name + ": EOF exception found")
1360 main.log.error(self.name + ": " + self.handle.before)
1361 main.cleanup()
1362 main.exit()
1363 except:
1364 main.log.info(self.name+" ::::::")
1365 main.log.error( traceback.print_exc())
1366 main.log.info(self.name+" ::::::")
1367 main.cleanup()
1368 main.exit()
1369
andrewonlab3e15ead2014-10-15 14:21:34 -04001370 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001371 #Wrapper functions use existing driver
1372 #functions and extends their use case.
1373 #For example, we may use the output of
1374 #a normal driver function, and parse it
1375 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001376
andrewonlab9a50dfe2014-10-17 17:22:31 -04001377 def get_all_intents_id(self):
1378 '''
1379 Description:
1380 Obtain all intent id's in a list
1381 '''
1382 try:
1383 #Obtain output of intents function
1384 intents_str = self.intents()
1385 all_intent_list = []
1386 intent_id_list = []
1387
1388 #Parse the intents output for ID's
1389 intents_list = [s.strip() for s in intents_str.splitlines()]
1390 for intents in intents_list:
1391 if "onos>" in intents:
1392 continue
1393 elif "intents" in intents:
1394 continue
1395 else:
1396 line_list = intents.split(" ")
1397 all_intent_list.append(line_list[0])
1398
1399 all_intent_list = all_intent_list[1:-2]
1400
1401 for intents in all_intent_list:
1402 if not intents:
1403 continue
1404 else:
1405 intent_id_list.append(intents)
1406
1407 return intent_id_list
1408
1409 except pexpect.EOF:
1410 main.log.error(self.name + ": EOF exception found")
1411 main.log.error(self.name + ": " + self.handle.before)
1412 main.cleanup()
1413 main.exit()
1414 except:
1415 main.log.info(self.name+" ::::::")
1416 main.log.error( traceback.print_exc())
1417 main.log.info(self.name+" ::::::")
1418 main.cleanup()
1419 main.exit()
1420
andrewonlab7e4d2d32014-10-15 13:23:21 -04001421 def get_all_devices_id(self):
1422 '''
1423 Use 'devices' function to obtain list of all devices
1424 and parse the result to obtain a list of all device
1425 id's. Returns this list. Returns empty list if no
1426 devices exist
1427 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001428
1429 This function may be useful if you are not sure of the
1430 device id, and wish to execute other commands using
1431 the ids. By obtaining the list of device ids on the fly,
1432 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001433 '''
1434 try:
1435 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001436 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001437 id_list = []
1438
1439 if not devices_str:
1440 main.log.info("There are no devices to get id from")
1441 return id_list
1442
1443 #Split the string into list by comma
1444 device_list = devices_str.split(",")
1445 #Get temporary list of all arguments with string 'id='
1446 temp_list = [dev for dev in device_list if "id=" in dev]
1447 #Split list further into arguments before and after string
1448 # 'id='. Get the latter portion (the actual device id) and
1449 # append to id_list
1450 for arg in temp_list:
1451 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001452 return id_list
1453
1454 except pexpect.EOF:
1455 main.log.error(self.name + ": EOF exception found")
1456 main.log.error(self.name + ": " + self.handle.before)
1457 main.cleanup()
1458 main.exit()
1459 except:
1460 main.log.info(self.name+" ::::::")
1461 main.log.error( traceback.print_exc())
1462 main.log.info(self.name+" ::::::")
1463 main.cleanup()
1464 main.exit()
1465
andrewonlab7c211572014-10-15 16:45:20 -04001466 def get_all_nodes_id(self):
1467 '''
1468 Uses 'nodes' function to obtain list of all nodes
1469 and parse the result of nodes to obtain just the
1470 node id's.
1471 Returns:
1472 list of node id's
1473 '''
1474 try:
1475 nodes_str = self.nodes()
1476 id_list = []
1477
1478 if not nodes_str:
1479 main.log.info("There are no nodes to get id from")
1480 return id_list
1481
1482 #Sample nodes_str output
1483 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1484
1485 #Split the string into list by comma
1486 nodes_list = nodes_str.split(",")
1487 temp_list = [node for node in nodes_list if "id=" in node]
1488 for arg in temp_list:
1489 id_list.append(arg.split("id=")[1])
1490
1491 return id_list
1492
1493 except pexpect.EOF:
1494 main.log.error(self.name + ": EOF exception found")
1495 main.log.error(self.name + ": " + self.handle.before)
1496 main.cleanup()
1497 main.exit()
1498 except:
1499 main.log.info(self.name+" ::::::")
1500 main.log.error( traceback.print_exc())
1501 main.log.info(self.name+" ::::::")
1502 main.cleanup()
1503 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001504
Jon Halla91c4dc2014-10-22 12:57:04 -04001505 def get_device(self, dpid=None):
1506 '''
1507 Return the first device from the devices api whose 'id' contains 'dpid'
1508 Return None if there is no match
1509 '''
1510 import json
1511 try:
1512 if dpid == None:
1513 return None
1514 else:
1515 dpid = dpid.replace(':', '')
1516 raw_devices = self.devices()
1517 devices_json = json.loads(raw_devices)
1518 #search json for the device with dpid then return the device
1519 for device in devices_json:
1520 #print "%s in %s?" % (dpid, device['id'])
1521 if dpid in device['id']:
1522 return device
1523 return None
1524 except pexpect.EOF:
1525 main.log.error(self.name + ": EOF exception found")
1526 main.log.error(self.name + ": " + self.handle.before)
1527 main.cleanup()
1528 main.exit()
1529 except:
1530 main.log.info(self.name+" ::::::")
1531 main.log.error( traceback.print_exc())
1532 main.log.info(self.name+" ::::::")
1533 main.cleanup()
1534 main.exit()
1535
Jon Hall42db6dc2014-10-24 19:03:48 -04001536 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1537 '''
1538 Checks the number of swithes & links that ONOS sees against the
1539 supplied values. By default this will report to main.log, but the
1540 log level can be specifid.
1541
1542 Params: ip = ip used for the onos cli
1543 numoswitch = expected number of switches
1544 numlink = expected number of links
1545 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1546
1547
1548 log_level can
1549
1550 Returns: main.TRUE if the number of switchs and links are correct,
1551 main.FALSE if the numer of switches and links is incorrect,
1552 and main.ERROR otherwise
1553 '''
1554
1555 try:
1556 topology = self.get_topology(ip)
1557 if topology == {}:
1558 return main.ERROR
1559 output = ""
1560 #Is the number of switches is what we expected
1561 devices = topology.get('devices',False)
1562 links = topology.get('links',False)
1563 if devices == False or links == False:
1564 return main.ERROR
1565 switch_check = ( int(devices) == int(numoswitch) )
1566 #Is the number of links is what we expected
1567 link_check = ( int(links) == int(numolink) )
1568 if (switch_check and link_check):
1569 #We expected the correct numbers
1570 output = output + "The number of links and switches match "\
1571 + "what was expected"
1572 result = main.TRUE
1573 else:
1574 output = output + \
1575 "The number of links and switches does not match what was expected"
1576 result = main.FALSE
1577 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1578 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1579 if log_level == "report":
1580 main.log.report(output)
1581 elif log_level == "warn":
1582 main.log.warn(output)
1583 else:
1584 main.log.info(output)
1585 return result
1586 except pexpect.EOF:
1587 main.log.error(self.name + ": EOF exception found")
1588 main.log.error(self.name + ": " + self.handle.before)
1589 main.cleanup()
1590 main.exit()
1591 except:
1592 main.log.info(self.name+" ::::::")
1593 main.log.error( traceback.print_exc())
1594 main.log.info(self.name+" ::::::")
1595 main.cleanup()
1596 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001597
1598 def device_role(self, device_id, onos_node, role="master"):
1599 '''
1600 Calls the device-role cli command.
1601 device_id must be the id of a device as seen in the onos devices command
1602 onos_node is the ip of one of the onos nodes in the cluster
1603 role must be either master, standby, or none
1604
Jon Hall983a1702014-10-28 18:44:22 -04001605 Returns main.TRUE or main.FALSE based argument varification.
1606 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001607 support that output
1608 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001609 try:
Jon Hall983a1702014-10-28 18:44:22 -04001610 #print "beginning device_role... \n\tdevice_id:" + device_id
1611 #print "\tonos_node: " + onos_node
1612 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001613 if role.lower() == "master" or \
1614 role.lower() == "standby" or \
1615 role.lower() == "none":
1616 self.handle.sendline("")
1617 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001618 self.handle.sendline("device-role " +
1619 str(device_id) + " " +
1620 str(onos_node) + " " +
1621 str(role))
1622 i= self.handle.expect(["Error","onos>"])
1623 if i == 0:
1624 output = str(self.handle.before)
1625 self.handle.expect("onos>")
1626 output = output + str(self.handle.before)
1627 main.log.error(self.name + ": " +
1628 output + '\033[0m')#end color output to escape any colours from the cli
1629 return main.ERROR
1630 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001631 self.handle.expect("onos>")
1632 return main.TRUE
1633 else:
1634 return main.FALSE
1635
1636 except pexpect.EOF:
1637 main.log.error(self.name + ": EOF exception found")
1638 main.log.error(self.name + ": " + self.handle.before)
1639 main.cleanup()
1640 main.exit()
1641 except:
1642 main.log.info(self.name+" ::::::")
1643 main.log.error( traceback.print_exc())
1644 main.log.info(self.name+" ::::::")
1645 main.cleanup()
1646 main.exit()
1647
1648
andrewonlab7e4d2d32014-10-15 13:23:21 -04001649 #***********************************