blob: 46ec33a841a11418db9ea8fa11013a1610d805a5 [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
Jon Hall05b2b432014-10-08 19:53:25 -040023import traceback
andrewonlab7735d852014-10-09 13:02:47 -040024import os.path
Jon Hall05b2b432014-10-08 19:53:25 -040025sys.path.append("../")
26from drivers.common.clidriver import CLI
27
28class OnosDriver(CLI):
29
30 def __init__(self):
andrewonlab6e20c342014-10-10 18:08:48 -040031 '''
32 Initialize client
33 '''
Jon Hall05b2b432014-10-08 19:53:25 -040034 super(CLI, self).__init__()
35
36 def connect(self,**connectargs):
37 '''
38 Creates ssh handle for ONOS "bench".
39 '''
40 try:
41 for key in connectargs:
42 vars(self)[key] = connectargs[key]
43 self.home = "~/ONOS"
44 for key in self.options:
45 if key == "home":
46 self.home = self.options['home']
47 break
48
49
50 self.name = self.options['name']
Jon Hallea7818b2014-10-09 14:30:59 -040051 self.handle = super(OnosDriver,self).connect(
52 user_name = self.user_name,
53 ip_address = self.ip_address,
54 port = self.port,
55 pwd = self.pwd,
56 home = self.home)
Jon Hallea7818b2014-10-09 14:30:59 -040057
58 self.handle.sendline("cd "+ self.home)
59 self.handle.expect("\$")
Jon Hall05b2b432014-10-08 19:53:25 -040060 if self.handle:
61 return self.handle
62 else :
63 main.log.info("NO ONOS HANDLE")
64 return main.FALSE
65 except pexpect.EOF:
66 main.log.error(self.name + ": EOF exception found")
67 main.log.error(self.name + ": " + self.handle.before)
68 main.cleanup()
69 main.exit()
70 except:
71 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
72 main.log.error( traceback.print_exc() )
73 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
74 main.cleanup()
75 main.exit()
76
77 def disconnect(self):
78 '''
79 Called when Test is complete to disconnect the ONOS handle.
80 '''
81 response = ''
82 try:
Jon Hallffb386d2014-11-21 13:43:38 -080083 self.handle.sendline("")
Jon Hallf8d10982014-10-22 12:23:38 -040084 self.handle.expect("\$")
Jon Hall05b2b432014-10-08 19:53:25 -040085 self.handle.sendline("exit")
86 self.handle.expect("closed")
87 except pexpect.EOF:
88 main.log.error(self.name + ": EOF exception found")
89 main.log.error(self.name + ": " + self.handle.before)
90 except:
91 main.log.error(self.name + ": Connection failed to the host")
92 response = main.FALSE
93 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040094
95 def onos_package(self):
96 '''
97 Produce a self-contained tar.gz file that can be deployed
98 and executed on any platform with Java 7 JRE.
99 '''
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400100
101 try:
102 self.handle.sendline("onos-package")
Jon Hallea7818b2014-10-09 14:30:59 -0400103 self.handle.expect("onos-package")
andrewonlabe9907902014-11-25 15:49:06 -0500104 self.handle.expect("tar.gz",timeout=30)
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400105 handle = str(self.handle.before)
106 main.log.info("onos-package command returned: "+
107 handle)
andrewonlab0748d2a2014-10-09 13:24:17 -0400108 #As long as the sendline does not time out,
109 #return true. However, be careful to interpret
110 #the results of the onos-package command return
111 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400112
andrewonlab7735d852014-10-09 13:02:47 -0400113 except pexpect.EOF:
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400114 main.log.error(self.name + ": EOF exception found")
115 main.log.error(self.name + ": " + self.handle.before)
116 except:
117 main.log.error("Failed to package ONOS")
118 main.cleanup()
119 main.exit()
120
andrewonlab8790abb2014-11-06 13:51:54 -0500121 def onos_build(self):
122 '''
123 Use the pre defined script to build onos via mvn
124 '''
125
126 try:
127 self.handle.sendline("onos-build")
128 self.handle.expect("onos-build")
129 i = self.handle.expect([
130 "BUILD SUCCESS",
131 "ERROR",
132 "BUILD FAILED"], timeout=120)
133 handle = str(self.handle.before)
134
135 main.log.info("onos-build command returned: "+
136 handle)
137
138 if i == 0:
139 return main.TRUE
140 else:
141 return handle
142
143 except pexpect.EOF:
144 main.log.error(self.name + ": EOF exception found")
145 main.log.error(self.name + ": " + self.handle.before)
146 except:
147 main.log.error("Failed to build ONOS")
148 main.cleanup()
149 main.exit()
150
Jon Hallde9d9aa2014-10-08 20:36:02 -0400151 def clean_install(self):
152 '''
153 Runs mvn clean install in the root of the ONOS directory.
154 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400155
Jon Hallde9d9aa2014-10-08 20:36:02 -0400156 Returns: main.TRUE on success
157 On Failure, exits the test
158 '''
159 try:
Jon Hallea7818b2014-10-09 14:30:59 -0400160 main.log.info("Running 'mvn clean install' on " + str(self.name) +
161 ". This may take some time.")
162 self.handle.sendline("cd "+ self.home)
163 self.handle.expect("\$")
164
Jon Hallffb386d2014-11-21 13:43:38 -0800165 self.handle.sendline("")
Jon Hallea7818b2014-10-09 14:30:59 -0400166 self.handle.expect("\$")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400167 self.handle.sendline("mvn clean install")
Jon Hallea7818b2014-10-09 14:30:59 -0400168 self.handle.expect("mvn clean install")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400169 while 1:
170 i=self.handle.expect([
171 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s\
172 Runtime\sEnvironment\sto\scontinue',
173 'BUILD\sFAILURE',
174 'BUILD\sSUCCESS',
175 'ONOS\$',
176 pexpect.TIMEOUT],timeout=600)
177 if i == 0:
178 main.log.error(self.name + ":There is insufficient memory \
179 for the Java Runtime Environment to continue.")
180 #return main.FALSE
181 main.cleanup()
182 main.exit()
183 if i == 1:
184 main.log.error(self.name + ": Build failure!")
185 #return main.FALSE
186 main.cleanup()
187 main.exit()
188 elif i == 2:
189 main.log.info(self.name + ": Build success!")
190 elif i == 3:
191 main.log.info(self.name + ": Build complete")
Jon Hallf8ef52c2014-10-09 19:37:33 -0400192 #Print the build time
193 for line in self.handle.before.splitlines():
194 if "Total time:" in line:
195 main.log.info(line)
Jon Hallffb386d2014-11-21 13:43:38 -0800196 self.handle.sendline("")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400197 self.handle.expect("\$", timeout=60)
198 return main.TRUE
199 elif i == 4:
200 main.log.error(self.name + ": mvn clean install TIMEOUT!")
201 #return main.FALSE
202 main.cleanup()
203 main.exit()
204 else:
205 main.log.error(self.name + ": unexpected response from \
206 mvn clean install")
207 #return main.FALSE
208 main.cleanup()
209 main.exit()
210 except pexpect.EOF:
211 main.log.error(self.name + ": EOF exception found")
212 main.log.error(self.name + ": " + self.handle.before)
213 main.cleanup()
214 main.exit()
215 except:
216 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
217 main.log.error( traceback.print_exc() )
218 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
219 main.cleanup()
220 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400221
222 def git_pull(self, comp1=""):
223 '''
224 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800225
Jon Hallacabffd2014-10-09 12:36:53 -0400226 This function will perform a git pull on the ONOS instance.
227 If used as git_pull("NODE") it will do git pull + NODE. This is
228 for the purpose of pulling from other nodes if necessary.
229
Jon Hall47a93fb2015-01-06 16:46:06 -0800230 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400231 ONOS repository. If it has any problems, it will return main.ERROR
Shreya Shahee15f6c2014-10-28 18:12:30 -0400232 If it successfully does a git_pull, it will return a 1 (main.TRUE)
233 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400234
235 '''
236 try:
237 # main.log.info(self.name + ": Stopping ONOS")
238 #self.stop()
239 self.handle.sendline("cd " + self.home)
240 self.handle.expect("ONOS\$")
241 if comp1=="":
242 self.handle.sendline("git pull")
243 else:
244 self.handle.sendline("git pull " + comp1)
Jon Hall47a93fb2015-01-06 16:46:06 -0800245
Jon Hallacabffd2014-10-09 12:36:53 -0400246 i=self.handle.expect(['fatal',
247 'Username\sfor\s(.*):\s',
248 '\sfile(s*) changed,\s',
249 'Already up-to-date',
250 'Aborting',
Jon Hall47a93fb2015-01-06 16:46:06 -0800251 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hallacabffd2014-10-09 12:36:53 -0400252 'You\sasked\sme\sto\spull\swithout\stelling\sme\swhich\sbranch\syou',
253 'Pull\sis\snot\spossible\sbecause\syou\shave\sunmerged\sfiles',
254 pexpect.TIMEOUT],
255 timeout=300)
256 #debug
Jon Hall81e29af2014-11-04 20:41:23 -0500257 #main.log.report(self.name +": DEBUG: \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after))
Jon Hallacabffd2014-10-09 12:36:53 -0400258 if i==0:
259 main.log.error(self.name + ": Git pull had some issue...")
260 return main.ERROR
261 elif i==1:
262 main.log.error(self.name + ": Git Pull Asking for username. ")
263 return main.ERROR
264 elif i==2:
265 main.log.info(self.name + ": Git Pull - pulling repository now")
266 self.handle.expect("ONOS\$", 120)
Shreya Shahee15f6c2014-10-28 18:12:30 -0400267 return main.TRUE # So that only when git pull is done, we do mvn clean compile
Jon Hallacabffd2014-10-09 12:36:53 -0400268 elif i==3:
269 main.log.info(self.name + ": Git Pull - Already up to date")
Jon Hall47a93fb2015-01-06 16:46:06 -0800270 return i
Jon Hallacabffd2014-10-09 12:36:53 -0400271 elif i==4:
272 main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?")
273 return main.ERROR
274 elif i==5:
275 main.log.info(self.name + ": Git Pull - You are not currently on a branch so git pull failed!")
276 return main.ERROR
277 elif i==6:
278 main.log.info(self.name + ": Git Pull - You have not configured an upstream branch to pull from. Git pull failed!")
279 return main.ERROR
280 elif i==7:
281 main.log.info(self.name + ": Git Pull - Pull is not possible because you have unmerged files.")
282 return main.ERROR
283 elif i==8:
284 main.log.error(self.name + ": Git Pull - TIMEOUT")
285 main.log.error(self.name + " Response was: " + str(self.handle.before))
286 return main.ERROR
287 else:
288 main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors")
289 return main.ERROR
290 except pexpect.EOF:
291 main.log.error(self.name + ": EOF exception found")
292 main.log.error(self.name + ": " + self.handle.before)
293 main.cleanup()
294 main.exit()
295 except:
296 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
297 main.log.error( traceback.print_exc() )
298 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
299 main.cleanup()
300 main.exit()
301
302 def git_checkout(self, branch="master"):
303 '''
304 Assumes that "git pull" works without login
305
306 This function will perform a git git checkout on the ONOS instance.
307 If used as git_checkout("branch") it will do git checkout of the "branch".
308
309 Otherwise, this function will perform a git checkout of the master
310 branch of the ONOS repository. If it has any problems, it will return
311 main.ERROR.
312 If the branch was already the specified branch, or the git checkout was
313 successful then the function will return main.TRUE.
314
315 '''
316 try:
Jon Hallacabffd2014-10-09 12:36:53 -0400317 self.handle.sendline("cd " + self.home)
318 self.handle.expect("ONOS\$")
Jon Hall81e29af2014-11-04 20:41:23 -0500319 main.log.info(self.name + ": Checking out git branch: " + branch + "...")
320 cmd = "git checkout "+branch
321 self.handle.sendline(cmd)
322 self.handle.expect(cmd)
323 i=self.handle.expect(['fatal',
324 'Username\sfor\s(.*):\s',
325 'Already\son\s\'',
326 'Switched\sto\sbranch\s\'' + str(branch),
327 pexpect.TIMEOUT,
328 'error: Your local changes to the following files would be overwritten by checkout:',
329 'error: you need to resolve your current index first'],timeout=60)
Jon Hallacabffd2014-10-09 12:36:53 -0400330
331 if i==0:
332 main.log.error(self.name + ": Git checkout had some issue...")
Jon Hall81e29af2014-11-04 20:41:23 -0500333 main.log.error(self.name + ": " + self.handle.before)
Jon Hallacabffd2014-10-09 12:36:53 -0400334 return main.ERROR
335 elif i==1:
Jon Hall81e29af2014-11-04 20:41:23 -0500336 main.log.error(self.name + ": Git checkout asking for username."\
337 +" Please configure your local git repository to be able "\
338 +"to access your remote repository passwordlessly")
Jon Hallacabffd2014-10-09 12:36:53 -0400339 return main.ERROR
340 elif i==2:
341 main.log.info(self.name + ": Git Checkout %s : Already on this branch" %branch)
342 self.handle.expect("ONOS\$")
Jon Hall81e29af2014-11-04 20:41:23 -0500343 #main.log.info("DEBUG: after checkout cmd = "+ self.handle.before)
Jon Hallacabffd2014-10-09 12:36:53 -0400344 return main.TRUE
345 elif i==3:
346 main.log.info(self.name + ": Git checkout %s - Switched to this branch" %branch)
347 self.handle.expect("ONOS\$")
Jon Hall81e29af2014-11-04 20:41:23 -0500348 #main.log.info("DEBUG: after checkout cmd = "+ self.handle.before)
Jon Hallacabffd2014-10-09 12:36:53 -0400349 return main.TRUE
350 elif i==4:
351 main.log.error(self.name + ": Git Checkout- TIMEOUT")
352 main.log.error(self.name + " Response was: " + str(self.handle.before))
353 return main.ERROR
Jon Hall81e29af2014-11-04 20:41:23 -0500354 elif i==5:
355 self.handle.expect("Aborting")
356 main.log.error(self.name + ": Git checkout error: \n" + \
357 "Your local changes to the following files would be overwritten by checkout:" + \
358 str(self.handle.before))
359 self.handle.expect("ONOS\$")
360 return main.ERROR
361 elif i==6:
362 main.log.error(self.name + ": Git checkout error: \n" + \
363 "You need to resolve your current index first:" + \
364 str(self.handle.before))
365 self.handle.expect("ONOS\$")
366 return main.ERROR
Jon Hallacabffd2014-10-09 12:36:53 -0400367 else:
368 main.log.error(self.name + ": Git Checkout - Unexpected response, check for pull errors")
Jon Hall81e29af2014-11-04 20:41:23 -0500369 main.log.error(self.name + ": " + self.handle.before)
Jon Hallacabffd2014-10-09 12:36:53 -0400370 return main.ERROR
371
372 except pexpect.EOF:
373 main.log.error(self.name + ": EOF exception found")
374 main.log.error(self.name + ": " + self.handle.before)
375 main.cleanup()
376 main.exit()
377 except:
378 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
379 main.log.error( traceback.print_exc() )
380 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
381 main.cleanup()
382 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400383
Jon Hallfd191202014-11-07 18:36:09 -0500384 def get_version(self, report=False):
Jon Hall45ec0922014-10-10 19:33:49 -0400385 '''
386 Writes the COMMIT number to the report to be parsed by Jenkins data collecter.
387 '''
388 try:
Jon Hallffb386d2014-11-21 13:43:38 -0800389 self.handle.sendline("")
Jon Hall42db6dc2014-10-24 19:03:48 -0400390 self.handle.expect("\$")
391 self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller --decorate=short | grep -A 6 \"commit\" --color=never")
Jon Hall73cf9cc2014-11-20 22:28:38 -0800392 #NOTE: for some reason there are backspaces inserted in this phrase when run from Jenkins on some tests
Jon Hallffb386d2014-11-21 13:43:38 -0800393 self.handle.expect("never")
Jon Hall45ec0922014-10-10 19:33:49 -0400394 self.handle.expect("\$")
395 response=(self.name +": \n"+ str(self.handle.before + self.handle.after))
Jon Hall42db6dc2014-10-24 19:03:48 -0400396 self.handle.sendline("cd " + self.home)
397 self.handle.expect("\$")
Jon Hall45ec0922014-10-10 19:33:49 -0400398 lines=response.splitlines()
399 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500400 print line
401 if report:
402 for line in lines[2:-1]:
andrewonlabac5810a2014-11-18 14:33:03 -0500403 #Bracket replacement is for Wiki-compliant
404 #formatting. '<' or '>' are interpreted
405 #as xml specific tags that cause errors
406 line = line.replace("<","[")
407 line = line.replace(">","]")
Jon Hallb1290e82014-11-18 16:17:48 -0500408 main.log.report("\t" + line)
Jon Hallfd191202014-11-07 18:36:09 -0500409 return lines[2]
Jon Hall45ec0922014-10-10 19:33:49 -0400410 except pexpect.EOF:
411 main.log.error(self.name + ": EOF exception found")
412 main.log.error(self.name + ": " + self.handle.before)
413 main.cleanup()
414 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800415 except pexpect.TIMEOUT:
416 main.log.error(self.name + ": TIMEOUT exception found")
417 main.log.error(self.name + ": " + self.handle.before)
418 main.cleanup()
419 main.exit()
Jon Hall45ec0922014-10-10 19:33:49 -0400420 except:
421 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
422 main.log.error( traceback.print_exc() )
423 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
424 main.cleanup()
425 main.exit()
426
andrewonlabba44bcf2014-10-16 16:54:41 -0400427 def create_cell_file(self, bench_ip, file_name, mn_ip_addrs,
andrewonlab3f0a4af2014-10-17 12:25:14 -0400428 extra_feature_string, *onos_ip_addrs):
andrewonlab94282092014-10-10 13:00:11 -0400429 '''
430 Creates a cell file based on arguments
431 Required:
432 * Bench IP address (bench_ip)
433 - Needed to copy the cell file over
434 * File name of the cell file (file_name)
435 * Mininet IP address (mn_ip_addrs)
436 - Note that only 1 ip address is
437 supported currently
438 * ONOS IP addresses (onos_ip_addrs)
439 - Must be passed in as last arguments
440
441 NOTE: Assumes cells are located at:
442 ~/<self.home>/tools/test/cells/
443 '''
444
445 #Variable initialization
446 cell_directory = self.home + "/tools/test/cells/"
447 #We want to create the cell file in the dependencies directory
448 #of TestON first, then copy over to ONOS bench
449 temp_directory = "/tmp/"
450 #Create the cell file in the directory for writing (w+)
451 cell_file = open(temp_directory+file_name , 'w+')
andrewonlabd4940492014-10-24 12:21:27 -0400452
andrewonlab7e4d2d32014-10-15 13:23:21 -0400453 #Feature string is hardcoded environment variables
454 #That you may wish to use by default on startup.
455 #Note that you may not want certain features listed
456 #on here.
andrewonlabba44bcf2014-10-16 16:54:41 -0400457 core_feature_string = "export ONOS_FEATURES=webconsole,onos-api,"+\
andrewonlabaedc8332014-12-04 12:43:03 -0500458 "onos-cli,onos-openflow,"+extra_feature_string
andrewonlab94282092014-10-10 13:00:11 -0400459 mn_string = "export OCN="
460 onos_string = "export OC"
461 temp_count = 1
462
463 #Create ONOS_NIC ip address prefix
464 temp_onos_ip = onos_ip_addrs[0]
465 temp_list = []
466 temp_list = temp_onos_ip.split(".")
andrewonlab7e4d2d32014-10-15 13:23:21 -0400467 #Omit last element of list to format for NIC
andrewonlab94282092014-10-10 13:00:11 -0400468 temp_list = temp_list[:-1]
469 #Structure the nic string ip
andrewonlabd4940492014-10-24 12:21:27 -0400470 nic_addr = ".".join(temp_list) + ".*"
andrewonlab94282092014-10-10 13:00:11 -0400471 onos_nic_string = "export ONOS_NIC="+nic_addr
472
473 try:
474 #Start writing to file
andrewonlabd4940492014-10-24 12:21:27 -0400475 cell_file.write(onos_nic_string + "\n")
andrewonlab94282092014-10-10 13:00:11 -0400476
477 for arg in onos_ip_addrs:
478 #For each argument in onos_ip_addrs, write to file
479 #Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400480 # export OC1="10.128.20.11"
481 # export OC2="10.128.20.12"
andrewonlab94282092014-10-10 13:00:11 -0400482 cell_file.write(onos_string + str(temp_count) +
andrewonlabd4940492014-10-24 12:21:27 -0400483 "=" + "\"" + arg + "\"" + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400484 temp_count = temp_count + 1
485
andrewonlabd4940492014-10-24 12:21:27 -0400486 cell_file.write(mn_string +"\""+ mn_ip_addrs +"\""+ "\n")
487 cell_file.write(core_feature_string + "\n")
andrewonlab94282092014-10-10 13:00:11 -0400488 cell_file.close()
489
490 #We use os.system to send the command to TestON cluster
491 #to account for the case in which TestON is not located
492 #on the same cluster as the ONOS bench
493 #Note that even if TestON is located on the same cluster
494 #as ONOS bench, you must setup passwordless ssh
andrewonlab7e4d2d32014-10-15 13:23:21 -0400495 #between TestON and ONOS bench in order to automate the test.
andrewonlab94282092014-10-10 13:00:11 -0400496 os.system("scp "+temp_directory+file_name+
497 " admin@"+bench_ip+":"+cell_directory)
498
andrewonlab2a6c9342014-10-16 13:40:15 -0400499 return main.TRUE
500
andrewonlab94282092014-10-10 13:00:11 -0400501 except pexpect.EOF:
502 main.log.error(self.name + ": EOF exception found")
503 main.log.error(self.name + ": " + self.handle.before)
504 main.cleanup()
505 main.exit()
506 except:
507 main.log.info(self.name + ":::::::::")
508 main.log.error( traceback.print_exc() )
509 main.log.info(":::::::")
510 main.cleanup()
511 main.exit()
512
andrewonlab95ca1462014-10-09 14:04:24 -0400513 def set_cell(self, cellname):
514 '''
515 Calls 'cell <name>' to set the environment variables on ONOSbench
516 '''
517 try:
518 if not cellname:
519 main.log.error("Must define cellname")
520 main.cleanup()
521 main.exit()
522 else:
523 self.handle.sendline("cell "+str(cellname))
524 #Expect the cellname in the ONOS_CELL variable.
525 #Note that this variable name is subject to change
526 # and that this driver will have to change accordingly
527 self.handle.expect("ONOS_CELL="+str(cellname))
528 handle_before = self.handle.before
529 handle_after = self.handle.after
andrewonlabc03bf6c2014-10-09 14:56:18 -0400530 #Get the rest of the handle
531 self.handle.sendline("")
532 self.handle.expect("\$")
533 handle_more = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400534
535 main.log.info("Cell call returned: "+handle_before+
andrewonlabc03bf6c2014-10-09 14:56:18 -0400536 handle_after + handle_more)
andrewonlab95ca1462014-10-09 14:04:24 -0400537
538 return main.TRUE
539
540 except pexpect.EOF:
541 main.log.error(self.name + ": EOF exception found")
542 main.log.error(self.name + ": " + self.handle.before)
543 main.cleanup()
544 main.exit()
545 except:
546 main.log.info(self.name+" ::::::")
547 main.log.error( traceback.print_exc())
548 main.log.info(self.name+" ::::::")
549 main.cleanup()
550 main.exit()
551
andrewonlabc03bf6c2014-10-09 14:56:18 -0400552 def verify_cell(self):
553 '''
554 Calls 'onos-verify-cell' to check for cell installation
555 '''
andrewonlab8d0d7d72014-10-09 16:33:15 -0400556 #TODO: Add meaningful expect value
557
andrewonlabc03bf6c2014-10-09 14:56:18 -0400558 try:
559 #Clean handle by sending empty and expecting $
560 self.handle.sendline("")
561 self.handle.expect("\$")
562 self.handle.sendline("onos-verify-cell")
563 self.handle.expect("\$")
564 handle_before = self.handle.before
565 handle_after = self.handle.after
566 #Get the rest of the handle
567 self.handle.sendline("")
568 self.handle.expect("\$")
569 handle_more = self.handle.before
570
571 main.log.info("Verify cell returned: "+handle_before+
572 handle_after + handle_more)
573
574 return main.TRUE
Jon Hall7993bfc2014-10-09 16:30:14 -0400575 except pexpect.EOF:
576 main.log.error(self.name + ": EOF exception found")
577 main.log.error(self.name + ": " + self.handle.before)
578 main.cleanup()
579 main.exit()
580 except:
581 main.log.info(self.name+" ::::::")
582 main.log.error( traceback.print_exc())
583 main.log.info(self.name+" ::::::")
584 main.cleanup()
585 main.exit()
586
andrewonlab05e362f2014-10-10 00:40:57 -0400587 def onos_cli(self, ONOS_ip, cmdstr):
588 '''
589 Uses 'onos' command to send various ONOS CLI arguments.
590 Required:
591 * ONOS_ip: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400592 * cmdstr: specify the command string to send
andrewonlab6e20c342014-10-10 18:08:48 -0400593
594 This function is intended to expose the entire karaf
595 CLI commands for ONOS. Try to use this function first
596 before attempting to write a ONOS CLI specific driver
597 function.
598 You can see a list of available 'cmdstr' arguments
599 by starting onos, and typing in 'onos' to enter the
600 onos> CLI. Then, type 'help' to see the list of
601 available commands.
andrewonlab05e362f2014-10-10 00:40:57 -0400602 '''
603 try:
604 if not ONOS_ip:
605 main.log.error("You must specify the IP address")
606 return main.FALSE
607 if not cmdstr:
608 main.log.error("You must specify the command string")
609 return main.FALSE
610
611 cmdstr = str(cmdstr)
612 self.handle.sendline("")
613 self.handle.expect("\$")
614
615 self.handle.sendline("onos -w " + ONOS_ip + " " + cmdstr)
616 self.handle.expect("\$")
617
Shreya Shaha73aaad2014-10-27 18:03:09 -0400618 handle_before = self.handle.before
619 print "handle_before = ", self.handle.before
Jon Hall47a93fb2015-01-06 16:46:06 -0800620 #handle_after = str(self.handle.after)
621
Shreya Shaha73aaad2014-10-27 18:03:09 -0400622 #self.handle.sendline("")
623 #self.handle.expect("\$")
624 #handle_more = str(self.handle.before)
andrewonlab05e362f2014-10-10 00:40:57 -0400625
626 main.log.info("Command sent successfully")
627
andrewonlab94282092014-10-10 13:00:11 -0400628 #Obtain return handle that consists of result from
629 #the onos command. The string may need to be
630 #configured further.
Shreya Shaha73aaad2014-10-27 18:03:09 -0400631 #return_string = handle_before + handle_after
632 return_string = handle_before
633 print "return_string = ", return_string
andrewonlab05e362f2014-10-10 00:40:57 -0400634 return return_string
635
636 except pexpect.EOF:
637 main.log.error(self.name + ": EOF exception found")
638 main.log.error(self.name + ": " + self.handle.before)
639 main.cleanup()
640 main.exit()
641 except:
642 main.log.info(self.name+" ::::::")
643 main.log.error( traceback.print_exc())
644 main.log.info(self.name+" ::::::")
645 main.cleanup()
646 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400647
648 def onos_install(self, options="-f", node = ""):
649 '''
650 Installs ONOS bits on the designated cell machine.
651 If -f option is provided, it also forces an uninstall.
652 Presently, install also includes onos-push-bits and
653 onos-config within.
654 The node option allows you to selectively only push the jar
655 files to certain onos nodes
656
657 Returns: main.TRUE on success and main.FALSE on failure
658 '''
659 try:
andrewonlab114768a2014-11-14 12:44:44 -0500660 if options:
661 self.handle.sendline("onos-install " + options + " " + node)
662 else:
663 self.handle.sendline("onos-install "+node)
Jon Hall7993bfc2014-10-09 16:30:14 -0400664 self.handle.expect("onos-install ")
665 #NOTE: this timeout may need to change depending on the network and size of ONOS
666 i=self.handle.expect(["Network\sis\sunreachable",
667 "onos\sstart/running,\sprocess",
andrewonlabd9a73a72014-11-14 17:28:21 -0500668 "ONOS\sis\salready\sinstalled",
Jon Hall7993bfc2014-10-09 16:30:14 -0400669 pexpect.TIMEOUT],timeout=60)
670
Jon Hall7993bfc2014-10-09 16:30:14 -0400671 if i == 0:
672 main.log.warn("Network is unreachable")
673 return main.FALSE
674 elif i == 1:
Jon Hall42db6dc2014-10-24 19:03:48 -0400675 main.log.info("ONOS was installed on " + node + " and started")
Jon Hall7993bfc2014-10-09 16:30:14 -0400676 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500677 elif i == 2:
678 main.log.info("ONOS is already installed on "+node)
679 return main.TRUE
680 elif i == 3:
Jon Hall42db6dc2014-10-24 19:03:48 -0400681 main.log.info("Installation of ONOS on " + node + " timed out")
Jon Hall7993bfc2014-10-09 16:30:14 -0400682 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400683
andrewonlabd9a73a72014-11-14 17:28:21 -0500684
andrewonlabc03bf6c2014-10-09 14:56:18 -0400685 except pexpect.EOF:
686 main.log.error(self.name + ": EOF exception found")
687 main.log.error(self.name + ": " + self.handle.before)
688 main.cleanup()
689 main.exit()
690 except:
691 main.log.info(self.name+" ::::::")
692 main.log.error( traceback.print_exc())
693 main.log.info(self.name+" ::::::")
694 main.cleanup()
695 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400696
andrewonlab8d0d7d72014-10-09 16:33:15 -0400697 def onos_start(self, node_ip):
698 '''
699 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400700 This command is a remote management of the ONOS upstart daemon
andrewonlab8d0d7d72014-10-09 16:33:15 -0400701 '''
702
703 try:
704 self.handle.sendline("")
705 self.handle.expect("\$")
706 self.handle.sendline("onos-service "+str(node_ip)+
707 " start")
708 i = self.handle.expect([
709 "Job\sis\salready\srunning",
710 "start/running",
711 "Unknown\sinstance",
Shreya Shahd01153d2014-10-23 15:08:56 -0400712 pexpect.TIMEOUT],timeout=120)
andrewonlab8d0d7d72014-10-09 16:33:15 -0400713
714 if i == 0:
715 main.log.info("Service is already running")
716 return main.TRUE
717 elif i == 1:
718 main.log.info("ONOS service started")
719 return main.TRUE
720 else:
721 main.log.error("ONOS service failed to start")
722 main.cleanup()
723 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400724 except pexpect.EOF:
725 main.log.error(self.name + ": EOF exception found")
726 main.log.error(self.name + ": " + self.handle.before)
727 main.cleanup()
728 main.exit()
729 except:
730 main.log.info(self.name+" ::::::")
731 main.log.error( traceback.print_exc())
732 main.log.info(self.name+" ::::::")
733 main.cleanup()
734 main.exit()
735
andrewonlab2b30bd32014-10-09 16:48:55 -0400736 def onos_stop(self, node_ip):
737 '''
738 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400739 This command is a remote management of the ONOS upstart daemon
andrewonlab2b30bd32014-10-09 16:48:55 -0400740 '''
741 try:
742 self.handle.sendline("")
743 self.handle.expect("\$")
744 self.handle.sendline("onos-service "+str(node_ip)+
745 " stop")
746 i = self.handle.expect([
747 "stop/waiting",
748 "Unknown\sinstance",
749 pexpect.TIMEOUT],timeout=60)
750
751 if i == 0:
752 main.log.info("ONOS service stopped")
753 return main.TRUE
754 elif i == 1:
755 main.log.info("Unknown ONOS instance specified: "+
756 str(node_ip))
757 return main.FALSE
758 else:
759 main.log.error("ONOS service failed to stop")
760 return main.FALSE
761
762 except pexpect.EOF:
763 main.log.error(self.name + ": EOF exception found")
764 main.log.error(self.name + ": " + self.handle.before)
765 main.cleanup()
766 main.exit()
767 except:
768 main.log.info(self.name+" ::::::")
769 main.log.error( traceback.print_exc())
770 main.log.info(self.name+" ::::::")
771 main.cleanup()
772 main.exit()
andrewonlab99d3f7f2014-11-13 17:45:08 -0500773
andrewonlaba6f7d882014-11-13 17:32:24 -0500774 def onos_uninstall(self, node_ip=""):
andrewonlabc8d47972014-10-09 16:52:36 -0400775 '''
776 Calls the command: 'onos-uninstall'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400777 Uninstalls ONOS from the designated cell machine, stopping
778 if needed
andrewonlabc8d47972014-10-09 16:52:36 -0400779 '''
780 try:
781 self.handle.sendline("")
782 self.handle.expect("\$")
Jon Hall73cf9cc2014-11-20 22:28:38 -0800783 self.handle.sendline( "onos-uninstall "+str(node_ip) )
andrewonlabc8d47972014-10-09 16:52:36 -0400784 self.handle.expect("\$")
785
andrewonlab9dfd2082014-11-13 17:44:03 -0500786 main.log.info("ONOS "+node_ip+" was uninstalled")
787
andrewonlabc8d47972014-10-09 16:52:36 -0400788 #onos-uninstall command does not return any text
789 return main.TRUE
790
791 except pexpect.EOF:
792 main.log.error(self.name + ": EOF exception found")
793 main.log.error(self.name + ": " + self.handle.before)
794 main.cleanup()
795 main.exit()
796 except:
797 main.log.info(self.name+" ::::::")
798 main.log.error( traceback.print_exc())
799 main.log.info(self.name+" ::::::")
800 main.cleanup()
801 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400802
andrewonlabaedc8332014-12-04 12:43:03 -0500803 def onos_die(self, node_ip):
804 '''
805 Issues the command 'onos-die <node-ip>'
806 This command calls onos-kill and also stops the node
807 '''
808 try:
809 self.handle.sendline("")
810 self.handle.expect("\$")
811 cmd_str = "onos-kill "+str(node_ip)
812 self.handle.sendline(cmd_str)
813 i = self.handle.expect([
814 "Killing\sONOS",
815 "ONOS\sprocess\sis\snot\srunning",
816 pexpect.TIMEOUT], timeout=20)
817 if i == 0:
818 main.log.info("ONOS instance "+str(node_ip)+
819 " was killed and stopped")
820 return main.TRUE
821 elif i == 1:
822 main.log.info("ONOS process was not running")
823 return main.FALSE
824 except pexpect.EOF:
825 main.log.error(self.name + ": EOF exception found")
826 main.log.error(self.name + ": " + self.handle.before)
827 main.cleanup()
828 main.exit()
829 except:
830 main.log.info(self.name+" ::::::")
831 main.log.error( traceback.print_exc())
832 main.log.info(self.name+" ::::::")
833 main.cleanup()
834 main.exit()
835
836
andrewonlabe8e56fd2014-10-09 17:12:44 -0400837 def onos_kill(self, node_ip):
838 '''
839 Calls the command: 'onos-kill [<node-ip>]'
840 "Remotely, and unceremoniously kills the ONOS instance running on
841 the specified cell machine" - Tom V
842 '''
843
844 try:
845 self.handle.sendline("")
846 self.handle.expect("\$")
847 self.handle.sendline("onos-kill " + str(node_ip))
848 i = self.handle.expect([
849 "\$",
850 "No\sroute\sto\shost",
851 "password:",
852 pexpect.TIMEOUT], timeout=20)
853
854 if i == 0:
855 main.log.info("ONOS instance "+str(node_ip)+" was killed")
856 return main.TRUE
857 elif i == 1:
858 main.log.info("No route to host")
859 return main.FALSE
860 elif i == 2:
861 main.log.info("Passwordless login for host: "+str(node_ip)+
862 " not configured")
863 return main.FALSE
864 else:
865 main.log.info("ONOS instasnce was not killed")
866 return main.FALSE
867
868 except pexpect.EOF:
869 main.log.error(self.name + ": EOF exception found")
870 main.log.error(self.name + ": " + self.handle.before)
871 main.cleanup()
872 main.exit()
873 except:
874 main.log.info(self.name+" ::::::")
875 main.log.error( traceback.print_exc())
876 main.log.info(self.name+" ::::::")
877 main.cleanup()
878 main.exit()
879
andrewonlab19fbdca2014-11-14 12:55:59 -0500880 def onos_remove_raft_logs(self):
881 '''
882 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -0500883 a cleaner environment.
884
andrewonlab19fbdca2014-11-14 12:55:59 -0500885 Description:
Jon Hallfcc88622014-11-25 13:09:54 -0500886 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -0500887 wipes the raft / copycat log files
888 '''
889 try:
890 self.handle.sendline("")
891 self.handle.expect("\$")
892 self.handle.sendline("onos-remove-raft-logs")
Jon Hallfcc88622014-11-25 13:09:54 -0500893 #Sometimes this command hangs
andrewonlabeb7b1072014-11-25 15:50:07 -0500894 i = self.handle.expect(["\$", pexpect.TIMEOUT],
895 timeout=120)
Jon Hallfcc88622014-11-25 13:09:54 -0500896 if i == 1:
andrewonlabeb7b1072014-11-25 15:50:07 -0500897 i = self.handle.expect(["\$", pexpect.TIMEOUT],
898 timeout=120)
Jon Hallfcc88622014-11-25 13:09:54 -0500899 if i == 1:
900 return main.FALSE
shahshreyae6c7cf42014-11-26 16:39:01 -0800901 self.handle.sendline("")
902 self.handle.expect("\$")
andrewonlab19fbdca2014-11-14 12:55:59 -0500903 return main.TRUE
904
905 except pexpect.EOF:
906 main.log.error(self.name + ": EOF exception found")
907 main.log.error(self.name + ": " + self.handle.before)
908 main.cleanup()
909 main.exit()
910 except:
911 main.log.info(self.name+" ::::::")
912 main.log.error( traceback.print_exc())
913 main.log.info(self.name+" ::::::")
914 main.cleanup()
915 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -0500916
andrewonlab94282092014-10-10 13:00:11 -0400917 def onos_start_network(self, mntopo):
918 '''
919 Calls the command 'onos-start-network [<mininet-topo>]
920 "remotely starts the specified topology on the cell's
921 mininet machine against all controllers configured in the
922 cell."
923 * Specify mininet topology file name for mntopo
924 * Topo files should be placed at:
925 ~/<your-onos-directory>/tools/test/topos
926
927 NOTE: This function will take you to the mininet prompt
928 '''
929 try:
930 if not mntopo:
931 main.log.error("You must specify a topo file to execute")
932 return main.FALSE
933
934 mntopo = str(mntopo)
935 self.handle.sendline("")
936 self.handle.expect("\$")
937
938 self.handle.sendline("onos-start-network " + mntopo)
939 self.handle.expect("mininet>")
940 main.log.info("Network started, entered mininet prompt")
941
942 #TODO: Think about whether return is necessary or not
943
944 except pexpect.EOF:
945 main.log.error(self.name + ": EOF exception found")
946 main.log.error(self.name + ": " + self.handle.before)
947 main.cleanup()
948 main.exit()
949 except:
950 main.log.info(self.name+" ::::::")
951 main.log.error( traceback.print_exc())
952 main.log.info(self.name+" ::::::")
953 main.cleanup()
954 main.exit()
955
956
Jon Hall7993bfc2014-10-09 16:30:14 -0400957 def isup(self, node = ""):
958 '''
Jon Hall77f53ce2014-10-13 18:02:06 -0400959 Run's onos-wait-for-start which only returns once ONOS is at run
960 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -0400961
Jon Hall7993bfc2014-10-09 16:30:14 -0400962 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
963 '''
964 try:
965 self.handle.sendline("onos-wait-for-start " + node )
966 self.handle.expect("onos-wait-for-start")
967 #NOTE: this timeout is arbitrary"
Jon Hall73cf9cc2014-11-20 22:28:38 -0800968 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout = 120)
Jon Hall7993bfc2014-10-09 16:30:14 -0400969 if i == 0:
970 main.log.info(self.name + ": " + node + " is up")
971 return main.TRUE
972 elif i == 1:
973 #NOTE: since this function won't return until ONOS is ready,
974 # we will kill it on timeout
Jon Hall73cf9cc2014-11-20 22:28:38 -0800975 main.log.error("ONOS has not started yet")
976 self.handle.send("\x03") #Control-C
Jon Hall7993bfc2014-10-09 16:30:14 -0400977 self.handle.expect("\$")
978 return main.FALSE
979 except pexpect.EOF:
980 main.log.error(self.name + ": EOF exception found")
981 main.log.error(self.name + ": " + self.handle.before)
982 main.cleanup()
983 main.exit()
984 except:
985 main.log.info(self.name+" ::::::")
986 main.log.error( traceback.print_exc())
987 main.log.info(self.name+" ::::::")
988 main.cleanup()
989 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -0400990
andrewonlabb66dfa12014-12-02 15:51:10 -0500991 def push_test_intents_shell(self, dpid_src, dpid_dst, num_intents,
andrewonlabaedc8332014-12-04 12:43:03 -0500992 dir_file, onos_ip, num_mult="", app_id="", report=True,
993 options=""):
andrewonlabb66dfa12014-12-02 15:51:10 -0500994 '''
995 Description:
996 Use the linux prompt to push test intents to
997 better parallelize the results than the CLI
998 Required:
999 * dpid_src: specify source dpid
1000 * dpid_dst: specify destination dpid
1001 * num_intents: specify number of intents to push
1002 * dir_file: specify directory and file name to save
1003 results
1004 * onos_ip: specify the IP of ONOS to install on
1005 NOTE:
1006 You must invoke this command at linux shell prompt
1007 '''
1008 try:
1009 #Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001010 if options:
1011 base_cmd = "onos "+str(onos_ip)+" push-test-intents "+\
1012 options+" "
1013 else:
1014 base_cmd = "onos "+str(onos_ip)+" push-test-intents "
1015
andrewonlabb66dfa12014-12-02 15:51:10 -05001016 add_dpid = base_cmd + str(dpid_src) + " " + str(dpid_dst)
1017 if not num_mult:
1018 add_intents = add_dpid + " " + str(num_intents)
1019 elif num_mult:
1020 add_intents = add_dpid + " " + str(num_intents) + " " +\
1021 str(num_mult)
1022 if app_id:
1023 add_app = add_intents + " " + str(app_id)
1024 else:
1025 add_app = add_intents
1026
andrewonlabaedc8332014-12-04 12:43:03 -05001027 if report:
1028 send_cmd = add_app + " > " + str(dir_file) + " &"
1029 else:
1030 send_cmd = add_app + " &"
andrewonlabb66dfa12014-12-02 15:51:10 -05001031 main.log.info("Send cmd: "+send_cmd)
1032
1033 self.handle.sendline(send_cmd)
1034
1035 except pexpect.EOF:
1036 main.log.error(self.name + ": EOF exception found")
1037 main.log.error(self.name + ": " + self.handle.before)
1038 main.cleanup()
1039 main.exit()
1040 except:
1041 main.log.info(self.name+" ::::::")
1042 main.log.error( traceback.print_exc())
1043 main.log.info(self.name+" ::::::")
1044 main.cleanup()
1045 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001046
Shreya Shaha73aaad2014-10-27 18:03:09 -04001047 def get_topology(self,topology_output):
Jon Hall77f53ce2014-10-13 18:02:06 -04001048 '''
1049 parses the onos:topology output
1050 Returns: a topology dict populated by the key values found in
1051 the cli command.
1052 '''
1053
1054 try:
1055 #call the cli to get the topology summary
Shreya Shaha73aaad2014-10-27 18:03:09 -04001056 #cmdstr = "onos:topology"
1057 #cli_result = self.onos_cli(ip, cmdstr)
1058 #print "cli_result = ", cli_result
Jon Hall77f53ce2014-10-13 18:02:06 -04001059
1060 #Parse the output
1061 topology = {}
1062 #for line in cli_result.split("\n"):
Shreya Shaha73aaad2014-10-27 18:03:09 -04001063 for line in topology_output.splitlines():
Jon Hall77f53ce2014-10-13 18:02:06 -04001064 if not line.startswith("time="):
1065 continue
1066 #else
Jon Hall94fd0472014-12-08 11:52:42 -08001067 #print line
Jon Hall77f53ce2014-10-13 18:02:06 -04001068 for var in line.split(","):
Shreya Shaha73aaad2014-10-27 18:03:09 -04001069 #print "'"+var+"'"
1070 #print "'"+var.strip()+"'"
Jon Hall77f53ce2014-10-13 18:02:06 -04001071 key, value = var.strip().split("=")
1072 topology[key] = value
Jon Hall94fd0472014-12-08 11:52:42 -08001073 #print "topology = ", topology
Jon Hall47a93fb2015-01-06 16:46:06 -08001074 #devices = topology.get('devices', False)
Jon Hall94fd0472014-12-08 11:52:42 -08001075 #print "devices = ", devices
Jon Hall47a93fb2015-01-06 16:46:06 -08001076 #links = topology.get('links', False)
Jon Hall94fd0472014-12-08 11:52:42 -08001077 #print "links = ", links
Jon Hall47a93fb2015-01-06 16:46:06 -08001078 #SCCs = topology.get('SCC(s)', False)
Jon Hall94fd0472014-12-08 11:52:42 -08001079 #print "SCCs = ", SCCs
Jon Hall47a93fb2015-01-06 16:46:06 -08001080 #paths = topology.get('paths', False)
Jon Hall94fd0472014-12-08 11:52:42 -08001081 #print "paths = ", paths
Jon Hall77f53ce2014-10-13 18:02:06 -04001082
1083 return topology
1084 except pexpect.EOF:
1085 main.log.error(self.name + ": EOF exception found")
1086 main.log.error(self.name + ": " + self.handle.before)
1087 main.cleanup()
1088 main.exit()
1089 except:
1090 main.log.info(self.name+" ::::::")
1091 main.log.error( traceback.print_exc())
1092 main.log.info(self.name+" ::::::")
1093 main.cleanup()
1094 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001095
1096
Jon Hall77f53ce2014-10-13 18:02:06 -04001097
Shreya Shaha73aaad2014-10-27 18:03:09 -04001098 def check_status(self, topology_result, numoswitch, numolink, log_level="info"):
Jon Hall77f53ce2014-10-13 18:02:06 -04001099 '''
1100 Checks the number of swithes & links that ONOS sees against the
1101 supplied values. By default this will report to main.log, but the
1102 log level can be specifid.
1103
1104 Params: ip = ip used for the onos cli
1105 numoswitch = expected number of switches
1106 numlink = expected number of links
1107 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1108
1109
1110 log_level can
1111
1112 Returns: main.TRUE if the number of switchs and links are correct,
1113 main.FALSE if the numer of switches and links is incorrect,
1114 and main.ERROR otherwise
1115 '''
1116
1117 try:
Shreya Shaha73aaad2014-10-27 18:03:09 -04001118 topology = self.get_topology(topology_result)
Jon Hall77f53ce2014-10-13 18:02:06 -04001119 if topology == {}:
1120 return main.ERROR
1121 output = ""
1122 #Is the number of switches is what we expected
1123 devices = topology.get('devices',False)
1124 links = topology.get('links',False)
1125 if devices == False or links == False:
1126 return main.ERROR
1127 switch_check = ( int(devices) == int(numoswitch) )
1128 #Is the number of links is what we expected
1129 link_check = ( int(links) == int(numolink) )
1130 if (switch_check and link_check):
1131 #We expected the correct numbers
1132 output = output + "The number of links and switches match "\
1133 + "what was expected"
1134 result = main.TRUE
1135 else:
1136 output = output + \
1137 "The number of links and switches does not match what was expected"
1138 result = main.FALSE
1139 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1140 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1141 if log_level == "report":
1142 main.log.report(output)
1143 elif log_level == "warn":
1144 main.log.warn(output)
1145 else:
1146 main.log.info(output)
1147 return result
1148 except pexpect.EOF:
1149 main.log.error(self.name + ": EOF exception found")
1150 main.log.error(self.name + ": " + self.handle.before)
1151 main.cleanup()
1152 main.exit()
1153 except:
1154 main.log.info(self.name+" ::::::")
1155 main.log.error( traceback.print_exc())
1156 main.log.info(self.name+" ::::::")
1157 main.cleanup()
1158 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001159
andrewonlab970399c2014-11-07 13:09:32 -05001160 def tshark_pcap(self, interface, dir_file):
1161 '''
1162 Capture all packet activity and store in specified
1163 directory/file
1164
1165 Required:
1166 * interface: interface to capture
1167 * dir: directory/filename to store pcap
1168 '''
1169 self.handle.sendline("")
1170 self.handle.expect("\$")
1171
1172 self.handle.sendline("tshark -i "+str(interface)+
andrewonlab5d7a8f32014-11-10 13:07:56 -05001173 " -t e -w "+str(dir_file)+ " &")
1174 self.handle.sendline("\r")
andrewonlab970399c2014-11-07 13:09:32 -05001175 self.handle.expect("Capturing on")
andrewonlab5d7a8f32014-11-10 13:07:56 -05001176 self.handle.sendline("\r")
1177 self.handle.expect("\$")
andrewonlab970399c2014-11-07 13:09:32 -05001178
1179 main.log.info("Tshark started capturing files on "+
1180 str(interface)+ " and saving to directory: "+
andrewonlab5d7a8f32014-11-10 13:07:56 -05001181 str(dir_file))
Shreya Shaha73aaad2014-10-27 18:03:09 -04001182
shahshreya4d79aab2014-11-07 15:20:41 -08001183
shahshreyae6c7cf42014-11-26 16:39:01 -08001184 def run_onos_topo_cfg(self, instance_name, json_file):
shahshreya4d79aab2014-11-07 15:20:41 -08001185 '''
shahshreyae6c7cf42014-11-26 16:39:01 -08001186 On ONOS bench, run this command: ./~/ONOS/tools/test/bin/onos-topo-cfg $OC1 filename
shahshreya4d79aab2014-11-07 15:20:41 -08001187 which starts the rest and copies the json file to the onos instance
1188 '''
shahshreyae6c7cf42014-11-26 16:39:01 -08001189 try:
1190 self.handle.sendline("")
1191 self.handle.expect("\$")
1192 self.handle.sendline("cd ~/ONOS/tools/test/bin")
1193 self.handle.expect("/bin$")
1194 cmd = "./onos-topo-cfg " +instance_name +" " +json_file
1195 print "cmd = ", cmd
1196 self.handle.sendline(cmd)
shahshreyad0c80432014-12-04 16:56:05 -08001197 self.handle.expect("\$")
shahshreyae6c7cf42014-11-26 16:39:01 -08001198 self.handle.sendline("cd ~")
1199 self.handle.expect("\$")
1200 return main.TRUE
1201 except:
1202 return main.FALSE
1203
andrewonlabba44bcf2014-10-16 16:54:41 -04001204 def tshark_grep(self, grep, directory, interface='eth0'):
1205 '''
1206 Required:
1207 * grep string
1208 * directory to store results
1209 Optional:
1210 * interface - default: eth0
1211 Description:
1212 Uses tshark command to grep specific group of packets
1213 and stores the results to specified directory.
1214 The timestamp is hardcoded to be in epoch
1215 '''
1216 self.handle.sendline("")
1217 self.handle.expect("\$")
andrewonlabefd7f3d2014-10-21 16:02:31 -04001218 self.handle.sendline("")
andrewonlabba44bcf2014-10-16 16:54:41 -04001219 self.handle.sendline("tshark -i "+str(interface)+
andrewonlabd4940492014-10-24 12:21:27 -04001220 " -t e | grep --line-buffered \""+str(grep)+"\" >"+directory+" &")
andrewonlabba44bcf2014-10-16 16:54:41 -04001221 self.handle.sendline("\r")
1222 self.handle.expect("Capturing on")
1223 self.handle.sendline("\r")
1224 self.handle.expect("\$")
1225
1226 def tshark_stop(self):
1227 '''
1228 Removes wireshark files from /tmp and kills all tshark processes
1229 '''
andrewonlab5ba0d9e2014-10-24 13:32:23 -04001230 #Remove all pcap from previous captures
1231 self.execute(cmd="sudo rm /tmp/wireshark*")
andrewonlabba44bcf2014-10-16 16:54:41 -04001232 self.handle.sendline("")
1233 self.handle.sendline("sudo kill -9 `ps -ef | grep \"tshark -i\" |"+
1234 " grep -v grep | awk '{print $2}'`")
1235 self.handle.sendline("")
1236 main.log.info("Tshark stopped")
1237
andrewonlab0c38a4a2014-10-28 18:35:35 -04001238 def ptpd(self, args):
1239 '''
1240 Initiate ptp with user-specified args.
1241 Required:
1242 * args: specify string of args after command
1243 'sudo ptpd'
1244 '''
1245 try:
1246 self.handle.sendline("sudo ptpd "+str(args))
andrewonlab978dff62014-10-29 16:53:35 -04001247 i = self.handle.expect([
andrewonlab0c38a4a2014-10-28 18:35:35 -04001248 "Multiple",
1249 "Error",
1250 "\$"])
1251 self.handle.expect("\$")
andrewonlabba44bcf2014-10-16 16:54:41 -04001252
andrewonlab0c38a4a2014-10-28 18:35:35 -04001253 if i == 0:
1254 handle = self.handle.before
1255 main.log.info("ptpd returned an error: "+
1256 str(handle))
1257 return handle
1258 elif i == 1:
1259 handle = self.handle.before
1260 main.log.error("ptpd returned an error: "+
1261 str(handle))
1262 return handle
1263 else:
1264 return main.TRUE
1265
1266 except pexpect.EOF:
1267 main.log.error(self.name + ": EOF exception found")
1268 main.log.error(self.name + ": " + self.handle.before)
1269 main.cleanup()
1270 main.exit()
1271 except:
1272 main.log.info(self.name+" ::::::")
1273 main.log.error( traceback.print_exc())
1274 main.log.info(self.name+" ::::::")
1275 main.cleanup()
1276 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001277
andrewonlab5d7a8f32014-11-10 13:07:56 -05001278 def cp_logs_to_dir(self, log_to_copy,
1279 dest_dir, copy_file_name=""):
1280 '''
1281 Copies logs to a desired directory.
1282 Current implementation of ONOS deletes its karaf
1283 logs on every iteration. For debugging purposes,
1284 you may want to use this function to capture
1285 certain karaf logs. (or any other logs if needed)
1286 Localtime will be attached to the filename
1287
1288 Required:
1289 * log_to_copy: specify directory and log name to
1290 copy.
1291 ex) /opt/onos/log/karaf.log.1
1292 For copying multiple files, leave copy_file_name
1293 empty and only specify dest_dir -
1294 ex) /opt/onos/log/karaf*
1295 * dest_dir: specify directory to copy to.
1296 ex) /tmp/
1297 Optional:
1298 * copy_file_name: If you want to rename the log
1299 file, specify copy_file_name. This will not work
1300 with multiple file copying
1301 '''
1302 try:
1303 localtime = time.strftime('%x %X')
1304 localtime = localtime.replace("/","")
1305 localtime = localtime.replace(" ","_")
1306 localtime = localtime.replace(":","")
1307 if dest_dir[-1:] != "/":
1308 dest_dir += "/"
1309
1310 if copy_file_name:
1311 self.handle.sendline("cp "+str(log_to_copy)+
1312 " "+str(dest_dir)+str(copy_file_name)+
1313 localtime)
1314 self.handle.expect("cp")
1315 self.handle.expect("\$")
1316 else:
1317 self.handle.sendline("cp "+str(log_to_copy)+
1318 " "+str(dest_dir))
1319 self.handle.expect("cp")
1320 self.handle.expect("\$")
1321
1322 return self.handle.before
1323
1324 except pexpect.EOF:
1325 main.log.error("Copying files failed")
1326 main.log.error(self.name + ": EOF exception found")
1327 main.log.error(self.name + ": " + self.handle.before)
1328 except:
1329 main.log.error("Copying files failed")
1330 main.log.info(self.name+" ::::::")
1331 main.log.error( traceback.print_exc())
1332 main.log.info(self.name+" ::::::")
1333
Jon Hall94fd0472014-12-08 11:52:42 -08001334 def check_logs(self, onos_ip):
1335 '''
1336 runs onos-check-logs on the given onos node
1337 returns the response
1338 '''
1339 try:
1340 cmd = "onos-check-logs " + str(onos_ip)
1341 self.handle.sendline(cmd)
1342 self.handle.expect(cmd)
1343 self.handle.expect("\$")
1344 response = self.handle.before
1345 return response
1346 except pexpect.EOF:
1347 main.log.error("Lost ssh connection")
1348 main.log.error(self.name + ": EOF exception found")
1349 main.log.error(self.name + ": " + self.handle.before)
1350 except:
1351 main.log.error("Some error in check_logs:")
1352 main.log.info(self.name+" ::::::")
1353 main.log.error( traceback.print_exc())
1354 main.log.info(self.name+" ::::::")
1355
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001356 def onos_status(self, node=""):
1357 '''
1358 Calls onos command: 'onos-service [<node-ip>] status'
1359 '''
Jon Hall94fd0472014-12-08 11:52:42 -08001360
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001361 try:
1362 self.handle.sendline("")
1363 self.handle.expect("\$")
1364 self.handle.sendline("onos-service "+str(node)+
1365 " status")
1366 i = self.handle.expect([
1367 "start/running",
1368 "stop/waiting",
1369 pexpect.TIMEOUT],timeout=120)
1370
1371 if i == 0:
1372 main.log.info("ONOS is running")
1373 return main.TRUE
1374 elif i == 1:
1375 main.log.info("ONOS is stopped")
1376 return main.FALSE
1377 else:
1378 main.log.error("ONOS service failed to check the status")
1379 main.cleanup()
1380 main.exit()
1381 except pexpect.EOF:
1382 main.log.error(self.name + ": EOF exception found")
1383 main.log.error(self.name + ": " + self.handle.before)
1384 main.cleanup()
1385 main.exit()
1386 except:
1387 main.log.info(self.name+" ::::::")
1388 main.log.error( traceback.print_exc())
1389 main.log.info(self.name+" ::::::")
1390 main.cleanup()
1391 main.exit()