blob: 3318f712a7191bda598fb067ae54b1d3ff6da4ec [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))
andrewonlab95ce8322014-10-13 14:12:04 -0400149 self.handle.expect("onos>")
150
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 '''
402 Lists all infrastructure devices
403 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
421 print "repr(handle) =", repr(handle)
422 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
423 handle1 = ansi_escape.sub('', handle)
424 print "repr(handle1) = ", repr(handle1)
425 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400426 else:
427 if not grep_str:
428 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400429 self.handle.expect("onos>")
430 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400431 self.handle.expect("onos>")
432 else:
433 self.handle.sendline("devices | grep '"+
434 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400435 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400436 self.handle.sendline("")
andrewonlab86dc3082014-10-13 18:18:38 -0400437 self.handle.expect("onos>")
Jon Hall11b2c662014-10-17 18:53:43 -0400438<<<<<<< HEAD
Jon Halla001c392014-10-17 18:50:59 -0400439 handle = self.handle.before
440 print "handle =",handle
441 return handle
Jon Hall11b2c662014-10-17 18:53:43 -0400442=======
andrewonlab86dc3082014-10-13 18:18:38 -0400443
444 handle = self.handle.before
andrewonlab9a50dfe2014-10-17 17:22:31 -0400445
andrewonlab86dc3082014-10-13 18:18:38 -0400446 return handle
Jon Hall11b2c662014-10-17 18:53:43 -0400447>>>>>>> d1192e144d179c68864828aa6a339764d69d1216
andrewonlab7c211572014-10-15 16:45:20 -0400448 except pexpect.EOF:
449 main.log.error(self.name + ": EOF exception found")
450 main.log.error(self.name + ": " + self.handle.before)
451 main.cleanup()
452 main.exit()
453 except:
454 main.log.info(self.name+" ::::::")
455 main.log.error( traceback.print_exc())
456 main.log.info(self.name+" ::::::")
457 main.cleanup()
458 main.exit()
459
Jon Halle8217482014-10-17 13:49:14 -0400460 def links(self, json_format=True, grep_str=""):
461 '''
462 Lists all core links
463 Optional argument:
464 * grep_str - pass in a string to grep
465 '''
466 try:
467 self.handle.sendline("")
468 self.handle.expect("onos>")
469
470 if json_format:
471 if not grep_str:
472 self.handle.sendline("links -j")
473 self.handle.expect("links -j")
474 self.handle.expect("onos>")
475 else:
476 self.handle.sendline("links -j | grep '"+
477 str(grep_str)+"'")
478 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
479 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400480 handle = self.handle.before
481 print "repr(handle) =", repr(handle)
482 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
483 handle1 = ansi_escape.sub('', handle)
484 print "repr(handle1) = ", repr(handle1)
485 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400486 else:
487 if not grep_str:
488 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400489 self.handle.expect("onos>")
490 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400491 self.handle.expect("onos>")
492 else:
493 self.handle.sendline("links | grep '"+
494 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400495 self.handle.expect("onos>")
496 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400497 self.handle.expect("onos>")
498
Jon Hall11b2c662014-10-17 18:53:43 -0400499<<<<<<< HEAD
Jon Halla001c392014-10-17 18:50:59 -0400500 handle = self.handle.before
501 print "handle =",handle
502 return handle
Jon Hall11b2c662014-10-17 18:53:43 -0400503=======
Jon Halle8217482014-10-17 13:49:14 -0400504 handle = self.handle.before
andrewonlab9a50dfe2014-10-17 17:22:31 -0400505
Jon Halle8217482014-10-17 13:49:14 -0400506 return handle
Jon Hall11b2c662014-10-17 18:53:43 -0400507>>>>>>> d1192e144d179c68864828aa6a339764d69d1216
Jon Halle8217482014-10-17 13:49:14 -0400508 except pexpect.EOF:
509 main.log.error(self.name + ": EOF exception found")
510 main.log.error(self.name + ": " + self.handle.before)
511 main.cleanup()
512 main.exit()
513 except:
514 main.log.info(self.name+" ::::::")
515 main.log.error( traceback.print_exc())
516 main.log.info(self.name+" ::::::")
517 main.cleanup()
518 main.exit()
519
520
521 def ports(self, json_format=True, grep_str=""):
522 '''
523 Lists all ports
524 Optional argument:
525 * grep_str - pass in a string to grep
526 '''
527 try:
528 self.handle.sendline("")
529 self.handle.expect("onos>")
530
531 if json_format:
532 if not grep_str:
533 self.handle.sendline("ports -j")
534 self.handle.expect("ports -j")
535 self.handle.expect("onos>")
536 else:
537 self.handle.sendline("ports -j | grep '"+
538 str(grep_str)+"'")
539 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
540 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400541 print "repr(handle) =", repr(handle)
542 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
543 handle1 = ansi_escape.sub('', handle)
544 print "repr(handle1) = ", repr(handle1)
545 return handle1
546
Jon Halle8217482014-10-17 13:49:14 -0400547 else:
548 if not grep_str:
549 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400550 self.handle.expect("onos>")
551 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400552 self.handle.expect("onos>")
553 else:
554 self.handle.sendline("ports | grep '"+
555 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400556 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400557 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400558 self.handle.expect("onos>")
Jon Hall11b2c662014-10-17 18:53:43 -0400559<<<<<<< HEAD
Jon Halla001c392014-10-17 18:50:59 -0400560 handle = self.handle.before
561 print "handle =",handle
562 return handle
Jon Hall11b2c662014-10-17 18:53:43 -0400563=======
Jon Halle8217482014-10-17 13:49:14 -0400564
565 handle = self.handle.before
andrewonlab9a50dfe2014-10-17 17:22:31 -0400566
Jon Halle8217482014-10-17 13:49:14 -0400567 return handle
Jon Hall11b2c662014-10-17 18:53:43 -0400568>>>>>>> d1192e144d179c68864828aa6a339764d69d1216
Jon Halle8217482014-10-17 13:49:14 -0400569 except pexpect.EOF:
570 main.log.error(self.name + ": EOF exception found")
571 main.log.error(self.name + ": " + self.handle.before)
572 main.cleanup()
573 main.exit()
574 except:
575 main.log.info(self.name+" ::::::")
576 main.log.error( traceback.print_exc())
577 main.log.info(self.name+" ::::::")
578 main.cleanup()
579 main.exit()
580
581
andrewonlab7c211572014-10-15 16:45:20 -0400582 def device_role(self, device_id, node_id, role):
583 '''
584 Set device role for specified device and node with role
585 Required:
586 * device_id : may be obtained by function get_all_devices_id
587 * node_id : may be obtained by function get_all_nodes_id
588 * role: specify one of the following roles:
589 - master
590 - standby
591 - none
592 '''
593 try:
594 self.handle.sendline("")
595 self.handle.expect("onos>")
596
597 self.handle.sendline("device-role "+
598 str(device_id) + " " +
599 str(node_id) + " " +
600 str(role))
601 i = self.handle.expect([
602 "Error",
603 "onos>"])
604
605 self.handle.sendline("")
606 self.handle.expect("onos>")
607
608 handle = self.handle.before
609
610 if i == 0:
611 main.log.error("device-role command returned error")
612 return handle
613 else:
614 return main.TRUE
615
andrewonlab86dc3082014-10-13 18:18:38 -0400616 except pexpect.EOF:
617 main.log.error(self.name + ": EOF exception found")
618 main.log.error(self.name + ": " + self.handle.before)
619 main.cleanup()
620 main.exit()
621 except:
622 main.log.info(self.name+" ::::::")
623 main.log.error( traceback.print_exc())
624 main.log.info(self.name+" ::::::")
625 main.cleanup()
626 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400627
andrewonlab3e15ead2014-10-15 14:21:34 -0400628 def paths(self, src_id, dst_id):
629 '''
630 Returns string of paths, and the cost.
631 Issues command: onos:paths <src> <dst>
632 '''
633 try:
634 self.handle.sendline("")
635 self.handle.expect("onos>")
636
637 self.handle.sendline("onos:paths "+
638 str(src_id) + " " + str(dst_id))
639 i = self.handle.expect([
640 "Error",
641 "onos>"])
642
643 self.handle.sendline("")
644 self.handle.expect("onos>")
645
646 handle = self.handle.before
647
648 if i == 0:
649 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400650 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400651 else:
652 path = handle.split(";")[0]
653 cost = handle.split(";")[1]
654 return (path, cost)
655
656 except pexpect.EOF:
657 main.log.error(self.name + ": EOF exception found")
658 main.log.error(self.name + ": " + self.handle.before)
659 main.cleanup()
660 main.exit()
661 except:
662 main.log.info(self.name+" ::::::")
663 main.log.error( traceback.print_exc())
664 main.log.info(self.name+" ::::::")
665 main.cleanup()
666 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400667
668 #TODO:
669 #def hosts(self):
670
671 def get_hosts_id(self, host_list):
672 '''
673 Obtain list of hosts
674 Issues command: 'onos> hosts'
675
676 Required:
677 * host_list: List of hosts obtained by Mininet
678 IMPORTANT:
679 This function assumes that you started your
680 topology with the option '--mac'.
681 Furthermore, it assumes that value of VLAN is '-1'
682 Description:
683 Converts mininet hosts (h1, h2, h3...) into
684 ONOS format (00:00:00:00:00:01/-1 , ...)
685 '''
686
687 try:
688 self.handle.sendline("")
689 self.handle.expect("onos>")
690
691 onos_host_list = []
692
693 for host in host_list:
694 host = host.replace("h", "")
695 host_hex = hex(int(host)).zfill(12)
696 host_hex = str(host_hex).replace('x','0')
697 i = iter(str(host_hex))
698 host_hex = ":".join(a+b for a,b in zip(i,i))
699 host_hex = host_hex + "/-1"
700 onos_host_list.append(host_hex)
701
702 return onos_host_list
703
704 except pexpect.EOF:
705 main.log.error(self.name + ": EOF exception found")
706 main.log.error(self.name + ": " + self.handle.before)
707 main.cleanup()
708 main.exit()
709 except:
710 main.log.info(self.name+" ::::::")
711 main.log.error( traceback.print_exc())
712 main.log.info(self.name+" ::::::")
713 main.cleanup()
714 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400715
andrewonlabe6745342014-10-17 14:29:13 -0400716 def add_host_intent(self, host_id_one, host_id_two):
717 '''
718 Required:
719 * host_id_one: ONOS host id for host1
720 * host_id_two: ONOS host id for host2
721 Description:
722 Adds a host-to-host intent (bidrectional) by
723 specifying the two hosts.
724 '''
725 try:
726 self.handle.sendline("")
727 self.handle.expect("onos>")
728
729 self.handle.sendline("add-host-intent "+
730 str(host_id_one) + " " + str(host_id_two))
731 self.handle.expect("onos>")
732
733 self.handle.sendline("")
734 self.handle.expect("onos>")
735
736 handle = self.handle.before
737
738 main.log.info("Intent installed between "+
739 str(host_id_one) + " and " + str(host_id_two))
740
741 return handle
742
743 except pexpect.EOF:
744 main.log.error(self.name + ": EOF exception found")
745 main.log.error(self.name + ": " + self.handle.before)
746 main.cleanup()
747 main.exit()
748 except:
749 main.log.info(self.name+" ::::::")
750 main.log.error( traceback.print_exc())
751 main.log.info(self.name+" ::::::")
752 main.cleanup()
753 main.exit()
754
andrewonlab4dbb4d82014-10-17 18:22:31 -0400755 def add_point_intent(self, ingress_device, port_ingress,
756 egress_device, port_egress):
757 '''
758 Required:
759 * ingress_device: device id of ingress device
760 * egress_device: device id of egress device
761 Description:
762 Adds a point-to-point intent (uni-directional) by
763 specifying device id's
764
765 NOTE: This function may change depending on the
766 options developers provide for point-to-point
767 intent via cli
768 '''
769 try:
770 self.handle.sendline("")
771 self.handle.expect("onos>")
772
773 self.handle.sendline("add-point-intent "+
774 str(ingress_device) + "/" + str(port_ingress) + " " +
775 str(egress_device) + "/" + str(port_egress))
776 i = self.handle.expect([
777 "Error",
778 "onos>"])
779
780 self.handle.sendline("")
781 self.handle.expect("onos>")
782
783 handle = self.handle.before
784
785 if i == 0:
786 main.log.error("Error in adding point-to-point intent")
787 return handle
788 else:
789 return main.TRUE
790
791 except pexpect.EOF:
792 main.log.error(self.name + ": EOF exception found")
793 main.log.error(self.name + ": " + self.handle.before)
794 main.cleanup()
795 main.exit()
796 except:
797 main.log.info(self.name+" ::::::")
798 main.log.error( traceback.print_exc())
799 main.log.info(self.name+" ::::::")
800 main.cleanup()
801 main.exit()
802
andrewonlab9a50dfe2014-10-17 17:22:31 -0400803 def remove_intent(self, intent_id):
804 '''
805 Remove intent for specified intent id
806 '''
807 try:
808 self.handle.sendline("")
809 self.handle.expect("onos>")
810
811 self.handle.sendline("remove-intent "+str(intent_id))
812 i = self.handle.expect([
813 "Error",
814 "onos>"])
815
816 handle = self.handle.before
817
818 if i == 0:
819 main.log.error("Error in removing intent")
820 return handle
821 else:
822 return handle
823
824 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
andrewonlabe6745342014-10-17 14:29:13 -0400836 def intents(self):
837 '''
838 Description:
839 Obtain intents currently installed
840 '''
841 try:
842 self.handle.sendline("")
843 self.handle.expect("onos>")
844
845 self.handle.sendline("intents")
846 self.handle.expect("onos>")
847
848 self.handle.sendline("")
849 self.handle.expect("onos>")
850
851 handle = self.handle.before
852
853 return handle
854
855 except pexpect.EOF:
856 main.log.error(self.name + ": EOF exception found")
857 main.log.error(self.name + ": " + self.handle.before)
858 main.cleanup()
859 main.exit()
860 except:
861 main.log.info(self.name+" ::::::")
862 main.log.error( traceback.print_exc())
863 main.log.info(self.name+" ::::::")
864 main.cleanup()
865 main.exit()
866
andrewonlab3e15ead2014-10-15 14:21:34 -0400867 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -0400868 #Wrapper functions use existing driver
869 #functions and extends their use case.
870 #For example, we may use the output of
871 #a normal driver function, and parse it
872 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -0400873
andrewonlab9a50dfe2014-10-17 17:22:31 -0400874 def get_all_intents_id(self):
875 '''
876 Description:
877 Obtain all intent id's in a list
878 '''
879 try:
880 #Obtain output of intents function
881 intents_str = self.intents()
882 all_intent_list = []
883 intent_id_list = []
884
885 #Parse the intents output for ID's
886 intents_list = [s.strip() for s in intents_str.splitlines()]
887 for intents in intents_list:
888 if "onos>" in intents:
889 continue
890 elif "intents" in intents:
891 continue
892 else:
893 line_list = intents.split(" ")
894 all_intent_list.append(line_list[0])
895
896 all_intent_list = all_intent_list[1:-2]
897
898 for intents in all_intent_list:
899 if not intents:
900 continue
901 else:
902 intent_id_list.append(intents)
903
904 return intent_id_list
905
906 except pexpect.EOF:
907 main.log.error(self.name + ": EOF exception found")
908 main.log.error(self.name + ": " + self.handle.before)
909 main.cleanup()
910 main.exit()
911 except:
912 main.log.info(self.name+" ::::::")
913 main.log.error( traceback.print_exc())
914 main.log.info(self.name+" ::::::")
915 main.cleanup()
916 main.exit()
917
andrewonlab7e4d2d32014-10-15 13:23:21 -0400918 def get_all_devices_id(self):
919 '''
920 Use 'devices' function to obtain list of all devices
921 and parse the result to obtain a list of all device
922 id's. Returns this list. Returns empty list if no
923 devices exist
924 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -0400925
926 This function may be useful if you are not sure of the
927 device id, and wish to execute other commands using
928 the ids. By obtaining the list of device ids on the fly,
929 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -0400930 '''
931 try:
932 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -0400933 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -0400934 id_list = []
935
936 if not devices_str:
937 main.log.info("There are no devices to get id from")
938 return id_list
939
940 #Split the string into list by comma
941 device_list = devices_str.split(",")
942 #Get temporary list of all arguments with string 'id='
943 temp_list = [dev for dev in device_list if "id=" in dev]
944 #Split list further into arguments before and after string
945 # 'id='. Get the latter portion (the actual device id) and
946 # append to id_list
947 for arg in temp_list:
948 id_list.append(arg.split("id=")[1])
949
950 return id_list
951
952 except pexpect.EOF:
953 main.log.error(self.name + ": EOF exception found")
954 main.log.error(self.name + ": " + self.handle.before)
955 main.cleanup()
956 main.exit()
957 except:
958 main.log.info(self.name+" ::::::")
959 main.log.error( traceback.print_exc())
960 main.log.info(self.name+" ::::::")
961 main.cleanup()
962 main.exit()
963
andrewonlab7c211572014-10-15 16:45:20 -0400964 def get_all_nodes_id(self):
965 '''
966 Uses 'nodes' function to obtain list of all nodes
967 and parse the result of nodes to obtain just the
968 node id's.
969 Returns:
970 list of node id's
971 '''
972 try:
973 nodes_str = self.nodes()
974 id_list = []
975
976 if not nodes_str:
977 main.log.info("There are no nodes to get id from")
978 return id_list
979
980 #Sample nodes_str output
981 #id=local, address=127.0.0.1:9876, state=ACTIVE *
982
983 #Split the string into list by comma
984 nodes_list = nodes_str.split(",")
985 temp_list = [node for node in nodes_list if "id=" in node]
986 for arg in temp_list:
987 id_list.append(arg.split("id=")[1])
988
989 return id_list
990
991 except pexpect.EOF:
992 main.log.error(self.name + ": EOF exception found")
993 main.log.error(self.name + ": " + self.handle.before)
994 main.cleanup()
995 main.exit()
996 except:
997 main.log.info(self.name+" ::::::")
998 main.log.error( traceback.print_exc())
999 main.log.info(self.name+" ::::::")
1000 main.cleanup()
1001 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001002
1003 #***********************************