blob: 7e6f142737f0456bb3956f6b8ccdc707e21a0214 [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=""):
Hari Krishna01bb5982015-01-05 09:54:39 -0800176 '''
177 karafTimeout is an optional arugument. karafTimeout value passed by user would be used to set the
178 current karaf shell idle timeout. Note that when ever this property is modified the shell will exit and
179 the subsequent login would reflect new idle timeout.
180 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400181 try:
182 self.handle.sendline("")
andrewonlab48829f62014-11-17 13:49:01 -0500183 x = self.handle.expect([
184 "\$", "onos>"], timeout=10)
185
186 if x == 1:
187 main.log.info("ONOS cli is already running")
188 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400189
190 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400191 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400192 i = self.handle.expect([
193 "onos>",
194 pexpect.TIMEOUT],timeout=60)
195
196 if i == 0:
197 main.log.info(str(ONOS_ip)+" CLI Started successfully")
Hari Krishnae36ef212015-01-04 14:09:13 -0800198 if karafTimeout:
199 self.handle.sendline("config:property-set -p org.apache.karaf.shell sshIdleTimeout "+karafTimeout)
200 self.handle.expect("\$")
201 self.handle.sendline("onos -w "+str(ONOS_ip))
202 self.handle.expect("onos>")
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400203 return main.TRUE
204 else:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400205 #If failed, send ctrl+c to process and try again
andrewonlabf47993a2014-10-24 17:56:01 -0400206 main.log.info("Starting CLI failed. Retrying...")
Jon Hallffb386d2014-11-21 13:43:38 -0800207 self.handle.send("\x03")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400208 self.handle.sendline("onos -w "+str(ONOS_ip))
209 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
210 timeout=30)
211 if i == 0:
andrewonlab28ca4b22014-11-20 13:15:59 -0500212 main.log.info(str(ONOS_ip)+" CLI Started "+
213 "successfully after retry attempt")
Hari Krishnae36ef212015-01-04 14:09:13 -0800214 if karafTimeout:
215 self.handle.sendline("config:property-set -p org.apache.karaf.shell sshIdleTimeout "+karafTimeout)
216 self.handle.expect("\$")
217 self.handle.sendline("onos -w "+str(ONOS_ip))
218 self.handle.expect("onos>")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400219 return main.TRUE
220 else:
221 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400222 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400223 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400224
225 except pexpect.EOF:
226 main.log.error(self.name + ": EOF exception found")
227 main.log.error(self.name + ": " + self.handle.before)
228 main.cleanup()
229 main.exit()
230 except:
231 main.log.info(self.name+" ::::::")
232 main.log.error( traceback.print_exc())
233 main.log.info(self.name+" ::::::")
234 main.cleanup()
235 main.exit()
236
andrewonlaba18f6bf2014-10-13 19:31:54 -0400237 def sendline(self, cmd_str):
238 '''
239 Send a completely user specified string to
240 the onos> prompt. Use this function if you have
241 a very specific command to send.
242
243 Warning: There are no sanity checking to commands
244 sent using this method.
245 '''
246 try:
247 self.handle.sendline("")
248 self.handle.expect("onos>")
249
250 self.handle.sendline(cmd_str)
251 self.handle.expect("onos>")
252
253 handle = self.handle.before
254
255 self.handle.sendline("")
256 self.handle.expect("onos>")
257
258 handle += self.handle.before
259 handle += self.handle.after
260
261 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400262 ansi_escape = re.compile(r'\x1b[^m]*m')
263 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400264
265 return handle
266 except pexpect.EOF:
267 main.log.error(self.name + ": EOF exception found")
268 main.log.error(self.name + ": " + self.handle.before)
269 main.cleanup()
270 main.exit()
271 except:
272 main.log.info(self.name+" ::::::")
273 main.log.error( traceback.print_exc())
274 main.log.info(self.name+" ::::::")
275 main.cleanup()
276 main.exit()
277
andrewonlab95ce8322014-10-13 14:12:04 -0400278 #IMPORTANT NOTE:
279 #For all cli commands, naming convention should match
280 #the cli command replacing ':' with '_'.
281 #Ex) onos:topology > onos_topology
282 # onos:links > onos_links
283 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400284
285 def add_node(self, node_id, ONOS_ip, tcp_port=""):
286 '''
287 Adds a new cluster node by ID and address information.
288 Required:
289 * node_id
290 * ONOS_ip
291 Optional:
292 * tcp_port
293 '''
294 try:
295 self.handle.sendline("")
296 self.handle.expect("onos>")
297
298 self.handle.sendline("add-node "+
299 str(node_id)+" "+
300 str(ONOS_ip)+" "+
301 str(tcp_port))
302
303 i = self.handle.expect([
304 "Error",
305 "onos>" ])
306
307 #Clear handle to get previous output
308 self.handle.sendline("")
309 self.handle.expect("onos>")
310
311 handle = self.handle.before
312
313 if i == 0:
314 main.log.error("Error in adding node")
315 main.log.error(handle)
316 return main.FALSE
317 else:
318 main.log.info("Node "+str(ONOS_ip)+" added")
319 return main.TRUE
320
321 except pexpect.EOF:
322 main.log.error(self.name + ": EOF exception found")
323 main.log.error(self.name + ": " + self.handle.before)
324 main.cleanup()
325 main.exit()
326 except:
327 main.log.info(self.name+" ::::::")
328 main.log.error( traceback.print_exc())
329 main.log.info(self.name+" ::::::")
330 main.cleanup()
331 main.exit()
332
andrewonlab86dc3082014-10-13 18:18:38 -0400333 def remove_node(self, node_id):
334 '''
335 Removes a cluster by ID
336 Issues command: 'remove-node [<node-id>]'
337 Required:
338 * node_id
339 '''
340 try:
341 self.handle.sendline("")
342 self.handle.expect("onos>")
343
344 self.handle.sendline("remove-node "+str(node_id))
345 self.handle.expect("onos>")
346
347 return main.TRUE
348
349 except pexpect.EOF:
350 main.log.error(self.name + ": EOF exception found")
351 main.log.error(self.name + ": " + self.handle.before)
352 main.cleanup()
353 main.exit()
354 except:
355 main.log.info(self.name+" ::::::")
356 main.log.error( traceback.print_exc())
357 main.log.info(self.name+" ::::::")
358 main.cleanup()
359 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400360
andrewonlab7c211572014-10-15 16:45:20 -0400361 def nodes(self):
362 '''
363 List the nodes currently visible
364 Issues command: 'nodes'
365 Returns: entire handle of list of nodes
366 '''
367 try:
368 self.handle.sendline("")
369 self.handle.expect("onos>")
370
371 self.handle.sendline("nodes")
372 self.handle.expect("onos>")
373
374 self.handle.sendline("")
375 self.handle.expect("onos>")
376
377 handle = self.handle.before
378
379 return handle
380
381 except pexpect.EOF:
382 main.log.error(self.name + ": EOF exception found")
383 main.log.error(self.name + ": " + self.handle.before)
384 main.cleanup()
385 main.exit()
386 except:
387 main.log.info(self.name+" ::::::")
388 main.log.error( traceback.print_exc())
389 main.log.info(self.name+" ::::::")
390 main.cleanup()
391 main.exit()
392
andrewonlab38d6ae22014-10-15 14:23:45 -0400393 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400394 '''
395 Shows the current state of the topology
396 by issusing command: 'onos> onos:topology'
397 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400398 try:
399 self.handle.sendline("")
400 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400401 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400402 self.handle.sendline("onos:topology")
403 self.handle.expect("onos>")
404
405 handle = self.handle.before
406
407 main.log.info("onos:topology returned: " +
408 str(handle))
409
410 return handle
411
412 except pexpect.EOF:
413 main.log.error(self.name + ": EOF exception found")
414 main.log.error(self.name + ": " + self.handle.before)
415 main.cleanup()
416 main.exit()
417 except:
418 main.log.info(self.name+" ::::::")
419 main.log.error( traceback.print_exc())
420 main.log.info(self.name+" ::::::")
421 main.cleanup()
422 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400423
424 def feature_install(self, feature_str):
425 '''
426 Installs a specified feature
427 by issuing command: 'onos> feature:install <feature_str>'
428 '''
429 try:
430 self.handle.sendline("")
431 self.handle.expect("onos>")
432
433 self.handle.sendline("feature:install "+str(feature_str))
434 self.handle.expect("onos>")
435
436 return main.TRUE
437
438 except pexpect.EOF:
439 main.log.error(self.name + ": EOF exception found")
440 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500441 main.log.report("Failed to install feature")
442 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400443 main.cleanup()
444 main.exit()
445 except:
446 main.log.info(self.name+" ::::::")
447 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500448 main.log.report("Failed to install feature")
449 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400450 main.log.info(self.name+" ::::::")
451 main.cleanup()
452 main.exit()
453
454 def feature_uninstall(self, feature_str):
455 '''
456 Uninstalls a specified feature
457 by issuing command: 'onos> feature:uninstall <feature_str>'
458 '''
459 try:
460 self.handle.sendline("")
461 self.handle.expect("onos>")
462
463 self.handle.sendline("feature:uninstall "+str(feature_str))
464 self.handle.expect("onos>")
465
466 return main.TRUE
467
468 except pexpect.EOF:
469 main.log.error(self.name + ": EOF exception found")
470 main.log.error(self.name + ": " + self.handle.before)
471 main.cleanup()
472 main.exit()
473 except:
474 main.log.info(self.name+" ::::::")
475 main.log.error( traceback.print_exc())
476 main.log.info(self.name+" ::::::")
477 main.cleanup()
478 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800479
480 def devices(self, json_format=True):
andrewonlab86dc3082014-10-13 18:18:38 -0400481 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400482 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400483 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800484 * json_format - boolean indicating if you want output in json
andrewonlab86dc3082014-10-13 18:18:38 -0400485 '''
486 try:
487 self.handle.sendline("")
488 self.handle.expect("onos>")
andrewonlabb66dfa12014-12-02 15:51:10 -0500489
Jon Halle8217482014-10-17 13:49:14 -0400490 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800491 self.handle.sendline("devices -j")
492 self.handle.expect("devices -j")
493 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400494 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400495 '''
496 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
497 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 -0400498 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 -0400499 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400500 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400501 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400502 '''
503 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400504 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
505 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400506 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400507 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400508 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800509 self.handle.sendline("devices")
510 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400511 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400512 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400513 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400514 except pexpect.EOF:
515 main.log.error(self.name + ": EOF exception found")
516 main.log.error(self.name + ": " + self.handle.before)
517 main.cleanup()
518 main.exit()
519 except:
520 main.log.info(self.name+" ::::::")
521 main.log.error( traceback.print_exc())
522 main.log.info(self.name+" ::::::")
523 main.cleanup()
524 main.exit()
525
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800526
527 def balance_masters(self):
528 '''
529 This balances the devices across all controllers
530 by issuing command: 'onos> onos:balance-masters'
531 If required this could be extended to return devices balanced output.
532 '''
533 try:
534 self.handle.sendline("")
535 self.handle.expect("onos>")
536
537 self.handle.sendline("onos:balance-masters")
538 self.handle.expect("onos>")
539 return main.TRUE
540
541 except pexpect.EOF:
542 main.log.error(self.name + ": EOF exception found")
543 main.log.error(self.name + ": " + self.handle.before)
544 main.cleanup()
545 main.exit()
546 except:
547 main.log.info(self.name+" ::::::")
548 main.log.error( traceback.print_exc())
549 main.log.info(self.name+" ::::::")
550 main.cleanup()
551 main.exit()
552
Hari Krishna2bbaa702014-12-19 14:46:12 -0800553 def links(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400554 '''
555 Lists all core links
556 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800557 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400558 '''
559 try:
560 self.handle.sendline("")
561 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800562
Jon Halle8217482014-10-17 13:49:14 -0400563 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800564 self.handle.sendline("links -j")
565 self.handle.expect("links -j")
566 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400567 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400568 '''
569 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
570 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 -0400571 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 -0800572 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400573 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800574 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400575 '''
576 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400577 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
578 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400579 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400580 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400581 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800582 self.handle.sendline("links")
583 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400584 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400585 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400586 return handle
Jon Halle8217482014-10-17 13:49:14 -0400587 except pexpect.EOF:
588 main.log.error(self.name + ": EOF exception found")
589 main.log.error(self.name + ": " + self.handle.before)
590 main.cleanup()
591 main.exit()
592 except:
593 main.log.info(self.name+" ::::::")
594 main.log.error( traceback.print_exc())
595 main.log.info(self.name+" ::::::")
596 main.cleanup()
597 main.exit()
598
599
Jon Hallffb386d2014-11-21 13:43:38 -0800600 def ports(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400601 '''
602 Lists all ports
603 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800604 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400605 '''
606 try:
607 self.handle.sendline("")
608 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800609
Jon Halle8217482014-10-17 13:49:14 -0400610 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800611 self.handle.sendline("ports -j")
612 self.handle.expect("ports -j")
613 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400614 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400615 '''
616 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
617 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 -0400618 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 -0800619 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400620 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800621 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400622 '''
623 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400624 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
625 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400626 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400627 return handle1
628
Jon Halle8217482014-10-17 13:49:14 -0400629 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800630 self.handle.sendline("ports")
631 self.handle.expect("onos>")
632 self.handle.sendline("")
633 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400634 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400635 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800636 return handle
Jon Halle8217482014-10-17 13:49:14 -0400637 except pexpect.EOF:
638 main.log.error(self.name + ": EOF exception found")
639 main.log.error(self.name + ": " + self.handle.before)
640 main.cleanup()
641 main.exit()
642 except:
643 main.log.info(self.name+" ::::::")
644 main.log.error( traceback.print_exc())
645 main.log.info(self.name+" ::::::")
646 main.cleanup()
647 main.exit()
648
649
Jon Hallffb386d2014-11-21 13:43:38 -0800650 def roles(self, json_format=True):
andrewonlab7c211572014-10-15 16:45:20 -0400651 '''
Jon Hall983a1702014-10-28 18:44:22 -0400652 Lists all devices and the controllers with roles assigned to them
653 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800654 * json_format - boolean indicating if you want output in json
andrewonlab7c211572014-10-15 16:45:20 -0400655 '''
656 try:
657 self.handle.sendline("")
658 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500659
Jon Hall983a1702014-10-28 18:44:22 -0400660 if json_format:
Jon Hallb1290e82014-11-18 16:17:48 -0500661 self.handle.sendline("roles -j")
662 self.handle.expect("roles -j")
663 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400664 handle = self.handle.before
665 '''
Jon Hallb1290e82014-11-18 16:17:48 -0500666 handle variable here contains some ANSI escape color code sequences at the
667 end which are invisible in the print command output. To make that escape
668 sequence visible, use repr() function. The repr(handle) output when printed
669 shows the ANSI escape sequences. In json.loads(somestring), this somestring
670 variable is actually repr(somestring) and json.loads would fail with the escape
671 sequence.
672
673 So we take off that escape sequence using the following commads:
Jon Hall983a1702014-10-28 18:44:22 -0400674 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallb1290e82014-11-18 16:17:48 -0500675 handle1 = ansi_escape.sub('', handle)
Jon Hall983a1702014-10-28 18:44:22 -0400676 '''
677 #print "repr(handle) =", repr(handle)
678 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
679 handle1 = ansi_escape.sub('', handle)
680 #print "repr(handle1) = ", repr(handle1)
681 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400682
andrewonlab7c211572014-10-15 16:45:20 -0400683 else:
Jon Hallb1290e82014-11-18 16:17:48 -0500684 self.handle.sendline("roles")
685 self.handle.expect("onos>")
686 self.handle.sendline("")
687 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400688 handle = self.handle.before
689 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800690 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400691 except pexpect.EOF:
692 main.log.error(self.name + ": EOF exception found")
693 main.log.error(self.name + ": " + self.handle.before)
694 main.cleanup()
695 main.exit()
696 except:
697 main.log.info(self.name+" ::::::")
698 main.log.error( traceback.print_exc())
699 main.log.info(self.name+" ::::::")
700 main.cleanup()
701 main.exit()
702
703 def get_role(self, device_id):
704 '''
705 Given the a string containing the json representation of the "roles" cli command and a
706 partial or whole device id, returns a json object containing the
707 roles output for the first device whose id contains "device_id"
708
709 Returns:
710 Dict of the role assignments for the given device or
711 None if not match
712 '''
713 try:
714 import json
715 if device_id == None:
716 return None
717 else:
718 raw_roles = self.roles()
719 roles_json = json.loads(raw_roles)
720 #search json for the device with id then return the device
721 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400722 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400723 if str(device_id) in device['id']:
724 return device
725 return None
andrewonlab7c211572014-10-15 16:45:20 -0400726
andrewonlab86dc3082014-10-13 18:18:38 -0400727 except pexpect.EOF:
728 main.log.error(self.name + ": EOF exception found")
729 main.log.error(self.name + ": " + self.handle.before)
730 main.cleanup()
731 main.exit()
732 except:
733 main.log.info(self.name+" ::::::")
734 main.log.error( traceback.print_exc())
735 main.log.info(self.name+" ::::::")
736 main.cleanup()
737 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800738
739 def roles_not_null(self):
740 '''
741 Iterates through each device and checks if there is a master assigned
742 Returns: main.TRUE if each device has a master
743 main.FALSE any device has no master
744 '''
745 try:
746 import json
747 raw_roles = self.roles()
748 roles_json = json.loads(raw_roles)
749 #search json for the device with id then return the device
750 for device in roles_json:
751 #print device
752 if device['master'] == "none":
753 main.log.warn("Device has no master: " + str(device) )
754 return main.FALSE
755 return main.TRUE
756
757 except pexpect.EOF:
758 main.log.error(self.name + ": EOF exception found")
759 main.log.error(self.name + ": " + self.handle.before)
760 main.cleanup()
761 main.exit()
762 except:
763 main.log.info(self.name+" ::::::")
764 main.log.error( traceback.print_exc())
765 main.log.info(self.name+" ::::::")
766 main.cleanup()
767 main.exit()
768
769
andrewonlab3e15ead2014-10-15 14:21:34 -0400770 def paths(self, src_id, dst_id):
771 '''
772 Returns string of paths, and the cost.
773 Issues command: onos:paths <src> <dst>
774 '''
775 try:
776 self.handle.sendline("")
777 self.handle.expect("onos>")
778
779 self.handle.sendline("onos:paths "+
780 str(src_id) + " " + str(dst_id))
781 i = self.handle.expect([
782 "Error",
783 "onos>"])
784
785 self.handle.sendline("")
786 self.handle.expect("onos>")
787
788 handle = self.handle.before
789
790 if i == 0:
791 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400792 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400793 else:
794 path = handle.split(";")[0]
795 cost = handle.split(";")[1]
796 return (path, cost)
797
798 except pexpect.EOF:
799 main.log.error(self.name + ": EOF exception found")
800 main.log.error(self.name + ": " + self.handle.before)
801 main.cleanup()
802 main.exit()
803 except:
804 main.log.info(self.name+" ::::::")
805 main.log.error( traceback.print_exc())
806 main.log.info(self.name+" ::::::")
807 main.cleanup()
808 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800809
810 def hosts(self, json_format=True):
Jon Hall42db6dc2014-10-24 19:03:48 -0400811 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800812 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400813 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800814 * json_format - boolean indicating if you want output in json
Jon Hall42db6dc2014-10-24 19:03:48 -0400815 '''
816 try:
817 self.handle.sendline("")
818 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800819
Jon Hall42db6dc2014-10-24 19:03:48 -0400820 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800821 self.handle.sendline("hosts -j")
822 self.handle.expect("hosts -j")
823 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400824 handle = self.handle.before
825 '''
826 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
827 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
828 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 -0800829 So we take off that escape sequence using
Jon Hall42db6dc2014-10-24 19:03:48 -0400830 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800831 handle1 = ansi_escape.sub('', handle)
Jon Hall42db6dc2014-10-24 19:03:48 -0400832 '''
833 #print "repr(handle) =", repr(handle)
834 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
835 handle1 = ansi_escape.sub('', handle)
836 #print "repr(handle1) = ", repr(handle1)
837 return handle1
838 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800839 self.handle.sendline("hosts")
840 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400841 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400842 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400843 return handle
844 except pexpect.EOF:
845 main.log.error(self.name + ": EOF exception found")
846 main.log.error(self.name + ": " + self.handle.before)
847 main.cleanup()
848 main.exit()
849 except:
850 main.log.info(self.name+" ::::::")
851 main.log.error( traceback.print_exc())
852 main.log.info(self.name+" ::::::")
853 main.cleanup()
854 main.exit()
855
856 def get_host(self, mac):
857 '''
858 Return the first host from the hosts api whose 'id' contains 'mac'
859 Note: mac must be a colon seperated mac address, but could be a partial mac address
860 Return None if there is no match
861 '''
862 import json
863 try:
864 if mac == None:
865 return None
866 else:
867 mac = mac
868 raw_hosts = self.hosts()
869 hosts_json = json.loads(raw_hosts)
870 #search json for the host with mac then return the device
871 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400872 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400873 if mac in host['id']:
874 return host
875 return None
876 except pexpect.EOF:
877 main.log.error(self.name + ": EOF exception found")
878 main.log.error(self.name + ": " + self.handle.before)
879 main.cleanup()
880 main.exit()
881 except:
882 main.log.info(self.name+" ::::::")
883 main.log.error( traceback.print_exc())
884 main.log.info(self.name+" ::::::")
885 main.cleanup()
886 main.exit()
887
andrewonlab3f0a4af2014-10-17 12:25:14 -0400888
889 def get_hosts_id(self, host_list):
890 '''
891 Obtain list of hosts
892 Issues command: 'onos> hosts'
893
894 Required:
895 * host_list: List of hosts obtained by Mininet
896 IMPORTANT:
897 This function assumes that you started your
898 topology with the option '--mac'.
899 Furthermore, it assumes that value of VLAN is '-1'
900 Description:
901 Converts mininet hosts (h1, h2, h3...) into
902 ONOS format (00:00:00:00:00:01/-1 , ...)
903 '''
904
905 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400906 onos_host_list = []
907
908 for host in host_list:
909 host = host.replace("h", "")
910 host_hex = hex(int(host)).zfill(12)
911 host_hex = str(host_hex).replace('x','0')
912 i = iter(str(host_hex))
913 host_hex = ":".join(a+b for a,b in zip(i,i))
914 host_hex = host_hex + "/-1"
915 onos_host_list.append(host_hex)
916
917 return onos_host_list
918
919 except pexpect.EOF:
920 main.log.error(self.name + ": EOF exception found")
921 main.log.error(self.name + ": " + self.handle.before)
922 main.cleanup()
923 main.exit()
924 except:
925 main.log.info(self.name+" ::::::")
926 main.log.error( traceback.print_exc())
927 main.log.info(self.name+" ::::::")
928 main.cleanup()
929 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400930
andrewonlabe6745342014-10-17 14:29:13 -0400931 def add_host_intent(self, host_id_one, host_id_two):
932 '''
933 Required:
934 * host_id_one: ONOS host id for host1
935 * host_id_two: ONOS host id for host2
936 Description:
937 Adds a host-to-host intent (bidrectional) by
Jon Hallb1290e82014-11-18 16:17:48 -0500938 specifying the two hosts.
andrewonlabe6745342014-10-17 14:29:13 -0400939 '''
940 try:
941 self.handle.sendline("")
942 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500943
andrewonlabe6745342014-10-17 14:29:13 -0400944 self.handle.sendline("add-host-intent "+
945 str(host_id_one) + " " + str(host_id_two))
946 self.handle.expect("onos>")
947
andrewonlabe6745342014-10-17 14:29:13 -0400948 handle = self.handle.before
Jon Hallffb386d2014-11-21 13:43:38 -0800949 #print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400950
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800951 main.log.info("Host intent installed between "+
andrewonlabe6745342014-10-17 14:29:13 -0400952 str(host_id_one) + " and " + str(host_id_two))
953
954 return handle
Jon Hallb1290e82014-11-18 16:17:48 -0500955
andrewonlabe6745342014-10-17 14:29:13 -0400956 except pexpect.EOF:
957 main.log.error(self.name + ": EOF exception found")
958 main.log.error(self.name + ": " + self.handle.before)
959 main.cleanup()
960 main.exit()
961 except:
962 main.log.info(self.name+" ::::::")
963 main.log.error( traceback.print_exc())
964 main.log.info(self.name+" ::::::")
965 main.cleanup()
966 main.exit()
967
andrewonlab7b31d232014-10-24 13:31:47 -0400968 def add_optical_intent(self, ingress_device, egress_device):
969 '''
970 Required:
971 * ingress_device: device id of ingress device
972 * egress_device: device id of egress device
973 Optional:
974 TODO: Still needs to be implemented via dev side
975 '''
976 try:
977 self.handle.sendline("add-optical-intent "+
978 str(ingress_device) + " " + str(egress_device))
979 self.handle.expect("add-optical-intent")
980 i = self.handle.expect([
981 "Error",
982 "onos>"])
983
984 handle = self.handle.before
985
986 #If error, return error message
987 if i == 0:
988 return handle
989 else:
990 return main.TRUE
991
992 except pexpect.EOF:
993 main.log.error(self.name + ": EOF exception found")
994 main.log.error(self.name + ": " + self.handle.before)
995 main.cleanup()
996 main.exit()
997 except:
998 main.log.info(self.name+" ::::::")
999 main.log.error( traceback.print_exc())
1000 main.log.info(self.name+" ::::::")
1001 main.cleanup()
1002 main.exit()
1003
andrewonlab36af3822014-11-18 17:48:18 -05001004 def add_point_intent(self, ingress_device, egress_device,
1005 port_ingress="", port_egress="", ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -05001006 ethDst="", bandwidth="", lambda_alloc=False,
1007 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -04001008 '''
1009 Required:
1010 * ingress_device: device id of ingress device
1011 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001012 Optional:
1013 * ethType: specify ethType
1014 * ethSrc: specify ethSrc (i.e. src mac addr)
1015 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001016 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -05001017 * lambda_alloc: if True, intent will allocate lambda
1018 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -05001019 * ipProto: specify ip protocol
1020 * ipSrc: specify ip source address
1021 * ipDst: specify ip destination address
1022 * tcpSrc: specify tcp source port
1023 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001024 Description:
1025 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -04001026 specifying device id's and optional fields
1027
andrewonlab4dbb4d82014-10-17 18:22:31 -04001028 NOTE: This function may change depending on the
1029 options developers provide for point-to-point
1030 intent via cli
1031 '''
1032 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001033 cmd = ""
1034
1035 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001036 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001037 and not bandwidth and not lambda_alloc \
1038 and not ipProto and not ipSrc and not ipDst \
1039 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001040 cmd = "add-point-intent"
1041
1042
andrewonlab289e4b72014-10-21 21:24:18 -04001043 else:
andrewonlab36af3822014-11-18 17:48:18 -05001044 cmd = "add-point-intent"
andrewonlab9a130be2014-10-22 12:44:56 -04001045
andrewonlab0c0a6772014-10-22 12:31:18 -04001046 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001047 cmd += " --ethType " + str(ethType)
1048 if ethSrc:
1049 cmd += " --ethSrc " + str(ethSrc)
1050 if ethDst:
1051 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001052 if bandwidth:
1053 cmd += " --bandwidth " + str(bandwidth)
1054 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001055 cmd += " --lambda "
1056 if ipProto:
1057 cmd += " --ipProto " + str(ipProto)
1058 if ipSrc:
1059 cmd += " --ipSrc " + str(ipSrc)
1060 if ipDst:
1061 cmd += " --ipDst " + str(ipDst)
1062 if tcpSrc:
1063 cmd += " --tcpSrc " + str(tcpSrc)
1064 if tcpDst:
1065 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001066
andrewonlab36af3822014-11-18 17:48:18 -05001067 #Check whether the user appended the port
1068 #or provided it as an input
1069 if "/" in ingress_device:
1070 cmd += " "+str(ingress_device)
1071 else:
1072 if not port_ingress:
1073 main.log.error("You must specify "+
1074 "the ingress port")
1075 #TODO: perhaps more meaningful return
1076 return main.FALSE
1077
1078 cmd += " "+ \
1079 str(ingress_device) + "/" +\
1080 str(port_ingress) + " "
1081
1082 if "/" in egress_device:
1083 cmd += " "+str(egress_device)
1084 else:
1085 if not port_egress:
1086 main.log.error("You must specify "+
1087 "the egress port")
1088 return main.FALSE
1089
1090 cmd += " "+\
1091 str(egress_device) + "/" +\
1092 str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001093
andrewonlab289e4b72014-10-21 21:24:18 -04001094 self.handle.sendline(cmd)
andrewonlab36af3822014-11-18 17:48:18 -05001095
1096 main.log.info(cmd + " sent")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001097 i = self.handle.expect([
1098 "Error",
1099 "onos>"])
andrewonlab4dbb4d82014-10-17 18:22:31 -04001100
1101 if i == 0:
1102 main.log.error("Error in adding point-to-point intent")
1103 return handle
1104 else:
1105 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001106
andrewonlab4dbb4d82014-10-17 18:22:31 -04001107 except pexpect.EOF:
1108 main.log.error(self.name + ": EOF exception found")
1109 main.log.error(self.name + ": " + self.handle.before)
1110 main.cleanup()
1111 main.exit()
1112 except:
1113 main.log.info(self.name+" ::::::")
1114 main.log.error( traceback.print_exc())
1115 main.log.info(self.name+" ::::::")
1116 main.cleanup()
1117 main.exit()
1118
shahshreyad0c80432014-12-04 16:56:05 -08001119
1120 def add_multipoint_to_singlepoint_intent(self, ingress_device1, ingress_device2,egress_device,
1121 port_ingress="", port_egress="", ethType="", ethSrc="",
1122 ethDst="", bandwidth="", lambda_alloc=False,
1123 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="", setEthDst=""):
1124 '''
1125 Note:
1126 This function assumes that there would be 2 ingress devices and one egress device
1127 For more number of ingress devices, this function needs to be modified
1128 Required:
1129 * ingress_device1: device id of ingress device1
1130 * ingress_device2: device id of ingress device2
1131 * egress_device: device id of egress device
1132 Optional:
1133 * ethType: specify ethType
1134 * ethSrc: specify ethSrc (i.e. src mac addr)
1135 * ethDst: specify ethDst (i.e. dst mac addr)
1136 * bandwidth: specify bandwidth capacity of link
1137 * lambda_alloc: if True, intent will allocate lambda
1138 for the specified intent
1139 * ipProto: specify ip protocol
1140 * ipSrc: specify ip source address
1141 * ipDst: specify ip destination address
1142 * tcpSrc: specify tcp source port
1143 * tcpDst: specify tcp destination port
1144 * setEthSrc: action to Rewrite Source MAC Address
1145 * setEthDst: action to Rewrite Destination MAC Address
1146 Description:
1147 Adds a multipoint-to-singlepoint intent (uni-directional) by
1148 specifying device id's and optional fields
1149
1150 NOTE: This function may change depending on the
1151 options developers provide for multipointpoint-to-singlepoint
1152 intent via cli
1153 '''
1154 try:
1155 cmd = ""
1156
1157 #If there are no optional arguments
1158 if not ethType and not ethSrc and not ethDst\
1159 and not bandwidth and not lambda_alloc \
1160 and not ipProto and not ipSrc and not ipDst \
1161 and not tcpSrc and not tcpDst and not setEthSrc and not setEthDst:
1162 cmd = "add-multi-to-single-intent"
1163
1164
1165 else:
1166 cmd = "add-multi-to-single-intent"
1167
1168 if ethType:
1169 cmd += " --ethType " + str(ethType)
1170 if ethSrc:
1171 cmd += " --ethSrc " + str(ethSrc)
1172 if ethDst:
1173 cmd += " --ethDst " + str(ethDst)
1174 if bandwidth:
1175 cmd += " --bandwidth " + str(bandwidth)
1176 if lambda_alloc:
1177 cmd += " --lambda "
1178 if ipProto:
1179 cmd += " --ipProto " + str(ipProto)
1180 if ipSrc:
1181 cmd += " --ipSrc " + str(ipSrc)
1182 if ipDst:
1183 cmd += " --ipDst " + str(ipDst)
1184 if tcpSrc:
1185 cmd += " --tcpSrc " + str(tcpSrc)
1186 if tcpDst:
1187 cmd += " --tcpDst " + str(tcpDst)
1188 if setEthSrc:
1189 cmd += " --setEthSrc "+ str(setEthSrc)
1190 if setEthDst:
1191 cmd += " --setEthDst "+ str(setEthDst)
1192
1193 #Check whether the user appended the port
1194 #or provided it as an input
1195 if "/" in ingress_device1:
1196 cmd += " "+str(ingress_device1)
1197 else:
1198 if not port_ingress1:
1199 main.log.error("You must specify "+
1200 "the ingress port1")
1201 #TODO: perhaps more meaningful return
1202 return main.FALSE
1203
1204 cmd += " "+ \
1205 str(ingress_device1) + "/" +\
1206 str(port_ingress1) + " "
1207
1208 if "/" in ingress_device2:
1209 cmd += " "+str(ingress_device2)
1210 else:
1211 if not port_ingress2:
1212 main.log.error("You must specify "+
1213 "the ingress port2")
1214 #TODO: perhaps more meaningful return
1215 return main.FALSE
1216
1217 cmd += " "+ \
1218 str(ingress_device2) + "/" +\
1219 str(port_ingress2) + " "
1220
1221 if "/" in egress_device:
1222 cmd += " "+str(egress_device)
1223 else:
1224 if not port_egress:
1225 main.log.error("You must specify "+
1226 "the egress port")
1227 return main.FALSE
1228
1229 cmd += " "+\
1230 str(egress_device) + "/" +\
1231 str(port_egress)
1232 print "cmd= ",cmd
1233 self.handle.sendline(cmd)
1234
1235 main.log.info(cmd + " sent")
1236 i = self.handle.expect([
1237 "Error",
1238 "onos>"])
1239
1240 if i == 0:
1241 main.log.error("Error in adding point-to-point intent")
1242 return self.handle
1243 else:
1244 return main.TRUE
1245
1246 except pexpect.EOF:
1247 main.log.error(self.name + ": EOF exception found")
1248 main.log.error(self.name + ": " + self.handle.before)
1249 main.cleanup()
1250 main.exit()
1251 except:
1252 main.log.info(self.name+" ::::::")
1253 main.log.error( traceback.print_exc())
1254 main.log.info(self.name+" ::::::")
1255 main.cleanup()
1256 main.exit()
1257
1258
andrewonlab9a50dfe2014-10-17 17:22:31 -04001259 def remove_intent(self, intent_id):
1260 '''
1261 Remove intent for specified intent id
1262 '''
1263 try:
1264 self.handle.sendline("")
1265 self.handle.expect("onos>")
1266
1267 self.handle.sendline("remove-intent "+str(intent_id))
1268 i = self.handle.expect([
1269 "Error",
1270 "onos>"])
1271
1272 handle = self.handle.before
1273
1274 if i == 0:
1275 main.log.error("Error in removing intent")
1276 return handle
1277 else:
1278 return handle
1279
1280 except pexpect.EOF:
1281 main.log.error(self.name + ": EOF exception found")
1282 main.log.error(self.name + ": " + self.handle.before)
1283 main.cleanup()
1284 main.exit()
1285 except:
1286 main.log.info(self.name+" ::::::")
1287 main.log.error( traceback.print_exc())
1288 main.log.info(self.name+" ::::::")
1289 main.cleanup()
1290 main.exit()
1291
pingping-lindabe7972014-11-17 19:29:44 -08001292 # This method should be used after installing application: onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001293 def routes(self, json_format=False):
1294 '''
1295 Optional:
1296 * json_format: enable output formatting in json
1297 Description:
1298 Obtain all routes in the system
1299 '''
1300 try:
1301 if json_format:
1302 self.handle.sendline("routes -j")
1303 self.handle.expect("routes -j")
1304 self.handle.expect("onos>")
1305 handle_tmp = self.handle.before
1306
1307 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1308 handle = ansi_escape.sub('', handle_tmp)
1309
1310 else:
1311 self.handle.sendline("")
1312 self.handle.expect("onos>")
1313
1314 self.handle.sendline("routes")
1315 self.handle.expect("onos>")
1316 handle = self.handle.before
1317
1318 return handle
1319
1320 except pexpect.EOF:
1321 main.log.error(self.name + ": EOF exception found")
1322 main.log.error(self.name + ": " + self.handle.before)
1323 main.cleanup()
1324 main.exit()
1325 except:
1326 main.log.info(self.name + " ::::::")
1327 main.log.error(traceback.print_exc())
1328 main.log.info(self.name + " ::::::")
1329 main.cleanup()
1330 main.exit()
1331
Jon Hallffb386d2014-11-21 13:43:38 -08001332 def intents(self, json_format = True):
andrewonlabe6745342014-10-17 14:29:13 -04001333 '''
andrewonlab377693f2014-10-21 16:00:30 -04001334 Optional:
1335 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001336 Description:
1337 Obtain intents currently installed
1338 '''
1339 try:
andrewonlab377693f2014-10-21 16:00:30 -04001340 if json_format:
1341 self.handle.sendline("intents -j")
1342 self.handle.expect("intents -j")
1343 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001344 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001345
1346 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1347 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001348 else:
1349 self.handle.sendline("")
1350 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001351
andrewonlab377693f2014-10-21 16:00:30 -04001352 self.handle.sendline("intents")
1353 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001354 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001355
1356 return handle
1357
1358 except pexpect.EOF:
1359 main.log.error(self.name + ": EOF exception found")
1360 main.log.error(self.name + ": " + self.handle.before)
1361 main.cleanup()
1362 main.exit()
1363 except:
1364 main.log.info(self.name+" ::::::")
1365 main.log.error( traceback.print_exc())
1366 main.log.info(self.name+" ::::::")
1367 main.cleanup()
1368 main.exit()
1369
Jon Hallffb386d2014-11-21 13:43:38 -08001370 def flows(self, json_format = True):
Shreya Shah0f01c812014-10-26 20:15:28 -04001371 '''
1372 Optional:
1373 * json_format: enable output formatting in json
1374 Description:
1375 Obtain flows currently installed
1376 '''
1377 try:
1378 if json_format:
1379 self.handle.sendline("flows -j")
1380 self.handle.expect("flows -j")
1381 self.handle.expect("onos>")
1382 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001383 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1384 handle = ansi_escape.sub('', handle)
Shreya Shah0f01c812014-10-26 20:15:28 -04001385
1386 else:
1387 self.handle.sendline("")
1388 self.handle.expect("onos>")
1389 self.handle.sendline("flows")
1390 self.handle.expect("onos>")
1391 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001392 if re.search("Error\sexecuting\scommand:", handle):
1393 main.log.error(self.name + ".flows() response: " + str(handle))
Shreya Shah0f01c812014-10-26 20:15:28 -04001394
1395 return handle
1396
1397 except pexpect.EOF:
1398 main.log.error(self.name + ": EOF exception found")
1399 main.log.error(self.name + ": " + self.handle.before)
1400 main.cleanup()
1401 main.exit()
1402 except:
1403 main.log.info(self.name+" ::::::")
1404 main.log.error( traceback.print_exc())
1405 main.log.info(self.name+" ::::::")
1406 main.cleanup()
1407 main.exit()
1408
andrewonlabb66dfa12014-12-02 15:51:10 -05001409 def push_test_intents(self, dpid_src, dpid_dst, num_intents,
1410 num_mult="", app_id="", report=True):
andrewonlab87852b02014-11-19 18:44:19 -05001411 '''
1412 Description:
1413 Push a number of intents in a batch format to
1414 a specific point-to-point intent definition
1415 Required:
1416 * dpid_src: specify source dpid
1417 * dpid_dst: specify destination dpid
1418 * num_intents: specify number of intents to push
1419 Optional:
andrewonlabb66dfa12014-12-02 15:51:10 -05001420 * num_mult: number multiplier for multiplying
1421 the number of intents specified
1422 * app_id: specify the application id init to further
1423 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001424 * report: default True, returns latency information
1425 '''
1426 try:
1427 cmd = "push-test-intents "+\
1428 str(dpid_src)+" "+str(dpid_dst)+" "+\
1429 str(num_intents)
andrewonlabb66dfa12014-12-02 15:51:10 -05001430
1431 if num_mult:
1432 cmd += " " + str(num_mult)
andrewonlab042b3912014-12-10 16:40:50 -05001433 #If app id is specified, then num_mult
1434 #must exist because of the way this command
1435 #takes in arguments
andrewonlabb66dfa12014-12-02 15:51:10 -05001436 if app_id:
1437 cmd += " " + str(app_id)
1438
andrewonlab87852b02014-11-19 18:44:19 -05001439 self.handle.sendline(cmd)
1440 self.handle.expect(cmd)
1441 self.handle.expect("onos>")
1442
1443 handle = self.handle.before
1444
1445 #Some color thing that we want to escape
1446 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1447 handle = ansi_escape.sub('', handle)
1448
1449 if report:
andrewonlabb66dfa12014-12-02 15:51:10 -05001450 lat_result = []
andrewonlab87852b02014-11-19 18:44:19 -05001451 main.log.info(handle)
andrewonlabb66dfa12014-12-02 15:51:10 -05001452 #Split result by newline
1453 newline = handle.split("\r\r\n")
1454 #Ignore the first object of list, which is empty
1455 newline = newline[1:]
1456 #Some sloppy parsing method to get the latency
1457 for result in newline:
1458 result = result.split(": ")
1459 #Append the first result of second parse
1460 lat_result.append(result[1].split(" ")[0])
1461
andrewonlab042b3912014-12-10 16:40:50 -05001462 main.log.info(lat_result)
andrewonlabb66dfa12014-12-02 15:51:10 -05001463 return lat_result
andrewonlab87852b02014-11-19 18:44:19 -05001464 else:
1465 return main.TRUE
1466
1467 except pexpect.EOF:
1468 main.log.error(self.name + ": EOF exception found")
1469 main.log.error(self.name + ": " + self.handle.before)
1470 main.cleanup()
1471 main.exit()
1472 except:
1473 main.log.info(self.name+" ::::::")
1474 main.log.error( traceback.print_exc())
1475 main.log.info(self.name+" ::::::")
1476 main.cleanup()
1477 main.exit()
1478
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001479 def intents_events_metrics(self, json_format=True):
1480 '''
1481 Description:Returns topology metrics
1482 Optional:
1483 * json_format: enable json formatting of output
1484 '''
1485 try:
1486 if json_format:
1487 self.handle.sendline("intents-events-metrics -j")
1488 self.handle.expect("intents-events-metrics -j")
1489 self.handle.expect("onos>")
1490
1491 handle = self.handle.before
1492
1493 #Some color thing that we want to escape
1494 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1495 handle = ansi_escape.sub('', handle)
1496
1497 else:
1498 self.handle.sendline("intents-events-metrics")
1499 self.handle.expect("intents-events-metrics")
1500 self.handle.expect("onos>")
1501
1502 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001503
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001504 return handle
1505
1506 except pexpect.EOF:
1507 main.log.error(self.name + ": EOF exception found")
1508 main.log.error(self.name + ": " + self.handle.before)
1509 main.cleanup()
1510 main.exit()
1511 except:
1512 main.log.info(self.name+" ::::::")
1513 main.log.error( traceback.print_exc())
1514 main.log.info(self.name+" ::::::")
1515 main.cleanup()
1516 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001517
andrewonlab867212a2014-10-22 20:13:38 -04001518 def topology_events_metrics(self, json_format=True):
1519 '''
1520 Description:Returns topology metrics
1521 Optional:
1522 * json_format: enable json formatting of output
1523 '''
1524 try:
1525 if json_format:
1526 self.handle.sendline("topology-events-metrics -j")
1527 self.handle.expect("topology-events-metrics -j")
1528 self.handle.expect("onos>")
1529
1530 handle = self.handle.before
1531
1532 #Some color thing that we want to escape
1533 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1534 handle = ansi_escape.sub('', handle)
1535
1536 else:
1537 self.handle.sendline("topology-events-metrics")
1538 self.handle.expect("topology-events-metrics")
1539 self.handle.expect("onos>")
1540
1541 handle = self.handle.before
1542
1543 return handle
1544
1545 except pexpect.EOF:
1546 main.log.error(self.name + ": EOF exception found")
1547 main.log.error(self.name + ": " + self.handle.before)
1548 main.cleanup()
1549 main.exit()
1550 except:
1551 main.log.info(self.name+" ::::::")
1552 main.log.error( traceback.print_exc())
1553 main.log.info(self.name+" ::::::")
1554 main.cleanup()
1555 main.exit()
1556
andrewonlab3e15ead2014-10-15 14:21:34 -04001557 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001558 #Wrapper functions use existing driver
1559 #functions and extends their use case.
1560 #For example, we may use the output of
1561 #a normal driver function, and parse it
1562 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001563
andrewonlab9a50dfe2014-10-17 17:22:31 -04001564 def get_all_intents_id(self):
1565 '''
1566 Description:
1567 Obtain all intent id's in a list
1568 '''
1569 try:
1570 #Obtain output of intents function
1571 intents_str = self.intents()
1572 all_intent_list = []
1573 intent_id_list = []
1574
1575 #Parse the intents output for ID's
1576 intents_list = [s.strip() for s in intents_str.splitlines()]
1577 for intents in intents_list:
1578 if "onos>" in intents:
1579 continue
1580 elif "intents" in intents:
1581 continue
1582 else:
1583 line_list = intents.split(" ")
1584 all_intent_list.append(line_list[0])
1585
1586 all_intent_list = all_intent_list[1:-2]
1587
1588 for intents in all_intent_list:
1589 if not intents:
1590 continue
1591 else:
1592 intent_id_list.append(intents)
1593
1594 return intent_id_list
1595
1596 except pexpect.EOF:
1597 main.log.error(self.name + ": EOF exception found")
1598 main.log.error(self.name + ": " + self.handle.before)
1599 main.cleanup()
1600 main.exit()
1601 except:
1602 main.log.info(self.name+" ::::::")
1603 main.log.error( traceback.print_exc())
1604 main.log.info(self.name+" ::::::")
1605 main.cleanup()
1606 main.exit()
1607
andrewonlab7e4d2d32014-10-15 13:23:21 -04001608 def get_all_devices_id(self):
1609 '''
1610 Use 'devices' function to obtain list of all devices
1611 and parse the result to obtain a list of all device
1612 id's. Returns this list. Returns empty list if no
1613 devices exist
1614 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001615
1616 This function may be useful if you are not sure of the
1617 device id, and wish to execute other commands using
1618 the ids. By obtaining the list of device ids on the fly,
1619 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001620 '''
1621 try:
1622 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001623 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001624 id_list = []
1625
1626 if not devices_str:
1627 main.log.info("There are no devices to get id from")
1628 return id_list
1629
1630 #Split the string into list by comma
1631 device_list = devices_str.split(",")
1632 #Get temporary list of all arguments with string 'id='
1633 temp_list = [dev for dev in device_list if "id=" in dev]
1634 #Split list further into arguments before and after string
1635 # 'id='. Get the latter portion (the actual device id) and
1636 # append to id_list
1637 for arg in temp_list:
1638 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001639 return id_list
1640
1641 except pexpect.EOF:
1642 main.log.error(self.name + ": EOF exception found")
1643 main.log.error(self.name + ": " + self.handle.before)
1644 main.cleanup()
1645 main.exit()
1646 except:
1647 main.log.info(self.name+" ::::::")
1648 main.log.error( traceback.print_exc())
1649 main.log.info(self.name+" ::::::")
1650 main.cleanup()
1651 main.exit()
1652
andrewonlab7c211572014-10-15 16:45:20 -04001653 def get_all_nodes_id(self):
1654 '''
1655 Uses 'nodes' function to obtain list of all nodes
1656 and parse the result of nodes to obtain just the
1657 node id's.
1658 Returns:
1659 list of node id's
1660 '''
1661 try:
1662 nodes_str = self.nodes()
1663 id_list = []
1664
1665 if not nodes_str:
1666 main.log.info("There are no nodes to get id from")
1667 return id_list
1668
1669 #Sample nodes_str output
1670 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1671
1672 #Split the string into list by comma
1673 nodes_list = nodes_str.split(",")
1674 temp_list = [node for node in nodes_list if "id=" in node]
1675 for arg in temp_list:
1676 id_list.append(arg.split("id=")[1])
1677
1678 return id_list
1679
1680 except pexpect.EOF:
1681 main.log.error(self.name + ": EOF exception found")
1682 main.log.error(self.name + ": " + self.handle.before)
1683 main.cleanup()
1684 main.exit()
1685 except:
1686 main.log.info(self.name+" ::::::")
1687 main.log.error( traceback.print_exc())
1688 main.log.info(self.name+" ::::::")
1689 main.cleanup()
1690 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001691
Jon Halla91c4dc2014-10-22 12:57:04 -04001692 def get_device(self, dpid=None):
1693 '''
1694 Return the first device from the devices api whose 'id' contains 'dpid'
1695 Return None if there is no match
1696 '''
1697 import json
1698 try:
1699 if dpid == None:
1700 return None
1701 else:
1702 dpid = dpid.replace(':', '')
1703 raw_devices = self.devices()
1704 devices_json = json.loads(raw_devices)
1705 #search json for the device with dpid then return the device
1706 for device in devices_json:
1707 #print "%s in %s?" % (dpid, device['id'])
1708 if dpid in device['id']:
1709 return device
1710 return None
1711 except pexpect.EOF:
1712 main.log.error(self.name + ": EOF exception found")
1713 main.log.error(self.name + ": " + self.handle.before)
1714 main.cleanup()
1715 main.exit()
1716 except:
1717 main.log.info(self.name+" ::::::")
1718 main.log.error( traceback.print_exc())
1719 main.log.info(self.name+" ::::::")
1720 main.cleanup()
1721 main.exit()
1722
Jon Hall42db6dc2014-10-24 19:03:48 -04001723 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1724 '''
1725 Checks the number of swithes & links that ONOS sees against the
1726 supplied values. By default this will report to main.log, but the
1727 log level can be specifid.
1728
1729 Params: ip = ip used for the onos cli
1730 numoswitch = expected number of switches
1731 numlink = expected number of links
1732 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1733
1734
1735 log_level can
1736
1737 Returns: main.TRUE if the number of switchs and links are correct,
1738 main.FALSE if the numer of switches and links is incorrect,
1739 and main.ERROR otherwise
1740 '''
1741
1742 try:
1743 topology = self.get_topology(ip)
1744 if topology == {}:
1745 return main.ERROR
1746 output = ""
1747 #Is the number of switches is what we expected
1748 devices = topology.get('devices',False)
1749 links = topology.get('links',False)
1750 if devices == False or links == False:
1751 return main.ERROR
1752 switch_check = ( int(devices) == int(numoswitch) )
1753 #Is the number of links is what we expected
1754 link_check = ( int(links) == int(numolink) )
1755 if (switch_check and link_check):
1756 #We expected the correct numbers
1757 output = output + "The number of links and switches match "\
1758 + "what was expected"
1759 result = main.TRUE
1760 else:
1761 output = output + \
1762 "The number of links and switches does not match what was expected"
1763 result = main.FALSE
1764 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1765 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1766 if log_level == "report":
1767 main.log.report(output)
1768 elif log_level == "warn":
1769 main.log.warn(output)
1770 else:
1771 main.log.info(output)
1772 return result
1773 except pexpect.EOF:
1774 main.log.error(self.name + ": EOF exception found")
1775 main.log.error(self.name + ": " + self.handle.before)
1776 main.cleanup()
1777 main.exit()
1778 except:
1779 main.log.info(self.name+" ::::::")
1780 main.log.error( traceback.print_exc())
1781 main.log.info(self.name+" ::::::")
1782 main.cleanup()
1783 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001784
1785 def device_role(self, device_id, onos_node, role="master"):
1786 '''
1787 Calls the device-role cli command.
1788 device_id must be the id of a device as seen in the onos devices command
1789 onos_node is the ip of one of the onos nodes in the cluster
1790 role must be either master, standby, or none
1791
Jon Hall94fd0472014-12-08 11:52:42 -08001792 Returns main.TRUE or main.FALSE based on argument verification.
Jon Hall983a1702014-10-28 18:44:22 -04001793 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001794 support that output
1795 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001796 try:
Jon Hall983a1702014-10-28 18:44:22 -04001797 #print "beginning device_role... \n\tdevice_id:" + device_id
1798 #print "\tonos_node: " + onos_node
1799 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001800 if role.lower() == "master" or \
1801 role.lower() == "standby" or \
1802 role.lower() == "none":
1803 self.handle.sendline("")
1804 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001805 self.handle.sendline("device-role " +
1806 str(device_id) + " " +
1807 str(onos_node) + " " +
1808 str(role))
1809 i= self.handle.expect(["Error","onos>"])
1810 if i == 0:
1811 output = str(self.handle.before)
1812 self.handle.expect("onos>")
1813 output = output + str(self.handle.before)
1814 main.log.error(self.name + ": " +
1815 output + '\033[0m')#end color output to escape any colours from the cli
1816 return main.ERROR
1817 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001818 self.handle.expect("onos>")
1819 return main.TRUE
1820 else:
1821 return main.FALSE
1822
1823 except pexpect.EOF:
1824 main.log.error(self.name + ": EOF exception found")
1825 main.log.error(self.name + ": " + self.handle.before)
1826 main.cleanup()
1827 main.exit()
1828 except:
1829 main.log.info(self.name+" ::::::")
1830 main.log.error( traceback.print_exc())
1831 main.log.info(self.name+" ::::::")
1832 main.cleanup()
1833 main.exit()
1834
Jon Hall73cf9cc2014-11-20 22:28:38 -08001835 def clusters(self, json_format=True):
1836 '''
1837 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001838 Optional argument:
1839 * json_format - boolean indicating if you want output in json
Jon Hall73cf9cc2014-11-20 22:28:38 -08001840 '''
1841 try:
1842 self.handle.sendline("")
1843 self.handle.expect("onos>")
1844
1845 if json_format:
1846 self.handle.sendline("clusters -j")
1847 self.handle.expect("clusters -j")
1848 self.handle.expect("onos>")
1849 handle = self.handle.before
1850 '''
1851 handle variable here contains some ANSI escape color code
1852 sequences at the end which are invisible in the print command
1853 output. To make that escape sequence visible, use repr() function.
1854 The repr(handle) output when printed shows the ANSI escape sequences.
1855 In json.loads(somestring), this somestring variable is actually
1856 repr(somestring) and json.loads would fail with the escape sequence.
1857 So we take off that escape sequence using
1858 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1859 handle1 = ansi_escape.sub('', handle)
1860 '''
1861 #print "repr(handle) =", repr(handle)
1862 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1863 handle1 = ansi_escape.sub('', handle)
1864 #print "repr(handle1) = ", repr(handle1)
1865 return handle1
1866 else:
Jon Hallffb386d2014-11-21 13:43:38 -08001867 self.handle.sendline("clusters")
Jon Hall73cf9cc2014-11-20 22:28:38 -08001868 self.handle.expect("onos>")
1869 handle = self.handle.before
1870 #print "handle =",handle
1871 return handle
1872 except pexpect.EOF:
1873 main.log.error(self.name + ": EOF exception found")
1874 main.log.error(self.name + ": " + self.handle.before)
1875 main.cleanup()
1876 main.exit()
1877 except:
1878 main.log.info(self.name+" ::::::")
1879 main.log.error( traceback.print_exc())
1880 main.log.info(self.name+" ::::::")
1881 main.cleanup()
1882 main.exit()
1883
Jon Hall94fd0472014-12-08 11:52:42 -08001884 def election_test_leader(self):
1885 '''
1886 * CLI command to get the current leader for the Election test application.
1887 #NOTE: Requires installation of the onos-app-election feature
1888 Returns: Node IP of the leader if one exists
1889 None if none exists
1890 Main.FALSE on error
1891 '''
1892 try:
1893 self.handle.sendline("election-test-leader")
1894 self.handle.expect("election-test-leader")
1895 self.handle.expect("onos>")
1896 response = self.handle.before
1897 #Leader
1898 node_search = re.search("The\scurrent\sleader\sfor\sthe\sElection\sapp\sis\s(?P<node>.+)\.", response)
1899 if node_search:
1900 node = node_search.group('node')
Jon Hall669173b2014-12-17 11:36:30 -08001901 main.log.info("Election-test-leader on "+str(self.name)+" found " + node + " as the leader")
Jon Hall94fd0472014-12-08 11:52:42 -08001902 return node
1903 #no leader
1904 null_search = re.search("There\sis\scurrently\sno\sleader\selected\sfor\sthe\sElection\sapp", response)
1905 if null_search:
Jon Hall669173b2014-12-17 11:36:30 -08001906 main.log.info("Election-test-leader found no leader on " + self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001907 return None
1908 #error
Jon Hall669173b2014-12-17 11:36:30 -08001909 if re.search("Command\snot\sfound", response):
1910 main.log.error("Election app is not loaded on " + self.name)
1911 return main.FALSE
1912 else:
1913 main.log.error("Error in election_test_leader: unexpected response")
1914 main.log.error( repr(response) )
1915 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001916
1917 except pexpect.EOF:
1918 main.log.error(self.name + ": EOF exception found")
1919 main.log.error(self.name + ": " + self.handle.before)
1920 main.cleanup()
1921 main.exit()
1922 except:
1923 main.log.info(self.name+" ::::::")
1924 main.log.error( traceback.print_exc())
1925 main.log.info(self.name+" ::::::")
1926 main.cleanup()
1927 main.exit()
1928
1929 def election_test_run(self):
1930 '''
1931 * CLI command to run for leadership of the Election test application.
1932 #NOTE: Requires installation of the onos-app-election feature
1933 Returns: Main.TRUE on success
1934 Main.FALSE on error
1935 '''
1936 try:
1937 self.handle.sendline("election-test-run")
1938 self.handle.expect("election-test-run")
1939 self.handle.expect("onos>")
1940 response = self.handle.before
1941 #success
1942 search = re.search("Entering\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1943 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001944 main.log.info(self.name + " entering leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001945 return main.TRUE
1946 #error
Jon Hall669173b2014-12-17 11:36:30 -08001947 if re.search("Command\snot\sfound", response):
1948 main.log.error("Election app is not loaded on " + self.name)
1949 return main.FALSE
1950 else:
1951 main.log.error("Error in election_test_run: unexpected response")
1952 main.log.error( repr(response) )
1953 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001954
1955 except pexpect.EOF:
1956 main.log.error(self.name + ": EOF exception found")
1957 main.log.error(self.name + ": " + self.handle.before)
1958 main.cleanup()
1959 main.exit()
1960 except:
1961 main.log.info(self.name+" ::::::")
1962 main.log.error( traceback.print_exc())
1963 main.log.info(self.name+" ::::::")
1964 main.cleanup()
1965 main.exit()
1966
1967 def election_test_withdraw(self):
1968 '''
1969 * CLI command to withdraw the local node from leadership election for
1970 * the Election test application.
1971 #NOTE: Requires installation of the onos-app-election feature
1972 Returns: Main.TRUE on success
1973 Main.FALSE on error
1974 '''
1975 try:
1976 self.handle.sendline("election-test-withdraw")
1977 self.handle.expect("election-test-withdraw")
1978 self.handle.expect("onos>")
1979 response = self.handle.before
1980 #success
1981 search = re.search("Withdrawing\sfrom\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1982 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001983 main.log.info(self.name + " withdrawing from leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001984 return main.TRUE
1985 #error
Jon Hall669173b2014-12-17 11:36:30 -08001986 if re.search("Command\snot\sfound", response):
1987 main.log.error("Election app is not loaded on " + self.name)
1988 return main.FALSE
1989 else:
1990 main.log.error("Error in election_test_withdraw: unexpected response")
1991 main.log.error( repr(response) )
1992 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001993
1994
1995 except pexpect.EOF:
1996 main.log.error(self.name + ": EOF exception found")
1997 main.log.error(self.name + ": " + self.handle.before)
1998 main.cleanup()
1999 main.exit()
2000 except:
2001 main.log.info(self.name+" ::::::")
2002 main.log.error( traceback.print_exc())
2003 main.log.info(self.name+" ::::::")
2004 main.cleanup()
2005 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002006
andrewonlab7e4d2d32014-10-15 13:23:21 -04002007 #***********************************
Hari Krishna416c3ca2014-12-19 15:39:31 -08002008 def getDevicePortsEnabledCount(self,dpid):
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002009 '''
2010 Get the count of all enabled ports on a particular device/switch
2011 '''
2012 try:
Hari Krishna2bbaa702014-12-19 14:46:12 -08002013 dpid = str(dpid)
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002014 self.handle.sendline("")
2015 self.handle.expect("onos>")
2016
2017 self.handle.sendline("onos:ports -e "+dpid+" | wc -l")
2018 i = self.handle.expect([
2019 "No such device",
2020 "onos>"])
2021
2022 #self.handle.sendline("")
2023 #self.handle.expect("onos>")
2024
2025 output = self.handle.before
2026
2027 if i == 0:
2028 main.log.error("Error in getting ports")
2029 return (ouput, "Error")
2030 else:
2031 result = output
2032 return result
2033
2034 except pexpect.EOF:
2035 main.log.error(self.name + ": EOF exception found")
2036 main.log.error(self.name + ": " + self.handle.before)
2037 main.cleanup()
2038 main.exit()
2039 except:
2040 main.log.info(self.name+" ::::::")
2041 main.log.error( traceback.print_exc())
2042 main.log.info(self.name+" ::::::")
2043 main.cleanup()
2044 main.exit()
2045
2046 def getDeviceLinksActiveCount(self,dpid):
2047 '''
2048 Get the count of all enabled ports on a particular device/switch
2049 '''
2050 try:
Hari Krishna2bbaa702014-12-19 14:46:12 -08002051 dpid = str(dpid)
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002052 self.handle.sendline("")
2053 self.handle.expect("onos>")
2054
2055 self.handle.sendline("onos:links "+dpid+" | grep ACTIVE | wc -l")
2056 i = self.handle.expect([
2057 "No such device",
2058 "onos>"])
2059
2060 output = self.handle.before
2061
2062 if i == 0:
2063 main.log.error("Error in getting ports")
2064 return (ouput, "Error")
2065 else:
2066 result = output
2067 return result
2068
2069 except pexpect.EOF:
2070 main.log.error(self.name + ": EOF exception found")
2071 main.log.error(self.name + ": " + self.handle.before)
2072 main.cleanup()
2073 main.exit()
2074 except:
2075 main.log.info(self.name+" ::::::")
2076 main.log.error( traceback.print_exc())
2077 main.log.info(self.name+" ::::::")
2078 main.cleanup()
2079 main.exit()
2080
2081 def getAllIntentIds(self):
2082 '''
2083 Return a list of all Intent IDs
2084 '''
2085 try:
2086 self.handle.sendline("")
2087 self.handle.expect("onos>")
2088
2089 self.handle.sendline("onos:intents | grep id=")
2090 i = self.handle.expect([
2091 "Error",
2092 "onos>"])
2093
2094 output = self.handle.before
2095
2096 if i == 0:
2097 main.log.error("Error in getting ports")
2098 return (ouput, "Error")
2099 else:
2100 result = output
2101 return result
2102
2103 except pexpect.EOF:
2104 main.log.error(self.name + ": EOF exception found")
2105 main.log.error(self.name + ": " + self.handle.before)
2106 main.cleanup()
2107 main.exit()
2108 except:
2109 main.log.info(self.name+" ::::::")
2110 main.log.error( traceback.print_exc())
2111 main.log.info(self.name+" ::::::")
2112 main.cleanup()
2113 main.exit()