blob: ded960253488abf42ca2aad4f1beff08ba221850 [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
Hari Krishnae36ef212015-01-04 14:09:13 -0800175 def start_onos_cli(self, ONOS_ip, karafTimeout=""):
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")
Hari Krishnae36ef212015-01-04 14:09:13 -0800193 if karafTimeout:
194 self.handle.sendline("config:property-set -p org.apache.karaf.shell sshIdleTimeout "+karafTimeout)
195 self.handle.expect("\$")
196 self.handle.sendline("onos -w "+str(ONOS_ip))
197 self.handle.expect("onos>")
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400198 return main.TRUE
199 else:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400200 #If failed, send ctrl+c to process and try again
andrewonlabf47993a2014-10-24 17:56:01 -0400201 main.log.info("Starting CLI failed. Retrying...")
Jon Hallffb386d2014-11-21 13:43:38 -0800202 self.handle.send("\x03")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400203 self.handle.sendline("onos -w "+str(ONOS_ip))
204 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
205 timeout=30)
206 if i == 0:
andrewonlab28ca4b22014-11-20 13:15:59 -0500207 main.log.info(str(ONOS_ip)+" CLI Started "+
208 "successfully after retry attempt")
Hari Krishnae36ef212015-01-04 14:09:13 -0800209 if karafTimeout:
210 self.handle.sendline("config:property-set -p org.apache.karaf.shell sshIdleTimeout "+karafTimeout)
211 self.handle.expect("\$")
212 self.handle.sendline("onos -w "+str(ONOS_ip))
213 self.handle.expect("onos>")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400214 return main.TRUE
215 else:
216 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400217 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400218 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400219
220 except pexpect.EOF:
221 main.log.error(self.name + ": EOF exception found")
222 main.log.error(self.name + ": " + self.handle.before)
223 main.cleanup()
224 main.exit()
225 except:
226 main.log.info(self.name+" ::::::")
227 main.log.error( traceback.print_exc())
228 main.log.info(self.name+" ::::::")
229 main.cleanup()
230 main.exit()
231
andrewonlaba18f6bf2014-10-13 19:31:54 -0400232 def sendline(self, cmd_str):
233 '''
234 Send a completely user specified string to
235 the onos> prompt. Use this function if you have
236 a very specific command to send.
237
238 Warning: There are no sanity checking to commands
239 sent using this method.
240 '''
241 try:
242 self.handle.sendline("")
243 self.handle.expect("onos>")
244
245 self.handle.sendline(cmd_str)
246 self.handle.expect("onos>")
247
248 handle = self.handle.before
249
250 self.handle.sendline("")
251 self.handle.expect("onos>")
252
253 handle += self.handle.before
254 handle += self.handle.after
255
256 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400257 ansi_escape = re.compile(r'\x1b[^m]*m')
258 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400259
260 return handle
261 except pexpect.EOF:
262 main.log.error(self.name + ": EOF exception found")
263 main.log.error(self.name + ": " + self.handle.before)
264 main.cleanup()
265 main.exit()
266 except:
267 main.log.info(self.name+" ::::::")
268 main.log.error( traceback.print_exc())
269 main.log.info(self.name+" ::::::")
270 main.cleanup()
271 main.exit()
272
andrewonlab95ce8322014-10-13 14:12:04 -0400273 #IMPORTANT NOTE:
274 #For all cli commands, naming convention should match
275 #the cli command replacing ':' with '_'.
276 #Ex) onos:topology > onos_topology
277 # onos:links > onos_links
278 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400279
280 def add_node(self, node_id, ONOS_ip, tcp_port=""):
281 '''
282 Adds a new cluster node by ID and address information.
283 Required:
284 * node_id
285 * ONOS_ip
286 Optional:
287 * tcp_port
288 '''
289 try:
290 self.handle.sendline("")
291 self.handle.expect("onos>")
292
293 self.handle.sendline("add-node "+
294 str(node_id)+" "+
295 str(ONOS_ip)+" "+
296 str(tcp_port))
297
298 i = self.handle.expect([
299 "Error",
300 "onos>" ])
301
302 #Clear handle to get previous output
303 self.handle.sendline("")
304 self.handle.expect("onos>")
305
306 handle = self.handle.before
307
308 if i == 0:
309 main.log.error("Error in adding node")
310 main.log.error(handle)
311 return main.FALSE
312 else:
313 main.log.info("Node "+str(ONOS_ip)+" added")
314 return main.TRUE
315
316 except pexpect.EOF:
317 main.log.error(self.name + ": EOF exception found")
318 main.log.error(self.name + ": " + self.handle.before)
319 main.cleanup()
320 main.exit()
321 except:
322 main.log.info(self.name+" ::::::")
323 main.log.error( traceback.print_exc())
324 main.log.info(self.name+" ::::::")
325 main.cleanup()
326 main.exit()
327
andrewonlab86dc3082014-10-13 18:18:38 -0400328 def remove_node(self, node_id):
329 '''
330 Removes a cluster by ID
331 Issues command: 'remove-node [<node-id>]'
332 Required:
333 * node_id
334 '''
335 try:
336 self.handle.sendline("")
337 self.handle.expect("onos>")
338
339 self.handle.sendline("remove-node "+str(node_id))
340 self.handle.expect("onos>")
341
342 return main.TRUE
343
344 except pexpect.EOF:
345 main.log.error(self.name + ": EOF exception found")
346 main.log.error(self.name + ": " + self.handle.before)
347 main.cleanup()
348 main.exit()
349 except:
350 main.log.info(self.name+" ::::::")
351 main.log.error( traceback.print_exc())
352 main.log.info(self.name+" ::::::")
353 main.cleanup()
354 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400355
andrewonlab7c211572014-10-15 16:45:20 -0400356 def nodes(self):
357 '''
358 List the nodes currently visible
359 Issues command: 'nodes'
360 Returns: entire handle of list of nodes
361 '''
362 try:
363 self.handle.sendline("")
364 self.handle.expect("onos>")
365
366 self.handle.sendline("nodes")
367 self.handle.expect("onos>")
368
369 self.handle.sendline("")
370 self.handle.expect("onos>")
371
372 handle = self.handle.before
373
374 return handle
375
376 except pexpect.EOF:
377 main.log.error(self.name + ": EOF exception found")
378 main.log.error(self.name + ": " + self.handle.before)
379 main.cleanup()
380 main.exit()
381 except:
382 main.log.info(self.name+" ::::::")
383 main.log.error( traceback.print_exc())
384 main.log.info(self.name+" ::::::")
385 main.cleanup()
386 main.exit()
387
andrewonlab38d6ae22014-10-15 14:23:45 -0400388 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400389 '''
390 Shows the current state of the topology
391 by issusing command: 'onos> onos:topology'
392 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400393 try:
394 self.handle.sendline("")
395 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400396 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400397 self.handle.sendline("onos:topology")
398 self.handle.expect("onos>")
399
400 handle = self.handle.before
401
402 main.log.info("onos:topology returned: " +
403 str(handle))
404
405 return handle
406
407 except pexpect.EOF:
408 main.log.error(self.name + ": EOF exception found")
409 main.log.error(self.name + ": " + self.handle.before)
410 main.cleanup()
411 main.exit()
412 except:
413 main.log.info(self.name+" ::::::")
414 main.log.error( traceback.print_exc())
415 main.log.info(self.name+" ::::::")
416 main.cleanup()
417 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400418
419 def feature_install(self, feature_str):
420 '''
421 Installs a specified feature
422 by issuing command: 'onos> feature:install <feature_str>'
423 '''
424 try:
425 self.handle.sendline("")
426 self.handle.expect("onos>")
427
428 self.handle.sendline("feature:install "+str(feature_str))
429 self.handle.expect("onos>")
430
431 return main.TRUE
432
433 except pexpect.EOF:
434 main.log.error(self.name + ": EOF exception found")
435 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500436 main.log.report("Failed to install feature")
437 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400438 main.cleanup()
439 main.exit()
440 except:
441 main.log.info(self.name+" ::::::")
442 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500443 main.log.report("Failed to install feature")
444 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400445 main.log.info(self.name+" ::::::")
446 main.cleanup()
447 main.exit()
448
449 def feature_uninstall(self, feature_str):
450 '''
451 Uninstalls a specified feature
452 by issuing command: 'onos> feature:uninstall <feature_str>'
453 '''
454 try:
455 self.handle.sendline("")
456 self.handle.expect("onos>")
457
458 self.handle.sendline("feature:uninstall "+str(feature_str))
459 self.handle.expect("onos>")
460
461 return main.TRUE
462
463 except pexpect.EOF:
464 main.log.error(self.name + ": EOF exception found")
465 main.log.error(self.name + ": " + self.handle.before)
466 main.cleanup()
467 main.exit()
468 except:
469 main.log.info(self.name+" ::::::")
470 main.log.error( traceback.print_exc())
471 main.log.info(self.name+" ::::::")
472 main.cleanup()
473 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800474
475 def devices(self, json_format=True):
andrewonlab86dc3082014-10-13 18:18:38 -0400476 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400477 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400478 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800479 * json_format - boolean indicating if you want output in json
andrewonlab86dc3082014-10-13 18:18:38 -0400480 '''
481 try:
482 self.handle.sendline("")
483 self.handle.expect("onos>")
andrewonlabb66dfa12014-12-02 15:51:10 -0500484
Jon Halle8217482014-10-17 13:49:14 -0400485 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800486 self.handle.sendline("devices -j")
487 self.handle.expect("devices -j")
488 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400489 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400490 '''
491 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
492 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 -0400493 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 -0400494 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400495 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400496 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400497 '''
498 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400499 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
500 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400501 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400502 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400503 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800504 self.handle.sendline("devices")
505 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400506 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400507 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400508 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400509 except pexpect.EOF:
510 main.log.error(self.name + ": EOF exception found")
511 main.log.error(self.name + ": " + self.handle.before)
512 main.cleanup()
513 main.exit()
514 except:
515 main.log.info(self.name+" ::::::")
516 main.log.error( traceback.print_exc())
517 main.log.info(self.name+" ::::::")
518 main.cleanup()
519 main.exit()
520
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800521
522 def balance_masters(self):
523 '''
524 This balances the devices across all controllers
525 by issuing command: 'onos> onos:balance-masters'
526 If required this could be extended to return devices balanced output.
527 '''
528 try:
529 self.handle.sendline("")
530 self.handle.expect("onos>")
531
532 self.handle.sendline("onos:balance-masters")
533 self.handle.expect("onos>")
534 return main.TRUE
535
536 except pexpect.EOF:
537 main.log.error(self.name + ": EOF exception found")
538 main.log.error(self.name + ": " + self.handle.before)
539 main.cleanup()
540 main.exit()
541 except:
542 main.log.info(self.name+" ::::::")
543 main.log.error( traceback.print_exc())
544 main.log.info(self.name+" ::::::")
545 main.cleanup()
546 main.exit()
547
Hari Krishna2bbaa702014-12-19 14:46:12 -0800548 def links(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400549 '''
550 Lists all core links
551 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800552 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400553 '''
554 try:
555 self.handle.sendline("")
556 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800557
Jon Halle8217482014-10-17 13:49:14 -0400558 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800559 self.handle.sendline("links -j")
560 self.handle.expect("links -j")
561 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400562 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400563 '''
564 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
565 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 -0400566 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 -0800567 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400568 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800569 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400570 '''
571 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400572 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
573 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400574 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400575 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400576 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800577 self.handle.sendline("links")
578 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400579 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400580 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400581 return handle
Jon Halle8217482014-10-17 13:49:14 -0400582 except pexpect.EOF:
583 main.log.error(self.name + ": EOF exception found")
584 main.log.error(self.name + ": " + self.handle.before)
585 main.cleanup()
586 main.exit()
587 except:
588 main.log.info(self.name+" ::::::")
589 main.log.error( traceback.print_exc())
590 main.log.info(self.name+" ::::::")
591 main.cleanup()
592 main.exit()
593
594
Jon Hallffb386d2014-11-21 13:43:38 -0800595 def ports(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400596 '''
597 Lists all ports
598 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800599 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400600 '''
601 try:
602 self.handle.sendline("")
603 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800604
Jon Halle8217482014-10-17 13:49:14 -0400605 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800606 self.handle.sendline("ports -j")
607 self.handle.expect("ports -j")
608 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400609 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400610 '''
611 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
612 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 -0400613 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 -0800614 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400615 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800616 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400617 '''
618 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400619 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
620 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400621 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400622 return handle1
623
Jon Halle8217482014-10-17 13:49:14 -0400624 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800625 self.handle.sendline("ports")
626 self.handle.expect("onos>")
627 self.handle.sendline("")
628 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400629 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400630 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800631 return handle
Jon Halle8217482014-10-17 13:49:14 -0400632 except pexpect.EOF:
633 main.log.error(self.name + ": EOF exception found")
634 main.log.error(self.name + ": " + self.handle.before)
635 main.cleanup()
636 main.exit()
637 except:
638 main.log.info(self.name+" ::::::")
639 main.log.error( traceback.print_exc())
640 main.log.info(self.name+" ::::::")
641 main.cleanup()
642 main.exit()
643
644
Jon Hallffb386d2014-11-21 13:43:38 -0800645 def roles(self, json_format=True):
andrewonlab7c211572014-10-15 16:45:20 -0400646 '''
Jon Hall983a1702014-10-28 18:44:22 -0400647 Lists all devices and the controllers with roles assigned to them
648 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800649 * json_format - boolean indicating if you want output in json
andrewonlab7c211572014-10-15 16:45:20 -0400650 '''
651 try:
652 self.handle.sendline("")
653 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500654
Jon Hall983a1702014-10-28 18:44:22 -0400655 if json_format:
Jon Hallb1290e82014-11-18 16:17:48 -0500656 self.handle.sendline("roles -j")
657 self.handle.expect("roles -j")
658 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400659 handle = self.handle.before
660 '''
Jon Hallb1290e82014-11-18 16:17:48 -0500661 handle variable here contains some ANSI escape color code sequences at the
662 end which are invisible in the print command output. To make that escape
663 sequence visible, use repr() function. The repr(handle) output when printed
664 shows the ANSI escape sequences. In json.loads(somestring), this somestring
665 variable is actually repr(somestring) and json.loads would fail with the escape
666 sequence.
667
668 So we take off that escape sequence using the following commads:
Jon Hall983a1702014-10-28 18:44:22 -0400669 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallb1290e82014-11-18 16:17:48 -0500670 handle1 = ansi_escape.sub('', handle)
Jon Hall983a1702014-10-28 18:44:22 -0400671 '''
672 #print "repr(handle) =", repr(handle)
673 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
674 handle1 = ansi_escape.sub('', handle)
675 #print "repr(handle1) = ", repr(handle1)
676 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400677
andrewonlab7c211572014-10-15 16:45:20 -0400678 else:
Jon Hallb1290e82014-11-18 16:17:48 -0500679 self.handle.sendline("roles")
680 self.handle.expect("onos>")
681 self.handle.sendline("")
682 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400683 handle = self.handle.before
684 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800685 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400686 except pexpect.EOF:
687 main.log.error(self.name + ": EOF exception found")
688 main.log.error(self.name + ": " + self.handle.before)
689 main.cleanup()
690 main.exit()
691 except:
692 main.log.info(self.name+" ::::::")
693 main.log.error( traceback.print_exc())
694 main.log.info(self.name+" ::::::")
695 main.cleanup()
696 main.exit()
697
698 def get_role(self, device_id):
699 '''
700 Given the a string containing the json representation of the "roles" cli command and a
701 partial or whole device id, returns a json object containing the
702 roles output for the first device whose id contains "device_id"
703
704 Returns:
705 Dict of the role assignments for the given device or
706 None if not match
707 '''
708 try:
709 import json
710 if device_id == None:
711 return None
712 else:
713 raw_roles = self.roles()
714 roles_json = json.loads(raw_roles)
715 #search json for the device with id then return the device
716 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400717 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400718 if str(device_id) in device['id']:
719 return device
720 return None
andrewonlab7c211572014-10-15 16:45:20 -0400721
andrewonlab86dc3082014-10-13 18:18:38 -0400722 except pexpect.EOF:
723 main.log.error(self.name + ": EOF exception found")
724 main.log.error(self.name + ": " + self.handle.before)
725 main.cleanup()
726 main.exit()
727 except:
728 main.log.info(self.name+" ::::::")
729 main.log.error( traceback.print_exc())
730 main.log.info(self.name+" ::::::")
731 main.cleanup()
732 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800733
734 def roles_not_null(self):
735 '''
736 Iterates through each device and checks if there is a master assigned
737 Returns: main.TRUE if each device has a master
738 main.FALSE any device has no master
739 '''
740 try:
741 import json
742 raw_roles = self.roles()
743 roles_json = json.loads(raw_roles)
744 #search json for the device with id then return the device
745 for device in roles_json:
746 #print device
747 if device['master'] == "none":
748 main.log.warn("Device has no master: " + str(device) )
749 return main.FALSE
750 return main.TRUE
751
752 except pexpect.EOF:
753 main.log.error(self.name + ": EOF exception found")
754 main.log.error(self.name + ": " + self.handle.before)
755 main.cleanup()
756 main.exit()
757 except:
758 main.log.info(self.name+" ::::::")
759 main.log.error( traceback.print_exc())
760 main.log.info(self.name+" ::::::")
761 main.cleanup()
762 main.exit()
763
764
andrewonlab3e15ead2014-10-15 14:21:34 -0400765 def paths(self, src_id, dst_id):
766 '''
767 Returns string of paths, and the cost.
768 Issues command: onos:paths <src> <dst>
769 '''
770 try:
771 self.handle.sendline("")
772 self.handle.expect("onos>")
773
774 self.handle.sendline("onos:paths "+
775 str(src_id) + " " + str(dst_id))
776 i = self.handle.expect([
777 "Error",
778 "onos>"])
779
780 self.handle.sendline("")
781 self.handle.expect("onos>")
782
783 handle = self.handle.before
784
785 if i == 0:
786 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400787 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400788 else:
789 path = handle.split(";")[0]
790 cost = handle.split(";")[1]
791 return (path, cost)
792
793 except pexpect.EOF:
794 main.log.error(self.name + ": EOF exception found")
795 main.log.error(self.name + ": " + self.handle.before)
796 main.cleanup()
797 main.exit()
798 except:
799 main.log.info(self.name+" ::::::")
800 main.log.error( traceback.print_exc())
801 main.log.info(self.name+" ::::::")
802 main.cleanup()
803 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800804
805 def hosts(self, json_format=True):
Jon Hall42db6dc2014-10-24 19:03:48 -0400806 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800807 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400808 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800809 * json_format - boolean indicating if you want output in json
Jon Hall42db6dc2014-10-24 19:03:48 -0400810 '''
811 try:
812 self.handle.sendline("")
813 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800814
Jon Hall42db6dc2014-10-24 19:03:48 -0400815 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800816 self.handle.sendline("hosts -j")
817 self.handle.expect("hosts -j")
818 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400819 handle = self.handle.before
820 '''
821 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
822 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
823 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 -0800824 So we take off that escape sequence using
Jon Hall42db6dc2014-10-24 19:03:48 -0400825 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800826 handle1 = ansi_escape.sub('', handle)
Jon Hall42db6dc2014-10-24 19:03:48 -0400827 '''
828 #print "repr(handle) =", repr(handle)
829 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
830 handle1 = ansi_escape.sub('', handle)
831 #print "repr(handle1) = ", repr(handle1)
832 return handle1
833 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800834 self.handle.sendline("hosts")
835 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400836 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400837 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400838 return handle
839 except pexpect.EOF:
840 main.log.error(self.name + ": EOF exception found")
841 main.log.error(self.name + ": " + self.handle.before)
842 main.cleanup()
843 main.exit()
844 except:
845 main.log.info(self.name+" ::::::")
846 main.log.error( traceback.print_exc())
847 main.log.info(self.name+" ::::::")
848 main.cleanup()
849 main.exit()
850
851 def get_host(self, mac):
852 '''
853 Return the first host from the hosts api whose 'id' contains 'mac'
854 Note: mac must be a colon seperated mac address, but could be a partial mac address
855 Return None if there is no match
856 '''
857 import json
858 try:
859 if mac == None:
860 return None
861 else:
862 mac = mac
863 raw_hosts = self.hosts()
864 hosts_json = json.loads(raw_hosts)
865 #search json for the host with mac then return the device
866 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400867 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400868 if mac in host['id']:
869 return host
870 return None
871 except pexpect.EOF:
872 main.log.error(self.name + ": EOF exception found")
873 main.log.error(self.name + ": " + self.handle.before)
874 main.cleanup()
875 main.exit()
876 except:
877 main.log.info(self.name+" ::::::")
878 main.log.error( traceback.print_exc())
879 main.log.info(self.name+" ::::::")
880 main.cleanup()
881 main.exit()
882
andrewonlab3f0a4af2014-10-17 12:25:14 -0400883
884 def get_hosts_id(self, host_list):
885 '''
886 Obtain list of hosts
887 Issues command: 'onos> hosts'
888
889 Required:
890 * host_list: List of hosts obtained by Mininet
891 IMPORTANT:
892 This function assumes that you started your
893 topology with the option '--mac'.
894 Furthermore, it assumes that value of VLAN is '-1'
895 Description:
896 Converts mininet hosts (h1, h2, h3...) into
897 ONOS format (00:00:00:00:00:01/-1 , ...)
898 '''
899
900 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400901 onos_host_list = []
902
903 for host in host_list:
904 host = host.replace("h", "")
905 host_hex = hex(int(host)).zfill(12)
906 host_hex = str(host_hex).replace('x','0')
907 i = iter(str(host_hex))
908 host_hex = ":".join(a+b for a,b in zip(i,i))
909 host_hex = host_hex + "/-1"
910 onos_host_list.append(host_hex)
911
912 return onos_host_list
913
914 except pexpect.EOF:
915 main.log.error(self.name + ": EOF exception found")
916 main.log.error(self.name + ": " + self.handle.before)
917 main.cleanup()
918 main.exit()
919 except:
920 main.log.info(self.name+" ::::::")
921 main.log.error( traceback.print_exc())
922 main.log.info(self.name+" ::::::")
923 main.cleanup()
924 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400925
andrewonlabe6745342014-10-17 14:29:13 -0400926 def add_host_intent(self, host_id_one, host_id_two):
927 '''
928 Required:
929 * host_id_one: ONOS host id for host1
930 * host_id_two: ONOS host id for host2
931 Description:
932 Adds a host-to-host intent (bidrectional) by
Jon Hallb1290e82014-11-18 16:17:48 -0500933 specifying the two hosts.
andrewonlabe6745342014-10-17 14:29:13 -0400934 '''
935 try:
936 self.handle.sendline("")
937 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500938
andrewonlabe6745342014-10-17 14:29:13 -0400939 self.handle.sendline("add-host-intent "+
940 str(host_id_one) + " " + str(host_id_two))
941 self.handle.expect("onos>")
942
andrewonlabe6745342014-10-17 14:29:13 -0400943 handle = self.handle.before
Jon Hallffb386d2014-11-21 13:43:38 -0800944 #print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400945
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800946 main.log.info("Host intent installed between "+
andrewonlabe6745342014-10-17 14:29:13 -0400947 str(host_id_one) + " and " + str(host_id_two))
948
949 return handle
Jon Hallb1290e82014-11-18 16:17:48 -0500950
andrewonlabe6745342014-10-17 14:29:13 -0400951 except pexpect.EOF:
952 main.log.error(self.name + ": EOF exception found")
953 main.log.error(self.name + ": " + self.handle.before)
954 main.cleanup()
955 main.exit()
956 except:
957 main.log.info(self.name+" ::::::")
958 main.log.error( traceback.print_exc())
959 main.log.info(self.name+" ::::::")
960 main.cleanup()
961 main.exit()
962
andrewonlab7b31d232014-10-24 13:31:47 -0400963 def add_optical_intent(self, ingress_device, egress_device):
964 '''
965 Required:
966 * ingress_device: device id of ingress device
967 * egress_device: device id of egress device
968 Optional:
969 TODO: Still needs to be implemented via dev side
970 '''
971 try:
972 self.handle.sendline("add-optical-intent "+
973 str(ingress_device) + " " + str(egress_device))
974 self.handle.expect("add-optical-intent")
975 i = self.handle.expect([
976 "Error",
977 "onos>"])
978
979 handle = self.handle.before
980
981 #If error, return error message
982 if i == 0:
983 return handle
984 else:
985 return main.TRUE
986
987 except pexpect.EOF:
988 main.log.error(self.name + ": EOF exception found")
989 main.log.error(self.name + ": " + self.handle.before)
990 main.cleanup()
991 main.exit()
992 except:
993 main.log.info(self.name+" ::::::")
994 main.log.error( traceback.print_exc())
995 main.log.info(self.name+" ::::::")
996 main.cleanup()
997 main.exit()
998
andrewonlab36af3822014-11-18 17:48:18 -0500999 def add_point_intent(self, ingress_device, egress_device,
1000 port_ingress="", port_egress="", ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -05001001 ethDst="", bandwidth="", lambda_alloc=False,
1002 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -04001003 '''
1004 Required:
1005 * ingress_device: device id of ingress device
1006 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001007 Optional:
1008 * ethType: specify ethType
1009 * ethSrc: specify ethSrc (i.e. src mac addr)
1010 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001011 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -05001012 * lambda_alloc: if True, intent will allocate lambda
1013 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -05001014 * ipProto: specify ip protocol
1015 * ipSrc: specify ip source address
1016 * ipDst: specify ip destination address
1017 * tcpSrc: specify tcp source port
1018 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001019 Description:
1020 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -04001021 specifying device id's and optional fields
1022
andrewonlab4dbb4d82014-10-17 18:22:31 -04001023 NOTE: This function may change depending on the
1024 options developers provide for point-to-point
1025 intent via cli
1026 '''
1027 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001028 cmd = ""
1029
1030 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001031 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001032 and not bandwidth and not lambda_alloc \
1033 and not ipProto and not ipSrc and not ipDst \
1034 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001035 cmd = "add-point-intent"
1036
1037
andrewonlab289e4b72014-10-21 21:24:18 -04001038 else:
andrewonlab36af3822014-11-18 17:48:18 -05001039 cmd = "add-point-intent"
andrewonlab9a130be2014-10-22 12:44:56 -04001040
andrewonlab0c0a6772014-10-22 12:31:18 -04001041 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001042 cmd += " --ethType " + str(ethType)
1043 if ethSrc:
1044 cmd += " --ethSrc " + str(ethSrc)
1045 if ethDst:
1046 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001047 if bandwidth:
1048 cmd += " --bandwidth " + str(bandwidth)
1049 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001050 cmd += " --lambda "
1051 if ipProto:
1052 cmd += " --ipProto " + str(ipProto)
1053 if ipSrc:
1054 cmd += " --ipSrc " + str(ipSrc)
1055 if ipDst:
1056 cmd += " --ipDst " + str(ipDst)
1057 if tcpSrc:
1058 cmd += " --tcpSrc " + str(tcpSrc)
1059 if tcpDst:
1060 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001061
andrewonlab36af3822014-11-18 17:48:18 -05001062 #Check whether the user appended the port
1063 #or provided it as an input
1064 if "/" in ingress_device:
1065 cmd += " "+str(ingress_device)
1066 else:
1067 if not port_ingress:
1068 main.log.error("You must specify "+
1069 "the ingress port")
1070 #TODO: perhaps more meaningful return
1071 return main.FALSE
1072
1073 cmd += " "+ \
1074 str(ingress_device) + "/" +\
1075 str(port_ingress) + " "
1076
1077 if "/" in egress_device:
1078 cmd += " "+str(egress_device)
1079 else:
1080 if not port_egress:
1081 main.log.error("You must specify "+
1082 "the egress port")
1083 return main.FALSE
1084
1085 cmd += " "+\
1086 str(egress_device) + "/" +\
1087 str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001088
andrewonlab289e4b72014-10-21 21:24:18 -04001089 self.handle.sendline(cmd)
andrewonlab36af3822014-11-18 17:48:18 -05001090
1091 main.log.info(cmd + " sent")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001092 i = self.handle.expect([
1093 "Error",
1094 "onos>"])
andrewonlab4dbb4d82014-10-17 18:22:31 -04001095
1096 if i == 0:
1097 main.log.error("Error in adding point-to-point intent")
1098 return handle
1099 else:
1100 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001101
andrewonlab4dbb4d82014-10-17 18:22:31 -04001102 except pexpect.EOF:
1103 main.log.error(self.name + ": EOF exception found")
1104 main.log.error(self.name + ": " + self.handle.before)
1105 main.cleanup()
1106 main.exit()
1107 except:
1108 main.log.info(self.name+" ::::::")
1109 main.log.error( traceback.print_exc())
1110 main.log.info(self.name+" ::::::")
1111 main.cleanup()
1112 main.exit()
1113
shahshreyad0c80432014-12-04 16:56:05 -08001114
1115 def add_multipoint_to_singlepoint_intent(self, ingress_device1, ingress_device2,egress_device,
1116 port_ingress="", port_egress="", ethType="", ethSrc="",
1117 ethDst="", bandwidth="", lambda_alloc=False,
1118 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="", setEthDst=""):
1119 '''
1120 Note:
1121 This function assumes that there would be 2 ingress devices and one egress device
1122 For more number of ingress devices, this function needs to be modified
1123 Required:
1124 * ingress_device1: device id of ingress device1
1125 * ingress_device2: device id of ingress device2
1126 * egress_device: device id of egress device
1127 Optional:
1128 * ethType: specify ethType
1129 * ethSrc: specify ethSrc (i.e. src mac addr)
1130 * ethDst: specify ethDst (i.e. dst mac addr)
1131 * bandwidth: specify bandwidth capacity of link
1132 * lambda_alloc: if True, intent will allocate lambda
1133 for the specified intent
1134 * ipProto: specify ip protocol
1135 * ipSrc: specify ip source address
1136 * ipDst: specify ip destination address
1137 * tcpSrc: specify tcp source port
1138 * tcpDst: specify tcp destination port
1139 * setEthSrc: action to Rewrite Source MAC Address
1140 * setEthDst: action to Rewrite Destination MAC Address
1141 Description:
1142 Adds a multipoint-to-singlepoint intent (uni-directional) by
1143 specifying device id's and optional fields
1144
1145 NOTE: This function may change depending on the
1146 options developers provide for multipointpoint-to-singlepoint
1147 intent via cli
1148 '''
1149 try:
1150 cmd = ""
1151
1152 #If there are no optional arguments
1153 if not ethType and not ethSrc and not ethDst\
1154 and not bandwidth and not lambda_alloc \
1155 and not ipProto and not ipSrc and not ipDst \
1156 and not tcpSrc and not tcpDst and not setEthSrc and not setEthDst:
1157 cmd = "add-multi-to-single-intent"
1158
1159
1160 else:
1161 cmd = "add-multi-to-single-intent"
1162
1163 if ethType:
1164 cmd += " --ethType " + str(ethType)
1165 if ethSrc:
1166 cmd += " --ethSrc " + str(ethSrc)
1167 if ethDst:
1168 cmd += " --ethDst " + str(ethDst)
1169 if bandwidth:
1170 cmd += " --bandwidth " + str(bandwidth)
1171 if lambda_alloc:
1172 cmd += " --lambda "
1173 if ipProto:
1174 cmd += " --ipProto " + str(ipProto)
1175 if ipSrc:
1176 cmd += " --ipSrc " + str(ipSrc)
1177 if ipDst:
1178 cmd += " --ipDst " + str(ipDst)
1179 if tcpSrc:
1180 cmd += " --tcpSrc " + str(tcpSrc)
1181 if tcpDst:
1182 cmd += " --tcpDst " + str(tcpDst)
1183 if setEthSrc:
1184 cmd += " --setEthSrc "+ str(setEthSrc)
1185 if setEthDst:
1186 cmd += " --setEthDst "+ str(setEthDst)
1187
1188 #Check whether the user appended the port
1189 #or provided it as an input
1190 if "/" in ingress_device1:
1191 cmd += " "+str(ingress_device1)
1192 else:
1193 if not port_ingress1:
1194 main.log.error("You must specify "+
1195 "the ingress port1")
1196 #TODO: perhaps more meaningful return
1197 return main.FALSE
1198
1199 cmd += " "+ \
1200 str(ingress_device1) + "/" +\
1201 str(port_ingress1) + " "
1202
1203 if "/" in ingress_device2:
1204 cmd += " "+str(ingress_device2)
1205 else:
1206 if not port_ingress2:
1207 main.log.error("You must specify "+
1208 "the ingress port2")
1209 #TODO: perhaps more meaningful return
1210 return main.FALSE
1211
1212 cmd += " "+ \
1213 str(ingress_device2) + "/" +\
1214 str(port_ingress2) + " "
1215
1216 if "/" in egress_device:
1217 cmd += " "+str(egress_device)
1218 else:
1219 if not port_egress:
1220 main.log.error("You must specify "+
1221 "the egress port")
1222 return main.FALSE
1223
1224 cmd += " "+\
1225 str(egress_device) + "/" +\
1226 str(port_egress)
1227 print "cmd= ",cmd
1228 self.handle.sendline(cmd)
1229
1230 main.log.info(cmd + " sent")
1231 i = self.handle.expect([
1232 "Error",
1233 "onos>"])
1234
1235 if i == 0:
1236 main.log.error("Error in adding point-to-point intent")
1237 return self.handle
1238 else:
1239 return main.TRUE
1240
1241 except pexpect.EOF:
1242 main.log.error(self.name + ": EOF exception found")
1243 main.log.error(self.name + ": " + self.handle.before)
1244 main.cleanup()
1245 main.exit()
1246 except:
1247 main.log.info(self.name+" ::::::")
1248 main.log.error( traceback.print_exc())
1249 main.log.info(self.name+" ::::::")
1250 main.cleanup()
1251 main.exit()
1252
1253
andrewonlab9a50dfe2014-10-17 17:22:31 -04001254 def remove_intent(self, intent_id):
1255 '''
1256 Remove intent for specified intent id
1257 '''
1258 try:
1259 self.handle.sendline("")
1260 self.handle.expect("onos>")
1261
1262 self.handle.sendline("remove-intent "+str(intent_id))
1263 i = self.handle.expect([
1264 "Error",
1265 "onos>"])
1266
1267 handle = self.handle.before
1268
1269 if i == 0:
1270 main.log.error("Error in removing intent")
1271 return handle
1272 else:
1273 return handle
1274
1275 except pexpect.EOF:
1276 main.log.error(self.name + ": EOF exception found")
1277 main.log.error(self.name + ": " + self.handle.before)
1278 main.cleanup()
1279 main.exit()
1280 except:
1281 main.log.info(self.name+" ::::::")
1282 main.log.error( traceback.print_exc())
1283 main.log.info(self.name+" ::::::")
1284 main.cleanup()
1285 main.exit()
1286
pingping-lindabe7972014-11-17 19:29:44 -08001287 # This method should be used after installing application: onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001288 def routes(self, json_format=False):
1289 '''
1290 Optional:
1291 * json_format: enable output formatting in json
1292 Description:
1293 Obtain all routes in the system
1294 '''
1295 try:
1296 if json_format:
1297 self.handle.sendline("routes -j")
1298 self.handle.expect("routes -j")
1299 self.handle.expect("onos>")
1300 handle_tmp = self.handle.before
1301
1302 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1303 handle = ansi_escape.sub('', handle_tmp)
1304
1305 else:
1306 self.handle.sendline("")
1307 self.handle.expect("onos>")
1308
1309 self.handle.sendline("routes")
1310 self.handle.expect("onos>")
1311 handle = self.handle.before
1312
1313 return handle
1314
1315 except pexpect.EOF:
1316 main.log.error(self.name + ": EOF exception found")
1317 main.log.error(self.name + ": " + self.handle.before)
1318 main.cleanup()
1319 main.exit()
1320 except:
1321 main.log.info(self.name + " ::::::")
1322 main.log.error(traceback.print_exc())
1323 main.log.info(self.name + " ::::::")
1324 main.cleanup()
1325 main.exit()
1326
Jon Hallffb386d2014-11-21 13:43:38 -08001327 def intents(self, json_format = True):
andrewonlabe6745342014-10-17 14:29:13 -04001328 '''
andrewonlab377693f2014-10-21 16:00:30 -04001329 Optional:
1330 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001331 Description:
1332 Obtain intents currently installed
1333 '''
1334 try:
andrewonlab377693f2014-10-21 16:00:30 -04001335 if json_format:
1336 self.handle.sendline("intents -j")
1337 self.handle.expect("intents -j")
1338 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001339 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001340
1341 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1342 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001343 else:
1344 self.handle.sendline("")
1345 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001346
andrewonlab377693f2014-10-21 16:00:30 -04001347 self.handle.sendline("intents")
1348 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001349 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001350
1351 return handle
1352
1353 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()
1364
Jon Hallffb386d2014-11-21 13:43:38 -08001365 def flows(self, json_format = True):
Shreya Shah0f01c812014-10-26 20:15:28 -04001366 '''
1367 Optional:
1368 * json_format: enable output formatting in json
1369 Description:
1370 Obtain flows currently installed
1371 '''
1372 try:
1373 if json_format:
1374 self.handle.sendline("flows -j")
1375 self.handle.expect("flows -j")
1376 self.handle.expect("onos>")
1377 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001378 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1379 handle = ansi_escape.sub('', handle)
Shreya Shah0f01c812014-10-26 20:15:28 -04001380
1381 else:
1382 self.handle.sendline("")
1383 self.handle.expect("onos>")
1384 self.handle.sendline("flows")
1385 self.handle.expect("onos>")
1386 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001387 if re.search("Error\sexecuting\scommand:", handle):
1388 main.log.error(self.name + ".flows() response: " + str(handle))
Shreya Shah0f01c812014-10-26 20:15:28 -04001389
1390 return handle
1391
1392 except pexpect.EOF:
1393 main.log.error(self.name + ": EOF exception found")
1394 main.log.error(self.name + ": " + self.handle.before)
1395 main.cleanup()
1396 main.exit()
1397 except:
1398 main.log.info(self.name+" ::::::")
1399 main.log.error( traceback.print_exc())
1400 main.log.info(self.name+" ::::::")
1401 main.cleanup()
1402 main.exit()
1403
andrewonlabb66dfa12014-12-02 15:51:10 -05001404 def push_test_intents(self, dpid_src, dpid_dst, num_intents,
1405 num_mult="", app_id="", report=True):
andrewonlab87852b02014-11-19 18:44:19 -05001406 '''
1407 Description:
1408 Push a number of intents in a batch format to
1409 a specific point-to-point intent definition
1410 Required:
1411 * dpid_src: specify source dpid
1412 * dpid_dst: specify destination dpid
1413 * num_intents: specify number of intents to push
1414 Optional:
andrewonlabb66dfa12014-12-02 15:51:10 -05001415 * num_mult: number multiplier for multiplying
1416 the number of intents specified
1417 * app_id: specify the application id init to further
1418 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001419 * report: default True, returns latency information
1420 '''
1421 try:
1422 cmd = "push-test-intents "+\
1423 str(dpid_src)+" "+str(dpid_dst)+" "+\
1424 str(num_intents)
andrewonlabb66dfa12014-12-02 15:51:10 -05001425
1426 if num_mult:
1427 cmd += " " + str(num_mult)
andrewonlab042b3912014-12-10 16:40:50 -05001428 #If app id is specified, then num_mult
1429 #must exist because of the way this command
1430 #takes in arguments
andrewonlabb66dfa12014-12-02 15:51:10 -05001431 if app_id:
1432 cmd += " " + str(app_id)
1433
andrewonlab87852b02014-11-19 18:44:19 -05001434 self.handle.sendline(cmd)
1435 self.handle.expect(cmd)
1436 self.handle.expect("onos>")
1437
1438 handle = self.handle.before
1439
1440 #Some color thing that we want to escape
1441 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1442 handle = ansi_escape.sub('', handle)
1443
1444 if report:
andrewonlabb66dfa12014-12-02 15:51:10 -05001445 lat_result = []
andrewonlab87852b02014-11-19 18:44:19 -05001446 main.log.info(handle)
andrewonlabb66dfa12014-12-02 15:51:10 -05001447 #Split result by newline
1448 newline = handle.split("\r\r\n")
1449 #Ignore the first object of list, which is empty
1450 newline = newline[1:]
1451 #Some sloppy parsing method to get the latency
1452 for result in newline:
1453 result = result.split(": ")
1454 #Append the first result of second parse
1455 lat_result.append(result[1].split(" ")[0])
1456
andrewonlab042b3912014-12-10 16:40:50 -05001457 main.log.info(lat_result)
andrewonlabb66dfa12014-12-02 15:51:10 -05001458 return lat_result
andrewonlab87852b02014-11-19 18:44:19 -05001459 else:
1460 return main.TRUE
1461
1462 except pexpect.EOF:
1463 main.log.error(self.name + ": EOF exception found")
1464 main.log.error(self.name + ": " + self.handle.before)
1465 main.cleanup()
1466 main.exit()
1467 except:
1468 main.log.info(self.name+" ::::::")
1469 main.log.error( traceback.print_exc())
1470 main.log.info(self.name+" ::::::")
1471 main.cleanup()
1472 main.exit()
1473
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001474 def intents_events_metrics(self, json_format=True):
1475 '''
1476 Description:Returns topology metrics
1477 Optional:
1478 * json_format: enable json formatting of output
1479 '''
1480 try:
1481 if json_format:
1482 self.handle.sendline("intents-events-metrics -j")
1483 self.handle.expect("intents-events-metrics -j")
1484 self.handle.expect("onos>")
1485
1486 handle = self.handle.before
1487
1488 #Some color thing that we want to escape
1489 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1490 handle = ansi_escape.sub('', handle)
1491
1492 else:
1493 self.handle.sendline("intents-events-metrics")
1494 self.handle.expect("intents-events-metrics")
1495 self.handle.expect("onos>")
1496
1497 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001498
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001499 return handle
1500
1501 except pexpect.EOF:
1502 main.log.error(self.name + ": EOF exception found")
1503 main.log.error(self.name + ": " + self.handle.before)
1504 main.cleanup()
1505 main.exit()
1506 except:
1507 main.log.info(self.name+" ::::::")
1508 main.log.error( traceback.print_exc())
1509 main.log.info(self.name+" ::::::")
1510 main.cleanup()
1511 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001512
andrewonlab867212a2014-10-22 20:13:38 -04001513 def topology_events_metrics(self, json_format=True):
1514 '''
1515 Description:Returns topology metrics
1516 Optional:
1517 * json_format: enable json formatting of output
1518 '''
1519 try:
1520 if json_format:
1521 self.handle.sendline("topology-events-metrics -j")
1522 self.handle.expect("topology-events-metrics -j")
1523 self.handle.expect("onos>")
1524
1525 handle = self.handle.before
1526
1527 #Some color thing that we want to escape
1528 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1529 handle = ansi_escape.sub('', handle)
1530
1531 else:
1532 self.handle.sendline("topology-events-metrics")
1533 self.handle.expect("topology-events-metrics")
1534 self.handle.expect("onos>")
1535
1536 handle = self.handle.before
1537
1538 return handle
1539
1540 except pexpect.EOF:
1541 main.log.error(self.name + ": EOF exception found")
1542 main.log.error(self.name + ": " + self.handle.before)
1543 main.cleanup()
1544 main.exit()
1545 except:
1546 main.log.info(self.name+" ::::::")
1547 main.log.error( traceback.print_exc())
1548 main.log.info(self.name+" ::::::")
1549 main.cleanup()
1550 main.exit()
1551
andrewonlab3e15ead2014-10-15 14:21:34 -04001552 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001553 #Wrapper functions use existing driver
1554 #functions and extends their use case.
1555 #For example, we may use the output of
1556 #a normal driver function, and parse it
1557 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001558
andrewonlab9a50dfe2014-10-17 17:22:31 -04001559 def get_all_intents_id(self):
1560 '''
1561 Description:
1562 Obtain all intent id's in a list
1563 '''
1564 try:
1565 #Obtain output of intents function
1566 intents_str = self.intents()
1567 all_intent_list = []
1568 intent_id_list = []
1569
1570 #Parse the intents output for ID's
1571 intents_list = [s.strip() for s in intents_str.splitlines()]
1572 for intents in intents_list:
1573 if "onos>" in intents:
1574 continue
1575 elif "intents" in intents:
1576 continue
1577 else:
1578 line_list = intents.split(" ")
1579 all_intent_list.append(line_list[0])
1580
1581 all_intent_list = all_intent_list[1:-2]
1582
1583 for intents in all_intent_list:
1584 if not intents:
1585 continue
1586 else:
1587 intent_id_list.append(intents)
1588
1589 return intent_id_list
1590
1591 except pexpect.EOF:
1592 main.log.error(self.name + ": EOF exception found")
1593 main.log.error(self.name + ": " + self.handle.before)
1594 main.cleanup()
1595 main.exit()
1596 except:
1597 main.log.info(self.name+" ::::::")
1598 main.log.error( traceback.print_exc())
1599 main.log.info(self.name+" ::::::")
1600 main.cleanup()
1601 main.exit()
1602
andrewonlab7e4d2d32014-10-15 13:23:21 -04001603 def get_all_devices_id(self):
1604 '''
1605 Use 'devices' function to obtain list of all devices
1606 and parse the result to obtain a list of all device
1607 id's. Returns this list. Returns empty list if no
1608 devices exist
1609 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001610
1611 This function may be useful if you are not sure of the
1612 device id, and wish to execute other commands using
1613 the ids. By obtaining the list of device ids on the fly,
1614 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001615 '''
1616 try:
1617 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001618 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001619 id_list = []
1620
1621 if not devices_str:
1622 main.log.info("There are no devices to get id from")
1623 return id_list
1624
1625 #Split the string into list by comma
1626 device_list = devices_str.split(",")
1627 #Get temporary list of all arguments with string 'id='
1628 temp_list = [dev for dev in device_list if "id=" in dev]
1629 #Split list further into arguments before and after string
1630 # 'id='. Get the latter portion (the actual device id) and
1631 # append to id_list
1632 for arg in temp_list:
1633 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001634 return id_list
1635
1636 except pexpect.EOF:
1637 main.log.error(self.name + ": EOF exception found")
1638 main.log.error(self.name + ": " + self.handle.before)
1639 main.cleanup()
1640 main.exit()
1641 except:
1642 main.log.info(self.name+" ::::::")
1643 main.log.error( traceback.print_exc())
1644 main.log.info(self.name+" ::::::")
1645 main.cleanup()
1646 main.exit()
1647
andrewonlab7c211572014-10-15 16:45:20 -04001648 def get_all_nodes_id(self):
1649 '''
1650 Uses 'nodes' function to obtain list of all nodes
1651 and parse the result of nodes to obtain just the
1652 node id's.
1653 Returns:
1654 list of node id's
1655 '''
1656 try:
1657 nodes_str = self.nodes()
1658 id_list = []
1659
1660 if not nodes_str:
1661 main.log.info("There are no nodes to get id from")
1662 return id_list
1663
1664 #Sample nodes_str output
1665 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1666
1667 #Split the string into list by comma
1668 nodes_list = nodes_str.split(",")
1669 temp_list = [node for node in nodes_list if "id=" in node]
1670 for arg in temp_list:
1671 id_list.append(arg.split("id=")[1])
1672
1673 return id_list
1674
1675 except pexpect.EOF:
1676 main.log.error(self.name + ": EOF exception found")
1677 main.log.error(self.name + ": " + self.handle.before)
1678 main.cleanup()
1679 main.exit()
1680 except:
1681 main.log.info(self.name+" ::::::")
1682 main.log.error( traceback.print_exc())
1683 main.log.info(self.name+" ::::::")
1684 main.cleanup()
1685 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001686
Jon Halla91c4dc2014-10-22 12:57:04 -04001687 def get_device(self, dpid=None):
1688 '''
1689 Return the first device from the devices api whose 'id' contains 'dpid'
1690 Return None if there is no match
1691 '''
1692 import json
1693 try:
1694 if dpid == None:
1695 return None
1696 else:
1697 dpid = dpid.replace(':', '')
1698 raw_devices = self.devices()
1699 devices_json = json.loads(raw_devices)
1700 #search json for the device with dpid then return the device
1701 for device in devices_json:
1702 #print "%s in %s?" % (dpid, device['id'])
1703 if dpid in device['id']:
1704 return device
1705 return None
1706 except pexpect.EOF:
1707 main.log.error(self.name + ": EOF exception found")
1708 main.log.error(self.name + ": " + self.handle.before)
1709 main.cleanup()
1710 main.exit()
1711 except:
1712 main.log.info(self.name+" ::::::")
1713 main.log.error( traceback.print_exc())
1714 main.log.info(self.name+" ::::::")
1715 main.cleanup()
1716 main.exit()
1717
Jon Hall42db6dc2014-10-24 19:03:48 -04001718 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1719 '''
1720 Checks the number of swithes & links that ONOS sees against the
1721 supplied values. By default this will report to main.log, but the
1722 log level can be specifid.
1723
1724 Params: ip = ip used for the onos cli
1725 numoswitch = expected number of switches
1726 numlink = expected number of links
1727 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1728
1729
1730 log_level can
1731
1732 Returns: main.TRUE if the number of switchs and links are correct,
1733 main.FALSE if the numer of switches and links is incorrect,
1734 and main.ERROR otherwise
1735 '''
1736
1737 try:
1738 topology = self.get_topology(ip)
1739 if topology == {}:
1740 return main.ERROR
1741 output = ""
1742 #Is the number of switches is what we expected
1743 devices = topology.get('devices',False)
1744 links = topology.get('links',False)
1745 if devices == False or links == False:
1746 return main.ERROR
1747 switch_check = ( int(devices) == int(numoswitch) )
1748 #Is the number of links is what we expected
1749 link_check = ( int(links) == int(numolink) )
1750 if (switch_check and link_check):
1751 #We expected the correct numbers
1752 output = output + "The number of links and switches match "\
1753 + "what was expected"
1754 result = main.TRUE
1755 else:
1756 output = output + \
1757 "The number of links and switches does not match what was expected"
1758 result = main.FALSE
1759 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1760 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1761 if log_level == "report":
1762 main.log.report(output)
1763 elif log_level == "warn":
1764 main.log.warn(output)
1765 else:
1766 main.log.info(output)
1767 return result
1768 except pexpect.EOF:
1769 main.log.error(self.name + ": EOF exception found")
1770 main.log.error(self.name + ": " + self.handle.before)
1771 main.cleanup()
1772 main.exit()
1773 except:
1774 main.log.info(self.name+" ::::::")
1775 main.log.error( traceback.print_exc())
1776 main.log.info(self.name+" ::::::")
1777 main.cleanup()
1778 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001779
1780 def device_role(self, device_id, onos_node, role="master"):
1781 '''
1782 Calls the device-role cli command.
1783 device_id must be the id of a device as seen in the onos devices command
1784 onos_node is the ip of one of the onos nodes in the cluster
1785 role must be either master, standby, or none
1786
Jon Hall94fd0472014-12-08 11:52:42 -08001787 Returns main.TRUE or main.FALSE based on argument verification.
Jon Hall983a1702014-10-28 18:44:22 -04001788 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001789 support that output
1790 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001791 try:
Jon Hall983a1702014-10-28 18:44:22 -04001792 #print "beginning device_role... \n\tdevice_id:" + device_id
1793 #print "\tonos_node: " + onos_node
1794 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001795 if role.lower() == "master" or \
1796 role.lower() == "standby" or \
1797 role.lower() == "none":
1798 self.handle.sendline("")
1799 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001800 self.handle.sendline("device-role " +
1801 str(device_id) + " " +
1802 str(onos_node) + " " +
1803 str(role))
1804 i= self.handle.expect(["Error","onos>"])
1805 if i == 0:
1806 output = str(self.handle.before)
1807 self.handle.expect("onos>")
1808 output = output + str(self.handle.before)
1809 main.log.error(self.name + ": " +
1810 output + '\033[0m')#end color output to escape any colours from the cli
1811 return main.ERROR
1812 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001813 self.handle.expect("onos>")
1814 return main.TRUE
1815 else:
1816 return main.FALSE
1817
1818 except pexpect.EOF:
1819 main.log.error(self.name + ": EOF exception found")
1820 main.log.error(self.name + ": " + self.handle.before)
1821 main.cleanup()
1822 main.exit()
1823 except:
1824 main.log.info(self.name+" ::::::")
1825 main.log.error( traceback.print_exc())
1826 main.log.info(self.name+" ::::::")
1827 main.cleanup()
1828 main.exit()
1829
Jon Hall73cf9cc2014-11-20 22:28:38 -08001830 def clusters(self, json_format=True):
1831 '''
1832 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001833 Optional argument:
1834 * json_format - boolean indicating if you want output in json
Jon Hall73cf9cc2014-11-20 22:28:38 -08001835 '''
1836 try:
1837 self.handle.sendline("")
1838 self.handle.expect("onos>")
1839
1840 if json_format:
1841 self.handle.sendline("clusters -j")
1842 self.handle.expect("clusters -j")
1843 self.handle.expect("onos>")
1844 handle = self.handle.before
1845 '''
1846 handle variable here contains some ANSI escape color code
1847 sequences at the end which are invisible in the print command
1848 output. To make that escape sequence visible, use repr() function.
1849 The repr(handle) output when printed shows the ANSI escape sequences.
1850 In json.loads(somestring), this somestring variable is actually
1851 repr(somestring) and json.loads would fail with the escape sequence.
1852 So we take off that escape sequence using
1853 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1854 handle1 = ansi_escape.sub('', handle)
1855 '''
1856 #print "repr(handle) =", repr(handle)
1857 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1858 handle1 = ansi_escape.sub('', handle)
1859 #print "repr(handle1) = ", repr(handle1)
1860 return handle1
1861 else:
Jon Hallffb386d2014-11-21 13:43:38 -08001862 self.handle.sendline("clusters")
Jon Hall73cf9cc2014-11-20 22:28:38 -08001863 self.handle.expect("onos>")
1864 handle = self.handle.before
1865 #print "handle =",handle
1866 return handle
1867 except pexpect.EOF:
1868 main.log.error(self.name + ": EOF exception found")
1869 main.log.error(self.name + ": " + self.handle.before)
1870 main.cleanup()
1871 main.exit()
1872 except:
1873 main.log.info(self.name+" ::::::")
1874 main.log.error( traceback.print_exc())
1875 main.log.info(self.name+" ::::::")
1876 main.cleanup()
1877 main.exit()
1878
Jon Hall94fd0472014-12-08 11:52:42 -08001879 def election_test_leader(self):
1880 '''
1881 * CLI command to get the current leader for the Election test application.
1882 #NOTE: Requires installation of the onos-app-election feature
1883 Returns: Node IP of the leader if one exists
1884 None if none exists
1885 Main.FALSE on error
1886 '''
1887 try:
1888 self.handle.sendline("election-test-leader")
1889 self.handle.expect("election-test-leader")
1890 self.handle.expect("onos>")
1891 response = self.handle.before
1892 #Leader
1893 node_search = re.search("The\scurrent\sleader\sfor\sthe\sElection\sapp\sis\s(?P<node>.+)\.", response)
1894 if node_search:
1895 node = node_search.group('node')
Jon Hall669173b2014-12-17 11:36:30 -08001896 main.log.info("Election-test-leader on "+str(self.name)+" found " + node + " as the leader")
Jon Hall94fd0472014-12-08 11:52:42 -08001897 return node
1898 #no leader
1899 null_search = re.search("There\sis\scurrently\sno\sleader\selected\sfor\sthe\sElection\sapp", response)
1900 if null_search:
Jon Hall669173b2014-12-17 11:36:30 -08001901 main.log.info("Election-test-leader found no leader on " + self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001902 return None
1903 #error
Jon Hall669173b2014-12-17 11:36:30 -08001904 if re.search("Command\snot\sfound", response):
1905 main.log.error("Election app is not loaded on " + self.name)
1906 return main.FALSE
1907 else:
1908 main.log.error("Error in election_test_leader: unexpected response")
1909 main.log.error( repr(response) )
1910 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001911
1912 except pexpect.EOF:
1913 main.log.error(self.name + ": EOF exception found")
1914 main.log.error(self.name + ": " + self.handle.before)
1915 main.cleanup()
1916 main.exit()
1917 except:
1918 main.log.info(self.name+" ::::::")
1919 main.log.error( traceback.print_exc())
1920 main.log.info(self.name+" ::::::")
1921 main.cleanup()
1922 main.exit()
1923
1924 def election_test_run(self):
1925 '''
1926 * CLI command to run for leadership of the Election test application.
1927 #NOTE: Requires installation of the onos-app-election feature
1928 Returns: Main.TRUE on success
1929 Main.FALSE on error
1930 '''
1931 try:
1932 self.handle.sendline("election-test-run")
1933 self.handle.expect("election-test-run")
1934 self.handle.expect("onos>")
1935 response = self.handle.before
1936 #success
1937 search = re.search("Entering\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1938 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001939 main.log.info(self.name + " entering leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001940 return main.TRUE
1941 #error
Jon Hall669173b2014-12-17 11:36:30 -08001942 if re.search("Command\snot\sfound", response):
1943 main.log.error("Election app is not loaded on " + self.name)
1944 return main.FALSE
1945 else:
1946 main.log.error("Error in election_test_run: unexpected response")
1947 main.log.error( repr(response) )
1948 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001949
1950 except pexpect.EOF:
1951 main.log.error(self.name + ": EOF exception found")
1952 main.log.error(self.name + ": " + self.handle.before)
1953 main.cleanup()
1954 main.exit()
1955 except:
1956 main.log.info(self.name+" ::::::")
1957 main.log.error( traceback.print_exc())
1958 main.log.info(self.name+" ::::::")
1959 main.cleanup()
1960 main.exit()
1961
1962 def election_test_withdraw(self):
1963 '''
1964 * CLI command to withdraw the local node from leadership election for
1965 * the Election test application.
1966 #NOTE: Requires installation of the onos-app-election feature
1967 Returns: Main.TRUE on success
1968 Main.FALSE on error
1969 '''
1970 try:
1971 self.handle.sendline("election-test-withdraw")
1972 self.handle.expect("election-test-withdraw")
1973 self.handle.expect("onos>")
1974 response = self.handle.before
1975 #success
1976 search = re.search("Withdrawing\sfrom\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1977 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001978 main.log.info(self.name + " withdrawing from leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001979 return main.TRUE
1980 #error
Jon Hall669173b2014-12-17 11:36:30 -08001981 if re.search("Command\snot\sfound", response):
1982 main.log.error("Election app is not loaded on " + self.name)
1983 return main.FALSE
1984 else:
1985 main.log.error("Error in election_test_withdraw: unexpected response")
1986 main.log.error( repr(response) )
1987 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001988
1989
1990 except pexpect.EOF:
1991 main.log.error(self.name + ": EOF exception found")
1992 main.log.error(self.name + ": " + self.handle.before)
1993 main.cleanup()
1994 main.exit()
1995 except:
1996 main.log.info(self.name+" ::::::")
1997 main.log.error( traceback.print_exc())
1998 main.log.info(self.name+" ::::::")
1999 main.cleanup()
2000 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002001
andrewonlab7e4d2d32014-10-15 13:23:21 -04002002 #***********************************
Hari Krishna416c3ca2014-12-19 15:39:31 -08002003 def getDevicePortsEnabledCount(self,dpid):
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002004 '''
2005 Get the count of all enabled ports on a particular device/switch
2006 '''
2007 try:
Hari Krishna2bbaa702014-12-19 14:46:12 -08002008 dpid = str(dpid)
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002009 self.handle.sendline("")
2010 self.handle.expect("onos>")
2011
2012 self.handle.sendline("onos:ports -e "+dpid+" | wc -l")
2013 i = self.handle.expect([
2014 "No such device",
2015 "onos>"])
2016
2017 #self.handle.sendline("")
2018 #self.handle.expect("onos>")
2019
2020 output = self.handle.before
2021
2022 if i == 0:
2023 main.log.error("Error in getting ports")
2024 return (ouput, "Error")
2025 else:
2026 result = output
2027 return result
2028
2029 except pexpect.EOF:
2030 main.log.error(self.name + ": EOF exception found")
2031 main.log.error(self.name + ": " + self.handle.before)
2032 main.cleanup()
2033 main.exit()
2034 except:
2035 main.log.info(self.name+" ::::::")
2036 main.log.error( traceback.print_exc())
2037 main.log.info(self.name+" ::::::")
2038 main.cleanup()
2039 main.exit()
2040
2041 def getDeviceLinksActiveCount(self,dpid):
2042 '''
2043 Get the count of all enabled ports on a particular device/switch
2044 '''
2045 try:
Hari Krishna2bbaa702014-12-19 14:46:12 -08002046 dpid = str(dpid)
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002047 self.handle.sendline("")
2048 self.handle.expect("onos>")
2049
2050 self.handle.sendline("onos:links "+dpid+" | grep ACTIVE | wc -l")
2051 i = self.handle.expect([
2052 "No such device",
2053 "onos>"])
2054
2055 output = self.handle.before
2056
2057 if i == 0:
2058 main.log.error("Error in getting ports")
2059 return (ouput, "Error")
2060 else:
2061 result = output
2062 return result
2063
2064 except pexpect.EOF:
2065 main.log.error(self.name + ": EOF exception found")
2066 main.log.error(self.name + ": " + self.handle.before)
2067 main.cleanup()
2068 main.exit()
2069 except:
2070 main.log.info(self.name+" ::::::")
2071 main.log.error( traceback.print_exc())
2072 main.log.info(self.name+" ::::::")
2073 main.cleanup()
2074 main.exit()
2075
2076 def getAllIntentIds(self):
2077 '''
2078 Return a list of all Intent IDs
2079 '''
2080 try:
2081 self.handle.sendline("")
2082 self.handle.expect("onos>")
2083
2084 self.handle.sendline("onos:intents | grep id=")
2085 i = self.handle.expect([
2086 "Error",
2087 "onos>"])
2088
2089 output = self.handle.before
2090
2091 if i == 0:
2092 main.log.error("Error in getting ports")
2093 return (ouput, "Error")
2094 else:
2095 result = output
2096 return result
2097
2098 except pexpect.EOF:
2099 main.log.error(self.name + ": EOF exception found")
2100 main.log.error(self.name + ": " + self.handle.before)
2101 main.cleanup()
2102 main.exit()
2103 except:
2104 main.log.info(self.name+" ::::::")
2105 main.log.error( traceback.print_exc())
2106 main.log.info(self.name+" ::::::")
2107 main.cleanup()
2108 main.exit()