blob: 03ebb4eec8fd234623a7d2264a41817cbc26516d [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:
74 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
75 main.log.error( traceback.print_exc() )
76 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
77 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("")
87 self.handle.expect("onos>")
andrewonlabc2d05aa2014-10-13 16:51:10 -040088 self.handle.sendline("system:shutdown")
89 self.handle.expect("Confirm")
andrewonlab2a6c9342014-10-16 13:40:15 -040090 self.handle.sendline("yes")
andrewonlabc2d05aa2014-10-13 16:51:10 -040091 self.handle.expect("\$")
92
andrewonlab95ce8322014-10-13 14:12:04 -040093 except pexpect.EOF:
94 main.log.error(self.name + ": EOF exception found")
95 main.log.error(self.name + ": " + self.handle.before)
96 except:
97 main.log.error(self.name + ": Connection failed to the host")
98 response = main.FALSE
99 return response
100
101 def set_cell(self, cellname):
102 '''
103 Calls 'cell <name>' to set the environment variables on ONOSbench
104
105 Before issuing any cli commands, set the environment variable first.
106 '''
107 try:
108 if not cellname:
109 main.log.error("Must define cellname")
110 main.cleanup()
111 main.exit()
112 else:
113 self.handle.sendline("cell "+str(cellname))
114 #Expect the cellname in the ONOS_CELL variable.
115 #Note that this variable name is subject to change
116 # and that this driver will have to change accordingly
117 self.handle.expect("ONOS_CELL="+str(cellname))
118 handle_before = self.handle.before
119 handle_after = self.handle.after
120 #Get the rest of the handle
121 self.handle.sendline("")
122 self.handle.expect("\$")
123 handle_more = self.handle.before
124
125 main.log.info("Cell call returned: "+handle_before+
126 handle_after + handle_more)
127
128 return main.TRUE
129
130 except pexpect.EOF:
131 main.log.error(self.name + ": EOF exception found")
132 main.log.error(self.name + ": " + self.handle.before)
133 main.cleanup()
134 main.exit()
135 except:
136 main.log.info(self.name+" ::::::")
137 main.log.error( traceback.print_exc())
138 main.log.info(self.name+" ::::::")
139 main.cleanup()
140 main.exit()
141
andrewonlabc2d05aa2014-10-13 16:51:10 -0400142 def start_onos_cli(self, ONOS_ip):
andrewonlab95ce8322014-10-13 14:12:04 -0400143 try:
144 self.handle.sendline("")
145 self.handle.expect("\$")
146
147 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400148 self.handle.sendline("onos -w "+str(ONOS_ip))
Jon Halld815ce42014-10-17 19:52:30 -0400149 self.handle.expect("onos>", timeout = 60)
andrewonlab95ce8322014-10-13 14:12:04 -0400150
151 except pexpect.EOF:
152 main.log.error(self.name + ": EOF exception found")
153 main.log.error(self.name + ": " + self.handle.before)
154 main.cleanup()
155 main.exit()
156 except:
157 main.log.info(self.name+" ::::::")
158 main.log.error( traceback.print_exc())
159 main.log.info(self.name+" ::::::")
160 main.cleanup()
161 main.exit()
162
andrewonlaba18f6bf2014-10-13 19:31:54 -0400163 def sendline(self, cmd_str):
164 '''
165 Send a completely user specified string to
166 the onos> prompt. Use this function if you have
167 a very specific command to send.
168
169 Warning: There are no sanity checking to commands
170 sent using this method.
171 '''
172 try:
173 self.handle.sendline("")
174 self.handle.expect("onos>")
175
176 self.handle.sendline(cmd_str)
177 self.handle.expect("onos>")
178
179 handle = self.handle.before
180
181 self.handle.sendline("")
182 self.handle.expect("onos>")
183
184 handle += self.handle.before
185 handle += self.handle.after
186
187 main.log.info("Command sent.")
188
189 return handle
190 except pexpect.EOF:
191 main.log.error(self.name + ": EOF exception found")
192 main.log.error(self.name + ": " + self.handle.before)
193 main.cleanup()
194 main.exit()
195 except:
196 main.log.info(self.name+" ::::::")
197 main.log.error( traceback.print_exc())
198 main.log.info(self.name+" ::::::")
199 main.cleanup()
200 main.exit()
201
andrewonlab95ce8322014-10-13 14:12:04 -0400202 #IMPORTANT NOTE:
203 #For all cli commands, naming convention should match
204 #the cli command replacing ':' with '_'.
205 #Ex) onos:topology > onos_topology
206 # onos:links > onos_links
207 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400208
209 def add_node(self, node_id, ONOS_ip, tcp_port=""):
210 '''
211 Adds a new cluster node by ID and address information.
212 Required:
213 * node_id
214 * ONOS_ip
215 Optional:
216 * tcp_port
217 '''
218 try:
219 self.handle.sendline("")
220 self.handle.expect("onos>")
221
222 self.handle.sendline("add-node "+
223 str(node_id)+" "+
224 str(ONOS_ip)+" "+
225 str(tcp_port))
226
227 i = self.handle.expect([
228 "Error",
229 "onos>" ])
230
231 #Clear handle to get previous output
232 self.handle.sendline("")
233 self.handle.expect("onos>")
234
235 handle = self.handle.before
236
237 if i == 0:
238 main.log.error("Error in adding node")
239 main.log.error(handle)
240 return main.FALSE
241 else:
242 main.log.info("Node "+str(ONOS_ip)+" added")
243 return main.TRUE
244
245 except pexpect.EOF:
246 main.log.error(self.name + ": EOF exception found")
247 main.log.error(self.name + ": " + self.handle.before)
248 main.cleanup()
249 main.exit()
250 except:
251 main.log.info(self.name+" ::::::")
252 main.log.error( traceback.print_exc())
253 main.log.info(self.name+" ::::::")
254 main.cleanup()
255 main.exit()
256
andrewonlab86dc3082014-10-13 18:18:38 -0400257 def remove_node(self, node_id):
258 '''
259 Removes a cluster by ID
260 Issues command: 'remove-node [<node-id>]'
261 Required:
262 * node_id
263 '''
264 try:
265 self.handle.sendline("")
266 self.handle.expect("onos>")
267
268 self.handle.sendline("remove-node "+str(node_id))
269 self.handle.expect("onos>")
270
271 return main.TRUE
272
273 except pexpect.EOF:
274 main.log.error(self.name + ": EOF exception found")
275 main.log.error(self.name + ": " + self.handle.before)
276 main.cleanup()
277 main.exit()
278 except:
279 main.log.info(self.name+" ::::::")
280 main.log.error( traceback.print_exc())
281 main.log.info(self.name+" ::::::")
282 main.cleanup()
283 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400284
andrewonlab7c211572014-10-15 16:45:20 -0400285 def nodes(self):
286 '''
287 List the nodes currently visible
288 Issues command: 'nodes'
289 Returns: entire handle of list of nodes
290 '''
291 try:
292 self.handle.sendline("")
293 self.handle.expect("onos>")
294
295 self.handle.sendline("nodes")
296 self.handle.expect("onos>")
297
298 self.handle.sendline("")
299 self.handle.expect("onos>")
300
301 handle = self.handle.before
302
303 return handle
304
305 except pexpect.EOF:
306 main.log.error(self.name + ": EOF exception found")
307 main.log.error(self.name + ": " + self.handle.before)
308 main.cleanup()
309 main.exit()
310 except:
311 main.log.info(self.name+" ::::::")
312 main.log.error( traceback.print_exc())
313 main.log.info(self.name+" ::::::")
314 main.cleanup()
315 main.exit()
316
andrewonlab38d6ae22014-10-15 14:23:45 -0400317 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400318 '''
319 Shows the current state of the topology
320 by issusing command: 'onos> onos:topology'
321 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400322 try:
323 self.handle.sendline("")
324 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400325 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400326 self.handle.sendline("onos:topology")
327 self.handle.expect("onos>")
328
329 handle = self.handle.before
330
331 main.log.info("onos:topology returned: " +
332 str(handle))
333
334 return handle
335
336 except pexpect.EOF:
337 main.log.error(self.name + ": EOF exception found")
338 main.log.error(self.name + ": " + self.handle.before)
339 main.cleanup()
340 main.exit()
341 except:
342 main.log.info(self.name+" ::::::")
343 main.log.error( traceback.print_exc())
344 main.log.info(self.name+" ::::::")
345 main.cleanup()
346 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400347
348 def feature_install(self, feature_str):
349 '''
350 Installs a specified feature
351 by issuing command: 'onos> feature:install <feature_str>'
352 '''
353 try:
354 self.handle.sendline("")
355 self.handle.expect("onos>")
356
357 self.handle.sendline("feature:install "+str(feature_str))
358 self.handle.expect("onos>")
359
360 return main.TRUE
361
362 except pexpect.EOF:
363 main.log.error(self.name + ": EOF exception found")
364 main.log.error(self.name + ": " + self.handle.before)
365 main.cleanup()
366 main.exit()
367 except:
368 main.log.info(self.name+" ::::::")
369 main.log.error( traceback.print_exc())
370 main.log.info(self.name+" ::::::")
371 main.cleanup()
372 main.exit()
373
374 def feature_uninstall(self, feature_str):
375 '''
376 Uninstalls a specified feature
377 by issuing command: 'onos> feature:uninstall <feature_str>'
378 '''
379 try:
380 self.handle.sendline("")
381 self.handle.expect("onos>")
382
383 self.handle.sendline("feature:uninstall "+str(feature_str))
384 self.handle.expect("onos>")
385
386 return main.TRUE
387
388 except pexpect.EOF:
389 main.log.error(self.name + ": EOF exception found")
390 main.log.error(self.name + ": " + self.handle.before)
391 main.cleanup()
392 main.exit()
393 except:
394 main.log.info(self.name+" ::::::")
395 main.log.error( traceback.print_exc())
396 main.log.info(self.name+" ::::::")
397 main.cleanup()
398 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400399
Jon Halle8217482014-10-17 13:49:14 -0400400 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400401 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400402 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400403 Optional argument:
404 * grep_str - pass in a string to grep
405 '''
406 try:
407 self.handle.sendline("")
408 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400409
410 if json_format:
411 if not grep_str:
412 self.handle.sendline("devices -j")
413 self.handle.expect("devices -j")
414 self.handle.expect("onos>")
415 else:
416 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400417 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400418 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
419 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400420 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400421 '''
422 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
423 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 -0400424 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Halld815ce42014-10-17 19:52:30 -0400425 So we take off that escape sequence using
426 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
427 handle1 = ansi_escape.sub('', handle)
428 '''
429 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400430 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
431 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400432 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400433 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400434 else:
435 if not grep_str:
436 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400437 self.handle.expect("onos>")
438 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400439 self.handle.expect("onos>")
440 else:
441 self.handle.sendline("devices | grep '"+
442 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400443 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400444 self.handle.sendline("")
andrewonlab86dc3082014-10-13 18:18:38 -0400445 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400446 handle = self.handle.before
447 print "handle =",handle
448 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400449 except pexpect.EOF:
450 main.log.error(self.name + ": EOF exception found")
451 main.log.error(self.name + ": " + self.handle.before)
452 main.cleanup()
453 main.exit()
454 except:
455 main.log.info(self.name+" ::::::")
456 main.log.error( traceback.print_exc())
457 main.log.info(self.name+" ::::::")
458 main.cleanup()
459 main.exit()
460
Jon Halle8217482014-10-17 13:49:14 -0400461 def links(self, json_format=True, grep_str=""):
462 '''
463 Lists all core links
464 Optional argument:
465 * grep_str - pass in a string to grep
466 '''
467 try:
468 self.handle.sendline("")
469 self.handle.expect("onos>")
470
471 if json_format:
472 if not grep_str:
473 self.handle.sendline("links -j")
474 self.handle.expect("links -j")
475 self.handle.expect("onos>")
476 else:
477 self.handle.sendline("links -j | grep '"+
478 str(grep_str)+"'")
479 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
480 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400481 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400482 '''
483 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
484 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 -0400485 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Jon Halld815ce42014-10-17 19:52:30 -0400486 So we take off that escape sequence using
487 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
488 handle1 = ansi_escape.sub('', handle)
489 '''
490 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400491 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
492 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400493 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400494 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400495 else:
496 if not grep_str:
497 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400498 self.handle.expect("onos>")
499 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400500 self.handle.expect("onos>")
501 else:
502 self.handle.sendline("links | grep '"+
503 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400504 self.handle.expect("onos>")
505 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400506 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400507 handle = self.handle.before
508 print "handle =",handle
509 return handle
Jon Halle8217482014-10-17 13:49:14 -0400510 except pexpect.EOF:
511 main.log.error(self.name + ": EOF exception found")
512 main.log.error(self.name + ": " + self.handle.before)
513 main.cleanup()
514 main.exit()
515 except:
516 main.log.info(self.name+" ::::::")
517 main.log.error( traceback.print_exc())
518 main.log.info(self.name+" ::::::")
519 main.cleanup()
520 main.exit()
521
522
523 def ports(self, json_format=True, grep_str=""):
524 '''
525 Lists all ports
526 Optional argument:
527 * grep_str - pass in a string to grep
528 '''
529 try:
530 self.handle.sendline("")
531 self.handle.expect("onos>")
532
533 if json_format:
534 if not grep_str:
535 self.handle.sendline("ports -j")
536 self.handle.expect("ports -j")
537 self.handle.expect("onos>")
538 else:
539 self.handle.sendline("ports -j | grep '"+
540 str(grep_str)+"'")
541 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
542 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400543 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400544 '''
545 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
546 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 -0400547 In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence.
Shreya Shah0c525cc2014-10-17 20:20:24 -0400548 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400549 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
550 handle1 = ansi_escape.sub('', handle)
551 '''
552 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400553 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
554 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400555 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400556 return handle1
557
Jon Halle8217482014-10-17 13:49:14 -0400558 else:
559 if not grep_str:
560 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400561 self.handle.expect("onos>")
562 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400563 self.handle.expect("onos>")
564 else:
565 self.handle.sendline("ports | grep '"+
566 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400567 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400568 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400569 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400570 handle = self.handle.before
571 print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400572 return handle
Jon Halle8217482014-10-17 13:49:14 -0400573 except pexpect.EOF:
574 main.log.error(self.name + ": EOF exception found")
575 main.log.error(self.name + ": " + self.handle.before)
576 main.cleanup()
577 main.exit()
578 except:
579 main.log.info(self.name+" ::::::")
580 main.log.error( traceback.print_exc())
581 main.log.info(self.name+" ::::::")
582 main.cleanup()
583 main.exit()
584
585
andrewonlab7c211572014-10-15 16:45:20 -0400586 def device_role(self, device_id, node_id, role):
587 '''
588 Set device role for specified device and node with role
589 Required:
590 * device_id : may be obtained by function get_all_devices_id
591 * node_id : may be obtained by function get_all_nodes_id
592 * role: specify one of the following roles:
593 - master
594 - standby
595 - none
596 '''
597 try:
598 self.handle.sendline("")
599 self.handle.expect("onos>")
600
601 self.handle.sendline("device-role "+
602 str(device_id) + " " +
603 str(node_id) + " " +
604 str(role))
605 i = self.handle.expect([
606 "Error",
607 "onos>"])
608
609 self.handle.sendline("")
610 self.handle.expect("onos>")
611
612 handle = self.handle.before
613
614 if i == 0:
615 main.log.error("device-role command returned error")
616 return handle
617 else:
618 return main.TRUE
619
andrewonlab86dc3082014-10-13 18:18:38 -0400620 except pexpect.EOF:
621 main.log.error(self.name + ": EOF exception found")
622 main.log.error(self.name + ": " + self.handle.before)
623 main.cleanup()
624 main.exit()
625 except:
626 main.log.info(self.name+" ::::::")
627 main.log.error( traceback.print_exc())
628 main.log.info(self.name+" ::::::")
629 main.cleanup()
630 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400631
andrewonlab3e15ead2014-10-15 14:21:34 -0400632 def paths(self, src_id, dst_id):
633 '''
634 Returns string of paths, and the cost.
635 Issues command: onos:paths <src> <dst>
636 '''
637 try:
638 self.handle.sendline("")
639 self.handle.expect("onos>")
640
641 self.handle.sendline("onos:paths "+
642 str(src_id) + " " + str(dst_id))
643 i = self.handle.expect([
644 "Error",
645 "onos>"])
646
647 self.handle.sendline("")
648 self.handle.expect("onos>")
649
650 handle = self.handle.before
651
652 if i == 0:
653 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400654 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400655 else:
656 path = handle.split(";")[0]
657 cost = handle.split(";")[1]
658 return (path, cost)
659
660 except pexpect.EOF:
661 main.log.error(self.name + ": EOF exception found")
662 main.log.error(self.name + ": " + self.handle.before)
663 main.cleanup()
664 main.exit()
665 except:
666 main.log.info(self.name+" ::::::")
667 main.log.error( traceback.print_exc())
668 main.log.info(self.name+" ::::::")
669 main.cleanup()
670 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400671
672 #TODO:
673 #def hosts(self):
674
675 def get_hosts_id(self, host_list):
676 '''
677 Obtain list of hosts
678 Issues command: 'onos> hosts'
679
680 Required:
681 * host_list: List of hosts obtained by Mininet
682 IMPORTANT:
683 This function assumes that you started your
684 topology with the option '--mac'.
685 Furthermore, it assumes that value of VLAN is '-1'
686 Description:
687 Converts mininet hosts (h1, h2, h3...) into
688 ONOS format (00:00:00:00:00:01/-1 , ...)
689 '''
690
691 try:
Shreya Shahd7310c52014-10-20 16:44:37 -0400692 #self.handle.sendline("")
693 #self.handle.expect("onos>")
andrewonlab3f0a4af2014-10-17 12:25:14 -0400694
695 onos_host_list = []
696
697 for host in host_list:
698 host = host.replace("h", "")
699 host_hex = hex(int(host)).zfill(12)
700 host_hex = str(host_hex).replace('x','0')
701 i = iter(str(host_hex))
702 host_hex = ":".join(a+b for a,b in zip(i,i))
703 host_hex = host_hex + "/-1"
704 onos_host_list.append(host_hex)
705
706 return onos_host_list
707
708 except pexpect.EOF:
709 main.log.error(self.name + ": EOF exception found")
710 main.log.error(self.name + ": " + self.handle.before)
711 main.cleanup()
712 main.exit()
713 except:
714 main.log.info(self.name+" ::::::")
715 main.log.error( traceback.print_exc())
716 main.log.info(self.name+" ::::::")
717 main.cleanup()
718 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400719
andrewonlabe6745342014-10-17 14:29:13 -0400720 def add_host_intent(self, host_id_one, host_id_two):
721 '''
722 Required:
723 * host_id_one: ONOS host id for host1
724 * host_id_two: ONOS host id for host2
725 Description:
726 Adds a host-to-host intent (bidrectional) by
727 specifying the two hosts.
728 '''
729 try:
730 self.handle.sendline("")
731 self.handle.expect("onos>")
732
733 self.handle.sendline("add-host-intent "+
734 str(host_id_one) + " " + str(host_id_two))
735 self.handle.expect("onos>")
736
737 self.handle.sendline("")
738 self.handle.expect("onos>")
739
740 handle = self.handle.before
741
742 main.log.info("Intent installed between "+
743 str(host_id_one) + " and " + str(host_id_two))
744
745 return handle
746
747 except pexpect.EOF:
748 main.log.error(self.name + ": EOF exception found")
749 main.log.error(self.name + ": " + self.handle.before)
750 main.cleanup()
751 main.exit()
752 except:
753 main.log.info(self.name+" ::::::")
754 main.log.error( traceback.print_exc())
755 main.log.info(self.name+" ::::::")
756 main.cleanup()
757 main.exit()
758
andrewonlab4dbb4d82014-10-17 18:22:31 -0400759 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400760 egress_device, port_egress, ethType="", ethSrc="",
761 ethDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400762 '''
763 Required:
764 * ingress_device: device id of ingress device
765 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400766 Optional:
767 * ethType: specify ethType
768 * ethSrc: specify ethSrc (i.e. src mac addr)
769 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400770 Description:
771 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400772 specifying device id's and optional fields
773
andrewonlab4dbb4d82014-10-17 18:22:31 -0400774 NOTE: This function may change depending on the
775 options developers provide for point-to-point
776 intent via cli
777 '''
778 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400779 cmd = ""
780
781 #If there are no optional arguments
782 if not ethType and not ethSrc and not ethDst:
783 cmd = "add-point-intent "+\
784 str(ingress_device) + "/" + str(port_ingress) + " " +\
785 str(egress_device) + "/" + str(port_egress)
786
787 else:
andrewonlab9a130be2014-10-22 12:44:56 -0400788 cmd = "add-point-intent "
789
andrewonlab0c0a6772014-10-22 12:31:18 -0400790 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -0400791 cmd += " --ethType " + str(ethType)
792 if ethSrc:
793 cmd += " --ethSrc " + str(ethSrc)
794 if ethDst:
795 cmd += " --ethDst " + str(ethDst)
andrewonlab9a130be2014-10-22 12:44:56 -0400796
797 cmd += " "+str(ingress_device) + "/" + str(port_ingress) + " " +\
798 str(egress_device) + "/" + str(port_egress)
andrewonlab289e4b72014-10-21 21:24:18 -0400799
andrewonlab4dbb4d82014-10-17 18:22:31 -0400800 self.handle.sendline("")
801 self.handle.expect("onos>")
802
andrewonlab289e4b72014-10-21 21:24:18 -0400803 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400804 i = self.handle.expect([
805 "Error",
806 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -0400807
andrewonlab4dbb4d82014-10-17 18:22:31 -0400808 self.handle.sendline("")
809 self.handle.expect("onos>")
810
811 handle = self.handle.before
812
813 if i == 0:
814 main.log.error("Error in adding point-to-point intent")
815 return handle
816 else:
817 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -0400818
andrewonlab4dbb4d82014-10-17 18:22:31 -0400819 except pexpect.EOF:
820 main.log.error(self.name + ": EOF exception found")
821 main.log.error(self.name + ": " + self.handle.before)
822 main.cleanup()
823 main.exit()
824 except:
825 main.log.info(self.name+" ::::::")
826 main.log.error( traceback.print_exc())
827 main.log.info(self.name+" ::::::")
828 main.cleanup()
829 main.exit()
830
andrewonlab9a50dfe2014-10-17 17:22:31 -0400831 def remove_intent(self, intent_id):
832 '''
833 Remove intent for specified intent id
834 '''
835 try:
836 self.handle.sendline("")
837 self.handle.expect("onos>")
838
839 self.handle.sendline("remove-intent "+str(intent_id))
840 i = self.handle.expect([
841 "Error",
842 "onos>"])
843
844 handle = self.handle.before
845
846 if i == 0:
847 main.log.error("Error in removing intent")
848 return handle
849 else:
850 return handle
851
852 except pexpect.EOF:
853 main.log.error(self.name + ": EOF exception found")
854 main.log.error(self.name + ": " + self.handle.before)
855 main.cleanup()
856 main.exit()
857 except:
858 main.log.info(self.name+" ::::::")
859 main.log.error( traceback.print_exc())
860 main.log.info(self.name+" ::::::")
861 main.cleanup()
862 main.exit()
863
andrewonlab377693f2014-10-21 16:00:30 -0400864 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -0400865 '''
andrewonlab377693f2014-10-21 16:00:30 -0400866 Optional:
867 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -0400868 Description:
869 Obtain intents currently installed
870 '''
871 try:
andrewonlab377693f2014-10-21 16:00:30 -0400872 if json_format:
873 self.handle.sendline("intents -j")
874 self.handle.expect("intents -j")
875 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -0400876
andrewonlab377693f2014-10-21 16:00:30 -0400877 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -0400878
andrewonlab377693f2014-10-21 16:00:30 -0400879 else:
880 self.handle.sendline("")
881 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -0400882
andrewonlab377693f2014-10-21 16:00:30 -0400883 self.handle.sendline("intents")
884 self.handle.expect("onos>")
885
886 self.handle.sendline("")
887 self.handle.expect("onos>")
888
889 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -0400890
891 return handle
892
893 except pexpect.EOF:
894 main.log.error(self.name + ": EOF exception found")
895 main.log.error(self.name + ": " + self.handle.before)
896 main.cleanup()
897 main.exit()
898 except:
899 main.log.info(self.name+" ::::::")
900 main.log.error( traceback.print_exc())
901 main.log.info(self.name+" ::::::")
902 main.cleanup()
903 main.exit()
904
andrewonlab3e15ead2014-10-15 14:21:34 -0400905 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -0400906 #Wrapper functions use existing driver
907 #functions and extends their use case.
908 #For example, we may use the output of
909 #a normal driver function, and parse it
910 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -0400911
andrewonlab9a50dfe2014-10-17 17:22:31 -0400912 def get_all_intents_id(self):
913 '''
914 Description:
915 Obtain all intent id's in a list
916 '''
917 try:
918 #Obtain output of intents function
919 intents_str = self.intents()
920 all_intent_list = []
921 intent_id_list = []
922
923 #Parse the intents output for ID's
924 intents_list = [s.strip() for s in intents_str.splitlines()]
925 for intents in intents_list:
926 if "onos>" in intents:
927 continue
928 elif "intents" in intents:
929 continue
930 else:
931 line_list = intents.split(" ")
932 all_intent_list.append(line_list[0])
933
934 all_intent_list = all_intent_list[1:-2]
935
936 for intents in all_intent_list:
937 if not intents:
938 continue
939 else:
940 intent_id_list.append(intents)
941
942 return intent_id_list
943
944 except pexpect.EOF:
945 main.log.error(self.name + ": EOF exception found")
946 main.log.error(self.name + ": " + self.handle.before)
947 main.cleanup()
948 main.exit()
949 except:
950 main.log.info(self.name+" ::::::")
951 main.log.error( traceback.print_exc())
952 main.log.info(self.name+" ::::::")
953 main.cleanup()
954 main.exit()
955
andrewonlab7e4d2d32014-10-15 13:23:21 -0400956 def get_all_devices_id(self):
957 '''
958 Use 'devices' function to obtain list of all devices
959 and parse the result to obtain a list of all device
960 id's. Returns this list. Returns empty list if no
961 devices exist
962 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -0400963
964 This function may be useful if you are not sure of the
965 device id, and wish to execute other commands using
966 the ids. By obtaining the list of device ids on the fly,
967 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -0400968 '''
969 try:
970 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -0400971 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -0400972 id_list = []
973
974 if not devices_str:
975 main.log.info("There are no devices to get id from")
976 return id_list
977
978 #Split the string into list by comma
979 device_list = devices_str.split(",")
980 #Get temporary list of all arguments with string 'id='
981 temp_list = [dev for dev in device_list if "id=" in dev]
982 #Split list further into arguments before and after string
983 # 'id='. Get the latter portion (the actual device id) and
984 # append to id_list
985 for arg in temp_list:
986 id_list.append(arg.split("id=")[1])
987
988 return id_list
989
990 except pexpect.EOF:
991 main.log.error(self.name + ": EOF exception found")
992 main.log.error(self.name + ": " + self.handle.before)
993 main.cleanup()
994 main.exit()
995 except:
996 main.log.info(self.name+" ::::::")
997 main.log.error( traceback.print_exc())
998 main.log.info(self.name+" ::::::")
999 main.cleanup()
1000 main.exit()
1001
andrewonlab7c211572014-10-15 16:45:20 -04001002 def get_all_nodes_id(self):
1003 '''
1004 Uses 'nodes' function to obtain list of all nodes
1005 and parse the result of nodes to obtain just the
1006 node id's.
1007 Returns:
1008 list of node id's
1009 '''
1010 try:
1011 nodes_str = self.nodes()
1012 id_list = []
1013
1014 if not nodes_str:
1015 main.log.info("There are no nodes to get id from")
1016 return id_list
1017
1018 #Sample nodes_str output
1019 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1020
1021 #Split the string into list by comma
1022 nodes_list = nodes_str.split(",")
1023 temp_list = [node for node in nodes_list if "id=" in node]
1024 for arg in temp_list:
1025 id_list.append(arg.split("id=")[1])
1026
1027 return id_list
1028
1029 except pexpect.EOF:
1030 main.log.error(self.name + ": EOF exception found")
1031 main.log.error(self.name + ": " + self.handle.before)
1032 main.cleanup()
1033 main.exit()
1034 except:
1035 main.log.info(self.name+" ::::::")
1036 main.log.error( traceback.print_exc())
1037 main.log.info(self.name+" ::::::")
1038 main.cleanup()
1039 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001040
1041 #***********************************