blob: ffafb0630144de6e4f7caf9c67dfdf8c7782d006 [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
3'''
4This driver enters the onos> prompt to issue commands.
5
6Please follow the coding style demonstrated by existing
7functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040014shreya@onlab.us
andrewonlab95ce8322014-10-13 14:12:04 -040015
16OCT 13 2014
17
18'''
19
20import sys
21import time
22import pexpect
23import re
24import traceback
25import os.path
26import pydoc
Jon Halla001c392014-10-17 18:50:59 -040027import re
andrewonlab95ce8322014-10-13 14:12:04 -040028sys.path.append("../")
29from drivers.common.clidriver import CLI
30
31class OnosCliDriver(CLI):
32
33 def __init__(self):
34 '''
35 Initialize client
36 '''
37 super(CLI, self).__init__()
38
39 def connect(self,**connectargs):
40 '''
41 Creates ssh handle for ONOS cli.
42 '''
43 try:
44 for key in connectargs:
45 vars(self)[key] = connectargs[key]
46 self.home = "~/ONOS"
47 for key in self.options:
48 if key == "home":
49 self.home = self.options['home']
50 break
51
52
53 self.name = self.options['name']
54 self.handle = super(OnosCliDriver,self).connect(
55 user_name = self.user_name,
56 ip_address = self.ip_address,
57 port = self.port,
58 pwd = self.pwd,
59 home = self.home)
60
61 self.handle.sendline("cd "+ self.home)
62 self.handle.expect("\$")
63 if self.handle:
64 return self.handle
65 else :
66 main.log.info("NO ONOS HANDLE")
67 return main.FALSE
68 except pexpect.EOF:
69 main.log.error(self.name + ": EOF exception found")
70 main.log.error(self.name + ": " + self.handle.before)
71 main.cleanup()
72 main.exit()
73 except:
andrewonlab9627f432014-11-14 12:45:10 -050074 main.log.info(self.name + ":::::::::::::::::::::::")
andrewonlab95ce8322014-10-13 14:12:04 -040075 main.log.error( traceback.print_exc() )
andrewonlab9627f432014-11-14 12:45:10 -050076 main.log.info(":::::::::::::::::::::::")
andrewonlab95ce8322014-10-13 14:12:04 -040077 main.cleanup()
78 main.exit()
79
80 def disconnect(self):
81 '''
82 Called when Test is complete to disconnect the ONOS handle.
83 '''
84 response = ''
85 try:
andrewonlab2a6c9342014-10-16 13:40:15 -040086 self.handle.sendline("")
Jon Hall7e5b9172014-10-22 12:32:47 -040087 i = self.handle.expect(["onos>","\$"])
88 if i == 0:
89 self.handle.sendline("system:shutdown")
90 self.handle.expect("Confirm")
91 self.handle.sendline("yes")
92 self.handle.expect("\$")
Jon Hallffb386d2014-11-21 13:43:38 -080093 self.handle.sendline("")
andrewonlabc2d05aa2014-10-13 16:51:10 -040094 self.handle.expect("\$")
Jon Hall7e5b9172014-10-22 12:32:47 -040095 self.handle.sendline("exit")
96 self.handle.expect("closed")
andrewonlabc2d05aa2014-10-13 16:51:10 -040097
andrewonlab95ce8322014-10-13 14:12:04 -040098 except pexpect.EOF:
99 main.log.error(self.name + ": EOF exception found")
100 main.log.error(self.name + ": " + self.handle.before)
101 except:
102 main.log.error(self.name + ": Connection failed to the host")
103 response = main.FALSE
104 return response
105
andrewonlab38d2b4a2014-11-13 16:28:47 -0500106 def logout(self):
107 '''
108 Sends 'logout' command to ONOS cli
109 '''
110 try:
andrewonlab9627f432014-11-14 12:45:10 -0500111 self.handle.sendline("")
112 i = self.handle.expect([
113 "onos>",
114 "\$"], timeout=10)
115 if i == 0:
116 self.handle.sendline("logout")
117 self.handle.expect("\$")
118 elif i == 1:
119 return main.TRUE
120
andrewonlab38d2b4a2014-11-13 16:28:47 -0500121 except pexpect.EOF:
122 main.log.error(self.name + ": eof exception found")
andrewonlab9627f432014-11-14 12:45:10 -0500123 main.log.error(self.name + ": " +
124 self.handle.before)
andrewonlab38d2b4a2014-11-13 16:28:47 -0500125 main.cleanup()
126 main.exit()
127 except:
128 main.log.info(self.name+" ::::::")
129 main.log.error( traceback.print_exc())
130 main.log.info(self.name+" ::::::")
131 main.cleanup()
132 main.exit()
133
andrewonlab95ce8322014-10-13 14:12:04 -0400134 def set_cell(self, cellname):
135 '''
136 Calls 'cell <name>' to set the environment variables on ONOSbench
137
138 Before issuing any cli commands, set the environment variable first.
139 '''
140 try:
141 if not cellname:
142 main.log.error("Must define cellname")
143 main.cleanup()
144 main.exit()
145 else:
146 self.handle.sendline("cell "+str(cellname))
147 #Expect the cellname in the ONOS_CELL variable.
148 #Note that this variable name is subject to change
149 # and that this driver will have to change accordingly
150 self.handle.expect("ONOS_CELL="+str(cellname))
151 handle_before = self.handle.before
152 handle_after = self.handle.after
153 #Get the rest of the handle
154 self.handle.sendline("")
155 self.handle.expect("\$")
156 handle_more = self.handle.before
157
158 main.log.info("Cell call returned: "+handle_before+
159 handle_after + handle_more)
160
161 return main.TRUE
162
163 except pexpect.EOF:
andrewonlab38d2b4a2014-11-13 16:28:47 -0500164 main.log.error(self.name + ": eof exception found")
andrewonlab95ce8322014-10-13 14:12:04 -0400165 main.log.error(self.name + ": " + self.handle.before)
166 main.cleanup()
167 main.exit()
168 except:
169 main.log.info(self.name+" ::::::")
170 main.log.error( traceback.print_exc())
171 main.log.info(self.name+" ::::::")
172 main.cleanup()
173 main.exit()
174
andrewonlabc2d05aa2014-10-13 16:51:10 -0400175 def start_onos_cli(self, ONOS_ip):
andrewonlab95ce8322014-10-13 14:12:04 -0400176 try:
177 self.handle.sendline("")
andrewonlab48829f62014-11-17 13:49:01 -0500178 x = self.handle.expect([
179 "\$", "onos>"], timeout=10)
180
181 if x == 1:
182 main.log.info("ONOS cli is already running")
183 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400184
185 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400186 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400187 i = self.handle.expect([
188 "onos>",
189 pexpect.TIMEOUT],timeout=60)
190
191 if i == 0:
192 main.log.info(str(ONOS_ip)+" CLI Started successfully")
193 return main.TRUE
194 else:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400195 #If failed, send ctrl+c to process and try again
andrewonlabf47993a2014-10-24 17:56:01 -0400196 main.log.info("Starting CLI failed. Retrying...")
Jon Hallffb386d2014-11-21 13:43:38 -0800197 self.handle.send("\x03")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400198 self.handle.sendline("onos -w "+str(ONOS_ip))
199 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
200 timeout=30)
201 if i == 0:
andrewonlab28ca4b22014-11-20 13:15:59 -0500202 main.log.info(str(ONOS_ip)+" CLI Started "+
203 "successfully after retry attempt")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400204 return main.TRUE
205 else:
206 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400207 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400208 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400209
210 except pexpect.EOF:
211 main.log.error(self.name + ": EOF exception found")
212 main.log.error(self.name + ": " + self.handle.before)
213 main.cleanup()
214 main.exit()
215 except:
216 main.log.info(self.name+" ::::::")
217 main.log.error( traceback.print_exc())
218 main.log.info(self.name+" ::::::")
219 main.cleanup()
220 main.exit()
221
andrewonlaba18f6bf2014-10-13 19:31:54 -0400222 def sendline(self, cmd_str):
223 '''
224 Send a completely user specified string to
225 the onos> prompt. Use this function if you have
226 a very specific command to send.
227
228 Warning: There are no sanity checking to commands
229 sent using this method.
230 '''
231 try:
232 self.handle.sendline("")
233 self.handle.expect("onos>")
234
235 self.handle.sendline(cmd_str)
236 self.handle.expect("onos>")
237
238 handle = self.handle.before
239
240 self.handle.sendline("")
241 self.handle.expect("onos>")
242
243 handle += self.handle.before
244 handle += self.handle.after
245
246 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400247 ansi_escape = re.compile(r'\x1b[^m]*m')
248 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400249
250 return handle
251 except pexpect.EOF:
252 main.log.error(self.name + ": EOF exception found")
253 main.log.error(self.name + ": " + self.handle.before)
254 main.cleanup()
255 main.exit()
256 except:
257 main.log.info(self.name+" ::::::")
258 main.log.error( traceback.print_exc())
259 main.log.info(self.name+" ::::::")
260 main.cleanup()
261 main.exit()
262
andrewonlab95ce8322014-10-13 14:12:04 -0400263 #IMPORTANT NOTE:
264 #For all cli commands, naming convention should match
265 #the cli command replacing ':' with '_'.
266 #Ex) onos:topology > onos_topology
267 # onos:links > onos_links
268 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400269
270 def add_node(self, node_id, ONOS_ip, tcp_port=""):
271 '''
272 Adds a new cluster node by ID and address information.
273 Required:
274 * node_id
275 * ONOS_ip
276 Optional:
277 * tcp_port
278 '''
279 try:
280 self.handle.sendline("")
281 self.handle.expect("onos>")
282
283 self.handle.sendline("add-node "+
284 str(node_id)+" "+
285 str(ONOS_ip)+" "+
286 str(tcp_port))
287
288 i = self.handle.expect([
289 "Error",
290 "onos>" ])
291
292 #Clear handle to get previous output
293 self.handle.sendline("")
294 self.handle.expect("onos>")
295
296 handle = self.handle.before
297
298 if i == 0:
299 main.log.error("Error in adding node")
300 main.log.error(handle)
301 return main.FALSE
302 else:
303 main.log.info("Node "+str(ONOS_ip)+" added")
304 return main.TRUE
305
306 except pexpect.EOF:
307 main.log.error(self.name + ": EOF exception found")
308 main.log.error(self.name + ": " + self.handle.before)
309 main.cleanup()
310 main.exit()
311 except:
312 main.log.info(self.name+" ::::::")
313 main.log.error( traceback.print_exc())
314 main.log.info(self.name+" ::::::")
315 main.cleanup()
316 main.exit()
317
andrewonlab86dc3082014-10-13 18:18:38 -0400318 def remove_node(self, node_id):
319 '''
320 Removes a cluster by ID
321 Issues command: 'remove-node [<node-id>]'
322 Required:
323 * node_id
324 '''
325 try:
326 self.handle.sendline("")
327 self.handle.expect("onos>")
328
329 self.handle.sendline("remove-node "+str(node_id))
330 self.handle.expect("onos>")
331
332 return main.TRUE
333
334 except pexpect.EOF:
335 main.log.error(self.name + ": EOF exception found")
336 main.log.error(self.name + ": " + self.handle.before)
337 main.cleanup()
338 main.exit()
339 except:
340 main.log.info(self.name+" ::::::")
341 main.log.error( traceback.print_exc())
342 main.log.info(self.name+" ::::::")
343 main.cleanup()
344 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400345
andrewonlab7c211572014-10-15 16:45:20 -0400346 def nodes(self):
347 '''
348 List the nodes currently visible
349 Issues command: 'nodes'
350 Returns: entire handle of list of nodes
351 '''
352 try:
353 self.handle.sendline("")
354 self.handle.expect("onos>")
355
356 self.handle.sendline("nodes")
357 self.handle.expect("onos>")
358
359 self.handle.sendline("")
360 self.handle.expect("onos>")
361
362 handle = self.handle.before
363
364 return handle
365
366 except pexpect.EOF:
367 main.log.error(self.name + ": EOF exception found")
368 main.log.error(self.name + ": " + self.handle.before)
369 main.cleanup()
370 main.exit()
371 except:
372 main.log.info(self.name+" ::::::")
373 main.log.error( traceback.print_exc())
374 main.log.info(self.name+" ::::::")
375 main.cleanup()
376 main.exit()
377
andrewonlab38d6ae22014-10-15 14:23:45 -0400378 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400379 '''
380 Shows the current state of the topology
381 by issusing command: 'onos> onos:topology'
382 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400383 try:
384 self.handle.sendline("")
385 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400386 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400387 self.handle.sendline("onos:topology")
388 self.handle.expect("onos>")
389
390 handle = self.handle.before
391
392 main.log.info("onos:topology returned: " +
393 str(handle))
394
395 return handle
396
397 except pexpect.EOF:
398 main.log.error(self.name + ": EOF exception found")
399 main.log.error(self.name + ": " + self.handle.before)
400 main.cleanup()
401 main.exit()
402 except:
403 main.log.info(self.name+" ::::::")
404 main.log.error( traceback.print_exc())
405 main.log.info(self.name+" ::::::")
406 main.cleanup()
407 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400408
409 def feature_install(self, feature_str):
410 '''
411 Installs a specified feature
412 by issuing command: 'onos> feature:install <feature_str>'
413 '''
414 try:
415 self.handle.sendline("")
416 self.handle.expect("onos>")
417
418 self.handle.sendline("feature:install "+str(feature_str))
419 self.handle.expect("onos>")
420
421 return main.TRUE
422
423 except pexpect.EOF:
424 main.log.error(self.name + ": EOF exception found")
425 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500426 main.log.report("Failed to install feature")
427 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400428 main.cleanup()
429 main.exit()
430 except:
431 main.log.info(self.name+" ::::::")
432 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500433 main.log.report("Failed to install feature")
434 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400435 main.log.info(self.name+" ::::::")
436 main.cleanup()
437 main.exit()
438
439 def feature_uninstall(self, feature_str):
440 '''
441 Uninstalls a specified feature
442 by issuing command: 'onos> feature:uninstall <feature_str>'
443 '''
444 try:
445 self.handle.sendline("")
446 self.handle.expect("onos>")
447
448 self.handle.sendline("feature:uninstall "+str(feature_str))
449 self.handle.expect("onos>")
450
451 return main.TRUE
452
453 except pexpect.EOF:
454 main.log.error(self.name + ": EOF exception found")
455 main.log.error(self.name + ": " + self.handle.before)
456 main.cleanup()
457 main.exit()
458 except:
459 main.log.info(self.name+" ::::::")
460 main.log.error( traceback.print_exc())
461 main.log.info(self.name+" ::::::")
462 main.cleanup()
463 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800464
465 def devices(self, json_format=True):
andrewonlab86dc3082014-10-13 18:18:38 -0400466 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400467 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400468 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800469 * json_format - boolean indicating if you want output in json
andrewonlab86dc3082014-10-13 18:18:38 -0400470 '''
471 try:
472 self.handle.sendline("")
473 self.handle.expect("onos>")
andrewonlabb66dfa12014-12-02 15:51:10 -0500474
Jon Halle8217482014-10-17 13:49:14 -0400475 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800476 self.handle.sendline("devices -j")
477 self.handle.expect("devices -j")
478 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400479 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400480 '''
481 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
482 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 -0400483 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 -0400484 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400485 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400486 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400487 '''
488 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400489 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
490 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400491 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400492 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400493 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800494 self.handle.sendline("devices")
495 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400496 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400497 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400498 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400499 except pexpect.EOF:
500 main.log.error(self.name + ": EOF exception found")
501 main.log.error(self.name + ": " + self.handle.before)
502 main.cleanup()
503 main.exit()
504 except:
505 main.log.info(self.name+" ::::::")
506 main.log.error( traceback.print_exc())
507 main.log.info(self.name+" ::::::")
508 main.cleanup()
509 main.exit()
510
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800511
512 def balance_masters(self):
513 '''
514 This balances the devices across all controllers
515 by issuing command: 'onos> onos:balance-masters'
516 If required this could be extended to return devices balanced output.
517 '''
518 try:
519 self.handle.sendline("")
520 self.handle.expect("onos>")
521
522 self.handle.sendline("onos:balance-masters")
523 self.handle.expect("onos>")
524 return main.TRUE
525
526 except pexpect.EOF:
527 main.log.error(self.name + ": EOF exception found")
528 main.log.error(self.name + ": " + self.handle.before)
529 main.cleanup()
530 main.exit()
531 except:
532 main.log.info(self.name+" ::::::")
533 main.log.error( traceback.print_exc())
534 main.log.info(self.name+" ::::::")
535 main.cleanup()
536 main.exit()
537
538 def links(self, json_format=True, grep_str=""):
Jon Halle8217482014-10-17 13:49:14 -0400539 '''
540 Lists all core links
541 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800542 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400543 '''
544 try:
545 self.handle.sendline("")
546 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800547
Jon Halle8217482014-10-17 13:49:14 -0400548 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800549 self.handle.sendline("links -j")
550 self.handle.expect("links -j")
551 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400552 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400553 '''
554 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
555 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 -0400556 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 -0800557 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400558 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800559 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400560 '''
561 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400562 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
563 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400564 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400565 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400566 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800567 self.handle.sendline("links")
568 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400569 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400570 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400571 return handle
Jon Halle8217482014-10-17 13:49:14 -0400572 except pexpect.EOF:
573 main.log.error(self.name + ": EOF exception found")
574 main.log.error(self.name + ": " + self.handle.before)
575 main.cleanup()
576 main.exit()
577 except:
578 main.log.info(self.name+" ::::::")
579 main.log.error( traceback.print_exc())
580 main.log.info(self.name+" ::::::")
581 main.cleanup()
582 main.exit()
583
584
Jon Hallffb386d2014-11-21 13:43:38 -0800585 def ports(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400586 '''
587 Lists all ports
588 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800589 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400590 '''
591 try:
592 self.handle.sendline("")
593 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800594
Jon Halle8217482014-10-17 13:49:14 -0400595 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800596 self.handle.sendline("ports -j")
597 self.handle.expect("ports -j")
598 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400599 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400600 '''
601 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
602 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 -0400603 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 -0800604 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400605 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800606 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400607 '''
608 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400609 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
610 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400611 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400612 return handle1
613
Jon Halle8217482014-10-17 13:49:14 -0400614 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800615 self.handle.sendline("ports")
616 self.handle.expect("onos>")
617 self.handle.sendline("")
618 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400619 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400620 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800621 return handle
Jon Halle8217482014-10-17 13:49:14 -0400622 except pexpect.EOF:
623 main.log.error(self.name + ": EOF exception found")
624 main.log.error(self.name + ": " + self.handle.before)
625 main.cleanup()
626 main.exit()
627 except:
628 main.log.info(self.name+" ::::::")
629 main.log.error( traceback.print_exc())
630 main.log.info(self.name+" ::::::")
631 main.cleanup()
632 main.exit()
633
634
Jon Hallffb386d2014-11-21 13:43:38 -0800635 def roles(self, json_format=True):
andrewonlab7c211572014-10-15 16:45:20 -0400636 '''
Jon Hall983a1702014-10-28 18:44:22 -0400637 Lists all devices and the controllers with roles assigned to them
638 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800639 * json_format - boolean indicating if you want output in json
andrewonlab7c211572014-10-15 16:45:20 -0400640 '''
641 try:
642 self.handle.sendline("")
643 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500644
Jon Hall983a1702014-10-28 18:44:22 -0400645 if json_format:
Jon Hallb1290e82014-11-18 16:17:48 -0500646 self.handle.sendline("roles -j")
647 self.handle.expect("roles -j")
648 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400649 handle = self.handle.before
650 '''
Jon Hallb1290e82014-11-18 16:17:48 -0500651 handle variable here contains some ANSI escape color code sequences at the
652 end which are invisible in the print command output. To make that escape
653 sequence visible, use repr() function. The repr(handle) output when printed
654 shows the ANSI escape sequences. In json.loads(somestring), this somestring
655 variable is actually repr(somestring) and json.loads would fail with the escape
656 sequence.
657
658 So we take off that escape sequence using the following commads:
Jon Hall983a1702014-10-28 18:44:22 -0400659 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallb1290e82014-11-18 16:17:48 -0500660 handle1 = ansi_escape.sub('', handle)
Jon Hall983a1702014-10-28 18:44:22 -0400661 '''
662 #print "repr(handle) =", repr(handle)
663 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
664 handle1 = ansi_escape.sub('', handle)
665 #print "repr(handle1) = ", repr(handle1)
666 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400667
andrewonlab7c211572014-10-15 16:45:20 -0400668 else:
Jon Hallb1290e82014-11-18 16:17:48 -0500669 self.handle.sendline("roles")
670 self.handle.expect("onos>")
671 self.handle.sendline("")
672 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400673 handle = self.handle.before
674 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800675 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400676 except pexpect.EOF:
677 main.log.error(self.name + ": EOF exception found")
678 main.log.error(self.name + ": " + self.handle.before)
679 main.cleanup()
680 main.exit()
681 except:
682 main.log.info(self.name+" ::::::")
683 main.log.error( traceback.print_exc())
684 main.log.info(self.name+" ::::::")
685 main.cleanup()
686 main.exit()
687
688 def get_role(self, device_id):
689 '''
690 Given the a string containing the json representation of the "roles" cli command and a
691 partial or whole device id, returns a json object containing the
692 roles output for the first device whose id contains "device_id"
693
694 Returns:
695 Dict of the role assignments for the given device or
696 None if not match
697 '''
698 try:
699 import json
700 if device_id == None:
701 return None
702 else:
703 raw_roles = self.roles()
704 roles_json = json.loads(raw_roles)
705 #search json for the device with id then return the device
706 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400707 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400708 if str(device_id) in device['id']:
709 return device
710 return None
andrewonlab7c211572014-10-15 16:45:20 -0400711
andrewonlab86dc3082014-10-13 18:18:38 -0400712 except pexpect.EOF:
713 main.log.error(self.name + ": EOF exception found")
714 main.log.error(self.name + ": " + self.handle.before)
715 main.cleanup()
716 main.exit()
717 except:
718 main.log.info(self.name+" ::::::")
719 main.log.error( traceback.print_exc())
720 main.log.info(self.name+" ::::::")
721 main.cleanup()
722 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800723
724 def roles_not_null(self):
725 '''
726 Iterates through each device and checks if there is a master assigned
727 Returns: main.TRUE if each device has a master
728 main.FALSE any device has no master
729 '''
730 try:
731 import json
732 raw_roles = self.roles()
733 roles_json = json.loads(raw_roles)
734 #search json for the device with id then return the device
735 for device in roles_json:
736 #print device
737 if device['master'] == "none":
738 main.log.warn("Device has no master: " + str(device) )
739 return main.FALSE
740 return main.TRUE
741
742 except pexpect.EOF:
743 main.log.error(self.name + ": EOF exception found")
744 main.log.error(self.name + ": " + self.handle.before)
745 main.cleanup()
746 main.exit()
747 except:
748 main.log.info(self.name+" ::::::")
749 main.log.error( traceback.print_exc())
750 main.log.info(self.name+" ::::::")
751 main.cleanup()
752 main.exit()
753
754
andrewonlab3e15ead2014-10-15 14:21:34 -0400755 def paths(self, src_id, dst_id):
756 '''
757 Returns string of paths, and the cost.
758 Issues command: onos:paths <src> <dst>
759 '''
760 try:
761 self.handle.sendline("")
762 self.handle.expect("onos>")
763
764 self.handle.sendline("onos:paths "+
765 str(src_id) + " " + str(dst_id))
766 i = self.handle.expect([
767 "Error",
768 "onos>"])
769
770 self.handle.sendline("")
771 self.handle.expect("onos>")
772
773 handle = self.handle.before
774
775 if i == 0:
776 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400777 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400778 else:
779 path = handle.split(";")[0]
780 cost = handle.split(";")[1]
781 return (path, cost)
782
783 except pexpect.EOF:
784 main.log.error(self.name + ": EOF exception found")
785 main.log.error(self.name + ": " + self.handle.before)
786 main.cleanup()
787 main.exit()
788 except:
789 main.log.info(self.name+" ::::::")
790 main.log.error( traceback.print_exc())
791 main.log.info(self.name+" ::::::")
792 main.cleanup()
793 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800794
795 def hosts(self, json_format=True):
Jon Hall42db6dc2014-10-24 19:03:48 -0400796 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800797 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400798 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800799 * json_format - boolean indicating if you want output in json
Jon Hall42db6dc2014-10-24 19:03:48 -0400800 '''
801 try:
802 self.handle.sendline("")
803 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800804
Jon Hall42db6dc2014-10-24 19:03:48 -0400805 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800806 self.handle.sendline("hosts -j")
807 self.handle.expect("hosts -j")
808 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400809 handle = self.handle.before
810 '''
811 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
812 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
813 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 -0800814 So we take off that escape sequence using
Jon Hall42db6dc2014-10-24 19:03:48 -0400815 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800816 handle1 = ansi_escape.sub('', handle)
Jon Hall42db6dc2014-10-24 19:03:48 -0400817 '''
818 #print "repr(handle) =", repr(handle)
819 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
820 handle1 = ansi_escape.sub('', handle)
821 #print "repr(handle1) = ", repr(handle1)
822 return handle1
823 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800824 self.handle.sendline("hosts")
825 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400826 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400827 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400828 return handle
829 except pexpect.EOF:
830 main.log.error(self.name + ": EOF exception found")
831 main.log.error(self.name + ": " + self.handle.before)
832 main.cleanup()
833 main.exit()
834 except:
835 main.log.info(self.name+" ::::::")
836 main.log.error( traceback.print_exc())
837 main.log.info(self.name+" ::::::")
838 main.cleanup()
839 main.exit()
840
841 def get_host(self, mac):
842 '''
843 Return the first host from the hosts api whose 'id' contains 'mac'
844 Note: mac must be a colon seperated mac address, but could be a partial mac address
845 Return None if there is no match
846 '''
847 import json
848 try:
849 if mac == None:
850 return None
851 else:
852 mac = mac
853 raw_hosts = self.hosts()
854 hosts_json = json.loads(raw_hosts)
855 #search json for the host with mac then return the device
856 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400857 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400858 if mac in host['id']:
859 return host
860 return None
861 except pexpect.EOF:
862 main.log.error(self.name + ": EOF exception found")
863 main.log.error(self.name + ": " + self.handle.before)
864 main.cleanup()
865 main.exit()
866 except:
867 main.log.info(self.name+" ::::::")
868 main.log.error( traceback.print_exc())
869 main.log.info(self.name+" ::::::")
870 main.cleanup()
871 main.exit()
872
andrewonlab3f0a4af2014-10-17 12:25:14 -0400873
874 def get_hosts_id(self, host_list):
875 '''
876 Obtain list of hosts
877 Issues command: 'onos> hosts'
878
879 Required:
880 * host_list: List of hosts obtained by Mininet
881 IMPORTANT:
882 This function assumes that you started your
883 topology with the option '--mac'.
884 Furthermore, it assumes that value of VLAN is '-1'
885 Description:
886 Converts mininet hosts (h1, h2, h3...) into
887 ONOS format (00:00:00:00:00:01/-1 , ...)
888 '''
889
890 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400891 onos_host_list = []
892
893 for host in host_list:
894 host = host.replace("h", "")
895 host_hex = hex(int(host)).zfill(12)
896 host_hex = str(host_hex).replace('x','0')
897 i = iter(str(host_hex))
898 host_hex = ":".join(a+b for a,b in zip(i,i))
899 host_hex = host_hex + "/-1"
900 onos_host_list.append(host_hex)
901
902 return onos_host_list
903
904 except pexpect.EOF:
905 main.log.error(self.name + ": EOF exception found")
906 main.log.error(self.name + ": " + self.handle.before)
907 main.cleanup()
908 main.exit()
909 except:
910 main.log.info(self.name+" ::::::")
911 main.log.error( traceback.print_exc())
912 main.log.info(self.name+" ::::::")
913 main.cleanup()
914 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400915
andrewonlabe6745342014-10-17 14:29:13 -0400916 def add_host_intent(self, host_id_one, host_id_two):
917 '''
918 Required:
919 * host_id_one: ONOS host id for host1
920 * host_id_two: ONOS host id for host2
921 Description:
922 Adds a host-to-host intent (bidrectional) by
Jon Hallb1290e82014-11-18 16:17:48 -0500923 specifying the two hosts.
andrewonlabe6745342014-10-17 14:29:13 -0400924 '''
925 try:
926 self.handle.sendline("")
927 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500928
andrewonlabe6745342014-10-17 14:29:13 -0400929 self.handle.sendline("add-host-intent "+
930 str(host_id_one) + " " + str(host_id_two))
931 self.handle.expect("onos>")
932
andrewonlabe6745342014-10-17 14:29:13 -0400933 handle = self.handle.before
Jon Hallffb386d2014-11-21 13:43:38 -0800934 #print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400935
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800936 main.log.info("Host intent installed between "+
andrewonlabe6745342014-10-17 14:29:13 -0400937 str(host_id_one) + " and " + str(host_id_two))
938
939 return handle
Jon Hallb1290e82014-11-18 16:17:48 -0500940
andrewonlabe6745342014-10-17 14:29:13 -0400941 except pexpect.EOF:
942 main.log.error(self.name + ": EOF exception found")
943 main.log.error(self.name + ": " + self.handle.before)
944 main.cleanup()
945 main.exit()
946 except:
947 main.log.info(self.name+" ::::::")
948 main.log.error( traceback.print_exc())
949 main.log.info(self.name+" ::::::")
950 main.cleanup()
951 main.exit()
952
andrewonlab7b31d232014-10-24 13:31:47 -0400953 def add_optical_intent(self, ingress_device, egress_device):
954 '''
955 Required:
956 * ingress_device: device id of ingress device
957 * egress_device: device id of egress device
958 Optional:
959 TODO: Still needs to be implemented via dev side
960 '''
961 try:
962 self.handle.sendline("add-optical-intent "+
963 str(ingress_device) + " " + str(egress_device))
964 self.handle.expect("add-optical-intent")
965 i = self.handle.expect([
966 "Error",
967 "onos>"])
968
969 handle = self.handle.before
970
971 #If error, return error message
972 if i == 0:
973 return handle
974 else:
975 return main.TRUE
976
977 except pexpect.EOF:
978 main.log.error(self.name + ": EOF exception found")
979 main.log.error(self.name + ": " + self.handle.before)
980 main.cleanup()
981 main.exit()
982 except:
983 main.log.info(self.name+" ::::::")
984 main.log.error( traceback.print_exc())
985 main.log.info(self.name+" ::::::")
986 main.cleanup()
987 main.exit()
988
andrewonlab36af3822014-11-18 17:48:18 -0500989 def add_point_intent(self, ingress_device, egress_device,
990 port_ingress="", port_egress="", ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -0500991 ethDst="", bandwidth="", lambda_alloc=False,
992 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400993 '''
994 Required:
995 * ingress_device: device id of ingress device
996 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400997 Optional:
998 * ethType: specify ethType
999 * ethSrc: specify ethSrc (i.e. src mac addr)
1000 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001001 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -05001002 * lambda_alloc: if True, intent will allocate lambda
1003 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -05001004 * ipProto: specify ip protocol
1005 * ipSrc: specify ip source address
1006 * ipDst: specify ip destination address
1007 * tcpSrc: specify tcp source port
1008 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001009 Description:
1010 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -04001011 specifying device id's and optional fields
1012
andrewonlab4dbb4d82014-10-17 18:22:31 -04001013 NOTE: This function may change depending on the
1014 options developers provide for point-to-point
1015 intent via cli
1016 '''
1017 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001018 cmd = ""
1019
1020 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001021 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001022 and not bandwidth and not lambda_alloc \
1023 and not ipProto and not ipSrc and not ipDst \
1024 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001025 cmd = "add-point-intent"
1026
1027
andrewonlab289e4b72014-10-21 21:24:18 -04001028 else:
andrewonlab36af3822014-11-18 17:48:18 -05001029 cmd = "add-point-intent"
andrewonlab9a130be2014-10-22 12:44:56 -04001030
andrewonlab0c0a6772014-10-22 12:31:18 -04001031 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001032 cmd += " --ethType " + str(ethType)
1033 if ethSrc:
1034 cmd += " --ethSrc " + str(ethSrc)
1035 if ethDst:
1036 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001037 if bandwidth:
1038 cmd += " --bandwidth " + str(bandwidth)
1039 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001040 cmd += " --lambda "
1041 if ipProto:
1042 cmd += " --ipProto " + str(ipProto)
1043 if ipSrc:
1044 cmd += " --ipSrc " + str(ipSrc)
1045 if ipDst:
1046 cmd += " --ipDst " + str(ipDst)
1047 if tcpSrc:
1048 cmd += " --tcpSrc " + str(tcpSrc)
1049 if tcpDst:
1050 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001051
andrewonlab36af3822014-11-18 17:48:18 -05001052 #Check whether the user appended the port
1053 #or provided it as an input
1054 if "/" in ingress_device:
1055 cmd += " "+str(ingress_device)
1056 else:
1057 if not port_ingress:
1058 main.log.error("You must specify "+
1059 "the ingress port")
1060 #TODO: perhaps more meaningful return
1061 return main.FALSE
1062
1063 cmd += " "+ \
1064 str(ingress_device) + "/" +\
1065 str(port_ingress) + " "
1066
1067 if "/" in egress_device:
1068 cmd += " "+str(egress_device)
1069 else:
1070 if not port_egress:
1071 main.log.error("You must specify "+
1072 "the egress port")
1073 return main.FALSE
1074
1075 cmd += " "+\
1076 str(egress_device) + "/" +\
1077 str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001078
andrewonlab289e4b72014-10-21 21:24:18 -04001079 self.handle.sendline(cmd)
andrewonlab36af3822014-11-18 17:48:18 -05001080
1081 main.log.info(cmd + " sent")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001082 i = self.handle.expect([
1083 "Error",
1084 "onos>"])
andrewonlab4dbb4d82014-10-17 18:22:31 -04001085
1086 if i == 0:
1087 main.log.error("Error in adding point-to-point intent")
1088 return handle
1089 else:
1090 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001091
andrewonlab4dbb4d82014-10-17 18:22:31 -04001092 except pexpect.EOF:
1093 main.log.error(self.name + ": EOF exception found")
1094 main.log.error(self.name + ": " + self.handle.before)
1095 main.cleanup()
1096 main.exit()
1097 except:
1098 main.log.info(self.name+" ::::::")
1099 main.log.error( traceback.print_exc())
1100 main.log.info(self.name+" ::::::")
1101 main.cleanup()
1102 main.exit()
1103
shahshreyad0c80432014-12-04 16:56:05 -08001104
1105 def add_multipoint_to_singlepoint_intent(self, ingress_device1, ingress_device2,egress_device,
1106 port_ingress="", port_egress="", ethType="", ethSrc="",
1107 ethDst="", bandwidth="", lambda_alloc=False,
1108 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="", setEthDst=""):
1109 '''
1110 Note:
1111 This function assumes that there would be 2 ingress devices and one egress device
1112 For more number of ingress devices, this function needs to be modified
1113 Required:
1114 * ingress_device1: device id of ingress device1
1115 * ingress_device2: device id of ingress device2
1116 * egress_device: device id of egress device
1117 Optional:
1118 * ethType: specify ethType
1119 * ethSrc: specify ethSrc (i.e. src mac addr)
1120 * ethDst: specify ethDst (i.e. dst mac addr)
1121 * bandwidth: specify bandwidth capacity of link
1122 * lambda_alloc: if True, intent will allocate lambda
1123 for the specified intent
1124 * ipProto: specify ip protocol
1125 * ipSrc: specify ip source address
1126 * ipDst: specify ip destination address
1127 * tcpSrc: specify tcp source port
1128 * tcpDst: specify tcp destination port
1129 * setEthSrc: action to Rewrite Source MAC Address
1130 * setEthDst: action to Rewrite Destination MAC Address
1131 Description:
1132 Adds a multipoint-to-singlepoint intent (uni-directional) by
1133 specifying device id's and optional fields
1134
1135 NOTE: This function may change depending on the
1136 options developers provide for multipointpoint-to-singlepoint
1137 intent via cli
1138 '''
1139 try:
1140 cmd = ""
1141
1142 #If there are no optional arguments
1143 if not ethType and not ethSrc and not ethDst\
1144 and not bandwidth and not lambda_alloc \
1145 and not ipProto and not ipSrc and not ipDst \
1146 and not tcpSrc and not tcpDst and not setEthSrc and not setEthDst:
1147 cmd = "add-multi-to-single-intent"
1148
1149
1150 else:
1151 cmd = "add-multi-to-single-intent"
1152
1153 if ethType:
1154 cmd += " --ethType " + str(ethType)
1155 if ethSrc:
1156 cmd += " --ethSrc " + str(ethSrc)
1157 if ethDst:
1158 cmd += " --ethDst " + str(ethDst)
1159 if bandwidth:
1160 cmd += " --bandwidth " + str(bandwidth)
1161 if lambda_alloc:
1162 cmd += " --lambda "
1163 if ipProto:
1164 cmd += " --ipProto " + str(ipProto)
1165 if ipSrc:
1166 cmd += " --ipSrc " + str(ipSrc)
1167 if ipDst:
1168 cmd += " --ipDst " + str(ipDst)
1169 if tcpSrc:
1170 cmd += " --tcpSrc " + str(tcpSrc)
1171 if tcpDst:
1172 cmd += " --tcpDst " + str(tcpDst)
1173 if setEthSrc:
1174 cmd += " --setEthSrc "+ str(setEthSrc)
1175 if setEthDst:
1176 cmd += " --setEthDst "+ str(setEthDst)
1177
1178 #Check whether the user appended the port
1179 #or provided it as an input
1180 if "/" in ingress_device1:
1181 cmd += " "+str(ingress_device1)
1182 else:
1183 if not port_ingress1:
1184 main.log.error("You must specify "+
1185 "the ingress port1")
1186 #TODO: perhaps more meaningful return
1187 return main.FALSE
1188
1189 cmd += " "+ \
1190 str(ingress_device1) + "/" +\
1191 str(port_ingress1) + " "
1192
1193 if "/" in ingress_device2:
1194 cmd += " "+str(ingress_device2)
1195 else:
1196 if not port_ingress2:
1197 main.log.error("You must specify "+
1198 "the ingress port2")
1199 #TODO: perhaps more meaningful return
1200 return main.FALSE
1201
1202 cmd += " "+ \
1203 str(ingress_device2) + "/" +\
1204 str(port_ingress2) + " "
1205
1206 if "/" in egress_device:
1207 cmd += " "+str(egress_device)
1208 else:
1209 if not port_egress:
1210 main.log.error("You must specify "+
1211 "the egress port")
1212 return main.FALSE
1213
1214 cmd += " "+\
1215 str(egress_device) + "/" +\
1216 str(port_egress)
1217 print "cmd= ",cmd
1218 self.handle.sendline(cmd)
1219
1220 main.log.info(cmd + " sent")
1221 i = self.handle.expect([
1222 "Error",
1223 "onos>"])
1224
1225 if i == 0:
1226 main.log.error("Error in adding point-to-point intent")
1227 return self.handle
1228 else:
1229 return main.TRUE
1230
1231 except pexpect.EOF:
1232 main.log.error(self.name + ": EOF exception found")
1233 main.log.error(self.name + ": " + self.handle.before)
1234 main.cleanup()
1235 main.exit()
1236 except:
1237 main.log.info(self.name+" ::::::")
1238 main.log.error( traceback.print_exc())
1239 main.log.info(self.name+" ::::::")
1240 main.cleanup()
1241 main.exit()
1242
1243
andrewonlab9a50dfe2014-10-17 17:22:31 -04001244 def remove_intent(self, intent_id):
1245 '''
1246 Remove intent for specified intent id
1247 '''
1248 try:
1249 self.handle.sendline("")
1250 self.handle.expect("onos>")
1251
1252 self.handle.sendline("remove-intent "+str(intent_id))
1253 i = self.handle.expect([
1254 "Error",
1255 "onos>"])
1256
1257 handle = self.handle.before
1258
1259 if i == 0:
1260 main.log.error("Error in removing intent")
1261 return handle
1262 else:
1263 return handle
1264
1265 except pexpect.EOF:
1266 main.log.error(self.name + ": EOF exception found")
1267 main.log.error(self.name + ": " + self.handle.before)
1268 main.cleanup()
1269 main.exit()
1270 except:
1271 main.log.info(self.name+" ::::::")
1272 main.log.error( traceback.print_exc())
1273 main.log.info(self.name+" ::::::")
1274 main.cleanup()
1275 main.exit()
1276
pingping-lindabe7972014-11-17 19:29:44 -08001277 # This method should be used after installing application: onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001278 def routes(self, json_format=False):
1279 '''
1280 Optional:
1281 * json_format: enable output formatting in json
1282 Description:
1283 Obtain all routes in the system
1284 '''
1285 try:
1286 if json_format:
1287 self.handle.sendline("routes -j")
1288 self.handle.expect("routes -j")
1289 self.handle.expect("onos>")
1290 handle_tmp = self.handle.before
1291
1292 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1293 handle = ansi_escape.sub('', handle_tmp)
1294
1295 else:
1296 self.handle.sendline("")
1297 self.handle.expect("onos>")
1298
1299 self.handle.sendline("routes")
1300 self.handle.expect("onos>")
1301 handle = self.handle.before
1302
1303 return handle
1304
1305 except pexpect.EOF:
1306 main.log.error(self.name + ": EOF exception found")
1307 main.log.error(self.name + ": " + self.handle.before)
1308 main.cleanup()
1309 main.exit()
1310 except:
1311 main.log.info(self.name + " ::::::")
1312 main.log.error(traceback.print_exc())
1313 main.log.info(self.name + " ::::::")
1314 main.cleanup()
1315 main.exit()
1316
Jon Hallffb386d2014-11-21 13:43:38 -08001317 def intents(self, json_format = True):
andrewonlabe6745342014-10-17 14:29:13 -04001318 '''
andrewonlab377693f2014-10-21 16:00:30 -04001319 Optional:
1320 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001321 Description:
1322 Obtain intents currently installed
1323 '''
1324 try:
andrewonlab377693f2014-10-21 16:00:30 -04001325 if json_format:
1326 self.handle.sendline("intents -j")
1327 self.handle.expect("intents -j")
1328 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001329 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001330
1331 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1332 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001333 else:
1334 self.handle.sendline("")
1335 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001336
andrewonlab377693f2014-10-21 16:00:30 -04001337 self.handle.sendline("intents")
1338 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001339 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001340
1341 return handle
1342
1343 except pexpect.EOF:
1344 main.log.error(self.name + ": EOF exception found")
1345 main.log.error(self.name + ": " + self.handle.before)
1346 main.cleanup()
1347 main.exit()
1348 except:
1349 main.log.info(self.name+" ::::::")
1350 main.log.error( traceback.print_exc())
1351 main.log.info(self.name+" ::::::")
1352 main.cleanup()
1353 main.exit()
1354
Jon Hallffb386d2014-11-21 13:43:38 -08001355 def flows(self, json_format = True):
Shreya Shah0f01c812014-10-26 20:15:28 -04001356 '''
1357 Optional:
1358 * json_format: enable output formatting in json
1359 Description:
1360 Obtain flows currently installed
1361 '''
1362 try:
1363 if json_format:
1364 self.handle.sendline("flows -j")
1365 self.handle.expect("flows -j")
1366 self.handle.expect("onos>")
1367 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001368 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1369 handle = ansi_escape.sub('', handle)
Shreya Shah0f01c812014-10-26 20:15:28 -04001370
1371 else:
1372 self.handle.sendline("")
1373 self.handle.expect("onos>")
1374 self.handle.sendline("flows")
1375 self.handle.expect("onos>")
1376 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001377 if re.search("Error\sexecuting\scommand:", handle):
1378 main.log.error(self.name + ".flows() response: " + str(handle))
Shreya Shah0f01c812014-10-26 20:15:28 -04001379
1380 return handle
1381
1382 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
andrewonlabb66dfa12014-12-02 15:51:10 -05001394 def push_test_intents(self, dpid_src, dpid_dst, num_intents,
1395 num_mult="", app_id="", report=True):
andrewonlab87852b02014-11-19 18:44:19 -05001396 '''
1397 Description:
1398 Push a number of intents in a batch format to
1399 a specific point-to-point intent definition
1400 Required:
1401 * dpid_src: specify source dpid
1402 * dpid_dst: specify destination dpid
1403 * num_intents: specify number of intents to push
1404 Optional:
andrewonlabb66dfa12014-12-02 15:51:10 -05001405 * num_mult: number multiplier for multiplying
1406 the number of intents specified
1407 * app_id: specify the application id init to further
1408 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001409 * report: default True, returns latency information
1410 '''
1411 try:
1412 cmd = "push-test-intents "+\
1413 str(dpid_src)+" "+str(dpid_dst)+" "+\
1414 str(num_intents)
andrewonlabb66dfa12014-12-02 15:51:10 -05001415
1416 if num_mult:
1417 cmd += " " + str(num_mult)
andrewonlab042b3912014-12-10 16:40:50 -05001418 #If app id is specified, then num_mult
1419 #must exist because of the way this command
1420 #takes in arguments
andrewonlabb66dfa12014-12-02 15:51:10 -05001421 if app_id:
1422 cmd += " " + str(app_id)
1423
andrewonlab87852b02014-11-19 18:44:19 -05001424 self.handle.sendline(cmd)
1425 self.handle.expect(cmd)
1426 self.handle.expect("onos>")
1427
1428 handle = self.handle.before
1429
1430 #Some color thing that we want to escape
1431 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1432 handle = ansi_escape.sub('', handle)
1433
1434 if report:
andrewonlabb66dfa12014-12-02 15:51:10 -05001435 lat_result = []
andrewonlab87852b02014-11-19 18:44:19 -05001436 main.log.info(handle)
andrewonlabb66dfa12014-12-02 15:51:10 -05001437 #Split result by newline
1438 newline = handle.split("\r\r\n")
1439 #Ignore the first object of list, which is empty
1440 newline = newline[1:]
1441 #Some sloppy parsing method to get the latency
1442 for result in newline:
1443 result = result.split(": ")
1444 #Append the first result of second parse
1445 lat_result.append(result[1].split(" ")[0])
1446
andrewonlab042b3912014-12-10 16:40:50 -05001447 main.log.info(lat_result)
andrewonlabb66dfa12014-12-02 15:51:10 -05001448 return lat_result
andrewonlab87852b02014-11-19 18:44:19 -05001449 else:
1450 return main.TRUE
1451
1452 except pexpect.EOF:
1453 main.log.error(self.name + ": EOF exception found")
1454 main.log.error(self.name + ": " + self.handle.before)
1455 main.cleanup()
1456 main.exit()
1457 except:
1458 main.log.info(self.name+" ::::::")
1459 main.log.error( traceback.print_exc())
1460 main.log.info(self.name+" ::::::")
1461 main.cleanup()
1462 main.exit()
1463
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001464 def intents_events_metrics(self, json_format=True):
1465 '''
1466 Description:Returns topology metrics
1467 Optional:
1468 * json_format: enable json formatting of output
1469 '''
1470 try:
1471 if json_format:
1472 self.handle.sendline("intents-events-metrics -j")
1473 self.handle.expect("intents-events-metrics -j")
1474 self.handle.expect("onos>")
1475
1476 handle = self.handle.before
1477
1478 #Some color thing that we want to escape
1479 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1480 handle = ansi_escape.sub('', handle)
1481
1482 else:
1483 self.handle.sendline("intents-events-metrics")
1484 self.handle.expect("intents-events-metrics")
1485 self.handle.expect("onos>")
1486
1487 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001488
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001489 return handle
1490
1491 except pexpect.EOF:
1492 main.log.error(self.name + ": EOF exception found")
1493 main.log.error(self.name + ": " + self.handle.before)
1494 main.cleanup()
1495 main.exit()
1496 except:
1497 main.log.info(self.name+" ::::::")
1498 main.log.error( traceback.print_exc())
1499 main.log.info(self.name+" ::::::")
1500 main.cleanup()
1501 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001502
andrewonlab867212a2014-10-22 20:13:38 -04001503 def topology_events_metrics(self, json_format=True):
1504 '''
1505 Description:Returns topology metrics
1506 Optional:
1507 * json_format: enable json formatting of output
1508 '''
1509 try:
1510 if json_format:
1511 self.handle.sendline("topology-events-metrics -j")
1512 self.handle.expect("topology-events-metrics -j")
1513 self.handle.expect("onos>")
1514
1515 handle = self.handle.before
1516
1517 #Some color thing that we want to escape
1518 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1519 handle = ansi_escape.sub('', handle)
1520
1521 else:
1522 self.handle.sendline("topology-events-metrics")
1523 self.handle.expect("topology-events-metrics")
1524 self.handle.expect("onos>")
1525
1526 handle = self.handle.before
1527
1528 return handle
1529
1530 except pexpect.EOF:
1531 main.log.error(self.name + ": EOF exception found")
1532 main.log.error(self.name + ": " + self.handle.before)
1533 main.cleanup()
1534 main.exit()
1535 except:
1536 main.log.info(self.name+" ::::::")
1537 main.log.error( traceback.print_exc())
1538 main.log.info(self.name+" ::::::")
1539 main.cleanup()
1540 main.exit()
1541
andrewonlab3e15ead2014-10-15 14:21:34 -04001542 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001543 #Wrapper functions use existing driver
1544 #functions and extends their use case.
1545 #For example, we may use the output of
1546 #a normal driver function, and parse it
1547 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001548
andrewonlab9a50dfe2014-10-17 17:22:31 -04001549 def get_all_intents_id(self):
1550 '''
1551 Description:
1552 Obtain all intent id's in a list
1553 '''
1554 try:
1555 #Obtain output of intents function
1556 intents_str = self.intents()
1557 all_intent_list = []
1558 intent_id_list = []
1559
1560 #Parse the intents output for ID's
1561 intents_list = [s.strip() for s in intents_str.splitlines()]
1562 for intents in intents_list:
1563 if "onos>" in intents:
1564 continue
1565 elif "intents" in intents:
1566 continue
1567 else:
1568 line_list = intents.split(" ")
1569 all_intent_list.append(line_list[0])
1570
1571 all_intent_list = all_intent_list[1:-2]
1572
1573 for intents in all_intent_list:
1574 if not intents:
1575 continue
1576 else:
1577 intent_id_list.append(intents)
1578
1579 return intent_id_list
1580
1581 except pexpect.EOF:
1582 main.log.error(self.name + ": EOF exception found")
1583 main.log.error(self.name + ": " + self.handle.before)
1584 main.cleanup()
1585 main.exit()
1586 except:
1587 main.log.info(self.name+" ::::::")
1588 main.log.error( traceback.print_exc())
1589 main.log.info(self.name+" ::::::")
1590 main.cleanup()
1591 main.exit()
1592
andrewonlab7e4d2d32014-10-15 13:23:21 -04001593 def get_all_devices_id(self):
1594 '''
1595 Use 'devices' function to obtain list of all devices
1596 and parse the result to obtain a list of all device
1597 id's. Returns this list. Returns empty list if no
1598 devices exist
1599 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001600
1601 This function may be useful if you are not sure of the
1602 device id, and wish to execute other commands using
1603 the ids. By obtaining the list of device ids on the fly,
1604 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001605 '''
1606 try:
1607 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001608 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001609 id_list = []
1610
1611 if not devices_str:
1612 main.log.info("There are no devices to get id from")
1613 return id_list
1614
1615 #Split the string into list by comma
1616 device_list = devices_str.split(",")
1617 #Get temporary list of all arguments with string 'id='
1618 temp_list = [dev for dev in device_list if "id=" in dev]
1619 #Split list further into arguments before and after string
1620 # 'id='. Get the latter portion (the actual device id) and
1621 # append to id_list
1622 for arg in temp_list:
1623 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001624 return id_list
1625
1626 except pexpect.EOF:
1627 main.log.error(self.name + ": EOF exception found")
1628 main.log.error(self.name + ": " + self.handle.before)
1629 main.cleanup()
1630 main.exit()
1631 except:
1632 main.log.info(self.name+" ::::::")
1633 main.log.error( traceback.print_exc())
1634 main.log.info(self.name+" ::::::")
1635 main.cleanup()
1636 main.exit()
1637
andrewonlab7c211572014-10-15 16:45:20 -04001638 def get_all_nodes_id(self):
1639 '''
1640 Uses 'nodes' function to obtain list of all nodes
1641 and parse the result of nodes to obtain just the
1642 node id's.
1643 Returns:
1644 list of node id's
1645 '''
1646 try:
1647 nodes_str = self.nodes()
1648 id_list = []
1649
1650 if not nodes_str:
1651 main.log.info("There are no nodes to get id from")
1652 return id_list
1653
1654 #Sample nodes_str output
1655 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1656
1657 #Split the string into list by comma
1658 nodes_list = nodes_str.split(",")
1659 temp_list = [node for node in nodes_list if "id=" in node]
1660 for arg in temp_list:
1661 id_list.append(arg.split("id=")[1])
1662
1663 return id_list
1664
1665 except pexpect.EOF:
1666 main.log.error(self.name + ": EOF exception found")
1667 main.log.error(self.name + ": " + self.handle.before)
1668 main.cleanup()
1669 main.exit()
1670 except:
1671 main.log.info(self.name+" ::::::")
1672 main.log.error( traceback.print_exc())
1673 main.log.info(self.name+" ::::::")
1674 main.cleanup()
1675 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001676
Jon Halla91c4dc2014-10-22 12:57:04 -04001677 def get_device(self, dpid=None):
1678 '''
1679 Return the first device from the devices api whose 'id' contains 'dpid'
1680 Return None if there is no match
1681 '''
1682 import json
1683 try:
1684 if dpid == None:
1685 return None
1686 else:
1687 dpid = dpid.replace(':', '')
1688 raw_devices = self.devices()
1689 devices_json = json.loads(raw_devices)
1690 #search json for the device with dpid then return the device
1691 for device in devices_json:
1692 #print "%s in %s?" % (dpid, device['id'])
1693 if dpid in device['id']:
1694 return device
1695 return None
1696 except pexpect.EOF:
1697 main.log.error(self.name + ": EOF exception found")
1698 main.log.error(self.name + ": " + self.handle.before)
1699 main.cleanup()
1700 main.exit()
1701 except:
1702 main.log.info(self.name+" ::::::")
1703 main.log.error( traceback.print_exc())
1704 main.log.info(self.name+" ::::::")
1705 main.cleanup()
1706 main.exit()
1707
Jon Hall42db6dc2014-10-24 19:03:48 -04001708 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1709 '''
1710 Checks the number of swithes & links that ONOS sees against the
1711 supplied values. By default this will report to main.log, but the
1712 log level can be specifid.
1713
1714 Params: ip = ip used for the onos cli
1715 numoswitch = expected number of switches
1716 numlink = expected number of links
1717 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1718
1719
1720 log_level can
1721
1722 Returns: main.TRUE if the number of switchs and links are correct,
1723 main.FALSE if the numer of switches and links is incorrect,
1724 and main.ERROR otherwise
1725 '''
1726
1727 try:
1728 topology = self.get_topology(ip)
1729 if topology == {}:
1730 return main.ERROR
1731 output = ""
1732 #Is the number of switches is what we expected
1733 devices = topology.get('devices',False)
1734 links = topology.get('links',False)
1735 if devices == False or links == False:
1736 return main.ERROR
1737 switch_check = ( int(devices) == int(numoswitch) )
1738 #Is the number of links is what we expected
1739 link_check = ( int(links) == int(numolink) )
1740 if (switch_check and link_check):
1741 #We expected the correct numbers
1742 output = output + "The number of links and switches match "\
1743 + "what was expected"
1744 result = main.TRUE
1745 else:
1746 output = output + \
1747 "The number of links and switches does not match what was expected"
1748 result = main.FALSE
1749 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1750 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1751 if log_level == "report":
1752 main.log.report(output)
1753 elif log_level == "warn":
1754 main.log.warn(output)
1755 else:
1756 main.log.info(output)
1757 return result
1758 except pexpect.EOF:
1759 main.log.error(self.name + ": EOF exception found")
1760 main.log.error(self.name + ": " + self.handle.before)
1761 main.cleanup()
1762 main.exit()
1763 except:
1764 main.log.info(self.name+" ::::::")
1765 main.log.error( traceback.print_exc())
1766 main.log.info(self.name+" ::::::")
1767 main.cleanup()
1768 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001769
1770 def device_role(self, device_id, onos_node, role="master"):
1771 '''
1772 Calls the device-role cli command.
1773 device_id must be the id of a device as seen in the onos devices command
1774 onos_node is the ip of one of the onos nodes in the cluster
1775 role must be either master, standby, or none
1776
Jon Hall94fd0472014-12-08 11:52:42 -08001777 Returns main.TRUE or main.FALSE based on argument verification.
Jon Hall983a1702014-10-28 18:44:22 -04001778 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001779 support that output
1780 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001781 try:
Jon Hall983a1702014-10-28 18:44:22 -04001782 #print "beginning device_role... \n\tdevice_id:" + device_id
1783 #print "\tonos_node: " + onos_node
1784 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001785 if role.lower() == "master" or \
1786 role.lower() == "standby" or \
1787 role.lower() == "none":
1788 self.handle.sendline("")
1789 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001790 self.handle.sendline("device-role " +
1791 str(device_id) + " " +
1792 str(onos_node) + " " +
1793 str(role))
1794 i= self.handle.expect(["Error","onos>"])
1795 if i == 0:
1796 output = str(self.handle.before)
1797 self.handle.expect("onos>")
1798 output = output + str(self.handle.before)
1799 main.log.error(self.name + ": " +
1800 output + '\033[0m')#end color output to escape any colours from the cli
1801 return main.ERROR
1802 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001803 self.handle.expect("onos>")
1804 return main.TRUE
1805 else:
1806 return main.FALSE
1807
1808 except pexpect.EOF:
1809 main.log.error(self.name + ": EOF exception found")
1810 main.log.error(self.name + ": " + self.handle.before)
1811 main.cleanup()
1812 main.exit()
1813 except:
1814 main.log.info(self.name+" ::::::")
1815 main.log.error( traceback.print_exc())
1816 main.log.info(self.name+" ::::::")
1817 main.cleanup()
1818 main.exit()
1819
Jon Hall73cf9cc2014-11-20 22:28:38 -08001820 def clusters(self, json_format=True):
1821 '''
1822 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001823 Optional argument:
1824 * json_format - boolean indicating if you want output in json
Jon Hall73cf9cc2014-11-20 22:28:38 -08001825 '''
1826 try:
1827 self.handle.sendline("")
1828 self.handle.expect("onos>")
1829
1830 if json_format:
1831 self.handle.sendline("clusters -j")
1832 self.handle.expect("clusters -j")
1833 self.handle.expect("onos>")
1834 handle = self.handle.before
1835 '''
1836 handle variable here contains some ANSI escape color code
1837 sequences at the end which are invisible in the print command
1838 output. To make that escape sequence visible, use repr() function.
1839 The repr(handle) output when printed shows the ANSI escape sequences.
1840 In json.loads(somestring), this somestring variable is actually
1841 repr(somestring) and json.loads would fail with the escape sequence.
1842 So we take off that escape sequence using
1843 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1844 handle1 = ansi_escape.sub('', handle)
1845 '''
1846 #print "repr(handle) =", repr(handle)
1847 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1848 handle1 = ansi_escape.sub('', handle)
1849 #print "repr(handle1) = ", repr(handle1)
1850 return handle1
1851 else:
Jon Hallffb386d2014-11-21 13:43:38 -08001852 self.handle.sendline("clusters")
Jon Hall73cf9cc2014-11-20 22:28:38 -08001853 self.handle.expect("onos>")
1854 handle = self.handle.before
1855 #print "handle =",handle
1856 return handle
1857 except pexpect.EOF:
1858 main.log.error(self.name + ": EOF exception found")
1859 main.log.error(self.name + ": " + self.handle.before)
1860 main.cleanup()
1861 main.exit()
1862 except:
1863 main.log.info(self.name+" ::::::")
1864 main.log.error( traceback.print_exc())
1865 main.log.info(self.name+" ::::::")
1866 main.cleanup()
1867 main.exit()
1868
Jon Hall94fd0472014-12-08 11:52:42 -08001869 def election_test_leader(self):
1870 '''
1871 * CLI command to get the current leader for the Election test application.
1872 #NOTE: Requires installation of the onos-app-election feature
1873 Returns: Node IP of the leader if one exists
1874 None if none exists
1875 Main.FALSE on error
1876 '''
1877 try:
1878 self.handle.sendline("election-test-leader")
1879 self.handle.expect("election-test-leader")
1880 self.handle.expect("onos>")
1881 response = self.handle.before
1882 #Leader
1883 node_search = re.search("The\scurrent\sleader\sfor\sthe\sElection\sapp\sis\s(?P<node>.+)\.", response)
1884 if node_search:
1885 node = node_search.group('node')
Jon Hall669173b2014-12-17 11:36:30 -08001886 main.log.info("Election-test-leader on "+str(self.name)+" found " + node + " as the leader")
Jon Hall94fd0472014-12-08 11:52:42 -08001887 return node
1888 #no leader
1889 null_search = re.search("There\sis\scurrently\sno\sleader\selected\sfor\sthe\sElection\sapp", response)
1890 if null_search:
Jon Hall669173b2014-12-17 11:36:30 -08001891 main.log.info("Election-test-leader found no leader on " + self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001892 return None
1893 #error
Jon Hall669173b2014-12-17 11:36:30 -08001894 if re.search("Command\snot\sfound", response):
1895 main.log.error("Election app is not loaded on " + self.name)
1896 return main.FALSE
1897 else:
1898 main.log.error("Error in election_test_leader: unexpected response")
1899 main.log.error( repr(response) )
1900 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001901
1902 except pexpect.EOF:
1903 main.log.error(self.name + ": EOF exception found")
1904 main.log.error(self.name + ": " + self.handle.before)
1905 main.cleanup()
1906 main.exit()
1907 except:
1908 main.log.info(self.name+" ::::::")
1909 main.log.error( traceback.print_exc())
1910 main.log.info(self.name+" ::::::")
1911 main.cleanup()
1912 main.exit()
1913
1914 def election_test_run(self):
1915 '''
1916 * CLI command to run for leadership of the Election test application.
1917 #NOTE: Requires installation of the onos-app-election feature
1918 Returns: Main.TRUE on success
1919 Main.FALSE on error
1920 '''
1921 try:
1922 self.handle.sendline("election-test-run")
1923 self.handle.expect("election-test-run")
1924 self.handle.expect("onos>")
1925 response = self.handle.before
1926 #success
1927 search = re.search("Entering\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1928 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001929 main.log.info(self.name + " entering leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001930 return main.TRUE
1931 #error
Jon Hall669173b2014-12-17 11:36:30 -08001932 if re.search("Command\snot\sfound", response):
1933 main.log.error("Election app is not loaded on " + self.name)
1934 return main.FALSE
1935 else:
1936 main.log.error("Error in election_test_run: unexpected response")
1937 main.log.error( repr(response) )
1938 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001939
1940 except pexpect.EOF:
1941 main.log.error(self.name + ": EOF exception found")
1942 main.log.error(self.name + ": " + self.handle.before)
1943 main.cleanup()
1944 main.exit()
1945 except:
1946 main.log.info(self.name+" ::::::")
1947 main.log.error( traceback.print_exc())
1948 main.log.info(self.name+" ::::::")
1949 main.cleanup()
1950 main.exit()
1951
1952 def election_test_withdraw(self):
1953 '''
1954 * CLI command to withdraw the local node from leadership election for
1955 * the Election test application.
1956 #NOTE: Requires installation of the onos-app-election feature
1957 Returns: Main.TRUE on success
1958 Main.FALSE on error
1959 '''
1960 try:
1961 self.handle.sendline("election-test-withdraw")
1962 self.handle.expect("election-test-withdraw")
1963 self.handle.expect("onos>")
1964 response = self.handle.before
1965 #success
1966 search = re.search("Withdrawing\sfrom\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1967 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001968 main.log.info(self.name + " withdrawing from leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001969 return main.TRUE
1970 #error
Jon Hall669173b2014-12-17 11:36:30 -08001971 if re.search("Command\snot\sfound", response):
1972 main.log.error("Election app is not loaded on " + self.name)
1973 return main.FALSE
1974 else:
1975 main.log.error("Error in election_test_withdraw: unexpected response")
1976 main.log.error( repr(response) )
1977 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001978
1979
1980 except pexpect.EOF:
1981 main.log.error(self.name + ": EOF exception found")
1982 main.log.error(self.name + ": " + self.handle.before)
1983 main.cleanup()
1984 main.exit()
1985 except:
1986 main.log.info(self.name+" ::::::")
1987 main.log.error( traceback.print_exc())
1988 main.log.info(self.name+" ::::::")
1989 main.cleanup()
1990 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001991
andrewonlab7e4d2d32014-10-15 13:23:21 -04001992 #***********************************
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001993 def getDevicePortsEnabledCount(self,dpid):
1994 '''
1995 Get the count of all enabled ports on a particular device/switch
1996 '''
1997 try:
1998 self.handle.sendline("")
1999 self.handle.expect("onos>")
2000
2001 self.handle.sendline("onos:ports -e "+dpid+" | wc -l")
2002 i = self.handle.expect([
2003 "No such device",
2004 "onos>"])
2005
2006 #self.handle.sendline("")
2007 #self.handle.expect("onos>")
2008
2009 output = self.handle.before
2010
2011 if i == 0:
2012 main.log.error("Error in getting ports")
2013 return (ouput, "Error")
2014 else:
2015 result = output
2016 return result
2017
2018 except pexpect.EOF:
2019 main.log.error(self.name + ": EOF exception found")
2020 main.log.error(self.name + ": " + self.handle.before)
2021 main.cleanup()
2022 main.exit()
2023 except:
2024 main.log.info(self.name+" ::::::")
2025 main.log.error( traceback.print_exc())
2026 main.log.info(self.name+" ::::::")
2027 main.cleanup()
2028 main.exit()
2029
2030 def getDeviceLinksActiveCount(self,dpid):
2031 '''
2032 Get the count of all enabled ports on a particular device/switch
2033 '''
2034 try:
2035 self.handle.sendline("")
2036 self.handle.expect("onos>")
2037
2038 self.handle.sendline("onos:links "+dpid+" | grep ACTIVE | wc -l")
2039 i = self.handle.expect([
2040 "No such device",
2041 "onos>"])
2042
2043 output = self.handle.before
2044
2045 if i == 0:
2046 main.log.error("Error in getting ports")
2047 return (ouput, "Error")
2048 else:
2049 result = output
2050 return result
2051
2052 except pexpect.EOF:
2053 main.log.error(self.name + ": EOF exception found")
2054 main.log.error(self.name + ": " + self.handle.before)
2055 main.cleanup()
2056 main.exit()
2057 except:
2058 main.log.info(self.name+" ::::::")
2059 main.log.error( traceback.print_exc())
2060 main.log.info(self.name+" ::::::")
2061 main.cleanup()
2062 main.exit()
2063
2064 def getAllIntentIds(self):
2065 '''
2066 Return a list of all Intent IDs
2067 '''
2068 try:
2069 self.handle.sendline("")
2070 self.handle.expect("onos>")
2071
2072 self.handle.sendline("onos:intents | grep id=")
2073 i = self.handle.expect([
2074 "Error",
2075 "onos>"])
2076
2077 output = self.handle.before
2078
2079 if i == 0:
2080 main.log.error("Error in getting ports")
2081 return (ouput, "Error")
2082 else:
2083 result = output
2084 return result
2085
2086 except pexpect.EOF:
2087 main.log.error(self.name + ": EOF exception found")
2088 main.log.error(self.name + ": " + self.handle.before)
2089 main.cleanup()
2090 main.exit()
2091 except:
2092 main.log.info(self.name+" ::::::")
2093 main.log.error( traceback.print_exc())
2094 main.log.info(self.name+" ::::::")
2095 main.cleanup()
2096 main.exit()