blob: 8f7c746f1ff499ece82ebf651fc56b3b908e9212 [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))
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400154 i = self.handle.expect([
155 "onos>",
156 pexpect.TIMEOUT],timeout=60)
157
158 if i == 0:
159 main.log.info(str(ONOS_ip)+" CLI Started successfully")
160 return main.TRUE
161 else:
162 main.log.error("Connection to CLI "+\
163 str(ONOS_ip)+" timeout")
164 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400165
166 except pexpect.EOF:
167 main.log.error(self.name + ": EOF exception found")
168 main.log.error(self.name + ": " + self.handle.before)
169 main.cleanup()
170 main.exit()
171 except:
172 main.log.info(self.name+" ::::::")
173 main.log.error( traceback.print_exc())
174 main.log.info(self.name+" ::::::")
175 main.cleanup()
176 main.exit()
177
andrewonlaba18f6bf2014-10-13 19:31:54 -0400178 def sendline(self, cmd_str):
179 '''
180 Send a completely user specified string to
181 the onos> prompt. Use this function if you have
182 a very specific command to send.
183
184 Warning: There are no sanity checking to commands
185 sent using this method.
186 '''
187 try:
188 self.handle.sendline("")
189 self.handle.expect("onos>")
190
191 self.handle.sendline(cmd_str)
192 self.handle.expect("onos>")
193
194 handle = self.handle.before
195
196 self.handle.sendline("")
197 self.handle.expect("onos>")
198
199 handle += self.handle.before
200 handle += self.handle.after
201
202 main.log.info("Command sent.")
203
204 return handle
205 except pexpect.EOF:
206 main.log.error(self.name + ": EOF exception found")
207 main.log.error(self.name + ": " + self.handle.before)
208 main.cleanup()
209 main.exit()
210 except:
211 main.log.info(self.name+" ::::::")
212 main.log.error( traceback.print_exc())
213 main.log.info(self.name+" ::::::")
214 main.cleanup()
215 main.exit()
216
andrewonlab95ce8322014-10-13 14:12:04 -0400217 #IMPORTANT NOTE:
218 #For all cli commands, naming convention should match
219 #the cli command replacing ':' with '_'.
220 #Ex) onos:topology > onos_topology
221 # onos:links > onos_links
222 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400223
224 def add_node(self, node_id, ONOS_ip, tcp_port=""):
225 '''
226 Adds a new cluster node by ID and address information.
227 Required:
228 * node_id
229 * ONOS_ip
230 Optional:
231 * tcp_port
232 '''
233 try:
234 self.handle.sendline("")
235 self.handle.expect("onos>")
236
237 self.handle.sendline("add-node "+
238 str(node_id)+" "+
239 str(ONOS_ip)+" "+
240 str(tcp_port))
241
242 i = self.handle.expect([
243 "Error",
244 "onos>" ])
245
246 #Clear handle to get previous output
247 self.handle.sendline("")
248 self.handle.expect("onos>")
249
250 handle = self.handle.before
251
252 if i == 0:
253 main.log.error("Error in adding node")
254 main.log.error(handle)
255 return main.FALSE
256 else:
257 main.log.info("Node "+str(ONOS_ip)+" added")
258 return main.TRUE
259
260 except pexpect.EOF:
261 main.log.error(self.name + ": EOF exception found")
262 main.log.error(self.name + ": " + self.handle.before)
263 main.cleanup()
264 main.exit()
265 except:
266 main.log.info(self.name+" ::::::")
267 main.log.error( traceback.print_exc())
268 main.log.info(self.name+" ::::::")
269 main.cleanup()
270 main.exit()
271
andrewonlab86dc3082014-10-13 18:18:38 -0400272 def remove_node(self, node_id):
273 '''
274 Removes a cluster by ID
275 Issues command: 'remove-node [<node-id>]'
276 Required:
277 * node_id
278 '''
279 try:
280 self.handle.sendline("")
281 self.handle.expect("onos>")
282
283 self.handle.sendline("remove-node "+str(node_id))
284 self.handle.expect("onos>")
285
286 return main.TRUE
287
288 except pexpect.EOF:
289 main.log.error(self.name + ": EOF exception found")
290 main.log.error(self.name + ": " + self.handle.before)
291 main.cleanup()
292 main.exit()
293 except:
294 main.log.info(self.name+" ::::::")
295 main.log.error( traceback.print_exc())
296 main.log.info(self.name+" ::::::")
297 main.cleanup()
298 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400299
andrewonlab7c211572014-10-15 16:45:20 -0400300 def nodes(self):
301 '''
302 List the nodes currently visible
303 Issues command: 'nodes'
304 Returns: entire handle of list of nodes
305 '''
306 try:
307 self.handle.sendline("")
308 self.handle.expect("onos>")
309
310 self.handle.sendline("nodes")
311 self.handle.expect("onos>")
312
313 self.handle.sendline("")
314 self.handle.expect("onos>")
315
316 handle = self.handle.before
317
318 return handle
319
320 except pexpect.EOF:
321 main.log.error(self.name + ": EOF exception found")
322 main.log.error(self.name + ": " + self.handle.before)
323 main.cleanup()
324 main.exit()
325 except:
326 main.log.info(self.name+" ::::::")
327 main.log.error( traceback.print_exc())
328 main.log.info(self.name+" ::::::")
329 main.cleanup()
330 main.exit()
331
andrewonlab38d6ae22014-10-15 14:23:45 -0400332 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400333 '''
334 Shows the current state of the topology
335 by issusing command: 'onos> onos:topology'
336 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400337 try:
338 self.handle.sendline("")
339 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400340 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400341 self.handle.sendline("onos:topology")
342 self.handle.expect("onos>")
343
344 handle = self.handle.before
345
346 main.log.info("onos:topology returned: " +
347 str(handle))
348
349 return handle
350
351 except pexpect.EOF:
352 main.log.error(self.name + ": EOF exception found")
353 main.log.error(self.name + ": " + self.handle.before)
354 main.cleanup()
355 main.exit()
356 except:
357 main.log.info(self.name+" ::::::")
358 main.log.error( traceback.print_exc())
359 main.log.info(self.name+" ::::::")
360 main.cleanup()
361 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400362
363 def feature_install(self, feature_str):
364 '''
365 Installs a specified feature
366 by issuing command: 'onos> feature:install <feature_str>'
367 '''
368 try:
369 self.handle.sendline("")
370 self.handle.expect("onos>")
371
372 self.handle.sendline("feature:install "+str(feature_str))
373 self.handle.expect("onos>")
374
375 return main.TRUE
376
377 except pexpect.EOF:
378 main.log.error(self.name + ": EOF exception found")
379 main.log.error(self.name + ": " + self.handle.before)
380 main.cleanup()
381 main.exit()
382 except:
383 main.log.info(self.name+" ::::::")
384 main.log.error( traceback.print_exc())
385 main.log.info(self.name+" ::::::")
386 main.cleanup()
387 main.exit()
388
389 def feature_uninstall(self, feature_str):
390 '''
391 Uninstalls a specified feature
392 by issuing command: 'onos> feature:uninstall <feature_str>'
393 '''
394 try:
395 self.handle.sendline("")
396 self.handle.expect("onos>")
397
398 self.handle.sendline("feature:uninstall "+str(feature_str))
399 self.handle.expect("onos>")
400
401 return main.TRUE
402
403 except pexpect.EOF:
404 main.log.error(self.name + ": EOF exception found")
405 main.log.error(self.name + ": " + self.handle.before)
406 main.cleanup()
407 main.exit()
408 except:
409 main.log.info(self.name+" ::::::")
410 main.log.error( traceback.print_exc())
411 main.log.info(self.name+" ::::::")
412 main.cleanup()
413 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400414
Jon Halle8217482014-10-17 13:49:14 -0400415 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400416 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400417 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400418 Optional argument:
419 * grep_str - pass in a string to grep
420 '''
421 try:
422 self.handle.sendline("")
423 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400424
425 if json_format:
426 if not grep_str:
427 self.handle.sendline("devices -j")
428 self.handle.expect("devices -j")
429 self.handle.expect("onos>")
430 else:
431 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400432 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400433 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
434 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400435 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400436 '''
437 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
438 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 -0400439 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 -0400440 So we take off that escape sequence using
441 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
442 handle1 = ansi_escape.sub('', handle)
443 '''
444 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400445 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
446 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400447 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400448 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400449 else:
450 if not grep_str:
451 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400452 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400453 else:
454 self.handle.sendline("devices | grep '"+
455 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400456 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400457 handle = self.handle.before
458 print "handle =",handle
459 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400460 except pexpect.EOF:
461 main.log.error(self.name + ": EOF exception found")
462 main.log.error(self.name + ": " + self.handle.before)
463 main.cleanup()
464 main.exit()
465 except:
466 main.log.info(self.name+" ::::::")
467 main.log.error( traceback.print_exc())
468 main.log.info(self.name+" ::::::")
469 main.cleanup()
470 main.exit()
471
Jon Halle8217482014-10-17 13:49:14 -0400472 def links(self, json_format=True, grep_str=""):
473 '''
474 Lists all core links
475 Optional argument:
476 * grep_str - pass in a string to grep
477 '''
478 try:
479 self.handle.sendline("")
480 self.handle.expect("onos>")
481
482 if json_format:
483 if not grep_str:
484 self.handle.sendline("links -j")
485 self.handle.expect("links -j")
486 self.handle.expect("onos>")
487 else:
488 self.handle.sendline("links -j | grep '"+
489 str(grep_str)+"'")
490 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
491 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400492 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400493 '''
494 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
495 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 -0400496 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 -0400497 So we take off that escape sequence using
498 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
499 handle1 = ansi_escape.sub('', handle)
500 '''
501 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400502 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
503 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400504 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400505 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400506 else:
507 if not grep_str:
508 self.handle.sendline("links")
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>")
512 else:
513 self.handle.sendline("links | grep '"+
514 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400515 self.handle.expect("onos>")
516 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400517 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400518 handle = self.handle.before
519 print "handle =",handle
520 return handle
Jon Halle8217482014-10-17 13:49:14 -0400521 except pexpect.EOF:
522 main.log.error(self.name + ": EOF exception found")
523 main.log.error(self.name + ": " + self.handle.before)
524 main.cleanup()
525 main.exit()
526 except:
527 main.log.info(self.name+" ::::::")
528 main.log.error( traceback.print_exc())
529 main.log.info(self.name+" ::::::")
530 main.cleanup()
531 main.exit()
532
533
534 def ports(self, json_format=True, grep_str=""):
535 '''
536 Lists all ports
537 Optional argument:
538 * grep_str - pass in a string to grep
539 '''
540 try:
541 self.handle.sendline("")
542 self.handle.expect("onos>")
543
544 if json_format:
545 if not grep_str:
546 self.handle.sendline("ports -j")
547 self.handle.expect("ports -j")
548 self.handle.expect("onos>")
549 else:
550 self.handle.sendline("ports -j | grep '"+
551 str(grep_str)+"'")
552 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
553 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400554 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400555 '''
556 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
557 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 -0400558 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 -0400559 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400560 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
561 handle1 = ansi_escape.sub('', handle)
562 '''
563 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400564 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
565 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400566 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400567 return handle1
568
Jon Halle8217482014-10-17 13:49:14 -0400569 else:
570 if not grep_str:
571 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400572 self.handle.expect("onos>")
573 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400574 self.handle.expect("onos>")
575 else:
576 self.handle.sendline("ports | grep '"+
577 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400578 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400579 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400580 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400581 handle = self.handle.before
582 print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400583 return handle
Jon Halle8217482014-10-17 13:49:14 -0400584 except pexpect.EOF:
585 main.log.error(self.name + ": EOF exception found")
586 main.log.error(self.name + ": " + self.handle.before)
587 main.cleanup()
588 main.exit()
589 except:
590 main.log.info(self.name+" ::::::")
591 main.log.error( traceback.print_exc())
592 main.log.info(self.name+" ::::::")
593 main.cleanup()
594 main.exit()
595
596
andrewonlab7c211572014-10-15 16:45:20 -0400597 def device_role(self, device_id, node_id, role):
598 '''
599 Set device role for specified device and node with role
600 Required:
601 * device_id : may be obtained by function get_all_devices_id
602 * node_id : may be obtained by function get_all_nodes_id
603 * role: specify one of the following roles:
604 - master
605 - standby
606 - none
607 '''
608 try:
609 self.handle.sendline("")
610 self.handle.expect("onos>")
611
612 self.handle.sendline("device-role "+
613 str(device_id) + " " +
614 str(node_id) + " " +
615 str(role))
616 i = self.handle.expect([
617 "Error",
618 "onos>"])
619
620 self.handle.sendline("")
621 self.handle.expect("onos>")
622
623 handle = self.handle.before
624
625 if i == 0:
626 main.log.error("device-role command returned error")
627 return handle
628 else:
629 return main.TRUE
630
andrewonlab86dc3082014-10-13 18:18:38 -0400631 except pexpect.EOF:
632 main.log.error(self.name + ": EOF exception found")
633 main.log.error(self.name + ": " + self.handle.before)
634 main.cleanup()
635 main.exit()
636 except:
637 main.log.info(self.name+" ::::::")
638 main.log.error( traceback.print_exc())
639 main.log.info(self.name+" ::::::")
640 main.cleanup()
641 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400642
andrewonlab3e15ead2014-10-15 14:21:34 -0400643 def paths(self, src_id, dst_id):
644 '''
645 Returns string of paths, and the cost.
646 Issues command: onos:paths <src> <dst>
647 '''
648 try:
649 self.handle.sendline("")
650 self.handle.expect("onos>")
651
652 self.handle.sendline("onos:paths "+
653 str(src_id) + " " + str(dst_id))
654 i = self.handle.expect([
655 "Error",
656 "onos>"])
657
658 self.handle.sendline("")
659 self.handle.expect("onos>")
660
661 handle = self.handle.before
662
663 if i == 0:
664 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400665 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400666 else:
667 path = handle.split(";")[0]
668 cost = handle.split(";")[1]
669 return (path, cost)
670
671 except pexpect.EOF:
672 main.log.error(self.name + ": EOF exception found")
673 main.log.error(self.name + ": " + self.handle.before)
674 main.cleanup()
675 main.exit()
676 except:
677 main.log.info(self.name+" ::::::")
678 main.log.error( traceback.print_exc())
679 main.log.info(self.name+" ::::::")
680 main.cleanup()
681 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400682
683 #TODO:
684 #def hosts(self):
685
686 def get_hosts_id(self, host_list):
687 '''
688 Obtain list of hosts
689 Issues command: 'onos> hosts'
690
691 Required:
692 * host_list: List of hosts obtained by Mininet
693 IMPORTANT:
694 This function assumes that you started your
695 topology with the option '--mac'.
696 Furthermore, it assumes that value of VLAN is '-1'
697 Description:
698 Converts mininet hosts (h1, h2, h3...) into
699 ONOS format (00:00:00:00:00:01/-1 , ...)
700 '''
701
702 try:
Shreya Shahd7310c52014-10-20 16:44:37 -0400703 #self.handle.sendline("")
704 #self.handle.expect("onos>")
andrewonlab3f0a4af2014-10-17 12:25:14 -0400705
706 onos_host_list = []
707
708 for host in host_list:
709 host = host.replace("h", "")
710 host_hex = hex(int(host)).zfill(12)
711 host_hex = str(host_hex).replace('x','0')
712 i = iter(str(host_hex))
713 host_hex = ":".join(a+b for a,b in zip(i,i))
714 host_hex = host_hex + "/-1"
715 onos_host_list.append(host_hex)
716
717 return onos_host_list
718
719 except pexpect.EOF:
720 main.log.error(self.name + ": EOF exception found")
721 main.log.error(self.name + ": " + self.handle.before)
722 main.cleanup()
723 main.exit()
724 except:
725 main.log.info(self.name+" ::::::")
726 main.log.error( traceback.print_exc())
727 main.log.info(self.name+" ::::::")
728 main.cleanup()
729 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400730
andrewonlabe6745342014-10-17 14:29:13 -0400731 def add_host_intent(self, host_id_one, host_id_two):
732 '''
733 Required:
734 * host_id_one: ONOS host id for host1
735 * host_id_two: ONOS host id for host2
736 Description:
737 Adds a host-to-host intent (bidrectional) by
738 specifying the two hosts.
739 '''
740 try:
741 self.handle.sendline("")
742 self.handle.expect("onos>")
743
744 self.handle.sendline("add-host-intent "+
745 str(host_id_one) + " " + str(host_id_two))
746 self.handle.expect("onos>")
747
748 self.handle.sendline("")
749 self.handle.expect("onos>")
750
751 handle = self.handle.before
752
753 main.log.info("Intent installed between "+
754 str(host_id_one) + " and " + str(host_id_two))
755
756 return handle
757
758 except pexpect.EOF:
759 main.log.error(self.name + ": EOF exception found")
760 main.log.error(self.name + ": " + self.handle.before)
761 main.cleanup()
762 main.exit()
763 except:
764 main.log.info(self.name+" ::::::")
765 main.log.error( traceback.print_exc())
766 main.log.info(self.name+" ::::::")
767 main.cleanup()
768 main.exit()
769
andrewonlab7b31d232014-10-24 13:31:47 -0400770 def add_optical_intent(self, ingress_device, egress_device):
771 '''
772 Required:
773 * ingress_device: device id of ingress device
774 * egress_device: device id of egress device
775 Optional:
776 TODO: Still needs to be implemented via dev side
777 '''
778 try:
779 self.handle.sendline("add-optical-intent "+
780 str(ingress_device) + " " + str(egress_device))
781 self.handle.expect("add-optical-intent")
782 i = self.handle.expect([
783 "Error",
784 "onos>"])
785
786 handle = self.handle.before
787
788 #If error, return error message
789 if i == 0:
790 return handle
791 else:
792 return main.TRUE
793
794 except pexpect.EOF:
795 main.log.error(self.name + ": EOF exception found")
796 main.log.error(self.name + ": " + self.handle.before)
797 main.cleanup()
798 main.exit()
799 except:
800 main.log.info(self.name+" ::::::")
801 main.log.error( traceback.print_exc())
802 main.log.info(self.name+" ::::::")
803 main.cleanup()
804 main.exit()
805
andrewonlab4dbb4d82014-10-17 18:22:31 -0400806 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400807 egress_device, port_egress, ethType="", ethSrc="",
808 ethDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400809 '''
810 Required:
811 * ingress_device: device id of ingress device
812 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400813 Optional:
814 * ethType: specify ethType
815 * ethSrc: specify ethSrc (i.e. src mac addr)
816 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400817 Description:
818 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400819 specifying device id's and optional fields
820
andrewonlab4dbb4d82014-10-17 18:22:31 -0400821 NOTE: This function may change depending on the
822 options developers provide for point-to-point
823 intent via cli
824 '''
825 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400826 cmd = ""
827
828 #If there are no optional arguments
829 if not ethType and not ethSrc and not ethDst:
830 cmd = "add-point-intent "+\
831 str(ingress_device) + "/" + str(port_ingress) + " " +\
832 str(egress_device) + "/" + str(port_egress)
833
834 else:
andrewonlab9a130be2014-10-22 12:44:56 -0400835 cmd = "add-point-intent "
836
andrewonlab0c0a6772014-10-22 12:31:18 -0400837 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -0400838 cmd += " --ethType " + str(ethType)
839 if ethSrc:
840 cmd += " --ethSrc " + str(ethSrc)
841 if ethDst:
842 cmd += " --ethDst " + str(ethDst)
andrewonlab9a130be2014-10-22 12:44:56 -0400843
844 cmd += " "+str(ingress_device) + "/" + str(port_ingress) + " " +\
845 str(egress_device) + "/" + str(port_egress)
andrewonlab289e4b72014-10-21 21:24:18 -0400846
andrewonlab4dbb4d82014-10-17 18:22:31 -0400847 self.handle.sendline("")
848 self.handle.expect("onos>")
849
andrewonlab289e4b72014-10-21 21:24:18 -0400850 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400851 i = self.handle.expect([
852 "Error",
853 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -0400854
andrewonlab4dbb4d82014-10-17 18:22:31 -0400855 self.handle.sendline("")
856 self.handle.expect("onos>")
857
858 handle = self.handle.before
859
860 if i == 0:
861 main.log.error("Error in adding point-to-point intent")
862 return handle
863 else:
864 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -0400865
andrewonlab4dbb4d82014-10-17 18:22:31 -0400866 except pexpect.EOF:
867 main.log.error(self.name + ": EOF exception found")
868 main.log.error(self.name + ": " + self.handle.before)
869 main.cleanup()
870 main.exit()
871 except:
872 main.log.info(self.name+" ::::::")
873 main.log.error( traceback.print_exc())
874 main.log.info(self.name+" ::::::")
875 main.cleanup()
876 main.exit()
877
andrewonlab9a50dfe2014-10-17 17:22:31 -0400878 def remove_intent(self, intent_id):
879 '''
880 Remove intent for specified intent id
881 '''
882 try:
883 self.handle.sendline("")
884 self.handle.expect("onos>")
885
886 self.handle.sendline("remove-intent "+str(intent_id))
887 i = self.handle.expect([
888 "Error",
889 "onos>"])
890
891 handle = self.handle.before
892
893 if i == 0:
894 main.log.error("Error in removing intent")
895 return handle
896 else:
897 return handle
898
899 except pexpect.EOF:
900 main.log.error(self.name + ": EOF exception found")
901 main.log.error(self.name + ": " + self.handle.before)
902 main.cleanup()
903 main.exit()
904 except:
905 main.log.info(self.name+" ::::::")
906 main.log.error( traceback.print_exc())
907 main.log.info(self.name+" ::::::")
908 main.cleanup()
909 main.exit()
910
andrewonlab377693f2014-10-21 16:00:30 -0400911 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -0400912 '''
andrewonlab377693f2014-10-21 16:00:30 -0400913 Optional:
914 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -0400915 Description:
916 Obtain intents currently installed
917 '''
918 try:
andrewonlab377693f2014-10-21 16:00:30 -0400919 if json_format:
920 self.handle.sendline("intents -j")
921 self.handle.expect("intents -j")
922 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -0400923
andrewonlab377693f2014-10-21 16:00:30 -0400924 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -0400925
andrewonlab377693f2014-10-21 16:00:30 -0400926 else:
927 self.handle.sendline("")
928 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -0400929
andrewonlab377693f2014-10-21 16:00:30 -0400930 self.handle.sendline("intents")
931 self.handle.expect("onos>")
932
933 self.handle.sendline("")
934 self.handle.expect("onos>")
935
936 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -0400937
938 return handle
939
940 except pexpect.EOF:
941 main.log.error(self.name + ": EOF exception found")
942 main.log.error(self.name + ": " + self.handle.before)
943 main.cleanup()
944 main.exit()
945 except:
946 main.log.info(self.name+" ::::::")
947 main.log.error( traceback.print_exc())
948 main.log.info(self.name+" ::::::")
949 main.cleanup()
950 main.exit()
951
andrewonlab867212a2014-10-22 20:13:38 -0400952 def topology_events_metrics(self, json_format=True):
953 '''
954 Description:Returns topology metrics
955 Optional:
956 * json_format: enable json formatting of output
957 '''
958 try:
959 if json_format:
960 self.handle.sendline("topology-events-metrics -j")
961 self.handle.expect("topology-events-metrics -j")
962 self.handle.expect("onos>")
963
964 handle = self.handle.before
965
966 #Some color thing that we want to escape
967 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
968 handle = ansi_escape.sub('', handle)
969
970 else:
971 self.handle.sendline("topology-events-metrics")
972 self.handle.expect("topology-events-metrics")
973 self.handle.expect("onos>")
974
975 handle = self.handle.before
976
977 return handle
978
979 except pexpect.EOF:
980 main.log.error(self.name + ": EOF exception found")
981 main.log.error(self.name + ": " + self.handle.before)
982 main.cleanup()
983 main.exit()
984 except:
985 main.log.info(self.name+" ::::::")
986 main.log.error( traceback.print_exc())
987 main.log.info(self.name+" ::::::")
988 main.cleanup()
989 main.exit()
990
andrewonlab3e15ead2014-10-15 14:21:34 -0400991 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -0400992 #Wrapper functions use existing driver
993 #functions and extends their use case.
994 #For example, we may use the output of
995 #a normal driver function, and parse it
996 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -0400997
andrewonlab9a50dfe2014-10-17 17:22:31 -0400998 def get_all_intents_id(self):
999 '''
1000 Description:
1001 Obtain all intent id's in a list
1002 '''
1003 try:
1004 #Obtain output of intents function
1005 intents_str = self.intents()
1006 all_intent_list = []
1007 intent_id_list = []
1008
1009 #Parse the intents output for ID's
1010 intents_list = [s.strip() for s in intents_str.splitlines()]
1011 for intents in intents_list:
1012 if "onos>" in intents:
1013 continue
1014 elif "intents" in intents:
1015 continue
1016 else:
1017 line_list = intents.split(" ")
1018 all_intent_list.append(line_list[0])
1019
1020 all_intent_list = all_intent_list[1:-2]
1021
1022 for intents in all_intent_list:
1023 if not intents:
1024 continue
1025 else:
1026 intent_id_list.append(intents)
1027
1028 return intent_id_list
1029
1030 except pexpect.EOF:
1031 main.log.error(self.name + ": EOF exception found")
1032 main.log.error(self.name + ": " + self.handle.before)
1033 main.cleanup()
1034 main.exit()
1035 except:
1036 main.log.info(self.name+" ::::::")
1037 main.log.error( traceback.print_exc())
1038 main.log.info(self.name+" ::::::")
1039 main.cleanup()
1040 main.exit()
1041
andrewonlab7e4d2d32014-10-15 13:23:21 -04001042 def get_all_devices_id(self):
1043 '''
1044 Use 'devices' function to obtain list of all devices
1045 and parse the result to obtain a list of all device
1046 id's. Returns this list. Returns empty list if no
1047 devices exist
1048 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001049
1050 This function may be useful if you are not sure of the
1051 device id, and wish to execute other commands using
1052 the ids. By obtaining the list of device ids on the fly,
1053 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001054 '''
1055 try:
1056 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001057 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001058 id_list = []
1059
1060 if not devices_str:
1061 main.log.info("There are no devices to get id from")
1062 return id_list
1063
1064 #Split the string into list by comma
1065 device_list = devices_str.split(",")
1066 #Get temporary list of all arguments with string 'id='
1067 temp_list = [dev for dev in device_list if "id=" in dev]
1068 #Split list further into arguments before and after string
1069 # 'id='. Get the latter portion (the actual device id) and
1070 # append to id_list
1071 for arg in temp_list:
1072 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001073 return id_list
1074
1075 except pexpect.EOF:
1076 main.log.error(self.name + ": EOF exception found")
1077 main.log.error(self.name + ": " + self.handle.before)
1078 main.cleanup()
1079 main.exit()
1080 except:
1081 main.log.info(self.name+" ::::::")
1082 main.log.error( traceback.print_exc())
1083 main.log.info(self.name+" ::::::")
1084 main.cleanup()
1085 main.exit()
1086
andrewonlab7c211572014-10-15 16:45:20 -04001087 def get_all_nodes_id(self):
1088 '''
1089 Uses 'nodes' function to obtain list of all nodes
1090 and parse the result of nodes to obtain just the
1091 node id's.
1092 Returns:
1093 list of node id's
1094 '''
1095 try:
1096 nodes_str = self.nodes()
1097 id_list = []
1098
1099 if not nodes_str:
1100 main.log.info("There are no nodes to get id from")
1101 return id_list
1102
1103 #Sample nodes_str output
1104 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1105
1106 #Split the string into list by comma
1107 nodes_list = nodes_str.split(",")
1108 temp_list = [node for node in nodes_list if "id=" in node]
1109 for arg in temp_list:
1110 id_list.append(arg.split("id=")[1])
1111
1112 return id_list
1113
1114 except pexpect.EOF:
1115 main.log.error(self.name + ": EOF exception found")
1116 main.log.error(self.name + ": " + self.handle.before)
1117 main.cleanup()
1118 main.exit()
1119 except:
1120 main.log.info(self.name+" ::::::")
1121 main.log.error( traceback.print_exc())
1122 main.log.info(self.name+" ::::::")
1123 main.cleanup()
1124 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001125
Jon Halla91c4dc2014-10-22 12:57:04 -04001126 def get_device(self, dpid=None):
1127 '''
1128 Return the first device from the devices api whose 'id' contains 'dpid'
1129 Return None if there is no match
1130 '''
1131 import json
1132 try:
1133 if dpid == None:
1134 return None
1135 else:
1136 dpid = dpid.replace(':', '')
1137 raw_devices = self.devices()
1138 devices_json = json.loads(raw_devices)
1139 #search json for the device with dpid then return the device
1140 for device in devices_json:
1141 #print "%s in %s?" % (dpid, device['id'])
1142 if dpid in device['id']:
1143 return device
1144 return None
1145 except pexpect.EOF:
1146 main.log.error(self.name + ": EOF exception found")
1147 main.log.error(self.name + ": " + self.handle.before)
1148 main.cleanup()
1149 main.exit()
1150 except:
1151 main.log.info(self.name+" ::::::")
1152 main.log.error( traceback.print_exc())
1153 main.log.info(self.name+" ::::::")
1154 main.cleanup()
1155 main.exit()
1156
andrewonlab7e4d2d32014-10-15 13:23:21 -04001157 #***********************************