blob: 6b7a02e84e3da5e58b9f6fc67980d17c50cc641b [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:
andrewonlab3a7c3c72014-10-24 17:21:03 -0400162 #If failed, send ctrl+c to process and try again
163 self.handle.sendline("\x03")
164 self.handle.sendline("onos -w "+str(ONOS_ip))
165 i = self.handle.expect(["onos>",pexpect.TIMEOUT],
166 timeout=30)
167 if i == 0:
168 return main.TRUE
169 else:
170 main.log.error("Connection to CLI "+\
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400171 str(ONOS_ip)+" timeout")
andrewonlab3a7c3c72014-10-24 17:21:03 -0400172 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400173
174 except pexpect.EOF:
175 main.log.error(self.name + ": EOF exception found")
176 main.log.error(self.name + ": " + self.handle.before)
177 main.cleanup()
178 main.exit()
179 except:
180 main.log.info(self.name+" ::::::")
181 main.log.error( traceback.print_exc())
182 main.log.info(self.name+" ::::::")
183 main.cleanup()
184 main.exit()
185
andrewonlaba18f6bf2014-10-13 19:31:54 -0400186 def sendline(self, cmd_str):
187 '''
188 Send a completely user specified string to
189 the onos> prompt. Use this function if you have
190 a very specific command to send.
191
192 Warning: There are no sanity checking to commands
193 sent using this method.
194 '''
195 try:
196 self.handle.sendline("")
197 self.handle.expect("onos>")
198
199 self.handle.sendline(cmd_str)
200 self.handle.expect("onos>")
201
202 handle = self.handle.before
203
204 self.handle.sendline("")
205 self.handle.expect("onos>")
206
207 handle += self.handle.before
208 handle += self.handle.after
209
210 main.log.info("Command sent.")
211
212 return handle
213 except pexpect.EOF:
214 main.log.error(self.name + ": EOF exception found")
215 main.log.error(self.name + ": " + self.handle.before)
216 main.cleanup()
217 main.exit()
218 except:
219 main.log.info(self.name+" ::::::")
220 main.log.error( traceback.print_exc())
221 main.log.info(self.name+" ::::::")
222 main.cleanup()
223 main.exit()
224
andrewonlab95ce8322014-10-13 14:12:04 -0400225 #IMPORTANT NOTE:
226 #For all cli commands, naming convention should match
227 #the cli command replacing ':' with '_'.
228 #Ex) onos:topology > onos_topology
229 # onos:links > onos_links
230 # feature:list > feature_list
andrewonlabc2d05aa2014-10-13 16:51:10 -0400231
232 def add_node(self, node_id, ONOS_ip, tcp_port=""):
233 '''
234 Adds a new cluster node by ID and address information.
235 Required:
236 * node_id
237 * ONOS_ip
238 Optional:
239 * tcp_port
240 '''
241 try:
242 self.handle.sendline("")
243 self.handle.expect("onos>")
244
245 self.handle.sendline("add-node "+
246 str(node_id)+" "+
247 str(ONOS_ip)+" "+
248 str(tcp_port))
249
250 i = self.handle.expect([
251 "Error",
252 "onos>" ])
253
254 #Clear handle to get previous output
255 self.handle.sendline("")
256 self.handle.expect("onos>")
257
258 handle = self.handle.before
259
260 if i == 0:
261 main.log.error("Error in adding node")
262 main.log.error(handle)
263 return main.FALSE
264 else:
265 main.log.info("Node "+str(ONOS_ip)+" added")
266 return main.TRUE
267
268 except pexpect.EOF:
269 main.log.error(self.name + ": EOF exception found")
270 main.log.error(self.name + ": " + self.handle.before)
271 main.cleanup()
272 main.exit()
273 except:
274 main.log.info(self.name+" ::::::")
275 main.log.error( traceback.print_exc())
276 main.log.info(self.name+" ::::::")
277 main.cleanup()
278 main.exit()
279
andrewonlab86dc3082014-10-13 18:18:38 -0400280 def remove_node(self, node_id):
281 '''
282 Removes a cluster by ID
283 Issues command: 'remove-node [<node-id>]'
284 Required:
285 * node_id
286 '''
287 try:
288 self.handle.sendline("")
289 self.handle.expect("onos>")
290
291 self.handle.sendline("remove-node "+str(node_id))
292 self.handle.expect("onos>")
293
294 return main.TRUE
295
296 except pexpect.EOF:
297 main.log.error(self.name + ": EOF exception found")
298 main.log.error(self.name + ": " + self.handle.before)
299 main.cleanup()
300 main.exit()
301 except:
302 main.log.info(self.name+" ::::::")
303 main.log.error( traceback.print_exc())
304 main.log.info(self.name+" ::::::")
305 main.cleanup()
306 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400307
andrewonlab7c211572014-10-15 16:45:20 -0400308 def nodes(self):
309 '''
310 List the nodes currently visible
311 Issues command: 'nodes'
312 Returns: entire handle of list of nodes
313 '''
314 try:
315 self.handle.sendline("")
316 self.handle.expect("onos>")
317
318 self.handle.sendline("nodes")
319 self.handle.expect("onos>")
320
321 self.handle.sendline("")
322 self.handle.expect("onos>")
323
324 handle = self.handle.before
325
326 return handle
327
328 except pexpect.EOF:
329 main.log.error(self.name + ": EOF exception found")
330 main.log.error(self.name + ": " + self.handle.before)
331 main.cleanup()
332 main.exit()
333 except:
334 main.log.info(self.name+" ::::::")
335 main.log.error( traceback.print_exc())
336 main.log.info(self.name+" ::::::")
337 main.cleanup()
338 main.exit()
339
andrewonlab38d6ae22014-10-15 14:23:45 -0400340 def topology(self):
andrewonlabc2d05aa2014-10-13 16:51:10 -0400341 '''
342 Shows the current state of the topology
343 by issusing command: 'onos> onos:topology'
344 '''
andrewonlab95ce8322014-10-13 14:12:04 -0400345 try:
346 self.handle.sendline("")
347 self.handle.expect("onos>")
andrewonlab38d6ae22014-10-15 14:23:45 -0400348 #either onos:topology or 'topology' will work in CLI
andrewonlab95ce8322014-10-13 14:12:04 -0400349 self.handle.sendline("onos:topology")
350 self.handle.expect("onos>")
351
352 handle = self.handle.before
353
354 main.log.info("onos:topology returned: " +
355 str(handle))
356
357 return handle
358
359 except pexpect.EOF:
360 main.log.error(self.name + ": EOF exception found")
361 main.log.error(self.name + ": " + self.handle.before)
362 main.cleanup()
363 main.exit()
364 except:
365 main.log.info(self.name+" ::::::")
366 main.log.error( traceback.print_exc())
367 main.log.info(self.name+" ::::::")
368 main.cleanup()
369 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400370
371 def feature_install(self, feature_str):
372 '''
373 Installs a specified feature
374 by issuing command: 'onos> feature:install <feature_str>'
375 '''
376 try:
377 self.handle.sendline("")
378 self.handle.expect("onos>")
379
380 self.handle.sendline("feature:install "+str(feature_str))
381 self.handle.expect("onos>")
382
383 return main.TRUE
384
385 except pexpect.EOF:
386 main.log.error(self.name + ": EOF exception found")
387 main.log.error(self.name + ": " + self.handle.before)
388 main.cleanup()
389 main.exit()
390 except:
391 main.log.info(self.name+" ::::::")
392 main.log.error( traceback.print_exc())
393 main.log.info(self.name+" ::::::")
394 main.cleanup()
395 main.exit()
396
397 def feature_uninstall(self, feature_str):
398 '''
399 Uninstalls a specified feature
400 by issuing command: 'onos> feature:uninstall <feature_str>'
401 '''
402 try:
403 self.handle.sendline("")
404 self.handle.expect("onos>")
405
406 self.handle.sendline("feature:uninstall "+str(feature_str))
407 self.handle.expect("onos>")
408
409 return main.TRUE
410
411 except pexpect.EOF:
412 main.log.error(self.name + ": EOF exception found")
413 main.log.error(self.name + ": " + self.handle.before)
414 main.cleanup()
415 main.exit()
416 except:
417 main.log.info(self.name+" ::::::")
418 main.log.error( traceback.print_exc())
419 main.log.info(self.name+" ::::::")
420 main.cleanup()
421 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -0400422
Jon Halle8217482014-10-17 13:49:14 -0400423 def devices(self, json_format=True, grep_str=""):
andrewonlab86dc3082014-10-13 18:18:38 -0400424 '''
Jon Hall7b02d952014-10-17 20:14:54 -0400425 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400426 Optional argument:
427 * grep_str - pass in a string to grep
428 '''
429 try:
430 self.handle.sendline("")
431 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400432
433 if json_format:
434 if not grep_str:
435 self.handle.sendline("devices -j")
436 self.handle.expect("devices -j")
437 self.handle.expect("onos>")
438 else:
439 self.handle.sendline("devices -j | grep '"+
andrewonlab86dc3082014-10-13 18:18:38 -0400440 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400441 self.handle.expect("devices -j | grep '"+str(grep_str)+"'")
442 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400443 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400444 '''
445 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
446 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 -0400447 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 -0400448 So we take off that escape sequence using
449 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
450 handle1 = ansi_escape.sub('', handle)
451 '''
452 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400453 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
454 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400455 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400456 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400457 else:
458 if not grep_str:
459 self.handle.sendline("devices")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400460 self.handle.expect("onos>")
Jon Halle8217482014-10-17 13:49:14 -0400461 else:
462 self.handle.sendline("devices | grep '"+
463 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400464 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400465 handle = self.handle.before
466 print "handle =",handle
467 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400468 except pexpect.EOF:
469 main.log.error(self.name + ": EOF exception found")
470 main.log.error(self.name + ": " + self.handle.before)
471 main.cleanup()
472 main.exit()
473 except:
474 main.log.info(self.name+" ::::::")
475 main.log.error( traceback.print_exc())
476 main.log.info(self.name+" ::::::")
477 main.cleanup()
478 main.exit()
479
Jon Halle8217482014-10-17 13:49:14 -0400480 def links(self, json_format=True, grep_str=""):
481 '''
482 Lists all core links
483 Optional argument:
484 * grep_str - pass in a string to grep
485 '''
486 try:
487 self.handle.sendline("")
488 self.handle.expect("onos>")
489
490 if json_format:
491 if not grep_str:
492 self.handle.sendline("links -j")
493 self.handle.expect("links -j")
494 self.handle.expect("onos>")
495 else:
496 self.handle.sendline("links -j | grep '"+
497 str(grep_str)+"'")
498 self.handle.expect("links -j | grep '"+str(grep_str)+"'")
499 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400500 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400501 '''
502 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
503 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 -0400504 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 -0400505 So we take off that escape sequence using
506 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
507 handle1 = ansi_escape.sub('', handle)
508 '''
509 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400510 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
511 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400512 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400513 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400514 else:
515 if not grep_str:
516 self.handle.sendline("links")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400517 self.handle.expect("onos>")
518 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400519 self.handle.expect("onos>")
520 else:
521 self.handle.sendline("links | grep '"+
522 str(grep_str)+"'")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400523 self.handle.expect("onos>")
524 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400525 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400526 handle = self.handle.before
527 print "handle =",handle
528 return handle
Jon Halle8217482014-10-17 13:49:14 -0400529 except pexpect.EOF:
530 main.log.error(self.name + ": EOF exception found")
531 main.log.error(self.name + ": " + self.handle.before)
532 main.cleanup()
533 main.exit()
534 except:
535 main.log.info(self.name+" ::::::")
536 main.log.error( traceback.print_exc())
537 main.log.info(self.name+" ::::::")
538 main.cleanup()
539 main.exit()
540
541
542 def ports(self, json_format=True, grep_str=""):
543 '''
544 Lists all ports
545 Optional argument:
546 * grep_str - pass in a string to grep
547 '''
548 try:
549 self.handle.sendline("")
550 self.handle.expect("onos>")
551
552 if json_format:
553 if not grep_str:
554 self.handle.sendline("ports -j")
555 self.handle.expect("ports -j")
556 self.handle.expect("onos>")
557 else:
558 self.handle.sendline("ports -j | grep '"+
559 str(grep_str)+"'")
560 self.handle.expect("ports -j | grep '"+str(grep_str)+"'")
561 self.handle.expect("onos>")
Jon Hallcd707292014-10-17 19:06:17 -0400562 handle = self.handle.before
Jon Halld815ce42014-10-17 19:52:30 -0400563 '''
564 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
565 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 -0400566 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 -0400567 So we take off that escape sequence using the following commads:
Jon Halld815ce42014-10-17 19:52:30 -0400568 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
569 handle1 = ansi_escape.sub('', handle)
570 '''
571 #print "repr(handle) =", repr(handle)
Jon Halla001c392014-10-17 18:50:59 -0400572 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
573 handle1 = ansi_escape.sub('', handle)
Jon Halld815ce42014-10-17 19:52:30 -0400574 #print "repr(handle1) = ", repr(handle1)
Jon Halla001c392014-10-17 18:50:59 -0400575 return handle1
576
Jon Halle8217482014-10-17 13:49:14 -0400577 else:
578 if not grep_str:
579 self.handle.sendline("ports")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400580 self.handle.expect("onos>")
581 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400582 self.handle.expect("onos>")
583 else:
584 self.handle.sendline("ports | grep '"+
585 str(grep_str)+"'")
Jon Halle8217482014-10-17 13:49:14 -0400586 self.handle.expect("onos>")
andrewonlab9a50dfe2014-10-17 17:22:31 -0400587 self.handle.sendline("")
Jon Halle8217482014-10-17 13:49:14 -0400588 self.handle.expect("onos>")
Jon Halla001c392014-10-17 18:50:59 -0400589 handle = self.handle.before
590 print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400591 return handle
Jon Halle8217482014-10-17 13:49:14 -0400592 except pexpect.EOF:
593 main.log.error(self.name + ": EOF exception found")
594 main.log.error(self.name + ": " + self.handle.before)
595 main.cleanup()
596 main.exit()
597 except:
598 main.log.info(self.name+" ::::::")
599 main.log.error( traceback.print_exc())
600 main.log.info(self.name+" ::::::")
601 main.cleanup()
602 main.exit()
603
604
andrewonlab7c211572014-10-15 16:45:20 -0400605 def device_role(self, device_id, node_id, role):
606 '''
607 Set device role for specified device and node with role
608 Required:
609 * device_id : may be obtained by function get_all_devices_id
610 * node_id : may be obtained by function get_all_nodes_id
611 * role: specify one of the following roles:
612 - master
613 - standby
614 - none
615 '''
616 try:
617 self.handle.sendline("")
618 self.handle.expect("onos>")
619
620 self.handle.sendline("device-role "+
621 str(device_id) + " " +
622 str(node_id) + " " +
623 str(role))
624 i = self.handle.expect([
625 "Error",
626 "onos>"])
627
628 self.handle.sendline("")
629 self.handle.expect("onos>")
630
631 handle = self.handle.before
632
633 if i == 0:
634 main.log.error("device-role command returned error")
635 return handle
636 else:
637 return main.TRUE
638
andrewonlab86dc3082014-10-13 18:18:38 -0400639 except pexpect.EOF:
640 main.log.error(self.name + ": EOF exception found")
641 main.log.error(self.name + ": " + self.handle.before)
642 main.cleanup()
643 main.exit()
644 except:
645 main.log.info(self.name+" ::::::")
646 main.log.error( traceback.print_exc())
647 main.log.info(self.name+" ::::::")
648 main.cleanup()
649 main.exit()
andrewonlab2a6c9342014-10-16 13:40:15 -0400650
andrewonlab3e15ead2014-10-15 14:21:34 -0400651 def paths(self, src_id, dst_id):
652 '''
653 Returns string of paths, and the cost.
654 Issues command: onos:paths <src> <dst>
655 '''
656 try:
657 self.handle.sendline("")
658 self.handle.expect("onos>")
659
660 self.handle.sendline("onos:paths "+
661 str(src_id) + " " + str(dst_id))
662 i = self.handle.expect([
663 "Error",
664 "onos>"])
665
666 self.handle.sendline("")
667 self.handle.expect("onos>")
668
669 handle = self.handle.before
670
671 if i == 0:
672 main.log.error("Error in getting paths")
andrewonlab7c211572014-10-15 16:45:20 -0400673 return (handle, "Error")
andrewonlab3e15ead2014-10-15 14:21:34 -0400674 else:
675 path = handle.split(";")[0]
676 cost = handle.split(";")[1]
677 return (path, cost)
678
679 except pexpect.EOF:
680 main.log.error(self.name + ": EOF exception found")
681 main.log.error(self.name + ": " + self.handle.before)
682 main.cleanup()
683 main.exit()
684 except:
685 main.log.info(self.name+" ::::::")
686 main.log.error( traceback.print_exc())
687 main.log.info(self.name+" ::::::")
688 main.cleanup()
689 main.exit()
andrewonlab3f0a4af2014-10-17 12:25:14 -0400690
691 #TODO:
692 #def hosts(self):
693
694 def get_hosts_id(self, host_list):
695 '''
696 Obtain list of hosts
697 Issues command: 'onos> hosts'
698
699 Required:
700 * host_list: List of hosts obtained by Mininet
701 IMPORTANT:
702 This function assumes that you started your
703 topology with the option '--mac'.
704 Furthermore, it assumes that value of VLAN is '-1'
705 Description:
706 Converts mininet hosts (h1, h2, h3...) into
707 ONOS format (00:00:00:00:00:01/-1 , ...)
708 '''
709
710 try:
Shreya Shahd7310c52014-10-20 16:44:37 -0400711 #self.handle.sendline("")
712 #self.handle.expect("onos>")
andrewonlab3f0a4af2014-10-17 12:25:14 -0400713
714 onos_host_list = []
715
716 for host in host_list:
717 host = host.replace("h", "")
718 host_hex = hex(int(host)).zfill(12)
719 host_hex = str(host_hex).replace('x','0')
720 i = iter(str(host_hex))
721 host_hex = ":".join(a+b for a,b in zip(i,i))
722 host_hex = host_hex + "/-1"
723 onos_host_list.append(host_hex)
724
725 return onos_host_list
726
727 except pexpect.EOF:
728 main.log.error(self.name + ": EOF exception found")
729 main.log.error(self.name + ": " + self.handle.before)
730 main.cleanup()
731 main.exit()
732 except:
733 main.log.info(self.name+" ::::::")
734 main.log.error( traceback.print_exc())
735 main.log.info(self.name+" ::::::")
736 main.cleanup()
737 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400738
andrewonlabe6745342014-10-17 14:29:13 -0400739 def add_host_intent(self, host_id_one, host_id_two):
740 '''
741 Required:
742 * host_id_one: ONOS host id for host1
743 * host_id_two: ONOS host id for host2
744 Description:
745 Adds a host-to-host intent (bidrectional) by
746 specifying the two hosts.
747 '''
748 try:
749 self.handle.sendline("")
750 self.handle.expect("onos>")
751
752 self.handle.sendline("add-host-intent "+
753 str(host_id_one) + " " + str(host_id_two))
754 self.handle.expect("onos>")
755
756 self.handle.sendline("")
757 self.handle.expect("onos>")
758
759 handle = self.handle.before
760
761 main.log.info("Intent installed between "+
762 str(host_id_one) + " and " + str(host_id_two))
763
764 return handle
765
766 except pexpect.EOF:
767 main.log.error(self.name + ": EOF exception found")
768 main.log.error(self.name + ": " + self.handle.before)
769 main.cleanup()
770 main.exit()
771 except:
772 main.log.info(self.name+" ::::::")
773 main.log.error( traceback.print_exc())
774 main.log.info(self.name+" ::::::")
775 main.cleanup()
776 main.exit()
777
andrewonlab7b31d232014-10-24 13:31:47 -0400778 def add_optical_intent(self, ingress_device, egress_device):
779 '''
780 Required:
781 * ingress_device: device id of ingress device
782 * egress_device: device id of egress device
783 Optional:
784 TODO: Still needs to be implemented via dev side
785 '''
786 try:
787 self.handle.sendline("add-optical-intent "+
788 str(ingress_device) + " " + str(egress_device))
789 self.handle.expect("add-optical-intent")
790 i = self.handle.expect([
791 "Error",
792 "onos>"])
793
794 handle = self.handle.before
795
796 #If error, return error message
797 if i == 0:
798 return handle
799 else:
800 return main.TRUE
801
802 except pexpect.EOF:
803 main.log.error(self.name + ": EOF exception found")
804 main.log.error(self.name + ": " + self.handle.before)
805 main.cleanup()
806 main.exit()
807 except:
808 main.log.info(self.name+" ::::::")
809 main.log.error( traceback.print_exc())
810 main.log.info(self.name+" ::::::")
811 main.cleanup()
812 main.exit()
813
andrewonlab4dbb4d82014-10-17 18:22:31 -0400814 def add_point_intent(self, ingress_device, port_ingress,
andrewonlab289e4b72014-10-21 21:24:18 -0400815 egress_device, port_egress, ethType="", ethSrc="",
816 ethDst=""):
andrewonlab4dbb4d82014-10-17 18:22:31 -0400817 '''
818 Required:
819 * ingress_device: device id of ingress device
820 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400821 Optional:
822 * ethType: specify ethType
823 * ethSrc: specify ethSrc (i.e. src mac addr)
824 * ethDst: specify ethDst (i.e. dst mac addr)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400825 Description:
826 Adds a point-to-point intent (uni-directional) by
andrewonlab289e4b72014-10-21 21:24:18 -0400827 specifying device id's and optional fields
828
andrewonlab4dbb4d82014-10-17 18:22:31 -0400829 NOTE: This function may change depending on the
830 options developers provide for point-to-point
831 intent via cli
832 '''
833 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400834 cmd = ""
835
836 #If there are no optional arguments
837 if not ethType and not ethSrc and not ethDst:
838 cmd = "add-point-intent "+\
839 str(ingress_device) + "/" + str(port_ingress) + " " +\
840 str(egress_device) + "/" + str(port_egress)
841
842 else:
andrewonlab9a130be2014-10-22 12:44:56 -0400843 cmd = "add-point-intent "
844
andrewonlab0c0a6772014-10-22 12:31:18 -0400845 if ethType:
andrewonlab289e4b72014-10-21 21:24:18 -0400846 cmd += " --ethType " + str(ethType)
847 if ethSrc:
848 cmd += " --ethSrc " + str(ethSrc)
849 if ethDst:
850 cmd += " --ethDst " + str(ethDst)
andrewonlab9a130be2014-10-22 12:44:56 -0400851
852 cmd += " "+str(ingress_device) + "/" + str(port_ingress) + " " +\
853 str(egress_device) + "/" + str(port_egress)
andrewonlab289e4b72014-10-21 21:24:18 -0400854
andrewonlab4dbb4d82014-10-17 18:22:31 -0400855 self.handle.sendline("")
856 self.handle.expect("onos>")
857
andrewonlab289e4b72014-10-21 21:24:18 -0400858 self.handle.sendline(cmd)
andrewonlab4dbb4d82014-10-17 18:22:31 -0400859 i = self.handle.expect([
860 "Error",
861 "onos>"])
andrewonlab289e4b72014-10-21 21:24:18 -0400862
andrewonlab4dbb4d82014-10-17 18:22:31 -0400863 self.handle.sendline("")
864 self.handle.expect("onos>")
865
866 handle = self.handle.before
867
868 if i == 0:
869 main.log.error("Error in adding point-to-point intent")
870 return handle
871 else:
872 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -0400873
andrewonlab4dbb4d82014-10-17 18:22:31 -0400874 except pexpect.EOF:
875 main.log.error(self.name + ": EOF exception found")
876 main.log.error(self.name + ": " + self.handle.before)
877 main.cleanup()
878 main.exit()
879 except:
880 main.log.info(self.name+" ::::::")
881 main.log.error( traceback.print_exc())
882 main.log.info(self.name+" ::::::")
883 main.cleanup()
884 main.exit()
885
andrewonlab9a50dfe2014-10-17 17:22:31 -0400886 def remove_intent(self, intent_id):
887 '''
888 Remove intent for specified intent id
889 '''
890 try:
891 self.handle.sendline("")
892 self.handle.expect("onos>")
893
894 self.handle.sendline("remove-intent "+str(intent_id))
895 i = self.handle.expect([
896 "Error",
897 "onos>"])
898
899 handle = self.handle.before
900
901 if i == 0:
902 main.log.error("Error in removing intent")
903 return handle
904 else:
905 return handle
906
907 except pexpect.EOF:
908 main.log.error(self.name + ": EOF exception found")
909 main.log.error(self.name + ": " + self.handle.before)
910 main.cleanup()
911 main.exit()
912 except:
913 main.log.info(self.name+" ::::::")
914 main.log.error( traceback.print_exc())
915 main.log.info(self.name+" ::::::")
916 main.cleanup()
917 main.exit()
918
andrewonlab377693f2014-10-21 16:00:30 -0400919 def intents(self, json_format = False):
andrewonlabe6745342014-10-17 14:29:13 -0400920 '''
andrewonlab377693f2014-10-21 16:00:30 -0400921 Optional:
922 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -0400923 Description:
924 Obtain intents currently installed
925 '''
926 try:
andrewonlab377693f2014-10-21 16:00:30 -0400927 if json_format:
928 self.handle.sendline("intents -j")
929 self.handle.expect("intents -j")
930 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -0400931
andrewonlab377693f2014-10-21 16:00:30 -0400932 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -0400933
andrewonlab377693f2014-10-21 16:00:30 -0400934 else:
935 self.handle.sendline("")
936 self.handle.expect("onos>")
andrewonlabe6745342014-10-17 14:29:13 -0400937
andrewonlab377693f2014-10-21 16:00:30 -0400938 self.handle.sendline("intents")
939 self.handle.expect("onos>")
940
941 self.handle.sendline("")
942 self.handle.expect("onos>")
943
944 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -0400945
946 return handle
947
948 except pexpect.EOF:
949 main.log.error(self.name + ": EOF exception found")
950 main.log.error(self.name + ": " + self.handle.before)
951 main.cleanup()
952 main.exit()
953 except:
954 main.log.info(self.name+" ::::::")
955 main.log.error( traceback.print_exc())
956 main.log.info(self.name+" ::::::")
957 main.cleanup()
958 main.exit()
959
andrewonlab867212a2014-10-22 20:13:38 -0400960 def topology_events_metrics(self, json_format=True):
961 '''
962 Description:Returns topology metrics
963 Optional:
964 * json_format: enable json formatting of output
965 '''
966 try:
967 if json_format:
968 self.handle.sendline("topology-events-metrics -j")
969 self.handle.expect("topology-events-metrics -j")
970 self.handle.expect("onos>")
971
972 handle = self.handle.before
973
974 #Some color thing that we want to escape
975 ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
976 handle = ansi_escape.sub('', handle)
977
978 else:
979 self.handle.sendline("topology-events-metrics")
980 self.handle.expect("topology-events-metrics")
981 self.handle.expect("onos>")
982
983 handle = self.handle.before
984
985 return handle
986
987 except pexpect.EOF:
988 main.log.error(self.name + ": EOF exception found")
989 main.log.error(self.name + ": " + self.handle.before)
990 main.cleanup()
991 main.exit()
992 except:
993 main.log.info(self.name+" ::::::")
994 main.log.error( traceback.print_exc())
995 main.log.info(self.name+" ::::::")
996 main.cleanup()
997 main.exit()
998
andrewonlab3e15ead2014-10-15 14:21:34 -0400999 #Wrapper functions ****************
andrewonlab7e4d2d32014-10-15 13:23:21 -04001000 #Wrapper functions use existing driver
1001 #functions and extends their use case.
1002 #For example, we may use the output of
1003 #a normal driver function, and parse it
1004 #using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001005
andrewonlab9a50dfe2014-10-17 17:22:31 -04001006 def get_all_intents_id(self):
1007 '''
1008 Description:
1009 Obtain all intent id's in a list
1010 '''
1011 try:
1012 #Obtain output of intents function
1013 intents_str = self.intents()
1014 all_intent_list = []
1015 intent_id_list = []
1016
1017 #Parse the intents output for ID's
1018 intents_list = [s.strip() for s in intents_str.splitlines()]
1019 for intents in intents_list:
1020 if "onos>" in intents:
1021 continue
1022 elif "intents" in intents:
1023 continue
1024 else:
1025 line_list = intents.split(" ")
1026 all_intent_list.append(line_list[0])
1027
1028 all_intent_list = all_intent_list[1:-2]
1029
1030 for intents in all_intent_list:
1031 if not intents:
1032 continue
1033 else:
1034 intent_id_list.append(intents)
1035
1036 return intent_id_list
1037
1038 except pexpect.EOF:
1039 main.log.error(self.name + ": EOF exception found")
1040 main.log.error(self.name + ": " + self.handle.before)
1041 main.cleanup()
1042 main.exit()
1043 except:
1044 main.log.info(self.name+" ::::::")
1045 main.log.error( traceback.print_exc())
1046 main.log.info(self.name+" ::::::")
1047 main.cleanup()
1048 main.exit()
1049
andrewonlab7e4d2d32014-10-15 13:23:21 -04001050 def get_all_devices_id(self):
1051 '''
1052 Use 'devices' function to obtain list of all devices
1053 and parse the result to obtain a list of all device
1054 id's. Returns this list. Returns empty list if no
1055 devices exist
1056 List is ordered sequentially
andrewonlab3e15ead2014-10-15 14:21:34 -04001057
1058 This function may be useful if you are not sure of the
1059 device id, and wish to execute other commands using
1060 the ids. By obtaining the list of device ids on the fly,
1061 you can iterate through the list to get mastership, etc.
andrewonlab7e4d2d32014-10-15 13:23:21 -04001062 '''
1063 try:
1064 #Call devices and store result string
andrewonlab9a50dfe2014-10-17 17:22:31 -04001065 devices_str = self.devices(json_format=False)
andrewonlab7e4d2d32014-10-15 13:23:21 -04001066 id_list = []
1067
1068 if not devices_str:
1069 main.log.info("There are no devices to get id from")
1070 return id_list
1071
1072 #Split the string into list by comma
1073 device_list = devices_str.split(",")
1074 #Get temporary list of all arguments with string 'id='
1075 temp_list = [dev for dev in device_list if "id=" in dev]
1076 #Split list further into arguments before and after string
1077 # 'id='. Get the latter portion (the actual device id) and
1078 # append to id_list
1079 for arg in temp_list:
1080 id_list.append(arg.split("id=")[1])
andrewonlab7e4d2d32014-10-15 13:23:21 -04001081 return id_list
1082
1083 except pexpect.EOF:
1084 main.log.error(self.name + ": EOF exception found")
1085 main.log.error(self.name + ": " + self.handle.before)
1086 main.cleanup()
1087 main.exit()
1088 except:
1089 main.log.info(self.name+" ::::::")
1090 main.log.error( traceback.print_exc())
1091 main.log.info(self.name+" ::::::")
1092 main.cleanup()
1093 main.exit()
1094
andrewonlab7c211572014-10-15 16:45:20 -04001095 def get_all_nodes_id(self):
1096 '''
1097 Uses 'nodes' function to obtain list of all nodes
1098 and parse the result of nodes to obtain just the
1099 node id's.
1100 Returns:
1101 list of node id's
1102 '''
1103 try:
1104 nodes_str = self.nodes()
1105 id_list = []
1106
1107 if not nodes_str:
1108 main.log.info("There are no nodes to get id from")
1109 return id_list
1110
1111 #Sample nodes_str output
1112 #id=local, address=127.0.0.1:9876, state=ACTIVE *
1113
1114 #Split the string into list by comma
1115 nodes_list = nodes_str.split(",")
1116 temp_list = [node for node in nodes_list if "id=" in node]
1117 for arg in temp_list:
1118 id_list.append(arg.split("id=")[1])
1119
1120 return id_list
1121
1122 except pexpect.EOF:
1123 main.log.error(self.name + ": EOF exception found")
1124 main.log.error(self.name + ": " + self.handle.before)
1125 main.cleanup()
1126 main.exit()
1127 except:
1128 main.log.info(self.name+" ::::::")
1129 main.log.error( traceback.print_exc())
1130 main.log.info(self.name+" ::::::")
1131 main.cleanup()
1132 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001133
Jon Halla91c4dc2014-10-22 12:57:04 -04001134 def get_device(self, dpid=None):
1135 '''
1136 Return the first device from the devices api whose 'id' contains 'dpid'
1137 Return None if there is no match
1138 '''
1139 import json
1140 try:
1141 if dpid == None:
1142 return None
1143 else:
1144 dpid = dpid.replace(':', '')
1145 raw_devices = self.devices()
1146 devices_json = json.loads(raw_devices)
1147 #search json for the device with dpid then return the device
1148 for device in devices_json:
1149 #print "%s in %s?" % (dpid, device['id'])
1150 if dpid in device['id']:
1151 return device
1152 return None
1153 except pexpect.EOF:
1154 main.log.error(self.name + ": EOF exception found")
1155 main.log.error(self.name + ": " + self.handle.before)
1156 main.cleanup()
1157 main.exit()
1158 except:
1159 main.log.info(self.name+" ::::::")
1160 main.log.error( traceback.print_exc())
1161 main.log.info(self.name+" ::::::")
1162 main.cleanup()
1163 main.exit()
1164
andrewonlab7e4d2d32014-10-15 13:23:21 -04001165 #***********************************