blob: a3a11b327340a2ff8cdd4d39c9ad1d0e22cfc708 [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:
788 cmd = "add-point-intent "+\
789 str(ingress_device) + "/" + str(port_ingress) + " " +\
790 str(egress_device) + "/" + str(port_egress)
791 if etherType:
792 cmd += " --ethType " + str(ethType)
793 if ethSrc:
794 cmd += " --ethSrc " + str(ethSrc)
795 if ethDst:
796 cmd += " --ethDst " + str(ethDst)
797
andrewonlab4dbb4d82014-10-17 18:22:31 -0400798 self.handle.sendline("")
799 self.handle.expect("onos>")
800
andrewonlab289e4b72014-10-21 21:24:18 -0400801 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400802 i = self.handle.expect([
803 "Error",
804 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -0400805
andrewonlab4dbb4d82014-10-17 18:22:31 -0400806 self.handle.sendline("")
807 self.handle.expect("onos>")
808
809 handle = self.handle.before
810
811 if i == 0:
812 main.log.error("Error in adding point-to-point intent")
813 return handle
814 else:
815 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -0400816
andrewonlab4dbb4d82014-10-17 18:22:31 -0400817 except pexpect.EOF:
818 main.log.error(self.name + ": EOF exception found")
819 main.log.error(self.name + ": " + self.handle.before)
820 main.cleanup()
821 main.exit()
822 except:
823 main.log.info(self.name+" ::::::")
824 main.log.error( traceback.print_exc())
825 main.log.info(self.name+" ::::::")
826 main.cleanup()
827 main.exit()
828
andrewonlab9a50dfe2014-10-17 17:22:31 -0400829 def remove_intent(self, intent_id):
830 '''
831 Remove intent for specified intent id
832 '''
833 try:
834 self.handle.sendline("")
835 self.handle.expect("onos>")
836
837 self.handle.sendline("remove-intent "+str(intent_id))
838 i = self.handle.expect([
839 "Error",
840 "onos>"])
841
842 handle = self.handle.before
843
844 if i == 0:
845 main.log.error("Error in removing intent")
846 return handle
847 else:
848 return handle
849
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
andrewonlab377693f2014-10-21 16:00:30 -0400862 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -0400863 '''
andrewonlab377693f2014-10-21 16:00:30 -0400864 Optional:
865 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -0400866 Description:
867 Obtain intents currently installed
868 '''
869 try:
andrewonlab377693f2014-10-21 16:00:30 -0400870 if json_format:
871 self.handle.sendline("intents -j")
872 self.handle.expect("intents -j")
873 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -0400874
andrewonlab377693f2014-10-21 16:00:30 -0400875 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -0400876
andrewonlab377693f2014-10-21 16:00:30 -0400877 else:
878 self.handle.sendline("")
879 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -0400880
andrewonlab377693f2014-10-21 16:00:30 -0400881 self.handle.sendline("intents")
882 self.handle.expect("onos>")
883
884 self.handle.sendline("")
885 self.handle.expect("onos>")
886
887 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -0400888
889 return handle
890
891 except pexpect.EOF:
892 main.log.error(self.name + ": EOF exception found")
893 main.log.error(self.name + ": " + self.handle.before)
894 main.cleanup()
895 main.exit()
896 except:
897 main.log.info(self.name+" ::::::")
898 main.log.error( traceback.print_exc())
899 main.log.info(self.name+" ::::::")
900 main.cleanup()
901 main.exit()
902
andrewonlab3e15ead2014-10-15 14:21:34 -0400903 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -0400904 #Wrapper functions use existing driver
905 #functions and extends their use case.
906 #For example, we may use the output of
907 #a normal driver function, and parse it
908 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -0400909
andrewonlab9a50dfe2014-10-17 17:22:31 -0400910 def get_all_intents_id(self):
911 '''
912 Description:
913 Obtain all intent id's in a list
914 '''
915 try:
916 #Obtain output of intents function
917 intents_str = self.intents()
918 all_intent_list = []
919 intent_id_list = []
920
921 #Parse the intents output for ID's
922 intents_list = [s.strip() for s in intents_str.splitlines()]
923 for intents in intents_list:
924 if "onos>" in intents:
925 continue
926 elif "intents" in intents:
927 continue
928 else:
929 line_list = intents.split(" ")
930 all_intent_list.append(line_list[0])
931
932 all_intent_list = all_intent_list[1:-2]
933
934 for intents in all_intent_list:
935 if not intents:
936 continue
937 else:
938 intent_id_list.append(intents)
939
940 return intent_id_list
941
942 except pexpect.EOF:
943 main.log.error(self.name + ": EOF exception found")
944 main.log.error(self.name + ": " + self.handle.before)
945 main.cleanup()
946 main.exit()
947 except:
948 main.log.info(self.name+" ::::::")
949 main.log.error( traceback.print_exc())
950 main.log.info(self.name+" ::::::")
951 main.cleanup()
952 main.exit()
953
andrewonlab7e4d2d32014-10-15 13:23:21 -0400954 def get_all_devices_id(self):
955 '''
956 Use 'devices' function to obtain list of all devices
957 and parse the result to obtain a list of all device
958 id's. Returns this list. Returns empty list if no
959 devices exist
960 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -0400961
962 This function may be useful if you are not sure of the
963 device id, and wish to execute other commands using
964 the ids. By obtaining the list of device ids on the fly,
965 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -0400966 '''
967 try:
968 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -0400969 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -0400970 id_list = []
971
972 if not devices_str:
973 main.log.info("There are no devices to get id from")
974 return id_list
975
976 #Split the string into list by comma
977 device_list = devices_str.split(",")
978 #Get temporary list of all arguments with string 'id='
979 temp_list = [dev for dev in device_list if "id=" in dev]
980 #Split list further into arguments before and after string
981 # 'id='. Get the latter portion (the actual device id) and
982 # append to id_list
983 for arg in temp_list:
984 id_list.append(arg.split("id=")[1])
985
986 return id_list
987
988 except pexpect.EOF:
989 main.log.error(self.name + ": EOF exception found")
990 main.log.error(self.name + ": " + self.handle.before)
991 main.cleanup()
992 main.exit()
993 except:
994 main.log.info(self.name+" ::::::")
995 main.log.error( traceback.print_exc())
996 main.log.info(self.name+" ::::::")
997 main.cleanup()
998 main.exit()
999
andrewonlab7c211572014-10-15 16:45:20 -04001000 def get_all_nodes_id(self):
1001 '''
1002 Uses 'nodes' function to obtain list of all nodes
1003 and parse the result of nodes to obtain just the
1004 node id's.
1005 Returns:
1006 list of node id's
1007 '''
1008 try:
1009 nodes_str = self.nodes()
1010 id_list = []
1011
1012 if not nodes_str:
1013 main.log.info("There are no nodes to get id from")
1014 return id_list
1015
1016 #Sample nodes_str output
1017 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1018
1019 #Split the string into list by comma
1020 nodes_list = nodes_str.split(",")
1021 temp_list = [node for node in nodes_list if "id=" in node]
1022 for arg in temp_list:
1023 id_list.append(arg.split("id=")[1])
1024
1025 return id_list
1026
1027 except pexpect.EOF:
1028 main.log.error(self.name + ": EOF exception found")
1029 main.log.error(self.name + ": " + self.handle.before)
1030 main.cleanup()
1031 main.exit()
1032 except:
1033 main.log.info(self.name+" ::::::")
1034 main.log.error( traceback.print_exc())
1035 main.log.info(self.name+" ::::::")
1036 main.cleanup()
1037 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001038
1039 #***********************************