blob: 76c7e87255f7542a1dc07e2a2b04820c336f8e49 [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("")
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("\$")
93 self.handle.sendline("\n")
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
106 def set_cell(self, cellname):
107 '''
108 Calls 'cell <name>' to set the environment variables on ONOSbench
109
110 Before issuing any cli commands, set the environment variable first.
111 '''
112 try:
113 if not cellname:
114 main.log.error("Must define cellname")
115 main.cleanup()
116 main.exit()
117 else:
118 self.handle.sendline("cell "+str(cellname))
119 #Expect the cellname in the ONOS_CELL variable.
120 #Note that this variable name is subject to change
121 # and that this driver will have to change accordingly
122 self.handle.expect("ONOS_CELL="+str(cellname))
123 handle_before = self.handle.before
124 handle_after = self.handle.after
125 #Get the rest of the handle
126 self.handle.sendline("")
127 self.handle.expect("\$")
128 handle_more = self.handle.before
129
130 main.log.info("Cell call returned: "+handle_before+
131 handle_after + handle_more)
132
133 return main.TRUE
134
135 except pexpect.EOF:
136 main.log.error(self.name + ": EOF exception found")
137 main.log.error(self.name + ": " + self.handle.before)
138 main.cleanup()
139 main.exit()
140 except:
141 main.log.info(self.name+" ::::::")
142 main.log.error( traceback.print_exc())
143 main.log.info(self.name+" ::::::")
144 main.cleanup()
145 main.exit()
146
andrewonlabc2d05aa2014-10-13 16:51:10 -0400147 def start_onos_cli(self, ONOS_ip):
andrewonlab95ce8322014-10-13 14:12:04 -0400148 try:
149 self.handle.sendline("")
150 self.handle.expect("\$")
151
152 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400153 self.handle.sendline("onos -w "+str(ONOS_ip))
Jon Halld815ce42014-10-17 19:52:30 -0400154 self.handle.expect("onos>", timeout = 60)
andrewonlab95ce8322014-10-13 14:12:04 -0400155
156 except pexpect.EOF:
157 main.log.error(self.name + ": EOF exception found")
158 main.log.error(self.name + ": " + self.handle.before)
159 main.cleanup()
160 main.exit()
161 except:
162 main.log.info(self.name+" ::::::")
163 main.log.error( traceback.print_exc())
164 main.log.info(self.name+" ::::::")
165 main.cleanup()
166 main.exit()
167
andrewonlaba18f6bf2014-10-13 19:31:54 -0400168 def sendline(self, cmd_str):
169 '''
170 Send a completely user specified string to
171 the onos> prompt. Use this function if you have
172 a very specific command to send.
173
174 Warning: There are no sanity checking to commands
175 sent using this method.
176 '''
177 try:
178 self.handle.sendline("")
179 self.handle.expect("onos>")
180
181 self.handle.sendline(cmd_str)
182 self.handle.expect("onos>")
183
184 handle = self.handle.before
185
186 self.handle.sendline("")
187 self.handle.expect("onos>")
188
189 handle += self.handle.before
190 handle += self.handle.after
191
192 main.log.info("Command sent.")
193
194 return handle
195 except pexpect.EOF:
196 main.log.error(self.name + ": EOF exception found")
197 main.log.error(self.name + ": " + self.handle.before)
198 main.cleanup()
199 main.exit()
200 except:
201 main.log.info(self.name+" ::::::")
202 main.log.error( traceback.print_exc())
203 main.log.info(self.name+" ::::::")
204 main.cleanup()
205 main.exit()
206
andrewonlab95ce8322014-10-13 14:12:04 -0400207 #IMPORTANT NOTE:
208 #For all cli commands, naming convention should match
209 #the cli command replacing ':' with '_'.
210 #Ex) onos:topology > onos_topology
211 # onos:links > onos_links
212 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400213
214 def add_node(self, node_id, ONOS_ip, tcp_port=""):
215 '''
216 Adds a new cluster node by ID and address information.
217 Required:
218 * node_id
219 * ONOS_ip
220 Optional:
221 * tcp_port
222 '''
223 try:
224 self.handle.sendline("")
225 self.handle.expect("onos>")
226
227 self.handle.sendline("add-node "+
228 str(node_id)+" "+
229 str(ONOS_ip)+" "+
230 str(tcp_port))
231
232 i = self.handle.expect([
233 "Error",
234 "onos>" ])
235
236 #Clear handle to get previous output
237 self.handle.sendline("")
238 self.handle.expect("onos>")
239
240 handle = self.handle.before
241
242 if i == 0:
243 main.log.error("Error in adding node")
244 main.log.error(handle)
245 return main.FALSE
246 else:
247 main.log.info("Node "+str(ONOS_ip)+" added")
248 return main.TRUE
249
250 except pexpect.EOF:
251 main.log.error(self.name + ": EOF exception found")
252 main.log.error(self.name + ": " + self.handle.before)
253 main.cleanup()
254 main.exit()
255 except:
256 main.log.info(self.name+" ::::::")
257 main.log.error( traceback.print_exc())
258 main.log.info(self.name+" ::::::")
259 main.cleanup()
260 main.exit()
261
andrewonlab86dc3082014-10-13 18:18:38 -0400262 def remove_node(self, node_id):
263 '''
264 Removes a cluster by ID
265 Issues command: 'remove-node [<node-id>]'
266 Required:
267 * node_id
268 '''
269 try:
270 self.handle.sendline("")
271 self.handle.expect("onos>")
272
273 self.handle.sendline("remove-node "+str(node_id))
274 self.handle.expect("onos>")
275
276 return main.TRUE
277
278 except pexpect.EOF:
279 main.log.error(self.name + ": EOF exception found")
280 main.log.error(self.name + ": " + self.handle.before)
281 main.cleanup()
282 main.exit()
283 except:
284 main.log.info(self.name+" ::::::")
285 main.log.error( traceback.print_exc())
286 main.log.info(self.name+" ::::::")
287 main.cleanup()
288 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400289
andrewonlab7c211572014-10-15 16:45:20 -0400290 def nodes(self):
291 '''
292 List the nodes currently visible
293 Issues command: 'nodes'
294 Returns: entire handle of list of nodes
295 '''
296 try:
297 self.handle.sendline("")
298 self.handle.expect("onos>")
299
300 self.handle.sendline("nodes")
301 self.handle.expect("onos>")
302
303 self.handle.sendline("")
304 self.handle.expect("onos>")
305
306 handle = self.handle.before
307
308 return handle
309
310 except pexpect.EOF:
311 main.log.error(self.name + ": EOF exception found")
312 main.log.error(self.name + ": " + self.handle.before)
313 main.cleanup()
314 main.exit()
315 except:
316 main.log.info(self.name+" ::::::")
317 main.log.error( traceback.print_exc())
318 main.log.info(self.name+" ::::::")
319 main.cleanup()
320 main.exit()
321
andrewonlab38d6ae22014-10-15 14:23:45 -0400322 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400323 '''
324 Shows the current state of the topology
325 by issusing command: 'onos> onos:topology'
326 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400327 try:
328 self.handle.sendline("")
329 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400330 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400331 self.handle.sendline("onos:topology")
332 self.handle.expect("onos>")
333
334 handle = self.handle.before
335
336 main.log.info("onos:topology returned: " +
337 str(handle))
338
339 return handle
340
341 except pexpect.EOF:
342 main.log.error(self.name + ": EOF exception found")
343 main.log.error(self.name + ": " + self.handle.before)
344 main.cleanup()
345 main.exit()
346 except:
347 main.log.info(self.name+" ::::::")
348 main.log.error( traceback.print_exc())
349 main.log.info(self.name+" ::::::")
350 main.cleanup()
351 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400352
353 def feature_install(self, feature_str):
354 '''
355 Installs a specified feature
356 by issuing command: 'onos> feature:install <feature_str>'
357 '''
358 try:
359 self.handle.sendline("")
360 self.handle.expect("onos>")
361
362 self.handle.sendline("feature:install "+str(feature_str))
363 self.handle.expect("onos>")
364
365 return main.TRUE
366
367 except pexpect.EOF:
368 main.log.error(self.name + ": EOF exception found")
369 main.log.error(self.name + ": " + self.handle.before)
370 main.cleanup()
371 main.exit()
372 except:
373 main.log.info(self.name+" ::::::")
374 main.log.error( traceback.print_exc())
375 main.log.info(self.name+" ::::::")
376 main.cleanup()
377 main.exit()
378
379 def feature_uninstall(self, feature_str):
380 '''
381 Uninstalls a specified feature
382 by issuing command: 'onos> feature:uninstall <feature_str>'
383 '''
384 try:
385 self.handle.sendline("")
386 self.handle.expect("onos>")
387
388 self.handle.sendline("feature:uninstall "+str(feature_str))
389 self.handle.expect("onos>")
390
391 return main.TRUE
392
393 except pexpect.EOF:
394 main.log.error(self.name + ": EOF exception found")
395 main.log.error(self.name + ": " + self.handle.before)
396 main.cleanup()
397 main.exit()
398 except:
399 main.log.info(self.name+" ::::::")
400 main.log.error( traceback.print_exc())
401 main.log.info(self.name+" ::::::")
402 main.cleanup()
403 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400404
Jon Halle8217482014-10-17 13:49:14 -0400405 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400406 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400407 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400408 Optional argument:
409 * grep_str - pass in a string to grep
410 '''
411 try:
412 self.handle.sendline("")
413 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400414
415 if json_format:
416 if not grep_str:
417 self.handle.sendline("devices -j")
418 self.handle.expect("devices -j")
419 self.handle.expect("onos>")
420 else:
421 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400422 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400423 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
424 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400425 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400426 '''
427 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
428 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 -0400429 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 -0400430 So we take off that escape sequence using
431 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
432 handle1 = ansi_escape.sub('', handle)
433 '''
434 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400435 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
436 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400437 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400438 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400439 else:
440 if not grep_str:
441 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400442 self.handle.expect("onos>")
443 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400444 self.handle.expect("onos>")
445 else:
446 self.handle.sendline("devices | grep '"+
447 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400448 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400449 self.handle.sendline("")
andrewonlab86dc3082014-10-13 18:18:38 -0400450 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400451 handle = self.handle.before
452 print "handle =",handle
453 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400454 except pexpect.EOF:
455 main.log.error(self.name + ": EOF exception found")
456 main.log.error(self.name + ": " + self.handle.before)
457 main.cleanup()
458 main.exit()
459 except:
460 main.log.info(self.name+" ::::::")
461 main.log.error( traceback.print_exc())
462 main.log.info(self.name+" ::::::")
463 main.cleanup()
464 main.exit()
465
Jon Halle8217482014-10-17 13:49:14 -0400466 def links(self, json_format=True, grep_str=""):
467 '''
468 Lists all core links
469 Optional argument:
470 * grep_str - pass in a string to grep
471 '''
472 try:
473 self.handle.sendline("")
474 self.handle.expect("onos>")
475
476 if json_format:
477 if not grep_str:
478 self.handle.sendline("links -j")
479 self.handle.expect("links -j")
480 self.handle.expect("onos>")
481 else:
482 self.handle.sendline("links -j | grep '"+
483 str(grep_str)+"'")
484 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
485 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400486 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400487 '''
488 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
489 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 -0400490 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 -0400491 So we take off that escape sequence using
492 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
493 handle1 = ansi_escape.sub('', handle)
494 '''
495 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400496 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
497 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400498 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400499 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400500 else:
501 if not grep_str:
502 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400503 self.handle.expect("onos>")
504 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400505 self.handle.expect("onos>")
506 else:
507 self.handle.sendline("links | grep '"+
508 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400509 self.handle.expect("onos>")
510 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400511 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400512 handle = self.handle.before
513 print "handle =",handle
514 return handle
Jon Halle8217482014-10-17 13:49:14 -0400515 except pexpect.EOF:
516 main.log.error(self.name + ": EOF exception found")
517 main.log.error(self.name + ": " + self.handle.before)
518 main.cleanup()
519 main.exit()
520 except:
521 main.log.info(self.name+" ::::::")
522 main.log.error( traceback.print_exc())
523 main.log.info(self.name+" ::::::")
524 main.cleanup()
525 main.exit()
526
527
528 def ports(self, json_format=True, grep_str=""):
529 '''
530 Lists all ports
531 Optional argument:
532 * grep_str - pass in a string to grep
533 '''
534 try:
535 self.handle.sendline("")
536 self.handle.expect("onos>")
537
538 if json_format:
539 if not grep_str:
540 self.handle.sendline("ports -j")
541 self.handle.expect("ports -j")
542 self.handle.expect("onos>")
543 else:
544 self.handle.sendline("ports -j | grep '"+
545 str(grep_str)+"'")
546 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
547 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400548 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400549 '''
550 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
551 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 -0400552 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 -0400553 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400554 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
555 handle1 = ansi_escape.sub('', handle)
556 '''
557 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400558 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
559 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400560 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400561 return handle1
562
Jon Halle8217482014-10-17 13:49:14 -0400563 else:
564 if not grep_str:
565 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400566 self.handle.expect("onos>")
567 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400568 self.handle.expect("onos>")
569 else:
570 self.handle.sendline("ports | grep '"+
571 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400572 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400573 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400574 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400575 handle = self.handle.before
576 print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400577 return handle
Jon Halle8217482014-10-17 13:49:14 -0400578 except pexpect.EOF:
579 main.log.error(self.name + ": EOF exception found")
580 main.log.error(self.name + ": " + self.handle.before)
581 main.cleanup()
582 main.exit()
583 except:
584 main.log.info(self.name+" ::::::")
585 main.log.error( traceback.print_exc())
586 main.log.info(self.name+" ::::::")
587 main.cleanup()
588 main.exit()
589
590
andrewonlab7c211572014-10-15 16:45:20 -0400591 def device_role(self, device_id, node_id, role):
592 '''
593 Set device role for specified device and node with role
594 Required:
595 * device_id : may be obtained by function get_all_devices_id
596 * node_id : may be obtained by function get_all_nodes_id
597 * role: specify one of the following roles:
598 - master
599 - standby
600 - none
601 '''
602 try:
603 self.handle.sendline("")
604 self.handle.expect("onos>")
605
606 self.handle.sendline("device-role "+
607 str(device_id) + " " +
608 str(node_id) + " " +
609 str(role))
610 i = self.handle.expect([
611 "Error",
612 "onos>"])
613
614 self.handle.sendline("")
615 self.handle.expect("onos>")
616
617 handle = self.handle.before
618
619 if i == 0:
620 main.log.error("device-role command returned error")
621 return handle
622 else:
623 return main.TRUE
624
andrewonlab86dc3082014-10-13 18:18:38 -0400625 except pexpect.EOF:
626 main.log.error(self.name + ": EOF exception found")
627 main.log.error(self.name + ": " + self.handle.before)
628 main.cleanup()
629 main.exit()
630 except:
631 main.log.info(self.name+" ::::::")
632 main.log.error( traceback.print_exc())
633 main.log.info(self.name+" ::::::")
634 main.cleanup()
635 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400636
andrewonlab3e15ead2014-10-15 14:21:34 -0400637 def paths(self, src_id, dst_id):
638 '''
639 Returns string of paths, and the cost.
640 Issues command: onos:paths <src> <dst>
641 '''
642 try:
643 self.handle.sendline("")
644 self.handle.expect("onos>")
645
646 self.handle.sendline("onos:paths "+
647 str(src_id) + " " + str(dst_id))
648 i = self.handle.expect([
649 "Error",
650 "onos>"])
651
652 self.handle.sendline("")
653 self.handle.expect("onos>")
654
655 handle = self.handle.before
656
657 if i == 0:
658 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400659 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400660 else:
661 path = handle.split(";")[0]
662 cost = handle.split(";")[1]
663 return (path, cost)
664
665 except pexpect.EOF:
666 main.log.error(self.name + ": EOF exception found")
667 main.log.error(self.name + ": " + self.handle.before)
668 main.cleanup()
669 main.exit()
670 except:
671 main.log.info(self.name+" ::::::")
672 main.log.error( traceback.print_exc())
673 main.log.info(self.name+" ::::::")
674 main.cleanup()
675 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400676
677 #TODO:
678 #def hosts(self):
679
680 def get_hosts_id(self, host_list):
681 '''
682 Obtain list of hosts
683 Issues command: 'onos> hosts'
684
685 Required:
686 * host_list: List of hosts obtained by Mininet
687 IMPORTANT:
688 This function assumes that you started your
689 topology with the option '--mac'.
690 Furthermore, it assumes that value of VLAN is '-1'
691 Description:
692 Converts mininet hosts (h1, h2, h3...) into
693 ONOS format (00:00:00:00:00:01/-1 , ...)
694 '''
695
696 try:
Shreya Shahd7310c52014-10-20 16:44:37 -0400697 #self.handle.sendline("")
698 #self.handle.expect("onos>")
andrewonlab3f0a4af2014-10-17 12:25:14 -0400699
700 onos_host_list = []
701
702 for host in host_list:
703 host = host.replace("h", "")
704 host_hex = hex(int(host)).zfill(12)
705 host_hex = str(host_hex).replace('x','0')
706 i = iter(str(host_hex))
707 host_hex = ":".join(a+b for a,b in zip(i,i))
708 host_hex = host_hex + "/-1"
709 onos_host_list.append(host_hex)
710
711 return onos_host_list
712
713 except pexpect.EOF:
714 main.log.error(self.name + ": EOF exception found")
715 main.log.error(self.name + ": " + self.handle.before)
716 main.cleanup()
717 main.exit()
718 except:
719 main.log.info(self.name+" ::::::")
720 main.log.error( traceback.print_exc())
721 main.log.info(self.name+" ::::::")
722 main.cleanup()
723 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400724
andrewonlabe6745342014-10-17 14:29:13 -0400725 def add_host_intent(self, host_id_one, host_id_two):
726 '''
727 Required:
728 * host_id_one: ONOS host id for host1
729 * host_id_two: ONOS host id for host2
730 Description:
731 Adds a host-to-host intent (bidrectional) by
732 specifying the two hosts.
733 '''
734 try:
735 self.handle.sendline("")
736 self.handle.expect("onos>")
737
738 self.handle.sendline("add-host-intent "+
739 str(host_id_one) + " " + str(host_id_two))
740 self.handle.expect("onos>")
741
742 self.handle.sendline("")
743 self.handle.expect("onos>")
744
745 handle = self.handle.before
746
747 main.log.info("Intent installed between "+
748 str(host_id_one) + " and " + str(host_id_two))
749
750 return handle
751
752 except pexpect.EOF:
753 main.log.error(self.name + ": EOF exception found")
754 main.log.error(self.name + ": " + self.handle.before)
755 main.cleanup()
756 main.exit()
757 except:
758 main.log.info(self.name+" ::::::")
759 main.log.error( traceback.print_exc())
760 main.log.info(self.name+" ::::::")
761 main.cleanup()
762 main.exit()
763
andrewonlab4dbb4d82014-10-17 18:22:31 -0400764 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400765 egress_device, port_egress, ethType="", ethSrc="",
766 ethDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400767 '''
768 Required:
769 * ingress_device: device id of ingress device
770 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400771 Optional:
772 * ethType: specify ethType
773 * ethSrc: specify ethSrc (i.e. src mac addr)
774 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400775 Description:
776 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400777 specifying device id's and optional fields
778
andrewonlab4dbb4d82014-10-17 18:22:31 -0400779 NOTE: This function may change depending on the
780 options developers provide for point-to-point
781 intent via cli
782 '''
783 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400784 cmd = ""
785
786 #If there are no optional arguments
787 if not ethType and not ethSrc and not ethDst:
788 cmd = "add-point-intent "+\
789 str(ingress_device) + "/" + str(port_ingress) + " " +\
790 str(egress_device) + "/" + str(port_egress)
791
792 else:
andrewonlab9a130be2014-10-22 12:44:56 -0400793 cmd = "add-point-intent "
794
andrewonlab0c0a6772014-10-22 12:31:18 -0400795 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -0400796 cmd += " --ethType " + str(ethType)
797 if ethSrc:
798 cmd += " --ethSrc " + str(ethSrc)
799 if ethDst:
800 cmd += " --ethDst " + str(ethDst)
andrewonlab9a130be2014-10-22 12:44:56 -0400801
802 cmd += " "+str(ingress_device) + "/" + str(port_ingress) + " " +\
803 str(egress_device) + "/" + str(port_egress)
andrewonlab289e4b72014-10-21 21:24:18 -0400804
andrewonlab4dbb4d82014-10-17 18:22:31 -0400805 self.handle.sendline("")
806 self.handle.expect("onos>")
807
andrewonlab289e4b72014-10-21 21:24:18 -0400808 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400809 i = self.handle.expect([
810 "Error",
811 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -0400812
andrewonlab4dbb4d82014-10-17 18:22:31 -0400813 self.handle.sendline("")
814 self.handle.expect("onos>")
815
816 handle = self.handle.before
817
818 if i == 0:
819 main.log.error("Error in adding point-to-point intent")
820 return handle
821 else:
822 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -0400823
andrewonlab4dbb4d82014-10-17 18:22:31 -0400824 except pexpect.EOF:
825 main.log.error(self.name + ": EOF exception found")
826 main.log.error(self.name + ": " + self.handle.before)
827 main.cleanup()
828 main.exit()
829 except:
830 main.log.info(self.name+" ::::::")
831 main.log.error( traceback.print_exc())
832 main.log.info(self.name+" ::::::")
833 main.cleanup()
834 main.exit()
835
andrewonlab9a50dfe2014-10-17 17:22:31 -0400836 def remove_intent(self, intent_id):
837 '''
838 Remove intent for specified intent id
839 '''
840 try:
841 self.handle.sendline("")
842 self.handle.expect("onos>")
843
844 self.handle.sendline("remove-intent "+str(intent_id))
845 i = self.handle.expect([
846 "Error",
847 "onos>"])
848
849 handle = self.handle.before
850
851 if i == 0:
852 main.log.error("Error in removing intent")
853 return handle
854 else:
855 return handle
856
857 except pexpect.EOF:
858 main.log.error(self.name + ": EOF exception found")
859 main.log.error(self.name + ": " + self.handle.before)
860 main.cleanup()
861 main.exit()
862 except:
863 main.log.info(self.name+" ::::::")
864 main.log.error( traceback.print_exc())
865 main.log.info(self.name+" ::::::")
866 main.cleanup()
867 main.exit()
868
andrewonlab377693f2014-10-21 16:00:30 -0400869 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -0400870 '''
andrewonlab377693f2014-10-21 16:00:30 -0400871 Optional:
872 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -0400873 Description:
874 Obtain intents currently installed
875 '''
876 try:
andrewonlab377693f2014-10-21 16:00:30 -0400877 if json_format:
878 self.handle.sendline("intents -j")
879 self.handle.expect("intents -j")
880 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -0400881
andrewonlab377693f2014-10-21 16:00:30 -0400882 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -0400883
andrewonlab377693f2014-10-21 16:00:30 -0400884 else:
885 self.handle.sendline("")
886 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -0400887
andrewonlab377693f2014-10-21 16:00:30 -0400888 self.handle.sendline("intents")
889 self.handle.expect("onos>")
890
891 self.handle.sendline("")
892 self.handle.expect("onos>")
893
894 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -0400895
896 return handle
897
898 except pexpect.EOF:
899 main.log.error(self.name + ": EOF exception found")
900 main.log.error(self.name + ": " + self.handle.before)
901 main.cleanup()
902 main.exit()
903 except:
904 main.log.info(self.name+" ::::::")
905 main.log.error( traceback.print_exc())
906 main.log.info(self.name+" ::::::")
907 main.cleanup()
908 main.exit()
909
andrewonlab867212a2014-10-22 20:13:38 -0400910 def topology_events_metrics(self, json_format=True):
911 '''
912 Description:Returns topology metrics
913 Optional:
914 * json_format: enable json formatting of output
915 '''
916 try:
917 if json_format:
918 self.handle.sendline("topology-events-metrics -j")
919 self.handle.expect("topology-events-metrics -j")
920 self.handle.expect("onos>")
921
922 handle = self.handle.before
923
924 #Some color thing that we want to escape
925 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
926 handle = ansi_escape.sub('', handle)
927
928 else:
929 self.handle.sendline("topology-events-metrics")
930 self.handle.expect("topology-events-metrics")
931 self.handle.expect("onos>")
932
933 handle = self.handle.before
934
935 return handle
936
937 except pexpect.EOF:
938 main.log.error(self.name + ": EOF exception found")
939 main.log.error(self.name + ": " + self.handle.before)
940 main.cleanup()
941 main.exit()
942 except:
943 main.log.info(self.name+" ::::::")
944 main.log.error( traceback.print_exc())
945 main.log.info(self.name+" ::::::")
946 main.cleanup()
947 main.exit()
948
andrewonlab3e15ead2014-10-15 14:21:34 -0400949 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -0400950 #Wrapper functions use existing driver
951 #functions and extends their use case.
952 #For example, we may use the output of
953 #a normal driver function, and parse it
954 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -0400955
andrewonlab9a50dfe2014-10-17 17:22:31 -0400956 def get_all_intents_id(self):
957 '''
958 Description:
959 Obtain all intent id's in a list
960 '''
961 try:
962 #Obtain output of intents function
963 intents_str = self.intents()
964 all_intent_list = []
965 intent_id_list = []
966
967 #Parse the intents output for ID's
968 intents_list = [s.strip() for s in intents_str.splitlines()]
969 for intents in intents_list:
970 if "onos>" in intents:
971 continue
972 elif "intents" in intents:
973 continue
974 else:
975 line_list = intents.split(" ")
976 all_intent_list.append(line_list[0])
977
978 all_intent_list = all_intent_list[1:-2]
979
980 for intents in all_intent_list:
981 if not intents:
982 continue
983 else:
984 intent_id_list.append(intents)
985
986 return intent_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
andrewonlab7e4d2d32014-10-15 13:23:21 -04001000 def get_all_devices_id(self):
1001 '''
1002 Use 'devices' function to obtain list of all devices
1003 and parse the result to obtain a list of all device
1004 id's. Returns this list. Returns empty list if no
1005 devices exist
1006 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001007
1008 This function may be useful if you are not sure of the
1009 device id, and wish to execute other commands using
1010 the ids. By obtaining the list of device ids on the fly,
1011 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001012 '''
1013 try:
1014 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001015 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001016 id_list = []
1017
1018 if not devices_str:
1019 main.log.info("There are no devices to get id from")
1020 return id_list
1021
1022 #Split the string into list by comma
1023 device_list = devices_str.split(",")
1024 #Get temporary list of all arguments with string 'id='
1025 temp_list = [dev for dev in device_list if "id=" in dev]
1026 #Split list further into arguments before and after string
1027 # 'id='. Get the latter portion (the actual device id) and
1028 # append to id_list
1029 for arg in temp_list:
1030 id_list.append(arg.split("id=")[1])
1031
1032 return id_list
1033
1034 except pexpect.EOF:
1035 main.log.error(self.name + ": EOF exception found")
1036 main.log.error(self.name + ": " + self.handle.before)
1037 main.cleanup()
1038 main.exit()
1039 except:
1040 main.log.info(self.name+" ::::::")
1041 main.log.error( traceback.print_exc())
1042 main.log.info(self.name+" ::::::")
1043 main.cleanup()
1044 main.exit()
1045
andrewonlab7c211572014-10-15 16:45:20 -04001046 def get_all_nodes_id(self):
1047 '''
1048 Uses 'nodes' function to obtain list of all nodes
1049 and parse the result of nodes to obtain just the
1050 node id's.
1051 Returns:
1052 list of node id's
1053 '''
1054 try:
1055 nodes_str = self.nodes()
1056 id_list = []
1057
1058 if not nodes_str:
1059 main.log.info("There are no nodes to get id from")
1060 return id_list
1061
1062 #Sample nodes_str output
1063 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1064
1065 #Split the string into list by comma
1066 nodes_list = nodes_str.split(",")
1067 temp_list = [node for node in nodes_list if "id=" in node]
1068 for arg in temp_list:
1069 id_list.append(arg.split("id=")[1])
1070
1071 return id_list
1072
1073 except pexpect.EOF:
1074 main.log.error(self.name + ": EOF exception found")
1075 main.log.error(self.name + ": " + self.handle.before)
1076 main.cleanup()
1077 main.exit()
1078 except:
1079 main.log.info(self.name+" ::::::")
1080 main.log.error( traceback.print_exc())
1081 main.log.info(self.name+" ::::::")
1082 main.cleanup()
1083 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001084
Jon Halla91c4dc2014-10-22 12:57:04 -04001085 def get_device(self, dpid=None):
1086 '''
1087 Return the first device from the devices api whose 'id' contains 'dpid'
1088 Return None if there is no match
1089 '''
1090 import json
1091 try:
1092 if dpid == None:
1093 return None
1094 else:
1095 dpid = dpid.replace(':', '')
1096 raw_devices = self.devices()
1097 devices_json = json.loads(raw_devices)
1098 #search json for the device with dpid then return the device
1099 for device in devices_json:
1100 #print "%s in %s?" % (dpid, device['id'])
1101 if dpid in device['id']:
1102 return device
1103 return None
1104 except pexpect.EOF:
1105 main.log.error(self.name + ": EOF exception found")
1106 main.log.error(self.name + ": " + self.handle.before)
1107 main.cleanup()
1108 main.exit()
1109 except:
1110 main.log.info(self.name+" ::::::")
1111 main.log.error( traceback.print_exc())
1112 main.log.info(self.name+" ::::::")
1113 main.cleanup()
1114 main.exit()
1115
andrewonlab7e4d2d32014-10-15 13:23:21 -04001116 #***********************************