blob: 838e6f41023ae3f35144d733952337189d6464e5 [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
andrewonlab95ce8322014-10-13 14:12:04 -040021import pexpect
22import re
23import traceback
Jon Hall47a93fb2015-01-06 16:46:06 -080024#import os.path
andrewonlab95ce8322014-10-13 14:12:04 -040025sys.path.append("../")
26from drivers.common.clidriver import CLI
27
28class OnosCliDriver(CLI):
29
30 def __init__(self):
31 '''
32 Initialize client
33 '''
34 super(CLI, self).__init__()
35
36 def connect(self,**connectargs):
37 '''
38 Creates ssh handle for ONOS cli.
39 '''
40 try:
41 for key in connectargs:
42 vars(self)[key] = connectargs[key]
43 self.home = "~/ONOS"
44 for key in self.options:
45 if key == "home":
46 self.home = self.options['home']
47 break
48
49
50 self.name = self.options['name']
51 self.handle = super(OnosCliDriver,self).connect(
52 user_name = self.user_name,
53 ip_address = self.ip_address,
54 port = self.port,
55 pwd = self.pwd,
56 home = self.home)
57
58 self.handle.sendline("cd "+ self.home)
59 self.handle.expect("\$")
60 if self.handle:
61 return self.handle
62 else :
63 main.log.info("NO ONOS HANDLE")
64 return main.FALSE
65 except pexpect.EOF:
66 main.log.error(self.name + ": EOF exception found")
67 main.log.error(self.name + ": " + self.handle.before)
68 main.cleanup()
69 main.exit()
70 except:
andrewonlab9627f432014-11-14 12:45:10 -050071 main.log.info(self.name + ":::::::::::::::::::::::")
andrewonlab95ce8322014-10-13 14:12:04 -040072 main.log.error( traceback.print_exc() )
andrewonlab9627f432014-11-14 12:45:10 -050073 main.log.info(":::::::::::::::::::::::")
andrewonlab95ce8322014-10-13 14:12:04 -040074 main.cleanup()
75 main.exit()
76
77 def disconnect(self):
78 '''
79 Called when Test is complete to disconnect the ONOS handle.
80 '''
81 response = ''
82 try:
andrewonlab2a6c9342014-10-16 13:40:15 -040083 self.handle.sendline("")
Jon Hall7e5b9172014-10-22 12:32:47 -040084 i = self.handle.expect(["onos>","\$"])
85 if i == 0:
86 self.handle.sendline("system:shutdown")
87 self.handle.expect("Confirm")
88 self.handle.sendline("yes")
89 self.handle.expect("\$")
Jon Hallffb386d2014-11-21 13:43:38 -080090 self.handle.sendline("")
andrewonlabc2d05aa2014-10-13 16:51:10 -040091 self.handle.expect("\$")
Jon Hall7e5b9172014-10-22 12:32:47 -040092 self.handle.sendline("exit")
93 self.handle.expect("closed")
andrewonlabc2d05aa2014-10-13 16:51:10 -040094
andrewonlab95ce8322014-10-13 14:12:04 -040095 except pexpect.EOF:
96 main.log.error(self.name + ": EOF exception found")
97 main.log.error(self.name + ": " + self.handle.before)
98 except:
99 main.log.error(self.name + ": Connection failed to the host")
100 response = main.FALSE
101 return response
102
andrewonlab38d2b4a2014-11-13 16:28:47 -0500103 def logout(self):
104 '''
105 Sends 'logout' command to ONOS cli
106 '''
107 try:
andrewonlab9627f432014-11-14 12:45:10 -0500108 self.handle.sendline("")
109 i = self.handle.expect([
110 "onos>",
111 "\$"], timeout=10)
112 if i == 0:
113 self.handle.sendline("logout")
114 self.handle.expect("\$")
115 elif i == 1:
116 return main.TRUE
117
andrewonlab38d2b4a2014-11-13 16:28:47 -0500118 except pexpect.EOF:
119 main.log.error(self.name + ": eof exception found")
andrewonlab9627f432014-11-14 12:45:10 -0500120 main.log.error(self.name + ": " +
121 self.handle.before)
andrewonlab38d2b4a2014-11-13 16:28:47 -0500122 main.cleanup()
123 main.exit()
124 except:
125 main.log.info(self.name+" ::::::")
126 main.log.error( traceback.print_exc())
127 main.log.info(self.name+" ::::::")
128 main.cleanup()
129 main.exit()
130
andrewonlab95ce8322014-10-13 14:12:04 -0400131 def set_cell(self, cellname):
132 '''
133 Calls 'cell <name>' to set the environment variables on ONOSbench
134
135 Before issuing any cli commands, set the environment variable first.
136 '''
137 try:
138 if not cellname:
139 main.log.error("Must define cellname")
140 main.cleanup()
141 main.exit()
142 else:
143 self.handle.sendline("cell "+str(cellname))
144 #Expect the cellname in the ONOS_CELL variable.
145 #Note that this variable name is subject to change
146 # and that this driver will have to change accordingly
147 self.handle.expect("ONOS_CELL="+str(cellname))
148 handle_before = self.handle.before
149 handle_after = self.handle.after
150 #Get the rest of the handle
151 self.handle.sendline("")
152 self.handle.expect("\$")
153 handle_more = self.handle.before
154
155 main.log.info("Cell call returned: "+handle_before+
156 handle_after + handle_more)
157
158 return main.TRUE
159
160 except pexpect.EOF:
andrewonlab38d2b4a2014-11-13 16:28:47 -0500161 main.log.error(self.name + ": eof exception found")
andrewonlab95ce8322014-10-13 14:12:04 -0400162 main.log.error(self.name + ": " + self.handle.before)
163 main.cleanup()
164 main.exit()
165 except:
166 main.log.info(self.name+" ::::::")
167 main.log.error( traceback.print_exc())
168 main.log.info(self.name+" ::::::")
169 main.cleanup()
170 main.exit()
171
Hari Krishnae36ef212015-01-04 14:09:13 -0800172 def start_onos_cli(self, ONOS_ip, karafTimeout=""):
Hari Krishnad7b9c202015-01-05 10:38:14 -0800173 '''
174 karafTimeout is an optional arugument. karafTimeout value passed by user would be used to set the
175 current karaf shell idle timeout. Note that when ever this property is modified the shell will exit and
176 the subsequent login would reflect new idle timeout.
Hari Krishna25d42f72015-01-05 15:08:28 -0800177 Below is an example to start a session with 60 seconds idle timeout (input value is in milliseconds):
178
179 tValue = "60000"
180 main.ONOScli1.start_onos_cli(ONOS_ip, karafTimeout=tValue)
181
182 Note: karafTimeout is left as str so that this could be read and passed to start_onos_cli from PARAMS file as str.
Hari Krishnad7b9c202015-01-05 10:38:14 -0800183 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400184 try:
185 self.handle.sendline("")
andrewonlab48829f62014-11-17 13:49:01 -0500186 x = self.handle.expect([
187 "\$", "onos>"], timeout=10)
188
189 if x == 1:
190 main.log.info("ONOS cli is already running")
191 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400192
193 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400194 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400195 i = self.handle.expect([
196 "onos>",
197 pexpect.TIMEOUT],timeout=60)
198
199 if i == 0:
200 main.log.info(str(ONOS_ip)+" CLI Started successfully")
Hari Krishnae36ef212015-01-04 14:09:13 -0800201 if karafTimeout:
202 self.handle.sendline("config:property-set -p org.apache.karaf.shell sshIdleTimeout "+karafTimeout)
203 self.handle.expect("\$")
204 self.handle.sendline("onos -w "+str(ONOS_ip))
205 self.handle.expect("onos>")
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400206 return main.TRUE
207 else:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400208 #If failed, send ctrl+c to process and try again
andrewonlabf47993a2014-10-24 17:56:01 -0400209 main.log.info("Starting CLI failed. Retrying...")
Jon Hallffb386d2014-11-21 13:43:38 -0800210 self.handle.send("\x03")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400211 self.handle.sendline("onos -w "+str(ONOS_ip))
212 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
213 timeout=30)
214 if i == 0:
andrewonlab28ca4b22014-11-20 13:15:59 -0500215 main.log.info(str(ONOS_ip)+" CLI Started "+
216 "successfully after retry attempt")
Hari Krishnae36ef212015-01-04 14:09:13 -0800217 if karafTimeout:
218 self.handle.sendline("config:property-set -p org.apache.karaf.shell sshIdleTimeout "+karafTimeout)
219 self.handle.expect("\$")
220 self.handle.sendline("onos -w "+str(ONOS_ip))
221 self.handle.expect("onos>")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400222 return main.TRUE
223 else:
224 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400225 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400226 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400227
228 except pexpect.EOF:
229 main.log.error(self.name + ": EOF exception found")
230 main.log.error(self.name + ": " + self.handle.before)
231 main.cleanup()
232 main.exit()
233 except:
234 main.log.info(self.name+" ::::::")
235 main.log.error( traceback.print_exc())
236 main.log.info(self.name+" ::::::")
237 main.cleanup()
238 main.exit()
239
andrewonlaba18f6bf2014-10-13 19:31:54 -0400240 def sendline(self, cmd_str):
241 '''
242 Send a completely user specified string to
243 the onos> prompt. Use this function if you have
244 a very specific command to send.
245
246 Warning: There are no sanity checking to commands
247 sent using this method.
248 '''
249 try:
250 self.handle.sendline("")
251 self.handle.expect("onos>")
252
253 self.handle.sendline(cmd_str)
254 self.handle.expect("onos>")
255
256 handle = self.handle.before
257
258 self.handle.sendline("")
259 self.handle.expect("onos>")
260
261 handle += self.handle.before
262 handle += self.handle.after
263
264 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400265 ansi_escape = re.compile(r'\x1b[^m]*m')
266 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400267
268 return handle
269 except pexpect.EOF:
270 main.log.error(self.name + ": EOF exception found")
271 main.log.error(self.name + ": " + self.handle.before)
272 main.cleanup()
273 main.exit()
274 except:
275 main.log.info(self.name+" ::::::")
276 main.log.error( traceback.print_exc())
277 main.log.info(self.name+" ::::::")
278 main.cleanup()
279 main.exit()
280
andrewonlab95ce8322014-10-13 14:12:04 -0400281 #IMPORTANT NOTE:
282 #For all cli commands, naming convention should match
283 #the cli command replacing ':' with '_'.
284 #Ex) onos:topology > onos_topology
285 # onos:links > onos_links
286 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400287
288 def add_node(self, node_id, ONOS_ip, tcp_port=""):
289 '''
290 Adds a new cluster node by ID and address information.
291 Required:
292 * node_id
293 * ONOS_ip
294 Optional:
295 * tcp_port
296 '''
297 try:
298 self.handle.sendline("")
299 self.handle.expect("onos>")
300
301 self.handle.sendline("add-node "+
302 str(node_id)+" "+
303 str(ONOS_ip)+" "+
304 str(tcp_port))
305
306 i = self.handle.expect([
307 "Error",
308 "onos>" ])
309
310 #Clear handle to get previous output
311 self.handle.sendline("")
312 self.handle.expect("onos>")
313
314 handle = self.handle.before
315
316 if i == 0:
317 main.log.error("Error in adding node")
318 main.log.error(handle)
319 return main.FALSE
320 else:
321 main.log.info("Node "+str(ONOS_ip)+" added")
322 return main.TRUE
323
324 except pexpect.EOF:
325 main.log.error(self.name + ": EOF exception found")
326 main.log.error(self.name + ": " + self.handle.before)
327 main.cleanup()
328 main.exit()
329 except:
330 main.log.info(self.name+" ::::::")
331 main.log.error( traceback.print_exc())
332 main.log.info(self.name+" ::::::")
333 main.cleanup()
334 main.exit()
335
andrewonlab86dc3082014-10-13 18:18:38 -0400336 def remove_node(self, node_id):
337 '''
338 Removes a cluster by ID
339 Issues command: 'remove-node [<node-id>]'
340 Required:
341 * node_id
342 '''
343 try:
344 self.handle.sendline("")
345 self.handle.expect("onos>")
346
347 self.handle.sendline("remove-node "+str(node_id))
348 self.handle.expect("onos>")
349
350 return main.TRUE
351
352 except pexpect.EOF:
353 main.log.error(self.name + ": EOF exception found")
354 main.log.error(self.name + ": " + self.handle.before)
355 main.cleanup()
356 main.exit()
357 except:
358 main.log.info(self.name+" ::::::")
359 main.log.error( traceback.print_exc())
360 main.log.info(self.name+" ::::::")
361 main.cleanup()
362 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400363
andrewonlab7c211572014-10-15 16:45:20 -0400364 def nodes(self):
365 '''
366 List the nodes currently visible
367 Issues command: 'nodes'
368 Returns: entire handle of list of nodes
369 '''
370 try:
371 self.handle.sendline("")
372 self.handle.expect("onos>")
373
374 self.handle.sendline("nodes")
375 self.handle.expect("onos>")
376
377 self.handle.sendline("")
378 self.handle.expect("onos>")
379
380 handle = self.handle.before
381
382 return handle
383
384 except pexpect.EOF:
385 main.log.error(self.name + ": EOF exception found")
386 main.log.error(self.name + ": " + self.handle.before)
387 main.cleanup()
388 main.exit()
389 except:
390 main.log.info(self.name+" ::::::")
391 main.log.error( traceback.print_exc())
392 main.log.info(self.name+" ::::::")
393 main.cleanup()
394 main.exit()
395
andrewonlab38d6ae22014-10-15 14:23:45 -0400396 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400397 '''
398 Shows the current state of the topology
399 by issusing command: 'onos> onos:topology'
400 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400401 try:
402 self.handle.sendline("")
403 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400404 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400405 self.handle.sendline("onos:topology")
406 self.handle.expect("onos>")
407
408 handle = self.handle.before
409
410 main.log.info("onos:topology returned: " +
411 str(handle))
412
413 return handle
414
415 except pexpect.EOF:
416 main.log.error(self.name + ": EOF exception found")
417 main.log.error(self.name + ": " + self.handle.before)
418 main.cleanup()
419 main.exit()
420 except:
421 main.log.info(self.name+" ::::::")
422 main.log.error( traceback.print_exc())
423 main.log.info(self.name+" ::::::")
424 main.cleanup()
425 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400426
427 def feature_install(self, feature_str):
428 '''
429 Installs a specified feature
430 by issuing command: 'onos> feature:install <feature_str>'
431 '''
432 try:
433 self.handle.sendline("")
434 self.handle.expect("onos>")
435
436 self.handle.sendline("feature:install "+str(feature_str))
437 self.handle.expect("onos>")
438
439 return main.TRUE
440
441 except pexpect.EOF:
442 main.log.error(self.name + ": EOF exception found")
443 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500444 main.log.report("Failed to install feature")
445 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400446 main.cleanup()
447 main.exit()
448 except:
449 main.log.info(self.name+" ::::::")
450 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500451 main.log.report("Failed to install feature")
452 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400453 main.log.info(self.name+" ::::::")
454 main.cleanup()
455 main.exit()
456
457 def feature_uninstall(self, feature_str):
458 '''
459 Uninstalls a specified feature
460 by issuing command: 'onos> feature:uninstall <feature_str>'
461 '''
462 try:
463 self.handle.sendline("")
464 self.handle.expect("onos>")
465
466 self.handle.sendline("feature:uninstall "+str(feature_str))
467 self.handle.expect("onos>")
468
469 return main.TRUE
470
471 except pexpect.EOF:
472 main.log.error(self.name + ": EOF exception found")
473 main.log.error(self.name + ": " + self.handle.before)
474 main.cleanup()
475 main.exit()
476 except:
477 main.log.info(self.name+" ::::::")
478 main.log.error( traceback.print_exc())
479 main.log.info(self.name+" ::::::")
480 main.cleanup()
481 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800482
483 def devices(self, json_format=True):
andrewonlab86dc3082014-10-13 18:18:38 -0400484 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400485 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400486 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800487 * json_format - boolean indicating if you want output in json
andrewonlab86dc3082014-10-13 18:18:38 -0400488 '''
489 try:
490 self.handle.sendline("")
491 self.handle.expect("onos>")
andrewonlabb66dfa12014-12-02 15:51:10 -0500492
Jon Halle8217482014-10-17 13:49:14 -0400493 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800494 self.handle.sendline("devices -j")
495 self.handle.expect("devices -j")
496 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400497 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400498 '''
499 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
500 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 -0400501 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 -0400502 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400503 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400504 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400505 '''
506 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400507 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
508 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400509 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400510 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400511 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800512 self.handle.sendline("devices")
513 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400514 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400515 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400516 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400517 except pexpect.EOF:
518 main.log.error(self.name + ": EOF exception found")
519 main.log.error(self.name + ": " + self.handle.before)
520 main.cleanup()
521 main.exit()
522 except:
523 main.log.info(self.name+" ::::::")
524 main.log.error( traceback.print_exc())
525 main.log.info(self.name+" ::::::")
526 main.cleanup()
527 main.exit()
528
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800529
530 def balance_masters(self):
531 '''
532 This balances the devices across all controllers
533 by issuing command: 'onos> onos:balance-masters'
534 If required this could be extended to return devices balanced output.
535 '''
536 try:
537 self.handle.sendline("")
538 self.handle.expect("onos>")
539
540 self.handle.sendline("onos:balance-masters")
541 self.handle.expect("onos>")
542 return main.TRUE
543
544 except pexpect.EOF:
545 main.log.error(self.name + ": EOF exception found")
546 main.log.error(self.name + ": " + self.handle.before)
547 main.cleanup()
548 main.exit()
549 except:
550 main.log.info(self.name+" ::::::")
551 main.log.error( traceback.print_exc())
552 main.log.info(self.name+" ::::::")
553 main.cleanup()
554 main.exit()
555
Hari Krishna2bbaa702014-12-19 14:46:12 -0800556 def links(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400557 '''
558 Lists all core links
559 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800560 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400561 '''
562 try:
563 self.handle.sendline("")
564 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800565
Jon Halle8217482014-10-17 13:49:14 -0400566 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800567 self.handle.sendline("links -j")
568 self.handle.expect("links -j")
569 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400570 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400571 '''
572 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
573 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 -0400574 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Hallffb386d2014-11-21 13:43:38 -0800575 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400576 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800577 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400578 '''
579 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400580 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
581 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400582 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400583 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400584 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800585 self.handle.sendline("links")
586 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400587 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400588 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400589 return handle
Jon Halle8217482014-10-17 13:49:14 -0400590 except pexpect.EOF:
591 main.log.error(self.name + ": EOF exception found")
592 main.log.error(self.name + ": " + self.handle.before)
593 main.cleanup()
594 main.exit()
595 except:
596 main.log.info(self.name+" ::::::")
597 main.log.error( traceback.print_exc())
598 main.log.info(self.name+" ::::::")
599 main.cleanup()
600 main.exit()
601
602
Jon Hallffb386d2014-11-21 13:43:38 -0800603 def ports(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400604 '''
605 Lists all ports
606 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800607 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400608 '''
609 try:
610 self.handle.sendline("")
611 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800612
Jon Halle8217482014-10-17 13:49:14 -0400613 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800614 self.handle.sendline("ports -j")
615 self.handle.expect("ports -j")
616 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400617 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400618 '''
619 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
620 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 -0400621 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Hallffb386d2014-11-21 13:43:38 -0800622 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400623 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800624 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400625 '''
626 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400627 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
628 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400629 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400630 return handle1
631
Jon Halle8217482014-10-17 13:49:14 -0400632 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800633 self.handle.sendline("ports")
634 self.handle.expect("onos>")
635 self.handle.sendline("")
636 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400637 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400638 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800639 return handle
Jon Halle8217482014-10-17 13:49:14 -0400640 except pexpect.EOF:
641 main.log.error(self.name + ": EOF exception found")
642 main.log.error(self.name + ": " + self.handle.before)
643 main.cleanup()
644 main.exit()
645 except:
646 main.log.info(self.name+" ::::::")
647 main.log.error( traceback.print_exc())
648 main.log.info(self.name+" ::::::")
649 main.cleanup()
650 main.exit()
651
652
Jon Hallffb386d2014-11-21 13:43:38 -0800653 def roles(self, json_format=True):
andrewonlab7c211572014-10-15 16:45:20 -0400654 '''
Jon Hall983a1702014-10-28 18:44:22 -0400655 Lists all devices and the controllers with roles assigned to them
656 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800657 * json_format - boolean indicating if you want output in json
andrewonlab7c211572014-10-15 16:45:20 -0400658 '''
659 try:
660 self.handle.sendline("")
661 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500662
Jon Hall983a1702014-10-28 18:44:22 -0400663 if json_format:
Jon Hallb1290e82014-11-18 16:17:48 -0500664 self.handle.sendline("roles -j")
665 self.handle.expect("roles -j")
666 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400667 handle = self.handle.before
668 '''
Jon Hallb1290e82014-11-18 16:17:48 -0500669 handle variable here contains some ANSI escape color code sequences at the
670 end which are invisible in the print command output. To make that escape
671 sequence visible, use repr() function. The repr(handle) output when printed
672 shows the ANSI escape sequences. In json.loads(somestring), this somestring
673 variable is actually repr(somestring) and json.loads would fail with the escape
674 sequence.
675
676 So we take off that escape sequence using the following commads:
Jon Hall983a1702014-10-28 18:44:22 -0400677 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallb1290e82014-11-18 16:17:48 -0500678 handle1 = ansi_escape.sub('', handle)
Jon Hall983a1702014-10-28 18:44:22 -0400679 '''
680 #print "repr(handle) =", repr(handle)
681 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
682 handle1 = ansi_escape.sub('', handle)
683 #print "repr(handle1) = ", repr(handle1)
684 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400685
andrewonlab7c211572014-10-15 16:45:20 -0400686 else:
Jon Hallb1290e82014-11-18 16:17:48 -0500687 self.handle.sendline("roles")
688 self.handle.expect("onos>")
689 self.handle.sendline("")
690 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400691 handle = self.handle.before
692 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800693 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400694 except pexpect.EOF:
695 main.log.error(self.name + ": EOF exception found")
696 main.log.error(self.name + ": " + self.handle.before)
697 main.cleanup()
698 main.exit()
699 except:
700 main.log.info(self.name+" ::::::")
701 main.log.error( traceback.print_exc())
702 main.log.info(self.name+" ::::::")
703 main.cleanup()
704 main.exit()
705
706 def get_role(self, device_id):
707 '''
708 Given the a string containing the json representation of the "roles" cli command and a
709 partial or whole device id, returns a json object containing the
710 roles output for the first device whose id contains "device_id"
711
712 Returns:
713 Dict of the role assignments for the given device or
714 None if not match
715 '''
716 try:
717 import json
718 if device_id == None:
719 return None
720 else:
721 raw_roles = self.roles()
722 roles_json = json.loads(raw_roles)
723 #search json for the device with id then return the device
724 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400725 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400726 if str(device_id) in device['id']:
727 return device
728 return None
andrewonlab7c211572014-10-15 16:45:20 -0400729
andrewonlab86dc3082014-10-13 18:18:38 -0400730 except pexpect.EOF:
731 main.log.error(self.name + ": EOF exception found")
732 main.log.error(self.name + ": " + self.handle.before)
733 main.cleanup()
734 main.exit()
735 except:
736 main.log.info(self.name+" ::::::")
737 main.log.error( traceback.print_exc())
738 main.log.info(self.name+" ::::::")
739 main.cleanup()
740 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800741
742 def roles_not_null(self):
743 '''
744 Iterates through each device and checks if there is a master assigned
745 Returns: main.TRUE if each device has a master
746 main.FALSE any device has no master
747 '''
748 try:
749 import json
750 raw_roles = self.roles()
751 roles_json = json.loads(raw_roles)
752 #search json for the device with id then return the device
753 for device in roles_json:
754 #print device
755 if device['master'] == "none":
756 main.log.warn("Device has no master: " + str(device) )
757 return main.FALSE
758 return main.TRUE
759
760 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()
771
772
andrewonlab3e15ead2014-10-15 14:21:34 -0400773 def paths(self, src_id, dst_id):
774 '''
775 Returns string of paths, and the cost.
776 Issues command: onos:paths <src> <dst>
777 '''
778 try:
779 self.handle.sendline("")
780 self.handle.expect("onos>")
781
782 self.handle.sendline("onos:paths "+
783 str(src_id) + " " + str(dst_id))
784 i = self.handle.expect([
785 "Error",
786 "onos>"])
787
788 self.handle.sendline("")
789 self.handle.expect("onos>")
790
791 handle = self.handle.before
792
793 if i == 0:
794 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400795 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400796 else:
797 path = handle.split(";")[0]
798 cost = handle.split(";")[1]
799 return (path, cost)
800
801 except pexpect.EOF:
802 main.log.error(self.name + ": EOF exception found")
803 main.log.error(self.name + ": " + self.handle.before)
804 main.cleanup()
805 main.exit()
806 except:
807 main.log.info(self.name+" ::::::")
808 main.log.error( traceback.print_exc())
809 main.log.info(self.name+" ::::::")
810 main.cleanup()
811 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800812
813 def hosts(self, json_format=True):
Jon Hall42db6dc2014-10-24 19:03:48 -0400814 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800815 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400816 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800817 * json_format - boolean indicating if you want output in json
Jon Hall42db6dc2014-10-24 19:03:48 -0400818 '''
819 try:
820 self.handle.sendline("")
821 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800822
Jon Hall42db6dc2014-10-24 19:03:48 -0400823 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800824 self.handle.sendline("hosts -j")
825 self.handle.expect("hosts -j")
826 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400827 handle = self.handle.before
828 '''
829 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
830 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
831 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Hallffb386d2014-11-21 13:43:38 -0800832 So we take off that escape sequence using
Jon Hall42db6dc2014-10-24 19:03:48 -0400833 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800834 handle1 = ansi_escape.sub('', handle)
Jon Hall42db6dc2014-10-24 19:03:48 -0400835 '''
836 #print "repr(handle) =", repr(handle)
837 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
838 handle1 = ansi_escape.sub('', handle)
839 #print "repr(handle1) = ", repr(handle1)
840 return handle1
841 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800842 self.handle.sendline("hosts")
843 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400844 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400845 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400846 return handle
847 except pexpect.EOF:
848 main.log.error(self.name + ": EOF exception found")
849 main.log.error(self.name + ": " + self.handle.before)
850 main.cleanup()
851 main.exit()
852 except:
853 main.log.info(self.name+" ::::::")
854 main.log.error( traceback.print_exc())
855 main.log.info(self.name+" ::::::")
856 main.cleanup()
857 main.exit()
858
859 def get_host(self, mac):
860 '''
861 Return the first host from the hosts api whose 'id' contains 'mac'
862 Note: mac must be a colon seperated mac address, but could be a partial mac address
863 Return None if there is no match
864 '''
865 import json
866 try:
867 if mac == None:
868 return None
869 else:
870 mac = mac
871 raw_hosts = self.hosts()
872 hosts_json = json.loads(raw_hosts)
873 #search json for the host with mac then return the device
874 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400875 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400876 if mac in host['id']:
877 return host
878 return None
879 except pexpect.EOF:
880 main.log.error(self.name + ": EOF exception found")
881 main.log.error(self.name + ": " + self.handle.before)
882 main.cleanup()
883 main.exit()
884 except:
885 main.log.info(self.name+" ::::::")
886 main.log.error( traceback.print_exc())
887 main.log.info(self.name+" ::::::")
888 main.cleanup()
889 main.exit()
890
andrewonlab3f0a4af2014-10-17 12:25:14 -0400891
892 def get_hosts_id(self, host_list):
893 '''
894 Obtain list of hosts
895 Issues command: 'onos> hosts'
896
897 Required:
898 * host_list: List of hosts obtained by Mininet
899 IMPORTANT:
900 This function assumes that you started your
901 topology with the option '--mac'.
902 Furthermore, it assumes that value of VLAN is '-1'
903 Description:
904 Converts mininet hosts (h1, h2, h3...) into
905 ONOS format (00:00:00:00:00:01/-1 , ...)
906 '''
907
908 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400909 onos_host_list = []
910
911 for host in host_list:
912 host = host.replace("h", "")
913 host_hex = hex(int(host)).zfill(12)
914 host_hex = str(host_hex).replace('x','0')
915 i = iter(str(host_hex))
916 host_hex = ":".join(a+b for a,b in zip(i,i))
917 host_hex = host_hex + "/-1"
918 onos_host_list.append(host_hex)
919
920 return onos_host_list
921
922 except pexpect.EOF:
923 main.log.error(self.name + ": EOF exception found")
924 main.log.error(self.name + ": " + self.handle.before)
925 main.cleanup()
926 main.exit()
927 except:
928 main.log.info(self.name+" ::::::")
929 main.log.error( traceback.print_exc())
930 main.log.info(self.name+" ::::::")
931 main.cleanup()
932 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400933
andrewonlabe6745342014-10-17 14:29:13 -0400934 def add_host_intent(self, host_id_one, host_id_two):
935 '''
936 Required:
937 * host_id_one: ONOS host id for host1
938 * host_id_two: ONOS host id for host2
939 Description:
940 Adds a host-to-host intent (bidrectional) by
Jon Hallb1290e82014-11-18 16:17:48 -0500941 specifying the two hosts.
andrewonlabe6745342014-10-17 14:29:13 -0400942 '''
943 try:
944 self.handle.sendline("")
945 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500946
andrewonlabe6745342014-10-17 14:29:13 -0400947 self.handle.sendline("add-host-intent "+
948 str(host_id_one) + " " + str(host_id_two))
949 self.handle.expect("onos>")
950
andrewonlabe6745342014-10-17 14:29:13 -0400951 handle = self.handle.before
Jon Hallffb386d2014-11-21 13:43:38 -0800952 #print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400953
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800954 main.log.info("Host intent installed between "+
andrewonlabe6745342014-10-17 14:29:13 -0400955 str(host_id_one) + " and " + str(host_id_two))
956
957 return handle
Jon Hallb1290e82014-11-18 16:17:48 -0500958
andrewonlabe6745342014-10-17 14:29:13 -0400959 except pexpect.EOF:
960 main.log.error(self.name + ": EOF exception found")
961 main.log.error(self.name + ": " + self.handle.before)
962 main.cleanup()
963 main.exit()
964 except:
965 main.log.info(self.name+" ::::::")
966 main.log.error( traceback.print_exc())
967 main.log.info(self.name+" ::::::")
968 main.cleanup()
969 main.exit()
970
andrewonlab7b31d232014-10-24 13:31:47 -0400971 def add_optical_intent(self, ingress_device, egress_device):
972 '''
973 Required:
974 * ingress_device: device id of ingress device
975 * egress_device: device id of egress device
976 Optional:
977 TODO: Still needs to be implemented via dev side
978 '''
979 try:
980 self.handle.sendline("add-optical-intent "+
981 str(ingress_device) + " " + str(egress_device))
982 self.handle.expect("add-optical-intent")
983 i = self.handle.expect([
984 "Error",
985 "onos>"])
986
987 handle = self.handle.before
988
989 #If error, return error message
990 if i == 0:
991 return handle
992 else:
993 return main.TRUE
994
995 except pexpect.EOF:
996 main.log.error(self.name + ": EOF exception found")
997 main.log.error(self.name + ": " + self.handle.before)
998 main.cleanup()
999 main.exit()
1000 except:
1001 main.log.info(self.name+" ::::::")
1002 main.log.error( traceback.print_exc())
1003 main.log.info(self.name+" ::::::")
1004 main.cleanup()
1005 main.exit()
1006
andrewonlab36af3822014-11-18 17:48:18 -05001007 def add_point_intent(self, ingress_device, egress_device,
1008 port_ingress="", port_egress="", ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -05001009 ethDst="", bandwidth="", lambda_alloc=False,
1010 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -04001011 '''
1012 Required:
1013 * ingress_device: device id of ingress device
1014 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001015 Optional:
1016 * ethType: specify ethType
1017 * ethSrc: specify ethSrc (i.e. src mac addr)
1018 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001019 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -05001020 * lambda_alloc: if True, intent will allocate lambda
1021 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -05001022 * ipProto: specify ip protocol
1023 * ipSrc: specify ip source address
1024 * ipDst: specify ip destination address
1025 * tcpSrc: specify tcp source port
1026 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001027 Description:
1028 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -04001029 specifying device id's and optional fields
1030
andrewonlab4dbb4d82014-10-17 18:22:31 -04001031 NOTE: This function may change depending on the
1032 options developers provide for point-to-point
1033 intent via cli
1034 '''
1035 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001036 cmd = ""
1037
1038 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001039 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001040 and not bandwidth and not lambda_alloc \
1041 and not ipProto and not ipSrc and not ipDst \
1042 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001043 cmd = "add-point-intent"
1044
1045
andrewonlab289e4b72014-10-21 21:24:18 -04001046 else:
andrewonlab36af3822014-11-18 17:48:18 -05001047 cmd = "add-point-intent"
andrewonlab9a130be2014-10-22 12:44:56 -04001048
andrewonlab0c0a6772014-10-22 12:31:18 -04001049 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001050 cmd += " --ethType " + str(ethType)
1051 if ethSrc:
1052 cmd += " --ethSrc " + str(ethSrc)
1053 if ethDst:
1054 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001055 if bandwidth:
1056 cmd += " --bandwidth " + str(bandwidth)
1057 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001058 cmd += " --lambda "
1059 if ipProto:
1060 cmd += " --ipProto " + str(ipProto)
1061 if ipSrc:
1062 cmd += " --ipSrc " + str(ipSrc)
1063 if ipDst:
1064 cmd += " --ipDst " + str(ipDst)
1065 if tcpSrc:
1066 cmd += " --tcpSrc " + str(tcpSrc)
1067 if tcpDst:
1068 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001069
andrewonlab36af3822014-11-18 17:48:18 -05001070 #Check whether the user appended the port
1071 #or provided it as an input
1072 if "/" in ingress_device:
1073 cmd += " "+str(ingress_device)
1074 else:
1075 if not port_ingress:
1076 main.log.error("You must specify "+
1077 "the ingress port")
1078 #TODO: perhaps more meaningful return
1079 return main.FALSE
1080
1081 cmd += " "+ \
1082 str(ingress_device) + "/" +\
1083 str(port_ingress) + " "
1084
1085 if "/" in egress_device:
1086 cmd += " "+str(egress_device)
1087 else:
1088 if not port_egress:
1089 main.log.error("You must specify "+
1090 "the egress port")
1091 return main.FALSE
1092
1093 cmd += " "+\
1094 str(egress_device) + "/" +\
1095 str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001096
andrewonlab289e4b72014-10-21 21:24:18 -04001097 self.handle.sendline(cmd)
andrewonlab36af3822014-11-18 17:48:18 -05001098
1099 main.log.info(cmd + " sent")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001100 i = self.handle.expect([
1101 "Error",
1102 "onos>"])
andrewonlab4dbb4d82014-10-17 18:22:31 -04001103
1104 if i == 0:
1105 main.log.error("Error in adding point-to-point intent")
Jon Hall47a93fb2015-01-06 16:46:06 -08001106 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001107 else:
1108 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001109
andrewonlab4dbb4d82014-10-17 18:22:31 -04001110 except pexpect.EOF:
1111 main.log.error(self.name + ": EOF exception found")
1112 main.log.error(self.name + ": " + self.handle.before)
1113 main.cleanup()
1114 main.exit()
1115 except:
1116 main.log.info(self.name+" ::::::")
1117 main.log.error( traceback.print_exc())
1118 main.log.info(self.name+" ::::::")
1119 main.cleanup()
1120 main.exit()
1121
shahshreyad0c80432014-12-04 16:56:05 -08001122
1123 def add_multipoint_to_singlepoint_intent(self, ingress_device1, ingress_device2,egress_device,
1124 port_ingress="", port_egress="", ethType="", ethSrc="",
1125 ethDst="", bandwidth="", lambda_alloc=False,
1126 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="", setEthDst=""):
1127 '''
1128 Note:
1129 This function assumes that there would be 2 ingress devices and one egress device
1130 For more number of ingress devices, this function needs to be modified
1131 Required:
1132 * ingress_device1: device id of ingress device1
1133 * ingress_device2: device id of ingress device2
1134 * egress_device: device id of egress device
1135 Optional:
1136 * ethType: specify ethType
1137 * ethSrc: specify ethSrc (i.e. src mac addr)
1138 * ethDst: specify ethDst (i.e. dst mac addr)
1139 * bandwidth: specify bandwidth capacity of link
1140 * lambda_alloc: if True, intent will allocate lambda
1141 for the specified intent
1142 * ipProto: specify ip protocol
1143 * ipSrc: specify ip source address
1144 * ipDst: specify ip destination address
1145 * tcpSrc: specify tcp source port
1146 * tcpDst: specify tcp destination port
1147 * setEthSrc: action to Rewrite Source MAC Address
1148 * setEthDst: action to Rewrite Destination MAC Address
1149 Description:
1150 Adds a multipoint-to-singlepoint intent (uni-directional) by
1151 specifying device id's and optional fields
1152
1153 NOTE: This function may change depending on the
1154 options developers provide for multipointpoint-to-singlepoint
1155 intent via cli
1156 '''
1157 try:
1158 cmd = ""
1159
1160 #If there are no optional arguments
1161 if not ethType and not ethSrc and not ethDst\
1162 and not bandwidth and not lambda_alloc \
1163 and not ipProto and not ipSrc and not ipDst \
1164 and not tcpSrc and not tcpDst and not setEthSrc and not setEthDst:
1165 cmd = "add-multi-to-single-intent"
1166
1167
1168 else:
1169 cmd = "add-multi-to-single-intent"
1170
1171 if ethType:
1172 cmd += " --ethType " + str(ethType)
1173 if ethSrc:
1174 cmd += " --ethSrc " + str(ethSrc)
1175 if ethDst:
1176 cmd += " --ethDst " + str(ethDst)
1177 if bandwidth:
1178 cmd += " --bandwidth " + str(bandwidth)
1179 if lambda_alloc:
1180 cmd += " --lambda "
1181 if ipProto:
1182 cmd += " --ipProto " + str(ipProto)
1183 if ipSrc:
1184 cmd += " --ipSrc " + str(ipSrc)
1185 if ipDst:
1186 cmd += " --ipDst " + str(ipDst)
1187 if tcpSrc:
1188 cmd += " --tcpSrc " + str(tcpSrc)
1189 if tcpDst:
1190 cmd += " --tcpDst " + str(tcpDst)
1191 if setEthSrc:
1192 cmd += " --setEthSrc "+ str(setEthSrc)
1193 if setEthDst:
1194 cmd += " --setEthDst "+ str(setEthDst)
1195
1196 #Check whether the user appended the port
1197 #or provided it as an input
1198 if "/" in ingress_device1:
1199 cmd += " "+str(ingress_device1)
1200 else:
1201 if not port_ingress1:
1202 main.log.error("You must specify "+
1203 "the ingress port1")
1204 #TODO: perhaps more meaningful return
1205 return main.FALSE
1206
1207 cmd += " "+ \
1208 str(ingress_device1) + "/" +\
1209 str(port_ingress1) + " "
1210
1211 if "/" in ingress_device2:
1212 cmd += " "+str(ingress_device2)
1213 else:
1214 if not port_ingress2:
1215 main.log.error("You must specify "+
1216 "the ingress port2")
1217 #TODO: perhaps more meaningful return
1218 return main.FALSE
1219
1220 cmd += " "+ \
1221 str(ingress_device2) + "/" +\
1222 str(port_ingress2) + " "
1223
1224 if "/" in egress_device:
1225 cmd += " "+str(egress_device)
1226 else:
1227 if not port_egress:
1228 main.log.error("You must specify "+
1229 "the egress port")
1230 return main.FALSE
1231
1232 cmd += " "+\
1233 str(egress_device) + "/" +\
1234 str(port_egress)
1235 print "cmd= ",cmd
1236 self.handle.sendline(cmd)
1237
1238 main.log.info(cmd + " sent")
1239 i = self.handle.expect([
1240 "Error",
1241 "onos>"])
1242
1243 if i == 0:
1244 main.log.error("Error in adding point-to-point intent")
1245 return self.handle
1246 else:
1247 return main.TRUE
1248
1249 except pexpect.EOF:
1250 main.log.error(self.name + ": EOF exception found")
1251 main.log.error(self.name + ": " + self.handle.before)
1252 main.cleanup()
1253 main.exit()
1254 except:
1255 main.log.info(self.name+" ::::::")
1256 main.log.error( traceback.print_exc())
1257 main.log.info(self.name+" ::::::")
1258 main.cleanup()
1259 main.exit()
1260
1261
andrewonlab9a50dfe2014-10-17 17:22:31 -04001262 def remove_intent(self, intent_id):
1263 '''
1264 Remove intent for specified intent id
1265 '''
1266 try:
1267 self.handle.sendline("")
1268 self.handle.expect("onos>")
1269
1270 self.handle.sendline("remove-intent "+str(intent_id))
1271 i = self.handle.expect([
1272 "Error",
1273 "onos>"])
1274
1275 handle = self.handle.before
1276
1277 if i == 0:
1278 main.log.error("Error in removing intent")
1279 return handle
1280 else:
1281 return handle
1282
1283 except pexpect.EOF:
1284 main.log.error(self.name + ": EOF exception found")
1285 main.log.error(self.name + ": " + self.handle.before)
1286 main.cleanup()
1287 main.exit()
1288 except:
1289 main.log.info(self.name+" ::::::")
1290 main.log.error( traceback.print_exc())
1291 main.log.info(self.name+" ::::::")
1292 main.cleanup()
1293 main.exit()
1294
pingping-lindabe7972014-11-17 19:29:44 -08001295 # This method should be used after installing application: onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001296 def routes(self, json_format=False):
1297 '''
1298 Optional:
1299 * json_format: enable output formatting in json
1300 Description:
1301 Obtain all routes in the system
1302 '''
1303 try:
1304 if json_format:
1305 self.handle.sendline("routes -j")
1306 self.handle.expect("routes -j")
1307 self.handle.expect("onos>")
1308 handle_tmp = self.handle.before
1309
1310 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1311 handle = ansi_escape.sub('', handle_tmp)
1312
1313 else:
1314 self.handle.sendline("")
1315 self.handle.expect("onos>")
1316
1317 self.handle.sendline("routes")
1318 self.handle.expect("onos>")
1319 handle = self.handle.before
1320
1321 return handle
1322
1323 except pexpect.EOF:
1324 main.log.error(self.name + ": EOF exception found")
1325 main.log.error(self.name + ": " + self.handle.before)
1326 main.cleanup()
1327 main.exit()
1328 except:
1329 main.log.info(self.name + " ::::::")
1330 main.log.error(traceback.print_exc())
1331 main.log.info(self.name + " ::::::")
1332 main.cleanup()
1333 main.exit()
1334
Jon Hallffb386d2014-11-21 13:43:38 -08001335 def intents(self, json_format = True):
andrewonlabe6745342014-10-17 14:29:13 -04001336 '''
andrewonlab377693f2014-10-21 16:00:30 -04001337 Optional:
1338 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001339 Description:
1340 Obtain intents currently installed
1341 '''
1342 try:
andrewonlab377693f2014-10-21 16:00:30 -04001343 if json_format:
1344 self.handle.sendline("intents -j")
1345 self.handle.expect("intents -j")
1346 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001347 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001348
1349 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1350 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001351 else:
1352 self.handle.sendline("")
1353 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001354
andrewonlab377693f2014-10-21 16:00:30 -04001355 self.handle.sendline("intents")
1356 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001357 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001358
1359 return handle
1360
1361 except pexpect.EOF:
1362 main.log.error(self.name + ": EOF exception found")
1363 main.log.error(self.name + ": " + self.handle.before)
1364 main.cleanup()
1365 main.exit()
1366 except:
1367 main.log.info(self.name+" ::::::")
1368 main.log.error( traceback.print_exc())
1369 main.log.info(self.name+" ::::::")
1370 main.cleanup()
1371 main.exit()
1372
Jon Hallffb386d2014-11-21 13:43:38 -08001373 def flows(self, json_format = True):
Shreya Shah0f01c812014-10-26 20:15:28 -04001374 '''
1375 Optional:
1376 * json_format: enable output formatting in json
1377 Description:
1378 Obtain flows currently installed
1379 '''
1380 try:
1381 if json_format:
1382 self.handle.sendline("flows -j")
1383 self.handle.expect("flows -j")
1384 self.handle.expect("onos>")
1385 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001386 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1387 handle = ansi_escape.sub('', handle)
Shreya Shah0f01c812014-10-26 20:15:28 -04001388
1389 else:
1390 self.handle.sendline("")
1391 self.handle.expect("onos>")
1392 self.handle.sendline("flows")
1393 self.handle.expect("onos>")
1394 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001395 if re.search("Error\sexecuting\scommand:", handle):
1396 main.log.error(self.name + ".flows() response: " + str(handle))
Shreya Shah0f01c812014-10-26 20:15:28 -04001397
1398 return handle
1399
1400 except pexpect.EOF:
1401 main.log.error(self.name + ": EOF exception found")
1402 main.log.error(self.name + ": " + self.handle.before)
1403 main.cleanup()
1404 main.exit()
1405 except:
1406 main.log.info(self.name+" ::::::")
1407 main.log.error( traceback.print_exc())
1408 main.log.info(self.name+" ::::::")
1409 main.cleanup()
1410 main.exit()
1411
andrewonlabb66dfa12014-12-02 15:51:10 -05001412 def push_test_intents(self, dpid_src, dpid_dst, num_intents,
1413 num_mult="", app_id="", report=True):
andrewonlab87852b02014-11-19 18:44:19 -05001414 '''
1415 Description:
1416 Push a number of intents in a batch format to
1417 a specific point-to-point intent definition
1418 Required:
1419 * dpid_src: specify source dpid
1420 * dpid_dst: specify destination dpid
1421 * num_intents: specify number of intents to push
1422 Optional:
andrewonlabb66dfa12014-12-02 15:51:10 -05001423 * num_mult: number multiplier for multiplying
1424 the number of intents specified
1425 * app_id: specify the application id init to further
1426 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001427 * report: default True, returns latency information
1428 '''
1429 try:
1430 cmd = "push-test-intents "+\
1431 str(dpid_src)+" "+str(dpid_dst)+" "+\
1432 str(num_intents)
andrewonlabb66dfa12014-12-02 15:51:10 -05001433
1434 if num_mult:
1435 cmd += " " + str(num_mult)
andrewonlab042b3912014-12-10 16:40:50 -05001436 #If app id is specified, then num_mult
1437 #must exist because of the way this command
1438 #takes in arguments
andrewonlabb66dfa12014-12-02 15:51:10 -05001439 if app_id:
1440 cmd += " " + str(app_id)
1441
andrewonlab87852b02014-11-19 18:44:19 -05001442 self.handle.sendline(cmd)
1443 self.handle.expect(cmd)
1444 self.handle.expect("onos>")
1445
1446 handle = self.handle.before
1447
1448 #Some color thing that we want to escape
1449 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1450 handle = ansi_escape.sub('', handle)
1451
1452 if report:
andrewonlabb66dfa12014-12-02 15:51:10 -05001453 lat_result = []
andrewonlab87852b02014-11-19 18:44:19 -05001454 main.log.info(handle)
andrewonlabb66dfa12014-12-02 15:51:10 -05001455 #Split result by newline
1456 newline = handle.split("\r\r\n")
1457 #Ignore the first object of list, which is empty
1458 newline = newline[1:]
1459 #Some sloppy parsing method to get the latency
1460 for result in newline:
1461 result = result.split(": ")
1462 #Append the first result of second parse
1463 lat_result.append(result[1].split(" ")[0])
1464
andrewonlab042b3912014-12-10 16:40:50 -05001465 main.log.info(lat_result)
andrewonlabb66dfa12014-12-02 15:51:10 -05001466 return lat_result
andrewonlab87852b02014-11-19 18:44:19 -05001467 else:
1468 return main.TRUE
1469
1470 except pexpect.EOF:
1471 main.log.error(self.name + ": EOF exception found")
1472 main.log.error(self.name + ": " + self.handle.before)
1473 main.cleanup()
1474 main.exit()
1475 except:
1476 main.log.info(self.name+" ::::::")
1477 main.log.error( traceback.print_exc())
1478 main.log.info(self.name+" ::::::")
1479 main.cleanup()
1480 main.exit()
1481
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001482 def intents_events_metrics(self, json_format=True):
1483 '''
1484 Description:Returns topology metrics
1485 Optional:
1486 * json_format: enable json formatting of output
1487 '''
1488 try:
1489 if json_format:
1490 self.handle.sendline("intents-events-metrics -j")
1491 self.handle.expect("intents-events-metrics -j")
1492 self.handle.expect("onos>")
1493
1494 handle = self.handle.before
1495
1496 #Some color thing that we want to escape
1497 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1498 handle = ansi_escape.sub('', handle)
1499
1500 else:
1501 self.handle.sendline("intents-events-metrics")
1502 self.handle.expect("intents-events-metrics")
1503 self.handle.expect("onos>")
1504
1505 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001506
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001507 return handle
1508
1509 except pexpect.EOF:
1510 main.log.error(self.name + ": EOF exception found")
1511 main.log.error(self.name + ": " + self.handle.before)
1512 main.cleanup()
1513 main.exit()
1514 except:
1515 main.log.info(self.name+" ::::::")
1516 main.log.error( traceback.print_exc())
1517 main.log.info(self.name+" ::::::")
1518 main.cleanup()
1519 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001520
andrewonlab867212a2014-10-22 20:13:38 -04001521 def topology_events_metrics(self, json_format=True):
1522 '''
1523 Description:Returns topology metrics
1524 Optional:
1525 * json_format: enable json formatting of output
1526 '''
1527 try:
1528 if json_format:
1529 self.handle.sendline("topology-events-metrics -j")
1530 self.handle.expect("topology-events-metrics -j")
1531 self.handle.expect("onos>")
1532
1533 handle = self.handle.before
1534
1535 #Some color thing that we want to escape
1536 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1537 handle = ansi_escape.sub('', handle)
1538
1539 else:
1540 self.handle.sendline("topology-events-metrics")
1541 self.handle.expect("topology-events-metrics")
1542 self.handle.expect("onos>")
1543
1544 handle = self.handle.before
1545
1546 return handle
1547
1548 except pexpect.EOF:
1549 main.log.error(self.name + ": EOF exception found")
1550 main.log.error(self.name + ": " + self.handle.before)
1551 main.cleanup()
1552 main.exit()
1553 except:
1554 main.log.info(self.name+" ::::::")
1555 main.log.error( traceback.print_exc())
1556 main.log.info(self.name+" ::::::")
1557 main.cleanup()
1558 main.exit()
1559
andrewonlab3e15ead2014-10-15 14:21:34 -04001560 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001561 #Wrapper functions use existing driver
1562 #functions and extends their use case.
1563 #For example, we may use the output of
1564 #a normal driver function, and parse it
1565 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001566
andrewonlab9a50dfe2014-10-17 17:22:31 -04001567 def get_all_intents_id(self):
1568 '''
1569 Description:
1570 Obtain all intent id's in a list
1571 '''
1572 try:
1573 #Obtain output of intents function
1574 intents_str = self.intents()
1575 all_intent_list = []
1576 intent_id_list = []
1577
1578 #Parse the intents output for ID's
1579 intents_list = [s.strip() for s in intents_str.splitlines()]
1580 for intents in intents_list:
1581 if "onos>" in intents:
1582 continue
1583 elif "intents" in intents:
1584 continue
1585 else:
1586 line_list = intents.split(" ")
1587 all_intent_list.append(line_list[0])
1588
1589 all_intent_list = all_intent_list[1:-2]
1590
1591 for intents in all_intent_list:
1592 if not intents:
1593 continue
1594 else:
1595 intent_id_list.append(intents)
1596
1597 return intent_id_list
1598
1599 except pexpect.EOF:
1600 main.log.error(self.name + ": EOF exception found")
1601 main.log.error(self.name + ": " + self.handle.before)
1602 main.cleanup()
1603 main.exit()
1604 except:
1605 main.log.info(self.name+" ::::::")
1606 main.log.error( traceback.print_exc())
1607 main.log.info(self.name+" ::::::")
1608 main.cleanup()
1609 main.exit()
1610
andrewonlab7e4d2d32014-10-15 13:23:21 -04001611 def get_all_devices_id(self):
1612 '''
1613 Use 'devices' function to obtain list of all devices
1614 and parse the result to obtain a list of all device
1615 id's. Returns this list. Returns empty list if no
1616 devices exist
1617 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001618
1619 This function may be useful if you are not sure of the
1620 device id, and wish to execute other commands using
1621 the ids. By obtaining the list of device ids on the fly,
1622 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001623 '''
1624 try:
1625 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001626 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001627 id_list = []
1628
1629 if not devices_str:
1630 main.log.info("There are no devices to get id from")
1631 return id_list
1632
1633 #Split the string into list by comma
1634 device_list = devices_str.split(",")
1635 #Get temporary list of all arguments with string 'id='
1636 temp_list = [dev for dev in device_list if "id=" in dev]
1637 #Split list further into arguments before and after string
1638 # 'id='. Get the latter portion (the actual device id) and
1639 # append to id_list
1640 for arg in temp_list:
1641 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001642 return id_list
1643
1644 except pexpect.EOF:
1645 main.log.error(self.name + ": EOF exception found")
1646 main.log.error(self.name + ": " + self.handle.before)
1647 main.cleanup()
1648 main.exit()
1649 except:
1650 main.log.info(self.name+" ::::::")
1651 main.log.error( traceback.print_exc())
1652 main.log.info(self.name+" ::::::")
1653 main.cleanup()
1654 main.exit()
1655
andrewonlab7c211572014-10-15 16:45:20 -04001656 def get_all_nodes_id(self):
1657 '''
1658 Uses 'nodes' function to obtain list of all nodes
1659 and parse the result of nodes to obtain just the
1660 node id's.
1661 Returns:
1662 list of node id's
1663 '''
1664 try:
1665 nodes_str = self.nodes()
1666 id_list = []
1667
1668 if not nodes_str:
1669 main.log.info("There are no nodes to get id from")
1670 return id_list
1671
1672 #Sample nodes_str output
1673 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1674
1675 #Split the string into list by comma
1676 nodes_list = nodes_str.split(",")
1677 temp_list = [node for node in nodes_list if "id=" in node]
1678 for arg in temp_list:
1679 id_list.append(arg.split("id=")[1])
1680
1681 return id_list
1682
1683 except pexpect.EOF:
1684 main.log.error(self.name + ": EOF exception found")
1685 main.log.error(self.name + ": " + self.handle.before)
1686 main.cleanup()
1687 main.exit()
1688 except:
1689 main.log.info(self.name+" ::::::")
1690 main.log.error( traceback.print_exc())
1691 main.log.info(self.name+" ::::::")
1692 main.cleanup()
1693 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001694
Jon Halla91c4dc2014-10-22 12:57:04 -04001695 def get_device(self, dpid=None):
1696 '''
1697 Return the first device from the devices api whose 'id' contains 'dpid'
1698 Return None if there is no match
1699 '''
1700 import json
1701 try:
1702 if dpid == None:
1703 return None
1704 else:
1705 dpid = dpid.replace(':', '')
1706 raw_devices = self.devices()
1707 devices_json = json.loads(raw_devices)
1708 #search json for the device with dpid then return the device
1709 for device in devices_json:
1710 #print "%s in %s?" % (dpid, device['id'])
1711 if dpid in device['id']:
1712 return device
1713 return None
1714 except pexpect.EOF:
1715 main.log.error(self.name + ": EOF exception found")
1716 main.log.error(self.name + ": " + self.handle.before)
1717 main.cleanup()
1718 main.exit()
1719 except:
1720 main.log.info(self.name+" ::::::")
1721 main.log.error( traceback.print_exc())
1722 main.log.info(self.name+" ::::::")
1723 main.cleanup()
1724 main.exit()
1725
Jon Hall42db6dc2014-10-24 19:03:48 -04001726 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1727 '''
1728 Checks the number of swithes & links that ONOS sees against the
1729 supplied values. By default this will report to main.log, but the
1730 log level can be specifid.
1731
1732 Params: ip = ip used for the onos cli
1733 numoswitch = expected number of switches
1734 numlink = expected number of links
1735 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1736
1737
1738 log_level can
1739
1740 Returns: main.TRUE if the number of switchs and links are correct,
1741 main.FALSE if the numer of switches and links is incorrect,
1742 and main.ERROR otherwise
1743 '''
1744
1745 try:
1746 topology = self.get_topology(ip)
1747 if topology == {}:
1748 return main.ERROR
1749 output = ""
1750 #Is the number of switches is what we expected
1751 devices = topology.get('devices',False)
1752 links = topology.get('links',False)
1753 if devices == False or links == False:
1754 return main.ERROR
1755 switch_check = ( int(devices) == int(numoswitch) )
1756 #Is the number of links is what we expected
1757 link_check = ( int(links) == int(numolink) )
1758 if (switch_check and link_check):
1759 #We expected the correct numbers
1760 output = output + "The number of links and switches match "\
1761 + "what was expected"
1762 result = main.TRUE
1763 else:
1764 output = output + \
1765 "The number of links and switches does not match what was expected"
1766 result = main.FALSE
1767 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1768 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1769 if log_level == "report":
1770 main.log.report(output)
1771 elif log_level == "warn":
1772 main.log.warn(output)
1773 else:
1774 main.log.info(output)
1775 return result
1776 except pexpect.EOF:
1777 main.log.error(self.name + ": EOF exception found")
1778 main.log.error(self.name + ": " + self.handle.before)
1779 main.cleanup()
1780 main.exit()
1781 except:
1782 main.log.info(self.name+" ::::::")
1783 main.log.error( traceback.print_exc())
1784 main.log.info(self.name+" ::::::")
1785 main.cleanup()
1786 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001787
1788 def device_role(self, device_id, onos_node, role="master"):
1789 '''
1790 Calls the device-role cli command.
1791 device_id must be the id of a device as seen in the onos devices command
1792 onos_node is the ip of one of the onos nodes in the cluster
1793 role must be either master, standby, or none
1794
Jon Hall94fd0472014-12-08 11:52:42 -08001795 Returns main.TRUE or main.FALSE based on argument verification.
Jon Hall983a1702014-10-28 18:44:22 -04001796 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001797 support that output
1798 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001799 try:
Jon Hall983a1702014-10-28 18:44:22 -04001800 #print "beginning device_role... \n\tdevice_id:" + device_id
1801 #print "\tonos_node: " + onos_node
1802 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001803 if role.lower() == "master" or \
1804 role.lower() == "standby" or \
1805 role.lower() == "none":
1806 self.handle.sendline("")
1807 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001808 self.handle.sendline("device-role " +
1809 str(device_id) + " " +
1810 str(onos_node) + " " +
1811 str(role))
1812 i= self.handle.expect(["Error","onos>"])
1813 if i == 0:
1814 output = str(self.handle.before)
1815 self.handle.expect("onos>")
1816 output = output + str(self.handle.before)
1817 main.log.error(self.name + ": " +
1818 output + '\033[0m')#end color output to escape any colours from the cli
1819 return main.ERROR
1820 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001821 self.handle.expect("onos>")
1822 return main.TRUE
1823 else:
1824 return main.FALSE
1825
1826 except pexpect.EOF:
1827 main.log.error(self.name + ": EOF exception found")
1828 main.log.error(self.name + ": " + self.handle.before)
1829 main.cleanup()
1830 main.exit()
1831 except:
1832 main.log.info(self.name+" ::::::")
1833 main.log.error( traceback.print_exc())
1834 main.log.info(self.name+" ::::::")
1835 main.cleanup()
1836 main.exit()
1837
Jon Hall73cf9cc2014-11-20 22:28:38 -08001838 def clusters(self, json_format=True):
1839 '''
1840 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001841 Optional argument:
1842 * json_format - boolean indicating if you want output in json
Jon Hall73cf9cc2014-11-20 22:28:38 -08001843 '''
1844 try:
1845 self.handle.sendline("")
1846 self.handle.expect("onos>")
1847
1848 if json_format:
1849 self.handle.sendline("clusters -j")
1850 self.handle.expect("clusters -j")
1851 self.handle.expect("onos>")
1852 handle = self.handle.before
1853 '''
1854 handle variable here contains some ANSI escape color code
1855 sequences at the end which are invisible in the print command
1856 output. To make that escape sequence visible, use repr() function.
1857 The repr(handle) output when printed shows the ANSI escape sequences.
1858 In json.loads(somestring), this somestring variable is actually
1859 repr(somestring) and json.loads would fail with the escape sequence.
1860 So we take off that escape sequence using
1861 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1862 handle1 = ansi_escape.sub('', handle)
1863 '''
1864 #print "repr(handle) =", repr(handle)
1865 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1866 handle1 = ansi_escape.sub('', handle)
1867 #print "repr(handle1) = ", repr(handle1)
1868 return handle1
1869 else:
Jon Hallffb386d2014-11-21 13:43:38 -08001870 self.handle.sendline("clusters")
Jon Hall73cf9cc2014-11-20 22:28:38 -08001871 self.handle.expect("onos>")
1872 handle = self.handle.before
1873 #print "handle =",handle
1874 return handle
1875 except pexpect.EOF:
1876 main.log.error(self.name + ": EOF exception found")
1877 main.log.error(self.name + ": " + self.handle.before)
1878 main.cleanup()
1879 main.exit()
1880 except:
1881 main.log.info(self.name+" ::::::")
1882 main.log.error( traceback.print_exc())
1883 main.log.info(self.name+" ::::::")
1884 main.cleanup()
1885 main.exit()
1886
Jon Hall94fd0472014-12-08 11:52:42 -08001887 def election_test_leader(self):
1888 '''
1889 * CLI command to get the current leader for the Election test application.
1890 #NOTE: Requires installation of the onos-app-election feature
1891 Returns: Node IP of the leader if one exists
1892 None if none exists
1893 Main.FALSE on error
1894 '''
1895 try:
1896 self.handle.sendline("election-test-leader")
1897 self.handle.expect("election-test-leader")
1898 self.handle.expect("onos>")
1899 response = self.handle.before
1900 #Leader
1901 node_search = re.search("The\scurrent\sleader\sfor\sthe\sElection\sapp\sis\s(?P<node>.+)\.", response)
1902 if node_search:
1903 node = node_search.group('node')
Jon Hall669173b2014-12-17 11:36:30 -08001904 main.log.info("Election-test-leader on "+str(self.name)+" found " + node + " as the leader")
Jon Hall94fd0472014-12-08 11:52:42 -08001905 return node
1906 #no leader
1907 null_search = re.search("There\sis\scurrently\sno\sleader\selected\sfor\sthe\sElection\sapp", response)
1908 if null_search:
Jon Hall669173b2014-12-17 11:36:30 -08001909 main.log.info("Election-test-leader found no leader on " + self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001910 return None
1911 #error
Jon Hall669173b2014-12-17 11:36:30 -08001912 if re.search("Command\snot\sfound", response):
1913 main.log.error("Election app is not loaded on " + self.name)
1914 return main.FALSE
1915 else:
1916 main.log.error("Error in election_test_leader: unexpected response")
1917 main.log.error( repr(response) )
1918 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001919
1920 except pexpect.EOF:
1921 main.log.error(self.name + ": EOF exception found")
1922 main.log.error(self.name + ": " + self.handle.before)
1923 main.cleanup()
1924 main.exit()
1925 except:
1926 main.log.info(self.name+" ::::::")
1927 main.log.error( traceback.print_exc())
1928 main.log.info(self.name+" ::::::")
1929 main.cleanup()
1930 main.exit()
1931
1932 def election_test_run(self):
1933 '''
1934 * CLI command to run for leadership of the Election test application.
1935 #NOTE: Requires installation of the onos-app-election feature
1936 Returns: Main.TRUE on success
1937 Main.FALSE on error
1938 '''
1939 try:
1940 self.handle.sendline("election-test-run")
1941 self.handle.expect("election-test-run")
1942 self.handle.expect("onos>")
1943 response = self.handle.before
1944 #success
1945 search = re.search("Entering\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1946 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001947 main.log.info(self.name + " entering leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001948 return main.TRUE
1949 #error
Jon Hall669173b2014-12-17 11:36:30 -08001950 if re.search("Command\snot\sfound", response):
1951 main.log.error("Election app is not loaded on " + self.name)
1952 return main.FALSE
1953 else:
1954 main.log.error("Error in election_test_run: unexpected response")
1955 main.log.error( repr(response) )
1956 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001957
1958 except pexpect.EOF:
1959 main.log.error(self.name + ": EOF exception found")
1960 main.log.error(self.name + ": " + self.handle.before)
1961 main.cleanup()
1962 main.exit()
1963 except:
1964 main.log.info(self.name+" ::::::")
1965 main.log.error( traceback.print_exc())
1966 main.log.info(self.name+" ::::::")
1967 main.cleanup()
1968 main.exit()
1969
1970 def election_test_withdraw(self):
1971 '''
1972 * CLI command to withdraw the local node from leadership election for
1973 * the Election test application.
1974 #NOTE: Requires installation of the onos-app-election feature
1975 Returns: Main.TRUE on success
1976 Main.FALSE on error
1977 '''
1978 try:
1979 self.handle.sendline("election-test-withdraw")
1980 self.handle.expect("election-test-withdraw")
1981 self.handle.expect("onos>")
1982 response = self.handle.before
1983 #success
1984 search = re.search("Withdrawing\sfrom\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1985 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001986 main.log.info(self.name + " withdrawing from leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001987 return main.TRUE
1988 #error
Jon Hall669173b2014-12-17 11:36:30 -08001989 if re.search("Command\snot\sfound", response):
1990 main.log.error("Election app is not loaded on " + self.name)
1991 return main.FALSE
1992 else:
1993 main.log.error("Error in election_test_withdraw: unexpected response")
1994 main.log.error( repr(response) )
1995 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001996
1997
1998 except pexpect.EOF:
1999 main.log.error(self.name + ": EOF exception found")
2000 main.log.error(self.name + ": " + self.handle.before)
2001 main.cleanup()
2002 main.exit()
2003 except:
2004 main.log.info(self.name+" ::::::")
2005 main.log.error( traceback.print_exc())
2006 main.log.info(self.name+" ::::::")
2007 main.cleanup()
2008 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002009
andrewonlab7e4d2d32014-10-15 13:23:21 -04002010 #***********************************
Hari Krishna416c3ca2014-12-19 15:39:31 -08002011 def getDevicePortsEnabledCount(self,dpid):
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002012 '''
2013 Get the count of all enabled ports on a particular device/switch
2014 '''
2015 try:
Hari Krishna2bbaa702014-12-19 14:46:12 -08002016 dpid = str(dpid)
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002017 self.handle.sendline("")
2018 self.handle.expect("onos>")
2019
2020 self.handle.sendline("onos:ports -e "+dpid+" | wc -l")
2021 i = self.handle.expect([
2022 "No such device",
2023 "onos>"])
2024
2025 #self.handle.sendline("")
2026 #self.handle.expect("onos>")
2027
2028 output = self.handle.before
2029
2030 if i == 0:
2031 main.log.error("Error in getting ports")
Jon Hall6a064602015-01-06 16:49:24 -08002032 return (output, "Error")
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002033 else:
2034 result = output
2035 return result
2036
2037 except pexpect.EOF:
2038 main.log.error(self.name + ": EOF exception found")
2039 main.log.error(self.name + ": " + self.handle.before)
2040 main.cleanup()
2041 main.exit()
2042 except:
2043 main.log.info(self.name+" ::::::")
2044 main.log.error( traceback.print_exc())
2045 main.log.info(self.name+" ::::::")
2046 main.cleanup()
2047 main.exit()
2048
2049 def getDeviceLinksActiveCount(self,dpid):
2050 '''
2051 Get the count of all enabled ports on a particular device/switch
2052 '''
2053 try:
Hari Krishna2bbaa702014-12-19 14:46:12 -08002054 dpid = str(dpid)
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002055 self.handle.sendline("")
2056 self.handle.expect("onos>")
2057
2058 self.handle.sendline("onos:links "+dpid+" | grep ACTIVE | wc -l")
2059 i = self.handle.expect([
2060 "No such device",
2061 "onos>"])
2062
2063 output = self.handle.before
2064
2065 if i == 0:
2066 main.log.error("Error in getting ports")
Jon Hall6a064602015-01-06 16:49:24 -08002067 return (output, "Error")
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002068 else:
2069 result = output
2070 return result
2071
2072 except pexpect.EOF:
2073 main.log.error(self.name + ": EOF exception found")
2074 main.log.error(self.name + ": " + self.handle.before)
2075 main.cleanup()
2076 main.exit()
2077 except:
2078 main.log.info(self.name+" ::::::")
2079 main.log.error( traceback.print_exc())
2080 main.log.info(self.name+" ::::::")
2081 main.cleanup()
2082 main.exit()
2083
2084 def getAllIntentIds(self):
2085 '''
2086 Return a list of all Intent IDs
2087 '''
2088 try:
2089 self.handle.sendline("")
2090 self.handle.expect("onos>")
2091
2092 self.handle.sendline("onos:intents | grep id=")
2093 i = self.handle.expect([
2094 "Error",
2095 "onos>"])
2096
2097 output = self.handle.before
2098
2099 if i == 0:
2100 main.log.error("Error in getting ports")
Jon Hall6a064602015-01-06 16:49:24 -08002101 return (output, "Error")
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002102 else:
2103 result = output
2104 return result
2105
2106 except pexpect.EOF:
2107 main.log.error(self.name + ": EOF exception found")
2108 main.log.error(self.name + ": " + self.handle.before)
2109 main.cleanup()
2110 main.exit()
2111 except:
2112 main.log.info(self.name+" ::::::")
2113 main.log.error( traceback.print_exc())
2114 main.log.info(self.name+" ::::::")
2115 main.cleanup()
2116 main.exit()