blob: feca7177330b2c4602107c6622191f5f6a30898c [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")
andrewonlab367b5132014-11-20 14:20:16 -0500198 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
199 timeout=30)
200 #Send ctrl+d to exit the onos> prompt that was
201 #not successful
202 self.handle.sendline("\x04")
203 self.handle.expect("\$")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400204 self.handle.sendline("onos -w "+str(ONOS_ip))
205 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
206 timeout=30)
207 if i == 0:
andrewonlab28ca4b22014-11-20 13:15:59 -0500208 main.log.info(str(ONOS_ip)+" CLI Started "+
209 "successfully after retry attempt")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400210 return main.TRUE
211 else:
212 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400213 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400214 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400215
216 except pexpect.EOF:
217 main.log.error(self.name + ": EOF exception found")
218 main.log.error(self.name + ": " + self.handle.before)
219 main.cleanup()
220 main.exit()
221 except:
222 main.log.info(self.name+" ::::::")
223 main.log.error( traceback.print_exc())
224 main.log.info(self.name+" ::::::")
225 main.cleanup()
226 main.exit()
227
andrewonlaba18f6bf2014-10-13 19:31:54 -0400228 def sendline(self, cmd_str):
229 '''
230 Send a completely user specified string to
231 the onos> prompt. Use this function if you have
232 a very specific command to send.
233
234 Warning: There are no sanity checking to commands
235 sent using this method.
236 '''
237 try:
238 self.handle.sendline("")
239 self.handle.expect("onos>")
240
241 self.handle.sendline(cmd_str)
242 self.handle.expect("onos>")
243
244 handle = self.handle.before
245
246 self.handle.sendline("")
247 self.handle.expect("onos>")
248
249 handle += self.handle.before
250 handle += self.handle.after
251
252 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400253 ansi_escape = re.compile(r'\x1b[^m]*m')
254 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400255
256 return handle
257 except pexpect.EOF:
258 main.log.error(self.name + ": EOF exception found")
259 main.log.error(self.name + ": " + self.handle.before)
260 main.cleanup()
261 main.exit()
262 except:
263 main.log.info(self.name+" ::::::")
264 main.log.error( traceback.print_exc())
265 main.log.info(self.name+" ::::::")
266 main.cleanup()
267 main.exit()
268
andrewonlab95ce8322014-10-13 14:12:04 -0400269 #IMPORTANT NOTE:
270 #For all cli commands, naming convention should match
271 #the cli command replacing ':' with '_'.
272 #Ex) onos:topology > onos_topology
273 # onos:links > onos_links
274 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400275
276 def add_node(self, node_id, ONOS_ip, tcp_port=""):
277 '''
278 Adds a new cluster node by ID and address information.
279 Required:
280 * node_id
281 * ONOS_ip
282 Optional:
283 * tcp_port
284 '''
285 try:
286 self.handle.sendline("")
287 self.handle.expect("onos>")
288
289 self.handle.sendline("add-node "+
290 str(node_id)+" "+
291 str(ONOS_ip)+" "+
292 str(tcp_port))
293
294 i = self.handle.expect([
295 "Error",
296 "onos>" ])
297
298 #Clear handle to get previous output
299 self.handle.sendline("")
300 self.handle.expect("onos>")
301
302 handle = self.handle.before
303
304 if i == 0:
305 main.log.error("Error in adding node")
306 main.log.error(handle)
307 return main.FALSE
308 else:
309 main.log.info("Node "+str(ONOS_ip)+" added")
310 return main.TRUE
311
312 except pexpect.EOF:
313 main.log.error(self.name + ": EOF exception found")
314 main.log.error(self.name + ": " + self.handle.before)
315 main.cleanup()
316 main.exit()
317 except:
318 main.log.info(self.name+" ::::::")
319 main.log.error( traceback.print_exc())
320 main.log.info(self.name+" ::::::")
321 main.cleanup()
322 main.exit()
323
andrewonlab86dc3082014-10-13 18:18:38 -0400324 def remove_node(self, node_id):
325 '''
326 Removes a cluster by ID
327 Issues command: 'remove-node [<node-id>]'
328 Required:
329 * node_id
330 '''
331 try:
332 self.handle.sendline("")
333 self.handle.expect("onos>")
334
335 self.handle.sendline("remove-node "+str(node_id))
336 self.handle.expect("onos>")
337
338 return main.TRUE
339
340 except pexpect.EOF:
341 main.log.error(self.name + ": EOF exception found")
342 main.log.error(self.name + ": " + self.handle.before)
343 main.cleanup()
344 main.exit()
345 except:
346 main.log.info(self.name+" ::::::")
347 main.log.error( traceback.print_exc())
348 main.log.info(self.name+" ::::::")
349 main.cleanup()
350 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400351
andrewonlab7c211572014-10-15 16:45:20 -0400352 def nodes(self):
353 '''
354 List the nodes currently visible
355 Issues command: 'nodes'
356 Returns: entire handle of list of nodes
357 '''
358 try:
359 self.handle.sendline("")
360 self.handle.expect("onos>")
361
362 self.handle.sendline("nodes")
363 self.handle.expect("onos>")
364
365 self.handle.sendline("")
366 self.handle.expect("onos>")
367
368 handle = self.handle.before
369
370 return handle
371
372 except pexpect.EOF:
373 main.log.error(self.name + ": EOF exception found")
374 main.log.error(self.name + ": " + self.handle.before)
375 main.cleanup()
376 main.exit()
377 except:
378 main.log.info(self.name+" ::::::")
379 main.log.error( traceback.print_exc())
380 main.log.info(self.name+" ::::::")
381 main.cleanup()
382 main.exit()
383
andrewonlab38d6ae22014-10-15 14:23:45 -0400384 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400385 '''
386 Shows the current state of the topology
387 by issusing command: 'onos> onos:topology'
388 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400389 try:
390 self.handle.sendline("")
391 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400392 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400393 self.handle.sendline("onos:topology")
394 self.handle.expect("onos>")
395
396 handle = self.handle.before
397
398 main.log.info("onos:topology returned: " +
399 str(handle))
400
401 return handle
402
403 except pexpect.EOF:
404 main.log.error(self.name + ": EOF exception found")
405 main.log.error(self.name + ": " + self.handle.before)
406 main.cleanup()
407 main.exit()
408 except:
409 main.log.info(self.name+" ::::::")
410 main.log.error( traceback.print_exc())
411 main.log.info(self.name+" ::::::")
412 main.cleanup()
413 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400414
415 def feature_install(self, feature_str):
416 '''
417 Installs a specified feature
418 by issuing command: 'onos> feature:install <feature_str>'
419 '''
420 try:
421 self.handle.sendline("")
422 self.handle.expect("onos>")
423
424 self.handle.sendline("feature:install "+str(feature_str))
425 self.handle.expect("onos>")
426
427 return main.TRUE
428
429 except pexpect.EOF:
430 main.log.error(self.name + ": EOF exception found")
431 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500432 main.log.report("Failed to install feature")
433 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400434 main.cleanup()
435 main.exit()
436 except:
437 main.log.info(self.name+" ::::::")
438 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500439 main.log.report("Failed to install feature")
440 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400441 main.log.info(self.name+" ::::::")
442 main.cleanup()
443 main.exit()
444
445 def feature_uninstall(self, feature_str):
446 '''
447 Uninstalls a specified feature
448 by issuing command: 'onos> feature:uninstall <feature_str>'
449 '''
450 try:
451 self.handle.sendline("")
452 self.handle.expect("onos>")
453
454 self.handle.sendline("feature:uninstall "+str(feature_str))
455 self.handle.expect("onos>")
456
457 return main.TRUE
458
459 except pexpect.EOF:
460 main.log.error(self.name + ": EOF exception found")
461 main.log.error(self.name + ": " + self.handle.before)
462 main.cleanup()
463 main.exit()
464 except:
465 main.log.info(self.name+" ::::::")
466 main.log.error( traceback.print_exc())
467 main.log.info(self.name+" ::::::")
468 main.cleanup()
469 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400470
Jon Halle8217482014-10-17 13:49:14 -0400471 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400472 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400473 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400474 Optional argument:
475 * grep_str - pass in a string to grep
476 '''
477 try:
478 self.handle.sendline("")
479 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400480
481 if json_format:
482 if not grep_str:
483 self.handle.sendline("devices -j")
484 self.handle.expect("devices -j")
485 self.handle.expect("onos>")
486 else:
487 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400488 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400489 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
490 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400491 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400492 '''
493 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
494 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 -0400495 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 -0400496 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400497 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400498 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400499 '''
500 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400501 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
502 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400503 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400504 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400505 else:
506 if not grep_str:
507 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400508 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400509 else:
510 self.handle.sendline("devices | grep '"+
511 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400512 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400513 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400514 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400515 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400516 except pexpect.EOF:
517 main.log.error(self.name + ": EOF exception found")
518 main.log.error(self.name + ": " + self.handle.before)
519 main.cleanup()
520 main.exit()
521 except:
522 main.log.info(self.name+" ::::::")
523 main.log.error( traceback.print_exc())
524 main.log.info(self.name+" ::::::")
525 main.cleanup()
526 main.exit()
527
Jon Halle8217482014-10-17 13:49:14 -0400528 def links(self, json_format=True, grep_str=""):
529 '''
530 Lists all core links
531 Optional argument:
532 * grep_str - pass in a string to grep
533 '''
534 try:
535 self.handle.sendline("")
536 self.handle.expect("onos>")
537
538 if json_format:
539 if not grep_str:
540 self.handle.sendline("links -j")
541 self.handle.expect("links -j")
542 self.handle.expect("onos>")
543 else:
544 self.handle.sendline("links -j | grep '"+
545 str(grep_str)+"'")
546 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
547 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400548 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400549 '''
550 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
551 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 -0400552 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 -0400553 So we take off that escape sequence using
554 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
555 handle1 = ansi_escape.sub('', handle)
556 '''
557 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400558 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
559 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400560 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400561 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400562 else:
563 if not grep_str:
564 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400565 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400566 else:
567 self.handle.sendline("links | grep '"+
568 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400569 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400570 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400571 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400572 return handle
Jon Halle8217482014-10-17 13:49:14 -0400573 except pexpect.EOF:
574 main.log.error(self.name + ": EOF exception found")
575 main.log.error(self.name + ": " + self.handle.before)
576 main.cleanup()
577 main.exit()
578 except:
579 main.log.info(self.name+" ::::::")
580 main.log.error( traceback.print_exc())
581 main.log.info(self.name+" ::::::")
582 main.cleanup()
583 main.exit()
584
585
586 def ports(self, json_format=True, grep_str=""):
587 '''
588 Lists all ports
589 Optional argument:
590 * grep_str - pass in a string to grep
591 '''
592 try:
593 self.handle.sendline("")
594 self.handle.expect("onos>")
595
596 if json_format:
597 if not grep_str:
598 self.handle.sendline("ports -j")
599 self.handle.expect("ports -j")
600 self.handle.expect("onos>")
601 else:
602 self.handle.sendline("ports -j | grep '"+
603 str(grep_str)+"'")
604 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
605 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400606 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400607 '''
608 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
609 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 -0400610 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 -0400611 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400612 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
613 handle1 = ansi_escape.sub('', handle)
614 '''
615 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400616 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
617 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400618 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400619 return handle1
620
Jon Halle8217482014-10-17 13:49:14 -0400621 else:
622 if not grep_str:
623 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400624 self.handle.expect("onos>")
625 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400626 self.handle.expect("onos>")
627 else:
628 self.handle.sendline("ports | grep '"+
629 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400630 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400631 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400632 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400633 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400634 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400635 return handle
Jon Halle8217482014-10-17 13:49:14 -0400636 except pexpect.EOF:
637 main.log.error(self.name + ": EOF exception found")
638 main.log.error(self.name + ": " + self.handle.before)
639 main.cleanup()
640 main.exit()
641 except:
642 main.log.info(self.name+" ::::::")
643 main.log.error( traceback.print_exc())
644 main.log.info(self.name+" ::::::")
645 main.cleanup()
646 main.exit()
647
648
Jon Hall983a1702014-10-28 18:44:22 -0400649 def roles(self, json_format=True, grep_str=""):
andrewonlab7c211572014-10-15 16:45:20 -0400650 '''
Jon Hall983a1702014-10-28 18:44:22 -0400651 Lists all devices and the controllers with roles assigned to them
652 Optional argument:
653 * grep_str - pass in a string to grep
andrewonlab7c211572014-10-15 16:45:20 -0400654 '''
655 try:
656 self.handle.sendline("")
657 self.handle.expect("onos>")
andrewonlab7c211572014-10-15 16:45:20 -0400658
Jon Hall983a1702014-10-28 18:44:22 -0400659 if json_format:
660 if not grep_str:
661 self.handle.sendline("roles -j")
662 self.handle.expect("roles -j")
663 self.handle.expect("onos>")
664 else:
665 self.handle.sendline("roles -j | grep '"+
666 str(grep_str)+"'")
667 self.handle.expect("roles -j | grep '"+str(grep_str)+"'")
668 self.handle.expect("onos>")
669 handle = self.handle.before
670 '''
671 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
672 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
673 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
674 So we take off that escape sequence using the following commads:
675 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
676 handle1 = ansi_escape.sub('', handle)
677 '''
678 #print "repr(handle) =", repr(handle)
679 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
680 handle1 = ansi_escape.sub('', handle)
681 #print "repr(handle1) = ", repr(handle1)
682 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400683
andrewonlab7c211572014-10-15 16:45:20 -0400684 else:
Jon Hall983a1702014-10-28 18:44:22 -0400685 if not grep_str:
686 self.handle.sendline("roles")
687 self.handle.expect("onos>")
688 self.handle.sendline("")
689 self.handle.expect("onos>")
690 else:
691 self.handle.sendline("roles | grep '"+
692 str(grep_str)+"'")
693 self.handle.expect("onos>")
694 self.handle.sendline("")
695 self.handle.expect("onos>")
696 handle = self.handle.before
697 #print "handle =",handle
698 return handle
699 except pexpect.EOF:
700 main.log.error(self.name + ": EOF exception found")
701 main.log.error(self.name + ": " + self.handle.before)
702 main.cleanup()
703 main.exit()
704 except:
705 main.log.info(self.name+" ::::::")
706 main.log.error( traceback.print_exc())
707 main.log.info(self.name+" ::::::")
708 main.cleanup()
709 main.exit()
710
711 def get_role(self, device_id):
712 '''
713 Given the a string containing the json representation of the "roles" cli command and a
714 partial or whole device id, returns a json object containing the
715 roles output for the first device whose id contains "device_id"
716
717 Returns:
718 Dict of the role assignments for the given device or
719 None if not match
720 '''
721 try:
722 import json
723 if device_id == None:
724 return None
725 else:
726 raw_roles = self.roles()
727 roles_json = json.loads(raw_roles)
728 #search json for the device with id then return the device
729 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400730 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400731 if str(device_id) in device['id']:
732 return device
733 return None
andrewonlab7c211572014-10-15 16:45:20 -0400734
andrewonlab86dc3082014-10-13 18:18:38 -0400735 except pexpect.EOF:
736 main.log.error(self.name + ": EOF exception found")
737 main.log.error(self.name + ": " + self.handle.before)
738 main.cleanup()
739 main.exit()
740 except:
741 main.log.info(self.name+" ::::::")
742 main.log.error( traceback.print_exc())
743 main.log.info(self.name+" ::::::")
744 main.cleanup()
745 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400746
andrewonlab3e15ead2014-10-15 14:21:34 -0400747 def paths(self, src_id, dst_id):
748 '''
749 Returns string of paths, and the cost.
750 Issues command: onos:paths <src> <dst>
751 '''
752 try:
753 self.handle.sendline("")
754 self.handle.expect("onos>")
755
756 self.handle.sendline("onos:paths "+
757 str(src_id) + " " + str(dst_id))
758 i = self.handle.expect([
759 "Error",
760 "onos>"])
761
762 self.handle.sendline("")
763 self.handle.expect("onos>")
764
765 handle = self.handle.before
766
767 if i == 0:
768 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400769 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400770 else:
771 path = handle.split(";")[0]
772 cost = handle.split(";")[1]
773 return (path, cost)
774
775 except pexpect.EOF:
776 main.log.error(self.name + ": EOF exception found")
777 main.log.error(self.name + ": " + self.handle.before)
778 main.cleanup()
779 main.exit()
780 except:
781 main.log.info(self.name+" ::::::")
782 main.log.error( traceback.print_exc())
783 main.log.info(self.name+" ::::::")
784 main.cleanup()
785 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400786
Jon Hall42db6dc2014-10-24 19:03:48 -0400787 def hosts(self, json_format=True, grep_str=""):
788 '''
789 Lists all discovered hosts
790 Optional argument:
791 * grep_str - pass in a string to grep
792 '''
793 try:
794 self.handle.sendline("")
795 self.handle.expect("onos>")
796
797 if json_format:
798 if not grep_str:
799 self.handle.sendline("hosts -j")
800 self.handle.expect("hosts -j")
801 self.handle.expect("onos>")
802 else:
803 self.handle.sendline("hosts -j | grep '"+
804 str(grep_str)+"'")
805 self.handle.expect("hosts -j | grep '"+str(grep_str)+"'")
806 self.handle.expect("onos>")
807 handle = self.handle.before
808 '''
809 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
810 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
811 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
812 So we take off that escape sequence using
813 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
814 handle1 = ansi_escape.sub('', handle)
815 '''
816 #print "repr(handle) =", repr(handle)
817 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
818 handle1 = ansi_escape.sub('', handle)
819 #print "repr(handle1) = ", repr(handle1)
820 return handle1
821 else:
822 if not grep_str:
823 self.handle.sendline("hosts")
824 self.handle.expect("onos>")
825 else:
826 self.handle.sendline("hosts | grep '"+
827 str(grep_str)+"'")
828 self.handle.expect("onos>")
829 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400830 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400831 return handle
832 except pexpect.EOF:
833 main.log.error(self.name + ": EOF exception found")
834 main.log.error(self.name + ": " + self.handle.before)
835 main.cleanup()
836 main.exit()
837 except:
838 main.log.info(self.name+" ::::::")
839 main.log.error( traceback.print_exc())
840 main.log.info(self.name+" ::::::")
841 main.cleanup()
842 main.exit()
843
844 def get_host(self, mac):
845 '''
846 Return the first host from the hosts api whose 'id' contains 'mac'
847 Note: mac must be a colon seperated mac address, but could be a partial mac address
848 Return None if there is no match
849 '''
850 import json
851 try:
852 if mac == None:
853 return None
854 else:
855 mac = mac
856 raw_hosts = self.hosts()
857 hosts_json = json.loads(raw_hosts)
858 #search json for the host with mac then return the device
859 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400860 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400861 if mac in host['id']:
862 return host
863 return None
864 except pexpect.EOF:
865 main.log.error(self.name + ": EOF exception found")
866 main.log.error(self.name + ": " + self.handle.before)
867 main.cleanup()
868 main.exit()
869 except:
870 main.log.info(self.name+" ::::::")
871 main.log.error( traceback.print_exc())
872 main.log.info(self.name+" ::::::")
873 main.cleanup()
874 main.exit()
875
andrewonlab3f0a4af2014-10-17 12:25:14 -0400876
877 def get_hosts_id(self, host_list):
878 '''
879 Obtain list of hosts
880 Issues command: 'onos> hosts'
881
882 Required:
883 * host_list: List of hosts obtained by Mininet
884 IMPORTANT:
885 This function assumes that you started your
886 topology with the option '--mac'.
887 Furthermore, it assumes that value of VLAN is '-1'
888 Description:
889 Converts mininet hosts (h1, h2, h3...) into
890 ONOS format (00:00:00:00:00:01/-1 , ...)
891 '''
892
893 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400894 onos_host_list = []
895
896 for host in host_list:
897 host = host.replace("h", "")
898 host_hex = hex(int(host)).zfill(12)
899 host_hex = str(host_hex).replace('x','0')
900 i = iter(str(host_hex))
901 host_hex = ":".join(a+b for a,b in zip(i,i))
902 host_hex = host_hex + "/-1"
903 onos_host_list.append(host_hex)
904
905 return onos_host_list
906
907 except pexpect.EOF:
908 main.log.error(self.name + ": EOF exception found")
909 main.log.error(self.name + ": " + self.handle.before)
910 main.cleanup()
911 main.exit()
912 except:
913 main.log.info(self.name+" ::::::")
914 main.log.error( traceback.print_exc())
915 main.log.info(self.name+" ::::::")
916 main.cleanup()
917 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400918
andrewonlabe6745342014-10-17 14:29:13 -0400919 def add_host_intent(self, host_id_one, host_id_two):
920 '''
921 Required:
922 * host_id_one: ONOS host id for host1
923 * host_id_two: ONOS host id for host2
924 Description:
925 Adds a host-to-host intent (bidrectional) by
926 specifying the two hosts.
927 '''
928 try:
929 self.handle.sendline("")
930 self.handle.expect("onos>")
931
932 self.handle.sendline("add-host-intent "+
933 str(host_id_one) + " " + str(host_id_two))
934 self.handle.expect("onos>")
935
andrewonlabe6745342014-10-17 14:29:13 -0400936 handle = self.handle.before
Shreya Shah4f25fdf2014-10-29 19:55:35 -0400937 print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400938
939 main.log.info("Intent installed between "+
940 str(host_id_one) + " and " + str(host_id_two))
941
942 return handle
943
944 except pexpect.EOF:
945 main.log.error(self.name + ": EOF exception found")
946 main.log.error(self.name + ": " + self.handle.before)
947 main.cleanup()
948 main.exit()
949 except:
950 main.log.info(self.name+" ::::::")
951 main.log.error( traceback.print_exc())
952 main.log.info(self.name+" ::::::")
953 main.cleanup()
954 main.exit()
955
andrewonlab7b31d232014-10-24 13:31:47 -0400956 def add_optical_intent(self, ingress_device, egress_device):
957 '''
958 Required:
959 * ingress_device: device id of ingress device
960 * egress_device: device id of egress device
961 Optional:
962 TODO: Still needs to be implemented via dev side
963 '''
964 try:
965 self.handle.sendline("add-optical-intent "+
966 str(ingress_device) + " " + str(egress_device))
967 self.handle.expect("add-optical-intent")
968 i = self.handle.expect([
969 "Error",
970 "onos>"])
971
972 handle = self.handle.before
973
974 #If error, return error message
975 if i == 0:
976 return handle
977 else:
978 return main.TRUE
979
980 except pexpect.EOF:
981 main.log.error(self.name + ": EOF exception found")
982 main.log.error(self.name + ": " + self.handle.before)
983 main.cleanup()
984 main.exit()
985 except:
986 main.log.info(self.name+" ::::::")
987 main.log.error( traceback.print_exc())
988 main.log.info(self.name+" ::::::")
989 main.cleanup()
990 main.exit()
991
andrewonlab36af3822014-11-18 17:48:18 -0500992 def add_point_intent(self, ingress_device, egress_device,
993 port_ingress="", port_egress="", ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -0500994 ethDst="", bandwidth="", lambda_alloc=False,
995 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400996 '''
997 Required:
998 * ingress_device: device id of ingress device
999 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001000 Optional:
1001 * ethType: specify ethType
1002 * ethSrc: specify ethSrc (i.e. src mac addr)
1003 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001004 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -05001005 * lambda_alloc: if True, intent will allocate lambda
1006 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -05001007 * ipProto: specify ip protocol
1008 * ipSrc: specify ip source address
1009 * ipDst: specify ip destination address
1010 * tcpSrc: specify tcp source port
1011 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001012 Description:
1013 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -04001014 specifying device id's and optional fields
1015
andrewonlab4dbb4d82014-10-17 18:22:31 -04001016 NOTE: This function may change depending on the
1017 options developers provide for point-to-point
1018 intent via cli
1019 '''
1020 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001021 cmd = ""
1022
1023 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001024 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001025 and not bandwidth and not lambda_alloc \
1026 and not ipProto and not ipSrc and not ipDst \
1027 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001028 cmd = "add-point-intent"
1029
1030
andrewonlab289e4b72014-10-21 21:24:18 -04001031 else:
andrewonlab36af3822014-11-18 17:48:18 -05001032 cmd = "add-point-intent"
andrewonlab9a130be2014-10-22 12:44:56 -04001033
andrewonlab0c0a6772014-10-22 12:31:18 -04001034 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001035 cmd += " --ethType " + str(ethType)
1036 if ethSrc:
1037 cmd += " --ethSrc " + str(ethSrc)
1038 if ethDst:
1039 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001040 if bandwidth:
1041 cmd += " --bandwidth " + str(bandwidth)
1042 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001043 cmd += " --lambda "
1044 if ipProto:
1045 cmd += " --ipProto " + str(ipProto)
1046 if ipSrc:
1047 cmd += " --ipSrc " + str(ipSrc)
1048 if ipDst:
1049 cmd += " --ipDst " + str(ipDst)
1050 if tcpSrc:
1051 cmd += " --tcpSrc " + str(tcpSrc)
1052 if tcpDst:
1053 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001054
andrewonlab36af3822014-11-18 17:48:18 -05001055 #Check whether the user appended the port
1056 #or provided it as an input
1057 if "/" in ingress_device:
1058 cmd += " "+str(ingress_device)
1059 else:
1060 if not port_ingress:
1061 main.log.error("You must specify "+
1062 "the ingress port")
1063 #TODO: perhaps more meaningful return
1064 return main.FALSE
1065
1066 cmd += " "+ \
1067 str(ingress_device) + "/" +\
1068 str(port_ingress) + " "
1069
1070 if "/" in egress_device:
1071 cmd += " "+str(egress_device)
1072 else:
1073 if not port_egress:
1074 main.log.error("You must specify "+
1075 "the egress port")
1076 return main.FALSE
1077
1078 cmd += " "+\
1079 str(egress_device) + "/" +\
1080 str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001081
andrewonlab289e4b72014-10-21 21:24:18 -04001082 self.handle.sendline(cmd)
andrewonlab36af3822014-11-18 17:48:18 -05001083
1084 main.log.info(cmd + " sent")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001085 i = self.handle.expect([
1086 "Error",
1087 "onos>"])
andrewonlab4dbb4d82014-10-17 18:22:31 -04001088
1089 if i == 0:
1090 main.log.error("Error in adding point-to-point intent")
1091 return handle
1092 else:
1093 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001094
andrewonlab4dbb4d82014-10-17 18:22:31 -04001095 except pexpect.EOF:
1096 main.log.error(self.name + ": EOF exception found")
1097 main.log.error(self.name + ": " + self.handle.before)
1098 main.cleanup()
1099 main.exit()
1100 except:
1101 main.log.info(self.name+" ::::::")
1102 main.log.error( traceback.print_exc())
1103 main.log.info(self.name+" ::::::")
1104 main.cleanup()
1105 main.exit()
1106
andrewonlab9a50dfe2014-10-17 17:22:31 -04001107 def remove_intent(self, intent_id):
1108 '''
1109 Remove intent for specified intent id
1110 '''
1111 try:
1112 self.handle.sendline("")
1113 self.handle.expect("onos>")
1114
1115 self.handle.sendline("remove-intent "+str(intent_id))
1116 i = self.handle.expect([
1117 "Error",
1118 "onos>"])
1119
1120 handle = self.handle.before
1121
1122 if i == 0:
1123 main.log.error("Error in removing intent")
1124 return handle
1125 else:
1126 return handle
1127
1128 except pexpect.EOF:
1129 main.log.error(self.name + ": EOF exception found")
1130 main.log.error(self.name + ": " + self.handle.before)
1131 main.cleanup()
1132 main.exit()
1133 except:
1134 main.log.info(self.name+" ::::::")
1135 main.log.error( traceback.print_exc())
1136 main.log.info(self.name+" ::::::")
1137 main.cleanup()
1138 main.exit()
1139
pingping-lindabe7972014-11-17 19:29:44 -08001140 # This method should be used after installing application: onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001141 def routes(self, json_format=False):
1142 '''
1143 Optional:
1144 * json_format: enable output formatting in json
1145 Description:
1146 Obtain all routes in the system
1147 '''
1148 try:
1149 if json_format:
1150 self.handle.sendline("routes -j")
1151 self.handle.expect("routes -j")
1152 self.handle.expect("onos>")
1153 handle_tmp = self.handle.before
1154
1155 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1156 handle = ansi_escape.sub('', handle_tmp)
1157
1158 else:
1159 self.handle.sendline("")
1160 self.handle.expect("onos>")
1161
1162 self.handle.sendline("routes")
1163 self.handle.expect("onos>")
1164 handle = self.handle.before
1165
1166 return handle
1167
1168 except pexpect.EOF:
1169 main.log.error(self.name + ": EOF exception found")
1170 main.log.error(self.name + ": " + self.handle.before)
1171 main.cleanup()
1172 main.exit()
1173 except:
1174 main.log.info(self.name + " ::::::")
1175 main.log.error(traceback.print_exc())
1176 main.log.info(self.name + " ::::::")
1177 main.cleanup()
1178 main.exit()
1179
andrewonlab377693f2014-10-21 16:00:30 -04001180 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001181 '''
andrewonlab377693f2014-10-21 16:00:30 -04001182 Optional:
1183 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001184 Description:
1185 Obtain intents currently installed
1186 '''
1187 try:
andrewonlab377693f2014-10-21 16:00:30 -04001188 if json_format:
1189 self.handle.sendline("intents -j")
1190 self.handle.expect("intents -j")
1191 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001192 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001193
1194 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1195 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001196 else:
1197 self.handle.sendline("")
1198 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001199
andrewonlab377693f2014-10-21 16:00:30 -04001200 self.handle.sendline("intents")
1201 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001202 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001203
1204 return handle
1205
1206 except pexpect.EOF:
1207 main.log.error(self.name + ": EOF exception found")
1208 main.log.error(self.name + ": " + self.handle.before)
1209 main.cleanup()
1210 main.exit()
1211 except:
1212 main.log.info(self.name+" ::::::")
1213 main.log.error( traceback.print_exc())
1214 main.log.info(self.name+" ::::::")
1215 main.cleanup()
1216 main.exit()
1217
Shreya Shah0f01c812014-10-26 20:15:28 -04001218 def flows(self, json_format = False):
1219 '''
1220 Optional:
1221 * json_format: enable output formatting in json
1222 Description:
1223 Obtain flows currently installed
1224 '''
1225 try:
1226 if json_format:
1227 self.handle.sendline("flows -j")
1228 self.handle.expect("flows -j")
1229 self.handle.expect("onos>")
1230 handle = self.handle.before
1231
1232 else:
1233 self.handle.sendline("")
1234 self.handle.expect("onos>")
1235 self.handle.sendline("flows")
1236 self.handle.expect("onos>")
1237 handle = self.handle.before
1238
1239 return handle
1240
1241 except pexpect.EOF:
1242 main.log.error(self.name + ": EOF exception found")
1243 main.log.error(self.name + ": " + self.handle.before)
1244 main.cleanup()
1245 main.exit()
1246 except:
1247 main.log.info(self.name+" ::::::")
1248 main.log.error( traceback.print_exc())
1249 main.log.info(self.name+" ::::::")
1250 main.cleanup()
1251 main.exit()
1252
andrewonlab87852b02014-11-19 18:44:19 -05001253 def push_test_intents(self, dpid_src, dpid_dst, num_intents,
1254 report=True):
1255 '''
1256 Description:
1257 Push a number of intents in a batch format to
1258 a specific point-to-point intent definition
1259 Required:
1260 * dpid_src: specify source dpid
1261 * dpid_dst: specify destination dpid
1262 * num_intents: specify number of intents to push
1263 Optional:
1264 * report: default True, returns latency information
1265 '''
1266 try:
1267 cmd = "push-test-intents "+\
1268 str(dpid_src)+" "+str(dpid_dst)+" "+\
1269 str(num_intents)
1270 self.handle.sendline(cmd)
1271 self.handle.expect(cmd)
1272 self.handle.expect("onos>")
1273
1274 handle = self.handle.before
1275
1276 #Some color thing that we want to escape
1277 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1278 handle = ansi_escape.sub('', handle)
1279
1280 if report:
1281 main.log.info(handle)
1282 return handle
1283 else:
1284 return main.TRUE
1285
1286 except pexpect.EOF:
1287 main.log.error(self.name + ": EOF exception found")
1288 main.log.error(self.name + ": " + self.handle.before)
1289 main.cleanup()
1290 main.exit()
1291 except:
1292 main.log.info(self.name+" ::::::")
1293 main.log.error( traceback.print_exc())
1294 main.log.info(self.name+" ::::::")
1295 main.cleanup()
1296 main.exit()
1297
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001298 def intents_events_metrics(self, json_format=True):
1299 '''
1300 Description:Returns topology metrics
1301 Optional:
1302 * json_format: enable json formatting of output
1303 '''
1304 try:
1305 if json_format:
1306 self.handle.sendline("intents-events-metrics -j")
1307 self.handle.expect("intents-events-metrics -j")
1308 self.handle.expect("onos>")
1309
1310 handle = self.handle.before
1311
1312 #Some color thing that we want to escape
1313 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1314 handle = ansi_escape.sub('', handle)
1315
1316 else:
1317 self.handle.sendline("intents-events-metrics")
1318 self.handle.expect("intents-events-metrics")
1319 self.handle.expect("onos>")
1320
1321 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001322
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001323 return handle
1324
1325 except pexpect.EOF:
1326 main.log.error(self.name + ": EOF exception found")
1327 main.log.error(self.name + ": " + self.handle.before)
1328 main.cleanup()
1329 main.exit()
1330 except:
1331 main.log.info(self.name+" ::::::")
1332 main.log.error( traceback.print_exc())
1333 main.log.info(self.name+" ::::::")
1334 main.cleanup()
1335 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001336
andrewonlab867212a2014-10-22 20:13:38 -04001337 def topology_events_metrics(self, json_format=True):
1338 '''
1339 Description:Returns topology metrics
1340 Optional:
1341 * json_format: enable json formatting of output
1342 '''
1343 try:
1344 if json_format:
1345 self.handle.sendline("topology-events-metrics -j")
1346 self.handle.expect("topology-events-metrics -j")
1347 self.handle.expect("onos>")
1348
1349 handle = self.handle.before
1350
1351 #Some color thing that we want to escape
1352 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1353 handle = ansi_escape.sub('', handle)
1354
1355 else:
1356 self.handle.sendline("topology-events-metrics")
1357 self.handle.expect("topology-events-metrics")
1358 self.handle.expect("onos>")
1359
1360 handle = self.handle.before
1361
1362 return handle
1363
1364 except pexpect.EOF:
1365 main.log.error(self.name + ": EOF exception found")
1366 main.log.error(self.name + ": " + self.handle.before)
1367 main.cleanup()
1368 main.exit()
1369 except:
1370 main.log.info(self.name+" ::::::")
1371 main.log.error( traceback.print_exc())
1372 main.log.info(self.name+" ::::::")
1373 main.cleanup()
1374 main.exit()
1375
andrewonlab3e15ead2014-10-15 14:21:34 -04001376 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001377 #Wrapper functions use existing driver
1378 #functions and extends their use case.
1379 #For example, we may use the output of
1380 #a normal driver function, and parse it
1381 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001382
andrewonlab9a50dfe2014-10-17 17:22:31 -04001383 def get_all_intents_id(self):
1384 '''
1385 Description:
1386 Obtain all intent id's in a list
1387 '''
1388 try:
1389 #Obtain output of intents function
1390 intents_str = self.intents()
1391 all_intent_list = []
1392 intent_id_list = []
1393
1394 #Parse the intents output for ID's
1395 intents_list = [s.strip() for s in intents_str.splitlines()]
1396 for intents in intents_list:
1397 if "onos>" in intents:
1398 continue
1399 elif "intents" in intents:
1400 continue
1401 else:
1402 line_list = intents.split(" ")
1403 all_intent_list.append(line_list[0])
1404
1405 all_intent_list = all_intent_list[1:-2]
1406
1407 for intents in all_intent_list:
1408 if not intents:
1409 continue
1410 else:
1411 intent_id_list.append(intents)
1412
1413 return intent_id_list
1414
1415 except pexpect.EOF:
1416 main.log.error(self.name + ": EOF exception found")
1417 main.log.error(self.name + ": " + self.handle.before)
1418 main.cleanup()
1419 main.exit()
1420 except:
1421 main.log.info(self.name+" ::::::")
1422 main.log.error( traceback.print_exc())
1423 main.log.info(self.name+" ::::::")
1424 main.cleanup()
1425 main.exit()
1426
andrewonlab7e4d2d32014-10-15 13:23:21 -04001427 def get_all_devices_id(self):
1428 '''
1429 Use 'devices' function to obtain list of all devices
1430 and parse the result to obtain a list of all device
1431 id's. Returns this list. Returns empty list if no
1432 devices exist
1433 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001434
1435 This function may be useful if you are not sure of the
1436 device id, and wish to execute other commands using
1437 the ids. By obtaining the list of device ids on the fly,
1438 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001439 '''
1440 try:
1441 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001442 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001443 id_list = []
1444
1445 if not devices_str:
1446 main.log.info("There are no devices to get id from")
1447 return id_list
1448
1449 #Split the string into list by comma
1450 device_list = devices_str.split(",")
1451 #Get temporary list of all arguments with string 'id='
1452 temp_list = [dev for dev in device_list if "id=" in dev]
1453 #Split list further into arguments before and after string
1454 # 'id='. Get the latter portion (the actual device id) and
1455 # append to id_list
1456 for arg in temp_list:
1457 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001458 return id_list
1459
1460 except pexpect.EOF:
1461 main.log.error(self.name + ": EOF exception found")
1462 main.log.error(self.name + ": " + self.handle.before)
1463 main.cleanup()
1464 main.exit()
1465 except:
1466 main.log.info(self.name+" ::::::")
1467 main.log.error( traceback.print_exc())
1468 main.log.info(self.name+" ::::::")
1469 main.cleanup()
1470 main.exit()
1471
andrewonlab7c211572014-10-15 16:45:20 -04001472 def get_all_nodes_id(self):
1473 '''
1474 Uses 'nodes' function to obtain list of all nodes
1475 and parse the result of nodes to obtain just the
1476 node id's.
1477 Returns:
1478 list of node id's
1479 '''
1480 try:
1481 nodes_str = self.nodes()
1482 id_list = []
1483
1484 if not nodes_str:
1485 main.log.info("There are no nodes to get id from")
1486 return id_list
1487
1488 #Sample nodes_str output
1489 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1490
1491 #Split the string into list by comma
1492 nodes_list = nodes_str.split(",")
1493 temp_list = [node for node in nodes_list if "id=" in node]
1494 for arg in temp_list:
1495 id_list.append(arg.split("id=")[1])
1496
1497 return id_list
1498
1499 except pexpect.EOF:
1500 main.log.error(self.name + ": EOF exception found")
1501 main.log.error(self.name + ": " + self.handle.before)
1502 main.cleanup()
1503 main.exit()
1504 except:
1505 main.log.info(self.name+" ::::::")
1506 main.log.error( traceback.print_exc())
1507 main.log.info(self.name+" ::::::")
1508 main.cleanup()
1509 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001510
Jon Halla91c4dc2014-10-22 12:57:04 -04001511 def get_device(self, dpid=None):
1512 '''
1513 Return the first device from the devices api whose 'id' contains 'dpid'
1514 Return None if there is no match
1515 '''
1516 import json
1517 try:
1518 if dpid == None:
1519 return None
1520 else:
1521 dpid = dpid.replace(':', '')
1522 raw_devices = self.devices()
1523 devices_json = json.loads(raw_devices)
1524 #search json for the device with dpid then return the device
1525 for device in devices_json:
1526 #print "%s in %s?" % (dpid, device['id'])
1527 if dpid in device['id']:
1528 return device
1529 return None
1530 except pexpect.EOF:
1531 main.log.error(self.name + ": EOF exception found")
1532 main.log.error(self.name + ": " + self.handle.before)
1533 main.cleanup()
1534 main.exit()
1535 except:
1536 main.log.info(self.name+" ::::::")
1537 main.log.error( traceback.print_exc())
1538 main.log.info(self.name+" ::::::")
1539 main.cleanup()
1540 main.exit()
1541
Jon Hall42db6dc2014-10-24 19:03:48 -04001542 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1543 '''
1544 Checks the number of swithes & links that ONOS sees against the
1545 supplied values. By default this will report to main.log, but the
1546 log level can be specifid.
1547
1548 Params: ip = ip used for the onos cli
1549 numoswitch = expected number of switches
1550 numlink = expected number of links
1551 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1552
1553
1554 log_level can
1555
1556 Returns: main.TRUE if the number of switchs and links are correct,
1557 main.FALSE if the numer of switches and links is incorrect,
1558 and main.ERROR otherwise
1559 '''
1560
1561 try:
1562 topology = self.get_topology(ip)
1563 if topology == {}:
1564 return main.ERROR
1565 output = ""
1566 #Is the number of switches is what we expected
1567 devices = topology.get('devices',False)
1568 links = topology.get('links',False)
1569 if devices == False or links == False:
1570 return main.ERROR
1571 switch_check = ( int(devices) == int(numoswitch) )
1572 #Is the number of links is what we expected
1573 link_check = ( int(links) == int(numolink) )
1574 if (switch_check and link_check):
1575 #We expected the correct numbers
1576 output = output + "The number of links and switches match "\
1577 + "what was expected"
1578 result = main.TRUE
1579 else:
1580 output = output + \
1581 "The number of links and switches does not match what was expected"
1582 result = main.FALSE
1583 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1584 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1585 if log_level == "report":
1586 main.log.report(output)
1587 elif log_level == "warn":
1588 main.log.warn(output)
1589 else:
1590 main.log.info(output)
1591 return result
1592 except pexpect.EOF:
1593 main.log.error(self.name + ": EOF exception found")
1594 main.log.error(self.name + ": " + self.handle.before)
1595 main.cleanup()
1596 main.exit()
1597 except:
1598 main.log.info(self.name+" ::::::")
1599 main.log.error( traceback.print_exc())
1600 main.log.info(self.name+" ::::::")
1601 main.cleanup()
1602 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001603
1604 def device_role(self, device_id, onos_node, role="master"):
1605 '''
1606 Calls the device-role cli command.
1607 device_id must be the id of a device as seen in the onos devices command
1608 onos_node is the ip of one of the onos nodes in the cluster
1609 role must be either master, standby, or none
1610
Jon Hall983a1702014-10-28 18:44:22 -04001611 Returns main.TRUE or main.FALSE based argument varification.
1612 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001613 support that output
1614 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001615 try:
Jon Hall983a1702014-10-28 18:44:22 -04001616 #print "beginning device_role... \n\tdevice_id:" + device_id
1617 #print "\tonos_node: " + onos_node
1618 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001619 if role.lower() == "master" or \
1620 role.lower() == "standby" or \
1621 role.lower() == "none":
1622 self.handle.sendline("")
1623 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001624 self.handle.sendline("device-role " +
1625 str(device_id) + " " +
1626 str(onos_node) + " " +
1627 str(role))
1628 i= self.handle.expect(["Error","onos>"])
1629 if i == 0:
1630 output = str(self.handle.before)
1631 self.handle.expect("onos>")
1632 output = output + str(self.handle.before)
1633 main.log.error(self.name + ": " +
1634 output + '\033[0m')#end color output to escape any colours from the cli
1635 return main.ERROR
1636 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001637 self.handle.expect("onos>")
1638 return main.TRUE
1639 else:
1640 return main.FALSE
1641
1642 except pexpect.EOF:
1643 main.log.error(self.name + ": EOF exception found")
1644 main.log.error(self.name + ": " + self.handle.before)
1645 main.cleanup()
1646 main.exit()
1647 except:
1648 main.log.info(self.name+" ::::::")
1649 main.log.error( traceback.print_exc())
1650 main.log.info(self.name+" ::::::")
1651 main.cleanup()
1652 main.exit()
1653
1654
andrewonlab7e4d2d32014-10-15 13:23:21 -04001655 #***********************************