blob: bb3033544f20dad8dd011277f5a54cfe8f4d5dda [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
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400101 def getEpochMs( self ):
102 """
103 Returns milliseconds since epoch
104
105 When checking multiple nodes in a for loop,
106 around a hundred milliseconds of difference (ascending) is
107 generally acceptable due to calltime of the function.
108 Few seconds, however, is not and it means clocks
109 are off sync.
110 """
111 try:
112 self.handle.sendline( 'date +%s.%N' )
113 self.handle.expect( 'date \+\%s\.\%N' )
114 self.handle.expect( '\$' )
115 epochMs = self.handle.before
116 return epochMs
117 except Exception:
118 main.log.exception( 'Uncaught exception getting epoch time' )
119 main.cleanup()
120 main.exit()
121
pingping-lin57a56ce2015-05-20 16:43:48 -0700122 def onosPackage( self, opTimeout=30 ):
kelvin8ec71442015-01-15 16:57:00 -0800123 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400124 Produce a self-contained tar.gz file that can be deployed
kelvin8ec71442015-01-15 16:57:00 -0800125 and executed on any platform with Java 7 JRE.
126 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400127 try:
kelvin8ec71442015-01-15 16:57:00 -0800128 self.handle.sendline( "onos-package" )
129 self.handle.expect( "onos-package" )
pingping-lin57a56ce2015-05-20 16:43:48 -0700130 self.handle.expect( "tar.gz", opTimeout )
kelvin8ec71442015-01-15 16:57:00 -0800131 handle = str( self.handle.before )
132 main.log.info( "onos-package command returned: " +
133 handle )
134 # As long as the sendline does not time out,
135 # return true. However, be careful to interpret
136 # the results of the onos-package command return
andrewonlab0748d2a2014-10-09 13:24:17 -0400137 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400138
andrewonlab7735d852014-10-09 13:02:47 -0400139 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800140 main.log.error( self.name + ": EOF exception found" )
141 main.log.error( self.name + ": " + self.handle.before )
pingping-lin763ee042015-05-20 17:45:30 -0700142 except Exception:
143 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400144 main.cleanup()
145 main.exit()
146
kelvin-onlabd3b64892015-01-20 13:26:24 -0800147 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800148 """
andrewonlab8790abb2014-11-06 13:51:54 -0500149 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800150 """
andrewonlab8790abb2014-11-06 13:51:54 -0500151 try:
kelvin8ec71442015-01-15 16:57:00 -0800152 self.handle.sendline( "onos-build" )
153 self.handle.expect( "onos-build" )
154 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800155 "BUILD SUCCESS",
156 "ERROR",
157 "BUILD FAILED" ],
158 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800159 handle = str( self.handle.before )
andrewonlab8790abb2014-11-06 13:51:54 -0500160
kelvin8ec71442015-01-15 16:57:00 -0800161 main.log.info( "onos-build command returned: " +
162 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500163
164 if i == 0:
165 return main.TRUE
166 else:
167 return handle
168
169 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800170 main.log.error( self.name + ": EOF exception found" )
171 main.log.error( self.name + ": " + self.handle.before )
pingping-lin763ee042015-05-20 17:45:30 -0700172 except Exception:
173 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500174 main.cleanup()
175 main.exit()
176
pingping-lin57a56ce2015-05-20 16:43:48 -0700177 def cleanInstall( self, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800178 """
179 Runs mvn clean install in the root of the ONOS directory.
180 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400181
kelvin8ec71442015-01-15 16:57:00 -0800182 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400183 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800184 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400185 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800186 main.log.info( "Running 'mvn clean install' on " +
187 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800188 ". This may take some time." )
189 self.handle.sendline( "cd " + self.home )
190 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400191
kelvin8ec71442015-01-15 16:57:00 -0800192 self.handle.sendline( "" )
193 self.handle.expect( "\$" )
194 self.handle.sendline( "mvn clean install" )
195 self.handle.expect( "mvn clean install" )
196 while True:
197 i = self.handle.expect( [
pingping-lin763ee042015-05-20 17:45:30 -0700198 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
199 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400200 'BUILD\sFAILURE',
201 'BUILD\sSUCCESS',
pingping-lin763ee042015-05-20 17:45:30 -0700202 'onos\$', #TODO: fix this to be more generic?
Jon Hallde9d9aa2014-10-08 20:36:02 -0400203 'ONOS\$',
pingping-lin57a56ce2015-05-20 16:43:48 -0700204 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400205 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800206 main.log.error( self.name + ":There is insufficient memory \
207 for the Java Runtime Environment to continue." )
208 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400209 main.cleanup()
210 main.exit()
211 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800212 main.log.error( self.name + ": Build failure!" )
213 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400214 main.cleanup()
215 main.exit()
216 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800217 main.log.info( self.name + ": Build success!" )
pingping-lin763ee042015-05-20 17:45:30 -0700218 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800219 main.log.info( self.name + ": Build complete" )
220 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400221 for line in self.handle.before.splitlines():
222 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800223 main.log.info( line )
224 self.handle.sendline( "" )
225 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400226 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -0700227 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800228 main.log.error(
229 self.name +
230 ": mvn clean install TIMEOUT!" )
231 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400232 main.cleanup()
233 main.exit()
234 else:
pingping-lin763ee042015-05-20 17:45:30 -0700235 main.log.error( self.name + ": unexpected response from " +
236 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800237 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400238 main.cleanup()
239 main.exit()
240 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800241 main.log.error( self.name + ": EOF exception found" )
242 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400243 main.cleanup()
244 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700245 except Exception:
246 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400247 main.cleanup()
248 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400249
pingping-lin763ee042015-05-20 17:45:30 -0700250 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800251 """
Jon Hallacabffd2014-10-09 12:36:53 -0400252 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800253
pingping-lin763ee042015-05-20 17:45:30 -0700254 If the fastForward boolean is set to true, only git pulls that can
255 be fast forwarded will be performed. IE if you have not local commits
256 in your branch.
257
Jon Hallacabffd2014-10-09 12:36:53 -0400258 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800259 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400260 for the purpose of pulling from other nodes if necessary.
261
Jon Hall47a93fb2015-01-06 16:46:06 -0800262 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400263 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800264 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400265 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400266
kelvin8ec71442015-01-15 16:57:00 -0800267 """
Jon Hallacabffd2014-10-09 12:36:53 -0400268 try:
kelvin8ec71442015-01-15 16:57:00 -0800269 # main.log.info( self.name + ": Stopping ONOS" )
270 # self.stop()
271 self.handle.sendline( "cd " + self.home )
pingping-lin763ee042015-05-20 17:45:30 -0700272 self.handle.expect( self.home + "\$" )
273 cmd = "git pull"
274 if comp1 != "":
275 cmd += ' ' + comp1
276 if fastForward:
277 cmd += ' ' + " --ff-only"
278 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800279 i = self.handle.expect(
280 [
281 'fatal',
282 'Username\sfor\s(.*):\s',
283 '\sfile(s*) changed,\s',
284 'Already up-to-date',
285 'Aborting',
286 'You\sare\snot\scurrently\son\sa\sbranch',
pingping-lin763ee042015-05-20 17:45:30 -0700287 'You asked me to pull without telling me which branch you',
288 'Pull is not possible because you have unmerged files',
289 'Please enter a commit message to explain why this merge',
290 'Found a swap file by the name',
291 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800292 pexpect.TIMEOUT ],
293 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800294 # debug
kelvin-onlabd3b64892015-01-20 13:26:24 -0800295 # main.log.report( self.name +": DEBUG: \n"+
296 # "git pull response: " +
297 # str( self.handle.before ) + str( self.handle.after ) )
kelvin8ec71442015-01-15 16:57:00 -0800298 if i == 0:
pingping-lin763ee042015-05-20 17:45:30 -0700299 main.log.error( self.name + ": Git pull had some issue" )
300 output = self.handle.after
301 self.handle.expect( '\$' )
302 output += self.handle.before
303 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400304 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800305 elif i == 1:
306 main.log.error(
307 self.name +
308 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400309 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800310 elif i == 2:
311 main.log.info(
312 self.name +
313 ": Git Pull - pulling repository now" )
pingping-lin763ee042015-05-20 17:45:30 -0700314 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800315 # So that only when git pull is done, we do mvn clean compile
316 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800317 elif i == 3:
318 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800319 return i
kelvin8ec71442015-01-15 16:57:00 -0800320 elif i == 4:
321 main.log.info(
322 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700323 ": Git Pull - Aborting..." +
324 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400325 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800326 elif i == 5:
327 main.log.info(
328 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700329 ": Git Pull - You are not currently " +
330 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400331 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800332 elif i == 6:
333 main.log.info(
334 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700335 ": Git Pull - You have not configured an upstream " +
336 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400337 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800338 elif i == 7:
339 main.log.info(
340 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700341 ": Git Pull - Pull is not possible because " +
342 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400343 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800344 elif i == 8:
pingping-lin763ee042015-05-20 17:45:30 -0700345 # NOTE: abandoning test since we can't reliably handle this
346 # there could be different default text editors and we
347 # also don't know if we actually want to make the commit
348 main.log.error( "Git pull resulted in a merge commit message" +
349 ". Exiting test!" )
350 main.cleanup()
351 main.exit()
352 elif i == 9: # Merge commit message but swap file exists
353 main.log.error( "Git pull resulted in a merge commit message" +
354 " but a swap file exists." )
355 try:
356 self.handle.send( 'A' ) # Abort
357 self.handle.expect( "\$" )
358 return main.ERROR
359 except Exception:
360 main.log.exception( "Couldn't exit editor prompt!")
361 main.cleanup()
362 main.exit()
363 elif i == 10: # In the middle of a merge commit
364 main.log.error( "Git branch is in the middle of a merge. " )
365 main.log.warn( self.handle.before + self.handle.after )
366 return main.ERROR
367 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800368 main.log.error( self.name + ": Git Pull - TIMEOUT" )
369 main.log.error(
370 self.name + " Response was: " + str(
371 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400372 return main.ERROR
373 else:
kelvin8ec71442015-01-15 16:57:00 -0800374 main.log.error(
375 self.name +
376 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400377 return main.ERROR
378 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800379 main.log.error( self.name + ": EOF exception found" )
380 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400381 main.cleanup()
382 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700383 except Exception:
384 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400385 main.cleanup()
386 main.exit()
387
kelvin-onlabd3b64892015-01-20 13:26:24 -0800388 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800389 """
Jon Hallacabffd2014-10-09 12:36:53 -0400390 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800391
Jon Hallacabffd2014-10-09 12:36:53 -0400392 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800393 If used as gitCheckout( "branch" ) it will do git checkout
394 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400395
396 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800397 branch of the ONOS repository. If it has any problems, it will return
398 main.ERROR.
399 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400400 successful then the function will return main.TRUE.
401
kelvin8ec71442015-01-15 16:57:00 -0800402 """
Jon Hallacabffd2014-10-09 12:36:53 -0400403 try:
kelvin8ec71442015-01-15 16:57:00 -0800404 self.handle.sendline( "cd " + self.home )
pingping-lin763ee042015-05-20 17:45:30 -0700405 self.handle.expect( self.home + "\$" )
406 main.log.info( self.name +
407 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800408 cmd = "git checkout " + branch
409 self.handle.sendline( cmd )
410 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800411 i = self.handle.expect(
pingping-lin763ee042015-05-20 17:45:30 -0700412 [ 'fatal',
413 'Username for (.*): ',
414 'Already on \'',
415 'Switched to branch \'' + str( branch ),
416 pexpect.TIMEOUT,
417 'error: Your local changes to the following files' +
418 'would be overwritten by checkout:',
419 'error: you need to resolve your current index first',
420 "You are in 'detached HEAD' state.",
421 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800422 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800423 if i == 0:
424 main.log.error(
425 self.name +
426 ": Git checkout had some issue..." )
427 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400428 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800429 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800430 main.log.error(
431 self.name +
432 ": Git checkout asking for username." +
433 " Please configure your local git repository to be able " +
434 "to access your remote repository passwordlessly" )
pingping-lin763ee042015-05-20 17:45:30 -0700435 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400436 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800437 elif i == 2:
438 main.log.info(
439 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700440 ": Git Checkout %s : Already on this branch" % branch )
441 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800442 # main.log.info( "DEBUG: after checkout cmd = "+
443 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400444 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800445 elif i == 3:
446 main.log.info(
447 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700448 ": Git checkout %s - Switched to this branch" % branch )
449 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800450 # main.log.info( "DEBUG: after checkout cmd = "+
451 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400452 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800453 elif i == 4:
454 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
455 main.log.error(
pingping-lin763ee042015-05-20 17:45:30 -0700456 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400457 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800458 elif i == 5:
459 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800460 main.log.error(
461 self.name +
462 ": Git checkout error: \n" +
pingping-lin763ee042015-05-20 17:45:30 -0700463 "Your local changes to the following files would" +
464 " be overwritten by checkout:" +
465 str( self.handle.before ) )
466 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500467 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800468 elif i == 6:
pingping-lin763ee042015-05-20 17:45:30 -0700469 main.log.error(
470 self.name +
471 ": Git checkout error: \n" +
472 "You need to resolve your current index first:" +
473 str( self.handle.before ) )
474 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500475 return main.ERROR
pingping-lin763ee042015-05-20 17:45:30 -0700476 elif i == 7:
477 main.log.info(
478 self.name +
479 ": Git checkout " + str( branch ) +
480 " - You are in 'detached HEAD' state. HEAD is now at " +
481 str( branch ) )
482 self.handle.expect( self.home + "\$" )
483 return main.TRUE
484 elif i == 8: # Already in detached HEAD on the specified commit
485 main.log.info(
486 self.name +
487 ": Git Checkout %s : Already on commit" % branch )
488 self.handle.expect( self.home + "\$" )
489 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400490 else:
kelvin8ec71442015-01-15 16:57:00 -0800491 main.log.error(
492 self.name +
pingping-lin763ee042015-05-20 17:45:30 -0700493 ": Git Checkout - Unexpected response, " +
494 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800495 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400496 return main.ERROR
497
498 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800499 main.log.error( self.name + ": EOF exception found" )
500 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400501 main.cleanup()
502 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700503 except Exception:
504 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400505 main.cleanup()
506 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400507
pingping-lin6d23d9e2015-02-02 16:54:24 -0800508 def getBranchName( self ):
pingping-linf30cf272015-05-29 15:54:07 -0700509 main.log.info( "self.home = " )
510 main.log.info( self.home )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800511 self.handle.sendline( "cd " + self.home )
pingping-lin9bf3d8f2015-05-29 16:05:28 -0700512 self.handle.expect( self.home + "\$" )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800513 self.handle.sendline( "git name-rev --name-only HEAD" )
514 self.handle.expect( "git name-rev --name-only HEAD" )
515 self.handle.expect( "\$" )
516
517 lines = self.handle.before.splitlines()
518 if lines[1] == "master":
519 return "master"
520 elif lines[1] == "onos-1.0":
521 return "onos-1.0"
522 else:
523 main.log.info( lines[1] )
524 return "unexpected ONOS branch for SDN-IP test"
525
kelvin-onlabd3b64892015-01-20 13:26:24 -0800526 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800527 """
pingping-lin763ee042015-05-20 17:45:30 -0700528 Writes the COMMIT number to the report to be parsed
529 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800530 """
Jon Hall45ec0922014-10-10 19:33:49 -0400531 try:
kelvin8ec71442015-01-15 16:57:00 -0800532 self.handle.sendline( "" )
533 self.handle.expect( "\$" )
534 self.handle.sendline(
535 "cd " +
536 self.home +
pingping-lin763ee042015-05-20 17:45:30 -0700537 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
538 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800539 # NOTE: for some reason there are backspaces inserted in this
540 # phrase when run from Jenkins on some tests
541 self.handle.expect( "never" )
542 self.handle.expect( "\$" )
543 response = ( self.name + ": \n" + str(
544 self.handle.before + self.handle.after ) )
545 self.handle.sendline( "cd " + self.home )
546 self.handle.expect( "\$" )
547 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400548 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500549 print line
550 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700551 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800552 for line in lines[ 2:-1 ]:
553 # Bracket replacement is for Wiki-compliant
554 # formatting. '<' or '>' are interpreted
555 # as xml specific tags that cause errors
556 line = line.replace( "<", "[" )
557 line = line.replace( ">", "]" )
pingping-lin763ee042015-05-20 17:45:30 -0700558 #main.log.wiki( "\t" + line )
559 main.log.wiki( line + "<br /> " )
560 main.log.summary( line )
561 main.log.wiki( "</blockquote>" )
562 main.log.summary("\n")
kelvin8ec71442015-01-15 16:57:00 -0800563 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400564 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800565 main.log.error( self.name + ": EOF exception found" )
566 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400567 main.cleanup()
568 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800569 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800570 main.log.error( self.name + ": TIMEOUT exception found" )
571 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800572 main.cleanup()
573 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700574 except Exception:
575 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400576 main.cleanup()
577 main.exit()
578
kelvin-onlabd3b64892015-01-20 13:26:24 -0800579 def createCellFile( self, benchIp, fileName, mnIpAddrs,
pingping-lin763ee042015-05-20 17:45:30 -0700580 appString, *onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800581 """
andrewonlab94282092014-10-10 13:00:11 -0400582 Creates a cell file based on arguments
583 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800584 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400585 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800586 * File name of the cell file ( fileName )
587 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800588 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400589 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800590 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400591 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800592
andrewonlab94282092014-10-10 13:00:11 -0400593 NOTE: Assumes cells are located at:
594 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800595 """
596 # Variable initialization
kelvin-onlabd3b64892015-01-20 13:26:24 -0800597 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800598 # We want to create the cell file in the dependencies directory
599 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800600 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800601 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800602 cellFile = open( tempDirectory + fileName, 'w+' )
kelvin8ec71442015-01-15 16:57:00 -0800603
pingping-lin763ee042015-05-20 17:45:30 -0700604 # App string is hardcoded environment variables
kelvin8ec71442015-01-15 16:57:00 -0800605 # That you may wish to use by default on startup.
pingping-lin763ee042015-05-20 17:45:30 -0700606 # Note that you may not want certain apps listed
kelvin8ec71442015-01-15 16:57:00 -0800607 # on here.
pingping-lin763ee042015-05-20 17:45:30 -0700608 appString = "export ONOS_APPS=" + appString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800609 mnString = "export OCN="
kelvin-onlabf70fd542015-05-07 18:41:40 -0700610 if mnIpAddrs == "":
611 mnString = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800612 onosString = "export OC"
613 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800614
kelvin-onlabd3b64892015-01-20 13:26:24 -0800615 # Create ONOSNIC ip address prefix
616 tempOnosIp = onosIpAddrs[ 0 ]
617 tempList = []
618 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800619 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800620 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800621 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800622 nicAddr = ".".join( tempList ) + ".*"
623 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400624
625 try:
kelvin8ec71442015-01-15 16:57:00 -0800626 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800627 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400628
kelvin-onlabd3b64892015-01-20 13:26:24 -0800629 for arg in onosIpAddrs:
630 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800631 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400632 # export OC1="10.128.20.11"
633 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800634 cellFile.write( onosString + str( tempCount ) +
635 "=" + "\"" + arg + "\"" + "\n" )
636 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800637
kelvin-onlabd3b64892015-01-20 13:26:24 -0800638 cellFile.write( mnString + "\"" + mnIpAddrs + "\"" + "\n" )
pingping-lin763ee042015-05-20 17:45:30 -0700639 cellFile.write( appString + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800640 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400641
kelvin8ec71442015-01-15 16:57:00 -0800642 # We use os.system to send the command to TestON cluster
643 # to account for the case in which TestON is not located
644 # on the same cluster as the ONOS bench
645 # Note that even if TestON is located on the same cluster
646 # as ONOS bench, you must setup passwordless ssh
647 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800648 os.system( "scp " + tempDirectory + fileName +
649 " admin@" + benchIp + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400650
andrewonlab2a6c9342014-10-16 13:40:15 -0400651 return main.TRUE
652
andrewonlab94282092014-10-10 13:00:11 -0400653 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800654 main.log.error( self.name + ": EOF exception found" )
655 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400656 main.cleanup()
657 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700658 except Exception:
659 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400660 main.cleanup()
661 main.exit()
662
kelvin-onlabd3b64892015-01-20 13:26:24 -0800663 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800664 """
andrewonlab95ca1462014-10-09 14:04:24 -0400665 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800666 """
andrewonlab95ca1462014-10-09 14:04:24 -0400667 try:
668 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800669 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400670 main.cleanup()
671 main.exit()
672 else:
kelvin8ec71442015-01-15 16:57:00 -0800673 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800674 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800675 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400676 # and that this driver will have to change accordingly
pingping-lin763ee042015-05-20 17:45:30 -0700677 self.handle.expect(str(cellname))
kelvin-onlabd3b64892015-01-20 13:26:24 -0800678 handleBefore = self.handle.before
679 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800680 # Get the rest of the handle
pingping-lin763ee042015-05-20 17:45:30 -0700681 self.handle.sendline("")
682 self.handle.expect("\$")
kelvin-onlabd3b64892015-01-20 13:26:24 -0800683 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400684
kelvin-onlabd3b64892015-01-20 13:26:24 -0800685 main.log.info( "Cell call returned: " + handleBefore +
686 handleAfter + handleMore )
andrewonlab95ca1462014-10-09 14:04:24 -0400687
688 return main.TRUE
689
690 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800691 main.log.error( self.name + ": EOF exception found" )
692 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400693 main.cleanup()
694 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700695 except Exception:
696 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400697 main.cleanup()
698 main.exit()
699
kelvin-onlabd3b64892015-01-20 13:26:24 -0800700 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800701 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400702 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800703 """
704 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400705
andrewonlabc03bf6c2014-10-09 14:56:18 -0400706 try:
kelvin8ec71442015-01-15 16:57:00 -0800707 # Clean handle by sending empty and expecting $
708 self.handle.sendline( "" )
709 self.handle.expect( "\$" )
710 self.handle.sendline( "onos-verify-cell" )
711 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800712 handleBefore = self.handle.before
713 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800714 # Get the rest of the handle
715 self.handle.sendline( "" )
716 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800717 handleMore = self.handle.before
andrewonlabc03bf6c2014-10-09 14:56:18 -0400718
kelvin-onlabd3b64892015-01-20 13:26:24 -0800719 main.log.info( "Verify cell returned: " + handleBefore +
720 handleAfter + handleMore )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400721
722 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -0700723 except pexpect.ExceptionPexpect as e:
724 main.log.error( self.name + ": Pexpect exception found of type " +
725 str( type( e ) ) )
726 main.log.error ( e.get_trace() )
kelvin8ec71442015-01-15 16:57:00 -0800727 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400728 main.cleanup()
729 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700730 except Exception:
731 main.log.exception( self.name + ": Uncaught exception!" )
732 main.cleanup()
733 main.exit()
734
735 def onosCfgSet( self, ONOSIp, configName, configParam ):
736 """
737 Uses 'onos <node-ip> cfg set' to change a parameter value of an
738 application.
739
740 ex)
741 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
742
743 ONOSIp = '10.0.0.1'
744 configName = 'org.onosproject.myapp'
745 configParam = 'appSetting 1'
746
747 """
748 try:
749 cfgStr = ( "onos "+str(ONOSIp)+" cfg set "+
750 str(configName) + " " +
751 str(configParam)
752 )
753
754 self.handle.sendline( "" )
755 self.handle.expect( "\$" )
756 self.handle.sendline( cfgStr )
757 self.handle.expect( "\$" )
758
759 # TODO: Add meaningful assertion
760
761 return main.TRUE
762
763 except pexpect.ExceptionPexpect as e:
764 main.log.error( self.name + ": Pexpect exception found of type " +
765 str( type( e ) ) )
766 main.log.error ( e.get_trace() )
767 main.log.error( self.name + ": " + self.handle.before )
768 main.cleanup()
769 main.exit()
770 except Exception:
771 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400772 main.cleanup()
773 main.exit()
774
kelvin-onlabd3b64892015-01-20 13:26:24 -0800775 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800776 """
andrewonlab05e362f2014-10-10 00:40:57 -0400777 Uses 'onos' command to send various ONOS CLI arguments.
778 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800779 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400780 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800781
782 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400783 CLI commands for ONOS. Try to use this function first
784 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800785 function.
786 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400787 by starting onos, and typing in 'onos' to enter the
788 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800789 available commands.
790 """
andrewonlab05e362f2014-10-10 00:40:57 -0400791 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800792 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800793 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400794 return main.FALSE
795 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800796 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400797 return main.FALSE
798
kelvin8ec71442015-01-15 16:57:00 -0800799 cmdstr = str( cmdstr )
800 self.handle.sendline( "" )
801 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400802
kelvin-onlabd3b64892015-01-20 13:26:24 -0800803 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800804 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400805
kelvin-onlabd3b64892015-01-20 13:26:24 -0800806 handleBefore = self.handle.before
Shreya Shaha73aaad2014-10-27 18:03:09 -0400807 print "handle_before = ", self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800808 # handleAfter = str( self.handle.after )
Jon Hall47a93fb2015-01-06 16:46:06 -0800809
kelvin8ec71442015-01-15 16:57:00 -0800810 # self.handle.sendline( "" )
811 # self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800812 # handleMore = str( self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400813
kelvin8ec71442015-01-15 16:57:00 -0800814 main.log.info( "Command sent successfully" )
andrewonlab05e362f2014-10-10 00:40:57 -0400815
kelvin8ec71442015-01-15 16:57:00 -0800816 # Obtain return handle that consists of result from
817 # the onos command. The string may need to be
818 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800819 # returnString = handleBefore + handleAfter
820 returnString = handleBefore
821 print "return_string = ", returnString
822 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400823
824 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800825 main.log.error( self.name + ": EOF exception found" )
826 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400827 main.cleanup()
828 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700829 except Exception:
830 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400831 main.cleanup()
832 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400833
kelvin-onlabd3b64892015-01-20 13:26:24 -0800834 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800835 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400836 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800837 If -f option is provided, it also forces an uninstall.
838 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400839 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800840 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400841 files to certain onos nodes
842
843 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800844 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400845 try:
andrewonlab114768a2014-11-14 12:44:44 -0500846 if options:
kelvin8ec71442015-01-15 16:57:00 -0800847 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500848 else:
kelvin8ec71442015-01-15 16:57:00 -0800849 self.handle.sendline( "onos-install " + node )
850 self.handle.expect( "onos-install " )
851 # NOTE: this timeout may need to change depending on the network
852 # and size of ONOS
853 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800854 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800855 "ONOS\sis\salready\sinstalled",
856 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400857
Jon Hall7993bfc2014-10-09 16:30:14 -0400858 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800859 main.log.warn( "Network is unreachable" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400860 return main.FALSE
861 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800862 main.log.info(
863 "ONOS was installed on " +
864 node +
865 " and started" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400866 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500867 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800868 main.log.info( "ONOS is already installed on " + node )
andrewonlabd9a73a72014-11-14 17:28:21 -0500869 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800870 elif i == 3:
871 main.log.info(
872 "Installation of ONOS on " +
873 node +
874 " timed out" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400875 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400876
877 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800878 main.log.error( self.name + ": EOF exception found" )
879 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400880 main.cleanup()
881 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700882 except Exception:
883 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400884 main.cleanup()
885 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400886
kelvin-onlabd3b64892015-01-20 13:26:24 -0800887 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800888 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400889 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400890 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800891 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400892 try:
kelvin8ec71442015-01-15 16:57:00 -0800893 self.handle.sendline( "" )
894 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800895 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800896 " start" )
897 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -0400898 "Job\sis\salready\srunning",
899 "start/running",
900 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800901 pexpect.TIMEOUT ], timeout=120 )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400902
903 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800904 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400905 return main.TRUE
906 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800907 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400908 return main.TRUE
909 else:
kelvin8ec71442015-01-15 16:57:00 -0800910 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400911 main.cleanup()
912 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400913 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800914 main.log.error( self.name + ": EOF exception found" )
915 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400916 main.cleanup()
917 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700918 except Exception:
919 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400920 main.cleanup()
921 main.exit()
922
kelvin-onlabd3b64892015-01-20 13:26:24 -0800923 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800924 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400925 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400926 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800927 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400928 try:
kelvin8ec71442015-01-15 16:57:00 -0800929 self.handle.sendline( "" )
930 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800931 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800932 " stop" )
933 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -0400934 "stop/waiting",
pingping-lin763ee042015-05-20 17:45:30 -0700935 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -0400936 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800937 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2b30bd32014-10-09 16:48:55 -0400938
939 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800940 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400941 return main.TRUE
942 elif i == 1:
pingping-lin763ee042015-05-20 17:45:30 -0700943 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800944 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -0400945 return main.FALSE
pingping-lin763ee042015-05-20 17:45:30 -0700946 elif i == 2:
947 main.log.warn( "ONOS wasn't running" )
948 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -0400949 else:
kelvin8ec71442015-01-15 16:57:00 -0800950 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400951 return main.FALSE
952
953 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800954 main.log.error( self.name + ": EOF exception found" )
955 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -0400956 main.cleanup()
957 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700958 except Exception:
959 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400960 main.cleanup()
961 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800962
kelvin-onlabd3b64892015-01-20 13:26:24 -0800963 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -0800964 """
andrewonlabc8d47972014-10-09 16:52:36 -0400965 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -0800966 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -0400967 if needed
kelvin8ec71442015-01-15 16:57:00 -0800968 """
andrewonlabc8d47972014-10-09 16:52:36 -0400969 try:
kelvin8ec71442015-01-15 16:57:00 -0800970 self.handle.sendline( "" )
pingping-lin763ee042015-05-20 17:45:30 -0700971 self.handle.expect( "\$", timeout=60 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800972 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800973 self.handle.expect( "\$" )
andrewonlabc8d47972014-10-09 16:52:36 -0400974
kelvin-onlabd3b64892015-01-20 13:26:24 -0800975 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
andrewonlab9dfd2082014-11-13 17:44:03 -0500976
kelvin8ec71442015-01-15 16:57:00 -0800977 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -0400978 return main.TRUE
979
pingping-lin763ee042015-05-20 17:45:30 -0700980 except pexpect.TIMEOUT:
981 main.log.exception( self.name + ": Timeout in onosUninstall" )
982 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -0400983 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800984 main.log.error( self.name + ": EOF exception found" )
985 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -0400986 main.cleanup()
987 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -0700988 except Exception:
989 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -0400990 main.cleanup()
991 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400992
kelvin-onlabd3b64892015-01-20 13:26:24 -0800993 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800994 """
andrewonlabaedc8332014-12-04 12:43:03 -0500995 Issues the command 'onos-die <node-ip>'
996 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -0800997 """
andrewonlabaedc8332014-12-04 12:43:03 -0500998 try:
kelvin8ec71442015-01-15 16:57:00 -0800999 self.handle.sendline( "" )
1000 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001001 cmdStr = "onos-kill " + str( nodeIp )
1002 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001003 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001004 "Killing\sONOS",
1005 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -08001006 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -05001007 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001008 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001009 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -05001010 return main.TRUE
1011 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001012 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -05001013 return main.FALSE
1014 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001015 main.log.error( self.name + ": EOF exception found" )
1016 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -05001017 main.cleanup()
1018 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001019 except Exception:
1020 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -05001021 main.cleanup()
1022 main.exit()
1023
kelvin-onlabd3b64892015-01-20 13:26:24 -08001024 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001025 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001026 Calls the command: 'onos-kill [<node-ip>]'
1027 "Remotely, and unceremoniously kills the ONOS instance running on
1028 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001029 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001030 try:
kelvin8ec71442015-01-15 16:57:00 -08001031 self.handle.sendline( "" )
1032 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001033 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001034 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -04001035 "\$",
1036 "No\sroute\sto\shost",
1037 "password:",
kelvin8ec71442015-01-15 16:57:00 -08001038 pexpect.TIMEOUT ], timeout=20 )
1039
andrewonlabe8e56fd2014-10-09 17:12:44 -04001040 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001041 main.log.info(
1042 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001043 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001044 return main.TRUE
1045 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001046 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001047 return main.FALSE
1048 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001049 main.log.info(
1050 "Passwordless login for host: " +
1051 str( nodeIp ) +
1052 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001053 return main.FALSE
1054 else:
pingping-lin763ee042015-05-20 17:45:30 -07001055 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001056 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001057
andrewonlabe8e56fd2014-10-09 17:12:44 -04001058 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001059 main.log.error( self.name + ": EOF exception found" )
1060 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001061 main.cleanup()
1062 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001063 except Exception:
1064 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001065 main.cleanup()
1066 main.exit()
1067
kelvin-onlabd3b64892015-01-20 13:26:24 -08001068 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001069 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001070 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001071 a cleaner environment.
1072
andrewonlab19fbdca2014-11-14 12:55:59 -05001073 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001074 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001075 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001076 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001077 try:
kelvin8ec71442015-01-15 16:57:00 -08001078 self.handle.sendline( "" )
1079 self.handle.expect( "\$" )
1080 self.handle.sendline( "onos-remove-raft-logs" )
1081 # Sometimes this command hangs
1082 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1083 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001084 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001085 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1086 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001087 if i == 1:
1088 return main.FALSE
pingping-lin763ee042015-05-20 17:45:30 -07001089 #self.handle.sendline( "" )
1090 #self.handle.expect( "\$" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001091 return main.TRUE
1092
1093 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001094 main.log.error( self.name + ": EOF exception found" )
1095 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001096 main.cleanup()
1097 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001098 except Exception:
1099 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001100 main.cleanup()
1101 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001102
kelvin-onlabd3b64892015-01-20 13:26:24 -08001103 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001104 """
1105 Calls the command 'onos-start-network [ <mininet-topo> ]
1106 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001107 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001108 cell."
andrewonlab94282092014-10-10 13:00:11 -04001109 * Specify mininet topology file name for mntopo
1110 * Topo files should be placed at:
1111 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001112
andrewonlab94282092014-10-10 13:00:11 -04001113 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001114 """
andrewonlab94282092014-10-10 13:00:11 -04001115 try:
1116 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001117 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001118 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001119
kelvin8ec71442015-01-15 16:57:00 -08001120 mntopo = str( mntopo )
1121 self.handle.sendline( "" )
1122 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001123
kelvin8ec71442015-01-15 16:57:00 -08001124 self.handle.sendline( "onos-start-network " + mntopo )
1125 self.handle.expect( "mininet>" )
1126 main.log.info( "Network started, entered mininet prompt" )
1127
1128 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001129
1130 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001131 main.log.error( self.name + ": EOF exception found" )
1132 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001133 main.cleanup()
1134 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001135 except Exception:
1136 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001137 main.cleanup()
1138 main.exit()
1139
pingping-lin763ee042015-05-20 17:45:30 -07001140 def isup(self, node = "", timeout = 120):
kelvin8ec71442015-01-15 16:57:00 -08001141 """
1142 Run's onos-wait-for-start which only returns once ONOS is at run
pingping-lin763ee042015-05-20 17:45:30 -07001143 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001144
Jon Hall7993bfc2014-10-09 16:30:14 -04001145 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001146 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001147 try:
pingping-lin763ee042015-05-20 17:45:30 -07001148 self.handle.sendline("onos-wait-for-start " + node )
1149 self.handle.expect("onos-wait-for-start")
kelvin8ec71442015-01-15 16:57:00 -08001150 # NOTE: this timeout is arbitrary"
pingping-lin763ee042015-05-20 17:45:30 -07001151 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001152 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001153 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001154 return main.TRUE
1155 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001156 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001157 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001158 main.log.error( "ONOS has not started yet" )
1159 self.handle.send( "\x03" ) # Control-C
1160 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001161 return main.FALSE
1162 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001163 main.log.error( self.name + ": EOF exception found" )
1164 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001165 main.cleanup()
1166 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001167 except Exception:
1168 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001169 main.cleanup()
1170 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001171
kelvin-onlabd3b64892015-01-20 13:26:24 -08001172 def pushTestIntentsShell(
1173 self,
1174 dpidSrc,
1175 dpidDst,
1176 numIntents,
1177 dirFile,
1178 onosIp,
1179 numMult="",
1180 appId="",
1181 report=True,
1182 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001183 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001184 Description:
kelvin8ec71442015-01-15 16:57:00 -08001185 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001186 better parallelize the results than the CLI
1187 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001188 * dpidSrc: specify source dpid
1189 * dpidDst: specify destination dpid
1190 * numIntents: specify number of intents to push
1191 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001192 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001193 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001194 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001195 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001196 """
1197 try:
1198 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001199 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001200 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001201 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001202 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001203 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001204
kelvin-onlabd3b64892015-01-20 13:26:24 -08001205 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1206 if not numMult:
1207 addIntents = addDpid + " " + str( numIntents )
1208 elif numMult:
1209 addIntents = addDpid + " " + str( numIntents ) + " " +\
1210 str( numMult )
1211 if appId:
1212 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001213 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001214 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001215
andrewonlabaedc8332014-12-04 12:43:03 -05001216 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001217 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001218 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001219 sendCmd = addApp + " &"
1220 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001221
kelvin-onlabd3b64892015-01-20 13:26:24 -08001222 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001223
1224 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001225 main.log.error( self.name + ": EOF exception found" )
1226 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001227 main.cleanup()
1228 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001229 except Exception:
1230 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001231 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001232 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001233
kelvin-onlabd3b64892015-01-20 13:26:24 -08001234 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001235 """
pingping-lin763ee042015-05-20 17:45:30 -07001236 Definition:
1237 Loads a json topology output
1238 Return:
1239 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -08001240 """
pingping-lin763ee042015-05-20 17:45:30 -07001241 import json
Jon Hall77f53ce2014-10-13 18:02:06 -04001242 try:
pingping-lin763ee042015-05-20 17:45:30 -07001243 # either onos:topology or 'topology' will work in CLI
1244 topology = json.loads(topologyOutput)
1245 print topology
Jon Hall77f53ce2014-10-13 18:02:06 -04001246 return topology
1247 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001248 main.log.error( self.name + ": EOF exception found" )
1249 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001250 main.cleanup()
1251 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001252 except Exception:
1253 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001254 main.cleanup()
1255 main.exit()
1256
kelvin-onlabd3b64892015-01-20 13:26:24 -08001257 def checkStatus(
1258 self,
1259 topologyResult,
1260 numoswitch,
1261 numolink,
1262 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001263 """
pingping-lin763ee042015-05-20 17:45:30 -07001264 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001265 supplied values. By default this will report to main.log, but the
pingping-lin763ee042015-05-20 17:45:30 -07001266 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08001267
Jon Hall77f53ce2014-10-13 18:02:06 -04001268 Params: ip = ip used for the onos cli
1269 numoswitch = expected number of switches
pingping-lin763ee042015-05-20 17:45:30 -07001270 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001271 logLevel = level to log to.
1272 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001273
1274
kelvin-onlabd3b64892015-01-20 13:26:24 -08001275 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001276
pingping-lin763ee042015-05-20 17:45:30 -07001277 Returns: main.TRUE if the number of switches and links are correct,
1278 main.FALSE if the number of switches and links is incorrect,
Jon Hall77f53ce2014-10-13 18:02:06 -04001279 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001280 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001281 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001282 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001283 if topology == {}:
1284 return main.ERROR
1285 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001286 # Is the number of switches is what we expected
kelvin-onlab90b6b042015-05-18 20:57:13 -07001287 devices = topology.get( 'devices', False )
1288 links = topology.get( 'links', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001289 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001290 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001291 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001292 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001293 linkCheck = ( int( links ) == int( numolink ) )
pingping-lin763ee042015-05-20 17:45:30 -07001294 if switchCheck and linkCheck:
kelvin8ec71442015-01-15 16:57:00 -08001295 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001296 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001297 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001298 result = main.TRUE
1299 else:
1300 output = output + \
pingping-lin763ee042015-05-20 17:45:30 -07001301 "The number of links and switches does not match " + \
1302 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001303 result = main.FALSE
pingping-lin763ee042015-05-20 17:45:30 -07001304 output = output + "\n ONOS sees %i devices" % int( devices )
1305 output = output + " (%i expected) " % int( numoswitch )
1306 output = output + "and %i links " % int( links )
1307 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001308 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001309 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001310 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001311 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001312 else:
kelvin8ec71442015-01-15 16:57:00 -08001313 main.log.info( output )
1314 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001315 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001316 main.log.error( self.name + ": EOF exception found" )
1317 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001318 main.cleanup()
1319 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001320 except Exception:
1321 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001322 main.cleanup()
1323 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001324
kelvin-onlabd3b64892015-01-20 13:26:24 -08001325 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001326 """
andrewonlab970399c2014-11-07 13:09:32 -05001327 Capture all packet activity and store in specified
1328 directory/file
1329
1330 Required:
1331 * interface: interface to capture
1332 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001333 """
pingping-lin763ee042015-05-20 17:45:30 -07001334 try:
1335 self.handle.sendline( "" )
1336 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001337
pingping-lin763ee042015-05-20 17:45:30 -07001338 self.handle.sendline( "tshark -i " + str( interface ) +
1339 " -t e -w " + str( dirFile ) + " &" )
1340 self.handle.sendline( "\r" )
1341 self.handle.expect( "Capturing on" )
1342 self.handle.sendline( "\r" )
1343 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001344
pingping-lin763ee042015-05-20 17:45:30 -07001345 main.log.info( "Tshark started capturing files on " +
1346 str( interface ) + " and saving to directory: " +
1347 str( dirFile ) )
1348 except pexpect.EOF:
1349 main.log.error( self.name + ": EOF exception found" )
1350 main.log.error( self.name + ": " + self.handle.before )
1351 main.cleanup()
1352 main.exit()
1353 except Exception:
1354 main.log.exception( self.name + ": Uncaught exception!" )
1355 main.cleanup()
1356 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001357
kelvin-onlabd3b64892015-01-20 13:26:24 -08001358 def runOnosTopoCfg( self, instanceName, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001359 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001360 On ONOS bench, run this command:
pingping-lin763ee042015-05-20 17:45:30 -07001361 {ONOS_HOME}/tools/test/bin/onos-topo-cfg $OC1 filename
kelvin-onlabd3b64892015-01-20 13:26:24 -08001362 which starts the rest and copies
1363 the json file to the onos instance
kelvin8ec71442015-01-15 16:57:00 -08001364 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001365 try:
kelvin8ec71442015-01-15 16:57:00 -08001366 self.handle.sendline( "" )
1367 self.handle.expect( "\$" )
pingping-lin763ee042015-05-20 17:45:30 -07001368 self.handle.sendline( "cd " + self.home + "/tools/test/bin" )
kelvin8ec71442015-01-15 16:57:00 -08001369 self.handle.expect( "/bin$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001370 cmd = "./onos-topo-cfg " + instanceName + " " + jsonFile
shahshreyae6c7cf42014-11-26 16:39:01 -08001371 print "cmd = ", cmd
kelvin8ec71442015-01-15 16:57:00 -08001372 self.handle.sendline( cmd )
1373 self.handle.expect( "\$" )
1374 self.handle.sendline( "cd ~" )
1375 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -08001376 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -07001377 except pexpect.EOF:
1378 main.log.error( self.name + ": EOF exception found" )
1379 main.log.error( self.name + ": " + self.handle.before )
1380 main.cleanup()
1381 main.exit()
1382 except Exception:
1383 main.log.exception( self.name + ": Uncaught exception!" )
1384 main.cleanup()
1385 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001386
pingping-lin763ee042015-05-20 17:45:30 -07001387 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001388 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001389 Required:
kelvin8ec71442015-01-15 16:57:00 -08001390 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001391 * directory to store results
1392 Optional:
1393 * interface - default: eth0
pingping-lin763ee042015-05-20 17:45:30 -07001394 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001395 Description:
1396 Uses tshark command to grep specific group of packets
1397 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001398 The timestamp is hardcoded to be in epoch
1399 """
pingping-lin763ee042015-05-20 17:45:30 -07001400 try:
1401 self.handle.sendline( "" )
1402 self.handle.expect( "\$" )
1403 self.handle.sendline( "" )
1404 if grepOptions:
1405 grepStr = "grep "+str(grepOptions)
1406 else:
1407 grepStr = "grep"
1408
1409 self.handle.sendline(
1410 "tshark -i " +
1411 str( interface ) +
1412 " -t e | " +
1413 grepStr + " --line-buffered \"" +
1414 str(grep) +
1415 "\" >" +
1416 directory +
1417 " &" )
1418 self.handle.sendline( "\r" )
1419 self.handle.expect( "Capturing on" )
1420 self.handle.sendline( "\r" )
1421 self.handle.expect( "\$" )
1422 except pexpect.EOF:
1423 main.log.error( self.name + ": EOF exception found" )
1424 main.log.error( self.name + ": " + self.handle.before )
1425 main.cleanup()
1426 main.exit()
1427 except Exception:
1428 main.log.exception( self.name + ": Uncaught exception!" )
1429 main.cleanup()
1430 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001431
kelvin-onlabd3b64892015-01-20 13:26:24 -08001432 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001433 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001434 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001435 """
1436 # Remove all pcap from previous captures
pingping-lin763ee042015-05-20 17:45:30 -07001437 try:
1438 self.execute( cmd="sudo rm /tmp/wireshark*" )
1439 self.handle.sendline( "" )
1440 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1441 " | grep -v grep | awk '{print $2}'`" )
1442 self.handle.sendline( "" )
1443 main.log.info( "Tshark stopped" )
1444 except pexpect.EOF:
1445 main.log.error( self.name + ": EOF exception found" )
1446 main.log.error( self.name + ": " + self.handle.before )
1447 main.cleanup()
1448 main.exit()
1449 except Exception:
1450 main.log.exception( self.name + ": Uncaught exception!" )
1451 main.cleanup()
1452 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001453
kelvin8ec71442015-01-15 16:57:00 -08001454 def ptpd( self, args ):
1455 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001456 Initiate ptp with user-specified args.
1457 Required:
1458 * args: specify string of args after command
1459 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001460 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001461 try:
kelvin8ec71442015-01-15 16:57:00 -08001462 self.handle.sendline( "sudo ptpd " + str( args ) )
1463 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001464 "Multiple",
1465 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001466 "\$" ] )
1467 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001468
andrewonlab0c38a4a2014-10-28 18:35:35 -04001469 if i == 0:
1470 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001471 main.log.info( "ptpd returned an error: " +
1472 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001473 return handle
1474 elif i == 1:
1475 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001476 main.log.error( "ptpd returned an error: " +
1477 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001478 return handle
1479 else:
1480 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001481
andrewonlab0c38a4a2014-10-28 18:35:35 -04001482 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001483 main.log.error( self.name + ": EOF exception found" )
1484 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001485 main.cleanup()
1486 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001487 except Exception:
1488 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001489 main.cleanup()
1490 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001491
kelvin-onlabd3b64892015-01-20 13:26:24 -08001492 def cpLogsToDir( self, logToCopy,
pingping-lin763ee042015-05-20 17:45:30 -07001493 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001494 """
1495 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001496 Current implementation of ONOS deletes its karaf
1497 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001498 you may want to use this function to capture
1499 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001500 Localtime will be attached to the filename
1501
1502 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001503 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001504 copy.
kelvin8ec71442015-01-15 16:57:00 -08001505 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001506 For copying multiple files, leave copyFileName
1507 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001508 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001509 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001510 ex ) /tmp/
1511 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001512 * copyFileName: If you want to rename the log
1513 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001514 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001515 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001516 try:
kelvin8ec71442015-01-15 16:57:00 -08001517 localtime = time.strftime( '%x %X' )
1518 localtime = localtime.replace( "/", "" )
1519 localtime = localtime.replace( " ", "_" )
1520 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001521 if destDir[ -1: ] != "/":
1522 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001523
kelvin-onlabd3b64892015-01-20 13:26:24 -08001524 if copyFileName:
pingping-lin763ee042015-05-20 17:45:30 -07001525 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1526 str( destDir ) + str( copyFileName ) +
1527 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001528 self.handle.expect( "cp" )
1529 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001530 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001531 self.handle.sendline( "cp " + str( logToCopy ) +
1532 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001533 self.handle.expect( "cp" )
1534 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001535
kelvin8ec71442015-01-15 16:57:00 -08001536 return self.handle.before
1537
1538 except pexpect.EOF:
1539 main.log.error( "Copying files failed" )
1540 main.log.error( self.name + ": EOF exception found" )
1541 main.log.error( self.name + ": " + self.handle.before )
pingping-lin763ee042015-05-20 17:45:30 -07001542 except Exception:
1543 main.log.exception( "Copying files failed" )
kelvin8ec71442015-01-15 16:57:00 -08001544
Jon Hall16b72c42015-05-20 10:23:36 -07001545 def checkLogs( self, onosIp, restart=False):
kelvin8ec71442015-01-15 16:57:00 -08001546 """
Jon Hall94fd0472014-12-08 11:52:42 -08001547 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001548 If restart is True, use the old version of onos-check-logs which
1549 does not print the full stacktrace, but shows the entire log file,
1550 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001551 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001552 """
Jon Hall94fd0472014-12-08 11:52:42 -08001553 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001554 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001555 if restart:
1556 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001557 self.handle.sendline( cmd )
1558 self.handle.expect( cmd )
1559 self.handle.expect( "\$" )
Jon Hall94fd0472014-12-08 11:52:42 -08001560 response = self.handle.before
1561 return response
1562 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001563 main.log.error( "Lost ssh connection" )
1564 main.log.error( self.name + ": EOF exception found" )
1565 main.log.error( self.name + ": " + self.handle.before )
pingping-lin763ee042015-05-20 17:45:30 -07001566 except Exception:
1567 main.log.exception( self.name + ": Uncaught exception!" )
1568 main.cleanup()
1569 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001570
kelvin-onlabd3b64892015-01-20 13:26:24 -08001571 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001572 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001573 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001574 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001575 try:
kelvin8ec71442015-01-15 16:57:00 -08001576 self.handle.sendline( "" )
1577 self.handle.expect( "\$" )
1578 self.handle.sendline( "onos-service " + str( node ) +
1579 " status" )
1580 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001581 "start/running",
1582 "stop/waiting",
kelvin8ec71442015-01-15 16:57:00 -08001583 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001584
1585 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001586 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001587 return main.TRUE
1588 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001589 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001590 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001591 main.cleanup()
1592 main.exit()
1593 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001594 main.log.error( self.name + ": EOF exception found" )
1595 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001596 main.cleanup()
1597 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001598 except Exception:
1599 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001600 main.cleanup()
1601 main.exit()
pingping-lin763ee042015-05-20 17:45:30 -07001602
1603 def setIpTables( self, ip, port='', action='add', packet_type='',
1604 direction='INPUT', rule='DROP', states=True ):
1605 """
1606 Description:
1607 add or remove iptables rule to DROP (default) packets from
1608 specific IP and PORT
1609 Usage:
1610 * specify action ('add' or 'remove')
1611 when removing, pass in the same argument as you would add. It will
1612 delete that specific rule.
1613 * specify the ip to block
1614 * specify the destination port to block (defaults to all ports)
1615 * optional packet type to block (default tcp)
1616 * optional iptables rule (default DROP)
1617 * optional direction to block (default 'INPUT')
1618 * States boolean toggles adding all supported tcp states to the
1619 firewall rule
1620 Returns:
1621 main.TRUE on success or
1622 main.FALSE if given invalid input or
1623 main.ERROR if there is an error in response from iptables
1624 WARNING:
1625 * This function uses root privilege iptables command which may result
1626 in unwanted network errors. USE WITH CAUTION
1627 """
1628 import time
1629
1630 # NOTE*********
1631 # The strict checking methods of this driver function is intentional
1632 # to discourage any misuse or error of iptables, which can cause
1633 # severe network errors
1634 # *************
1635
1636 # NOTE: Sleep needed to give some time for rule to be added and
1637 # registered to the instance. If you are calling this function
1638 # multiple times this sleep will prevent any errors.
1639 # DO NOT REMOVE
1640 # time.sleep( 5 )
1641 try:
1642 # input validation
1643 action_type = action.lower()
1644 rule = rule.upper()
1645 direction = direction.upper()
1646 if action_type != 'add' and action_type != 'remove':
1647 main.log.error( "Invalid action type. Use 'add' or "
1648 "'remove' table rule" )
1649 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1650 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1651 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1652 "'ACCEPT' or 'LOG' only." )
1653 if direction != 'INPUT' and direction != 'OUTPUT':
1654 # NOTE currently only supports rules INPUT and OUPTUT
1655 main.log.error( "Invalid rule. Valid directions are"
1656 " 'OUTPUT' or 'INPUT'" )
1657 return main.FALSE
1658 return main.FALSE
1659 return main.FALSE
1660 if action_type == 'add':
1661 # -A is the 'append' action of iptables
1662 actionFlag = '-A'
1663 elif action_type == 'remove':
1664 # -D is the 'delete' rule of iptables
1665 actionFlag = '-D'
1666 self.handle.sendline( "" )
1667 self.handle.expect( "\$" )
1668 cmd = "sudo iptables " + actionFlag + " " +\
1669 direction +\
1670 " -s " + str( ip )
1671 # " -p " + str( packet_type ) +\
1672 if packet_type:
1673 cmd += " -p " + str( packet_type )
1674 if port:
1675 cmd += " --dport " + str( port )
1676 if states:
1677 cmd += " -m state --state="
1678 #FIXME- Allow user to configure which states to block
1679 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
1680 cmd += " -j " + str( rule )
1681
1682 self.handle.sendline( cmd )
1683 self.handle.expect( "\$" )
1684 main.log.warn( self.handle.before )
1685
1686 info_string = "On " + str( self.name )
1687 info_string += " " + str( action_type )
1688 info_string += " iptable rule [ "
1689 info_string += " IP: " + str( ip )
1690 info_string += " Port: " + str( port )
1691 info_string += " Rule: " + str( rule )
1692 info_string += " Direction: " + str( direction ) + " ]"
1693 main.log.info( info_string )
1694 return main.TRUE
1695 except pexpect.TIMEOUT:
1696 main.log.exception( self.name + ": Timeout exception in "
1697 "setIpTables function" )
1698 return main.ERROR
1699 except pexpect.EOF:
1700 main.log.error( self.name + ": EOF exception found" )
1701 main.log.error( self.name + ": " + self.handle.before )
1702 main.cleanup()
1703 main.exit()
1704 except Exception:
1705 main.log.exception( self.name + ": Uncaught exception!" )
1706 main.cleanup()
1707 main.exit()
1708
1709 def detailed_status(self, log_filename):
1710 """
1711 This method is used by STS to check the status of the controller
1712 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
1713 """
1714 import re
1715 try:
1716 self.handle.sendline( "" )
1717 self.handle.expect( "\$" )
1718 self.handle.sendline( "cd " + self.home )
1719 self.handle.expect( "\$" )
1720 self.handle.sendline( "service onos status" )
1721 self.handle.expect( "\$" )
1722 response = self.handle.before
1723 if re.search( "onos start/running", response ):
1724 # onos start/running, process 10457
1725 return 'RUNNING'
1726 # FIXME: Implement this case
1727 # elif re.search( pattern, response ):
1728 # return 'STARTING'
1729 elif re.search( "onos stop/", response ):
1730 # onos stop/waiting
1731 # FIXME handle this differently?: onos stop/pre-stop
1732 return 'STOPPED'
1733 # FIXME: Implement this case
1734 # elif re.search( pattern, response ):
1735 # return 'FROZEN'
1736 else:
1737 main.log.warn( self.name +
1738 " WARNING: status received unknown response" )
1739 main.log.warn( response )
1740 return 'ERROR', "Unknown response: %s" % response
1741 except pexpect.TIMEOUT:
1742 main.log.exception( self.name + ": Timeout exception in "
1743 "setIpTables function" )
1744 return 'ERROR', "Pexpect Timeout"
1745 except pexpect.EOF:
1746 main.log.error( self.name + ": EOF exception found" )
1747 main.log.error( self.name + ": " + self.handle.before )
1748 main.cleanup()
1749 main.exit()
1750 except Exception:
1751 main.log.exception( self.name + ": Uncaught exception!" )
1752 main.cleanup()
1753 main.exit()
1754
1755 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1756 '''
1757 Create/formats the LinkGraph.cfg file based on arguments
1758 -only creates a linear topology and connects islands
1759 -evenly distributes devices
1760 -must be called by ONOSbench
1761
1762 ONOSIpList - list of all of the node IPs to be used
1763
1764 deviceCount - number of switches to be assigned
1765 '''
1766 main.log.step("Creating link graph configuration file." )
1767 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
1768 tempFile = "/tmp/linkGraph.cfg"
1769
1770 linkGraph = open(tempFile, 'w+')
1771 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1772 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1773 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
1774
1775 clusterCount = len(ONOSIpList)
1776
1777 if type(deviceCount) is int or type(deviceCount) is str:
1778 deviceCount = int(deviceCount)
1779 switchList = [0]*(clusterCount+1)
1780 baselineSwitchCount = deviceCount/clusterCount
1781
1782 for node in range(1, clusterCount + 1):
1783 switchList[node] = baselineSwitchCount
1784
1785 for node in range(1, (deviceCount%clusterCount)+1):
1786 switchList[node] += 1
1787
1788 if type(deviceCount) is list:
1789 main.log.info("Using provided device distribution")
1790 switchList = [0]
1791 for i in deviceCount:
1792 switchList.append(int(i))
1793
1794 tempList = ['0']
1795 tempList.extend(ONOSIpList)
1796 ONOSIpList = tempList
1797
1798 myPort = 6
1799 lastSwitch = 0
1800 for node in range(1, clusterCount+1):
1801 if switchList[node] == 0:
1802 continue
1803
1804 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
1805
1806 if node > 1:
1807 #connect to last device on previous node
1808 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
1809 linkGraph.write(line)
1810
1811 lastSwitch = 0
1812 for switch in range (0, switchList[node]-1):
1813 line = ""
1814 line = ("\t" + str(switch) + ":" + str(myPort))
1815 line += " -- "
1816 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
1817 linkGraph.write(line)
1818 lastSwitch = switch+1
1819 lastIp = ONOSIpList[node]
1820
1821 #lastSwitch += 1
1822 if node < (clusterCount):
1823 #connect to first device on the next node
1824 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
1825 linkGraph.write(line)
1826
1827 linkGraph.write("}\n")
1828 linkGraph.close()
1829
1830 #SCP
1831 os.system( "scp " + tempFile + " admin@" + benchIp + ":" + linkGraphPath)
1832 main.log.info("linkGraph.cfg creation complete")
1833
1834 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
1835
1836 '''
1837 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
1838 deviceCount = number of switches to distribute, or list of values to use as custom distribution
1839 numPorts = number of ports per device. Defaults to 10 both in this function and in ONOS. Optional arg
1840 '''
1841
1842 main.log.step("Configuring Null Device Provider" )
1843 clusterCount = len(ONOSIpList)
1844
1845 try:
1846
1847 if type(deviceCount) is int or type(deviceCount) is str:
1848 main.log.step("Creating device distribution")
1849 deviceCount = int(deviceCount)
1850 switchList = [0]*(clusterCount+1)
1851 baselineSwitchCount = deviceCount/clusterCount
1852
1853 for node in range(1, clusterCount + 1):
1854 switchList[node] = baselineSwitchCount
1855
1856 for node in range(1, (deviceCount%clusterCount)+1):
1857 switchList[node] += 1
1858
1859 if type(deviceCount) is list:
1860 main.log.info("Using provided device distribution")
1861
1862 if len(deviceCount) == clusterCount:
1863 switchList = ['0']
1864 switchList.extend(deviceCount)
1865
1866 if len(deviceCount) == (clusterCount + 1):
1867 if deviceCount[0] == '0' or deviceCount[0] == 0:
1868 switchList = deviceCount
1869
1870 assert len(switchList) == (clusterCount + 1)
1871
1872 except AssertionError:
1873 main.log.error( "Bad device/Ip list match")
1874 except TypeError:
1875 main.log.exception( self.name + ": Object not as expected" )
1876 return None
1877 except Exception:
1878 main.log.exception( self.name + ": Uncaught exception!" )
1879 main.cleanup()
1880 main.exit()
1881
1882
1883 ONOSIp = [0]
1884 ONOSIp.extend(ONOSIpList)
1885
1886 devicesString = "devConfigs = "
1887 for node in range(1, len(ONOSIp)):
1888 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
1889 if node < clusterCount:
1890 devicesString += (",")
1891
1892 try:
1893 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
1894 self.handle.expect(":~")
1895 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
1896 self.handle.expect(":~")
1897
1898 for i in range(10):
1899 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
1900 self.handle.expect(":~")
1901 verification = self.handle.before
1902 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
1903 break
1904 else:
1905 time.sleep(1)
1906
1907 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
1908
1909 except AssertionError:
1910 main.log.error("Incorrect Config settings: " + verification)
1911 except Exception:
1912 main.log.exception( self.name + ": Uncaught exception!" )
1913 main.cleanup()
1914 main.exit()
1915
1916 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
1917 '''
1918 fileName default is currently the same as the default on ONOS, specify alternate file if
1919 you want to use a different topology file than linkGraph.cfg
1920 '''
1921
1922
1923 try:
1924 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
1925 self.handle.expect(":~")
1926 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
1927 self.handle.expect(":~")
1928
1929 for i in range(10):
1930 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
1931 self.handle.expect(":~")
1932 verification = self.handle.before
1933 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
1934 break
1935 else:
1936 time.sleep(1)
1937
1938 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
1939
1940 except pexpect.EOF:
1941 main.log.error( self.name + ": EOF exception found" )
1942 main.log.error( self.name + ": " + self.handle.before )
1943 main.cleanup()
1944 main.exit()
1945 except AssertionError:
1946 main.log.info("Settings did not post to ONOS")
1947 main.log.error(varification)
1948 except Exception:
1949 main.log.exception( self.name + ": Uncaught exception!" )
1950 main.log.error(varification)
1951 main.cleanup()
1952 main.exit()
1953
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001954 def getOnosIps(self):
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001955
1956 import os
kelvin-onlab0a28a742015-05-18 16:03:13 -07001957
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001958 # reads env for OC variables, also saves file with OC variables. If file and env conflict
1959 # priority goes to env. If file has OCs that are not in the env, the file OCs are used.
1960 # In other words, if the env is set, the test will use those values.
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001961
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001962 # returns a list of ip addresses for the onos nodes, will work with up to 7 nodes + OCN and OCI
1963 # returns in format [ OC1 ip, OC2 ...ect. , OCN, OCI ]
1964
1965 envONOSIps = {}
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001966
1967 x = 1
1968 while True:
1969 try:
1970 temp = os.environ[ 'OC' + str(x) ]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001971 except KeyError:
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001972 break
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001973 envONOSIps[ ("OC" + str(x)) ] = temp
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001974 x += 1
1975
1976 try:
1977 temp = os.environ[ 'OCN' ]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001978 envONOSIps[ "OCN" ] = temp
1979 except KeyError:
1980 main.log.info("OCN not set in env")
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001981
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001982 try:
1983 temp = os.environ[ 'OCI' ]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001984 envONOSIps[ "OCI" ] = temp
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001985 except:
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001986 main.log.error("OCI not set in env")
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001987
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001988 print(str(envONOSIps))
1989
1990 order = [ "OC1", "OC2", "OC3","OC4","OC5","OC6","OC7","OCN","OCI" ]
1991 ONOSIps = []
1992
1993 try:
cameron@onlab.us2e166212015-05-19 14:28:25 -07001994 if os.path.exists("myIps"):
1995 ipFile = open("myIps","r+")
1996 else:
1997 ipFile = open("myIps","w+")
1998
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001999 fileONOSIps = ipFile.readlines()
2000 ipFile.close()
2001
2002 print str(fileONOSIps)
2003
2004 if str(fileONOSIps) == "[]":
2005 ipFile = open("myIps","w+")
2006 for key in envONOSIps:
2007 ipFile.write(key+ "=" + envONOSIps[key] + "\n")
2008 ipFile.close()
2009 for i in order:
2010 if i in envONOSIps:
2011 ONOSIps.append(envONOSIps[i])
2012
2013 return ONOSIps
2014
2015 else:
2016 fileDict = {}
2017 for line in fileONOSIps:
2018 line = line.replace("\n","")
2019 line = line.split("=")
2020 key = line[0]
2021 value = line[1]
2022 fileDict[key] = value
2023
2024 for x in envONOSIps:
2025 if x in fileDict:
2026 if envONOSIps[x] == fileDict[x]:
2027 continue
2028 else:
2029 fileDict[x] = envONOSIps[x]
2030 else:
2031 fileDict[x] = envONOSIps[x]
2032
2033 ipFile = open("myIps","w+")
2034 for key in order:
2035 if key in fileDict:
2036 ipFile.write(key + "=" + fileDict[key] + "\n")
2037 ONOSIps.append(fileDict[key])
2038 ipFile.close()
2039
2040 return ONOSIps
2041
2042 except IOError as a:
2043 main.log.error(a)
2044
kelvin-onlab0a28a742015-05-18 16:03:13 -07002045 except Exception as a:
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002046 main.log.error(a)
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002047
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002048
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002049 def logReport(self, nodeIp, searchTerms, outputMode="s"):
2050 '''
2051 - accepts either a list or a string for "searchTerms" these
2052 terms will be searched for in the log and have their
2053 instances counted
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002054
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002055 - nodeIp is the ip of the node whos log is to be scanned
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002056
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002057 - output modes:
2058 "s" - Simple. Quiet output mode that just prints
cameron@onlab.us2e166212015-05-19 14:28:25 -07002059 the occurences of each search term
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002060
cameron@onlab.us2e166212015-05-19 14:28:25 -07002061 "d" - Detailed. Prints number of occurences as well as the entire
2062 line for each of the last 5 occurences
2063
2064 - returns total of the number of instances of all search terms
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002065 '''
2066 main.log.info("========================== Log Report ===========================\n")
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002067
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002068 if type(searchTerms) is str:
2069 searchTerms = [searchTerms]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002070
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002071 logLines = [ [" "] for i in range(len(searchTerms)) ]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002072
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002073 for term in range(len(searchTerms)):
2074 logLines[term][0] = searchTerms[term]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002075
cameron@onlab.us2e166212015-05-19 14:28:25 -07002076 totalHits = 0
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002077 for term in range(len(searchTerms)):
2078 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep " + searchTerms[term]
2079 self.handle.sendline(cmd)
2080 self.handle.expect(":~")
2081 before = (self.handle.before).splitlines()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002082
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002083 count = [searchTerms[term],0]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002084
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002085 for line in before:
2086 if searchTerms[term] in line and "grep" not in line:
2087 count[1] += 1
2088 if before.index(line) > ( len(before) - 7 ):
2089 logLines[term].append(line)
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002090
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002091 main.log.info( str(count[0]) + ": " + str(count[1]) )
2092 if term == len(searchTerms)-1:
2093 print("\n")
cameron@onlab.us2e166212015-05-19 14:28:25 -07002094 totalHits += int(count[1])
2095
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002096 if outputMode != "s" and outputMode != "S":
2097 outputString = ""
2098 for i in logLines:
2099 outputString = i[0] + ": \n"
2100 for x in range(1,len(i)):
2101 outputString += ( i[x] + "\n" )
2102
2103 if outputString != (i[0] + ": \n"):
2104 main.log.info(outputString)
2105
2106 main.log.info("================================================================\n")
cameron@onlab.us2e166212015-05-19 14:28:25 -07002107 return totalHits
pingping-lin763ee042015-05-20 17:45:30 -07002108
2109 def getOnosIpFromEnv(self):
2110
2111 import string
2112
2113 # returns a list of ip addresses for the onos nodes, will work with up to 7 nodes + OCN and OCI
2114 # returns in format [ 'x', OC1 ip, OC2 i... ect. ... , ONN ip ]
2115
2116 self.handle.sendline("env| grep OC")
2117 self.handle.expect(":~")
2118 rawOutput = self.handle.before
2119 print rawOutput
2120 print "-----------------------------"
2121 print repr(rawOutput)
2122 mpa = dict.fromkeys(range(32))
2123 translated = rawOutput.translate(mpa)
2124 print translated
2125
2126
2127 # create list with only the lines that have the needed IPs
2128 unparsedIps = []
2129
2130 # remove preceeding or trailing lines
2131 for line in rawOutput:
2132 if "OC" in line and "=" in line:
2133 unparsedIps.append(str(line))
2134
2135 # determine cluster size
2136 clusterCount = 0
2137 for line in unparsedIps:
2138 line = str(line)
2139 print line
2140 temp = line.replace("OC","")
2141 print("line index " + str(line.index("=")))
2142 OCindex = temp[0]
2143 for i in range(0, 7):
2144 if OCindex == str(i) and i > clusterCount:
2145 clusterCount == i
2146 print(clusterCount)
2147 # create list to hold ips such that OC1 is at list[1] and OCN and OCI are at the end (in that order)
2148 ONOSIps = ["x"] * (clusterCount + 3)
2149
2150 # populate list
2151 for line in unparsedIps:
2152 main.log.info(line)##########
2153 temp = str(line.replace("OC",""))
2154 main.log.info(str(list(temp)))
2155 OCindex = temp[0]
2156 main.log.info(OCindex)############
2157 if OCindex == "N":
2158 ONOSIps[ clusterCount + 1 ] = temp.replace("N=","")
2159
2160 if OCindex == "I":
2161 ONOSIps[ clusterCount + 2 ] = temp.replace("I=","")
2162
2163 else:
2164 ONOSIps[ int(OCindex) ] = temp.replace((OCindex + "=") ,"")
2165
2166 # validate
2167 for x in ONOSIps:
2168 if ONOSIps.index(x) != 0 and x == "x":
2169 main.log.error("ENV READ FAILURE, MISSING DATA: \n\n" + str(ONOSIps) + "\n\n")
2170
2171 return ONOSIps
2172
kelvin-onlab0a28a742015-05-18 16:03:13 -07002173
pingping-lin763ee042015-05-20 17:45:30 -07002174 def onosErrorLog(self, nodeIp):
2175
2176 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep WARN"
2177 self.handle.sendline(cmd)
2178 self.handle.expect(":~")
2179 before = (self.handle.before).splitlines()
kelvin-onlab0a28a742015-05-18 16:03:13 -07002180
pingping-lin763ee042015-05-20 17:45:30 -07002181 warnings = []
2182
2183 for line in before:
2184 if "WARN" in line and "grep" not in line:
2185 warnings.append(line)
2186 main.warnings[main.warnings[0]+1] = line
2187 main.warnings[0] += 1
2188 if main.warnings[0] >= 10:
2189 main.warnings[0] = 0
2190
2191 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep ERROR"
2192 self.handle.sendline(cmd)
2193 self.handle.expect(":~")
2194 before = (self.handle.before).splitlines()
2195
2196 errors = []
2197
2198 for line in before:
2199 if "ERROR" in line and "grep" not in line:
2200 errors.append(line)
2201 main.errors[main.errors[0]+1] = line
2202 main.errors[0] += 1
2203 if main.errors[0] >= 10:
2204 main.errors[0] = 0
2205
2206 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep Exept"
2207 self.handle.sendline(cmd)
2208 self.handle.expect(":~")
2209 before = (self.handle.before).splitlines()
2210
2211 exceptions = []
2212
2213 for line in before:
2214 if "Except" in line and "grep" not in line:
2215 exceptions.append(line)
2216 main.exceptions[main.errors[0]+1] = line
2217 main.exceptions[0] += 1
2218 if main.exceptions[0] >= 10:
2219 main.exceptions[0] = 0
2220
2221
2222
2223 ################################################################
2224
2225 msg1 = "WARNINGS: \n"
2226 for i in main.warnings:
2227 if type(i) is not int:
2228 msg1 += ( i + "\n")
2229
2230 msg2 = "ERRORS: \n"
2231 for i in main.errors:
2232 if type(i) is not int:
2233 msg2 += ( i + "\n")
2234
2235 msg3 = "EXCEPTIONS: \n"
2236 for i in main.exceptions:
2237 if type(i) is not int:
2238 msg3 += ( i + "\n")
2239
2240 main.log.info("===============================================================\n")
2241 main.log.info( "Warnings: " + str(len(warnings)))
2242 main.log.info( "Errors: " + str(len(errors)))
2243 main.log.info( "Exceptions: " + str(len(exceptions)))
2244 if len(warnings) > 0:
2245 main.log.info(msg1)
2246 if len(errors) > 0:
2247 main.log.info(msg2)
2248 if len(exceptions) > 0:
2249 main.log.info(msg3)
2250 main.log.info("===============================================================\n")