blob: 832e139887da4fd53e7986fb48bf51880b90238d [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
andrewonlabe8e56fd2014-10-09 17:12:44 -04002
kelvin8ec71442015-01-15 16:57:00 -08003"""
4This driver interacts with ONOS bench, the OSGi platform
5that configures the ONOS nodes. ( aka ONOS-next )
andrewonlabe8e56fd2014-10-09 17:12:44 -04006
kelvin8ec71442015-01-15 16:57:00 -08007Please follow the coding style demonstrated by existing
andrewonlabe8e56fd2014-10-09 17:12:44 -04008functions 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
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab7735d852014-10-09 13:02:47 -040019import sys
Jon Hall05b2b432014-10-08 19:53:25 -040020import time
21import pexpect
andrewonlab7735d852014-10-09 13:02:47 -040022import os.path
pingping-lin6d23d9e2015-02-02 16:54:24 -080023from requests.models import Response
kelvin8ec71442015-01-15 16:57:00 -080024sys.path.append( "../" )
Jon Hall05b2b432014-10-08 19:53:25 -040025from drivers.common.clidriver import CLI
26
Jon Hall05b2b432014-10-08 19:53:25 -040027
kelvin8ec71442015-01-15 16:57:00 -080028class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040029
kelvin8ec71442015-01-15 16:57:00 -080030 def __init__( self ):
31 """
32 Initialize client
33 """
pingping-lin763ee042015-05-20 17:45:30 -070034 self.name = None
35 self.home = None
36 self.handle = None
kelvin8ec71442015-01-15 16:57:00 -080037 super( CLI, self ).__init__()
38
39 def connect( self, **connectargs ):
40 """
Jon Hall05b2b432014-10-08 19:53:25 -040041 Creates ssh handle for ONOS "bench".
kelvin8ec71442015-01-15 16:57:00 -080042 """
Jon Hall05b2b432014-10-08 19:53:25 -040043 try:
44 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080045 vars( self )[ key ] = connectargs[ key ]
pingping-lin763ee042015-05-20 17:45:30 -070046 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040047 for key in self.options:
48 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080049 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040050 break
pingping-lin763ee042015-05-20 17:45:30 -070051 if self.home is None or self.home == "":
52 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040053
kelvin8ec71442015-01-15 16:57:00 -080054 self.name = self.options[ 'name' ]
55 self.handle = super( OnosDriver, self ).connect(
56 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -080057 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -080058 port=self.port,
59 pwd=self.pwd,
60 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -040061
kelvin8ec71442015-01-15 16:57:00 -080062 self.handle.sendline( "cd " + self.home )
63 self.handle.expect( "\$" )
Jon Hall05b2b432014-10-08 19:53:25 -040064 if self.handle:
65 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080066 else:
67 main.log.info( "NO ONOS HANDLE" )
Jon Hall05b2b432014-10-08 19:53:25 -040068 return main.FALSE
69 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080070 main.log.error( self.name + ": EOF exception found" )
71 main.log.error( self.name + ": " + self.handle.before )
Jon Hall05b2b432014-10-08 19:53:25 -040072 main.cleanup()
73 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -070074 except Exception:
75 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall05b2b432014-10-08 19:53:25 -040076 main.cleanup()
77 main.exit()
78
kelvin8ec71442015-01-15 16:57:00 -080079 def disconnect( self ):
80 """
Jon Hall05b2b432014-10-08 19:53:25 -040081 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080082 """
pingping-lin763ee042015-05-20 17:45:30 -070083 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -040084 try:
pingping-lin763ee042015-05-20 17:45:30 -070085 if self.handle:
86 self.handle.sendline( "" )
87 self.handle.expect( "\$" )
88 self.handle.sendline( "exit" )
89 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -040090 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080091 main.log.error( self.name + ": EOF exception found" )
92 main.log.error( self.name + ": " + self.handle.before )
pingping-lin763ee042015-05-20 17:45:30 -070093 except ValueError:
94 main.log.exception( "Exception in disconnect of " + self.name )
95 response = main.TRUE
96 except Exception:
97 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -040098 response = main.FALSE
99 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400100
pingping-lin57a56ce2015-05-20 16:43:48 -0700101 def onosPackage( self, opTimeout=30 ):
kelvin8ec71442015-01-15 16:57:00 -0800102 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400103 Produce a self-contained tar.gz file that can be deployed
kelvin8ec71442015-01-15 16:57:00 -0800104 and executed on any platform with Java 7 JRE.
105 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400106 try:
kelvin8ec71442015-01-15 16:57:00 -0800107 self.handle.sendline( "onos-package" )
108 self.handle.expect( "onos-package" )
pingping-lin57a56ce2015-05-20 16:43:48 -0700109 self.handle.expect( "tar.gz", opTimeout )
kelvin8ec71442015-01-15 16:57:00 -0800110 handle = str( self.handle.before )
111 main.log.info( "onos-package command returned: " +
112 handle )
113 # As long as the sendline does not time out,
114 # return true. However, be careful to interpret
115 # the results of the onos-package command return
andrewonlab0748d2a2014-10-09 13:24:17 -0400116 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400117
andrewonlab7735d852014-10-09 13:02:47 -0400118 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800119 main.log.error( self.name + ": EOF exception found" )
120 main.log.error( self.name + ": " + self.handle.before )
pingping-lin763ee042015-05-20 17:45:30 -0700121 except Exception:
122 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400123 main.cleanup()
124 main.exit()
125
kelvin-onlabd3b64892015-01-20 13:26:24 -0800126 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800127 """
andrewonlab8790abb2014-11-06 13:51:54 -0500128 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800129 """
andrewonlab8790abb2014-11-06 13:51:54 -0500130 try:
kelvin8ec71442015-01-15 16:57:00 -0800131 self.handle.sendline( "onos-build" )
132 self.handle.expect( "onos-build" )
133 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800134 "BUILD SUCCESS",
135 "ERROR",
136 "BUILD FAILED" ],
137 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800138 handle = str( self.handle.before )
andrewonlab8790abb2014-11-06 13:51:54 -0500139
kelvin8ec71442015-01-15 16:57:00 -0800140 main.log.info( "onos-build command returned: " +
141 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500142
143 if i == 0:
144 return main.TRUE
145 else:
146 return handle
147
148 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800149 main.log.error( self.name + ": EOF exception found" )
150 main.log.error( self.name + ": " + self.handle.before )
pingping-lin763ee042015-05-20 17:45:30 -0700151 except Exception:
152 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500153 main.cleanup()
154 main.exit()
155
pingping-lin57a56ce2015-05-20 16:43:48 -0700156 def cleanInstall( self, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800157 """
158 Runs mvn clean install in the root of the ONOS directory.
159 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400160
kelvin8ec71442015-01-15 16:57:00 -0800161 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400162 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800163 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400164 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800165 main.log.info( "Running 'mvn clean install' on " +
166 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800167 ". This may take some time." )
168 self.handle.sendline( "cd " + self.home )
169 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400170
kelvin8ec71442015-01-15 16:57:00 -0800171 self.handle.sendline( "" )
172 self.handle.expect( "\$" )
173 self.handle.sendline( "mvn clean install" )
174 self.handle.expect( "mvn clean install" )
175 while True:
176 i = self.handle.expect( [
pingping-lin763ee042015-05-20 17:45:30 -0700177 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
178 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400179 'BUILD\sFAILURE',
180 'BUILD\sSUCCESS',
pingping-lin763ee042015-05-20 17:45:30 -0700181 'onos\$', #TODO: fix this to be more generic?
Jon Hallde9d9aa2014-10-08 20:36:02 -0400182 'ONOS\$',
pingping-lin57a56ce2015-05-20 16:43:48 -0700183 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400184 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800185 main.log.error( self.name + ":There is insufficient memory \
186 for the Java Runtime Environment to continue." )
187 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400188 main.cleanup()
189 main.exit()
190 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800191 main.log.error( self.name + ": Build failure!" )
192 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400193 main.cleanup()
194 main.exit()
195 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800196 main.log.info( self.name + ": Build success!" )
pingping-lin763ee042015-05-20 17:45:30 -0700197 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800198 main.log.info( self.name + ": Build complete" )
199 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400200 for line in self.handle.before.splitlines():
201 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800202 main.log.info( line )
203 self.handle.sendline( "" )
204 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400205 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -0700206 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800207 main.log.error(
208 self.name +
209 ": mvn clean install TIMEOUT!" )
210 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400211 main.cleanup()
212 main.exit()
213 else:
pingping-lin763ee042015-05-20 17:45:30 -0700214 main.log.error( self.name + ": unexpected response from " +
215 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800216 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400217 main.cleanup()
218 main.exit()
219 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800220 main.log.error( self.name + ": EOF exception found" )
221 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400222 main.cleanup()
223 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700224 except Exception:
225 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400226 main.cleanup()
227 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400228
pingping-lin763ee042015-05-20 17:45:30 -0700229 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800230 """
Jon Hallacabffd2014-10-09 12:36:53 -0400231 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800232
pingping-lin763ee042015-05-20 17:45:30 -0700233 If the fastForward boolean is set to true, only git pulls that can
234 be fast forwarded will be performed. IE if you have not local commits
235 in your branch.
236
Jon Hallacabffd2014-10-09 12:36:53 -0400237 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800238 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400239 for the purpose of pulling from other nodes if necessary.
240
Jon Hall47a93fb2015-01-06 16:46:06 -0800241 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400242 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800243 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400244 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400245
kelvin8ec71442015-01-15 16:57:00 -0800246 """
Jon Hallacabffd2014-10-09 12:36:53 -0400247 try:
kelvin8ec71442015-01-15 16:57:00 -0800248 # main.log.info( self.name + ": Stopping ONOS" )
249 # self.stop()
250 self.handle.sendline( "cd " + self.home )
pingping-lin763ee042015-05-20 17:45:30 -0700251 self.handle.expect( self.home + "\$" )
252 cmd = "git pull"
253 if comp1 != "":
254 cmd += ' ' + comp1
255 if fastForward:
256 cmd += ' ' + " --ff-only"
257 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800258 i = self.handle.expect(
259 [
260 'fatal',
261 'Username\sfor\s(.*):\s',
262 '\sfile(s*) changed,\s',
263 'Already up-to-date',
264 'Aborting',
265 'You\sare\snot\scurrently\son\sa\sbranch',
pingping-lin763ee042015-05-20 17:45:30 -0700266 'You asked me to pull without telling me which branch you',
267 'Pull is not possible because you have unmerged files',
268 'Please enter a commit message to explain why this merge',
269 'Found a swap file by the name',
270 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800271 pexpect.TIMEOUT ],
272 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800273 # debug
kelvin-onlabd3b64892015-01-20 13:26:24 -0800274 # main.log.report( self.name +": DEBUG: \n"+
275 # "git pull response: " +
276 # str( self.handle.before ) + str( self.handle.after ) )
kelvin8ec71442015-01-15 16:57:00 -0800277 if i == 0:
pingping-lin763ee042015-05-20 17:45:30 -0700278 main.log.error( self.name + ": Git pull had some issue" )
279 output = self.handle.after
280 self.handle.expect( '\$' )
281 output += self.handle.before
282 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400283 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800284 elif i == 1:
285 main.log.error(
286 self.name +
287 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400288 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800289 elif i == 2:
290 main.log.info(
291 self.name +
292 ": Git Pull - pulling repository now" )
pingping-lin763ee042015-05-20 17:45:30 -0700293 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800294 # So that only when git pull is done, we do mvn clean compile
295 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800296 elif i == 3:
297 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800298 return i
kelvin8ec71442015-01-15 16:57:00 -0800299 elif i == 4:
300 main.log.info(
301 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700302 ": Git Pull - Aborting..." +
303 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400304 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800305 elif i == 5:
306 main.log.info(
307 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700308 ": Git Pull - You are not currently " +
309 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400310 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800311 elif i == 6:
312 main.log.info(
313 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700314 ": Git Pull - You have not configured an upstream " +
315 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400316 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800317 elif i == 7:
318 main.log.info(
319 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700320 ": Git Pull - Pull is not possible because " +
321 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400322 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800323 elif i == 8:
pingping-lin763ee042015-05-20 17:45:30 -0700324 # NOTE: abandoning test since we can't reliably handle this
325 # there could be different default text editors and we
326 # also don't know if we actually want to make the commit
327 main.log.error( "Git pull resulted in a merge commit message" +
328 ". Exiting test!" )
329 main.cleanup()
330 main.exit()
331 elif i == 9: # Merge commit message but swap file exists
332 main.log.error( "Git pull resulted in a merge commit message" +
333 " but a swap file exists." )
334 try:
335 self.handle.send( 'A' ) # Abort
336 self.handle.expect( "\$" )
337 return main.ERROR
338 except Exception:
339 main.log.exception( "Couldn't exit editor prompt!")
340 main.cleanup()
341 main.exit()
342 elif i == 10: # In the middle of a merge commit
343 main.log.error( "Git branch is in the middle of a merge. " )
344 main.log.warn( self.handle.before + self.handle.after )
345 return main.ERROR
346 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800347 main.log.error( self.name + ": Git Pull - TIMEOUT" )
348 main.log.error(
349 self.name + " Response was: " + str(
350 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400351 return main.ERROR
352 else:
kelvin8ec71442015-01-15 16:57:00 -0800353 main.log.error(
354 self.name +
355 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400356 return main.ERROR
357 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800358 main.log.error( self.name + ": EOF exception found" )
359 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400360 main.cleanup()
361 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700362 except Exception:
363 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400364 main.cleanup()
365 main.exit()
366
kelvin-onlabd3b64892015-01-20 13:26:24 -0800367 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800368 """
Jon Hallacabffd2014-10-09 12:36:53 -0400369 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800370
Jon Hallacabffd2014-10-09 12:36:53 -0400371 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800372 If used as gitCheckout( "branch" ) it will do git checkout
373 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400374
375 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800376 branch of the ONOS repository. If it has any problems, it will return
377 main.ERROR.
378 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400379 successful then the function will return main.TRUE.
380
kelvin8ec71442015-01-15 16:57:00 -0800381 """
Jon Hallacabffd2014-10-09 12:36:53 -0400382 try:
kelvin8ec71442015-01-15 16:57:00 -0800383 self.handle.sendline( "cd " + self.home )
pingping-lin763ee042015-05-20 17:45:30 -0700384 self.handle.expect( self.home + "\$" )
385 main.log.info( self.name +
386 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800387 cmd = "git checkout " + branch
388 self.handle.sendline( cmd )
389 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800390 i = self.handle.expect(
pingping-lin763ee042015-05-20 17:45:30 -0700391 [ 'fatal',
392 'Username for (.*): ',
393 'Already on \'',
394 'Switched to branch \'' + str( branch ),
395 pexpect.TIMEOUT,
396 'error: Your local changes to the following files' +
397 'would be overwritten by checkout:',
398 'error: you need to resolve your current index first',
399 "You are in 'detached HEAD' state.",
400 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800401 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800402 if i == 0:
403 main.log.error(
404 self.name +
405 ": Git checkout had some issue..." )
406 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400407 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800408 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800409 main.log.error(
410 self.name +
411 ": Git checkout asking for username." +
412 " Please configure your local git repository to be able " +
413 "to access your remote repository passwordlessly" )
pingping-lin763ee042015-05-20 17:45:30 -0700414 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400415 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800416 elif i == 2:
417 main.log.info(
418 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700419 ": Git Checkout %s : Already on this branch" % branch )
420 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800421 # main.log.info( "DEBUG: after checkout cmd = "+
422 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400423 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800424 elif i == 3:
425 main.log.info(
426 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700427 ": Git checkout %s - Switched to this branch" % branch )
428 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800429 # main.log.info( "DEBUG: after checkout cmd = "+
430 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400431 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800432 elif i == 4:
433 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
434 main.log.error(
pingping-lin763ee042015-05-20 17:45:30 -0700435 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400436 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800437 elif i == 5:
438 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800439 main.log.error(
440 self.name +
441 ": Git checkout error: \n" +
pingping-lin763ee042015-05-20 17:45:30 -0700442 "Your local changes to the following files would" +
443 " be overwritten by checkout:" +
444 str( self.handle.before ) )
445 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500446 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800447 elif i == 6:
pingping-lin763ee042015-05-20 17:45:30 -0700448 main.log.error(
449 self.name +
450 ": Git checkout error: \n" +
451 "You need to resolve your current index first:" +
452 str( self.handle.before ) )
453 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500454 return main.ERROR
pingping-lin763ee042015-05-20 17:45:30 -0700455 elif i == 7:
456 main.log.info(
457 self.name +
458 ": Git checkout " + str( branch ) +
459 " - You are in 'detached HEAD' state. HEAD is now at " +
460 str( branch ) )
461 self.handle.expect( self.home + "\$" )
462 return main.TRUE
463 elif i == 8: # Already in detached HEAD on the specified commit
464 main.log.info(
465 self.name +
466 ": Git Checkout %s : Already on commit" % branch )
467 self.handle.expect( self.home + "\$" )
468 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400469 else:
kelvin8ec71442015-01-15 16:57:00 -0800470 main.log.error(
471 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700472 ": Git Checkout - Unexpected response, " +
473 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800474 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400475 return main.ERROR
476
477 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800478 main.log.error( self.name + ": EOF exception found" )
479 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400480 main.cleanup()
481 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700482 except Exception:
483 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400484 main.cleanup()
485 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400486
pingping-lin6d23d9e2015-02-02 16:54:24 -0800487 def getBranchName( self ):
pingping-linf30cf272015-05-29 15:54:07 -0700488 main.log.info( "self.home = " )
489 main.log.info( self.home )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800490 self.handle.sendline( "cd " + self.home )
pingping-lin9bf3d8f2015-05-29 16:05:28 -0700491 self.handle.expect( self.home + "\$" )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800492 self.handle.sendline( "git name-rev --name-only HEAD" )
493 self.handle.expect( "git name-rev --name-only HEAD" )
494 self.handle.expect( "\$" )
495
496 lines = self.handle.before.splitlines()
497 if lines[1] == "master":
498 return "master"
499 elif lines[1] == "onos-1.0":
500 return "onos-1.0"
501 else:
502 main.log.info( lines[1] )
503 return "unexpected ONOS branch for SDN-IP test"
504
kelvin-onlabd3b64892015-01-20 13:26:24 -0800505 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800506 """
pingping-lin763ee042015-05-20 17:45:30 -0700507 Writes the COMMIT number to the report to be parsed
508 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800509 """
Jon Hall45ec0922014-10-10 19:33:49 -0400510 try:
kelvin8ec71442015-01-15 16:57:00 -0800511 self.handle.sendline( "" )
512 self.handle.expect( "\$" )
513 self.handle.sendline(
514 "cd " +
515 self.home +
pingping-lin763ee042015-05-20 17:45:30 -0700516 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
517 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800518 # NOTE: for some reason there are backspaces inserted in this
519 # phrase when run from Jenkins on some tests
520 self.handle.expect( "never" )
521 self.handle.expect( "\$" )
522 response = ( self.name + ": \n" + str(
523 self.handle.before + self.handle.after ) )
524 self.handle.sendline( "cd " + self.home )
525 self.handle.expect( "\$" )
526 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400527 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500528 print line
529 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700530 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800531 for line in lines[ 2:-1 ]:
532 # Bracket replacement is for Wiki-compliant
533 # formatting. '<' or '>' are interpreted
534 # as xml specific tags that cause errors
535 line = line.replace( "<", "[" )
536 line = line.replace( ">", "]" )
pingping-lin763ee042015-05-20 17:45:30 -0700537 #main.log.wiki( "\t" + line )
538 main.log.wiki( line + "<br /> " )
539 main.log.summary( line )
540 main.log.wiki( "</blockquote>" )
541 main.log.summary("\n")
kelvin8ec71442015-01-15 16:57:00 -0800542 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400543 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800544 main.log.error( self.name + ": EOF exception found" )
545 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400546 main.cleanup()
547 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800548 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800549 main.log.error( self.name + ": TIMEOUT exception found" )
550 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800551 main.cleanup()
552 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700553 except Exception:
554 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400555 main.cleanup()
556 main.exit()
557
kelvin-onlabd3b64892015-01-20 13:26:24 -0800558 def createCellFile( self, benchIp, fileName, mnIpAddrs,
pingping-lin763ee042015-05-20 17:45:30 -0700559 appString, *onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800560 """
andrewonlab94282092014-10-10 13:00:11 -0400561 Creates a cell file based on arguments
562 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800563 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400564 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800565 * File name of the cell file ( fileName )
566 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800567 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400568 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800569 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400570 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800571
andrewonlab94282092014-10-10 13:00:11 -0400572 NOTE: Assumes cells are located at:
573 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800574 """
575 # Variable initialization
kelvin-onlabd3b64892015-01-20 13:26:24 -0800576 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800577 # We want to create the cell file in the dependencies directory
578 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800579 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800580 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800581 cellFile = open( tempDirectory + fileName, 'w+' )
kelvin8ec71442015-01-15 16:57:00 -0800582
pingping-lin763ee042015-05-20 17:45:30 -0700583 # App string is hardcoded environment variables
kelvin8ec71442015-01-15 16:57:00 -0800584 # That you may wish to use by default on startup.
pingping-lin763ee042015-05-20 17:45:30 -0700585 # Note that you may not want certain apps listed
kelvin8ec71442015-01-15 16:57:00 -0800586 # on here.
pingping-lin763ee042015-05-20 17:45:30 -0700587 appString = "export ONOS_APPS=" + appString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800588 mnString = "export OCN="
kelvin-onlabf70fd542015-05-07 18:41:40 -0700589 if mnIpAddrs == "":
590 mnString = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800591 onosString = "export OC"
592 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800593
kelvin-onlabd3b64892015-01-20 13:26:24 -0800594 # Create ONOSNIC ip address prefix
595 tempOnosIp = onosIpAddrs[ 0 ]
596 tempList = []
597 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800598 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800599 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800600 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800601 nicAddr = ".".join( tempList ) + ".*"
602 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400603
604 try:
kelvin8ec71442015-01-15 16:57:00 -0800605 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800606 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400607
kelvin-onlabd3b64892015-01-20 13:26:24 -0800608 for arg in onosIpAddrs:
609 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800610 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400611 # export OC1="10.128.20.11"
612 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800613 cellFile.write( onosString + str( tempCount ) +
614 "=" + "\"" + arg + "\"" + "\n" )
615 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800616
kelvin-onlabd3b64892015-01-20 13:26:24 -0800617 cellFile.write( mnString + "\"" + mnIpAddrs + "\"" + "\n" )
pingping-lin763ee042015-05-20 17:45:30 -0700618 cellFile.write( appString + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800619 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400620
kelvin8ec71442015-01-15 16:57:00 -0800621 # We use os.system to send the command to TestON cluster
622 # to account for the case in which TestON is not located
623 # on the same cluster as the ONOS bench
624 # Note that even if TestON is located on the same cluster
625 # as ONOS bench, you must setup passwordless ssh
626 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800627 os.system( "scp " + tempDirectory + fileName +
628 " admin@" + benchIp + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400629
andrewonlab2a6c9342014-10-16 13:40:15 -0400630 return main.TRUE
631
andrewonlab94282092014-10-10 13:00:11 -0400632 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800633 main.log.error( self.name + ": EOF exception found" )
634 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400635 main.cleanup()
636 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700637 except Exception:
638 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400639 main.cleanup()
640 main.exit()
641
kelvin-onlabd3b64892015-01-20 13:26:24 -0800642 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800643 """
andrewonlab95ca1462014-10-09 14:04:24 -0400644 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800645 """
andrewonlab95ca1462014-10-09 14:04:24 -0400646 try:
647 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800648 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400649 main.cleanup()
650 main.exit()
651 else:
kelvin8ec71442015-01-15 16:57:00 -0800652 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800653 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800654 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400655 # and that this driver will have to change accordingly
pingping-lin763ee042015-05-20 17:45:30 -0700656 self.handle.expect(str(cellname))
kelvin-onlabd3b64892015-01-20 13:26:24 -0800657 handleBefore = self.handle.before
658 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800659 # Get the rest of the handle
pingping-lin763ee042015-05-20 17:45:30 -0700660 self.handle.sendline("")
661 self.handle.expect("\$")
kelvin-onlabd3b64892015-01-20 13:26:24 -0800662 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400663
kelvin-onlabd3b64892015-01-20 13:26:24 -0800664 main.log.info( "Cell call returned: " + handleBefore +
665 handleAfter + handleMore )
andrewonlab95ca1462014-10-09 14:04:24 -0400666
667 return main.TRUE
668
669 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800670 main.log.error( self.name + ": EOF exception found" )
671 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400672 main.cleanup()
673 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700674 except Exception:
675 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400676 main.cleanup()
677 main.exit()
678
kelvin-onlabd3b64892015-01-20 13:26:24 -0800679 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800680 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400681 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800682 """
683 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400684
andrewonlabc03bf6c2014-10-09 14:56:18 -0400685 try:
kelvin8ec71442015-01-15 16:57:00 -0800686 # Clean handle by sending empty and expecting $
687 self.handle.sendline( "" )
688 self.handle.expect( "\$" )
689 self.handle.sendline( "onos-verify-cell" )
690 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800691 handleBefore = self.handle.before
692 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800693 # Get the rest of the handle
694 self.handle.sendline( "" )
695 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800696 handleMore = self.handle.before
andrewonlabc03bf6c2014-10-09 14:56:18 -0400697
kelvin-onlabd3b64892015-01-20 13:26:24 -0800698 main.log.info( "Verify cell returned: " + handleBefore +
699 handleAfter + handleMore )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400700
701 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -0700702 except pexpect.ExceptionPexpect as e:
703 main.log.error( self.name + ": Pexpect exception found of type " +
704 str( type( e ) ) )
705 main.log.error ( e.get_trace() )
kelvin8ec71442015-01-15 16:57:00 -0800706 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400707 main.cleanup()
708 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700709 except Exception:
710 main.log.exception( self.name + ": Uncaught exception!" )
711 main.cleanup()
712 main.exit()
713
714 def onosCfgSet( self, ONOSIp, configName, configParam ):
715 """
716 Uses 'onos <node-ip> cfg set' to change a parameter value of an
717 application.
718
719 ex)
720 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
721
722 ONOSIp = '10.0.0.1'
723 configName = 'org.onosproject.myapp'
724 configParam = 'appSetting 1'
725
726 """
727 try:
728 cfgStr = ( "onos "+str(ONOSIp)+" cfg set "+
729 str(configName) + " " +
730 str(configParam)
731 )
732
733 self.handle.sendline( "" )
734 self.handle.expect( "\$" )
735 self.handle.sendline( cfgStr )
736 self.handle.expect( "\$" )
737
738 # TODO: Add meaningful assertion
739
740 return main.TRUE
741
742 except pexpect.ExceptionPexpect as e:
743 main.log.error( self.name + ": Pexpect exception found of type " +
744 str( type( e ) ) )
745 main.log.error ( e.get_trace() )
746 main.log.error( self.name + ": " + self.handle.before )
747 main.cleanup()
748 main.exit()
749 except Exception:
750 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400751 main.cleanup()
752 main.exit()
753
kelvin-onlabd3b64892015-01-20 13:26:24 -0800754 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800755 """
andrewonlab05e362f2014-10-10 00:40:57 -0400756 Uses 'onos' command to send various ONOS CLI arguments.
757 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800758 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400759 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800760
761 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400762 CLI commands for ONOS. Try to use this function first
763 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800764 function.
765 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400766 by starting onos, and typing in 'onos' to enter the
767 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800768 available commands.
769 """
andrewonlab05e362f2014-10-10 00:40:57 -0400770 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800771 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800772 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400773 return main.FALSE
774 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800775 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400776 return main.FALSE
777
kelvin8ec71442015-01-15 16:57:00 -0800778 cmdstr = str( cmdstr )
779 self.handle.sendline( "" )
780 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400781
kelvin-onlabd3b64892015-01-20 13:26:24 -0800782 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800783 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400784
kelvin-onlabd3b64892015-01-20 13:26:24 -0800785 handleBefore = self.handle.before
Shreya Shaha73aaad2014-10-27 18:03:09 -0400786 print "handle_before = ", self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800787 # handleAfter = str( self.handle.after )
Jon Hall47a93fb2015-01-06 16:46:06 -0800788
kelvin8ec71442015-01-15 16:57:00 -0800789 # self.handle.sendline( "" )
790 # self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800791 # handleMore = str( self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400792
kelvin8ec71442015-01-15 16:57:00 -0800793 main.log.info( "Command sent successfully" )
andrewonlab05e362f2014-10-10 00:40:57 -0400794
kelvin8ec71442015-01-15 16:57:00 -0800795 # Obtain return handle that consists of result from
796 # the onos command. The string may need to be
797 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800798 # returnString = handleBefore + handleAfter
799 returnString = handleBefore
800 print "return_string = ", returnString
801 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400802
803 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800804 main.log.error( self.name + ": EOF exception found" )
805 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400806 main.cleanup()
807 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700808 except Exception:
809 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400810 main.cleanup()
811 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400812
kelvin-onlabd3b64892015-01-20 13:26:24 -0800813 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800814 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400815 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800816 If -f option is provided, it also forces an uninstall.
817 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400818 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800819 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400820 files to certain onos nodes
821
822 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800823 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400824 try:
andrewonlab114768a2014-11-14 12:44:44 -0500825 if options:
kelvin8ec71442015-01-15 16:57:00 -0800826 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500827 else:
kelvin8ec71442015-01-15 16:57:00 -0800828 self.handle.sendline( "onos-install " + node )
829 self.handle.expect( "onos-install " )
830 # NOTE: this timeout may need to change depending on the network
831 # and size of ONOS
832 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800833 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800834 "ONOS\sis\salready\sinstalled",
835 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400836
Jon Hall7993bfc2014-10-09 16:30:14 -0400837 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800838 main.log.warn( "Network is unreachable" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400839 return main.FALSE
840 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800841 main.log.info(
842 "ONOS was installed on " +
843 node +
844 " and started" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400845 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500846 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800847 main.log.info( "ONOS is already installed on " + node )
andrewonlabd9a73a72014-11-14 17:28:21 -0500848 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800849 elif i == 3:
850 main.log.info(
851 "Installation of ONOS on " +
852 node +
853 " timed out" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400854 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400855
856 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800857 main.log.error( self.name + ": EOF exception found" )
858 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400859 main.cleanup()
860 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700861 except Exception:
862 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400863 main.cleanup()
864 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400865
kelvin-onlabd3b64892015-01-20 13:26:24 -0800866 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800867 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400868 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400869 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800870 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400871 try:
kelvin8ec71442015-01-15 16:57:00 -0800872 self.handle.sendline( "" )
873 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800874 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800875 " start" )
876 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -0400877 "Job\sis\salready\srunning",
878 "start/running",
879 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800880 pexpect.TIMEOUT ], timeout=120 )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400881
882 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800883 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400884 return main.TRUE
885 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800886 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400887 return main.TRUE
888 else:
kelvin8ec71442015-01-15 16:57:00 -0800889 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400890 main.cleanup()
891 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400892 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800893 main.log.error( self.name + ": EOF exception found" )
894 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400895 main.cleanup()
896 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700897 except Exception:
898 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400899 main.cleanup()
900 main.exit()
901
kelvin-onlabd3b64892015-01-20 13:26:24 -0800902 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800903 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400904 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400905 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800906 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400907 try:
kelvin8ec71442015-01-15 16:57:00 -0800908 self.handle.sendline( "" )
909 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800910 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800911 " stop" )
912 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -0400913 "stop/waiting",
pingping-lin763ee042015-05-20 17:45:30 -0700914 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -0400915 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800916 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2b30bd32014-10-09 16:48:55 -0400917
918 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800919 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400920 return main.TRUE
921 elif i == 1:
pingping-lin763ee042015-05-20 17:45:30 -0700922 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800923 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -0400924 return main.FALSE
pingping-lin763ee042015-05-20 17:45:30 -0700925 elif i == 2:
926 main.log.warn( "ONOS wasn't running" )
927 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -0400928 else:
kelvin8ec71442015-01-15 16:57:00 -0800929 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400930 return main.FALSE
931
932 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800933 main.log.error( self.name + ": EOF exception found" )
934 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -0400935 main.cleanup()
936 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700937 except Exception:
938 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400939 main.cleanup()
940 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800941
kelvin-onlabd3b64892015-01-20 13:26:24 -0800942 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -0800943 """
andrewonlabc8d47972014-10-09 16:52:36 -0400944 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -0800945 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -0400946 if needed
kelvin8ec71442015-01-15 16:57:00 -0800947 """
andrewonlabc8d47972014-10-09 16:52:36 -0400948 try:
kelvin8ec71442015-01-15 16:57:00 -0800949 self.handle.sendline( "" )
pingping-lin763ee042015-05-20 17:45:30 -0700950 self.handle.expect( "\$", timeout=60 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800951 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800952 self.handle.expect( "\$" )
andrewonlabc8d47972014-10-09 16:52:36 -0400953
kelvin-onlabd3b64892015-01-20 13:26:24 -0800954 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
andrewonlab9dfd2082014-11-13 17:44:03 -0500955
kelvin8ec71442015-01-15 16:57:00 -0800956 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -0400957 return main.TRUE
958
pingping-lin763ee042015-05-20 17:45:30 -0700959 except pexpect.TIMEOUT:
960 main.log.exception( self.name + ": Timeout in onosUninstall" )
961 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -0400962 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800963 main.log.error( self.name + ": EOF exception found" )
964 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -0400965 main.cleanup()
966 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700967 except Exception:
968 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -0400969 main.cleanup()
970 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400971
kelvin-onlabd3b64892015-01-20 13:26:24 -0800972 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800973 """
andrewonlabaedc8332014-12-04 12:43:03 -0500974 Issues the command 'onos-die <node-ip>'
975 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -0800976 """
andrewonlabaedc8332014-12-04 12:43:03 -0500977 try:
kelvin8ec71442015-01-15 16:57:00 -0800978 self.handle.sendline( "" )
979 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800980 cmdStr = "onos-kill " + str( nodeIp )
981 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800982 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -0500983 "Killing\sONOS",
984 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -0800985 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -0500986 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800987 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800988 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -0500989 return main.TRUE
990 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800991 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -0500992 return main.FALSE
993 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800994 main.log.error( self.name + ": EOF exception found" )
995 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -0500996 main.cleanup()
997 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700998 except Exception:
999 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -05001000 main.cleanup()
1001 main.exit()
1002
kelvin-onlabd3b64892015-01-20 13:26:24 -08001003 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001004 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001005 Calls the command: 'onos-kill [<node-ip>]'
1006 "Remotely, and unceremoniously kills the ONOS instance running on
1007 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001008 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001009 try:
kelvin8ec71442015-01-15 16:57:00 -08001010 self.handle.sendline( "" )
1011 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001012 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001013 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -04001014 "\$",
1015 "No\sroute\sto\shost",
1016 "password:",
kelvin8ec71442015-01-15 16:57:00 -08001017 pexpect.TIMEOUT ], timeout=20 )
1018
andrewonlabe8e56fd2014-10-09 17:12:44 -04001019 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001020 main.log.info(
1021 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001022 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001023 return main.TRUE
1024 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001025 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001026 return main.FALSE
1027 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001028 main.log.info(
1029 "Passwordless login for host: " +
1030 str( nodeIp ) +
1031 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001032 return main.FALSE
1033 else:
pingping-lin763ee042015-05-20 17:45:30 -07001034 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001035 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001036
andrewonlabe8e56fd2014-10-09 17:12:44 -04001037 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001038 main.log.error( self.name + ": EOF exception found" )
1039 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001040 main.cleanup()
1041 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001042 except Exception:
1043 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001044 main.cleanup()
1045 main.exit()
1046
kelvin-onlabd3b64892015-01-20 13:26:24 -08001047 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001048 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001049 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001050 a cleaner environment.
1051
andrewonlab19fbdca2014-11-14 12:55:59 -05001052 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001053 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001054 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001055 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001056 try:
kelvin8ec71442015-01-15 16:57:00 -08001057 self.handle.sendline( "" )
1058 self.handle.expect( "\$" )
1059 self.handle.sendline( "onos-remove-raft-logs" )
1060 # Sometimes this command hangs
1061 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1062 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001063 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001064 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1065 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001066 if i == 1:
1067 return main.FALSE
pingping-lin763ee042015-05-20 17:45:30 -07001068 #self.handle.sendline( "" )
1069 #self.handle.expect( "\$" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001070 return main.TRUE
1071
1072 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001073 main.log.error( self.name + ": EOF exception found" )
1074 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001075 main.cleanup()
1076 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001077 except Exception:
1078 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001079 main.cleanup()
1080 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001081
kelvin-onlabd3b64892015-01-20 13:26:24 -08001082 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001083 """
1084 Calls the command 'onos-start-network [ <mininet-topo> ]
1085 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001086 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001087 cell."
andrewonlab94282092014-10-10 13:00:11 -04001088 * Specify mininet topology file name for mntopo
1089 * Topo files should be placed at:
1090 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001091
andrewonlab94282092014-10-10 13:00:11 -04001092 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001093 """
andrewonlab94282092014-10-10 13:00:11 -04001094 try:
1095 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001096 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001097 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001098
kelvin8ec71442015-01-15 16:57:00 -08001099 mntopo = str( mntopo )
1100 self.handle.sendline( "" )
1101 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001102
kelvin8ec71442015-01-15 16:57:00 -08001103 self.handle.sendline( "onos-start-network " + mntopo )
1104 self.handle.expect( "mininet>" )
1105 main.log.info( "Network started, entered mininet prompt" )
1106
1107 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001108
1109 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001110 main.log.error( self.name + ": EOF exception found" )
1111 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001112 main.cleanup()
1113 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001114 except Exception:
1115 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001116 main.cleanup()
1117 main.exit()
1118
pingping-lin763ee042015-05-20 17:45:30 -07001119 def isup(self, node = "", timeout = 120):
kelvin8ec71442015-01-15 16:57:00 -08001120 """
1121 Run's onos-wait-for-start which only returns once ONOS is at run
pingping-lin763ee042015-05-20 17:45:30 -07001122 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001123
Jon Hall7993bfc2014-10-09 16:30:14 -04001124 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001125 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001126 try:
pingping-lin763ee042015-05-20 17:45:30 -07001127 self.handle.sendline("onos-wait-for-start " + node )
1128 self.handle.expect("onos-wait-for-start")
kelvin8ec71442015-01-15 16:57:00 -08001129 # NOTE: this timeout is arbitrary"
pingping-lin763ee042015-05-20 17:45:30 -07001130 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001131 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001132 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001133 return main.TRUE
1134 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001135 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001136 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001137 main.log.error( "ONOS has not started yet" )
1138 self.handle.send( "\x03" ) # Control-C
1139 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001140 return main.FALSE
1141 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001142 main.log.error( self.name + ": EOF exception found" )
1143 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001144 main.cleanup()
1145 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001146 except Exception:
1147 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001148 main.cleanup()
1149 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001150
kelvin-onlabd3b64892015-01-20 13:26:24 -08001151 def pushTestIntentsShell(
1152 self,
1153 dpidSrc,
1154 dpidDst,
1155 numIntents,
1156 dirFile,
1157 onosIp,
1158 numMult="",
1159 appId="",
1160 report=True,
1161 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001162 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001163 Description:
kelvin8ec71442015-01-15 16:57:00 -08001164 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001165 better parallelize the results than the CLI
1166 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001167 * dpidSrc: specify source dpid
1168 * dpidDst: specify destination dpid
1169 * numIntents: specify number of intents to push
1170 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001171 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001172 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001173 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001174 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001175 """
1176 try:
1177 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001178 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001179 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001180 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001181 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001182 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001183
kelvin-onlabd3b64892015-01-20 13:26:24 -08001184 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1185 if not numMult:
1186 addIntents = addDpid + " " + str( numIntents )
1187 elif numMult:
1188 addIntents = addDpid + " " + str( numIntents ) + " " +\
1189 str( numMult )
1190 if appId:
1191 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001192 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001193 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001194
andrewonlabaedc8332014-12-04 12:43:03 -05001195 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001196 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001197 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001198 sendCmd = addApp + " &"
1199 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001200
kelvin-onlabd3b64892015-01-20 13:26:24 -08001201 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001202
1203 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001204 main.log.error( self.name + ": EOF exception found" )
1205 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001206 main.cleanup()
1207 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001208 except Exception:
1209 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001210 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001211 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001212
kelvin-onlabd3b64892015-01-20 13:26:24 -08001213 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001214 """
pingping-lin763ee042015-05-20 17:45:30 -07001215 Definition:
1216 Loads a json topology output
1217 Return:
1218 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -08001219 """
pingping-lin763ee042015-05-20 17:45:30 -07001220 import json
Jon Hall77f53ce2014-10-13 18:02:06 -04001221 try:
pingping-lin763ee042015-05-20 17:45:30 -07001222 # either onos:topology or 'topology' will work in CLI
1223 topology = json.loads(topologyOutput)
1224 print topology
Jon Hall77f53ce2014-10-13 18:02:06 -04001225 return topology
1226 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001227 main.log.error( self.name + ": EOF exception found" )
1228 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001229 main.cleanup()
1230 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001231 except Exception:
1232 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001233 main.cleanup()
1234 main.exit()
1235
kelvin-onlabd3b64892015-01-20 13:26:24 -08001236 def checkStatus(
1237 self,
1238 topologyResult,
1239 numoswitch,
1240 numolink,
1241 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001242 """
pingping-lin763ee042015-05-20 17:45:30 -07001243 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001244 supplied values. By default this will report to main.log, but the
pingping-lin763ee042015-05-20 17:45:30 -07001245 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08001246
Jon Hall77f53ce2014-10-13 18:02:06 -04001247 Params: ip = ip used for the onos cli
1248 numoswitch = expected number of switches
pingping-lin763ee042015-05-20 17:45:30 -07001249 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001250 logLevel = level to log to.
1251 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001252
1253
kelvin-onlabd3b64892015-01-20 13:26:24 -08001254 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001255
pingping-lin763ee042015-05-20 17:45:30 -07001256 Returns: main.TRUE if the number of switches and links are correct,
1257 main.FALSE if the number of switches and links is incorrect,
Jon Hall77f53ce2014-10-13 18:02:06 -04001258 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001259 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001260 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001261 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001262 if topology == {}:
1263 return main.ERROR
1264 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001265 # Is the number of switches is what we expected
kelvin-onlab90b6b042015-05-18 20:57:13 -07001266 devices = topology.get( 'devices', False )
1267 links = topology.get( 'links', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001268 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001269 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001270 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001271 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001272 linkCheck = ( int( links ) == int( numolink ) )
pingping-lin763ee042015-05-20 17:45:30 -07001273 if switchCheck and linkCheck:
kelvin8ec71442015-01-15 16:57:00 -08001274 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001275 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001276 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001277 result = main.TRUE
1278 else:
1279 output = output + \
pingping-lin763ee042015-05-20 17:45:30 -07001280 "The number of links and switches does not match " + \
1281 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001282 result = main.FALSE
pingping-lin763ee042015-05-20 17:45:30 -07001283 output = output + "\n ONOS sees %i devices" % int( devices )
1284 output = output + " (%i expected) " % int( numoswitch )
1285 output = output + "and %i links " % int( links )
1286 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001287 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001288 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001289 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001290 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001291 else:
kelvin8ec71442015-01-15 16:57:00 -08001292 main.log.info( output )
1293 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001294 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001295 main.log.error( self.name + ": EOF exception found" )
1296 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001297 main.cleanup()
1298 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001299 except Exception:
1300 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001301 main.cleanup()
1302 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001303
kelvin-onlabd3b64892015-01-20 13:26:24 -08001304 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001305 """
andrewonlab970399c2014-11-07 13:09:32 -05001306 Capture all packet activity and store in specified
1307 directory/file
1308
1309 Required:
1310 * interface: interface to capture
1311 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001312 """
pingping-lin763ee042015-05-20 17:45:30 -07001313 try:
1314 self.handle.sendline( "" )
1315 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001316
pingping-lin763ee042015-05-20 17:45:30 -07001317 self.handle.sendline( "tshark -i " + str( interface ) +
1318 " -t e -w " + str( dirFile ) + " &" )
1319 self.handle.sendline( "\r" )
1320 self.handle.expect( "Capturing on" )
1321 self.handle.sendline( "\r" )
1322 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001323
pingping-lin763ee042015-05-20 17:45:30 -07001324 main.log.info( "Tshark started capturing files on " +
1325 str( interface ) + " and saving to directory: " +
1326 str( dirFile ) )
1327 except pexpect.EOF:
1328 main.log.error( self.name + ": EOF exception found" )
1329 main.log.error( self.name + ": " + self.handle.before )
1330 main.cleanup()
1331 main.exit()
1332 except Exception:
1333 main.log.exception( self.name + ": Uncaught exception!" )
1334 main.cleanup()
1335 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001336
kelvin-onlabd3b64892015-01-20 13:26:24 -08001337 def runOnosTopoCfg( self, instanceName, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001338 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001339 On ONOS bench, run this command:
pingping-lin763ee042015-05-20 17:45:30 -07001340 {ONOS_HOME}/tools/test/bin/onos-topo-cfg $OC1 filename
kelvin-onlabd3b64892015-01-20 13:26:24 -08001341 which starts the rest and copies
1342 the json file to the onos instance
kelvin8ec71442015-01-15 16:57:00 -08001343 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001344 try:
kelvin8ec71442015-01-15 16:57:00 -08001345 self.handle.sendline( "" )
1346 self.handle.expect( "\$" )
pingping-lin763ee042015-05-20 17:45:30 -07001347 self.handle.sendline( "cd " + self.home + "/tools/test/bin" )
kelvin8ec71442015-01-15 16:57:00 -08001348 self.handle.expect( "/bin$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001349 cmd = "./onos-topo-cfg " + instanceName + " " + jsonFile
shahshreyae6c7cf42014-11-26 16:39:01 -08001350 print "cmd = ", cmd
kelvin8ec71442015-01-15 16:57:00 -08001351 self.handle.sendline( cmd )
1352 self.handle.expect( "\$" )
1353 self.handle.sendline( "cd ~" )
1354 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -08001355 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -07001356 except pexpect.EOF:
1357 main.log.error( self.name + ": EOF exception found" )
1358 main.log.error( self.name + ": " + self.handle.before )
1359 main.cleanup()
1360 main.exit()
1361 except Exception:
1362 main.log.exception( self.name + ": Uncaught exception!" )
1363 main.cleanup()
1364 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001365
pingping-lin763ee042015-05-20 17:45:30 -07001366 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001367 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001368 Required:
kelvin8ec71442015-01-15 16:57:00 -08001369 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001370 * directory to store results
1371 Optional:
1372 * interface - default: eth0
pingping-lin763ee042015-05-20 17:45:30 -07001373 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001374 Description:
1375 Uses tshark command to grep specific group of packets
1376 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001377 The timestamp is hardcoded to be in epoch
1378 """
pingping-lin763ee042015-05-20 17:45:30 -07001379 try:
1380 self.handle.sendline( "" )
1381 self.handle.expect( "\$" )
1382 self.handle.sendline( "" )
1383 if grepOptions:
1384 grepStr = "grep "+str(grepOptions)
1385 else:
1386 grepStr = "grep"
1387
1388 self.handle.sendline(
1389 "tshark -i " +
1390 str( interface ) +
1391 " -t e | " +
1392 grepStr + " --line-buffered \"" +
1393 str(grep) +
1394 "\" >" +
1395 directory +
1396 " &" )
1397 self.handle.sendline( "\r" )
1398 self.handle.expect( "Capturing on" )
1399 self.handle.sendline( "\r" )
1400 self.handle.expect( "\$" )
1401 except pexpect.EOF:
1402 main.log.error( self.name + ": EOF exception found" )
1403 main.log.error( self.name + ": " + self.handle.before )
1404 main.cleanup()
1405 main.exit()
1406 except Exception:
1407 main.log.exception( self.name + ": Uncaught exception!" )
1408 main.cleanup()
1409 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001410
kelvin-onlabd3b64892015-01-20 13:26:24 -08001411 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001412 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001413 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001414 """
1415 # Remove all pcap from previous captures
pingping-lin763ee042015-05-20 17:45:30 -07001416 try:
1417 self.execute( cmd="sudo rm /tmp/wireshark*" )
1418 self.handle.sendline( "" )
1419 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1420 " | grep -v grep | awk '{print $2}'`" )
1421 self.handle.sendline( "" )
1422 main.log.info( "Tshark stopped" )
1423 except pexpect.EOF:
1424 main.log.error( self.name + ": EOF exception found" )
1425 main.log.error( self.name + ": " + self.handle.before )
1426 main.cleanup()
1427 main.exit()
1428 except Exception:
1429 main.log.exception( self.name + ": Uncaught exception!" )
1430 main.cleanup()
1431 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001432
kelvin8ec71442015-01-15 16:57:00 -08001433 def ptpd( self, args ):
1434 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001435 Initiate ptp with user-specified args.
1436 Required:
1437 * args: specify string of args after command
1438 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001439 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001440 try:
kelvin8ec71442015-01-15 16:57:00 -08001441 self.handle.sendline( "sudo ptpd " + str( args ) )
1442 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001443 "Multiple",
1444 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001445 "\$" ] )
1446 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001447
andrewonlab0c38a4a2014-10-28 18:35:35 -04001448 if i == 0:
1449 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001450 main.log.info( "ptpd returned an error: " +
1451 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001452 return handle
1453 elif i == 1:
1454 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001455 main.log.error( "ptpd returned an error: " +
1456 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001457 return handle
1458 else:
1459 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001460
andrewonlab0c38a4a2014-10-28 18:35:35 -04001461 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001462 main.log.error( self.name + ": EOF exception found" )
1463 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001464 main.cleanup()
1465 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001466 except Exception:
1467 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001468 main.cleanup()
1469 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001470
kelvin-onlabd3b64892015-01-20 13:26:24 -08001471 def cpLogsToDir( self, logToCopy,
pingping-lin763ee042015-05-20 17:45:30 -07001472 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001473 """
1474 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001475 Current implementation of ONOS deletes its karaf
1476 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001477 you may want to use this function to capture
1478 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001479 Localtime will be attached to the filename
1480
1481 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001482 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001483 copy.
kelvin8ec71442015-01-15 16:57:00 -08001484 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001485 For copying multiple files, leave copyFileName
1486 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001487 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001488 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001489 ex ) /tmp/
1490 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001491 * copyFileName: If you want to rename the log
1492 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001493 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001494 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001495 try:
kelvin8ec71442015-01-15 16:57:00 -08001496 localtime = time.strftime( '%x %X' )
1497 localtime = localtime.replace( "/", "" )
1498 localtime = localtime.replace( " ", "_" )
1499 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001500 if destDir[ -1: ] != "/":
1501 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001502
kelvin-onlabd3b64892015-01-20 13:26:24 -08001503 if copyFileName:
pingping-lin763ee042015-05-20 17:45:30 -07001504 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1505 str( destDir ) + str( copyFileName ) +
1506 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001507 self.handle.expect( "cp" )
1508 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001509 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001510 self.handle.sendline( "cp " + str( logToCopy ) +
1511 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001512 self.handle.expect( "cp" )
1513 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001514
kelvin8ec71442015-01-15 16:57:00 -08001515 return self.handle.before
1516
1517 except pexpect.EOF:
1518 main.log.error( "Copying files failed" )
1519 main.log.error( self.name + ": EOF exception found" )
1520 main.log.error( self.name + ": " + self.handle.before )
pingping-lin763ee042015-05-20 17:45:30 -07001521 except Exception:
1522 main.log.exception( "Copying files failed" )
kelvin8ec71442015-01-15 16:57:00 -08001523
Jon Hall16b72c42015-05-20 10:23:36 -07001524 def checkLogs( self, onosIp, restart=False):
kelvin8ec71442015-01-15 16:57:00 -08001525 """
Jon Hall94fd0472014-12-08 11:52:42 -08001526 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001527 If restart is True, use the old version of onos-check-logs which
1528 does not print the full stacktrace, but shows the entire log file,
1529 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001530 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001531 """
Jon Hall94fd0472014-12-08 11:52:42 -08001532 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001533 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001534 if restart:
1535 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001536 self.handle.sendline( cmd )
1537 self.handle.expect( cmd )
1538 self.handle.expect( "\$" )
Jon Hall94fd0472014-12-08 11:52:42 -08001539 response = self.handle.before
1540 return response
1541 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001542 main.log.error( "Lost ssh connection" )
1543 main.log.error( self.name + ": EOF exception found" )
1544 main.log.error( self.name + ": " + self.handle.before )
pingping-lin763ee042015-05-20 17:45:30 -07001545 except Exception:
1546 main.log.exception( self.name + ": Uncaught exception!" )
1547 main.cleanup()
1548 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001549
kelvin-onlabd3b64892015-01-20 13:26:24 -08001550 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001551 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001552 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001553 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001554 try:
kelvin8ec71442015-01-15 16:57:00 -08001555 self.handle.sendline( "" )
1556 self.handle.expect( "\$" )
1557 self.handle.sendline( "onos-service " + str( node ) +
1558 " status" )
1559 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001560 "start/running",
1561 "stop/waiting",
kelvin8ec71442015-01-15 16:57:00 -08001562 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001563
1564 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001565 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001566 return main.TRUE
1567 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001568 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001569 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001570 main.cleanup()
1571 main.exit()
1572 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001573 main.log.error( self.name + ": EOF exception found" )
1574 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001575 main.cleanup()
1576 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001577 except Exception:
1578 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001579 main.cleanup()
1580 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001581
1582 def setIpTables( self, ip, port='', action='add', packet_type='',
1583 direction='INPUT', rule='DROP', states=True ):
1584 """
1585 Description:
1586 add or remove iptables rule to DROP (default) packets from
1587 specific IP and PORT
1588 Usage:
1589 * specify action ('add' or 'remove')
1590 when removing, pass in the same argument as you would add. It will
1591 delete that specific rule.
1592 * specify the ip to block
1593 * specify the destination port to block (defaults to all ports)
1594 * optional packet type to block (default tcp)
1595 * optional iptables rule (default DROP)
1596 * optional direction to block (default 'INPUT')
1597 * States boolean toggles adding all supported tcp states to the
1598 firewall rule
1599 Returns:
1600 main.TRUE on success or
1601 main.FALSE if given invalid input or
1602 main.ERROR if there is an error in response from iptables
1603 WARNING:
1604 * This function uses root privilege iptables command which may result
1605 in unwanted network errors. USE WITH CAUTION
1606 """
1607 import time
1608
1609 # NOTE*********
1610 # The strict checking methods of this driver function is intentional
1611 # to discourage any misuse or error of iptables, which can cause
1612 # severe network errors
1613 # *************
1614
1615 # NOTE: Sleep needed to give some time for rule to be added and
1616 # registered to the instance. If you are calling this function
1617 # multiple times this sleep will prevent any errors.
1618 # DO NOT REMOVE
1619 # time.sleep( 5 )
1620 try:
1621 # input validation
1622 action_type = action.lower()
1623 rule = rule.upper()
1624 direction = direction.upper()
1625 if action_type != 'add' and action_type != 'remove':
1626 main.log.error( "Invalid action type. Use 'add' or "
1627 "'remove' table rule" )
1628 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1629 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1630 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1631 "'ACCEPT' or 'LOG' only." )
1632 if direction != 'INPUT' and direction != 'OUTPUT':
1633 # NOTE currently only supports rules INPUT and OUPTUT
1634 main.log.error( "Invalid rule. Valid directions are"
1635 " 'OUTPUT' or 'INPUT'" )
1636 return main.FALSE
1637 return main.FALSE
1638 return main.FALSE
1639 if action_type == 'add':
1640 # -A is the 'append' action of iptables
1641 actionFlag = '-A'
1642 elif action_type == 'remove':
1643 # -D is the 'delete' rule of iptables
1644 actionFlag = '-D'
1645 self.handle.sendline( "" )
1646 self.handle.expect( "\$" )
1647 cmd = "sudo iptables " + actionFlag + " " +\
1648 direction +\
1649 " -s " + str( ip )
1650 # " -p " + str( packet_type ) +\
1651 if packet_type:
1652 cmd += " -p " + str( packet_type )
1653 if port:
1654 cmd += " --dport " + str( port )
1655 if states:
1656 cmd += " -m state --state="
1657 #FIXME- Allow user to configure which states to block
1658 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
1659 cmd += " -j " + str( rule )
1660
1661 self.handle.sendline( cmd )
1662 self.handle.expect( "\$" )
1663 main.log.warn( self.handle.before )
1664
1665 info_string = "On " + str( self.name )
1666 info_string += " " + str( action_type )
1667 info_string += " iptable rule [ "
1668 info_string += " IP: " + str( ip )
1669 info_string += " Port: " + str( port )
1670 info_string += " Rule: " + str( rule )
1671 info_string += " Direction: " + str( direction ) + " ]"
1672 main.log.info( info_string )
1673 return main.TRUE
1674 except pexpect.TIMEOUT:
1675 main.log.exception( self.name + ": Timeout exception in "
1676 "setIpTables function" )
1677 return main.ERROR
1678 except pexpect.EOF:
1679 main.log.error( self.name + ": EOF exception found" )
1680 main.log.error( self.name + ": " + self.handle.before )
1681 main.cleanup()
1682 main.exit()
1683 except Exception:
1684 main.log.exception( self.name + ": Uncaught exception!" )
1685 main.cleanup()
1686 main.exit()
1687
1688 def detailed_status(self, log_filename):
1689 """
1690 This method is used by STS to check the status of the controller
1691 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
1692 """
1693 import re
1694 try:
1695 self.handle.sendline( "" )
1696 self.handle.expect( "\$" )
1697 self.handle.sendline( "cd " + self.home )
1698 self.handle.expect( "\$" )
1699 self.handle.sendline( "service onos status" )
1700 self.handle.expect( "\$" )
1701 response = self.handle.before
1702 if re.search( "onos start/running", response ):
1703 # onos start/running, process 10457
1704 return 'RUNNING'
1705 # FIXME: Implement this case
1706 # elif re.search( pattern, response ):
1707 # return 'STARTING'
1708 elif re.search( "onos stop/", response ):
1709 # onos stop/waiting
1710 # FIXME handle this differently?: onos stop/pre-stop
1711 return 'STOPPED'
1712 # FIXME: Implement this case
1713 # elif re.search( pattern, response ):
1714 # return 'FROZEN'
1715 else:
1716 main.log.warn( self.name +
1717 " WARNING: status received unknown response" )
1718 main.log.warn( response )
1719 return 'ERROR', "Unknown response: %s" % response
1720 except pexpect.TIMEOUT:
1721 main.log.exception( self.name + ": Timeout exception in "
1722 "setIpTables function" )
1723 return 'ERROR', "Pexpect Timeout"
1724 except pexpect.EOF:
1725 main.log.error( self.name + ": EOF exception found" )
1726 main.log.error( self.name + ": " + self.handle.before )
1727 main.cleanup()
1728 main.exit()
1729 except Exception:
1730 main.log.exception( self.name + ": Uncaught exception!" )
1731 main.cleanup()
1732 main.exit()
1733
1734 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1735 '''
1736 Create/formats the LinkGraph.cfg file based on arguments
1737 -only creates a linear topology and connects islands
1738 -evenly distributes devices
1739 -must be called by ONOSbench
1740
1741 ONOSIpList - list of all of the node IPs to be used
1742
1743 deviceCount - number of switches to be assigned
1744 '''
1745 main.log.step("Creating link graph configuration file." )
1746 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
1747 tempFile = "/tmp/linkGraph.cfg"
1748
1749 linkGraph = open(tempFile, 'w+')
1750 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1751 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1752 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
1753
1754 clusterCount = len(ONOSIpList)
1755
1756 if type(deviceCount) is int or type(deviceCount) is str:
1757 deviceCount = int(deviceCount)
1758 switchList = [0]*(clusterCount+1)
1759 baselineSwitchCount = deviceCount/clusterCount
1760
1761 for node in range(1, clusterCount + 1):
1762 switchList[node] = baselineSwitchCount
1763
1764 for node in range(1, (deviceCount%clusterCount)+1):
1765 switchList[node] += 1
1766
1767 if type(deviceCount) is list:
1768 main.log.info("Using provided device distribution")
1769 switchList = [0]
1770 for i in deviceCount:
1771 switchList.append(int(i))
1772
1773 tempList = ['0']
1774 tempList.extend(ONOSIpList)
1775 ONOSIpList = tempList
1776
1777 myPort = 6
1778 lastSwitch = 0
1779 for node in range(1, clusterCount+1):
1780 if switchList[node] == 0:
1781 continue
1782
1783 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
1784
1785 if node > 1:
1786 #connect to last device on previous node
1787 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
1788 linkGraph.write(line)
1789
1790 lastSwitch = 0
1791 for switch in range (0, switchList[node]-1):
1792 line = ""
1793 line = ("\t" + str(switch) + ":" + str(myPort))
1794 line += " -- "
1795 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
1796 linkGraph.write(line)
1797 lastSwitch = switch+1
1798 lastIp = ONOSIpList[node]
1799
1800 #lastSwitch += 1
1801 if node < (clusterCount):
1802 #connect to first device on the next node
1803 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
1804 linkGraph.write(line)
1805
1806 linkGraph.write("}\n")
1807 linkGraph.close()
1808
1809 #SCP
1810 os.system( "scp " + tempFile + " admin@" + benchIp + ":" + linkGraphPath)
1811 main.log.info("linkGraph.cfg creation complete")
1812
1813 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
1814
1815 '''
1816 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
1817 deviceCount = number of switches to distribute, or list of values to use as custom distribution
1818 numPorts = number of ports per device. Defaults to 10 both in this function and in ONOS. Optional arg
1819 '''
1820
1821 main.log.step("Configuring Null Device Provider" )
1822 clusterCount = len(ONOSIpList)
1823
1824 try:
1825
1826 if type(deviceCount) is int or type(deviceCount) is str:
1827 main.log.step("Creating device distribution")
1828 deviceCount = int(deviceCount)
1829 switchList = [0]*(clusterCount+1)
1830 baselineSwitchCount = deviceCount/clusterCount
1831
1832 for node in range(1, clusterCount + 1):
1833 switchList[node] = baselineSwitchCount
1834
1835 for node in range(1, (deviceCount%clusterCount)+1):
1836 switchList[node] += 1
1837
1838 if type(deviceCount) is list:
1839 main.log.info("Using provided device distribution")
1840
1841 if len(deviceCount) == clusterCount:
1842 switchList = ['0']
1843 switchList.extend(deviceCount)
1844
1845 if len(deviceCount) == (clusterCount + 1):
1846 if deviceCount[0] == '0' or deviceCount[0] == 0:
1847 switchList = deviceCount
1848
1849 assert len(switchList) == (clusterCount + 1)
1850
1851 except AssertionError:
1852 main.log.error( "Bad device/Ip list match")
1853 except TypeError:
1854 main.log.exception( self.name + ": Object not as expected" )
1855 return None
1856 except Exception:
1857 main.log.exception( self.name + ": Uncaught exception!" )
1858 main.cleanup()
1859 main.exit()
1860
1861
1862 ONOSIp = [0]
1863 ONOSIp.extend(ONOSIpList)
1864
1865 devicesString = "devConfigs = "
1866 for node in range(1, len(ONOSIp)):
1867 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
1868 if node < clusterCount:
1869 devicesString += (",")
1870
1871 try:
1872 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
1873 self.handle.expect(":~")
1874 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
1875 self.handle.expect(":~")
1876
1877 for i in range(10):
1878 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
1879 self.handle.expect(":~")
1880 verification = self.handle.before
1881 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
1882 break
1883 else:
1884 time.sleep(1)
1885
1886 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
1887
1888 except AssertionError:
1889 main.log.error("Incorrect Config settings: " + verification)
1890 except Exception:
1891 main.log.exception( self.name + ": Uncaught exception!" )
1892 main.cleanup()
1893 main.exit()
1894
1895 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
1896 '''
1897 fileName default is currently the same as the default on ONOS, specify alternate file if
1898 you want to use a different topology file than linkGraph.cfg
1899 '''
1900
1901
1902 try:
1903 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
1904 self.handle.expect(":~")
1905 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
1906 self.handle.expect(":~")
1907
1908 for i in range(10):
1909 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
1910 self.handle.expect(":~")
1911 verification = self.handle.before
1912 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
1913 break
1914 else:
1915 time.sleep(1)
1916
1917 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
1918
1919 except pexpect.EOF:
1920 main.log.error( self.name + ": EOF exception found" )
1921 main.log.error( self.name + ": " + self.handle.before )
1922 main.cleanup()
1923 main.exit()
1924 except AssertionError:
1925 main.log.info("Settings did not post to ONOS")
1926 main.log.error(varification)
1927 except Exception:
1928 main.log.exception( self.name + ": Uncaught exception!" )
1929 main.log.error(varification)
1930 main.cleanup()
1931 main.exit()
1932
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001933 def getOnosIps(self):
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001934
1935 import os
kelvin-onlab0a28a742015-05-18 16:03:13 -07001936
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001937 # reads env for OC variables, also saves file with OC variables. If file and env conflict
1938 # priority goes to env. If file has OCs that are not in the env, the file OCs are used.
1939 # In other words, if the env is set, the test will use those values.
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001940
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001941 # returns a list of ip addresses for the onos nodes, will work with up to 7 nodes + OCN and OCI
1942 # returns in format [ OC1 ip, OC2 ...ect. , OCN, OCI ]
1943
1944 envONOSIps = {}
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001945
1946 x = 1
1947 while True:
1948 try:
1949 temp = os.environ[ 'OC' + str(x) ]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001950 except KeyError:
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001951 break
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001952 envONOSIps[ ("OC" + str(x)) ] = temp
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001953 x += 1
1954
1955 try:
1956 temp = os.environ[ 'OCN' ]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001957 envONOSIps[ "OCN" ] = temp
1958 except KeyError:
1959 main.log.info("OCN not set in env")
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001960
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001961 try:
1962 temp = os.environ[ 'OCI' ]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001963 envONOSIps[ "OCI" ] = temp
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001964 except:
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001965 main.log.error("OCI not set in env")
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001966
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001967 print(str(envONOSIps))
1968
1969 order = [ "OC1", "OC2", "OC3","OC4","OC5","OC6","OC7","OCN","OCI" ]
1970 ONOSIps = []
1971
1972 try:
cameron@onlab.us2e166212015-05-19 14:28:25 -07001973 if os.path.exists("myIps"):
1974 ipFile = open("myIps","r+")
1975 else:
1976 ipFile = open("myIps","w+")
1977
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001978 fileONOSIps = ipFile.readlines()
1979 ipFile.close()
1980
1981 print str(fileONOSIps)
1982
1983 if str(fileONOSIps) == "[]":
1984 ipFile = open("myIps","w+")
1985 for key in envONOSIps:
1986 ipFile.write(key+ "=" + envONOSIps[key] + "\n")
1987 ipFile.close()
1988 for i in order:
1989 if i in envONOSIps:
1990 ONOSIps.append(envONOSIps[i])
1991
1992 return ONOSIps
1993
1994 else:
1995 fileDict = {}
1996 for line in fileONOSIps:
1997 line = line.replace("\n","")
1998 line = line.split("=")
1999 key = line[0]
2000 value = line[1]
2001 fileDict[key] = value
2002
2003 for x in envONOSIps:
2004 if x in fileDict:
2005 if envONOSIps[x] == fileDict[x]:
2006 continue
2007 else:
2008 fileDict[x] = envONOSIps[x]
2009 else:
2010 fileDict[x] = envONOSIps[x]
2011
2012 ipFile = open("myIps","w+")
2013 for key in order:
2014 if key in fileDict:
2015 ipFile.write(key + "=" + fileDict[key] + "\n")
2016 ONOSIps.append(fileDict[key])
2017 ipFile.close()
2018
2019 return ONOSIps
2020
2021 except IOError as a:
2022 main.log.error(a)
2023
kelvin-onlab0a28a742015-05-18 16:03:13 -07002024 except Exception as a:
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002025 main.log.error(a)
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002026
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002027
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002028 def logReport(self, nodeIp, searchTerms, outputMode="s"):
2029 '''
2030 - accepts either a list or a string for "searchTerms" these
2031 terms will be searched for in the log and have their
2032 instances counted
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002033
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002034 - nodeIp is the ip of the node whos log is to be scanned
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002035
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002036 - output modes:
2037 "s" - Simple. Quiet output mode that just prints
cameron@onlab.us2e166212015-05-19 14:28:25 -07002038 the occurences of each search term
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002039
cameron@onlab.us2e166212015-05-19 14:28:25 -07002040 "d" - Detailed. Prints number of occurences as well as the entire
2041 line for each of the last 5 occurences
2042
2043 - returns total of the number of instances of all search terms
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002044 '''
2045 main.log.info("========================== Log Report ===========================\n")
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002046
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002047 if type(searchTerms) is str:
2048 searchTerms = [searchTerms]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002049
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002050 logLines = [ [" "] for i in range(len(searchTerms)) ]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002051
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002052 for term in range(len(searchTerms)):
2053 logLines[term][0] = searchTerms[term]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002054
cameron@onlab.us2e166212015-05-19 14:28:25 -07002055 totalHits = 0
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002056 for term in range(len(searchTerms)):
2057 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep " + searchTerms[term]
2058 self.handle.sendline(cmd)
2059 self.handle.expect(":~")
2060 before = (self.handle.before).splitlines()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002061
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002062 count = [searchTerms[term],0]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002063
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002064 for line in before:
2065 if searchTerms[term] in line and "grep" not in line:
2066 count[1] += 1
2067 if before.index(line) > ( len(before) - 7 ):
2068 logLines[term].append(line)
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002069
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002070 main.log.info( str(count[0]) + ": " + str(count[1]) )
2071 if term == len(searchTerms)-1:
2072 print("\n")
cameron@onlab.us2e166212015-05-19 14:28:25 -07002073 totalHits += int(count[1])
2074
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002075 if outputMode != "s" and outputMode != "S":
2076 outputString = ""
2077 for i in logLines:
2078 outputString = i[0] + ": \n"
2079 for x in range(1,len(i)):
2080 outputString += ( i[x] + "\n" )
2081
2082 if outputString != (i[0] + ": \n"):
2083 main.log.info(outputString)
2084
2085 main.log.info("================================================================\n")
cameron@onlab.us2e166212015-05-19 14:28:25 -07002086 return totalHits
pingping-lin763ee042015-05-20 17:45:30 -07002087
2088 def getOnosIpFromEnv(self):
2089
2090 import string
2091
2092 # returns a list of ip addresses for the onos nodes, will work with up to 7 nodes + OCN and OCI
2093 # returns in format [ 'x', OC1 ip, OC2 i... ect. ... , ONN ip ]
2094
2095 self.handle.sendline("env| grep OC")
2096 self.handle.expect(":~")
2097 rawOutput = self.handle.before
2098 print rawOutput
2099 print "-----------------------------"
2100 print repr(rawOutput)
2101 mpa = dict.fromkeys(range(32))
2102 translated = rawOutput.translate(mpa)
2103 print translated
2104
2105
2106 # create list with only the lines that have the needed IPs
2107 unparsedIps = []
2108
2109 # remove preceeding or trailing lines
2110 for line in rawOutput:
2111 if "OC" in line and "=" in line:
2112 unparsedIps.append(str(line))
2113
2114 # determine cluster size
2115 clusterCount = 0
2116 for line in unparsedIps:
2117 line = str(line)
2118 print line
2119 temp = line.replace("OC","")
2120 print("line index " + str(line.index("=")))
2121 OCindex = temp[0]
2122 for i in range(0, 7):
2123 if OCindex == str(i) and i > clusterCount:
2124 clusterCount == i
2125 print(clusterCount)
2126 # create list to hold ips such that OC1 is at list[1] and OCN and OCI are at the end (in that order)
2127 ONOSIps = ["x"] * (clusterCount + 3)
2128
2129 # populate list
2130 for line in unparsedIps:
2131 main.log.info(line)##########
2132 temp = str(line.replace("OC",""))
2133 main.log.info(str(list(temp)))
2134 OCindex = temp[0]
2135 main.log.info(OCindex)############
2136 if OCindex == "N":
2137 ONOSIps[ clusterCount + 1 ] = temp.replace("N=","")
2138
2139 if OCindex == "I":
2140 ONOSIps[ clusterCount + 2 ] = temp.replace("I=","")
2141
2142 else:
2143 ONOSIps[ int(OCindex) ] = temp.replace((OCindex + "=") ,"")
2144
2145 # validate
2146 for x in ONOSIps:
2147 if ONOSIps.index(x) != 0 and x == "x":
2148 main.log.error("ENV READ FAILURE, MISSING DATA: \n\n" + str(ONOSIps) + "\n\n")
2149
2150 return ONOSIps
2151
kelvin-onlab0a28a742015-05-18 16:03:13 -07002152
pingping-lin763ee042015-05-20 17:45:30 -07002153 def onosErrorLog(self, nodeIp):
2154
2155 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep WARN"
2156 self.handle.sendline(cmd)
2157 self.handle.expect(":~")
2158 before = (self.handle.before).splitlines()
kelvin-onlab0a28a742015-05-18 16:03:13 -07002159
pingping-lin763ee042015-05-20 17:45:30 -07002160 warnings = []
2161
2162 for line in before:
2163 if "WARN" in line and "grep" not in line:
2164 warnings.append(line)
2165 main.warnings[main.warnings[0]+1] = line
2166 main.warnings[0] += 1
2167 if main.warnings[0] >= 10:
2168 main.warnings[0] = 0
2169
2170 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep ERROR"
2171 self.handle.sendline(cmd)
2172 self.handle.expect(":~")
2173 before = (self.handle.before).splitlines()
2174
2175 errors = []
2176
2177 for line in before:
2178 if "ERROR" in line and "grep" not in line:
2179 errors.append(line)
2180 main.errors[main.errors[0]+1] = line
2181 main.errors[0] += 1
2182 if main.errors[0] >= 10:
2183 main.errors[0] = 0
2184
2185 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep Exept"
2186 self.handle.sendline(cmd)
2187 self.handle.expect(":~")
2188 before = (self.handle.before).splitlines()
2189
2190 exceptions = []
2191
2192 for line in before:
2193 if "Except" in line and "grep" not in line:
2194 exceptions.append(line)
2195 main.exceptions[main.errors[0]+1] = line
2196 main.exceptions[0] += 1
2197 if main.exceptions[0] >= 10:
2198 main.exceptions[0] = 0
2199
2200
2201
2202 ################################################################
2203
2204 msg1 = "WARNINGS: \n"
2205 for i in main.warnings:
2206 if type(i) is not int:
2207 msg1 += ( i + "\n")
2208
2209 msg2 = "ERRORS: \n"
2210 for i in main.errors:
2211 if type(i) is not int:
2212 msg2 += ( i + "\n")
2213
2214 msg3 = "EXCEPTIONS: \n"
2215 for i in main.exceptions:
2216 if type(i) is not int:
2217 msg3 += ( i + "\n")
2218
2219 main.log.info("===============================================================\n")
2220 main.log.info( "Warnings: " + str(len(warnings)))
2221 main.log.info( "Errors: " + str(len(errors)))
2222 main.log.info( "Exceptions: " + str(len(exceptions)))
2223 if len(warnings) > 0:
2224 main.log.info(msg1)
2225 if len(errors) > 0:
2226 main.log.info(msg2)
2227 if len(exceptions) > 0:
2228 main.log.info(msg3)
2229 main.log.info("===============================================================\n")