blob: 3a336194e153cce1cde663e0efb8e96d907e919e [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
andrewonlabb66dfa12014-12-02 15:51:10 -0500471 def devices(self, json_format=True, grep_str="",node_ip=""):
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:
andrewonlabb66dfa12014-12-02 15:51:10 -0500475 * grep_str : pass in a string to grep
476 * node_ip : used to reattempt CLI connection
andrewonlab86dc3082014-10-13 18:18:38 -0400477 '''
478 try:
479 self.handle.sendline("")
480 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400481
482 if json_format:
483 if not grep_str:
484 self.handle.sendline("devices -j")
485 self.handle.expect("devices -j")
andrewonlabb66dfa12014-12-02 15:51:10 -0500486 i = self.handle.expect([
487 "onos>", "\$", pexpect.TIMEOUT])
488 if (i == 1 or i == 2) and node_ip:
489 self.handle.sendline("onos -w "+node_ip)
490 self.handle.expect("onos>")
491 self.handle.sendline("devices -j")
492 self.handle.expect("devices -j")
493 elif not node_ip and i == 2:
494 main.log.info("ONOS CLI exited. Please "+
495 "provide node_ip in the function to "+
496 "attempt to reconnect")
Jon Halle8217482014-10-17 13:49:14 -0400497 else:
498 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400499 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400500 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
andrewonlabb66dfa12014-12-02 15:51:10 -0500501 i = self.handle.expect([
502 "onos>","\$", pexpect.TIMEOUT])
503
504 if (i == 1 or i == 2) and node_ip:
505 main.log.info("CLI dropped. Logging back into"+
506 " ONOS")
507 self.handle.sendline("onos -w "+node_ip)
508 self.handle.expect("onos>")
509 self.handle.sendline("devices -j")
510 self.handle.expect("devices -j")
511 elif not node_ip and i == 2:
512 main.log.info("ONOS CLI exited. Please "+
513 "provide node_ip in the function to "+
514 "attempt to reconnect")
515
Jon Halla001c392014-10-17 18:50:59 -0400516 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400517 '''
518 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
519 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 -0400520 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 -0400521 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400522 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400523 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400524 '''
525 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400526 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
527 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400528 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400529 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400530 else:
531 if not grep_str:
532 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400533 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400534 else:
535 self.handle.sendline("devices | grep '"+
536 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400537 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400538 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400539 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400540 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400541 except pexpect.EOF:
542 main.log.error(self.name + ": EOF exception found")
543 main.log.error(self.name + ": " + self.handle.before)
544 main.cleanup()
545 main.exit()
546 except:
547 main.log.info(self.name+" ::::::")
548 main.log.error( traceback.print_exc())
549 main.log.info(self.name+" ::::::")
550 main.cleanup()
551 main.exit()
552
Jon Halle8217482014-10-17 13:49:14 -0400553 def links(self, json_format=True, grep_str=""):
554 '''
555 Lists all core links
556 Optional argument:
557 * grep_str - pass in a string to grep
558 '''
559 try:
560 self.handle.sendline("")
561 self.handle.expect("onos>")
562
563 if json_format:
564 if not grep_str:
565 self.handle.sendline("links -j")
566 self.handle.expect("links -j")
567 self.handle.expect("onos>")
568 else:
569 self.handle.sendline("links -j | grep '"+
570 str(grep_str)+"'")
571 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
572 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400573 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400574 '''
575 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
576 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 -0400577 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 -0400578 So we take off that escape sequence using
579 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
580 handle1 = ansi_escape.sub('', handle)
581 '''
582 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400583 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
584 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400585 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400586 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400587 else:
588 if not grep_str:
589 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400590 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400591 else:
592 self.handle.sendline("links | grep '"+
593 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400594 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400595 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400596 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400597 return handle
Jon Halle8217482014-10-17 13:49:14 -0400598 except pexpect.EOF:
599 main.log.error(self.name + ": EOF exception found")
600 main.log.error(self.name + ": " + self.handle.before)
601 main.cleanup()
602 main.exit()
603 except:
604 main.log.info(self.name+" ::::::")
605 main.log.error( traceback.print_exc())
606 main.log.info(self.name+" ::::::")
607 main.cleanup()
608 main.exit()
609
610
611 def ports(self, json_format=True, grep_str=""):
612 '''
613 Lists all ports
614 Optional argument:
615 * grep_str - pass in a string to grep
616 '''
617 try:
618 self.handle.sendline("")
619 self.handle.expect("onos>")
620
621 if json_format:
622 if not grep_str:
623 self.handle.sendline("ports -j")
624 self.handle.expect("ports -j")
625 self.handle.expect("onos>")
626 else:
627 self.handle.sendline("ports -j | grep '"+
628 str(grep_str)+"'")
629 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
630 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400631 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400632 '''
633 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
634 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 -0400635 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 -0400636 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400637 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
638 handle1 = ansi_escape.sub('', handle)
639 '''
640 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400641 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
642 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400643 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400644 return handle1
645
Jon Halle8217482014-10-17 13:49:14 -0400646 else:
647 if not grep_str:
648 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400649 self.handle.expect("onos>")
650 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400651 self.handle.expect("onos>")
652 else:
653 self.handle.sendline("ports | grep '"+
654 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400655 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400656 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400657 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400658 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400659 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400660 return handle
Jon Halle8217482014-10-17 13:49:14 -0400661 except pexpect.EOF:
662 main.log.error(self.name + ": EOF exception found")
663 main.log.error(self.name + ": " + self.handle.before)
664 main.cleanup()
665 main.exit()
666 except:
667 main.log.info(self.name+" ::::::")
668 main.log.error( traceback.print_exc())
669 main.log.info(self.name+" ::::::")
670 main.cleanup()
671 main.exit()
672
673
Jon Hall983a1702014-10-28 18:44:22 -0400674 def roles(self, json_format=True, grep_str=""):
andrewonlab7c211572014-10-15 16:45:20 -0400675 '''
Jon Hall983a1702014-10-28 18:44:22 -0400676 Lists all devices and the controllers with roles assigned to them
677 Optional argument:
678 * grep_str - pass in a string to grep
andrewonlab7c211572014-10-15 16:45:20 -0400679 '''
680 try:
681 self.handle.sendline("")
682 self.handle.expect("onos>")
andrewonlab7c211572014-10-15 16:45:20 -0400683
Jon Hall983a1702014-10-28 18:44:22 -0400684 if json_format:
685 if not grep_str:
686 self.handle.sendline("roles -j")
687 self.handle.expect("roles -j")
688 self.handle.expect("onos>")
689 else:
690 self.handle.sendline("roles -j | grep '"+
691 str(grep_str)+"'")
692 self.handle.expect("roles -j | grep '"+str(grep_str)+"'")
693 self.handle.expect("onos>")
694 handle = self.handle.before
695 '''
696 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
697 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
698 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
699 So we take off that escape sequence using the following commads:
700 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
701 handle1 = ansi_escape.sub('', handle)
702 '''
703 #print "repr(handle) =", repr(handle)
704 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
705 handle1 = ansi_escape.sub('', handle)
706 #print "repr(handle1) = ", repr(handle1)
707 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400708
andrewonlab7c211572014-10-15 16:45:20 -0400709 else:
Jon Hall983a1702014-10-28 18:44:22 -0400710 if not grep_str:
711 self.handle.sendline("roles")
712 self.handle.expect("onos>")
713 self.handle.sendline("")
714 self.handle.expect("onos>")
715 else:
716 self.handle.sendline("roles | grep '"+
717 str(grep_str)+"'")
718 self.handle.expect("onos>")
719 self.handle.sendline("")
720 self.handle.expect("onos>")
721 handle = self.handle.before
722 #print "handle =",handle
723 return handle
724 except pexpect.EOF:
725 main.log.error(self.name + ": EOF exception found")
726 main.log.error(self.name + ": " + self.handle.before)
727 main.cleanup()
728 main.exit()
729 except:
730 main.log.info(self.name+" ::::::")
731 main.log.error( traceback.print_exc())
732 main.log.info(self.name+" ::::::")
733 main.cleanup()
734 main.exit()
735
736 def get_role(self, device_id):
737 '''
738 Given the a string containing the json representation of the "roles" cli command and a
739 partial or whole device id, returns a json object containing the
740 roles output for the first device whose id contains "device_id"
741
742 Returns:
743 Dict of the role assignments for the given device or
744 None if not match
745 '''
746 try:
747 import json
748 if device_id == None:
749 return None
750 else:
751 raw_roles = self.roles()
752 roles_json = json.loads(raw_roles)
753 #search json for the device with id then return the device
754 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400755 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400756 if str(device_id) in device['id']:
757 return device
758 return None
andrewonlab7c211572014-10-15 16:45:20 -0400759
andrewonlab86dc3082014-10-13 18:18:38 -0400760 except pexpect.EOF:
761 main.log.error(self.name + ": EOF exception found")
762 main.log.error(self.name + ": " + self.handle.before)
763 main.cleanup()
764 main.exit()
765 except:
766 main.log.info(self.name+" ::::::")
767 main.log.error( traceback.print_exc())
768 main.log.info(self.name+" ::::::")
769 main.cleanup()
770 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400771
andrewonlab3e15ead2014-10-15 14:21:34 -0400772 def paths(self, src_id, dst_id):
773 '''
774 Returns string of paths, and the cost.
775 Issues command: onos:paths <src> <dst>
776 '''
777 try:
778 self.handle.sendline("")
779 self.handle.expect("onos>")
780
781 self.handle.sendline("onos:paths "+
782 str(src_id) + " " + str(dst_id))
783 i = self.handle.expect([
784 "Error",
785 "onos>"])
786
787 self.handle.sendline("")
788 self.handle.expect("onos>")
789
790 handle = self.handle.before
791
792 if i == 0:
793 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400794 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400795 else:
796 path = handle.split(";")[0]
797 cost = handle.split(";")[1]
798 return (path, cost)
799
800 except pexpect.EOF:
801 main.log.error(self.name + ": EOF exception found")
802 main.log.error(self.name + ": " + self.handle.before)
803 main.cleanup()
804 main.exit()
805 except:
806 main.log.info(self.name+" ::::::")
807 main.log.error( traceback.print_exc())
808 main.log.info(self.name+" ::::::")
809 main.cleanup()
810 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400811
Jon Hall42db6dc2014-10-24 19:03:48 -0400812 def hosts(self, json_format=True, grep_str=""):
813 '''
814 Lists all discovered hosts
815 Optional argument:
816 * grep_str - pass in a string to grep
817 '''
818 try:
819 self.handle.sendline("")
820 self.handle.expect("onos>")
821
822 if json_format:
823 if not grep_str:
824 self.handle.sendline("hosts -j")
825 self.handle.expect("hosts -j")
826 self.handle.expect("onos>")
827 else:
828 self.handle.sendline("hosts -j | grep '"+
829 str(grep_str)+"'")
830 self.handle.expect("hosts -j | grep '"+str(grep_str)+"'")
831 self.handle.expect("onos>")
832 handle = self.handle.before
833 '''
834 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
835 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
836 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
837 So we take off that escape sequence using
838 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
839 handle1 = ansi_escape.sub('', handle)
840 '''
841 #print "repr(handle) =", repr(handle)
842 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
843 handle1 = ansi_escape.sub('', handle)
844 #print "repr(handle1) = ", repr(handle1)
845 return handle1
846 else:
847 if not grep_str:
848 self.handle.sendline("hosts")
849 self.handle.expect("onos>")
850 else:
851 self.handle.sendline("hosts | grep '"+
852 str(grep_str)+"'")
853 self.handle.expect("onos>")
854 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400855 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400856 return handle
857 except pexpect.EOF:
858 main.log.error(self.name + ": EOF exception found")
859 main.log.error(self.name + ": " + self.handle.before)
860 main.cleanup()
861 main.exit()
862 except:
863 main.log.info(self.name+" ::::::")
864 main.log.error( traceback.print_exc())
865 main.log.info(self.name+" ::::::")
866 main.cleanup()
867 main.exit()
868
869 def get_host(self, mac):
870 '''
871 Return the first host from the hosts api whose 'id' contains 'mac'
872 Note: mac must be a colon seperated mac address, but could be a partial mac address
873 Return None if there is no match
874 '''
875 import json
876 try:
877 if mac == None:
878 return None
879 else:
880 mac = mac
881 raw_hosts = self.hosts()
882 hosts_json = json.loads(raw_hosts)
883 #search json for the host with mac then return the device
884 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400885 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400886 if mac in host['id']:
887 return host
888 return None
889 except pexpect.EOF:
890 main.log.error(self.name + ": EOF exception found")
891 main.log.error(self.name + ": " + self.handle.before)
892 main.cleanup()
893 main.exit()
894 except:
895 main.log.info(self.name+" ::::::")
896 main.log.error( traceback.print_exc())
897 main.log.info(self.name+" ::::::")
898 main.cleanup()
899 main.exit()
900
andrewonlab3f0a4af2014-10-17 12:25:14 -0400901
902 def get_hosts_id(self, host_list):
903 '''
904 Obtain list of hosts
905 Issues command: 'onos> hosts'
906
907 Required:
908 * host_list: List of hosts obtained by Mininet
909 IMPORTANT:
910 This function assumes that you started your
911 topology with the option '--mac'.
912 Furthermore, it assumes that value of VLAN is '-1'
913 Description:
914 Converts mininet hosts (h1, h2, h3...) into
915 ONOS format (00:00:00:00:00:01/-1 , ...)
916 '''
917
918 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400919 onos_host_list = []
920
921 for host in host_list:
922 host = host.replace("h", "")
923 host_hex = hex(int(host)).zfill(12)
924 host_hex = str(host_hex).replace('x','0')
925 i = iter(str(host_hex))
926 host_hex = ":".join(a+b for a,b in zip(i,i))
927 host_hex = host_hex + "/-1"
928 onos_host_list.append(host_hex)
929
930 return onos_host_list
931
932 except pexpect.EOF:
933 main.log.error(self.name + ": EOF exception found")
934 main.log.error(self.name + ": " + self.handle.before)
935 main.cleanup()
936 main.exit()
937 except:
938 main.log.info(self.name+" ::::::")
939 main.log.error( traceback.print_exc())
940 main.log.info(self.name+" ::::::")
941 main.cleanup()
942 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400943
andrewonlabe6745342014-10-17 14:29:13 -0400944 def add_host_intent(self, host_id_one, host_id_two):
945 '''
946 Required:
947 * host_id_one: ONOS host id for host1
948 * host_id_two: ONOS host id for host2
949 Description:
950 Adds a host-to-host intent (bidrectional) by
951 specifying the two hosts.
952 '''
953 try:
954 self.handle.sendline("")
955 self.handle.expect("onos>")
956
957 self.handle.sendline("add-host-intent "+
958 str(host_id_one) + " " + str(host_id_two))
959 self.handle.expect("onos>")
960
andrewonlabe6745342014-10-17 14:29:13 -0400961 handle = self.handle.before
Shreya Shah4f25fdf2014-10-29 19:55:35 -0400962 print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400963
964 main.log.info("Intent installed between "+
965 str(host_id_one) + " and " + str(host_id_two))
966
967 return handle
968
969 except pexpect.EOF:
970 main.log.error(self.name + ": EOF exception found")
971 main.log.error(self.name + ": " + self.handle.before)
972 main.cleanup()
973 main.exit()
974 except:
975 main.log.info(self.name+" ::::::")
976 main.log.error( traceback.print_exc())
977 main.log.info(self.name+" ::::::")
978 main.cleanup()
979 main.exit()
980
andrewonlab7b31d232014-10-24 13:31:47 -0400981 def add_optical_intent(self, ingress_device, egress_device):
982 '''
983 Required:
984 * ingress_device: device id of ingress device
985 * egress_device: device id of egress device
986 Optional:
987 TODO: Still needs to be implemented via dev side
988 '''
989 try:
990 self.handle.sendline("add-optical-intent "+
991 str(ingress_device) + " " + str(egress_device))
992 self.handle.expect("add-optical-intent")
993 i = self.handle.expect([
994 "Error",
995 "onos>"])
996
997 handle = self.handle.before
998
999 #If error, return error message
1000 if i == 0:
1001 return handle
1002 else:
1003 return main.TRUE
1004
1005 except pexpect.EOF:
1006 main.log.error(self.name + ": EOF exception found")
1007 main.log.error(self.name + ": " + self.handle.before)
1008 main.cleanup()
1009 main.exit()
1010 except:
1011 main.log.info(self.name+" ::::::")
1012 main.log.error( traceback.print_exc())
1013 main.log.info(self.name+" ::::::")
1014 main.cleanup()
1015 main.exit()
1016
andrewonlab36af3822014-11-18 17:48:18 -05001017 def add_point_intent(self, ingress_device, egress_device,
1018 port_ingress="", port_egress="", ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -05001019 ethDst="", bandwidth="", lambda_alloc=False,
1020 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -04001021 '''
1022 Required:
1023 * ingress_device: device id of ingress device
1024 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001025 Optional:
1026 * ethType: specify ethType
1027 * ethSrc: specify ethSrc (i.e. src mac addr)
1028 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001029 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -05001030 * lambda_alloc: if True, intent will allocate lambda
1031 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -05001032 * ipProto: specify ip protocol
1033 * ipSrc: specify ip source address
1034 * ipDst: specify ip destination address
1035 * tcpSrc: specify tcp source port
1036 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001037 Description:
1038 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -04001039 specifying device id's and optional fields
1040
andrewonlab4dbb4d82014-10-17 18:22:31 -04001041 NOTE: This function may change depending on the
1042 options developers provide for point-to-point
1043 intent via cli
1044 '''
1045 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001046 cmd = ""
1047
1048 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001049 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001050 and not bandwidth and not lambda_alloc \
1051 and not ipProto and not ipSrc and not ipDst \
1052 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001053 cmd = "add-point-intent"
1054
1055
andrewonlab289e4b72014-10-21 21:24:18 -04001056 else:
andrewonlab36af3822014-11-18 17:48:18 -05001057 cmd = "add-point-intent"
andrewonlab9a130be2014-10-22 12:44:56 -04001058
andrewonlab0c0a6772014-10-22 12:31:18 -04001059 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001060 cmd += " --ethType " + str(ethType)
1061 if ethSrc:
1062 cmd += " --ethSrc " + str(ethSrc)
1063 if ethDst:
1064 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001065 if bandwidth:
1066 cmd += " --bandwidth " + str(bandwidth)
1067 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001068 cmd += " --lambda "
1069 if ipProto:
1070 cmd += " --ipProto " + str(ipProto)
1071 if ipSrc:
1072 cmd += " --ipSrc " + str(ipSrc)
1073 if ipDst:
1074 cmd += " --ipDst " + str(ipDst)
1075 if tcpSrc:
1076 cmd += " --tcpSrc " + str(tcpSrc)
1077 if tcpDst:
1078 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001079
andrewonlab36af3822014-11-18 17:48:18 -05001080 #Check whether the user appended the port
1081 #or provided it as an input
1082 if "/" in ingress_device:
1083 cmd += " "+str(ingress_device)
1084 else:
1085 if not port_ingress:
1086 main.log.error("You must specify "+
1087 "the ingress port")
1088 #TODO: perhaps more meaningful return
1089 return main.FALSE
1090
1091 cmd += " "+ \
1092 str(ingress_device) + "/" +\
1093 str(port_ingress) + " "
1094
1095 if "/" in egress_device:
1096 cmd += " "+str(egress_device)
1097 else:
1098 if not port_egress:
1099 main.log.error("You must specify "+
1100 "the egress port")
1101 return main.FALSE
1102
1103 cmd += " "+\
1104 str(egress_device) + "/" +\
1105 str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001106
andrewonlab289e4b72014-10-21 21:24:18 -04001107 self.handle.sendline(cmd)
andrewonlab36af3822014-11-18 17:48:18 -05001108
1109 main.log.info(cmd + " sent")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001110 i = self.handle.expect([
1111 "Error",
1112 "onos>"])
andrewonlab4dbb4d82014-10-17 18:22:31 -04001113
1114 if i == 0:
1115 main.log.error("Error in adding point-to-point intent")
1116 return handle
1117 else:
1118 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001119
andrewonlab4dbb4d82014-10-17 18:22:31 -04001120 except pexpect.EOF:
1121 main.log.error(self.name + ": EOF exception found")
1122 main.log.error(self.name + ": " + self.handle.before)
1123 main.cleanup()
1124 main.exit()
1125 except:
1126 main.log.info(self.name+" ::::::")
1127 main.log.error( traceback.print_exc())
1128 main.log.info(self.name+" ::::::")
1129 main.cleanup()
1130 main.exit()
1131
andrewonlab9a50dfe2014-10-17 17:22:31 -04001132 def remove_intent(self, intent_id):
1133 '''
1134 Remove intent for specified intent id
1135 '''
1136 try:
1137 self.handle.sendline("")
1138 self.handle.expect("onos>")
1139
1140 self.handle.sendline("remove-intent "+str(intent_id))
1141 i = self.handle.expect([
1142 "Error",
1143 "onos>"])
1144
1145 handle = self.handle.before
1146
1147 if i == 0:
1148 main.log.error("Error in removing intent")
1149 return handle
1150 else:
1151 return handle
1152
1153 except pexpect.EOF:
1154 main.log.error(self.name + ": EOF exception found")
1155 main.log.error(self.name + ": " + self.handle.before)
1156 main.cleanup()
1157 main.exit()
1158 except:
1159 main.log.info(self.name+" ::::::")
1160 main.log.error( traceback.print_exc())
1161 main.log.info(self.name+" ::::::")
1162 main.cleanup()
1163 main.exit()
1164
pingping-lindabe7972014-11-17 19:29:44 -08001165 # This method should be used after installing application: onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001166 def routes(self, json_format=False):
1167 '''
1168 Optional:
1169 * json_format: enable output formatting in json
1170 Description:
1171 Obtain all routes in the system
1172 '''
1173 try:
1174 if json_format:
1175 self.handle.sendline("routes -j")
1176 self.handle.expect("routes -j")
1177 self.handle.expect("onos>")
1178 handle_tmp = self.handle.before
1179
1180 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1181 handle = ansi_escape.sub('', handle_tmp)
1182
1183 else:
1184 self.handle.sendline("")
1185 self.handle.expect("onos>")
1186
1187 self.handle.sendline("routes")
1188 self.handle.expect("onos>")
1189 handle = self.handle.before
1190
1191 return handle
1192
1193 except pexpect.EOF:
1194 main.log.error(self.name + ": EOF exception found")
1195 main.log.error(self.name + ": " + self.handle.before)
1196 main.cleanup()
1197 main.exit()
1198 except:
1199 main.log.info(self.name + " ::::::")
1200 main.log.error(traceback.print_exc())
1201 main.log.info(self.name + " ::::::")
1202 main.cleanup()
1203 main.exit()
1204
andrewonlab377693f2014-10-21 16:00:30 -04001205 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -04001206 '''
andrewonlab377693f2014-10-21 16:00:30 -04001207 Optional:
1208 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001209 Description:
1210 Obtain intents currently installed
1211 '''
1212 try:
andrewonlab377693f2014-10-21 16:00:30 -04001213 if json_format:
1214 self.handle.sendline("intents -j")
1215 self.handle.expect("intents -j")
1216 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001217 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001218
1219 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1220 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001221 else:
1222 self.handle.sendline("")
1223 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001224
andrewonlab377693f2014-10-21 16:00:30 -04001225 self.handle.sendline("intents")
1226 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001227 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001228
1229 return handle
1230
1231 except pexpect.EOF:
1232 main.log.error(self.name + ": EOF exception found")
1233 main.log.error(self.name + ": " + self.handle.before)
1234 main.cleanup()
1235 main.exit()
1236 except:
1237 main.log.info(self.name+" ::::::")
1238 main.log.error( traceback.print_exc())
1239 main.log.info(self.name+" ::::::")
1240 main.cleanup()
1241 main.exit()
1242
Shreya Shah0f01c812014-10-26 20:15:28 -04001243 def flows(self, json_format = False):
1244 '''
1245 Optional:
1246 * json_format: enable output formatting in json
1247 Description:
1248 Obtain flows currently installed
1249 '''
1250 try:
1251 if json_format:
1252 self.handle.sendline("flows -j")
1253 self.handle.expect("flows -j")
1254 self.handle.expect("onos>")
1255 handle = self.handle.before
1256
1257 else:
1258 self.handle.sendline("")
1259 self.handle.expect("onos>")
1260 self.handle.sendline("flows")
1261 self.handle.expect("onos>")
1262 handle = self.handle.before
1263
1264 return handle
1265
1266 except pexpect.EOF:
1267 main.log.error(self.name + ": EOF exception found")
1268 main.log.error(self.name + ": " + self.handle.before)
1269 main.cleanup()
1270 main.exit()
1271 except:
1272 main.log.info(self.name+" ::::::")
1273 main.log.error( traceback.print_exc())
1274 main.log.info(self.name+" ::::::")
1275 main.cleanup()
1276 main.exit()
1277
andrewonlabb66dfa12014-12-02 15:51:10 -05001278 def push_test_intents(self, dpid_src, dpid_dst, num_intents,
1279 num_mult="", app_id="", report=True):
andrewonlab87852b02014-11-19 18:44:19 -05001280 '''
1281 Description:
1282 Push a number of intents in a batch format to
1283 a specific point-to-point intent definition
1284 Required:
1285 * dpid_src: specify source dpid
1286 * dpid_dst: specify destination dpid
1287 * num_intents: specify number of intents to push
1288 Optional:
andrewonlabb66dfa12014-12-02 15:51:10 -05001289 * num_mult: number multiplier for multiplying
1290 the number of intents specified
1291 * app_id: specify the application id init to further
1292 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001293 * report: default True, returns latency information
1294 '''
1295 try:
1296 cmd = "push-test-intents "+\
1297 str(dpid_src)+" "+str(dpid_dst)+" "+\
1298 str(num_intents)
andrewonlabb66dfa12014-12-02 15:51:10 -05001299
1300 if num_mult:
1301 cmd += " " + str(num_mult)
1302 if app_id:
1303 cmd += " " + str(app_id)
1304
andrewonlab87852b02014-11-19 18:44:19 -05001305 self.handle.sendline(cmd)
1306 self.handle.expect(cmd)
1307 self.handle.expect("onos>")
1308
1309 handle = self.handle.before
1310
1311 #Some color thing that we want to escape
1312 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1313 handle = ansi_escape.sub('', handle)
1314
1315 if report:
andrewonlabb66dfa12014-12-02 15:51:10 -05001316 lat_result = []
andrewonlab87852b02014-11-19 18:44:19 -05001317 main.log.info(handle)
andrewonlabb66dfa12014-12-02 15:51:10 -05001318 #Split result by newline
1319 newline = handle.split("\r\r\n")
1320 #Ignore the first object of list, which is empty
1321 newline = newline[1:]
1322 #Some sloppy parsing method to get the latency
1323 for result in newline:
1324 result = result.split(": ")
1325 #Append the first result of second parse
1326 lat_result.append(result[1].split(" ")[0])
1327
1328 print lat_result
1329 return lat_result
andrewonlab87852b02014-11-19 18:44:19 -05001330 else:
1331 return main.TRUE
1332
1333 except pexpect.EOF:
1334 main.log.error(self.name + ": EOF exception found")
1335 main.log.error(self.name + ": " + self.handle.before)
1336 main.cleanup()
1337 main.exit()
1338 except:
1339 main.log.info(self.name+" ::::::")
1340 main.log.error( traceback.print_exc())
1341 main.log.info(self.name+" ::::::")
1342 main.cleanup()
1343 main.exit()
1344
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001345 def intents_events_metrics(self, json_format=True):
1346 '''
1347 Description:Returns topology metrics
1348 Optional:
1349 * json_format: enable json formatting of output
1350 '''
1351 try:
1352 if json_format:
1353 self.handle.sendline("intents-events-metrics -j")
1354 self.handle.expect("intents-events-metrics -j")
1355 self.handle.expect("onos>")
1356
1357 handle = self.handle.before
1358
1359 #Some color thing that we want to escape
1360 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1361 handle = ansi_escape.sub('', handle)
1362
1363 else:
1364 self.handle.sendline("intents-events-metrics")
1365 self.handle.expect("intents-events-metrics")
1366 self.handle.expect("onos>")
1367
1368 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001369
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001370 return handle
1371
1372 except pexpect.EOF:
1373 main.log.error(self.name + ": EOF exception found")
1374 main.log.error(self.name + ": " + self.handle.before)
1375 main.cleanup()
1376 main.exit()
1377 except:
1378 main.log.info(self.name+" ::::::")
1379 main.log.error( traceback.print_exc())
1380 main.log.info(self.name+" ::::::")
1381 main.cleanup()
1382 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001383
andrewonlab867212a2014-10-22 20:13:38 -04001384 def topology_events_metrics(self, json_format=True):
1385 '''
1386 Description:Returns topology metrics
1387 Optional:
1388 * json_format: enable json formatting of output
1389 '''
1390 try:
1391 if json_format:
1392 self.handle.sendline("topology-events-metrics -j")
1393 self.handle.expect("topology-events-metrics -j")
1394 self.handle.expect("onos>")
1395
1396 handle = self.handle.before
1397
1398 #Some color thing that we want to escape
1399 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1400 handle = ansi_escape.sub('', handle)
1401
1402 else:
1403 self.handle.sendline("topology-events-metrics")
1404 self.handle.expect("topology-events-metrics")
1405 self.handle.expect("onos>")
1406
1407 handle = self.handle.before
1408
1409 return handle
1410
1411 except pexpect.EOF:
1412 main.log.error(self.name + ": EOF exception found")
1413 main.log.error(self.name + ": " + self.handle.before)
1414 main.cleanup()
1415 main.exit()
1416 except:
1417 main.log.info(self.name+" ::::::")
1418 main.log.error( traceback.print_exc())
1419 main.log.info(self.name+" ::::::")
1420 main.cleanup()
1421 main.exit()
1422
andrewonlab3e15ead2014-10-15 14:21:34 -04001423 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001424 #Wrapper functions use existing driver
1425 #functions and extends their use case.
1426 #For example, we may use the output of
1427 #a normal driver function, and parse it
1428 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001429
andrewonlab9a50dfe2014-10-17 17:22:31 -04001430 def get_all_intents_id(self):
1431 '''
1432 Description:
1433 Obtain all intent id's in a list
1434 '''
1435 try:
1436 #Obtain output of intents function
1437 intents_str = self.intents()
1438 all_intent_list = []
1439 intent_id_list = []
1440
1441 #Parse the intents output for ID's
1442 intents_list = [s.strip() for s in intents_str.splitlines()]
1443 for intents in intents_list:
1444 if "onos>" in intents:
1445 continue
1446 elif "intents" in intents:
1447 continue
1448 else:
1449 line_list = intents.split(" ")
1450 all_intent_list.append(line_list[0])
1451
1452 all_intent_list = all_intent_list[1:-2]
1453
1454 for intents in all_intent_list:
1455 if not intents:
1456 continue
1457 else:
1458 intent_id_list.append(intents)
1459
1460 return intent_id_list
1461
1462 except pexpect.EOF:
1463 main.log.error(self.name + ": EOF exception found")
1464 main.log.error(self.name + ": " + self.handle.before)
1465 main.cleanup()
1466 main.exit()
1467 except:
1468 main.log.info(self.name+" ::::::")
1469 main.log.error( traceback.print_exc())
1470 main.log.info(self.name+" ::::::")
1471 main.cleanup()
1472 main.exit()
1473
andrewonlab7e4d2d32014-10-15 13:23:21 -04001474 def get_all_devices_id(self):
1475 '''
1476 Use 'devices' function to obtain list of all devices
1477 and parse the result to obtain a list of all device
1478 id's. Returns this list. Returns empty list if no
1479 devices exist
1480 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001481
1482 This function may be useful if you are not sure of the
1483 device id, and wish to execute other commands using
1484 the ids. By obtaining the list of device ids on the fly,
1485 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001486 '''
1487 try:
1488 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001489 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001490 id_list = []
1491
1492 if not devices_str:
1493 main.log.info("There are no devices to get id from")
1494 return id_list
1495
1496 #Split the string into list by comma
1497 device_list = devices_str.split(",")
1498 #Get temporary list of all arguments with string 'id='
1499 temp_list = [dev for dev in device_list if "id=" in dev]
1500 #Split list further into arguments before and after string
1501 # 'id='. Get the latter portion (the actual device id) and
1502 # append to id_list
1503 for arg in temp_list:
1504 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001505 return id_list
1506
1507 except pexpect.EOF:
1508 main.log.error(self.name + ": EOF exception found")
1509 main.log.error(self.name + ": " + self.handle.before)
1510 main.cleanup()
1511 main.exit()
1512 except:
1513 main.log.info(self.name+" ::::::")
1514 main.log.error( traceback.print_exc())
1515 main.log.info(self.name+" ::::::")
1516 main.cleanup()
1517 main.exit()
1518
andrewonlab7c211572014-10-15 16:45:20 -04001519 def get_all_nodes_id(self):
1520 '''
1521 Uses 'nodes' function to obtain list of all nodes
1522 and parse the result of nodes to obtain just the
1523 node id's.
1524 Returns:
1525 list of node id's
1526 '''
1527 try:
1528 nodes_str = self.nodes()
1529 id_list = []
1530
1531 if not nodes_str:
1532 main.log.info("There are no nodes to get id from")
1533 return id_list
1534
1535 #Sample nodes_str output
1536 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1537
1538 #Split the string into list by comma
1539 nodes_list = nodes_str.split(",")
1540 temp_list = [node for node in nodes_list if "id=" in node]
1541 for arg in temp_list:
1542 id_list.append(arg.split("id=")[1])
1543
1544 return id_list
1545
1546 except pexpect.EOF:
1547 main.log.error(self.name + ": EOF exception found")
1548 main.log.error(self.name + ": " + self.handle.before)
1549 main.cleanup()
1550 main.exit()
1551 except:
1552 main.log.info(self.name+" ::::::")
1553 main.log.error( traceback.print_exc())
1554 main.log.info(self.name+" ::::::")
1555 main.cleanup()
1556 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001557
Jon Halla91c4dc2014-10-22 12:57:04 -04001558 def get_device(self, dpid=None):
1559 '''
1560 Return the first device from the devices api whose 'id' contains 'dpid'
1561 Return None if there is no match
1562 '''
1563 import json
1564 try:
1565 if dpid == None:
1566 return None
1567 else:
1568 dpid = dpid.replace(':', '')
1569 raw_devices = self.devices()
1570 devices_json = json.loads(raw_devices)
1571 #search json for the device with dpid then return the device
1572 for device in devices_json:
1573 #print "%s in %s?" % (dpid, device['id'])
1574 if dpid in device['id']:
1575 return device
1576 return None
1577 except pexpect.EOF:
1578 main.log.error(self.name + ": EOF exception found")
1579 main.log.error(self.name + ": " + self.handle.before)
1580 main.cleanup()
1581 main.exit()
1582 except:
1583 main.log.info(self.name+" ::::::")
1584 main.log.error( traceback.print_exc())
1585 main.log.info(self.name+" ::::::")
1586 main.cleanup()
1587 main.exit()
1588
Jon Hall42db6dc2014-10-24 19:03:48 -04001589 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1590 '''
1591 Checks the number of swithes & links that ONOS sees against the
1592 supplied values. By default this will report to main.log, but the
1593 log level can be specifid.
1594
1595 Params: ip = ip used for the onos cli
1596 numoswitch = expected number of switches
1597 numlink = expected number of links
1598 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1599
1600
1601 log_level can
1602
1603 Returns: main.TRUE if the number of switchs and links are correct,
1604 main.FALSE if the numer of switches and links is incorrect,
1605 and main.ERROR otherwise
1606 '''
1607
1608 try:
1609 topology = self.get_topology(ip)
1610 if topology == {}:
1611 return main.ERROR
1612 output = ""
1613 #Is the number of switches is what we expected
1614 devices = topology.get('devices',False)
1615 links = topology.get('links',False)
1616 if devices == False or links == False:
1617 return main.ERROR
1618 switch_check = ( int(devices) == int(numoswitch) )
1619 #Is the number of links is what we expected
1620 link_check = ( int(links) == int(numolink) )
1621 if (switch_check and link_check):
1622 #We expected the correct numbers
1623 output = output + "The number of links and switches match "\
1624 + "what was expected"
1625 result = main.TRUE
1626 else:
1627 output = output + \
1628 "The number of links and switches does not match what was expected"
1629 result = main.FALSE
1630 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1631 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1632 if log_level == "report":
1633 main.log.report(output)
1634 elif log_level == "warn":
1635 main.log.warn(output)
1636 else:
1637 main.log.info(output)
1638 return result
1639 except pexpect.EOF:
1640 main.log.error(self.name + ": EOF exception found")
1641 main.log.error(self.name + ": " + self.handle.before)
1642 main.cleanup()
1643 main.exit()
1644 except:
1645 main.log.info(self.name+" ::::::")
1646 main.log.error( traceback.print_exc())
1647 main.log.info(self.name+" ::::::")
1648 main.cleanup()
1649 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001650
1651 def device_role(self, device_id, onos_node, role="master"):
1652 '''
1653 Calls the device-role cli command.
1654 device_id must be the id of a device as seen in the onos devices command
1655 onos_node is the ip of one of the onos nodes in the cluster
1656 role must be either master, standby, or none
1657
Jon Hall983a1702014-10-28 18:44:22 -04001658 Returns main.TRUE or main.FALSE based argument varification.
1659 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001660 support that output
1661 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001662 try:
Jon Hall983a1702014-10-28 18:44:22 -04001663 #print "beginning device_role... \n\tdevice_id:" + device_id
1664 #print "\tonos_node: " + onos_node
1665 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001666 if role.lower() == "master" or \
1667 role.lower() == "standby" or \
1668 role.lower() == "none":
1669 self.handle.sendline("")
1670 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001671 self.handle.sendline("device-role " +
1672 str(device_id) + " " +
1673 str(onos_node) + " " +
1674 str(role))
1675 i= self.handle.expect(["Error","onos>"])
1676 if i == 0:
1677 output = str(self.handle.before)
1678 self.handle.expect("onos>")
1679 output = output + str(self.handle.before)
1680 main.log.error(self.name + ": " +
1681 output + '\033[0m')#end color output to escape any colours from the cli
1682 return main.ERROR
1683 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001684 self.handle.expect("onos>")
1685 return main.TRUE
1686 else:
1687 return main.FALSE
1688
1689 except pexpect.EOF:
1690 main.log.error(self.name + ": EOF exception found")
1691 main.log.error(self.name + ": " + self.handle.before)
1692 main.cleanup()
1693 main.exit()
1694 except:
1695 main.log.info(self.name+" ::::::")
1696 main.log.error( traceback.print_exc())
1697 main.log.info(self.name+" ::::::")
1698 main.cleanup()
1699 main.exit()
1700
1701
andrewonlab7e4d2d32014-10-15 13:23:21 -04001702 #***********************************