blob: 7e862721f22eeed88d1e34953562a5a789643b53 [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 '''
Jon Halle3f39ff2015-01-13 11:50:53 -0800242 Send a completely user specified string to
243 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400244 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800245
andrewonlaba18f6bf2014-10-13 19:31:54 -0400246 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
Jon Halle3f39ff2015-01-13 11:50:53 -0800253 self.handle.sendline("log:log \"Sending CLI command: '"
254 + cmd_str + "'\"")
255 self.handle.expect("onos>")
256 self.handle.sendline( cmd_str )
257 self.handle.expect( cmd_str )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400258 self.handle.expect("onos>")
259
260 handle = self.handle.before
Jon Halle3f39ff2015-01-13 11:50:53 -0800261
andrewonlaba18f6bf2014-10-13 19:31:54 -0400262 self.handle.sendline("")
263 self.handle.expect("onos>")
andrewonlaba18f6bf2014-10-13 19:31:54 -0400264
Jon Halle3f39ff2015-01-13 11:50:53 -0800265 #handle += self.handle.before
266 #handle += self.handle.after
267
268 main.log.info("Command '" + str( cmd_str ) + "' sent to "
269 + self.name + ".")
Jon Hall42db6dc2014-10-24 19:03:48 -0400270 ansi_escape = re.compile(r'\x1b[^m]*m')
271 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400272
273 return handle
274 except pexpect.EOF:
275 main.log.error(self.name + ": EOF exception found")
276 main.log.error(self.name + ": " + self.handle.before)
277 main.cleanup()
278 main.exit()
279 except:
280 main.log.info(self.name+" ::::::")
281 main.log.error( traceback.print_exc())
282 main.log.info(self.name+" ::::::")
283 main.cleanup()
284 main.exit()
285
andrewonlab95ce8322014-10-13 14:12:04 -0400286 #IMPORTANT NOTE:
287 #For all cli commands, naming convention should match
288 #the cli command replacing ':' with '_'.
289 #Ex) onos:topology > onos_topology
290 # onos:links > onos_links
291 # feature:list > feature_list
Jon Halle3f39ff2015-01-13 11:50:53 -0800292
andrewonlabc2d05aa2014-10-13 16:51:10 -0400293 def add_node(self, node_id, ONOS_ip, tcp_port=""):
294 '''
295 Adds a new cluster node by ID and address information.
296 Required:
297 * node_id
298 * ONOS_ip
299 Optional:
300 * tcp_port
301 '''
302 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800303 cmd_str = "add-node " + str(node_id) + " " +\
304 str(ONOS_ip) + " " + str(tcp_port)
305 handle = self.sendline( cmd_str )
306 if re.search("Error", handle):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400307 main.log.error("Error in adding node")
308 main.log.error(handle)
Jon Halle3f39ff2015-01-13 11:50:53 -0800309 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400310 else:
311 main.log.info("Node "+str(ONOS_ip)+" added")
312 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400313 except pexpect.EOF:
314 main.log.error(self.name + ": EOF exception found")
315 main.log.error(self.name + ": " + self.handle.before)
316 main.cleanup()
317 main.exit()
318 except:
319 main.log.info(self.name+" ::::::")
320 main.log.error( traceback.print_exc())
321 main.log.info(self.name+" ::::::")
322 main.cleanup()
323 main.exit()
324
andrewonlab86dc3082014-10-13 18:18:38 -0400325 def remove_node(self, node_id):
326 '''
327 Removes a cluster by ID
328 Issues command: 'remove-node [<node-id>]'
329 Required:
330 * node_id
331 '''
332 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400333
Jon Halle3f39ff2015-01-13 11:50:53 -0800334 cmd_str = "remove-node " + str(node_id)
335 self.sendline( cmd_str )
336 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400337
338 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800339
andrewonlab86dc3082014-10-13 18:18:38 -0400340 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:
Jon Halle3f39ff2015-01-13 11:50:53 -0800359 cmd_str = "nodes"
360 handle = self.sendline( cmd_str )
andrewonlab7c211572014-10-15 16:45:20 -0400361 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400362 except pexpect.EOF:
363 main.log.error(self.name + ": EOF exception found")
364 main.log.error(self.name + ": " + self.handle.before)
365 main.cleanup()
366 main.exit()
367 except:
368 main.log.info(self.name+" ::::::")
369 main.log.error( traceback.print_exc())
370 main.log.info(self.name+" ::::::")
371 main.cleanup()
372 main.exit()
373
andrewonlab38d6ae22014-10-15 14:23:45 -0400374 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400375 '''
376 Shows the current state of the topology
377 by issusing command: 'onos> onos:topology'
378 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400379 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800380 # either onos:topology or 'topology' will work in CLI
381 cmd_str = "onos:topology"
382 handle = self.sendline( cmd_str )
383 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400384 return handle
andrewonlab95ce8322014-10-13 14:12:04 -0400385 except pexpect.EOF:
386 main.log.error(self.name + ": EOF exception found")
387 main.log.error(self.name + ": " + self.handle.before)
388 main.cleanup()
389 main.exit()
390 except:
391 main.log.info(self.name+" ::::::")
392 main.log.error( traceback.print_exc())
393 main.log.info(self.name+" ::::::")
394 main.cleanup()
395 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800396
andrewonlabc2d05aa2014-10-13 16:51:10 -0400397 def feature_install(self, feature_str):
398 '''
Jon Halle3f39ff2015-01-13 11:50:53 -0800399 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400400 by issuing command: 'onos> feature:install <feature_str>'
401 '''
402 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800403 cmd_str = "feature:install " + str( feature_str )
404 self.sendline( cmd_str )
405 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400406 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400407 except pexpect.EOF:
408 main.log.error(self.name + ": EOF exception found")
409 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500410 main.log.report("Failed to install feature")
411 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400412 main.cleanup()
413 main.exit()
414 except:
415 main.log.info(self.name+" ::::::")
416 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500417 main.log.report("Failed to install feature")
418 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400419 main.log.info(self.name+" ::::::")
420 main.cleanup()
421 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800422
andrewonlabc2d05aa2014-10-13 16:51:10 -0400423 def feature_uninstall(self, feature_str):
424 '''
425 Uninstalls a specified feature
426 by issuing command: 'onos> feature:uninstall <feature_str>'
427 '''
428 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800429 cmd_str = "feature:uninstall " + str( feature_str )
430 self.sendline( cmd_str )
431 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400432 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400433 except pexpect.EOF:
434 main.log.error(self.name + ": EOF exception found")
435 main.log.error(self.name + ": " + self.handle.before)
436 main.cleanup()
437 main.exit()
438 except:
439 main.log.info(self.name+" ::::::")
440 main.log.error( traceback.print_exc())
441 main.log.info(self.name+" ::::::")
442 main.cleanup()
443 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800444
445 def devices(self, json_format=True):
andrewonlab86dc3082014-10-13 18:18:38 -0400446 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400447 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400448 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800449 * json_format - boolean indicating if you want output in json
andrewonlab86dc3082014-10-13 18:18:38 -0400450 '''
451 try:
Jon Halle8217482014-10-17 13:49:14 -0400452 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -0800453 cmd_str = "devices -j"
454 handle = self.sendline( cmd_str )
Jon Halld815ce42014-10-17 19:52:30 -0400455 '''
Jon Halle3f39ff2015-01-13 11:50:53 -0800456 handle variable here contains some ANSI escape color code
457 sequences at the end which are invisible in the print command
458 output. To make that escape sequence visible, use repr()
459 function. The repr(handle) output when printed shows the
460 ANSI escape sequences. In json.loads(somestring), this
461 somestring variable is actually repr(somestring) and
462 json.loads would fail with the escape sequence. So we take off
463 that escape sequence using:
464
Jon Halld815ce42014-10-17 19:52:30 -0400465 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400466 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400467 '''
Jon Halla001c392014-10-17 18:50:59 -0400468 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
469 handle1 = ansi_escape.sub('', handle)
Jon Halla001c392014-10-17 18:50:59 -0400470 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400471 else:
Jon Halle3f39ff2015-01-13 11:50:53 -0800472 cmd_str = "devices"
473 handle = self.sendline( cmd_str )
Jon Hallcd707292014-10-17 19:06:17 -0400474 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400475 except pexpect.EOF:
476 main.log.error(self.name + ": EOF exception found")
477 main.log.error(self.name + ": " + self.handle.before)
478 main.cleanup()
479 main.exit()
480 except:
481 main.log.info(self.name+" ::::::")
482 main.log.error( traceback.print_exc())
483 main.log.info(self.name+" ::::::")
484 main.cleanup()
485 main.exit()
486
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800487 def balance_masters(self):
488 '''
489 This balances the devices across all controllers
490 by issuing command: 'onos> onos:balance-masters'
491 If required this could be extended to return devices balanced output.
492 '''
493 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800494 cmd_str = "onos:balance-masters"
495 self.sendline( cmd_str )
496 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800497 return main.TRUE
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800498 except pexpect.EOF:
499 main.log.error(self.name + ": EOF exception found")
500 main.log.error(self.name + ": " + self.handle.before)
501 main.cleanup()
502 main.exit()
503 except:
504 main.log.info(self.name+" ::::::")
505 main.log.error( traceback.print_exc())
506 main.log.info(self.name+" ::::::")
507 main.cleanup()
508 main.exit()
509
Hari Krishna2bbaa702014-12-19 14:46:12 -0800510 def links(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400511 '''
512 Lists all core links
513 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800514 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400515 '''
516 try:
Jon Halle8217482014-10-17 13:49:14 -0400517 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -0800518 cmd_str = "links -j"
519 handle = self.sendline( cmd_str )
Jon Halld815ce42014-10-17 19:52:30 -0400520 '''
Jon Halle3f39ff2015-01-13 11:50:53 -0800521 handle variable here contains some ANSI escape color code
522 sequences at the end which are invisible in the print command
523 output. To make that escape sequence visible, use repr()
524 function. The repr(handle) output when printed shows the ANSI
525 escape sequences. In json.loads(somestring), this somestring
526 variable is actually repr(somestring) and json.loads would
527 fail with the escape sequence. So we take off that escape
528 sequence using:
529
Jon Halld815ce42014-10-17 19:52:30 -0400530 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800531 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400532 '''
Jon Halla001c392014-10-17 18:50:59 -0400533 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
534 handle1 = ansi_escape.sub('', handle)
Jon Halla001c392014-10-17 18:50:59 -0400535 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400536 else:
Jon Halle3f39ff2015-01-13 11:50:53 -0800537 cmd_str = "links"
538 handle = self.sendline( cmd_str )
Jon Halla001c392014-10-17 18:50:59 -0400539 return handle
Jon Halle8217482014-10-17 13:49:14 -0400540 except pexpect.EOF:
541 main.log.error(self.name + ": EOF exception found")
542 main.log.error(self.name + ": " + self.handle.before)
543 main.cleanup()
544 main.exit()
545 except:
546 main.log.info(self.name+" ::::::")
547 main.log.error( traceback.print_exc())
548 main.log.info(self.name+" ::::::")
549 main.cleanup()
550 main.exit()
551
Jon Hallffb386d2014-11-21 13:43:38 -0800552 def ports(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400553 '''
554 Lists all ports
555 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800556 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400557 '''
558 try:
Jon Halle8217482014-10-17 13:49:14 -0400559 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -0800560 cmd_str = "ports -j"
561 handle = self.sendline( cmd_str )
Jon Halld815ce42014-10-17 19:52:30 -0400562 '''
Jon Halle3f39ff2015-01-13 11:50:53 -0800563 handle variable here contains some ANSI escape color code
564 sequences at the end which are invisible in the print command
565 output. To make that escape sequence visible, use repr()
566 function. The repr(handle) output when printed shows the ANSI
567 escape sequences. In json.loads(somestring), this somestring
568 variable is actually repr(somestring) and json.loads would
569 fail with the escape sequence. So we take off that escape
570 sequence using the following commads:
571
Jon Halld815ce42014-10-17 19:52:30 -0400572 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800573 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400574 '''
Jon Halla001c392014-10-17 18:50:59 -0400575 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
576 handle1 = ansi_escape.sub('', handle)
Jon Halla001c392014-10-17 18:50:59 -0400577 return handle1
578
Jon Halle8217482014-10-17 13:49:14 -0400579 else:
Jon Halle3f39ff2015-01-13 11:50:53 -0800580 cmd_str = "ports"
581 handle = self.sendline( cmd_str )
Jon Hallffb386d2014-11-21 13:43:38 -0800582 return handle
Jon Halle8217482014-10-17 13:49:14 -0400583 except pexpect.EOF:
584 main.log.error(self.name + ": EOF exception found")
585 main.log.error(self.name + ": " + self.handle.before)
586 main.cleanup()
587 main.exit()
588 except:
589 main.log.info(self.name+" ::::::")
590 main.log.error( traceback.print_exc())
591 main.log.info(self.name+" ::::::")
592 main.cleanup()
593 main.exit()
594
595
Jon Hallffb386d2014-11-21 13:43:38 -0800596 def roles(self, json_format=True):
andrewonlab7c211572014-10-15 16:45:20 -0400597 '''
Jon Hall983a1702014-10-28 18:44:22 -0400598 Lists all devices and the controllers with roles assigned to them
599 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800600 * json_format - boolean indicating if you want output in json
andrewonlab7c211572014-10-15 16:45:20 -0400601 '''
602 try:
Jon Hall983a1702014-10-28 18:44:22 -0400603 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -0800604 cmd_str = "roles -j"
605 handle = self.sendline( cmd_str )
Jon Hall983a1702014-10-28 18:44:22 -0400606 '''
Jon Halle3f39ff2015-01-13 11:50:53 -0800607 handle variable here contains some ANSI escape color code
608 sequences at the end which are invisible in the print command
609 output. To make that escape sequence visible, use repr()
610 function. The repr(handle) output when printed shows the ANSI
611 escape sequences. In json.loads(somestring), this somestring
612 variable is actually repr(somestring) and json.loads would
613 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500614
Jon Halle3f39ff2015-01-13 11:50:53 -0800615 So we take off that escape sequence using the following
616 commads:
617
Jon Hall983a1702014-10-28 18:44:22 -0400618 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallb1290e82014-11-18 16:17:48 -0500619 handle1 = ansi_escape.sub('', handle)
Jon Hall983a1702014-10-28 18:44:22 -0400620 '''
Jon Hall983a1702014-10-28 18:44:22 -0400621 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
622 handle1 = ansi_escape.sub('', handle)
Jon Hall983a1702014-10-28 18:44:22 -0400623 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400624
andrewonlab7c211572014-10-15 16:45:20 -0400625 else:
Jon Halle3f39ff2015-01-13 11:50:53 -0800626 cmd_str = "roles"
627 handle = self.sendline( cmd_str )
Jon Hallffb386d2014-11-21 13:43:38 -0800628 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400629 except pexpect.EOF:
630 main.log.error(self.name + ": EOF exception found")
631 main.log.error(self.name + ": " + self.handle.before)
632 main.cleanup()
633 main.exit()
634 except:
635 main.log.info(self.name+" ::::::")
636 main.log.error( traceback.print_exc())
637 main.log.info(self.name+" ::::::")
638 main.cleanup()
639 main.exit()
640
641 def get_role(self, device_id):
642 '''
Jon Halle3f39ff2015-01-13 11:50:53 -0800643 Given the a string containing the json representation of the "roles"
644 cli command and a partial or whole device id, returns a json object
645 containing the roles output for the first device whose id contains
646 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400647
648 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800649 A dict of the role assignments for the given device or
650 None if no match
Jon Hall983a1702014-10-28 18:44:22 -0400651 '''
652 try:
653 import json
654 if device_id == None:
655 return None
656 else:
657 raw_roles = self.roles()
658 roles_json = json.loads(raw_roles)
659 #search json for the device with id then return the device
660 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400661 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400662 if str(device_id) in device['id']:
663 return device
664 return None
andrewonlab7c211572014-10-15 16:45:20 -0400665
andrewonlab86dc3082014-10-13 18:18:38 -0400666 except pexpect.EOF:
667 main.log.error(self.name + ": EOF exception found")
668 main.log.error(self.name + ": " + self.handle.before)
669 main.cleanup()
670 main.exit()
671 except:
672 main.log.info(self.name+" ::::::")
673 main.log.error( traceback.print_exc())
674 main.log.info(self.name+" ::::::")
675 main.cleanup()
676 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800677
678 def roles_not_null(self):
679 '''
680 Iterates through each device and checks if there is a master assigned
681 Returns: main.TRUE if each device has a master
682 main.FALSE any device has no master
683 '''
684 try:
685 import json
686 raw_roles = self.roles()
687 roles_json = json.loads(raw_roles)
688 #search json for the device with id then return the device
689 for device in roles_json:
690 #print device
691 if device['master'] == "none":
692 main.log.warn("Device has no master: " + str(device) )
693 return main.FALSE
694 return main.TRUE
695
696 except pexpect.EOF:
697 main.log.error(self.name + ": EOF exception found")
698 main.log.error(self.name + ": " + self.handle.before)
699 main.cleanup()
700 main.exit()
701 except:
702 main.log.info(self.name+" ::::::")
703 main.log.error( traceback.print_exc())
704 main.log.info(self.name+" ::::::")
705 main.cleanup()
706 main.exit()
707
708
andrewonlab3e15ead2014-10-15 14:21:34 -0400709 def paths(self, src_id, dst_id):
710 '''
711 Returns string of paths, and the cost.
712 Issues command: onos:paths <src> <dst>
713 '''
714 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800715 cmd_str = "onos:paths " + str(src_id) + " " + str(dst_id)
716 handle = self.sendline( cmd_str )
717 if re.search( "Error", handle ):
andrewonlab3e15ead2014-10-15 14:21:34 -0400718 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400719 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400720 else:
721 path = handle.split(";")[0]
722 cost = handle.split(";")[1]
723 return (path, cost)
andrewonlab3e15ead2014-10-15 14:21:34 -0400724 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()
Jon Hallffb386d2014-11-21 13:43:38 -0800735
736 def hosts(self, json_format=True):
Jon Hall42db6dc2014-10-24 19:03:48 -0400737 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800738 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400739 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800740 * json_format - boolean indicating if you want output in json
Jon Hall42db6dc2014-10-24 19:03:48 -0400741 '''
742 try:
Jon Hall42db6dc2014-10-24 19:03:48 -0400743 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -0800744 cmd_str = "hosts -j"
745 handle = self.sendline( cmd_str )
Jon Hall42db6dc2014-10-24 19:03:48 -0400746 '''
Jon Halle3f39ff2015-01-13 11:50:53 -0800747 handle variable here contains some ANSI escape color code
748 sequences at the end which are invisible in the print command
749 output. To make that escape sequence visible, use repr()
750 function. The repr(handle) output when printed shows the ANSI
751 escape sequences. In json.loads(somestring), this somestring
752 variable is actually repr(somestring) and json.loads would
753 fail with the escape sequence. So we take off that escape
754 sequence using:
755
Jon Hall42db6dc2014-10-24 19:03:48 -0400756 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800757 handle1 = ansi_escape.sub('', handle)
Jon Hall42db6dc2014-10-24 19:03:48 -0400758 '''
Jon Hall42db6dc2014-10-24 19:03:48 -0400759 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
760 handle1 = ansi_escape.sub('', handle)
Jon Hall42db6dc2014-10-24 19:03:48 -0400761 return handle1
762 else:
Jon Halle3f39ff2015-01-13 11:50:53 -0800763 cmd_str = "hosts"
764 handle = self.sendline( cmd_str )
Jon Hall42db6dc2014-10-24 19:03:48 -0400765 return handle
766 except pexpect.EOF:
767 main.log.error(self.name + ": EOF exception found")
768 main.log.error(self.name + ": " + self.handle.before)
769 main.cleanup()
770 main.exit()
771 except:
772 main.log.info(self.name+" ::::::")
773 main.log.error( traceback.print_exc())
774 main.log.info(self.name+" ::::::")
775 main.cleanup()
776 main.exit()
777
778 def get_host(self, mac):
779 '''
780 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800781
782 Note: mac must be a colon seperated mac address, but could be a
783 partial mac address
784
Jon Hall42db6dc2014-10-24 19:03:48 -0400785 Return None if there is no match
786 '''
787 import json
788 try:
789 if mac == None:
790 return None
791 else:
792 mac = mac
793 raw_hosts = self.hosts()
794 hosts_json = json.loads(raw_hosts)
795 #search json for the host with mac then return the device
796 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400797 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400798 if mac in host['id']:
799 return host
800 return None
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()
812
andrewonlab3f0a4af2014-10-17 12:25:14 -0400813
814 def get_hosts_id(self, host_list):
815 '''
816 Obtain list of hosts
817 Issues command: 'onos> hosts'
818
819 Required:
820 * host_list: List of hosts obtained by Mininet
821 IMPORTANT:
822 This function assumes that you started your
823 topology with the option '--mac'.
824 Furthermore, it assumes that value of VLAN is '-1'
825 Description:
826 Converts mininet hosts (h1, h2, h3...) into
827 ONOS format (00:00:00:00:00:01/-1 , ...)
828 '''
829
830 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400831 onos_host_list = []
832
833 for host in host_list:
834 host = host.replace("h", "")
835 host_hex = hex(int(host)).zfill(12)
836 host_hex = str(host_hex).replace('x','0')
837 i = iter(str(host_hex))
838 host_hex = ":".join(a+b for a,b in zip(i,i))
839 host_hex = host_hex + "/-1"
840 onos_host_list.append(host_hex)
841
842 return onos_host_list
843
844 except pexpect.EOF:
845 main.log.error(self.name + ": EOF exception found")
846 main.log.error(self.name + ": " + self.handle.before)
847 main.cleanup()
848 main.exit()
849 except:
850 main.log.info(self.name+" ::::::")
851 main.log.error( traceback.print_exc())
852 main.log.info(self.name+" ::::::")
853 main.cleanup()
854 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400855
andrewonlabe6745342014-10-17 14:29:13 -0400856 def add_host_intent(self, host_id_one, host_id_two):
857 '''
858 Required:
859 * host_id_one: ONOS host id for host1
860 * host_id_two: ONOS host id for host2
861 Description:
862 Adds a host-to-host intent (bidrectional) by
Jon Hallb1290e82014-11-18 16:17:48 -0500863 specifying the two hosts.
andrewonlabe6745342014-10-17 14:29:13 -0400864 '''
865 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800866 cmd_str = "add-host-intent " + str(host_id_one) +\
867 " " + str(host_id_two)
868 handle = self.sendline( cmd_str )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800869 main.log.info("Host intent installed between "+
andrewonlabe6745342014-10-17 14:29:13 -0400870 str(host_id_one) + " and " + str(host_id_two))
andrewonlabe6745342014-10-17 14:29:13 -0400871 return handle
andrewonlabe6745342014-10-17 14:29:13 -0400872 except pexpect.EOF:
873 main.log.error(self.name + ": EOF exception found")
874 main.log.error(self.name + ": " + self.handle.before)
875 main.cleanup()
876 main.exit()
877 except:
878 main.log.info(self.name+" ::::::")
879 main.log.error( traceback.print_exc())
880 main.log.info(self.name+" ::::::")
881 main.cleanup()
882 main.exit()
883
andrewonlab7b31d232014-10-24 13:31:47 -0400884 def add_optical_intent(self, ingress_device, egress_device):
885 '''
886 Required:
887 * ingress_device: device id of ingress device
888 * egress_device: device id of egress device
889 Optional:
890 TODO: Still needs to be implemented via dev side
Jon Halle3f39ff2015-01-13 11:50:53 -0800891 '''
andrewonlab7b31d232014-10-24 13:31:47 -0400892 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800893 cmd_str = "add-optical-intent " + str( ingress_device ) +\
894 " " + str( egress_device )
895 handle = self.sendline( cmd_str )
andrewonlab7b31d232014-10-24 13:31:47 -0400896 #If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800897 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400898 return handle
899 else:
900 return main.TRUE
andrewonlab7b31d232014-10-24 13:31:47 -0400901 except pexpect.EOF:
902 main.log.error(self.name + ": EOF exception found")
903 main.log.error(self.name + ": " + self.handle.before)
904 main.cleanup()
905 main.exit()
906 except:
907 main.log.info(self.name+" ::::::")
908 main.log.error( traceback.print_exc())
909 main.log.info(self.name+" ::::::")
910 main.cleanup()
911 main.exit()
912
andrewonlab36af3822014-11-18 17:48:18 -0500913 def add_point_intent(self, ingress_device, egress_device,
914 port_ingress="", port_egress="", ethType="", ethSrc="",
Jon Halle3f39ff2015-01-13 11:50:53 -0800915 ethDst="", bandwidth="", lambda_alloc=False,
andrewonlabfa4ff502014-11-11 16:41:30 -0500916 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400917 '''
918 Required:
919 * ingress_device: device id of ingress device
920 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400921 Optional:
922 * ethType: specify ethType
923 * ethSrc: specify ethSrc (i.e. src mac addr)
924 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500925 * bandwidth: specify bandwidth capacity of link
Jon Halle3f39ff2015-01-13 11:50:53 -0800926 * lambda_alloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -0500927 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -0800928 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -0500929 * ipSrc: specify ip source address
930 * ipDst: specify ip destination address
931 * tcpSrc: specify tcp source port
932 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400933 Description:
934 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400935 specifying device id's and optional fields
936
Jon Halle3f39ff2015-01-13 11:50:53 -0800937 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -0400938 options developers provide for point-to-point
939 intent via cli
940 '''
941 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400942 cmd = ""
943
944 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500945 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -0500946 and not bandwidth and not lambda_alloc \
947 and not ipProto and not ipSrc and not ipDst \
948 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -0500949 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -0800950
andrewonlab36af3822014-11-18 17:48:18 -0500951
andrewonlab289e4b72014-10-21 21:24:18 -0400952 else:
andrewonlab36af3822014-11-18 17:48:18 -0500953 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -0800954
andrewonlab0c0a6772014-10-22 12:31:18 -0400955 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -0400956 cmd += " --ethType " + str(ethType)
957 if ethSrc:
Jon Halle3f39ff2015-01-13 11:50:53 -0800958 cmd += " --ethSrc " + str(ethSrc)
959 if ethDst:
960 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500961 if bandwidth:
962 cmd += " --bandwidth " + str(bandwidth)
963 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -0500964 cmd += " --lambda "
965 if ipProto:
966 cmd += " --ipProto " + str(ipProto)
967 if ipSrc:
968 cmd += " --ipSrc " + str(ipSrc)
969 if ipDst:
970 cmd += " --ipDst " + str(ipDst)
971 if tcpSrc:
972 cmd += " --tcpSrc " + str(tcpSrc)
973 if tcpDst:
974 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -0400975
Jon Halle3f39ff2015-01-13 11:50:53 -0800976 #Check whether the user appended the port
andrewonlab36af3822014-11-18 17:48:18 -0500977 #or provided it as an input
978 if "/" in ingress_device:
979 cmd += " "+str(ingress_device)
980 else:
981 if not port_ingress:
982 main.log.error("You must specify "+
983 "the ingress port")
984 #TODO: perhaps more meaningful return
985 return main.FALSE
986
987 cmd += " "+ \
988 str(ingress_device) + "/" +\
Jon Halle3f39ff2015-01-13 11:50:53 -0800989 str(port_ingress) + " "
andrewonlab36af3822014-11-18 17:48:18 -0500990
991 if "/" in egress_device:
992 cmd += " "+str(egress_device)
993 else:
994 if not port_egress:
995 main.log.error("You must specify "+
996 "the egress port")
997 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -0800998
andrewonlab36af3822014-11-18 17:48:18 -0500999 cmd += " "+\
1000 str(egress_device) + "/" +\
Jon Halle3f39ff2015-01-13 11:50:53 -08001001 str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001002
Jon Halle3f39ff2015-01-13 11:50:53 -08001003 handle = self.sendline(cmd)
1004 if re.search( "Error", handle ):
andrewonlab4dbb4d82014-10-17 18:22:31 -04001005 main.log.error("Error in adding point-to-point intent")
Jon Hall47a93fb2015-01-06 16:46:06 -08001006 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001007 else:
1008 return main.TRUE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001009 except pexpect.EOF:
1010 main.log.error(self.name + ": EOF exception found")
1011 main.log.error(self.name + ": " + self.handle.before)
1012 main.cleanup()
1013 main.exit()
1014 except:
1015 main.log.info(self.name+" ::::::")
1016 main.log.error( traceback.print_exc())
1017 main.log.info(self.name+" ::::::")
1018 main.cleanup()
1019 main.exit()
1020
shahshreyad0c80432014-12-04 16:56:05 -08001021
Jon Halle3f39ff2015-01-13 11:50:53 -08001022 def add_multipoint_to_singlepoint_intent(self, ingress_device1,
1023 ingress_device2, egress_device, port_ingress="", port_egress="",
1024 ethType="", ethSrc="", ethDst="", bandwidth="", lambda_alloc=False,
1025 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="",
1026 setEthDst=""):
shahshreyad0c80432014-12-04 16:56:05 -08001027 '''
1028 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001029 This function assumes that there would be 2 ingress devices and
1030 one egress device. For more number of ingress devices, this
1031 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001032 Required:
1033 * ingress_device1: device id of ingress device1
1034 * ingress_device2: device id of ingress device2
1035 * egress_device: device id of egress device
1036 Optional:
1037 * ethType: specify ethType
1038 * ethSrc: specify ethSrc (i.e. src mac addr)
1039 * ethDst: specify ethDst (i.e. dst mac addr)
1040 * bandwidth: specify bandwidth capacity of link
Jon Halle3f39ff2015-01-13 11:50:53 -08001041 * lambda_alloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001042 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001043 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001044 * ipSrc: specify ip source address
1045 * ipDst: specify ip destination address
1046 * tcpSrc: specify tcp source port
1047 * tcpDst: specify tcp destination port
1048 * setEthSrc: action to Rewrite Source MAC Address
1049 * setEthDst: action to Rewrite Destination MAC Address
1050 Description:
1051 Adds a multipoint-to-singlepoint intent (uni-directional) by
1052 specifying device id's and optional fields
1053
Jon Halle3f39ff2015-01-13 11:50:53 -08001054 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001055 options developers provide for multipointpoint-to-singlepoint
1056 intent via cli
1057 '''
1058 try:
1059 cmd = ""
1060
1061 #If there are no optional arguments
1062 if not ethType and not ethSrc and not ethDst\
Jon Halle3f39ff2015-01-13 11:50:53 -08001063 and not bandwidth and not lambda_alloc\
1064 and not ipProto and not ipSrc and not ipDst\
1065 and not tcpSrc and not tcpDst and not setEthSrc\
1066 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001067 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001068
1069 else:
1070 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001071
shahshreyad0c80432014-12-04 16:56:05 -08001072 if ethType:
1073 cmd += " --ethType " + str(ethType)
1074 if ethSrc:
Jon Halle3f39ff2015-01-13 11:50:53 -08001075 cmd += " --ethSrc " + str(ethSrc)
1076 if ethDst:
1077 cmd += " --ethDst " + str(ethDst)
shahshreyad0c80432014-12-04 16:56:05 -08001078 if bandwidth:
1079 cmd += " --bandwidth " + str(bandwidth)
1080 if lambda_alloc:
1081 cmd += " --lambda "
1082 if ipProto:
1083 cmd += " --ipProto " + str(ipProto)
1084 if ipSrc:
1085 cmd += " --ipSrc " + str(ipSrc)
1086 if ipDst:
1087 cmd += " --ipDst " + str(ipDst)
1088 if tcpSrc:
1089 cmd += " --tcpSrc " + str(tcpSrc)
1090 if tcpDst:
1091 cmd += " --tcpDst " + str(tcpDst)
1092 if setEthSrc:
1093 cmd += " --setEthSrc "+ str(setEthSrc)
1094 if setEthDst:
1095 cmd += " --setEthDst "+ str(setEthDst)
1096
Jon Halle3f39ff2015-01-13 11:50:53 -08001097 #Check whether the user appended the port
shahshreyad0c80432014-12-04 16:56:05 -08001098 #or provided it as an input
1099 if "/" in ingress_device1:
Jon Halle3f39ff2015-01-13 11:50:53 -08001100 cmd += " "+str(ingress_device1)
shahshreyad0c80432014-12-04 16:56:05 -08001101 else:
1102 if not port_ingress1:
1103 main.log.error("You must specify "+
1104 "the ingress port1")
1105 #TODO: perhaps more meaningful return
1106 return main.FALSE
1107
1108 cmd += " "+ \
1109 str(ingress_device1) + "/" +\
Jon Halle3f39ff2015-01-13 11:50:53 -08001110 str(port_ingress1) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001111
1112 if "/" in ingress_device2:
1113 cmd += " "+str(ingress_device2)
1114 else:
1115 if not port_ingress2:
1116 main.log.error("You must specify "+
1117 "the ingress port2")
1118 #TODO: perhaps more meaningful return
1119 return main.FALSE
1120
1121 cmd += " "+ \
1122 str(ingress_device2) + "/" +\
1123 str(port_ingress2) + " "
1124
1125 if "/" in egress_device:
1126 cmd += " "+str(egress_device)
1127 else:
1128 if not port_egress:
1129 main.log.error("You must specify "+
1130 "the egress port")
1131 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001132
shahshreyad0c80432014-12-04 16:56:05 -08001133 cmd += " "+\
1134 str(egress_device) + "/" +\
Jon Halle3f39ff2015-01-13 11:50:53 -08001135 str(port_egress)
shahshreyad0c80432014-12-04 16:56:05 -08001136 print "cmd= ",cmd
Jon Halle3f39ff2015-01-13 11:50:53 -08001137 handle = self.sendline(cmd)
1138 if re.search( "Error", handle ):
shahshreyad0c80432014-12-04 16:56:05 -08001139 main.log.error("Error in adding point-to-point intent")
1140 return self.handle
1141 else:
1142 return main.TRUE
shahshreyad0c80432014-12-04 16:56:05 -08001143 except pexpect.EOF:
1144 main.log.error(self.name + ": EOF exception found")
1145 main.log.error(self.name + ": " + self.handle.before)
1146 main.cleanup()
1147 main.exit()
1148 except:
1149 main.log.info(self.name+" ::::::")
1150 main.log.error( traceback.print_exc())
1151 main.log.info(self.name+" ::::::")
1152 main.cleanup()
1153 main.exit()
1154
andrewonlab9a50dfe2014-10-17 17:22:31 -04001155 def remove_intent(self, intent_id):
1156 '''
1157 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001158
1159 Returns:
1160 main.False on error and
1161 cli output otherwise
andrewonlab9a50dfe2014-10-17 17:22:31 -04001162 '''
1163 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001164 cmd_str = "remove-intent " + str( intent_id )
1165 handle = self.sendline( cmd_str )
1166 if re.search( "Error", handle ):
andrewonlab9a50dfe2014-10-17 17:22:31 -04001167 main.log.error("Error in removing intent")
Jon Halle3f39ff2015-01-13 11:50:53 -08001168 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001169 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001170 # TODO: Should this be main.TRUE
1171 return handle
andrewonlab9a50dfe2014-10-17 17:22:31 -04001172 except pexpect.EOF:
1173 main.log.error(self.name + ": EOF exception found")
1174 main.log.error(self.name + ": " + self.handle.before)
1175 main.cleanup()
1176 main.exit()
1177 except:
1178 main.log.info(self.name+" ::::::")
1179 main.log.error( traceback.print_exc())
1180 main.log.info(self.name+" ::::::")
1181 main.cleanup()
1182 main.exit()
1183
pingping-lin8b306ac2014-11-17 18:13:51 -08001184 def routes(self, json_format=False):
1185 '''
Jon Halle3f39ff2015-01-13 11:50:53 -08001186 NOTE: This method should be used after installing application:
1187 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001188 Optional:
1189 * json_format: enable output formatting in json
1190 Description:
1191 Obtain all routes in the system
1192 '''
1193 try:
1194 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -08001195 cmd_str = "routes -j"
1196 handle_tmp = self.sendline( cmd_str )
pingping-lin8b306ac2014-11-17 18:13:51 -08001197 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1198 handle = ansi_escape.sub('', handle_tmp)
pingping-lin8b306ac2014-11-17 18:13:51 -08001199 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001200 cmd_str = "routes"
1201 handle = self.sendline( cmd_str )
pingping-lin8b306ac2014-11-17 18:13:51 -08001202 return handle
pingping-lin8b306ac2014-11-17 18:13:51 -08001203 except pexpect.EOF:
1204 main.log.error(self.name + ": EOF exception found")
1205 main.log.error(self.name + ": " + self.handle.before)
1206 main.cleanup()
1207 main.exit()
1208 except:
1209 main.log.info(self.name + " ::::::")
1210 main.log.error(traceback.print_exc())
1211 main.log.info(self.name + " ::::::")
1212 main.cleanup()
1213 main.exit()
1214
Jon Hallffb386d2014-11-21 13:43:38 -08001215 def intents(self, json_format = True):
andrewonlabe6745342014-10-17 14:29:13 -04001216 '''
andrewonlab377693f2014-10-21 16:00:30 -04001217 Optional:
1218 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001219 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001220 Obtain intents currently installed
andrewonlabe6745342014-10-17 14:29:13 -04001221 '''
1222 try:
andrewonlab377693f2014-10-21 16:00:30 -04001223 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -08001224 cmd_str = "intents -j"
1225 handle = self.sendline( cmd_str )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001226 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1227 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001228 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001229 cmd_str = "intents"
1230 handle = self.sendline( cmd_str )
andrewonlabe6745342014-10-17 14:29:13 -04001231 return handle
andrewonlabe6745342014-10-17 14:29:13 -04001232 except pexpect.EOF:
1233 main.log.error(self.name + ": EOF exception found")
1234 main.log.error(self.name + ": " + self.handle.before)
1235 main.cleanup()
1236 main.exit()
1237 except:
1238 main.log.info(self.name+" ::::::")
1239 main.log.error( traceback.print_exc())
1240 main.log.info(self.name+" ::::::")
1241 main.cleanup()
1242 main.exit()
1243
Jon Hallffb386d2014-11-21 13:43:38 -08001244 def flows(self, json_format = True):
Shreya Shah0f01c812014-10-26 20:15:28 -04001245 '''
1246 Optional:
1247 * json_format: enable output formatting in json
1248 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001249 Obtain flows currently installed
Shreya Shah0f01c812014-10-26 20:15:28 -04001250 '''
1251 try:
1252 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -08001253 cmd_str = "flows -j"
1254 handle = self.sendline( cmd_str )
Jon Hallb1290e82014-11-18 16:17:48 -05001255 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1256 handle = ansi_escape.sub('', handle)
Shreya Shah0f01c812014-10-26 20:15:28 -04001257 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001258 cmd_str = "flows"
1259 handle = self.sendline( cmd_str )
Jon Hallb1290e82014-11-18 16:17:48 -05001260 if re.search("Error\sexecuting\scommand:", handle):
Jon Halle3f39ff2015-01-13 11:50:53 -08001261 main.log.error( self.name + ".flows() response: " +
1262 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001263 return handle
Shreya Shah0f01c812014-10-26 20:15:28 -04001264 except pexpect.EOF:
1265 main.log.error(self.name + ": EOF exception found")
1266 main.log.error(self.name + ": " + self.handle.before)
1267 main.cleanup()
1268 main.exit()
1269 except:
1270 main.log.info(self.name+" ::::::")
1271 main.log.error( traceback.print_exc())
1272 main.log.info(self.name+" ::::::")
1273 main.cleanup()
1274 main.exit()
1275
Jon Halle3f39ff2015-01-13 11:50:53 -08001276 def push_test_intents(self, dpid_src, dpid_dst, num_intents,
andrewonlabb66dfa12014-12-02 15:51:10 -05001277 num_mult="", app_id="", report=True):
andrewonlab87852b02014-11-19 18:44:19 -05001278 '''
1279 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001280 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001281 a specific point-to-point intent definition
1282 Required:
1283 * dpid_src: specify source dpid
1284 * dpid_dst: specify destination dpid
1285 * num_intents: specify number of intents to push
1286 Optional:
andrewonlabb66dfa12014-12-02 15:51:10 -05001287 * num_mult: number multiplier for multiplying
1288 the number of intents specified
1289 * app_id: specify the application id init to further
1290 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001291 * report: default True, returns latency information
1292 '''
1293 try:
1294 cmd = "push-test-intents "+\
1295 str(dpid_src)+" "+str(dpid_dst)+" "+\
1296 str(num_intents)
andrewonlabb66dfa12014-12-02 15:51:10 -05001297 if num_mult:
1298 cmd += " " + str(num_mult)
Jon Halle3f39ff2015-01-13 11:50:53 -08001299 #If app id is specified, then num_mult
andrewonlab042b3912014-12-10 16:40:50 -05001300 #must exist because of the way this command
1301 #takes in arguments
andrewonlabb66dfa12014-12-02 15:51:10 -05001302 if app_id:
1303 cmd += " " + str(app_id)
Jon Halle3f39ff2015-01-13 11:50:53 -08001304 handle = self.sendline( cmd )
andrewonlab87852b02014-11-19 18:44:19 -05001305 #Some color thing that we want to escape
1306 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1307 handle = ansi_escape.sub('', handle)
andrewonlab87852b02014-11-19 18:44:19 -05001308 if report:
andrewonlabb66dfa12014-12-02 15:51:10 -05001309 lat_result = []
andrewonlab87852b02014-11-19 18:44:19 -05001310 main.log.info(handle)
andrewonlabb66dfa12014-12-02 15:51:10 -05001311 #Split result by newline
1312 newline = handle.split("\r\r\n")
1313 #Ignore the first object of list, which is empty
1314 newline = newline[1:]
1315 #Some sloppy parsing method to get the latency
1316 for result in newline:
1317 result = result.split(": ")
1318 #Append the first result of second parse
1319 lat_result.append(result[1].split(" ")[0])
Jon Halle3f39ff2015-01-13 11:50:53 -08001320 main.log.info(lat_result)
1321 return lat_result
andrewonlab87852b02014-11-19 18:44:19 -05001322 else:
1323 return main.TRUE
andrewonlab87852b02014-11-19 18:44:19 -05001324 except pexpect.EOF:
1325 main.log.error(self.name + ": EOF exception found")
1326 main.log.error(self.name + ": " + self.handle.before)
1327 main.cleanup()
1328 main.exit()
1329 except:
1330 main.log.info(self.name+" ::::::")
1331 main.log.error( traceback.print_exc())
1332 main.log.info(self.name+" ::::::")
1333 main.cleanup()
1334 main.exit()
1335
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001336 def intents_events_metrics(self, json_format=True):
1337 '''
Jon Halle3f39ff2015-01-13 11:50:53 -08001338 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001339 Optional:
1340 * json_format: enable json formatting of output
1341 '''
1342 try:
1343 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -08001344 cmd_str = "intents-events-metrics -j"
1345 handle = self.sendline( cmd_str )
1346 # Some color thing that we want to escape
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001347 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1348 handle = ansi_escape.sub('', handle)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001349 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001350 cmd_str = "intents-events-metrics"
1351 handle = self.sendline( cmd_str )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001352 return handle
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001353 except pexpect.EOF:
1354 main.log.error(self.name + ": EOF exception found")
1355 main.log.error(self.name + ": " + self.handle.before)
1356 main.cleanup()
1357 main.exit()
1358 except:
1359 main.log.info(self.name+" ::::::")
1360 main.log.error( traceback.print_exc())
1361 main.log.info(self.name+" ::::::")
1362 main.cleanup()
1363 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001364
andrewonlab867212a2014-10-22 20:13:38 -04001365 def topology_events_metrics(self, json_format=True):
1366 '''
1367 Description:Returns topology metrics
1368 Optional:
1369 * json_format: enable json formatting of output
1370 '''
1371 try:
1372 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -08001373 cmd_str = "topology-events-metrics -j"
1374 handle = self.sendline( cmd_str )
andrewonlab867212a2014-10-22 20:13:38 -04001375 #Some color thing that we want to escape
1376 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1377 handle = ansi_escape.sub('', handle)
andrewonlab867212a2014-10-22 20:13:38 -04001378 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001379 cmd_str = "topology-events-metrics"
1380 handle = self.sendline( cmd_str )
andrewonlab867212a2014-10-22 20:13:38 -04001381 return handle
andrewonlab867212a2014-10-22 20:13:38 -04001382 except pexpect.EOF:
1383 main.log.error(self.name + ": EOF exception found")
1384 main.log.error(self.name + ": " + self.handle.before)
1385 main.cleanup()
1386 main.exit()
1387 except:
1388 main.log.info(self.name+" ::::::")
1389 main.log.error( traceback.print_exc())
1390 main.log.info(self.name+" ::::::")
1391 main.cleanup()
1392 main.exit()
1393
andrewonlab3e15ead2014-10-15 14:21:34 -04001394 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001395 #Wrapper functions use existing driver
1396 #functions and extends their use case.
1397 #For example, we may use the output of
1398 #a normal driver function, and parse it
1399 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001400
andrewonlab9a50dfe2014-10-17 17:22:31 -04001401 def get_all_intents_id(self):
1402 '''
1403 Description:
1404 Obtain all intent id's in a list
1405 '''
1406 try:
1407 #Obtain output of intents function
1408 intents_str = self.intents()
1409 all_intent_list = []
1410 intent_id_list = []
1411
1412 #Parse the intents output for ID's
1413 intents_list = [s.strip() for s in intents_str.splitlines()]
1414 for intents in intents_list:
1415 if "onos>" in intents:
1416 continue
1417 elif "intents" in intents:
1418 continue
1419 else:
1420 line_list = intents.split(" ")
1421 all_intent_list.append(line_list[0])
1422
1423 all_intent_list = all_intent_list[1:-2]
1424
1425 for intents in all_intent_list:
1426 if not intents:
1427 continue
1428 else:
1429 intent_id_list.append(intents)
1430
1431 return intent_id_list
1432
1433 except pexpect.EOF:
1434 main.log.error(self.name + ": EOF exception found")
1435 main.log.error(self.name + ": " + self.handle.before)
1436 main.cleanup()
1437 main.exit()
1438 except:
1439 main.log.info(self.name+" ::::::")
1440 main.log.error( traceback.print_exc())
1441 main.log.info(self.name+" ::::::")
1442 main.cleanup()
1443 main.exit()
1444
andrewonlab7e4d2d32014-10-15 13:23:21 -04001445 def get_all_devices_id(self):
1446 '''
1447 Use 'devices' function to obtain list of all devices
1448 and parse the result to obtain a list of all device
1449 id's. Returns this list. Returns empty list if no
1450 devices exist
1451 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001452
1453 This function may be useful if you are not sure of the
1454 device id, and wish to execute other commands using
1455 the ids. By obtaining the list of device ids on the fly,
1456 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001457 '''
1458 try:
1459 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001460 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001461 id_list = []
1462
1463 if not devices_str:
1464 main.log.info("There are no devices to get id from")
1465 return id_list
1466
1467 #Split the string into list by comma
1468 device_list = devices_str.split(",")
1469 #Get temporary list of all arguments with string 'id='
1470 temp_list = [dev for dev in device_list if "id=" in dev]
1471 #Split list further into arguments before and after string
1472 # 'id='. Get the latter portion (the actual device id) and
1473 # append to id_list
1474 for arg in temp_list:
1475 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001476 return id_list
1477
1478 except pexpect.EOF:
1479 main.log.error(self.name + ": EOF exception found")
1480 main.log.error(self.name + ": " + self.handle.before)
1481 main.cleanup()
1482 main.exit()
1483 except:
1484 main.log.info(self.name+" ::::::")
1485 main.log.error( traceback.print_exc())
1486 main.log.info(self.name+" ::::::")
1487 main.cleanup()
1488 main.exit()
1489
andrewonlab7c211572014-10-15 16:45:20 -04001490 def get_all_nodes_id(self):
1491 '''
1492 Uses 'nodes' function to obtain list of all nodes
1493 and parse the result of nodes to obtain just the
1494 node id's.
1495 Returns:
1496 list of node id's
1497 '''
1498 try:
1499 nodes_str = self.nodes()
1500 id_list = []
1501
1502 if not nodes_str:
1503 main.log.info("There are no nodes to get id from")
1504 return id_list
1505
1506 #Sample nodes_str output
1507 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1508
1509 #Split the string into list by comma
1510 nodes_list = nodes_str.split(",")
1511 temp_list = [node for node in nodes_list if "id=" in node]
1512 for arg in temp_list:
1513 id_list.append(arg.split("id=")[1])
1514
1515 return id_list
1516
1517 except pexpect.EOF:
1518 main.log.error(self.name + ": EOF exception found")
1519 main.log.error(self.name + ": " + self.handle.before)
1520 main.cleanup()
1521 main.exit()
1522 except:
1523 main.log.info(self.name+" ::::::")
1524 main.log.error( traceback.print_exc())
1525 main.log.info(self.name+" ::::::")
1526 main.cleanup()
1527 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001528
Jon Halla91c4dc2014-10-22 12:57:04 -04001529 def get_device(self, dpid=None):
1530 '''
1531 Return the first device from the devices api whose 'id' contains 'dpid'
1532 Return None if there is no match
1533 '''
1534 import json
1535 try:
1536 if dpid == None:
1537 return None
1538 else:
1539 dpid = dpid.replace(':', '')
1540 raw_devices = self.devices()
1541 devices_json = json.loads(raw_devices)
1542 #search json for the device with dpid then return the device
1543 for device in devices_json:
1544 #print "%s in %s?" % (dpid, device['id'])
1545 if dpid in device['id']:
1546 return device
1547 return None
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
Jon Hall42db6dc2014-10-24 19:03:48 -04001560 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1561 '''
1562 Checks the number of swithes & links that ONOS sees against the
1563 supplied values. By default this will report to main.log, but the
1564 log level can be specifid.
1565
1566 Params: ip = ip used for the onos cli
1567 numoswitch = expected number of switches
1568 numlink = expected number of links
1569 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1570
1571
1572 log_level can
1573
1574 Returns: main.TRUE if the number of switchs and links are correct,
1575 main.FALSE if the numer of switches and links is incorrect,
1576 and main.ERROR otherwise
1577 '''
1578
1579 try:
1580 topology = self.get_topology(ip)
1581 if topology == {}:
1582 return main.ERROR
1583 output = ""
1584 #Is the number of switches is what we expected
1585 devices = topology.get('devices',False)
1586 links = topology.get('links',False)
1587 if devices == False or links == False:
1588 return main.ERROR
1589 switch_check = ( int(devices) == int(numoswitch) )
1590 #Is the number of links is what we expected
1591 link_check = ( int(links) == int(numolink) )
1592 if (switch_check and link_check):
1593 #We expected the correct numbers
1594 output = output + "The number of links and switches match "\
1595 + "what was expected"
1596 result = main.TRUE
1597 else:
1598 output = output + \
1599 "The number of links and switches does not match what was expected"
1600 result = main.FALSE
1601 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1602 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1603 if log_level == "report":
1604 main.log.report(output)
1605 elif log_level == "warn":
1606 main.log.warn(output)
1607 else:
1608 main.log.info(output)
1609 return result
1610 except pexpect.EOF:
1611 main.log.error(self.name + ": EOF exception found")
1612 main.log.error(self.name + ": " + self.handle.before)
1613 main.cleanup()
1614 main.exit()
1615 except:
1616 main.log.info(self.name+" ::::::")
1617 main.log.error( traceback.print_exc())
1618 main.log.info(self.name+" ::::::")
1619 main.cleanup()
1620 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001621
1622 def device_role(self, device_id, onos_node, role="master"):
1623 '''
1624 Calls the device-role cli command.
1625 device_id must be the id of a device as seen in the onos devices command
1626 onos_node is the ip of one of the onos nodes in the cluster
1627 role must be either master, standby, or none
1628
Jon Halle3f39ff2015-01-13 11:50:53 -08001629 Returns:
1630 main.TRUE or main.FALSE based on argument verification and
1631 main.ERROR if command returns and error
Jon Hall1c9e8732014-10-27 19:29:27 -04001632 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001633 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001634 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001635 role.lower() == "none":
Jon Halle3f39ff2015-01-13 11:50:53 -08001636 cmd_str = "device-role " +\
1637 str(device_id) + " " +\
1638 str(onos_node) + " " +\
1639 str(role)
1640 handle = self.sendline( cmd_str )
1641 if re.search( "Error", handle ):
1642 # end color output to escape any colours
1643 # from the cli
1644 main.log.error( self.name + ": " +\
1645 handle + '\033[0m' )
Jon Hall983a1702014-10-28 18:44:22 -04001646 return main.ERROR
Jon Hall1c9e8732014-10-27 19:29:27 -04001647 return main.TRUE
1648 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001649 main.log.error("Invalid 'role' given to device_role(). " +
1650 "Value was '" + str( role ) + "'.")
Jon Hall1c9e8732014-10-27 19:29:27 -04001651 return main.FALSE
Jon Hall1c9e8732014-10-27 19:29:27 -04001652 except pexpect.EOF:
1653 main.log.error(self.name + ": EOF exception found")
1654 main.log.error(self.name + ": " + self.handle.before)
1655 main.cleanup()
1656 main.exit()
1657 except:
1658 main.log.info(self.name+" ::::::")
1659 main.log.error( traceback.print_exc())
1660 main.log.info(self.name+" ::::::")
1661 main.cleanup()
1662 main.exit()
1663
Jon Hall73cf9cc2014-11-20 22:28:38 -08001664 def clusters(self, json_format=True):
1665 '''
1666 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001667 Optional argument:
1668 * json_format - boolean indicating if you want output in json
Jon Hall73cf9cc2014-11-20 22:28:38 -08001669 '''
1670 try:
Jon Hall73cf9cc2014-11-20 22:28:38 -08001671 if json_format:
Jon Halle3f39ff2015-01-13 11:50:53 -08001672 cmd_str = "clusters -j"
1673 handle = self.sendline( cmd_str )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001674 '''
1675 handle variable here contains some ANSI escape color code
1676 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001677 output. To make that escape sequence visible, use repr()
1678 function. The repr(handle) output when printed shows the ANSI
1679 escape sequences. In json.loads(somestring), this somestring
1680 variable is actually repr(somestring) and json.loads would fail
1681 with the escape sequence. So we take off that escape sequence
1682 using:
1683
Jon Hall73cf9cc2014-11-20 22:28:38 -08001684 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1685 handle1 = ansi_escape.sub('', handle)
1686 '''
Jon Hall73cf9cc2014-11-20 22:28:38 -08001687 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1688 handle1 = ansi_escape.sub('', handle)
Jon Hall73cf9cc2014-11-20 22:28:38 -08001689 return handle1
1690 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001691 cmd_str = "clusters"
1692 handle = self.sendline( cmd_str )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001693 return handle
1694 except pexpect.EOF:
1695 main.log.error(self.name + ": EOF exception found")
1696 main.log.error(self.name + ": " + self.handle.before)
1697 main.cleanup()
1698 main.exit()
1699 except:
1700 main.log.info(self.name+" ::::::")
1701 main.log.error( traceback.print_exc())
1702 main.log.info(self.name+" ::::::")
1703 main.cleanup()
1704 main.exit()
1705
Jon Hall94fd0472014-12-08 11:52:42 -08001706 def election_test_leader(self):
1707 '''
Jon Halle3f39ff2015-01-13 11:50:53 -08001708 CLI command to get the current leader for the Election test application
1709 NOTE: Requires installation of the onos-app-election feature
1710 Returns: Node IP of the leader if one exists
1711 None if none exists
1712 Main.FALSE on error
Jon Hall94fd0472014-12-08 11:52:42 -08001713 '''
1714 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001715 cmd_str = "election-test-leader"
1716 response = self.sendline( cmd_str )
1717 # Leader
1718 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
1719 "app\sis\s(?P<node>.+)\."
1720 node_search = re.search(leaderPattern, response)
Jon Hall94fd0472014-12-08 11:52:42 -08001721 if node_search:
1722 node = node_search.group('node')
Jon Halle3f39ff2015-01-13 11:50:53 -08001723 main.log.info( "Election-test-leader on " + str( self.name ) +
1724 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001725 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001726 # no leader
1727 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
1728 "the\sElection\sapp"
1729 null_search = re.search(nullPattern, response)
Jon Hall94fd0472014-12-08 11:52:42 -08001730 if null_search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001731 main.log.info( "Election-test-leader found no leader on " +
1732 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001733 return None
1734 #error
Jon Halle3f39ff2015-01-13 11:50:53 -08001735 errorPattern = "Command\snot\sfound"
1736 if re.search(errorPattern, response):
Jon Hall669173b2014-12-17 11:36:30 -08001737 main.log.error("Election app is not loaded on " + self.name)
Jon Halle3f39ff2015-01-13 11:50:53 -08001738 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001739 return main.FALSE
1740 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001741 main.log.error("Error in election_test_leader: " +
1742 "unexpected response")
Jon Hall669173b2014-12-17 11:36:30 -08001743 main.log.error( repr(response) )
1744 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001745 except pexpect.EOF:
1746 main.log.error(self.name + ": EOF exception found")
1747 main.log.error(self.name + ": " + self.handle.before)
1748 main.cleanup()
1749 main.exit()
1750 except:
1751 main.log.info(self.name+" ::::::")
1752 main.log.error( traceback.print_exc())
1753 main.log.info(self.name+" ::::::")
1754 main.cleanup()
1755 main.exit()
1756
1757 def election_test_run(self):
1758 '''
Jon Halle3f39ff2015-01-13 11:50:53 -08001759 CLI command to run for leadership of the Election test application.
1760 NOTE: Requires installation of the onos-app-election feature
1761 Returns: Main.TRUE on success
1762 Main.FALSE on error
Jon Hall94fd0472014-12-08 11:52:42 -08001763 '''
1764 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001765 cmd_str = "election-test-run"
1766 response = self.sendline( cmd_str )
Jon Hall94fd0472014-12-08 11:52:42 -08001767 #success
Jon Halle3f39ff2015-01-13 11:50:53 -08001768 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
1769 "Election\sapp."
1770 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001771 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001772 main.log.info( self.name + " entering leadership elections " +
1773 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001774 return main.TRUE
1775 #error
Jon Halle3f39ff2015-01-13 11:50:53 -08001776 errorPattern = "Command\snot\sfound"
1777 if re.search( errorPattern, response ):
1778 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001779 return main.FALSE
1780 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001781 main.log.error( "Error in election_test_run: " +
1782 "unexpected response" )
1783 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001784 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001785 except pexpect.EOF:
1786 main.log.error(self.name + ": EOF exception found")
1787 main.log.error(self.name + ": " + self.handle.before)
1788 main.cleanup()
1789 main.exit()
1790 except:
1791 main.log.info(self.name+" ::::::")
1792 main.log.error( traceback.print_exc())
1793 main.log.info(self.name+" ::::::")
1794 main.cleanup()
1795 main.exit()
1796
1797 def election_test_withdraw(self):
1798 '''
1799 * CLI command to withdraw the local node from leadership election for
1800 * the Election test application.
1801 #NOTE: Requires installation of the onos-app-election feature
1802 Returns: Main.TRUE on success
1803 Main.FALSE on error
1804 '''
1805 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001806 cmd_str = "election-test-withdraw"
1807 response = self.sendline( cmd_str )
Jon Hall94fd0472014-12-08 11:52:42 -08001808 #success
Jon Halle3f39ff2015-01-13 11:50:53 -08001809 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
1810 "\sthe\sElection\sapp."
1811 if re.search( successPattern, response ):
1812 main.log.info( self.name + " withdrawing from leadership " +
1813 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001814 return main.TRUE
1815 #error
Jon Halle3f39ff2015-01-13 11:50:53 -08001816 errorPattern = "Command\snot\sfound"
1817 if re.search( errorPattern, response ):
1818 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001819 return main.FALSE
1820 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001821 main.log.error( "Error in election_test_withdraw: " +
1822 "unexpected response" )
1823 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001824 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001825 except pexpect.EOF:
1826 main.log.error(self.name + ": EOF exception found")
1827 main.log.error(self.name + ": " + self.handle.before)
1828 main.cleanup()
1829 main.exit()
1830 except:
1831 main.log.info(self.name+" ::::::")
1832 main.log.error( traceback.print_exc())
1833 main.log.info(self.name+" ::::::")
1834 main.cleanup()
1835 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001836
andrewonlab7e4d2d32014-10-15 13:23:21 -04001837 #***********************************
Hari Krishna416c3ca2014-12-19 15:39:31 -08001838 def getDevicePortsEnabledCount(self,dpid):
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001839 '''
1840 Get the count of all enabled ports on a particular device/switch
1841 '''
1842 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001843 dpid = str( dpid )
1844 cmd_str = "onos:ports -e " + dpid + " | wc -l"
1845 output = self.sendline( cmd_str )
1846 if re.search( "No such device", output ):
1847 main.log.error( "Error in getting ports" )
1848 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001849 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001850 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001851 except pexpect.EOF:
1852 main.log.error(self.name + ": EOF exception found")
1853 main.log.error(self.name + ": " + self.handle.before)
1854 main.cleanup()
1855 main.exit()
1856 except:
1857 main.log.info(self.name+" ::::::")
1858 main.log.error( traceback.print_exc())
1859 main.log.info(self.name+" ::::::")
1860 main.cleanup()
1861 main.exit()
1862
1863 def getDeviceLinksActiveCount(self,dpid):
1864 '''
1865 Get the count of all enabled ports on a particular device/switch
1866 '''
1867 try:
Hari Krishna2bbaa702014-12-19 14:46:12 -08001868 dpid = str(dpid)
Jon Halle3f39ff2015-01-13 11:50:53 -08001869 cmd_str = "onos:links " + dpid + " | grep ACTIVE | wc -l"
1870 output = self.sendline( cmd_str )
1871 if re.search( "No such device", output ):
1872 main.log.error( "Error in getting ports ")
1873 return ( output, "Error ")
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001874 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001875 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001876 except pexpect.EOF:
1877 main.log.error(self.name + ": EOF exception found")
1878 main.log.error(self.name + ": " + self.handle.before)
1879 main.cleanup()
1880 main.exit()
1881 except:
1882 main.log.info(self.name+" ::::::")
1883 main.log.error( traceback.print_exc())
1884 main.log.info(self.name+" ::::::")
1885 main.cleanup()
1886 main.exit()
1887
1888 def getAllIntentIds(self):
1889 '''
1890 Return a list of all Intent IDs
1891 '''
1892 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001893 cmd_str = "onos:intents | grep id="
1894 output = self.sendline( cmd_str )
1895 if re.search( "Error", output ):
1896 main.log.error( "Error in getting ports" )
1897 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001898 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001899 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001900 except pexpect.EOF:
1901 main.log.error(self.name + ": EOF exception found")
1902 main.log.error(self.name + ": " + self.handle.before)
1903 main.cleanup()
1904 main.exit()
1905 except:
1906 main.log.info(self.name+" ::::::")
1907 main.log.error( traceback.print_exc())
1908 main.log.info(self.name+" ::::::")
1909 main.cleanup()
1910 main.exit()