blob: c987f7005c926586431916f536655104926ee5d6 [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 Krishnad7b9c202015-01-05 10:38:14 -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.
Hari Krishna25d42f72015-01-05 15:08:28 -0800180 Below is an example to start a session with 60 seconds idle timeout (input value is in milliseconds):
181
182 tValue = "60000"
183 main.ONOScli1.start_onos_cli(ONOS_ip, karafTimeout=tValue)
184
185 Note: karafTimeout is left as str so that this could be read and passed to start_onos_cli from PARAMS file as str.
Hari Krishnad7b9c202015-01-05 10:38:14 -0800186 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400187 try:
188 self.handle.sendline("")
andrewonlab48829f62014-11-17 13:49:01 -0500189 x = self.handle.expect([
190 "\$", "onos>"], timeout=10)
191
192 if x == 1:
193 main.log.info("ONOS cli is already running")
194 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400195
196 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400197 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400198 i = self.handle.expect([
199 "onos>",
200 pexpect.TIMEOUT],timeout=60)
201
202 if i == 0:
203 main.log.info(str(ONOS_ip)+" CLI Started successfully")
Hari Krishnae36ef212015-01-04 14:09:13 -0800204 if karafTimeout:
205 self.handle.sendline("config:property-set -p org.apache.karaf.shell sshIdleTimeout "+karafTimeout)
206 self.handle.expect("\$")
207 self.handle.sendline("onos -w "+str(ONOS_ip))
208 self.handle.expect("onos>")
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400209 return main.TRUE
210 else:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400211 #If failed, send ctrl+c to process and try again
andrewonlabf47993a2014-10-24 17:56:01 -0400212 main.log.info("Starting CLI failed. Retrying...")
Jon Hallffb386d2014-11-21 13:43:38 -0800213 self.handle.send("\x03")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400214 self.handle.sendline("onos -w "+str(ONOS_ip))
215 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
216 timeout=30)
217 if i == 0:
andrewonlab28ca4b22014-11-20 13:15:59 -0500218 main.log.info(str(ONOS_ip)+" CLI Started "+
219 "successfully after retry attempt")
Hari Krishnae36ef212015-01-04 14:09:13 -0800220 if karafTimeout:
221 self.handle.sendline("config:property-set -p org.apache.karaf.shell sshIdleTimeout "+karafTimeout)
222 self.handle.expect("\$")
223 self.handle.sendline("onos -w "+str(ONOS_ip))
224 self.handle.expect("onos>")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400225 return main.TRUE
226 else:
227 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400228 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400229 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400230
231 except pexpect.EOF:
232 main.log.error(self.name + ": EOF exception found")
233 main.log.error(self.name + ": " + self.handle.before)
234 main.cleanup()
235 main.exit()
236 except:
237 main.log.info(self.name+" ::::::")
238 main.log.error( traceback.print_exc())
239 main.log.info(self.name+" ::::::")
240 main.cleanup()
241 main.exit()
242
andrewonlaba18f6bf2014-10-13 19:31:54 -0400243 def sendline(self, cmd_str):
244 '''
245 Send a completely user specified string to
246 the onos> prompt. Use this function if you have
247 a very specific command to send.
248
249 Warning: There are no sanity checking to commands
250 sent using this method.
251 '''
252 try:
253 self.handle.sendline("")
254 self.handle.expect("onos>")
255
256 self.handle.sendline(cmd_str)
257 self.handle.expect("onos>")
258
259 handle = self.handle.before
260
261 self.handle.sendline("")
262 self.handle.expect("onos>")
263
264 handle += self.handle.before
265 handle += self.handle.after
266
267 main.log.info("Command sent.")
Jon Hall42db6dc2014-10-24 19:03:48 -0400268 ansi_escape = re.compile(r'\x1b[^m]*m')
269 handle = ansi_escape.sub('', handle)
andrewonlaba18f6bf2014-10-13 19:31:54 -0400270
271 return handle
272 except pexpect.EOF:
273 main.log.error(self.name + ": EOF exception found")
274 main.log.error(self.name + ": " + self.handle.before)
275 main.cleanup()
276 main.exit()
277 except:
278 main.log.info(self.name+" ::::::")
279 main.log.error( traceback.print_exc())
280 main.log.info(self.name+" ::::::")
281 main.cleanup()
282 main.exit()
283
andrewonlab95ce8322014-10-13 14:12:04 -0400284 #IMPORTANT NOTE:
285 #For all cli commands, naming convention should match
286 #the cli command replacing ':' with '_'.
287 #Ex) onos:topology > onos_topology
288 # onos:links > onos_links
289 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400290
291 def add_node(self, node_id, ONOS_ip, tcp_port=""):
292 '''
293 Adds a new cluster node by ID and address information.
294 Required:
295 * node_id
296 * ONOS_ip
297 Optional:
298 * tcp_port
299 '''
300 try:
301 self.handle.sendline("")
302 self.handle.expect("onos>")
303
304 self.handle.sendline("add-node "+
305 str(node_id)+" "+
306 str(ONOS_ip)+" "+
307 str(tcp_port))
308
309 i = self.handle.expect([
310 "Error",
311 "onos>" ])
312
313 #Clear handle to get previous output
314 self.handle.sendline("")
315 self.handle.expect("onos>")
316
317 handle = self.handle.before
318
319 if i == 0:
320 main.log.error("Error in adding node")
321 main.log.error(handle)
322 return main.FALSE
323 else:
324 main.log.info("Node "+str(ONOS_ip)+" added")
325 return main.TRUE
326
327 except pexpect.EOF:
328 main.log.error(self.name + ": EOF exception found")
329 main.log.error(self.name + ": " + self.handle.before)
330 main.cleanup()
331 main.exit()
332 except:
333 main.log.info(self.name+" ::::::")
334 main.log.error( traceback.print_exc())
335 main.log.info(self.name+" ::::::")
336 main.cleanup()
337 main.exit()
338
andrewonlab86dc3082014-10-13 18:18:38 -0400339 def remove_node(self, node_id):
340 '''
341 Removes a cluster by ID
342 Issues command: 'remove-node [<node-id>]'
343 Required:
344 * node_id
345 '''
346 try:
347 self.handle.sendline("")
348 self.handle.expect("onos>")
349
350 self.handle.sendline("remove-node "+str(node_id))
351 self.handle.expect("onos>")
352
353 return main.TRUE
354
355 except pexpect.EOF:
356 main.log.error(self.name + ": EOF exception found")
357 main.log.error(self.name + ": " + self.handle.before)
358 main.cleanup()
359 main.exit()
360 except:
361 main.log.info(self.name+" ::::::")
362 main.log.error( traceback.print_exc())
363 main.log.info(self.name+" ::::::")
364 main.cleanup()
365 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400366
andrewonlab7c211572014-10-15 16:45:20 -0400367 def nodes(self):
368 '''
369 List the nodes currently visible
370 Issues command: 'nodes'
371 Returns: entire handle of list of nodes
372 '''
373 try:
374 self.handle.sendline("")
375 self.handle.expect("onos>")
376
377 self.handle.sendline("nodes")
378 self.handle.expect("onos>")
379
380 self.handle.sendline("")
381 self.handle.expect("onos>")
382
383 handle = self.handle.before
384
385 return handle
386
387 except pexpect.EOF:
388 main.log.error(self.name + ": EOF exception found")
389 main.log.error(self.name + ": " + self.handle.before)
390 main.cleanup()
391 main.exit()
392 except:
393 main.log.info(self.name+" ::::::")
394 main.log.error( traceback.print_exc())
395 main.log.info(self.name+" ::::::")
396 main.cleanup()
397 main.exit()
398
andrewonlab38d6ae22014-10-15 14:23:45 -0400399 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400400 '''
401 Shows the current state of the topology
402 by issusing command: 'onos> onos:topology'
403 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400404 try:
405 self.handle.sendline("")
406 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400407 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400408 self.handle.sendline("onos:topology")
409 self.handle.expect("onos>")
410
411 handle = self.handle.before
412
413 main.log.info("onos:topology returned: " +
414 str(handle))
415
416 return handle
417
418 except pexpect.EOF:
419 main.log.error(self.name + ": EOF exception found")
420 main.log.error(self.name + ": " + self.handle.before)
421 main.cleanup()
422 main.exit()
423 except:
424 main.log.info(self.name+" ::::::")
425 main.log.error( traceback.print_exc())
426 main.log.info(self.name+" ::::::")
427 main.cleanup()
428 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400429
430 def feature_install(self, feature_str):
431 '''
432 Installs a specified feature
433 by issuing command: 'onos> feature:install <feature_str>'
434 '''
435 try:
436 self.handle.sendline("")
437 self.handle.expect("onos>")
438
439 self.handle.sendline("feature:install "+str(feature_str))
440 self.handle.expect("onos>")
441
442 return main.TRUE
443
444 except pexpect.EOF:
445 main.log.error(self.name + ": EOF exception found")
446 main.log.error(self.name + ": " + self.handle.before)
andrewonlabbf225b02014-11-12 12:14:05 -0500447 main.log.report("Failed to install feature")
448 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400449 main.cleanup()
450 main.exit()
451 except:
452 main.log.info(self.name+" ::::::")
453 main.log.error( traceback.print_exc())
andrewonlabbf225b02014-11-12 12:14:05 -0500454 main.log.report("Failed to install feature")
455 main.log.report("Exiting test")
andrewonlabc2d05aa2014-10-13 16:51:10 -0400456 main.log.info(self.name+" ::::::")
457 main.cleanup()
458 main.exit()
459
460 def feature_uninstall(self, feature_str):
461 '''
462 Uninstalls a specified feature
463 by issuing command: 'onos> feature:uninstall <feature_str>'
464 '''
465 try:
466 self.handle.sendline("")
467 self.handle.expect("onos>")
468
469 self.handle.sendline("feature:uninstall "+str(feature_str))
470 self.handle.expect("onos>")
471
472 return main.TRUE
473
474 except pexpect.EOF:
475 main.log.error(self.name + ": EOF exception found")
476 main.log.error(self.name + ": " + self.handle.before)
477 main.cleanup()
478 main.exit()
479 except:
480 main.log.info(self.name+" ::::::")
481 main.log.error( traceback.print_exc())
482 main.log.info(self.name+" ::::::")
483 main.cleanup()
484 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800485
486 def devices(self, json_format=True):
andrewonlab86dc3082014-10-13 18:18:38 -0400487 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400488 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400489 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800490 * json_format - boolean indicating if you want output in json
andrewonlab86dc3082014-10-13 18:18:38 -0400491 '''
492 try:
493 self.handle.sendline("")
494 self.handle.expect("onos>")
andrewonlabb66dfa12014-12-02 15:51:10 -0500495
Jon Halle8217482014-10-17 13:49:14 -0400496 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800497 self.handle.sendline("devices -j")
498 self.handle.expect("devices -j")
499 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400500 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400501 '''
502 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
503 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 -0400504 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 -0400505 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400506 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hall983a1702014-10-28 18:44:22 -0400507 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400508 '''
509 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400510 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
511 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400512 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400513 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400514 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800515 self.handle.sendline("devices")
516 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400517 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400518 #print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400519 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400520 except pexpect.EOF:
521 main.log.error(self.name + ": EOF exception found")
522 main.log.error(self.name + ": " + self.handle.before)
523 main.cleanup()
524 main.exit()
525 except:
526 main.log.info(self.name+" ::::::")
527 main.log.error( traceback.print_exc())
528 main.log.info(self.name+" ::::::")
529 main.cleanup()
530 main.exit()
531
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800532
533 def balance_masters(self):
534 '''
535 This balances the devices across all controllers
536 by issuing command: 'onos> onos:balance-masters'
537 If required this could be extended to return devices balanced output.
538 '''
539 try:
540 self.handle.sendline("")
541 self.handle.expect("onos>")
542
543 self.handle.sendline("onos:balance-masters")
544 self.handle.expect("onos>")
545 return main.TRUE
546
547 except pexpect.EOF:
548 main.log.error(self.name + ": EOF exception found")
549 main.log.error(self.name + ": " + self.handle.before)
550 main.cleanup()
551 main.exit()
552 except:
553 main.log.info(self.name+" ::::::")
554 main.log.error( traceback.print_exc())
555 main.log.info(self.name+" ::::::")
556 main.cleanup()
557 main.exit()
558
Hari Krishna2bbaa702014-12-19 14:46:12 -0800559 def links(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400560 '''
561 Lists all core links
562 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800563 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400564 '''
565 try:
566 self.handle.sendline("")
567 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800568
Jon Halle8217482014-10-17 13:49:14 -0400569 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800570 self.handle.sendline("links -j")
571 self.handle.expect("links -j")
572 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400573 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400574 '''
575 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
576 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 -0400577 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 -0800578 So we take off that escape sequence using
Jon Halld815ce42014-10-17 19:52:30 -0400579 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800580 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400581 '''
582 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400583 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
584 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400585 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400586 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400587 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800588 self.handle.sendline("links")
589 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400590 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400591 #print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400592 return handle
Jon Halle8217482014-10-17 13:49:14 -0400593 except pexpect.EOF:
594 main.log.error(self.name + ": EOF exception found")
595 main.log.error(self.name + ": " + self.handle.before)
596 main.cleanup()
597 main.exit()
598 except:
599 main.log.info(self.name+" ::::::")
600 main.log.error( traceback.print_exc())
601 main.log.info(self.name+" ::::::")
602 main.cleanup()
603 main.exit()
604
605
Jon Hallffb386d2014-11-21 13:43:38 -0800606 def ports(self, json_format=True):
Jon Halle8217482014-10-17 13:49:14 -0400607 '''
608 Lists all ports
609 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800610 * json_format - boolean indicating if you want output in json
Jon Halle8217482014-10-17 13:49:14 -0400611 '''
612 try:
613 self.handle.sendline("")
614 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800615
Jon Halle8217482014-10-17 13:49:14 -0400616 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800617 self.handle.sendline("ports -j")
618 self.handle.expect("ports -j")
619 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400620 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400621 '''
622 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
623 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 -0400624 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 -0800625 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400626 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800627 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400628 '''
629 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400630 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
631 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400632 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400633 return handle1
634
Jon Halle8217482014-10-17 13:49:14 -0400635 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800636 self.handle.sendline("ports")
637 self.handle.expect("onos>")
638 self.handle.sendline("")
639 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400640 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400641 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800642 return handle
Jon Halle8217482014-10-17 13:49:14 -0400643 except pexpect.EOF:
644 main.log.error(self.name + ": EOF exception found")
645 main.log.error(self.name + ": " + self.handle.before)
646 main.cleanup()
647 main.exit()
648 except:
649 main.log.info(self.name+" ::::::")
650 main.log.error( traceback.print_exc())
651 main.log.info(self.name+" ::::::")
652 main.cleanup()
653 main.exit()
654
655
Jon Hallffb386d2014-11-21 13:43:38 -0800656 def roles(self, json_format=True):
andrewonlab7c211572014-10-15 16:45:20 -0400657 '''
Jon Hall983a1702014-10-28 18:44:22 -0400658 Lists all devices and the controllers with roles assigned to them
659 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800660 * json_format - boolean indicating if you want output in json
andrewonlab7c211572014-10-15 16:45:20 -0400661 '''
662 try:
663 self.handle.sendline("")
664 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500665
Jon Hall983a1702014-10-28 18:44:22 -0400666 if json_format:
Jon Hallb1290e82014-11-18 16:17:48 -0500667 self.handle.sendline("roles -j")
668 self.handle.expect("roles -j")
669 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400670 handle = self.handle.before
671 '''
Jon Hallb1290e82014-11-18 16:17:48 -0500672 handle variable here contains some ANSI escape color code sequences at the
673 end which are invisible in the print command output. To make that escape
674 sequence visible, use repr() function. The repr(handle) output when printed
675 shows the ANSI escape sequences. In json.loads(somestring), this somestring
676 variable is actually repr(somestring) and json.loads would fail with the escape
677 sequence.
678
679 So we take off that escape sequence using the following commads:
Jon Hall983a1702014-10-28 18:44:22 -0400680 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallb1290e82014-11-18 16:17:48 -0500681 handle1 = ansi_escape.sub('', handle)
Jon Hall983a1702014-10-28 18:44:22 -0400682 '''
683 #print "repr(handle) =", repr(handle)
684 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
685 handle1 = ansi_escape.sub('', handle)
686 #print "repr(handle1) = ", repr(handle1)
687 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400688
andrewonlab7c211572014-10-15 16:45:20 -0400689 else:
Jon Hallb1290e82014-11-18 16:17:48 -0500690 self.handle.sendline("roles")
691 self.handle.expect("onos>")
692 self.handle.sendline("")
693 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -0400694 handle = self.handle.before
695 #print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800696 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400697 except pexpect.EOF:
698 main.log.error(self.name + ": EOF exception found")
699 main.log.error(self.name + ": " + self.handle.before)
700 main.cleanup()
701 main.exit()
702 except:
703 main.log.info(self.name+" ::::::")
704 main.log.error( traceback.print_exc())
705 main.log.info(self.name+" ::::::")
706 main.cleanup()
707 main.exit()
708
709 def get_role(self, device_id):
710 '''
711 Given the a string containing the json representation of the "roles" cli command and a
712 partial or whole device id, returns a json object containing the
713 roles output for the first device whose id contains "device_id"
714
715 Returns:
716 Dict of the role assignments for the given device or
717 None if not match
718 '''
719 try:
720 import json
721 if device_id == None:
722 return None
723 else:
724 raw_roles = self.roles()
725 roles_json = json.loads(raw_roles)
726 #search json for the device with id then return the device
727 for device in roles_json:
Jon Hall720653a2014-10-28 19:02:37 -0400728 #print device
Jon Hall983a1702014-10-28 18:44:22 -0400729 if str(device_id) in device['id']:
730 return device
731 return None
andrewonlab7c211572014-10-15 16:45:20 -0400732
andrewonlab86dc3082014-10-13 18:18:38 -0400733 except pexpect.EOF:
734 main.log.error(self.name + ": EOF exception found")
735 main.log.error(self.name + ": " + self.handle.before)
736 main.cleanup()
737 main.exit()
738 except:
739 main.log.info(self.name+" ::::::")
740 main.log.error( traceback.print_exc())
741 main.log.info(self.name+" ::::::")
742 main.cleanup()
743 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800744
745 def roles_not_null(self):
746 '''
747 Iterates through each device and checks if there is a master assigned
748 Returns: main.TRUE if each device has a master
749 main.FALSE any device has no master
750 '''
751 try:
752 import json
753 raw_roles = self.roles()
754 roles_json = json.loads(raw_roles)
755 #search json for the device with id then return the device
756 for device in roles_json:
757 #print device
758 if device['master'] == "none":
759 main.log.warn("Device has no master: " + str(device) )
760 return main.FALSE
761 return main.TRUE
762
763 except pexpect.EOF:
764 main.log.error(self.name + ": EOF exception found")
765 main.log.error(self.name + ": " + self.handle.before)
766 main.cleanup()
767 main.exit()
768 except:
769 main.log.info(self.name+" ::::::")
770 main.log.error( traceback.print_exc())
771 main.log.info(self.name+" ::::::")
772 main.cleanup()
773 main.exit()
774
775
andrewonlab3e15ead2014-10-15 14:21:34 -0400776 def paths(self, src_id, dst_id):
777 '''
778 Returns string of paths, and the cost.
779 Issues command: onos:paths <src> <dst>
780 '''
781 try:
782 self.handle.sendline("")
783 self.handle.expect("onos>")
784
785 self.handle.sendline("onos:paths "+
786 str(src_id) + " " + str(dst_id))
787 i = self.handle.expect([
788 "Error",
789 "onos>"])
790
791 self.handle.sendline("")
792 self.handle.expect("onos>")
793
794 handle = self.handle.before
795
796 if i == 0:
797 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400798 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400799 else:
800 path = handle.split(";")[0]
801 cost = handle.split(";")[1]
802 return (path, cost)
803
804 except pexpect.EOF:
805 main.log.error(self.name + ": EOF exception found")
806 main.log.error(self.name + ": " + self.handle.before)
807 main.cleanup()
808 main.exit()
809 except:
810 main.log.info(self.name+" ::::::")
811 main.log.error( traceback.print_exc())
812 main.log.info(self.name+" ::::::")
813 main.cleanup()
814 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800815
816 def hosts(self, json_format=True):
Jon Hall42db6dc2014-10-24 19:03:48 -0400817 '''
Jon Hallffb386d2014-11-21 13:43:38 -0800818 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400819 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800820 * json_format - boolean indicating if you want output in json
Jon Hall42db6dc2014-10-24 19:03:48 -0400821 '''
822 try:
823 self.handle.sendline("")
824 self.handle.expect("onos>")
Jon Hallffb386d2014-11-21 13:43:38 -0800825
Jon Hall42db6dc2014-10-24 19:03:48 -0400826 if json_format:
Jon Hallffb386d2014-11-21 13:43:38 -0800827 self.handle.sendline("hosts -j")
828 self.handle.expect("hosts -j")
829 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400830 handle = self.handle.before
831 '''
832 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
833 To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences.
834 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 -0800835 So we take off that escape sequence using
Jon Hall42db6dc2014-10-24 19:03:48 -0400836 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
Jon Hallffb386d2014-11-21 13:43:38 -0800837 handle1 = ansi_escape.sub('', handle)
Jon Hall42db6dc2014-10-24 19:03:48 -0400838 '''
839 #print "repr(handle) =", repr(handle)
840 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
841 handle1 = ansi_escape.sub('', handle)
842 #print "repr(handle1) = ", repr(handle1)
843 return handle1
844 else:
Jon Hallffb386d2014-11-21 13:43:38 -0800845 self.handle.sendline("hosts")
846 self.handle.expect("onos>")
Jon Hall42db6dc2014-10-24 19:03:48 -0400847 handle = self.handle.before
Jon Hall983a1702014-10-28 18:44:22 -0400848 #print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400849 return handle
850 except pexpect.EOF:
851 main.log.error(self.name + ": EOF exception found")
852 main.log.error(self.name + ": " + self.handle.before)
853 main.cleanup()
854 main.exit()
855 except:
856 main.log.info(self.name+" ::::::")
857 main.log.error( traceback.print_exc())
858 main.log.info(self.name+" ::::::")
859 main.cleanup()
860 main.exit()
861
862 def get_host(self, mac):
863 '''
864 Return the first host from the hosts api whose 'id' contains 'mac'
865 Note: mac must be a colon seperated mac address, but could be a partial mac address
866 Return None if there is no match
867 '''
868 import json
869 try:
870 if mac == None:
871 return None
872 else:
873 mac = mac
874 raw_hosts = self.hosts()
875 hosts_json = json.loads(raw_hosts)
876 #search json for the host with mac then return the device
877 for host in hosts_json:
Jon Hall983a1702014-10-28 18:44:22 -0400878 #print "%s in %s?" % (mac, host['id'])
Jon Hall42db6dc2014-10-24 19:03:48 -0400879 if mac in host['id']:
880 return host
881 return None
882 except pexpect.EOF:
883 main.log.error(self.name + ": EOF exception found")
884 main.log.error(self.name + ": " + self.handle.before)
885 main.cleanup()
886 main.exit()
887 except:
888 main.log.info(self.name+" ::::::")
889 main.log.error( traceback.print_exc())
890 main.log.info(self.name+" ::::::")
891 main.cleanup()
892 main.exit()
893
andrewonlab3f0a4af2014-10-17 12:25:14 -0400894
895 def get_hosts_id(self, host_list):
896 '''
897 Obtain list of hosts
898 Issues command: 'onos> hosts'
899
900 Required:
901 * host_list: List of hosts obtained by Mininet
902 IMPORTANT:
903 This function assumes that you started your
904 topology with the option '--mac'.
905 Furthermore, it assumes that value of VLAN is '-1'
906 Description:
907 Converts mininet hosts (h1, h2, h3...) into
908 ONOS format (00:00:00:00:00:01/-1 , ...)
909 '''
910
911 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400912 onos_host_list = []
913
914 for host in host_list:
915 host = host.replace("h", "")
916 host_hex = hex(int(host)).zfill(12)
917 host_hex = str(host_hex).replace('x','0')
918 i = iter(str(host_hex))
919 host_hex = ":".join(a+b for a,b in zip(i,i))
920 host_hex = host_hex + "/-1"
921 onos_host_list.append(host_hex)
922
923 return onos_host_list
924
925 except pexpect.EOF:
926 main.log.error(self.name + ": EOF exception found")
927 main.log.error(self.name + ": " + self.handle.before)
928 main.cleanup()
929 main.exit()
930 except:
931 main.log.info(self.name+" ::::::")
932 main.log.error( traceback.print_exc())
933 main.log.info(self.name+" ::::::")
934 main.cleanup()
935 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400936
andrewonlabe6745342014-10-17 14:29:13 -0400937 def add_host_intent(self, host_id_one, host_id_two):
938 '''
939 Required:
940 * host_id_one: ONOS host id for host1
941 * host_id_two: ONOS host id for host2
942 Description:
943 Adds a host-to-host intent (bidrectional) by
Jon Hallb1290e82014-11-18 16:17:48 -0500944 specifying the two hosts.
andrewonlabe6745342014-10-17 14:29:13 -0400945 '''
946 try:
947 self.handle.sendline("")
948 self.handle.expect("onos>")
Jon Hallb1290e82014-11-18 16:17:48 -0500949
andrewonlabe6745342014-10-17 14:29:13 -0400950 self.handle.sendline("add-host-intent "+
951 str(host_id_one) + " " + str(host_id_two))
952 self.handle.expect("onos>")
953
andrewonlabe6745342014-10-17 14:29:13 -0400954 handle = self.handle.before
Jon Hallffb386d2014-11-21 13:43:38 -0800955 #print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400956
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800957 main.log.info("Host intent installed between "+
andrewonlabe6745342014-10-17 14:29:13 -0400958 str(host_id_one) + " and " + str(host_id_two))
959
960 return handle
Jon Hallb1290e82014-11-18 16:17:48 -0500961
andrewonlabe6745342014-10-17 14:29:13 -0400962 except pexpect.EOF:
963 main.log.error(self.name + ": EOF exception found")
964 main.log.error(self.name + ": " + self.handle.before)
965 main.cleanup()
966 main.exit()
967 except:
968 main.log.info(self.name+" ::::::")
969 main.log.error( traceback.print_exc())
970 main.log.info(self.name+" ::::::")
971 main.cleanup()
972 main.exit()
973
andrewonlab7b31d232014-10-24 13:31:47 -0400974 def add_optical_intent(self, ingress_device, egress_device):
975 '''
976 Required:
977 * ingress_device: device id of ingress device
978 * egress_device: device id of egress device
979 Optional:
980 TODO: Still needs to be implemented via dev side
981 '''
982 try:
983 self.handle.sendline("add-optical-intent "+
984 str(ingress_device) + " " + str(egress_device))
985 self.handle.expect("add-optical-intent")
986 i = self.handle.expect([
987 "Error",
988 "onos>"])
989
990 handle = self.handle.before
991
992 #If error, return error message
993 if i == 0:
994 return handle
995 else:
996 return main.TRUE
997
998 except pexpect.EOF:
999 main.log.error(self.name + ": EOF exception found")
1000 main.log.error(self.name + ": " + self.handle.before)
1001 main.cleanup()
1002 main.exit()
1003 except:
1004 main.log.info(self.name+" ::::::")
1005 main.log.error( traceback.print_exc())
1006 main.log.info(self.name+" ::::::")
1007 main.cleanup()
1008 main.exit()
1009
andrewonlab36af3822014-11-18 17:48:18 -05001010 def add_point_intent(self, ingress_device, egress_device,
1011 port_ingress="", port_egress="", ethType="", ethSrc="",
andrewonlabfa4ff502014-11-11 16:41:30 -05001012 ethDst="", bandwidth="", lambda_alloc=False,
1013 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -04001014 '''
1015 Required:
1016 * ingress_device: device id of ingress device
1017 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001018 Optional:
1019 * ethType: specify ethType
1020 * ethSrc: specify ethSrc (i.e. src mac addr)
1021 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001022 * bandwidth: specify bandwidth capacity of link
andrewonlab40ccd8b2014-11-06 16:23:34 -05001023 * lambda_alloc: if True, intent will allocate lambda
1024 for the specified intent
andrewonlabf77e0cb2014-11-11 17:17:59 -05001025 * ipProto: specify ip protocol
1026 * ipSrc: specify ip source address
1027 * ipDst: specify ip destination address
1028 * tcpSrc: specify tcp source port
1029 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001030 Description:
1031 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -04001032 specifying device id's and optional fields
1033
andrewonlab4dbb4d82014-10-17 18:22:31 -04001034 NOTE: This function may change depending on the
1035 options developers provide for point-to-point
1036 intent via cli
1037 '''
1038 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001039 cmd = ""
1040
1041 #If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001042 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001043 and not bandwidth and not lambda_alloc \
1044 and not ipProto and not ipSrc and not ipDst \
1045 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001046 cmd = "add-point-intent"
1047
1048
andrewonlab289e4b72014-10-21 21:24:18 -04001049 else:
andrewonlab36af3822014-11-18 17:48:18 -05001050 cmd = "add-point-intent"
andrewonlab9a130be2014-10-22 12:44:56 -04001051
andrewonlab0c0a6772014-10-22 12:31:18 -04001052 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -04001053 cmd += " --ethType " + str(ethType)
1054 if ethSrc:
1055 cmd += " --ethSrc " + str(ethSrc)
1056 if ethDst:
1057 cmd += " --ethDst " + str(ethDst)
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001058 if bandwidth:
1059 cmd += " --bandwidth " + str(bandwidth)
1060 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001061 cmd += " --lambda "
1062 if ipProto:
1063 cmd += " --ipProto " + str(ipProto)
1064 if ipSrc:
1065 cmd += " --ipSrc " + str(ipSrc)
1066 if ipDst:
1067 cmd += " --ipDst " + str(ipDst)
1068 if tcpSrc:
1069 cmd += " --tcpSrc " + str(tcpSrc)
1070 if tcpDst:
1071 cmd += " --tcpDst " + str(tcpDst)
andrewonlab289e4b72014-10-21 21:24:18 -04001072
andrewonlab36af3822014-11-18 17:48:18 -05001073 #Check whether the user appended the port
1074 #or provided it as an input
1075 if "/" in ingress_device:
1076 cmd += " "+str(ingress_device)
1077 else:
1078 if not port_ingress:
1079 main.log.error("You must specify "+
1080 "the ingress port")
1081 #TODO: perhaps more meaningful return
1082 return main.FALSE
1083
1084 cmd += " "+ \
1085 str(ingress_device) + "/" +\
1086 str(port_ingress) + " "
1087
1088 if "/" in egress_device:
1089 cmd += " "+str(egress_device)
1090 else:
1091 if not port_egress:
1092 main.log.error("You must specify "+
1093 "the egress port")
1094 return main.FALSE
1095
1096 cmd += " "+\
1097 str(egress_device) + "/" +\
1098 str(port_egress)
andrewonlab4dbb4d82014-10-17 18:22:31 -04001099
andrewonlab289e4b72014-10-21 21:24:18 -04001100 self.handle.sendline(cmd)
andrewonlab36af3822014-11-18 17:48:18 -05001101
1102 main.log.info(cmd + " sent")
andrewonlab4dbb4d82014-10-17 18:22:31 -04001103 i = self.handle.expect([
1104 "Error",
1105 "onos>"])
andrewonlab4dbb4d82014-10-17 18:22:31 -04001106
1107 if i == 0:
1108 main.log.error("Error in adding point-to-point intent")
1109 return handle
1110 else:
1111 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001112
andrewonlab4dbb4d82014-10-17 18:22:31 -04001113 except pexpect.EOF:
1114 main.log.error(self.name + ": EOF exception found")
1115 main.log.error(self.name + ": " + self.handle.before)
1116 main.cleanup()
1117 main.exit()
1118 except:
1119 main.log.info(self.name+" ::::::")
1120 main.log.error( traceback.print_exc())
1121 main.log.info(self.name+" ::::::")
1122 main.cleanup()
1123 main.exit()
1124
shahshreyad0c80432014-12-04 16:56:05 -08001125
1126 def add_multipoint_to_singlepoint_intent(self, ingress_device1, ingress_device2,egress_device,
1127 port_ingress="", port_egress="", ethType="", ethSrc="",
1128 ethDst="", bandwidth="", lambda_alloc=False,
1129 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="", setEthDst=""):
1130 '''
1131 Note:
1132 This function assumes that there would be 2 ingress devices and one egress device
1133 For more number of ingress devices, this function needs to be modified
1134 Required:
1135 * ingress_device1: device id of ingress device1
1136 * ingress_device2: device id of ingress device2
1137 * egress_device: device id of egress device
1138 Optional:
1139 * ethType: specify ethType
1140 * ethSrc: specify ethSrc (i.e. src mac addr)
1141 * ethDst: specify ethDst (i.e. dst mac addr)
1142 * bandwidth: specify bandwidth capacity of link
1143 * lambda_alloc: if True, intent will allocate lambda
1144 for the specified intent
1145 * ipProto: specify ip protocol
1146 * ipSrc: specify ip source address
1147 * ipDst: specify ip destination address
1148 * tcpSrc: specify tcp source port
1149 * tcpDst: specify tcp destination port
1150 * setEthSrc: action to Rewrite Source MAC Address
1151 * setEthDst: action to Rewrite Destination MAC Address
1152 Description:
1153 Adds a multipoint-to-singlepoint intent (uni-directional) by
1154 specifying device id's and optional fields
1155
1156 NOTE: This function may change depending on the
1157 options developers provide for multipointpoint-to-singlepoint
1158 intent via cli
1159 '''
1160 try:
1161 cmd = ""
1162
1163 #If there are no optional arguments
1164 if not ethType and not ethSrc and not ethDst\
1165 and not bandwidth and not lambda_alloc \
1166 and not ipProto and not ipSrc and not ipDst \
1167 and not tcpSrc and not tcpDst and not setEthSrc and not setEthDst:
1168 cmd = "add-multi-to-single-intent"
1169
1170
1171 else:
1172 cmd = "add-multi-to-single-intent"
1173
1174 if ethType:
1175 cmd += " --ethType " + str(ethType)
1176 if ethSrc:
1177 cmd += " --ethSrc " + str(ethSrc)
1178 if ethDst:
1179 cmd += " --ethDst " + str(ethDst)
1180 if bandwidth:
1181 cmd += " --bandwidth " + str(bandwidth)
1182 if lambda_alloc:
1183 cmd += " --lambda "
1184 if ipProto:
1185 cmd += " --ipProto " + str(ipProto)
1186 if ipSrc:
1187 cmd += " --ipSrc " + str(ipSrc)
1188 if ipDst:
1189 cmd += " --ipDst " + str(ipDst)
1190 if tcpSrc:
1191 cmd += " --tcpSrc " + str(tcpSrc)
1192 if tcpDst:
1193 cmd += " --tcpDst " + str(tcpDst)
1194 if setEthSrc:
1195 cmd += " --setEthSrc "+ str(setEthSrc)
1196 if setEthDst:
1197 cmd += " --setEthDst "+ str(setEthDst)
1198
1199 #Check whether the user appended the port
1200 #or provided it as an input
1201 if "/" in ingress_device1:
1202 cmd += " "+str(ingress_device1)
1203 else:
1204 if not port_ingress1:
1205 main.log.error("You must specify "+
1206 "the ingress port1")
1207 #TODO: perhaps more meaningful return
1208 return main.FALSE
1209
1210 cmd += " "+ \
1211 str(ingress_device1) + "/" +\
1212 str(port_ingress1) + " "
1213
1214 if "/" in ingress_device2:
1215 cmd += " "+str(ingress_device2)
1216 else:
1217 if not port_ingress2:
1218 main.log.error("You must specify "+
1219 "the ingress port2")
1220 #TODO: perhaps more meaningful return
1221 return main.FALSE
1222
1223 cmd += " "+ \
1224 str(ingress_device2) + "/" +\
1225 str(port_ingress2) + " "
1226
1227 if "/" in egress_device:
1228 cmd += " "+str(egress_device)
1229 else:
1230 if not port_egress:
1231 main.log.error("You must specify "+
1232 "the egress port")
1233 return main.FALSE
1234
1235 cmd += " "+\
1236 str(egress_device) + "/" +\
1237 str(port_egress)
1238 print "cmd= ",cmd
1239 self.handle.sendline(cmd)
1240
1241 main.log.info(cmd + " sent")
1242 i = self.handle.expect([
1243 "Error",
1244 "onos>"])
1245
1246 if i == 0:
1247 main.log.error("Error in adding point-to-point intent")
1248 return self.handle
1249 else:
1250 return main.TRUE
1251
1252 except pexpect.EOF:
1253 main.log.error(self.name + ": EOF exception found")
1254 main.log.error(self.name + ": " + self.handle.before)
1255 main.cleanup()
1256 main.exit()
1257 except:
1258 main.log.info(self.name+" ::::::")
1259 main.log.error( traceback.print_exc())
1260 main.log.info(self.name+" ::::::")
1261 main.cleanup()
1262 main.exit()
1263
1264
andrewonlab9a50dfe2014-10-17 17:22:31 -04001265 def remove_intent(self, intent_id):
1266 '''
1267 Remove intent for specified intent id
1268 '''
1269 try:
1270 self.handle.sendline("")
1271 self.handle.expect("onos>")
1272
1273 self.handle.sendline("remove-intent "+str(intent_id))
1274 i = self.handle.expect([
1275 "Error",
1276 "onos>"])
1277
1278 handle = self.handle.before
1279
1280 if i == 0:
1281 main.log.error("Error in removing intent")
1282 return handle
1283 else:
1284 return handle
1285
1286 except pexpect.EOF:
1287 main.log.error(self.name + ": EOF exception found")
1288 main.log.error(self.name + ": " + self.handle.before)
1289 main.cleanup()
1290 main.exit()
1291 except:
1292 main.log.info(self.name+" ::::::")
1293 main.log.error( traceback.print_exc())
1294 main.log.info(self.name+" ::::::")
1295 main.cleanup()
1296 main.exit()
1297
pingping-lindabe7972014-11-17 19:29:44 -08001298 # This method should be used after installing application: onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001299 def routes(self, json_format=False):
1300 '''
1301 Optional:
1302 * json_format: enable output formatting in json
1303 Description:
1304 Obtain all routes in the system
1305 '''
1306 try:
1307 if json_format:
1308 self.handle.sendline("routes -j")
1309 self.handle.expect("routes -j")
1310 self.handle.expect("onos>")
1311 handle_tmp = self.handle.before
1312
1313 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1314 handle = ansi_escape.sub('', handle_tmp)
1315
1316 else:
1317 self.handle.sendline("")
1318 self.handle.expect("onos>")
1319
1320 self.handle.sendline("routes")
1321 self.handle.expect("onos>")
1322 handle = self.handle.before
1323
1324 return handle
1325
1326 except pexpect.EOF:
1327 main.log.error(self.name + ": EOF exception found")
1328 main.log.error(self.name + ": " + self.handle.before)
1329 main.cleanup()
1330 main.exit()
1331 except:
1332 main.log.info(self.name + " ::::::")
1333 main.log.error(traceback.print_exc())
1334 main.log.info(self.name + " ::::::")
1335 main.cleanup()
1336 main.exit()
1337
Jon Hallffb386d2014-11-21 13:43:38 -08001338 def intents(self, json_format = True):
andrewonlabe6745342014-10-17 14:29:13 -04001339 '''
andrewonlab377693f2014-10-21 16:00:30 -04001340 Optional:
1341 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001342 Description:
1343 Obtain intents currently installed
1344 '''
1345 try:
andrewonlab377693f2014-10-21 16:00:30 -04001346 if json_format:
1347 self.handle.sendline("intents -j")
1348 self.handle.expect("intents -j")
1349 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001350 handle = self.handle.before
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001351
1352 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1353 handle = ansi_escape.sub('', handle)
andrewonlab377693f2014-10-21 16:00:30 -04001354 else:
1355 self.handle.sendline("")
1356 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -04001357
andrewonlab377693f2014-10-21 16:00:30 -04001358 self.handle.sendline("intents")
1359 self.handle.expect("onos>")
andrewonlab377693f2014-10-21 16:00:30 -04001360 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001361
1362 return handle
1363
1364 except pexpect.EOF:
1365 main.log.error(self.name + ": EOF exception found")
1366 main.log.error(self.name + ": " + self.handle.before)
1367 main.cleanup()
1368 main.exit()
1369 except:
1370 main.log.info(self.name+" ::::::")
1371 main.log.error( traceback.print_exc())
1372 main.log.info(self.name+" ::::::")
1373 main.cleanup()
1374 main.exit()
1375
Jon Hallffb386d2014-11-21 13:43:38 -08001376 def flows(self, json_format = True):
Shreya Shah0f01c812014-10-26 20:15:28 -04001377 '''
1378 Optional:
1379 * json_format: enable output formatting in json
1380 Description:
1381 Obtain flows currently installed
1382 '''
1383 try:
1384 if json_format:
1385 self.handle.sendline("flows -j")
1386 self.handle.expect("flows -j")
1387 self.handle.expect("onos>")
1388 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001389 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1390 handle = ansi_escape.sub('', handle)
Shreya Shah0f01c812014-10-26 20:15:28 -04001391
1392 else:
1393 self.handle.sendline("")
1394 self.handle.expect("onos>")
1395 self.handle.sendline("flows")
1396 self.handle.expect("onos>")
1397 handle = self.handle.before
Jon Hallb1290e82014-11-18 16:17:48 -05001398 if re.search("Error\sexecuting\scommand:", handle):
1399 main.log.error(self.name + ".flows() response: " + str(handle))
Shreya Shah0f01c812014-10-26 20:15:28 -04001400
1401 return handle
1402
1403 except pexpect.EOF:
1404 main.log.error(self.name + ": EOF exception found")
1405 main.log.error(self.name + ": " + self.handle.before)
1406 main.cleanup()
1407 main.exit()
1408 except:
1409 main.log.info(self.name+" ::::::")
1410 main.log.error( traceback.print_exc())
1411 main.log.info(self.name+" ::::::")
1412 main.cleanup()
1413 main.exit()
1414
andrewonlabb66dfa12014-12-02 15:51:10 -05001415 def push_test_intents(self, dpid_src, dpid_dst, num_intents,
1416 num_mult="", app_id="", report=True):
andrewonlab87852b02014-11-19 18:44:19 -05001417 '''
1418 Description:
1419 Push a number of intents in a batch format to
1420 a specific point-to-point intent definition
1421 Required:
1422 * dpid_src: specify source dpid
1423 * dpid_dst: specify destination dpid
1424 * num_intents: specify number of intents to push
1425 Optional:
andrewonlabb66dfa12014-12-02 15:51:10 -05001426 * num_mult: number multiplier for multiplying
1427 the number of intents specified
1428 * app_id: specify the application id init to further
1429 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001430 * report: default True, returns latency information
1431 '''
1432 try:
1433 cmd = "push-test-intents "+\
1434 str(dpid_src)+" "+str(dpid_dst)+" "+\
1435 str(num_intents)
andrewonlabb66dfa12014-12-02 15:51:10 -05001436
1437 if num_mult:
1438 cmd += " " + str(num_mult)
andrewonlab042b3912014-12-10 16:40:50 -05001439 #If app id is specified, then num_mult
1440 #must exist because of the way this command
1441 #takes in arguments
andrewonlabb66dfa12014-12-02 15:51:10 -05001442 if app_id:
1443 cmd += " " + str(app_id)
1444
andrewonlab87852b02014-11-19 18:44:19 -05001445 self.handle.sendline(cmd)
1446 self.handle.expect(cmd)
1447 self.handle.expect("onos>")
1448
1449 handle = self.handle.before
1450
1451 #Some color thing that we want to escape
1452 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1453 handle = ansi_escape.sub('', handle)
1454
1455 if report:
andrewonlabb66dfa12014-12-02 15:51:10 -05001456 lat_result = []
andrewonlab87852b02014-11-19 18:44:19 -05001457 main.log.info(handle)
andrewonlabb66dfa12014-12-02 15:51:10 -05001458 #Split result by newline
1459 newline = handle.split("\r\r\n")
1460 #Ignore the first object of list, which is empty
1461 newline = newline[1:]
1462 #Some sloppy parsing method to get the latency
1463 for result in newline:
1464 result = result.split(": ")
1465 #Append the first result of second parse
1466 lat_result.append(result[1].split(" ")[0])
1467
andrewonlab042b3912014-12-10 16:40:50 -05001468 main.log.info(lat_result)
andrewonlabb66dfa12014-12-02 15:51:10 -05001469 return lat_result
andrewonlab87852b02014-11-19 18:44:19 -05001470 else:
1471 return main.TRUE
1472
1473 except pexpect.EOF:
1474 main.log.error(self.name + ": EOF exception found")
1475 main.log.error(self.name + ": " + self.handle.before)
1476 main.cleanup()
1477 main.exit()
1478 except:
1479 main.log.info(self.name+" ::::::")
1480 main.log.error( traceback.print_exc())
1481 main.log.info(self.name+" ::::::")
1482 main.cleanup()
1483 main.exit()
1484
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001485 def intents_events_metrics(self, json_format=True):
1486 '''
1487 Description:Returns topology metrics
1488 Optional:
1489 * json_format: enable json formatting of output
1490 '''
1491 try:
1492 if json_format:
1493 self.handle.sendline("intents-events-metrics -j")
1494 self.handle.expect("intents-events-metrics -j")
1495 self.handle.expect("onos>")
1496
1497 handle = self.handle.before
1498
1499 #Some color thing that we want to escape
1500 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1501 handle = ansi_escape.sub('', handle)
1502
1503 else:
1504 self.handle.sendline("intents-events-metrics")
1505 self.handle.expect("intents-events-metrics")
1506 self.handle.expect("onos>")
1507
1508 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001509
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001510 return handle
1511
1512 except pexpect.EOF:
1513 main.log.error(self.name + ": EOF exception found")
1514 main.log.error(self.name + ": " + self.handle.before)
1515 main.cleanup()
1516 main.exit()
1517 except:
1518 main.log.info(self.name+" ::::::")
1519 main.log.error( traceback.print_exc())
1520 main.log.info(self.name+" ::::::")
1521 main.cleanup()
1522 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001523
andrewonlab867212a2014-10-22 20:13:38 -04001524 def topology_events_metrics(self, json_format=True):
1525 '''
1526 Description:Returns topology metrics
1527 Optional:
1528 * json_format: enable json formatting of output
1529 '''
1530 try:
1531 if json_format:
1532 self.handle.sendline("topology-events-metrics -j")
1533 self.handle.expect("topology-events-metrics -j")
1534 self.handle.expect("onos>")
1535
1536 handle = self.handle.before
1537
1538 #Some color thing that we want to escape
1539 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1540 handle = ansi_escape.sub('', handle)
1541
1542 else:
1543 self.handle.sendline("topology-events-metrics")
1544 self.handle.expect("topology-events-metrics")
1545 self.handle.expect("onos>")
1546
1547 handle = self.handle.before
1548
1549 return handle
1550
1551 except pexpect.EOF:
1552 main.log.error(self.name + ": EOF exception found")
1553 main.log.error(self.name + ": " + self.handle.before)
1554 main.cleanup()
1555 main.exit()
1556 except:
1557 main.log.info(self.name+" ::::::")
1558 main.log.error( traceback.print_exc())
1559 main.log.info(self.name+" ::::::")
1560 main.cleanup()
1561 main.exit()
1562
andrewonlab3e15ead2014-10-15 14:21:34 -04001563 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001564 #Wrapper functions use existing driver
1565 #functions and extends their use case.
1566 #For example, we may use the output of
1567 #a normal driver function, and parse it
1568 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001569
andrewonlab9a50dfe2014-10-17 17:22:31 -04001570 def get_all_intents_id(self):
1571 '''
1572 Description:
1573 Obtain all intent id's in a list
1574 '''
1575 try:
1576 #Obtain output of intents function
1577 intents_str = self.intents()
1578 all_intent_list = []
1579 intent_id_list = []
1580
1581 #Parse the intents output for ID's
1582 intents_list = [s.strip() for s in intents_str.splitlines()]
1583 for intents in intents_list:
1584 if "onos>" in intents:
1585 continue
1586 elif "intents" in intents:
1587 continue
1588 else:
1589 line_list = intents.split(" ")
1590 all_intent_list.append(line_list[0])
1591
1592 all_intent_list = all_intent_list[1:-2]
1593
1594 for intents in all_intent_list:
1595 if not intents:
1596 continue
1597 else:
1598 intent_id_list.append(intents)
1599
1600 return intent_id_list
1601
1602 except pexpect.EOF:
1603 main.log.error(self.name + ": EOF exception found")
1604 main.log.error(self.name + ": " + self.handle.before)
1605 main.cleanup()
1606 main.exit()
1607 except:
1608 main.log.info(self.name+" ::::::")
1609 main.log.error( traceback.print_exc())
1610 main.log.info(self.name+" ::::::")
1611 main.cleanup()
1612 main.exit()
1613
andrewonlab7e4d2d32014-10-15 13:23:21 -04001614 def get_all_devices_id(self):
1615 '''
1616 Use 'devices' function to obtain list of all devices
1617 and parse the result to obtain a list of all device
1618 id's. Returns this list. Returns empty list if no
1619 devices exist
1620 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001621
1622 This function may be useful if you are not sure of the
1623 device id, and wish to execute other commands using
1624 the ids. By obtaining the list of device ids on the fly,
1625 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001626 '''
1627 try:
1628 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001629 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001630 id_list = []
1631
1632 if not devices_str:
1633 main.log.info("There are no devices to get id from")
1634 return id_list
1635
1636 #Split the string into list by comma
1637 device_list = devices_str.split(",")
1638 #Get temporary list of all arguments with string 'id='
1639 temp_list = [dev for dev in device_list if "id=" in dev]
1640 #Split list further into arguments before and after string
1641 # 'id='. Get the latter portion (the actual device id) and
1642 # append to id_list
1643 for arg in temp_list:
1644 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001645 return id_list
1646
1647 except pexpect.EOF:
1648 main.log.error(self.name + ": EOF exception found")
1649 main.log.error(self.name + ": " + self.handle.before)
1650 main.cleanup()
1651 main.exit()
1652 except:
1653 main.log.info(self.name+" ::::::")
1654 main.log.error( traceback.print_exc())
1655 main.log.info(self.name+" ::::::")
1656 main.cleanup()
1657 main.exit()
1658
andrewonlab7c211572014-10-15 16:45:20 -04001659 def get_all_nodes_id(self):
1660 '''
1661 Uses 'nodes' function to obtain list of all nodes
1662 and parse the result of nodes to obtain just the
1663 node id's.
1664 Returns:
1665 list of node id's
1666 '''
1667 try:
1668 nodes_str = self.nodes()
1669 id_list = []
1670
1671 if not nodes_str:
1672 main.log.info("There are no nodes to get id from")
1673 return id_list
1674
1675 #Sample nodes_str output
1676 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1677
1678 #Split the string into list by comma
1679 nodes_list = nodes_str.split(",")
1680 temp_list = [node for node in nodes_list if "id=" in node]
1681 for arg in temp_list:
1682 id_list.append(arg.split("id=")[1])
1683
1684 return id_list
1685
1686 except pexpect.EOF:
1687 main.log.error(self.name + ": EOF exception found")
1688 main.log.error(self.name + ": " + self.handle.before)
1689 main.cleanup()
1690 main.exit()
1691 except:
1692 main.log.info(self.name+" ::::::")
1693 main.log.error( traceback.print_exc())
1694 main.log.info(self.name+" ::::::")
1695 main.cleanup()
1696 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001697
Jon Halla91c4dc2014-10-22 12:57:04 -04001698 def get_device(self, dpid=None):
1699 '''
1700 Return the first device from the devices api whose 'id' contains 'dpid'
1701 Return None if there is no match
1702 '''
1703 import json
1704 try:
1705 if dpid == None:
1706 return None
1707 else:
1708 dpid = dpid.replace(':', '')
1709 raw_devices = self.devices()
1710 devices_json = json.loads(raw_devices)
1711 #search json for the device with dpid then return the device
1712 for device in devices_json:
1713 #print "%s in %s?" % (dpid, device['id'])
1714 if dpid in device['id']:
1715 return device
1716 return None
1717 except pexpect.EOF:
1718 main.log.error(self.name + ": EOF exception found")
1719 main.log.error(self.name + ": " + self.handle.before)
1720 main.cleanup()
1721 main.exit()
1722 except:
1723 main.log.info(self.name+" ::::::")
1724 main.log.error( traceback.print_exc())
1725 main.log.info(self.name+" ::::::")
1726 main.cleanup()
1727 main.exit()
1728
Jon Hall42db6dc2014-10-24 19:03:48 -04001729 def check_status(self, ip, numoswitch, numolink, log_level="info"):
1730 '''
1731 Checks the number of swithes & links that ONOS sees against the
1732 supplied values. By default this will report to main.log, but the
1733 log level can be specifid.
1734
1735 Params: ip = ip used for the onos cli
1736 numoswitch = expected number of switches
1737 numlink = expected number of links
1738 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1739
1740
1741 log_level can
1742
1743 Returns: main.TRUE if the number of switchs and links are correct,
1744 main.FALSE if the numer of switches and links is incorrect,
1745 and main.ERROR otherwise
1746 '''
1747
1748 try:
1749 topology = self.get_topology(ip)
1750 if topology == {}:
1751 return main.ERROR
1752 output = ""
1753 #Is the number of switches is what we expected
1754 devices = topology.get('devices',False)
1755 links = topology.get('links',False)
1756 if devices == False or links == False:
1757 return main.ERROR
1758 switch_check = ( int(devices) == int(numoswitch) )
1759 #Is the number of links is what we expected
1760 link_check = ( int(links) == int(numolink) )
1761 if (switch_check and link_check):
1762 #We expected the correct numbers
1763 output = output + "The number of links and switches match "\
1764 + "what was expected"
1765 result = main.TRUE
1766 else:
1767 output = output + \
1768 "The number of links and switches does not match what was expected"
1769 result = main.FALSE
1770 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1771 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1772 if log_level == "report":
1773 main.log.report(output)
1774 elif log_level == "warn":
1775 main.log.warn(output)
1776 else:
1777 main.log.info(output)
1778 return result
1779 except pexpect.EOF:
1780 main.log.error(self.name + ": EOF exception found")
1781 main.log.error(self.name + ": " + self.handle.before)
1782 main.cleanup()
1783 main.exit()
1784 except:
1785 main.log.info(self.name+" ::::::")
1786 main.log.error( traceback.print_exc())
1787 main.log.info(self.name+" ::::::")
1788 main.cleanup()
1789 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001790
1791 def device_role(self, device_id, onos_node, role="master"):
1792 '''
1793 Calls the device-role cli command.
1794 device_id must be the id of a device as seen in the onos devices command
1795 onos_node is the ip of one of the onos nodes in the cluster
1796 role must be either master, standby, or none
1797
Jon Hall94fd0472014-12-08 11:52:42 -08001798 Returns main.TRUE or main.FALSE based on argument verification.
Jon Hall983a1702014-10-28 18:44:22 -04001799 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001800 support that output
1801 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001802 try:
Jon Hall983a1702014-10-28 18:44:22 -04001803 #print "beginning device_role... \n\tdevice_id:" + device_id
1804 #print "\tonos_node: " + onos_node
1805 #print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001806 if role.lower() == "master" or \
1807 role.lower() == "standby" or \
1808 role.lower() == "none":
1809 self.handle.sendline("")
1810 self.handle.expect("onos>")
Jon Hall983a1702014-10-28 18:44:22 -04001811 self.handle.sendline("device-role " +
1812 str(device_id) + " " +
1813 str(onos_node) + " " +
1814 str(role))
1815 i= self.handle.expect(["Error","onos>"])
1816 if i == 0:
1817 output = str(self.handle.before)
1818 self.handle.expect("onos>")
1819 output = output + str(self.handle.before)
1820 main.log.error(self.name + ": " +
1821 output + '\033[0m')#end color output to escape any colours from the cli
1822 return main.ERROR
1823 self.handle.sendline("")
Jon Hall1c9e8732014-10-27 19:29:27 -04001824 self.handle.expect("onos>")
1825 return main.TRUE
1826 else:
1827 return main.FALSE
1828
1829 except pexpect.EOF:
1830 main.log.error(self.name + ": EOF exception found")
1831 main.log.error(self.name + ": " + self.handle.before)
1832 main.cleanup()
1833 main.exit()
1834 except:
1835 main.log.info(self.name+" ::::::")
1836 main.log.error( traceback.print_exc())
1837 main.log.info(self.name+" ::::::")
1838 main.cleanup()
1839 main.exit()
1840
Jon Hall73cf9cc2014-11-20 22:28:38 -08001841 def clusters(self, json_format=True):
1842 '''
1843 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001844 Optional argument:
1845 * json_format - boolean indicating if you want output in json
Jon Hall73cf9cc2014-11-20 22:28:38 -08001846 '''
1847 try:
1848 self.handle.sendline("")
1849 self.handle.expect("onos>")
1850
1851 if json_format:
1852 self.handle.sendline("clusters -j")
1853 self.handle.expect("clusters -j")
1854 self.handle.expect("onos>")
1855 handle = self.handle.before
1856 '''
1857 handle variable here contains some ANSI escape color code
1858 sequences at the end which are invisible in the print command
1859 output. To make that escape sequence visible, use repr() function.
1860 The repr(handle) output when printed shows the ANSI escape sequences.
1861 In json.loads(somestring), this somestring variable is actually
1862 repr(somestring) and json.loads would fail with the escape sequence.
1863 So we take off that escape sequence using
1864 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1865 handle1 = ansi_escape.sub('', handle)
1866 '''
1867 #print "repr(handle) =", repr(handle)
1868 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
1869 handle1 = ansi_escape.sub('', handle)
1870 #print "repr(handle1) = ", repr(handle1)
1871 return handle1
1872 else:
Jon Hallffb386d2014-11-21 13:43:38 -08001873 self.handle.sendline("clusters")
Jon Hall73cf9cc2014-11-20 22:28:38 -08001874 self.handle.expect("onos>")
1875 handle = self.handle.before
1876 #print "handle =",handle
1877 return handle
1878 except pexpect.EOF:
1879 main.log.error(self.name + ": EOF exception found")
1880 main.log.error(self.name + ": " + self.handle.before)
1881 main.cleanup()
1882 main.exit()
1883 except:
1884 main.log.info(self.name+" ::::::")
1885 main.log.error( traceback.print_exc())
1886 main.log.info(self.name+" ::::::")
1887 main.cleanup()
1888 main.exit()
1889
Jon Hall94fd0472014-12-08 11:52:42 -08001890 def election_test_leader(self):
1891 '''
1892 * CLI command to get the current leader for the Election test application.
1893 #NOTE: Requires installation of the onos-app-election feature
1894 Returns: Node IP of the leader if one exists
1895 None if none exists
1896 Main.FALSE on error
1897 '''
1898 try:
1899 self.handle.sendline("election-test-leader")
1900 self.handle.expect("election-test-leader")
1901 self.handle.expect("onos>")
1902 response = self.handle.before
1903 #Leader
1904 node_search = re.search("The\scurrent\sleader\sfor\sthe\sElection\sapp\sis\s(?P<node>.+)\.", response)
1905 if node_search:
1906 node = node_search.group('node')
Jon Hall669173b2014-12-17 11:36:30 -08001907 main.log.info("Election-test-leader on "+str(self.name)+" found " + node + " as the leader")
Jon Hall94fd0472014-12-08 11:52:42 -08001908 return node
1909 #no leader
1910 null_search = re.search("There\sis\scurrently\sno\sleader\selected\sfor\sthe\sElection\sapp", response)
1911 if null_search:
Jon Hall669173b2014-12-17 11:36:30 -08001912 main.log.info("Election-test-leader found no leader on " + self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001913 return None
1914 #error
Jon Hall669173b2014-12-17 11:36:30 -08001915 if re.search("Command\snot\sfound", response):
1916 main.log.error("Election app is not loaded on " + self.name)
1917 return main.FALSE
1918 else:
1919 main.log.error("Error in election_test_leader: unexpected response")
1920 main.log.error( repr(response) )
1921 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001922
1923 except pexpect.EOF:
1924 main.log.error(self.name + ": EOF exception found")
1925 main.log.error(self.name + ": " + self.handle.before)
1926 main.cleanup()
1927 main.exit()
1928 except:
1929 main.log.info(self.name+" ::::::")
1930 main.log.error( traceback.print_exc())
1931 main.log.info(self.name+" ::::::")
1932 main.cleanup()
1933 main.exit()
1934
1935 def election_test_run(self):
1936 '''
1937 * CLI command to run for leadership of the Election test application.
1938 #NOTE: Requires installation of the onos-app-election feature
1939 Returns: Main.TRUE on success
1940 Main.FALSE on error
1941 '''
1942 try:
1943 self.handle.sendline("election-test-run")
1944 self.handle.expect("election-test-run")
1945 self.handle.expect("onos>")
1946 response = self.handle.before
1947 #success
1948 search = re.search("Entering\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1949 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001950 main.log.info(self.name + " entering leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001951 return main.TRUE
1952 #error
Jon Hall669173b2014-12-17 11:36:30 -08001953 if re.search("Command\snot\sfound", response):
1954 main.log.error("Election app is not loaded on " + self.name)
1955 return main.FALSE
1956 else:
1957 main.log.error("Error in election_test_run: unexpected response")
1958 main.log.error( repr(response) )
1959 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001960
1961 except pexpect.EOF:
1962 main.log.error(self.name + ": EOF exception found")
1963 main.log.error(self.name + ": " + self.handle.before)
1964 main.cleanup()
1965 main.exit()
1966 except:
1967 main.log.info(self.name+" ::::::")
1968 main.log.error( traceback.print_exc())
1969 main.log.info(self.name+" ::::::")
1970 main.cleanup()
1971 main.exit()
1972
1973 def election_test_withdraw(self):
1974 '''
1975 * CLI command to withdraw the local node from leadership election for
1976 * the Election test application.
1977 #NOTE: Requires installation of the onos-app-election feature
1978 Returns: Main.TRUE on success
1979 Main.FALSE on error
1980 '''
1981 try:
1982 self.handle.sendline("election-test-withdraw")
1983 self.handle.expect("election-test-withdraw")
1984 self.handle.expect("onos>")
1985 response = self.handle.before
1986 #success
1987 search = re.search("Withdrawing\sfrom\sleadership\selections\sfor\sthe\sElection\sapp.", response)
1988 if search:
Jon Hall669173b2014-12-17 11:36:30 -08001989 main.log.info(self.name + " withdrawing from leadership elections for the Election app.")
Jon Hall94fd0472014-12-08 11:52:42 -08001990 return main.TRUE
1991 #error
Jon Hall669173b2014-12-17 11:36:30 -08001992 if re.search("Command\snot\sfound", response):
1993 main.log.error("Election app is not loaded on " + self.name)
1994 return main.FALSE
1995 else:
1996 main.log.error("Error in election_test_withdraw: unexpected response")
1997 main.log.error( repr(response) )
1998 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001999
2000
2001 except pexpect.EOF:
2002 main.log.error(self.name + ": EOF exception found")
2003 main.log.error(self.name + ": " + self.handle.before)
2004 main.cleanup()
2005 main.exit()
2006 except:
2007 main.log.info(self.name+" ::::::")
2008 main.log.error( traceback.print_exc())
2009 main.log.info(self.name+" ::::::")
2010 main.cleanup()
2011 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002012
andrewonlab7e4d2d32014-10-15 13:23:21 -04002013 #***********************************
Hari Krishna416c3ca2014-12-19 15:39:31 -08002014 def getDevicePortsEnabledCount(self,dpid):
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002015 '''
2016 Get the count of all enabled ports on a particular device/switch
2017 '''
2018 try:
Hari Krishna2bbaa702014-12-19 14:46:12 -08002019 dpid = str(dpid)
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002020 self.handle.sendline("")
2021 self.handle.expect("onos>")
2022
2023 self.handle.sendline("onos:ports -e "+dpid+" | wc -l")
2024 i = self.handle.expect([
2025 "No such device",
2026 "onos>"])
2027
2028 #self.handle.sendline("")
2029 #self.handle.expect("onos>")
2030
2031 output = self.handle.before
2032
2033 if i == 0:
2034 main.log.error("Error in getting ports")
2035 return (ouput, "Error")
2036 else:
2037 result = output
2038 return result
2039
2040 except pexpect.EOF:
2041 main.log.error(self.name + ": EOF exception found")
2042 main.log.error(self.name + ": " + self.handle.before)
2043 main.cleanup()
2044 main.exit()
2045 except:
2046 main.log.info(self.name+" ::::::")
2047 main.log.error( traceback.print_exc())
2048 main.log.info(self.name+" ::::::")
2049 main.cleanup()
2050 main.exit()
2051
2052 def getDeviceLinksActiveCount(self,dpid):
2053 '''
2054 Get the count of all enabled ports on a particular device/switch
2055 '''
2056 try:
Hari Krishna2bbaa702014-12-19 14:46:12 -08002057 dpid = str(dpid)
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002058 self.handle.sendline("")
2059 self.handle.expect("onos>")
2060
2061 self.handle.sendline("onos:links "+dpid+" | grep ACTIVE | wc -l")
2062 i = self.handle.expect([
2063 "No such device",
2064 "onos>"])
2065
2066 output = self.handle.before
2067
2068 if i == 0:
2069 main.log.error("Error in getting ports")
2070 return (ouput, "Error")
2071 else:
2072 result = output
2073 return result
2074
2075 except pexpect.EOF:
2076 main.log.error(self.name + ": EOF exception found")
2077 main.log.error(self.name + ": " + self.handle.before)
2078 main.cleanup()
2079 main.exit()
2080 except:
2081 main.log.info(self.name+" ::::::")
2082 main.log.error( traceback.print_exc())
2083 main.log.info(self.name+" ::::::")
2084 main.cleanup()
2085 main.exit()
2086
2087 def getAllIntentIds(self):
2088 '''
2089 Return a list of all Intent IDs
2090 '''
2091 try:
2092 self.handle.sendline("")
2093 self.handle.expect("onos>")
2094
2095 self.handle.sendline("onos:intents | grep id=")
2096 i = self.handle.expect([
2097 "Error",
2098 "onos>"])
2099
2100 output = self.handle.before
2101
2102 if i == 0:
2103 main.log.error("Error in getting ports")
2104 return (ouput, "Error")
2105 else:
2106 result = output
2107 return result
2108
2109 except pexpect.EOF:
2110 main.log.error(self.name + ": EOF exception found")
2111 main.log.error(self.name + ": " + self.handle.before)
2112 main.cleanup()
2113 main.exit()
2114 except:
2115 main.log.info(self.name+" ::::::")
2116 main.log.error( traceback.print_exc())
2117 main.log.info(self.name+" ::::::")
2118 main.cleanup()
2119 main.exit()