blob: 8ca5cde9be452757b61d93cfa86fd7b68da02d0a [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
andrewonlabe8e56fd2014-10-09 17:12:44 -04002
Jon Hall05b2b432014-10-08 19:53:25 -04003'''
andrewonlabe8e56fd2014-10-09 17:12:44 -04004This driver interacts with ONOS bench, the OSGi platform
5that configures the ONOS nodes. (aka ONOS-next)
6
7Please follow the coding style demonstrated by existing
8functions and document properly.
9
10If you are a contributor to the driver, please
11list your email here for future contact:
12
13jhall@onlab.us
14andrew@onlab.us
15
16OCT 9 2014
17
Jon Hall05b2b432014-10-08 19:53:25 -040018'''
andrewonlabe8e56fd2014-10-09 17:12:44 -040019
andrewonlab7735d852014-10-09 13:02:47 -040020import sys
Jon Hall05b2b432014-10-08 19:53:25 -040021import time
22import pexpect
23import re
24import traceback
andrewonlab7735d852014-10-09 13:02:47 -040025import os.path
andrewonlab6e20c342014-10-10 18:08:48 -040026import pydoc
Jon Hall05b2b432014-10-08 19:53:25 -040027sys.path.append("../")
28from drivers.common.clidriver import CLI
29
30class OnosDriver(CLI):
31
32 def __init__(self):
andrewonlab6e20c342014-10-10 18:08:48 -040033 '''
34 Initialize client
35 '''
Jon Hall05b2b432014-10-08 19:53:25 -040036 super(CLI, self).__init__()
37
38 def connect(self,**connectargs):
39 '''
40 Creates ssh handle for ONOS "bench".
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']
Jon Hallea7818b2014-10-09 14:30:59 -040053 self.handle = super(OnosDriver,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)
Jon Hallea7818b2014-10-09 14:30:59 -040059
60 self.handle.sendline("cd "+ self.home)
61 self.handle.expect("\$")
Jon Hall05b2b432014-10-08 19:53:25 -040062 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:
Jon Hallf8d10982014-10-22 12:23:38 -040085 self.handle.sendline("\n")
86 self.handle.expect("\$")
Jon Hall05b2b432014-10-08 19:53:25 -040087 self.handle.sendline("exit")
88 self.handle.expect("closed")
89 except pexpect.EOF:
90 main.log.error(self.name + ": EOF exception found")
91 main.log.error(self.name + ": " + self.handle.before)
92 except:
93 main.log.error(self.name + ": Connection failed to the host")
94 response = main.FALSE
95 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040096
97 def onos_package(self):
98 '''
99 Produce a self-contained tar.gz file that can be deployed
100 and executed on any platform with Java 7 JRE.
101 '''
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400102
103 try:
104 self.handle.sendline("onos-package")
Jon Hallea7818b2014-10-09 14:30:59 -0400105 self.handle.expect("onos-package")
andrewonlab0748d2a2014-10-09 13:24:17 -0400106 self.handle.expect("tar.gz",timeout=10)
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400107 handle = str(self.handle.before)
108 main.log.info("onos-package command returned: "+
109 handle)
andrewonlab0748d2a2014-10-09 13:24:17 -0400110 #As long as the sendline does not time out,
111 #return true. However, be careful to interpret
112 #the results of the onos-package command return
113 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400114
andrewonlab7735d852014-10-09 13:02:47 -0400115 except pexpect.EOF:
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400116 main.log.error(self.name + ": EOF exception found")
117 main.log.error(self.name + ": " + self.handle.before)
118 except:
119 main.log.error("Failed to package ONOS")
120 main.cleanup()
121 main.exit()
122
andrewonlab8790abb2014-11-06 13:51:54 -0500123 def onos_build(self):
124 '''
125 Use the pre defined script to build onos via mvn
126 '''
127
128 try:
129 self.handle.sendline("onos-build")
130 self.handle.expect("onos-build")
131 i = self.handle.expect([
132 "BUILD SUCCESS",
133 "ERROR",
134 "BUILD FAILED"], timeout=120)
135 handle = str(self.handle.before)
136
137 main.log.info("onos-build command returned: "+
138 handle)
139
140 if i == 0:
141 return main.TRUE
142 else:
143 return handle
144
145 except pexpect.EOF:
146 main.log.error(self.name + ": EOF exception found")
147 main.log.error(self.name + ": " + self.handle.before)
148 except:
149 main.log.error("Failed to build ONOS")
150 main.cleanup()
151 main.exit()
152
Jon Hallde9d9aa2014-10-08 20:36:02 -0400153 def clean_install(self):
154 '''
155 Runs mvn clean install in the root of the ONOS directory.
156 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400157
Jon Hallde9d9aa2014-10-08 20:36:02 -0400158 Returns: main.TRUE on success
159 On Failure, exits the test
160 '''
161 try:
Jon Hallea7818b2014-10-09 14:30:59 -0400162 main.log.info("Running 'mvn clean install' on " + str(self.name) +
163 ". This may take some time.")
164 self.handle.sendline("cd "+ self.home)
165 self.handle.expect("\$")
166
167 self.handle.sendline("\n")
168 self.handle.expect("\$")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400169 self.handle.sendline("mvn clean install")
Jon Hallea7818b2014-10-09 14:30:59 -0400170 self.handle.expect("mvn clean install")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400171 while 1:
172 i=self.handle.expect([
173 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s\
174 Runtime\sEnvironment\sto\scontinue',
175 'BUILD\sFAILURE',
176 'BUILD\sSUCCESS',
177 'ONOS\$',
178 pexpect.TIMEOUT],timeout=600)
179 if i == 0:
180 main.log.error(self.name + ":There is insufficient memory \
181 for the Java Runtime Environment to continue.")
182 #return main.FALSE
183 main.cleanup()
184 main.exit()
185 if i == 1:
186 main.log.error(self.name + ": Build failure!")
187 #return main.FALSE
188 main.cleanup()
189 main.exit()
190 elif i == 2:
191 main.log.info(self.name + ": Build success!")
192 elif i == 3:
193 main.log.info(self.name + ": Build complete")
Jon Hallf8ef52c2014-10-09 19:37:33 -0400194 #Print the build time
195 for line in self.handle.before.splitlines():
196 if "Total time:" in line:
197 main.log.info(line)
Jon Hallea7818b2014-10-09 14:30:59 -0400198 self.handle.sendline("\n")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400199 self.handle.expect("\$", timeout=60)
200 return main.TRUE
201 elif i == 4:
202 main.log.error(self.name + ": mvn clean install TIMEOUT!")
203 #return main.FALSE
204 main.cleanup()
205 main.exit()
206 else:
207 main.log.error(self.name + ": unexpected response from \
208 mvn clean install")
209 #return main.FALSE
210 main.cleanup()
211 main.exit()
212 except pexpect.EOF:
213 main.log.error(self.name + ": EOF exception found")
214 main.log.error(self.name + ": " + self.handle.before)
215 main.cleanup()
216 main.exit()
217 except:
218 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
219 main.log.error( traceback.print_exc() )
220 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
221 main.cleanup()
222 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400223
224 def git_pull(self, comp1=""):
225 '''
226 Assumes that "git pull" works without login
227
228 This function will perform a git pull on the ONOS instance.
229 If used as git_pull("NODE") it will do git pull + NODE. This is
230 for the purpose of pulling from other nodes if necessary.
231
232 Otherwise, this function will perform a git pull in the
233 ONOS repository. If it has any problems, it will return main.ERROR
Shreya Shahee15f6c2014-10-28 18:12:30 -0400234 If it successfully does a git_pull, it will return a 1 (main.TRUE)
235 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400236
237 '''
238 try:
239 # main.log.info(self.name + ": Stopping ONOS")
240 #self.stop()
241 self.handle.sendline("cd " + self.home)
242 self.handle.expect("ONOS\$")
243 if comp1=="":
244 self.handle.sendline("git pull")
245 else:
246 self.handle.sendline("git pull " + comp1)
247
248 uptodate = 0
249 i=self.handle.expect(['fatal',
250 'Username\sfor\s(.*):\s',
251 '\sfile(s*) changed,\s',
252 'Already up-to-date',
253 'Aborting',
254 'You\sare\snot\scurrently\son\sa\sbranch',
255 'You\sasked\sme\sto\spull\swithout\stelling\sme\swhich\sbranch\syou',
256 'Pull\sis\snot\spossible\sbecause\syou\shave\sunmerged\sfiles',
257 pexpect.TIMEOUT],
258 timeout=300)
259 #debug
260 #main.log.report(self.name +": \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after))
261 if i==0:
262 main.log.error(self.name + ": Git pull had some issue...")
263 return main.ERROR
264 elif i==1:
265 main.log.error(self.name + ": Git Pull Asking for username. ")
266 return main.ERROR
267 elif i==2:
268 main.log.info(self.name + ": Git Pull - pulling repository now")
269 self.handle.expect("ONOS\$", 120)
Shreya Shahee15f6c2014-10-28 18:12:30 -0400270 return main.TRUE # So that only when git pull is done, we do mvn clean compile
Jon Hallacabffd2014-10-09 12:36:53 -0400271 elif i==3:
272 main.log.info(self.name + ": Git Pull - Already up to date")
Shreya Shahee15f6c2014-10-28 18:12:30 -0400273 return i
Jon Hallacabffd2014-10-09 12:36:53 -0400274 elif i==4:
275 main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?")
276 return main.ERROR
277 elif i==5:
278 main.log.info(self.name + ": Git Pull - You are not currently on a branch so git pull failed!")
279 return main.ERROR
280 elif i==6:
281 main.log.info(self.name + ": Git Pull - You have not configured an upstream branch to pull from. Git pull failed!")
282 return main.ERROR
283 elif i==7:
284 main.log.info(self.name + ": Git Pull - Pull is not possible because you have unmerged files.")
285 return main.ERROR
286 elif i==8:
287 main.log.error(self.name + ": Git Pull - TIMEOUT")
288 main.log.error(self.name + " Response was: " + str(self.handle.before))
289 return main.ERROR
290 else:
291 main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors")
292 return main.ERROR
293 except pexpect.EOF:
294 main.log.error(self.name + ": EOF exception found")
295 main.log.error(self.name + ": " + self.handle.before)
296 main.cleanup()
297 main.exit()
298 except:
299 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
300 main.log.error( traceback.print_exc() )
301 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
302 main.cleanup()
303 main.exit()
304
305 def git_checkout(self, branch="master"):
306 '''
307 Assumes that "git pull" works without login
308
309 This function will perform a git git checkout on the ONOS instance.
310 If used as git_checkout("branch") it will do git checkout of the "branch".
311
312 Otherwise, this function will perform a git checkout of the master
313 branch of the ONOS repository. If it has any problems, it will return
314 main.ERROR.
315 If the branch was already the specified branch, or the git checkout was
316 successful then the function will return main.TRUE.
317
318 '''
319 try:
320 # main.log.info(self.name + ": Stopping ONOS")
321 #self.stop()
322 self.handle.sendline("cd " + self.home)
323 self.handle.expect("ONOS\$")
324 if branch != 'master':
325 #self.handle.sendline('git stash')
326 #self.handle.expect('ONOS\$')
327 #print "After issuing git stash cmnd: ", self.handle.before
328 cmd = "git checkout "+branch
329 print "checkout cmd = ", cmd
330 self.handle.sendline(cmd)
331 uptodate = 0
332 i=self.handle.expect(['fatal',
333 'Username\sfor\s(.*):\s',
334 'Already\son\s\'',
335 'Switched\sto\sbranch\s\'',
336 pexpect.TIMEOUT],timeout=60)
337 else:
338 #self.handle.sendline('git stash apply')
339 #self.handle.expect('ONOS\$')
340 #print "After issuing git stash apply cmnd: ", self.handle.before
341 cmd = "git checkout "+branch
342 print "checkout cmd = ", cmd
343 self.handle.sendline(cmd)
344 uptodate = 0
345 switchedToMaster = 0
346 i=self.handle.expect(['fatal',
347 'Username\sfor\s(.*):\s',
348 'Already\son\s\'master\'',
349 'Switched\sto\sbranch\s\'master\'',
350 pexpect.TIMEOUT],timeout=60)
351
352
353 if i==0:
354 main.log.error(self.name + ": Git checkout had some issue...")
355 return main.ERROR
356 elif i==1:
357 main.log.error(self.name + ": Git checkout Asking for username!!! Bad!")
358 return main.ERROR
359 elif i==2:
360 main.log.info(self.name + ": Git Checkout %s : Already on this branch" %branch)
361 self.handle.expect("ONOS\$")
362 print "after checkout cmd = ", self.handle.before
363 switchedToMaster = 1
364 return main.TRUE
365 elif i==3:
366 main.log.info(self.name + ": Git checkout %s - Switched to this branch" %branch)
367 self.handle.expect("ONOS\$")
368 print "after checkout cmd = ", self.handle.before
369 switchedToMaster = 1
370 return main.TRUE
371 elif i==4:
372 main.log.error(self.name + ": Git Checkout- TIMEOUT")
373 main.log.error(self.name + " Response was: " + str(self.handle.before))
374 return main.ERROR
375 else:
376 main.log.error(self.name + ": Git Checkout - Unexpected response, check for pull errors")
377 return main.ERROR
378
379 except pexpect.EOF:
380 main.log.error(self.name + ": EOF exception found")
381 main.log.error(self.name + ": " + self.handle.before)
382 main.cleanup()
383 main.exit()
384 except:
385 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
386 main.log.error( traceback.print_exc() )
387 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
388 main.cleanup()
389 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400390
Jon Hallfd191202014-11-07 18:36:09 -0500391 def get_version(self, report=False):
Jon Hall45ec0922014-10-10 19:33:49 -0400392 '''
393 Writes the COMMIT number to the report to be parsed by Jenkins data collecter.
394 '''
395 try:
396 self.handle.sendline("export TERM=xterm-256color")
397 self.handle.expect("xterm-256color")
398 self.handle.expect("\$")
Jon Hall42db6dc2014-10-24 19:03:48 -0400399 self.handle.sendline("\n")
400 self.handle.expect("\$")
401 self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller --decorate=short | grep -A 6 \"commit\" --color=never")
402 self.handle.expect("--color=never")
Jon Hall45ec0922014-10-10 19:33:49 -0400403 self.handle.expect("\$")
404 response=(self.name +": \n"+ str(self.handle.before + self.handle.after))
Jon Hall42db6dc2014-10-24 19:03:48 -0400405 self.handle.sendline("cd " + self.home)
406 self.handle.expect("\$")
Jon Hall45ec0922014-10-10 19:33:49 -0400407 lines=response.splitlines()
408 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500409 print line
410 if report:
411 for line in lines[2:-1]:
412 main.log.report(line)
413 return lines[2]
Jon Hall45ec0922014-10-10 19:33:49 -0400414 except pexpect.EOF:
415 main.log.error(self.name + ": EOF exception found")
416 main.log.error(self.name + ": " + self.handle.before)
417 main.cleanup()
418 main.exit()
419 except:
420 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
421 main.log.error( traceback.print_exc() )
422 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
423 main.cleanup()
424 main.exit()
425
andrewonlabba44bcf2014-10-16 16:54:41 -0400426 def create_cell_file(self, bench_ip, file_name, mn_ip_addrs,
andrewonlab3f0a4af2014-10-17 12:25:14 -0400427 extra_feature_string, *onos_ip_addrs):
andrewonlab94282092014-10-10 13:00:11 -0400428 '''
429 Creates a cell file based on arguments
430 Required:
431 * Bench IP address (bench_ip)
432 - Needed to copy the cell file over
433 * File name of the cell file (file_name)
434 * Mininet IP address (mn_ip_addrs)
435 - Note that only 1 ip address is
436 supported currently
437 * ONOS IP addresses (onos_ip_addrs)
438 - Must be passed in as last arguments
439
440 NOTE: Assumes cells are located at:
441 ~/<self.home>/tools/test/cells/
442 '''
443
444 #Variable initialization
445 cell_directory = self.home + "/tools/test/cells/"
446 #We want to create the cell file in the dependencies directory
447 #of TestON first, then copy over to ONOS bench
448 temp_directory = "/tmp/"
449 #Create the cell file in the directory for writing (w+)
450 cell_file = open(temp_directory+file_name , 'w+')
andrewonlabd4940492014-10-24 12:21:27 -0400451
452 comment = ""
453 comment_string = "#"+ comment
andrewonlab7e4d2d32014-10-15 13:23:21 -0400454 #Feature string is hardcoded environment variables
455 #That you may wish to use by default on startup.
456 #Note that you may not want certain features listed
457 #on here.
andrewonlabba44bcf2014-10-16 16:54:41 -0400458 core_feature_string = "export ONOS_FEATURES=webconsole,onos-api,"+\
459 "onos-cli,onos-openflow,onos-app-mobility,onos-app-tvue,"+\
460 "onos-app-proxyarp,"+extra_feature_string
andrewonlab94282092014-10-10 13:00:11 -0400461 mn_string = "export OCN="
462 onos_string = "export OC"
463 temp_count = 1
464
465 #Create ONOS_NIC ip address prefix
466 temp_onos_ip = onos_ip_addrs[0]
467 temp_list = []
468 temp_list = temp_onos_ip.split(".")
andrewonlab7e4d2d32014-10-15 13:23:21 -0400469 #Omit last element of list to format for NIC
andrewonlab94282092014-10-10 13:00:11 -0400470 temp_list = temp_list[:-1]
471 #Structure the nic string ip
andrewonlabd4940492014-10-24 12:21:27 -0400472 nic_addr = ".".join(temp_list) + ".*"
andrewonlab94282092014-10-10 13:00:11 -0400473 onos_nic_string = "export ONOS_NIC="+nic_addr
474
475 try:
476 #Start writing to file
andrewonlabd4940492014-10-24 12:21:27 -0400477 cell_file.write(onos_nic_string + "\n")
andrewonlab94282092014-10-10 13:00:11 -0400478
479 for arg in onos_ip_addrs:
480 #For each argument in onos_ip_addrs, write to file
481 #Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400482 # export OC1="10.128.20.11"
483 # export OC2="10.128.20.12"
andrewonlab94282092014-10-10 13:00:11 -0400484 cell_file.write(onos_string + str(temp_count) +
andrewonlabd4940492014-10-24 12:21:27 -0400485 "=" + "\"" + arg + "\"" + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400486 temp_count = temp_count + 1
487
andrewonlabd4940492014-10-24 12:21:27 -0400488 cell_file.write(mn_string +"\""+ mn_ip_addrs +"\""+ "\n")
489 cell_file.write(core_feature_string + "\n")
andrewonlab94282092014-10-10 13:00:11 -0400490 cell_file.close()
491
492 #We use os.system to send the command to TestON cluster
493 #to account for the case in which TestON is not located
494 #on the same cluster as the ONOS bench
495 #Note that even if TestON is located on the same cluster
496 #as ONOS bench, you must setup passwordless ssh
andrewonlab7e4d2d32014-10-15 13:23:21 -0400497 #between TestON and ONOS bench in order to automate the test.
andrewonlab94282092014-10-10 13:00:11 -0400498 os.system("scp "+temp_directory+file_name+
499 " admin@"+bench_ip+":"+cell_directory)
500
andrewonlab2a6c9342014-10-16 13:40:15 -0400501 return main.TRUE
502
andrewonlab94282092014-10-10 13:00:11 -0400503 except pexpect.EOF:
504 main.log.error(self.name + ": EOF exception found")
505 main.log.error(self.name + ": " + self.handle.before)
506 main.cleanup()
507 main.exit()
508 except:
509 main.log.info(self.name + ":::::::::")
510 main.log.error( traceback.print_exc() )
511 main.log.info(":::::::")
512 main.cleanup()
513 main.exit()
514
andrewonlab95ca1462014-10-09 14:04:24 -0400515 def set_cell(self, cellname):
516 '''
517 Calls 'cell <name>' to set the environment variables on ONOSbench
518 '''
519 try:
520 if not cellname:
521 main.log.error("Must define cellname")
522 main.cleanup()
523 main.exit()
524 else:
525 self.handle.sendline("cell "+str(cellname))
526 #Expect the cellname in the ONOS_CELL variable.
527 #Note that this variable name is subject to change
528 # and that this driver will have to change accordingly
529 self.handle.expect("ONOS_CELL="+str(cellname))
530 handle_before = self.handle.before
531 handle_after = self.handle.after
andrewonlabc03bf6c2014-10-09 14:56:18 -0400532 #Get the rest of the handle
533 self.handle.sendline("")
534 self.handle.expect("\$")
535 handle_more = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400536
537 main.log.info("Cell call returned: "+handle_before+
andrewonlabc03bf6c2014-10-09 14:56:18 -0400538 handle_after + handle_more)
andrewonlab95ca1462014-10-09 14:04:24 -0400539
540 return main.TRUE
541
542 except pexpect.EOF:
543 main.log.error(self.name + ": EOF exception found")
544 main.log.error(self.name + ": " + self.handle.before)
545 main.cleanup()
546 main.exit()
547 except:
548 main.log.info(self.name+" ::::::")
549 main.log.error( traceback.print_exc())
550 main.log.info(self.name+" ::::::")
551 main.cleanup()
552 main.exit()
553
andrewonlabc03bf6c2014-10-09 14:56:18 -0400554 def verify_cell(self):
555 '''
556 Calls 'onos-verify-cell' to check for cell installation
557 '''
andrewonlab8d0d7d72014-10-09 16:33:15 -0400558 #TODO: Add meaningful expect value
559
andrewonlabc03bf6c2014-10-09 14:56:18 -0400560 try:
561 #Clean handle by sending empty and expecting $
562 self.handle.sendline("")
563 self.handle.expect("\$")
564 self.handle.sendline("onos-verify-cell")
565 self.handle.expect("\$")
566 handle_before = self.handle.before
567 handle_after = self.handle.after
568 #Get the rest of the handle
569 self.handle.sendline("")
570 self.handle.expect("\$")
571 handle_more = self.handle.before
572
573 main.log.info("Verify cell returned: "+handle_before+
574 handle_after + handle_more)
575
576 return main.TRUE
Jon Hall7993bfc2014-10-09 16:30:14 -0400577 except pexpect.EOF:
578 main.log.error(self.name + ": EOF exception found")
579 main.log.error(self.name + ": " + self.handle.before)
580 main.cleanup()
581 main.exit()
582 except:
583 main.log.info(self.name+" ::::::")
584 main.log.error( traceback.print_exc())
585 main.log.info(self.name+" ::::::")
586 main.cleanup()
587 main.exit()
588
andrewonlab05e362f2014-10-10 00:40:57 -0400589 def onos_cli(self, ONOS_ip, cmdstr):
590 '''
591 Uses 'onos' command to send various ONOS CLI arguments.
592 Required:
593 * ONOS_ip: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400594 * cmdstr: specify the command string to send
andrewonlab6e20c342014-10-10 18:08:48 -0400595
596 This function is intended to expose the entire karaf
597 CLI commands for ONOS. Try to use this function first
598 before attempting to write a ONOS CLI specific driver
599 function.
600 You can see a list of available 'cmdstr' arguments
601 by starting onos, and typing in 'onos' to enter the
602 onos> CLI. Then, type 'help' to see the list of
603 available commands.
andrewonlab05e362f2014-10-10 00:40:57 -0400604 '''
605 try:
606 if not ONOS_ip:
607 main.log.error("You must specify the IP address")
608 return main.FALSE
609 if not cmdstr:
610 main.log.error("You must specify the command string")
611 return main.FALSE
612
613 cmdstr = str(cmdstr)
614 self.handle.sendline("")
615 self.handle.expect("\$")
616
617 self.handle.sendline("onos -w " + ONOS_ip + " " + cmdstr)
618 self.handle.expect("\$")
619
Shreya Shaha73aaad2014-10-27 18:03:09 -0400620 handle_before = self.handle.before
621 print "handle_before = ", self.handle.before
andrewonlab05e362f2014-10-10 00:40:57 -0400622 handle_after = str(self.handle.after)
623
Shreya Shaha73aaad2014-10-27 18:03:09 -0400624 #self.handle.sendline("")
625 #self.handle.expect("\$")
626 #handle_more = str(self.handle.before)
andrewonlab05e362f2014-10-10 00:40:57 -0400627
628 main.log.info("Command sent successfully")
629
andrewonlab94282092014-10-10 13:00:11 -0400630 #Obtain return handle that consists of result from
631 #the onos command. The string may need to be
632 #configured further.
Shreya Shaha73aaad2014-10-27 18:03:09 -0400633 #return_string = handle_before + handle_after
634 return_string = handle_before
635 print "return_string = ", return_string
andrewonlab05e362f2014-10-10 00:40:57 -0400636 return return_string
637
638 except pexpect.EOF:
639 main.log.error(self.name + ": EOF exception found")
640 main.log.error(self.name + ": " + self.handle.before)
641 main.cleanup()
642 main.exit()
643 except:
644 main.log.info(self.name+" ::::::")
645 main.log.error( traceback.print_exc())
646 main.log.info(self.name+" ::::::")
647 main.cleanup()
648 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400649
650 def onos_install(self, options="-f", node = ""):
651 '''
652 Installs ONOS bits on the designated cell machine.
653 If -f option is provided, it also forces an uninstall.
654 Presently, install also includes onos-push-bits and
655 onos-config within.
656 The node option allows you to selectively only push the jar
657 files to certain onos nodes
658
659 Returns: main.TRUE on success and main.FALSE on failure
660 '''
661 try:
662 self.handle.sendline("onos-install " + options + " " + node)
663 self.handle.expect("onos-install ")
664 #NOTE: this timeout may need to change depending on the network and size of ONOS
665 i=self.handle.expect(["Network\sis\sunreachable",
666 "onos\sstart/running,\sprocess",
667 pexpect.TIMEOUT],timeout=60)
668
Jon Hall7993bfc2014-10-09 16:30:14 -0400669 if i == 0:
670 main.log.warn("Network is unreachable")
671 return main.FALSE
672 elif i == 1:
Jon Hall42db6dc2014-10-24 19:03:48 -0400673 main.log.info("ONOS was installed on " + node + " and started")
Jon Hall7993bfc2014-10-09 16:30:14 -0400674 return main.TRUE
675 elif i == 2:
Jon Hall42db6dc2014-10-24 19:03:48 -0400676 main.log.info("Installation of ONOS on " + node + " timed out")
Jon Hall7993bfc2014-10-09 16:30:14 -0400677 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400678
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()
andrewonlab95ca1462014-10-09 14:04:24 -0400690
andrewonlab8d0d7d72014-10-09 16:33:15 -0400691 def onos_start(self, node_ip):
692 '''
693 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400694 This command is a remote management of the ONOS upstart daemon
andrewonlab8d0d7d72014-10-09 16:33:15 -0400695 '''
696
697 try:
698 self.handle.sendline("")
699 self.handle.expect("\$")
700 self.handle.sendline("onos-service "+str(node_ip)+
701 " start")
702 i = self.handle.expect([
703 "Job\sis\salready\srunning",
704 "start/running",
705 "Unknown\sinstance",
Shreya Shahd01153d2014-10-23 15:08:56 -0400706 pexpect.TIMEOUT],timeout=120)
andrewonlab8d0d7d72014-10-09 16:33:15 -0400707
708 if i == 0:
709 main.log.info("Service is already running")
710 return main.TRUE
711 elif i == 1:
712 main.log.info("ONOS service started")
713 return main.TRUE
714 else:
715 main.log.error("ONOS service failed to start")
716 main.cleanup()
717 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400718 except pexpect.EOF:
719 main.log.error(self.name + ": EOF exception found")
720 main.log.error(self.name + ": " + self.handle.before)
721 main.cleanup()
722 main.exit()
723 except:
724 main.log.info(self.name+" ::::::")
725 main.log.error( traceback.print_exc())
726 main.log.info(self.name+" ::::::")
727 main.cleanup()
728 main.exit()
729
andrewonlab2b30bd32014-10-09 16:48:55 -0400730 def onos_stop(self, node_ip):
731 '''
732 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400733 This command is a remote management of the ONOS upstart daemon
andrewonlab2b30bd32014-10-09 16:48:55 -0400734 '''
735 try:
736 self.handle.sendline("")
737 self.handle.expect("\$")
738 self.handle.sendline("onos-service "+str(node_ip)+
739 " stop")
740 i = self.handle.expect([
741 "stop/waiting",
742 "Unknown\sinstance",
743 pexpect.TIMEOUT],timeout=60)
744
745 if i == 0:
746 main.log.info("ONOS service stopped")
747 return main.TRUE
748 elif i == 1:
749 main.log.info("Unknown ONOS instance specified: "+
750 str(node_ip))
751 return main.FALSE
752 else:
753 main.log.error("ONOS service failed to stop")
754 return main.FALSE
755
756 except pexpect.EOF:
757 main.log.error(self.name + ": EOF exception found")
758 main.log.error(self.name + ": " + self.handle.before)
759 main.cleanup()
760 main.exit()
761 except:
762 main.log.info(self.name+" ::::::")
763 main.log.error( traceback.print_exc())
764 main.log.info(self.name+" ::::::")
765 main.cleanup()
766 main.exit()
767
andrewonlabc8d47972014-10-09 16:52:36 -0400768 def onos_uninstall(self):
769 '''
770 Calls the command: 'onos-uninstall'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400771 Uninstalls ONOS from the designated cell machine, stopping
772 if needed
andrewonlabc8d47972014-10-09 16:52:36 -0400773 '''
774 try:
775 self.handle.sendline("")
776 self.handle.expect("\$")
777 self.handle.sendline("onos-uninstall")
778 self.handle.expect("\$")
779
andrewonlab84727452014-10-09 18:15:36 -0400780 main.log.info("ONOS cell machine was uninstalled")
andrewonlabc8d47972014-10-09 16:52:36 -0400781 #onos-uninstall command does not return any text
782 return main.TRUE
783
784 except pexpect.EOF:
785 main.log.error(self.name + ": EOF exception found")
786 main.log.error(self.name + ": " + self.handle.before)
787 main.cleanup()
788 main.exit()
789 except:
790 main.log.info(self.name+" ::::::")
791 main.log.error( traceback.print_exc())
792 main.log.info(self.name+" ::::::")
793 main.cleanup()
794 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400795
andrewonlabe8e56fd2014-10-09 17:12:44 -0400796 def onos_kill(self, node_ip):
797 '''
798 Calls the command: 'onos-kill [<node-ip>]'
799 "Remotely, and unceremoniously kills the ONOS instance running on
800 the specified cell machine" - Tom V
801 '''
802
803 try:
804 self.handle.sendline("")
805 self.handle.expect("\$")
806 self.handle.sendline("onos-kill " + str(node_ip))
807 i = self.handle.expect([
808 "\$",
809 "No\sroute\sto\shost",
810 "password:",
811 pexpect.TIMEOUT], timeout=20)
812
813 if i == 0:
814 main.log.info("ONOS instance "+str(node_ip)+" was killed")
815 return main.TRUE
816 elif i == 1:
817 main.log.info("No route to host")
818 return main.FALSE
819 elif i == 2:
820 main.log.info("Passwordless login for host: "+str(node_ip)+
821 " not configured")
822 return main.FALSE
823 else:
824 main.log.info("ONOS instasnce was not killed")
825 return main.FALSE
826
827 except pexpect.EOF:
828 main.log.error(self.name + ": EOF exception found")
829 main.log.error(self.name + ": " + self.handle.before)
830 main.cleanup()
831 main.exit()
832 except:
833 main.log.info(self.name+" ::::::")
834 main.log.error( traceback.print_exc())
835 main.log.info(self.name+" ::::::")
836 main.cleanup()
837 main.exit()
838
andrewonlab94282092014-10-10 13:00:11 -0400839 def onos_start_network(self, mntopo):
840 '''
841 Calls the command 'onos-start-network [<mininet-topo>]
842 "remotely starts the specified topology on the cell's
843 mininet machine against all controllers configured in the
844 cell."
845 * Specify mininet topology file name for mntopo
846 * Topo files should be placed at:
847 ~/<your-onos-directory>/tools/test/topos
848
849 NOTE: This function will take you to the mininet prompt
850 '''
851 try:
852 if not mntopo:
853 main.log.error("You must specify a topo file to execute")
854 return main.FALSE
855
856 mntopo = str(mntopo)
857 self.handle.sendline("")
858 self.handle.expect("\$")
859
860 self.handle.sendline("onos-start-network " + mntopo)
861 self.handle.expect("mininet>")
862 main.log.info("Network started, entered mininet prompt")
863
864 #TODO: Think about whether return is necessary or not
865
866 except pexpect.EOF:
867 main.log.error(self.name + ": EOF exception found")
868 main.log.error(self.name + ": " + self.handle.before)
869 main.cleanup()
870 main.exit()
871 except:
872 main.log.info(self.name+" ::::::")
873 main.log.error( traceback.print_exc())
874 main.log.info(self.name+" ::::::")
875 main.cleanup()
876 main.exit()
877
878
Jon Hall7993bfc2014-10-09 16:30:14 -0400879 def isup(self, node = ""):
880 '''
Jon Hall77f53ce2014-10-13 18:02:06 -0400881 Run's onos-wait-for-start which only returns once ONOS is at run
882 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -0400883
Jon Hall7993bfc2014-10-09 16:30:14 -0400884 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
885 '''
886 try:
887 self.handle.sendline("onos-wait-for-start " + node )
888 self.handle.expect("onos-wait-for-start")
889 #NOTE: this timeout is arbitrary"
shahshreya4d79aab2014-11-07 15:20:41 -0800890 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout = 300)
Jon Hall7993bfc2014-10-09 16:30:14 -0400891 if i == 0:
892 main.log.info(self.name + ": " + node + " is up")
893 return main.TRUE
894 elif i == 1:
895 #NOTE: since this function won't return until ONOS is ready,
896 # we will kill it on timeout
897 self.handle.sendline("\003") #Control-C
898 self.handle.expect("\$")
899 return main.FALSE
900 except pexpect.EOF:
901 main.log.error(self.name + ": EOF exception found")
902 main.log.error(self.name + ": " + self.handle.before)
903 main.cleanup()
904 main.exit()
905 except:
906 main.log.info(self.name+" ::::::")
907 main.log.error( traceback.print_exc())
908 main.log.info(self.name+" ::::::")
909 main.cleanup()
910 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -0400911
912
Shreya Shaha73aaad2014-10-27 18:03:09 -0400913 def get_topology(self,topology_output):
Jon Hall77f53ce2014-10-13 18:02:06 -0400914 '''
915 parses the onos:topology output
916 Returns: a topology dict populated by the key values found in
917 the cli command.
918 '''
919
920 try:
921 #call the cli to get the topology summary
Shreya Shaha73aaad2014-10-27 18:03:09 -0400922 #cmdstr = "onos:topology"
923 #cli_result = self.onos_cli(ip, cmdstr)
924 #print "cli_result = ", cli_result
Jon Hall77f53ce2014-10-13 18:02:06 -0400925
926 #Parse the output
927 topology = {}
928 #for line in cli_result.split("\n"):
Shreya Shaha73aaad2014-10-27 18:03:09 -0400929 for line in topology_output.splitlines():
Jon Hall77f53ce2014-10-13 18:02:06 -0400930 if not line.startswith("time="):
931 continue
932 #else
933 print line
934 for var in line.split(","):
Shreya Shaha73aaad2014-10-27 18:03:09 -0400935 #print "'"+var+"'"
936 #print "'"+var.strip()+"'"
Jon Hall77f53ce2014-10-13 18:02:06 -0400937 key, value = var.strip().split("=")
938 topology[key] = value
Shreya Shaha73aaad2014-10-27 18:03:09 -0400939 print "topology = ", topology
Jon Hall77f53ce2014-10-13 18:02:06 -0400940 devices = topology.get('devices', False)
Shreya Shaha73aaad2014-10-27 18:03:09 -0400941 print "devices = ", devices
Jon Hall77f53ce2014-10-13 18:02:06 -0400942 links = topology.get('links', False)
Shreya Shaha73aaad2014-10-27 18:03:09 -0400943 print "links = ", links
Jon Hall77f53ce2014-10-13 18:02:06 -0400944 clusters = topology.get('clusters', False)
Shreya Shaha73aaad2014-10-27 18:03:09 -0400945 print "clusters = ", clusters
Jon Hall77f53ce2014-10-13 18:02:06 -0400946 paths = topology.get('paths', False)
Shreya Shaha73aaad2014-10-27 18:03:09 -0400947 print "paths = ", paths
Jon Hall77f53ce2014-10-13 18:02:06 -0400948
949 return topology
950 except pexpect.EOF:
951 main.log.error(self.name + ": EOF exception found")
952 main.log.error(self.name + ": " + self.handle.before)
953 main.cleanup()
954 main.exit()
955 except:
956 main.log.info(self.name+" ::::::")
957 main.log.error( traceback.print_exc())
958 main.log.info(self.name+" ::::::")
959 main.cleanup()
960 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -0400961
962
Jon Hall77f53ce2014-10-13 18:02:06 -0400963
Shreya Shaha73aaad2014-10-27 18:03:09 -0400964 def check_status(self, topology_result, numoswitch, numolink, log_level="info"):
Jon Hall77f53ce2014-10-13 18:02:06 -0400965 '''
966 Checks the number of swithes & links that ONOS sees against the
967 supplied values. By default this will report to main.log, but the
968 log level can be specifid.
969
970 Params: ip = ip used for the onos cli
971 numoswitch = expected number of switches
972 numlink = expected number of links
973 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
974
975
976 log_level can
977
978 Returns: main.TRUE if the number of switchs and links are correct,
979 main.FALSE if the numer of switches and links is incorrect,
980 and main.ERROR otherwise
981 '''
982
983 try:
Shreya Shaha73aaad2014-10-27 18:03:09 -0400984 topology = self.get_topology(topology_result)
Jon Hall77f53ce2014-10-13 18:02:06 -0400985 if topology == {}:
986 return main.ERROR
987 output = ""
988 #Is the number of switches is what we expected
989 devices = topology.get('devices',False)
990 links = topology.get('links',False)
991 if devices == False or links == False:
992 return main.ERROR
993 switch_check = ( int(devices) == int(numoswitch) )
994 #Is the number of links is what we expected
995 link_check = ( int(links) == int(numolink) )
996 if (switch_check and link_check):
997 #We expected the correct numbers
998 output = output + "The number of links and switches match "\
999 + "what was expected"
1000 result = main.TRUE
1001 else:
1002 output = output + \
1003 "The number of links and switches does not match what was expected"
1004 result = main.FALSE
1005 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1006 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1007 if log_level == "report":
1008 main.log.report(output)
1009 elif log_level == "warn":
1010 main.log.warn(output)
1011 else:
1012 main.log.info(output)
1013 return result
1014 except pexpect.EOF:
1015 main.log.error(self.name + ": EOF exception found")
1016 main.log.error(self.name + ": " + self.handle.before)
1017 main.cleanup()
1018 main.exit()
1019 except:
1020 main.log.info(self.name+" ::::::")
1021 main.log.error( traceback.print_exc())
1022 main.log.info(self.name+" ::::::")
1023 main.cleanup()
1024 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001025
andrewonlab970399c2014-11-07 13:09:32 -05001026 def tshark_pcap(self, interface, dir_file):
1027 '''
1028 Capture all packet activity and store in specified
1029 directory/file
1030
1031 Required:
1032 * interface: interface to capture
1033 * dir: directory/filename to store pcap
1034 '''
1035 self.handle.sendline("")
1036 self.handle.expect("\$")
1037
1038 self.handle.sendline("tshark -i "+str(interface)+
1039 " -t e -w "+str(dir_file))
1040 self.handle.expect("Capturing on")
1041
1042 main.log.info("Tshark started capturing files on "+
1043 str(interface)+ " and saving to directory: "+
1044 str(dir_File))
Shreya Shaha73aaad2014-10-27 18:03:09 -04001045
shahshreya4d79aab2014-11-07 15:20:41 -08001046
1047 def run_onos_topo_cfg(self):
1048 '''
1049 On ONOS bench, run this command: ./~/ONOS/tools/test/bin/onos-topo-cfg
1050 which starts the rest and copies the json file to the onos instance
1051 '''
1052 self.handle.sendline("")
1053 self.handle.expect("\$")
1054 self.handle.sendline("cd ~/ONOS/tools/test/bin")
1055 self.handle.expect("/bin$")
1056 self.handle.sendline("./onos-topo-cfg")
1057 self.handle.expect("{}admin@onosTestBench")
1058 self.handle.sendline("cd ~")
1059 self.handle.expect("\$")
1060
1061
andrewonlabba44bcf2014-10-16 16:54:41 -04001062 def tshark_grep(self, grep, directory, interface='eth0'):
1063 '''
1064 Required:
1065 * grep string
1066 * directory to store results
1067 Optional:
1068 * interface - default: eth0
1069 Description:
1070 Uses tshark command to grep specific group of packets
1071 and stores the results to specified directory.
1072 The timestamp is hardcoded to be in epoch
1073 '''
1074 self.handle.sendline("")
1075 self.handle.expect("\$")
andrewonlabefd7f3d2014-10-21 16:02:31 -04001076 self.handle.sendline("")
andrewonlabba44bcf2014-10-16 16:54:41 -04001077 self.handle.sendline("tshark -i "+str(interface)+
andrewonlabd4940492014-10-24 12:21:27 -04001078 " -t e | grep --line-buffered \""+str(grep)+"\" >"+directory+" &")
andrewonlabba44bcf2014-10-16 16:54:41 -04001079 self.handle.sendline("\r")
1080 self.handle.expect("Capturing on")
1081 self.handle.sendline("\r")
1082 self.handle.expect("\$")
1083
1084 def tshark_stop(self):
1085 '''
1086 Removes wireshark files from /tmp and kills all tshark processes
1087 '''
andrewonlab5ba0d9e2014-10-24 13:32:23 -04001088 #Remove all pcap from previous captures
1089 self.execute(cmd="sudo rm /tmp/wireshark*")
andrewonlabba44bcf2014-10-16 16:54:41 -04001090 self.handle.sendline("")
1091 self.handle.sendline("sudo kill -9 `ps -ef | grep \"tshark -i\" |"+
1092 " grep -v grep | awk '{print $2}'`")
1093 self.handle.sendline("")
1094 main.log.info("Tshark stopped")
1095
andrewonlab0c38a4a2014-10-28 18:35:35 -04001096 def ptpd(self, args):
1097 '''
1098 Initiate ptp with user-specified args.
1099 Required:
1100 * args: specify string of args after command
1101 'sudo ptpd'
1102 '''
1103 try:
1104 self.handle.sendline("sudo ptpd "+str(args))
andrewonlab978dff62014-10-29 16:53:35 -04001105 i = self.handle.expect([
andrewonlab0c38a4a2014-10-28 18:35:35 -04001106 "Multiple",
1107 "Error",
1108 "\$"])
1109 self.handle.expect("\$")
andrewonlabba44bcf2014-10-16 16:54:41 -04001110
andrewonlab0c38a4a2014-10-28 18:35:35 -04001111 if i == 0:
1112 handle = self.handle.before
1113 main.log.info("ptpd returned an error: "+
1114 str(handle))
1115 return handle
1116 elif i == 1:
1117 handle = self.handle.before
1118 main.log.error("ptpd returned an error: "+
1119 str(handle))
1120 return handle
1121 else:
1122 return main.TRUE
1123
1124 except pexpect.EOF:
1125 main.log.error(self.name + ": EOF exception found")
1126 main.log.error(self.name + ": " + self.handle.before)
1127 main.cleanup()
1128 main.exit()
1129 except:
1130 main.log.info(self.name+" ::::::")
1131 main.log.error( traceback.print_exc())
1132 main.log.info(self.name+" ::::::")
1133 main.cleanup()
1134 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001135
1136