blob: f7f4ef4a3c35a19a14207a416865fdee56c3d3fb [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
27sys.path.append("../")
28from drivers.common.clidriver import CLI
29
30class OnosCliDriver(CLI):
31
32 def __init__(self):
33 '''
34 Initialize client
35 '''
36 super(CLI, self).__init__()
37
38 def connect(self,**connectargs):
39 '''
40 Creates ssh handle for ONOS cli.
41 '''
42 try:
43 for key in connectargs:
44 vars(self)[key] = connectargs[key]
45 self.home = "~/ONOS"
46 for key in self.options:
47 if key == "home":
48 self.home = self.options['home']
49 break
50
51
52 self.name = self.options['name']
53 self.handle = super(OnosCliDriver,self).connect(
54 user_name = self.user_name,
55 ip_address = self.ip_address,
56 port = self.port,
57 pwd = self.pwd,
58 home = self.home)
59
60 self.handle.sendline("cd "+ self.home)
61 self.handle.expect("\$")
62 if self.handle:
63 return self.handle
64 else :
65 main.log.info("NO ONOS HANDLE")
66 return main.FALSE
67 except pexpect.EOF:
68 main.log.error(self.name + ": EOF exception found")
69 main.log.error(self.name + ": " + self.handle.before)
70 main.cleanup()
71 main.exit()
72 except:
73 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
74 main.log.error( traceback.print_exc() )
75 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
76 main.cleanup()
77 main.exit()
78
79 def disconnect(self):
80 '''
81 Called when Test is complete to disconnect the ONOS handle.
82 '''
83 response = ''
84 try:
andrewonlab2a6c9342014-10-16 13:40:15 -040085 self.handle.sendline("")
86 self.handle.expect("onos>")
andrewonlabc2d05aa2014-10-13 16:51:10 -040087 self.handle.sendline("system:shutdown")
88 self.handle.expect("Confirm")
andrewonlab2a6c9342014-10-16 13:40:15 -040089 self.handle.sendline("yes")
andrewonlabc2d05aa2014-10-13 16:51:10 -040090 self.handle.expect("\$")
91
andrewonlab95ce8322014-10-13 14:12:04 -040092 except pexpect.EOF:
93 main.log.error(self.name + ": EOF exception found")
94 main.log.error(self.name + ": " + self.handle.before)
95 except:
96 main.log.error(self.name + ": Connection failed to the host")
97 response = main.FALSE
98 return response
99
100 def set_cell(self, cellname):
101 '''
102 Calls 'cell <name>' to set the environment variables on ONOSbench
103
104 Before issuing any cli commands, set the environment variable first.
105 '''
106 try:
107 if not cellname:
108 main.log.error("Must define cellname")
109 main.cleanup()
110 main.exit()
111 else:
112 self.handle.sendline("cell "+str(cellname))
113 #Expect the cellname in the ONOS_CELL variable.
114 #Note that this variable name is subject to change
115 # and that this driver will have to change accordingly
116 self.handle.expect("ONOS_CELL="+str(cellname))
117 handle_before = self.handle.before
118 handle_after = self.handle.after
119 #Get the rest of the handle
120 self.handle.sendline("")
121 self.handle.expect("\$")
122 handle_more = self.handle.before
123
124 main.log.info("Cell call returned: "+handle_before+
125 handle_after + handle_more)
126
127 return main.TRUE
128
129 except pexpect.EOF:
130 main.log.error(self.name + ": EOF exception found")
131 main.log.error(self.name + ": " + self.handle.before)
132 main.cleanup()
133 main.exit()
134 except:
135 main.log.info(self.name+" ::::::")
136 main.log.error( traceback.print_exc())
137 main.log.info(self.name+" ::::::")
138 main.cleanup()
139 main.exit()
140
andrewonlabc2d05aa2014-10-13 16:51:10 -0400141 def start_onos_cli(self, ONOS_ip):
andrewonlab95ce8322014-10-13 14:12:04 -0400142 try:
143 self.handle.sendline("")
144 self.handle.expect("\$")
145
146 #Wait for onos start (-w) and enter onos cli
andrewonlabc2d05aa2014-10-13 16:51:10 -0400147 self.handle.sendline("onos -w "+str(ONOS_ip))
andrewonlab95ce8322014-10-13 14:12:04 -0400148 self.handle.expect("onos>")
149
150 except pexpect.EOF:
151 main.log.error(self.name + ": EOF exception found")
152 main.log.error(self.name + ": " + self.handle.before)
153 main.cleanup()
154 main.exit()
155 except:
156 main.log.info(self.name+" ::::::")
157 main.log.error( traceback.print_exc())
158 main.log.info(self.name+" ::::::")
159 main.cleanup()
160 main.exit()
161
andrewonlaba18f6bf2014-10-13 19:31:54 -0400162 def sendline(self, cmd_str):
163 '''
164 Send a completely user specified string to
165 the onos> prompt. Use this function if you have
166 a very specific command to send.
167
168 Warning: There are no sanity checking to commands
169 sent using this method.
170 '''
171 try:
172 self.handle.sendline("")
173 self.handle.expect("onos>")
174
175 self.handle.sendline(cmd_str)
176 self.handle.expect("onos>")
177
178 handle = self.handle.before
179
180 self.handle.sendline("")
181 self.handle.expect("onos>")
182
183 handle += self.handle.before
184 handle += self.handle.after
185
186 main.log.info("Command sent.")
187
188 return handle
189 except pexpect.EOF:
190 main.log.error(self.name + ": EOF exception found")
191 main.log.error(self.name + ": " + self.handle.before)
192 main.cleanup()
193 main.exit()
194 except:
195 main.log.info(self.name+" ::::::")
196 main.log.error( traceback.print_exc())
197 main.log.info(self.name+" ::::::")
198 main.cleanup()
199 main.exit()
200
andrewonlab95ce8322014-10-13 14:12:04 -0400201 #IMPORTANT NOTE:
202 #For all cli commands, naming convention should match
203 #the cli command replacing ':' with '_'.
204 #Ex) onos:topology > onos_topology
205 # onos:links > onos_links
206 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400207
208 def add_node(self, node_id, ONOS_ip, tcp_port=""):
209 '''
210 Adds a new cluster node by ID and address information.
211 Required:
212 * node_id
213 * ONOS_ip
214 Optional:
215 * tcp_port
216 '''
217 try:
218 self.handle.sendline("")
219 self.handle.expect("onos>")
220
221 self.handle.sendline("add-node "+
222 str(node_id)+" "+
223 str(ONOS_ip)+" "+
224 str(tcp_port))
225
226 i = self.handle.expect([
227 "Error",
228 "onos>" ])
229
230 #Clear handle to get previous output
231 self.handle.sendline("")
232 self.handle.expect("onos>")
233
234 handle = self.handle.before
235
236 if i == 0:
237 main.log.error("Error in adding node")
238 main.log.error(handle)
239 return main.FALSE
240 else:
241 main.log.info("Node "+str(ONOS_ip)+" added")
242 return main.TRUE
243
244 except pexpect.EOF:
245 main.log.error(self.name + ": EOF exception found")
246 main.log.error(self.name + ": " + self.handle.before)
247 main.cleanup()
248 main.exit()
249 except:
250 main.log.info(self.name+" ::::::")
251 main.log.error( traceback.print_exc())
252 main.log.info(self.name+" ::::::")
253 main.cleanup()
254 main.exit()
255
andrewonlab86dc3082014-10-13 18:18:38 -0400256 def remove_node(self, node_id):
257 '''
258 Removes a cluster by ID
259 Issues command: 'remove-node [<node-id>]'
260 Required:
261 * node_id
262 '''
263 try:
264 self.handle.sendline("")
265 self.handle.expect("onos>")
266
267 self.handle.sendline("remove-node "+str(node_id))
268 self.handle.expect("onos>")
269
270 return main.TRUE
271
272 except pexpect.EOF:
273 main.log.error(self.name + ": EOF exception found")
274 main.log.error(self.name + ": " + self.handle.before)
275 main.cleanup()
276 main.exit()
277 except:
278 main.log.info(self.name+" ::::::")
279 main.log.error( traceback.print_exc())
280 main.log.info(self.name+" ::::::")
281 main.cleanup()
282 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400283
andrewonlab7c211572014-10-15 16:45:20 -0400284 def nodes(self):
285 '''
286 List the nodes currently visible
287 Issues command: 'nodes'
288 Returns: entire handle of list of nodes
289 '''
290 try:
291 self.handle.sendline("")
292 self.handle.expect("onos>")
293
294 self.handle.sendline("nodes")
295 self.handle.expect("onos>")
296
297 self.handle.sendline("")
298 self.handle.expect("onos>")
299
300 handle = self.handle.before
301
302 return handle
303
304 except pexpect.EOF:
305 main.log.error(self.name + ": EOF exception found")
306 main.log.error(self.name + ": " + self.handle.before)
307 main.cleanup()
308 main.exit()
309 except:
310 main.log.info(self.name+" ::::::")
311 main.log.error( traceback.print_exc())
312 main.log.info(self.name+" ::::::")
313 main.cleanup()
314 main.exit()
315
andrewonlab38d6ae22014-10-15 14:23:45 -0400316 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400317 '''
318 Shows the current state of the topology
319 by issusing command: 'onos> onos:topology'
320 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400321 try:
322 self.handle.sendline("")
323 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400324 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400325 self.handle.sendline("onos:topology")
326 self.handle.expect("onos>")
327
328 handle = self.handle.before
329
330 main.log.info("onos:topology returned: " +
331 str(handle))
332
333 return handle
334
335 except pexpect.EOF:
336 main.log.error(self.name + ": EOF exception found")
337 main.log.error(self.name + ": " + self.handle.before)
338 main.cleanup()
339 main.exit()
340 except:
341 main.log.info(self.name+" ::::::")
342 main.log.error( traceback.print_exc())
343 main.log.info(self.name+" ::::::")
344 main.cleanup()
345 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400346
347 def feature_install(self, feature_str):
348 '''
349 Installs a specified feature
350 by issuing command: 'onos> feature:install <feature_str>'
351 '''
352 try:
353 self.handle.sendline("")
354 self.handle.expect("onos>")
355
356 self.handle.sendline("feature:install "+str(feature_str))
357 self.handle.expect("onos>")
358
359 return main.TRUE
360
361 except pexpect.EOF:
362 main.log.error(self.name + ": EOF exception found")
363 main.log.error(self.name + ": " + self.handle.before)
364 main.cleanup()
365 main.exit()
366 except:
367 main.log.info(self.name+" ::::::")
368 main.log.error( traceback.print_exc())
369 main.log.info(self.name+" ::::::")
370 main.cleanup()
371 main.exit()
372
373 def feature_uninstall(self, feature_str):
374 '''
375 Uninstalls a specified feature
376 by issuing command: 'onos> feature:uninstall <feature_str>'
377 '''
378 try:
379 self.handle.sendline("")
380 self.handle.expect("onos>")
381
382 self.handle.sendline("feature:uninstall "+str(feature_str))
383 self.handle.expect("onos>")
384
385 return main.TRUE
386
387 except pexpect.EOF:
388 main.log.error(self.name + ": EOF exception found")
389 main.log.error(self.name + ": " + self.handle.before)
390 main.cleanup()
391 main.exit()
392 except:
393 main.log.info(self.name+" ::::::")
394 main.log.error( traceback.print_exc())
395 main.log.info(self.name+" ::::::")
396 main.cleanup()
397 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400398
Jon Halle8217482014-10-17 13:49:14 -0400399 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400400 '''
401 Lists all infrastructure devices
402 Optional argument:
403 * grep_str - pass in a string to grep
404 '''
405 try:
406 self.handle.sendline("")
407 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400408
409 if json_format:
410 if not grep_str:
411 self.handle.sendline("devices -j")
412 self.handle.expect("devices -j")
413 self.handle.expect("onos>")
414 else:
415 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400416 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400417 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
418 self.handle.expect("onos>")
419 else:
420 if not grep_str:
421 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400422 self.handle.expect("onos>")
423 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400424 self.handle.expect("onos>")
425 else:
426 self.handle.sendline("devices | grep '"+
427 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400428 self.handle.expect("onos>")
429 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400430 self.handle.expect("onos>")
andrewonlab86dc3082014-10-13 18:18:38 -0400431
432 handle = self.handle.before
andrewonlab9a50dfe2014-10-17 17:22:31 -0400433
andrewonlab86dc3082014-10-13 18:18:38 -0400434 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400435 except pexpect.EOF:
436 main.log.error(self.name + ": EOF exception found")
437 main.log.error(self.name + ": " + self.handle.before)
438 main.cleanup()
439 main.exit()
440 except:
441 main.log.info(self.name+" ::::::")
442 main.log.error( traceback.print_exc())
443 main.log.info(self.name+" ::::::")
444 main.cleanup()
445 main.exit()
446
Jon Halle8217482014-10-17 13:49:14 -0400447 def links(self, json_format=True, grep_str=""):
448 '''
449 Lists all core links
450 Optional argument:
451 * grep_str - pass in a string to grep
452 '''
453 try:
454 self.handle.sendline("")
455 self.handle.expect("onos>")
456
457 if json_format:
458 if not grep_str:
459 self.handle.sendline("links -j")
460 self.handle.expect("links -j")
461 self.handle.expect("onos>")
462 else:
463 self.handle.sendline("links -j | grep '"+
464 str(grep_str)+"'")
465 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
466 self.handle.expect("onos>")
467 else:
468 if not grep_str:
469 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400470 self.handle.expect("onos>")
471 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400472 self.handle.expect("onos>")
473 else:
474 self.handle.sendline("links | grep '"+
475 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400476 self.handle.expect("onos>")
477 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400478 self.handle.expect("onos>")
479
480 handle = self.handle.before
andrewonlab9a50dfe2014-10-17 17:22:31 -0400481
Jon Halle8217482014-10-17 13:49:14 -0400482 return handle
483 except pexpect.EOF:
484 main.log.error(self.name + ": EOF exception found")
485 main.log.error(self.name + ": " + self.handle.before)
486 main.cleanup()
487 main.exit()
488 except:
489 main.log.info(self.name+" ::::::")
490 main.log.error( traceback.print_exc())
491 main.log.info(self.name+" ::::::")
492 main.cleanup()
493 main.exit()
494
495
496 def ports(self, json_format=True, grep_str=""):
497 '''
498 Lists all ports
499 Optional argument:
500 * grep_str - pass in a string to grep
501 '''
502 try:
503 self.handle.sendline("")
504 self.handle.expect("onos>")
505
506 if json_format:
507 if not grep_str:
508 self.handle.sendline("ports -j")
509 self.handle.expect("ports -j")
510 self.handle.expect("onos>")
511 else:
512 self.handle.sendline("ports -j | grep '"+
513 str(grep_str)+"'")
514 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
515 self.handle.expect("onos>")
516 else:
517 if not grep_str:
518 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400519 self.handle.expect("onos>")
520 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400521 self.handle.expect("onos>")
522 else:
523 self.handle.sendline("ports | grep '"+
524 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400525 self.handle.expect("onos>")
526 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400527 self.handle.expect("onos>")
528
529 handle = self.handle.before
andrewonlab9a50dfe2014-10-17 17:22:31 -0400530
Jon Halle8217482014-10-17 13:49:14 -0400531 return handle
532 except pexpect.EOF:
533 main.log.error(self.name + ": EOF exception found")
534 main.log.error(self.name + ": " + self.handle.before)
535 main.cleanup()
536 main.exit()
537 except:
538 main.log.info(self.name+" ::::::")
539 main.log.error( traceback.print_exc())
540 main.log.info(self.name+" ::::::")
541 main.cleanup()
542 main.exit()
543
544
andrewonlab7c211572014-10-15 16:45:20 -0400545 def device_role(self, device_id, node_id, role):
546 '''
547 Set device role for specified device and node with role
548 Required:
549 * device_id : may be obtained by function get_all_devices_id
550 * node_id : may be obtained by function get_all_nodes_id
551 * role: specify one of the following roles:
552 - master
553 - standby
554 - none
555 '''
556 try:
557 self.handle.sendline("")
558 self.handle.expect("onos>")
559
560 self.handle.sendline("device-role "+
561 str(device_id) + " " +
562 str(node_id) + " " +
563 str(role))
564 i = self.handle.expect([
565 "Error",
566 "onos>"])
567
568 self.handle.sendline("")
569 self.handle.expect("onos>")
570
571 handle = self.handle.before
572
573 if i == 0:
574 main.log.error("device-role command returned error")
575 return handle
576 else:
577 return main.TRUE
578
andrewonlab86dc3082014-10-13 18:18:38 -0400579 except pexpect.EOF:
580 main.log.error(self.name + ": EOF exception found")
581 main.log.error(self.name + ": " + self.handle.before)
582 main.cleanup()
583 main.exit()
584 except:
585 main.log.info(self.name+" ::::::")
586 main.log.error( traceback.print_exc())
587 main.log.info(self.name+" ::::::")
588 main.cleanup()
589 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400590
andrewonlab3e15ead2014-10-15 14:21:34 -0400591 def paths(self, src_id, dst_id):
592 '''
593 Returns string of paths, and the cost.
594 Issues command: onos:paths <src> <dst>
595 '''
596 try:
597 self.handle.sendline("")
598 self.handle.expect("onos>")
599
600 self.handle.sendline("onos:paths "+
601 str(src_id) + " " + str(dst_id))
602 i = self.handle.expect([
603 "Error",
604 "onos>"])
605
606 self.handle.sendline("")
607 self.handle.expect("onos>")
608
609 handle = self.handle.before
610
611 if i == 0:
612 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400613 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400614 else:
615 path = handle.split(";")[0]
616 cost = handle.split(";")[1]
617 return (path, cost)
618
619 except pexpect.EOF:
620 main.log.error(self.name + ": EOF exception found")
621 main.log.error(self.name + ": " + self.handle.before)
622 main.cleanup()
623 main.exit()
624 except:
625 main.log.info(self.name+" ::::::")
626 main.log.error( traceback.print_exc())
627 main.log.info(self.name+" ::::::")
628 main.cleanup()
629 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400630
631 #TODO:
632 #def hosts(self):
633
634 def get_hosts_id(self, host_list):
635 '''
636 Obtain list of hosts
637 Issues command: 'onos> hosts'
638
639 Required:
640 * host_list: List of hosts obtained by Mininet
641 IMPORTANT:
642 This function assumes that you started your
643 topology with the option '--mac'.
644 Furthermore, it assumes that value of VLAN is '-1'
645 Description:
646 Converts mininet hosts (h1, h2, h3...) into
647 ONOS format (00:00:00:00:00:01/-1 , ...)
648 '''
649
650 try:
651 self.handle.sendline("")
652 self.handle.expect("onos>")
653
654 onos_host_list = []
655
656 for host in host_list:
657 host = host.replace("h", "")
658 host_hex = hex(int(host)).zfill(12)
659 host_hex = str(host_hex).replace('x','0')
660 i = iter(str(host_hex))
661 host_hex = ":".join(a+b for a,b in zip(i,i))
662 host_hex = host_hex + "/-1"
663 onos_host_list.append(host_hex)
664
665 return onos_host_list
666
667 except pexpect.EOF:
668 main.log.error(self.name + ": EOF exception found")
669 main.log.error(self.name + ": " + self.handle.before)
670 main.cleanup()
671 main.exit()
672 except:
673 main.log.info(self.name+" ::::::")
674 main.log.error( traceback.print_exc())
675 main.log.info(self.name+" ::::::")
676 main.cleanup()
677 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400678
andrewonlabe6745342014-10-17 14:29:13 -0400679 def add_host_intent(self, host_id_one, host_id_two):
680 '''
681 Required:
682 * host_id_one: ONOS host id for host1
683 * host_id_two: ONOS host id for host2
684 Description:
685 Adds a host-to-host intent (bidrectional) by
686 specifying the two hosts.
687 '''
688 try:
689 self.handle.sendline("")
690 self.handle.expect("onos>")
691
692 self.handle.sendline("add-host-intent "+
693 str(host_id_one) + " " + str(host_id_two))
694 self.handle.expect("onos>")
695
696 self.handle.sendline("")
697 self.handle.expect("onos>")
698
699 handle = self.handle.before
700
701 main.log.info("Intent installed between "+
702 str(host_id_one) + " and " + str(host_id_two))
703
704 return handle
705
706 except pexpect.EOF:
707 main.log.error(self.name + ": EOF exception found")
708 main.log.error(self.name + ": " + self.handle.before)
709 main.cleanup()
710 main.exit()
711 except:
712 main.log.info(self.name+" ::::::")
713 main.log.error( traceback.print_exc())
714 main.log.info(self.name+" ::::::")
715 main.cleanup()
716 main.exit()
717
andrewonlab9a50dfe2014-10-17 17:22:31 -0400718 def remove_intent(self, intent_id):
719 '''
720 Remove intent for specified intent id
721 '''
722 try:
723 self.handle.sendline("")
724 self.handle.expect("onos>")
725
726 self.handle.sendline("remove-intent "+str(intent_id))
727 i = self.handle.expect([
728 "Error",
729 "onos>"])
730
731 handle = self.handle.before
732
733 if i == 0:
734 main.log.error("Error in removing intent")
735 return handle
736 else:
737 return handle
738
739 except pexpect.EOF:
740 main.log.error(self.name + ": EOF exception found")
741 main.log.error(self.name + ": " + self.handle.before)
742 main.cleanup()
743 main.exit()
744 except:
745 main.log.info(self.name+" ::::::")
746 main.log.error( traceback.print_exc())
747 main.log.info(self.name+" ::::::")
748 main.cleanup()
749 main.exit()
750
andrewonlabe6745342014-10-17 14:29:13 -0400751 def intents(self):
752 '''
753 Description:
754 Obtain intents currently installed
755 '''
756 try:
757 self.handle.sendline("")
758 self.handle.expect("onos>")
759
760 self.handle.sendline("intents")
761 self.handle.expect("onos>")
762
763 self.handle.sendline("")
764 self.handle.expect("onos>")
765
766 handle = self.handle.before
767
768 return handle
769
770 except pexpect.EOF:
771 main.log.error(self.name + ": EOF exception found")
772 main.log.error(self.name + ": " + self.handle.before)
773 main.cleanup()
774 main.exit()
775 except:
776 main.log.info(self.name+" ::::::")
777 main.log.error( traceback.print_exc())
778 main.log.info(self.name+" ::::::")
779 main.cleanup()
780 main.exit()
781
andrewonlab3e15ead2014-10-15 14:21:34 -0400782 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -0400783 #Wrapper functions use existing driver
784 #functions and extends their use case.
785 #For example, we may use the output of
786 #a normal driver function, and parse it
787 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -0400788
andrewonlab9a50dfe2014-10-17 17:22:31 -0400789 def get_all_intents_id(self):
790 '''
791 Description:
792 Obtain all intent id's in a list
793 '''
794 try:
795 #Obtain output of intents function
796 intents_str = self.intents()
797 all_intent_list = []
798 intent_id_list = []
799
800 #Parse the intents output for ID's
801 intents_list = [s.strip() for s in intents_str.splitlines()]
802 for intents in intents_list:
803 if "onos>" in intents:
804 continue
805 elif "intents" in intents:
806 continue
807 else:
808 line_list = intents.split(" ")
809 all_intent_list.append(line_list[0])
810
811 all_intent_list = all_intent_list[1:-2]
812
813 for intents in all_intent_list:
814 if not intents:
815 continue
816 else:
817 intent_id_list.append(intents)
818
819 return intent_id_list
820
821 except pexpect.EOF:
822 main.log.error(self.name + ": EOF exception found")
823 main.log.error(self.name + ": " + self.handle.before)
824 main.cleanup()
825 main.exit()
826 except:
827 main.log.info(self.name+" ::::::")
828 main.log.error( traceback.print_exc())
829 main.log.info(self.name+" ::::::")
830 main.cleanup()
831 main.exit()
832
andrewonlab7e4d2d32014-10-15 13:23:21 -0400833 def get_all_devices_id(self):
834 '''
835 Use 'devices' function to obtain list of all devices
836 and parse the result to obtain a list of all device
837 id's. Returns this list. Returns empty list if no
838 devices exist
839 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -0400840
841 This function may be useful if you are not sure of the
842 device id, and wish to execute other commands using
843 the ids. By obtaining the list of device ids on the fly,
844 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -0400845 '''
846 try:
847 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -0400848 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -0400849 id_list = []
850
851 if not devices_str:
852 main.log.info("There are no devices to get id from")
853 return id_list
854
855 #Split the string into list by comma
856 device_list = devices_str.split(",")
857 #Get temporary list of all arguments with string 'id='
858 temp_list = [dev for dev in device_list if "id=" in dev]
859 #Split list further into arguments before and after string
860 # 'id='. Get the latter portion (the actual device id) and
861 # append to id_list
862 for arg in temp_list:
863 id_list.append(arg.split("id=")[1])
864
865 return id_list
866
867 except pexpect.EOF:
868 main.log.error(self.name + ": EOF exception found")
869 main.log.error(self.name + ": " + self.handle.before)
870 main.cleanup()
871 main.exit()
872 except:
873 main.log.info(self.name+" ::::::")
874 main.log.error( traceback.print_exc())
875 main.log.info(self.name+" ::::::")
876 main.cleanup()
877 main.exit()
878
andrewonlab7c211572014-10-15 16:45:20 -0400879 def get_all_nodes_id(self):
880 '''
881 Uses 'nodes' function to obtain list of all nodes
882 and parse the result of nodes to obtain just the
883 node id's.
884 Returns:
885 list of node id's
886 '''
887 try:
888 nodes_str = self.nodes()
889 id_list = []
890
891 if not nodes_str:
892 main.log.info("There are no nodes to get id from")
893 return id_list
894
895 #Sample nodes_str output
896 #id=local, address=127.0.0.1:9876, state=ACTIVE *
897
898 #Split the string into list by comma
899 nodes_list = nodes_str.split(",")
900 temp_list = [node for node in nodes_list if "id=" in node]
901 for arg in temp_list:
902 id_list.append(arg.split("id=")[1])
903
904 return 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()
andrewonlab7e4d2d32014-10-15 13:23:21 -0400917
918 #***********************************