blob: eafe00f03629a31249e63e7ab4b3b519090e2977 [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
kelvin8ec71442015-01-15 16:57:00 -080023sys.path.append( "../" )
Jon Hall05b2b432014-10-08 19:53:25 -040024from drivers.common.clidriver import CLI
25
Jon Hall05b2b432014-10-08 19:53:25 -040026
kelvin8ec71442015-01-15 16:57:00 -080027class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040028
kelvin8ec71442015-01-15 16:57:00 -080029 def __init__( self ):
30 """
31 Initialize client
32 """
Jon Hallefbd9792015-03-05 16:11:36 -080033 self.name = None
34 self.home = None
35 self.handle = None
kelvin8ec71442015-01-15 16:57:00 -080036 super( CLI, self ).__init__()
37
38 def connect( self, **connectargs ):
39 """
Jon Hall05b2b432014-10-08 19:53:25 -040040 Creates ssh handle for ONOS "bench".
kelvin8ec71442015-01-15 16:57:00 -080041 """
Jon Hall05b2b432014-10-08 19:53:25 -040042 try:
43 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080044 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us3b087132015-03-11 15:00:08 -070045 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040046 for key in self.options:
47 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080048 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040049 break
Jon Hall274b6642015-02-17 11:57:17 -080050 if self.home is None or self.home == "":
Jon Halle94919c2015-03-23 11:42:57 -070051 self.home = "~/onos"
Jon Hall21270ac2015-02-16 17:59:55 -080052
kelvin8ec71442015-01-15 16:57:00 -080053 self.name = self.options[ 'name' ]
54 self.handle = super( OnosDriver, self ).connect(
55 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -080056 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -080057 port=self.port,
58 pwd=self.pwd,
59 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -040060
kelvin8ec71442015-01-15 16:57:00 -080061 self.handle.sendline( "cd " + self.home )
62 self.handle.expect( "\$" )
Jon Hall05b2b432014-10-08 19:53:25 -040063 if self.handle:
64 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080065 else:
66 main.log.info( "NO ONOS HANDLE" )
Jon Hall05b2b432014-10-08 19:53:25 -040067 return main.FALSE
68 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080069 main.log.error( self.name + ": EOF exception found" )
70 main.log.error( self.name + ": " + self.handle.before )
Jon Hall05b2b432014-10-08 19:53:25 -040071 main.cleanup()
72 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080073 except Exception:
74 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall05b2b432014-10-08 19:53:25 -040075 main.cleanup()
76 main.exit()
77
kelvin8ec71442015-01-15 16:57:00 -080078 def disconnect( self ):
79 """
Jon Hall05b2b432014-10-08 19:53:25 -040080 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080081 """
Jon Halld61331b2015-02-17 16:35:47 -080082 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -040083 try:
Jon Hall61282e32015-03-19 11:34:11 -070084 if self.handle:
85 self.handle.sendline( "" )
86 self.handle.expect( "\$" )
87 self.handle.sendline( "exit" )
88 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -040089 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080090 main.log.error( self.name + ": EOF exception found" )
91 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -070092 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -070093 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -070094 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -080095 except Exception:
96 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -040097 response = main.FALSE
98 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040099
kelvin-onlabd3b64892015-01-20 13:26:24 -0800100 def onosPackage( self ):
kelvin8ec71442015-01-15 16:57:00 -0800101 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400102 Produce a self-contained tar.gz file that can be deployed
kelvin8ec71442015-01-15 16:57:00 -0800103 and executed on any platform with Java 7 JRE.
104 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400105 try:
kelvin8ec71442015-01-15 16:57:00 -0800106 self.handle.sendline( "onos-package" )
107 self.handle.expect( "onos-package" )
108 self.handle.expect( "tar.gz", timeout=30 )
109 handle = str( self.handle.before )
110 main.log.info( "onos-package command returned: " +
111 handle )
112 # As long as the sendline does not time out,
113 # return true. However, be careful to interpret
114 # the results of the onos-package command return
andrewonlab0748d2a2014-10-09 13:24:17 -0400115 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400116
andrewonlab7735d852014-10-09 13:02:47 -0400117 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800118 main.log.error( self.name + ": EOF exception found" )
119 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800120 except Exception:
121 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400122 main.cleanup()
123 main.exit()
124
kelvin-onlabd3b64892015-01-20 13:26:24 -0800125 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800126 """
andrewonlab8790abb2014-11-06 13:51:54 -0500127 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800128 """
andrewonlab8790abb2014-11-06 13:51:54 -0500129 try:
kelvin8ec71442015-01-15 16:57:00 -0800130 self.handle.sendline( "onos-build" )
131 self.handle.expect( "onos-build" )
132 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800133 "BUILD SUCCESS",
134 "ERROR",
135 "BUILD FAILED" ],
136 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800137 handle = str( self.handle.before )
andrewonlab8790abb2014-11-06 13:51:54 -0500138
kelvin8ec71442015-01-15 16:57:00 -0800139 main.log.info( "onos-build command returned: " +
140 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500141
142 if i == 0:
143 return main.TRUE
144 else:
145 return handle
146
147 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800148 main.log.error( self.name + ": EOF exception found" )
149 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800150 except Exception:
151 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500152 main.cleanup()
153 main.exit()
154
kelvin-onlabd3b64892015-01-20 13:26:24 -0800155 def cleanInstall( self ):
kelvin8ec71442015-01-15 16:57:00 -0800156 """
157 Runs mvn clean install in the root of the ONOS directory.
158 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400159
kelvin8ec71442015-01-15 16:57:00 -0800160 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400161 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800162 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400163 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800164 main.log.info( "Running 'mvn clean install' on " +
165 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800166 ". This may take some time." )
167 self.handle.sendline( "cd " + self.home )
168 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400169
kelvin8ec71442015-01-15 16:57:00 -0800170 self.handle.sendline( "" )
171 self.handle.expect( "\$" )
172 self.handle.sendline( "mvn clean install" )
173 self.handle.expect( "mvn clean install" )
174 while True:
175 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800176 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800177 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400178 'BUILD\sFAILURE',
179 'BUILD\sSUCCESS',
Jon Halle94919c2015-03-23 11:42:57 -0700180 'onos\$', #TODO: fix this to be more generic?
Jon Hallde9d9aa2014-10-08 20:36:02 -0400181 'ONOS\$',
kelvin8ec71442015-01-15 16:57:00 -0800182 pexpect.TIMEOUT ], timeout=600 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400183 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800184 main.log.error( self.name + ":There is insufficient memory \
185 for the Java Runtime Environment to continue." )
186 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400187 main.cleanup()
188 main.exit()
189 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800190 main.log.error( self.name + ": Build failure!" )
191 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400192 main.cleanup()
193 main.exit()
194 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800195 main.log.info( self.name + ": Build success!" )
Jon Halle94919c2015-03-23 11:42:57 -0700196 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800197 main.log.info( self.name + ": Build complete" )
198 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400199 for line in self.handle.before.splitlines():
200 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800201 main.log.info( line )
202 self.handle.sendline( "" )
203 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400204 return main.TRUE
Jon Halle94919c2015-03-23 11:42:57 -0700205 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800206 main.log.error(
207 self.name +
208 ": mvn clean install TIMEOUT!" )
209 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400210 main.cleanup()
211 main.exit()
212 else:
Jon Hall274b6642015-02-17 11:57:17 -0800213 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800214 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800215 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400216 main.cleanup()
217 main.exit()
218 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800219 main.log.error( self.name + ": EOF exception found" )
220 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400221 main.cleanup()
222 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800223 except Exception:
224 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400225 main.cleanup()
226 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400227
Jon Hall61282e32015-03-19 11:34:11 -0700228 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800229 """
Jon Hallacabffd2014-10-09 12:36:53 -0400230 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800231
Jon Hall61282e32015-03-19 11:34:11 -0700232 If the fastForward boolean is set to true, only git pulls that can
233 be fast forwarded will be performed. IE if you have not local commits
234 in your branch.
235
Jon Hallacabffd2014-10-09 12:36:53 -0400236 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800237 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400238 for the purpose of pulling from other nodes if necessary.
239
Jon Hall47a93fb2015-01-06 16:46:06 -0800240 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400241 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800242 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400243 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400244
kelvin8ec71442015-01-15 16:57:00 -0800245 """
Jon Hallacabffd2014-10-09 12:36:53 -0400246 try:
kelvin8ec71442015-01-15 16:57:00 -0800247 # main.log.info( self.name + ": Stopping ONOS" )
248 # self.stop()
249 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800250 self.handle.expect( self.home + "\$" )
Jon Hall61282e32015-03-19 11:34:11 -0700251 cmd = "git pull"
252 if comp1 != "":
253 cmd += ' ' + comp1
254 if fastForward:
255 cmd += ' ' + " --ff-only"
256 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800257 i = self.handle.expect(
258 [
259 'fatal',
260 'Username\sfor\s(.*):\s',
261 '\sfile(s*) changed,\s',
262 'Already up-to-date',
263 'Aborting',
264 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800265 'You asked me to pull without telling me which branch you',
266 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700267 'Please enter a commit message to explain why this merge',
268 'Found a swap file by the name',
269 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800270 pexpect.TIMEOUT ],
271 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800272 # debug
kelvin-onlabd3b64892015-01-20 13:26:24 -0800273 # main.log.report( self.name +": DEBUG: \n"+
274 # "git pull response: " +
275 # str( self.handle.before ) + str( self.handle.after ) )
kelvin8ec71442015-01-15 16:57:00 -0800276 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700277 main.log.error( self.name + ": Git pull had some issue" )
278 output = self.handle.after
279 self.handle.expect( '\$' )
280 output += self.handle.before
281 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400282 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800283 elif i == 1:
284 main.log.error(
285 self.name +
286 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400287 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800288 elif i == 2:
289 main.log.info(
290 self.name +
291 ": Git Pull - pulling repository now" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800292 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800293 # So that only when git pull is done, we do mvn clean compile
294 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800295 elif i == 3:
296 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800297 return i
kelvin8ec71442015-01-15 16:57:00 -0800298 elif i == 4:
299 main.log.info(
300 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800301 ": Git Pull - Aborting..." +
302 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400303 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800304 elif i == 5:
305 main.log.info(
306 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800307 ": Git Pull - You are not currently " +
308 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400309 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800310 elif i == 6:
311 main.log.info(
312 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800313 ": Git Pull - You have not configured an upstream " +
314 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400315 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800316 elif i == 7:
317 main.log.info(
318 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800319 ": Git Pull - Pull is not possible because " +
320 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400321 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800322 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700323 # NOTE: abandoning test since we can't reliably handle this
324 # there could be different default text editors and we
325 # also don't know if we actually want to make the commit
326 main.log.error( "Git pull resulted in a merge commit message" +
327 ". Exiting test!" )
328 main.cleanup()
329 main.exit()
330 elif i == 9: # Merge commit message but swap file exists
331 main.log.error( "Git pull resulted in a merge commit message" +
332 " but a swap file exists." )
333 try:
334 self.handle.send( 'A' ) # Abort
335 self.handle.expect( "\$" )
336 return main.ERROR
337 except Exception:
338 main.log.exception( "Couldn't exit editor prompt!")
339 main.cleanup()
340 main.exit()
341 elif i == 10: # In the middle of a merge commit
342 main.log.error( "Git branch is in the middle of a merge. " )
343 main.log.warn( self.handle.before + self.handle.after )
344 return main.ERROR
345 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800346 main.log.error( self.name + ": Git Pull - TIMEOUT" )
347 main.log.error(
348 self.name + " Response was: " + str(
349 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400350 return main.ERROR
351 else:
kelvin8ec71442015-01-15 16:57:00 -0800352 main.log.error(
353 self.name +
354 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400355 return main.ERROR
356 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800357 main.log.error( self.name + ": EOF exception found" )
358 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400359 main.cleanup()
360 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800361 except Exception:
362 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400363 main.cleanup()
364 main.exit()
365
kelvin-onlabd3b64892015-01-20 13:26:24 -0800366 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800367 """
Jon Hallacabffd2014-10-09 12:36:53 -0400368 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800369
Jon Hallacabffd2014-10-09 12:36:53 -0400370 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800371 If used as gitCheckout( "branch" ) it will do git checkout
372 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400373
374 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800375 branch of the ONOS repository. If it has any problems, it will return
376 main.ERROR.
377 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400378 successful then the function will return main.TRUE.
379
kelvin8ec71442015-01-15 16:57:00 -0800380 """
Jon Hallacabffd2014-10-09 12:36:53 -0400381 try:
kelvin8ec71442015-01-15 16:57:00 -0800382 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800383 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800384 main.log.info( self.name +
385 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800386 cmd = "git checkout " + branch
387 self.handle.sendline( cmd )
388 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800389 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800390 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700391 'Username for (.*): ',
392 'Already on \'',
393 'Switched to branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800394 pexpect.TIMEOUT,
395 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800396 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800397 'error: you need to resolve your current index first',
398 "You are in 'detached HEAD' state.",
399 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800400 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800401 if i == 0:
402 main.log.error(
403 self.name +
404 ": Git checkout had some issue..." )
405 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400406 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800407 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800408 main.log.error(
409 self.name +
410 ": Git checkout asking for username." +
411 " Please configure your local git repository to be able " +
412 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800413 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400414 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800415 elif i == 2:
416 main.log.info(
417 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800418 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800419 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800420 # main.log.info( "DEBUG: after checkout cmd = "+
421 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400422 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800423 elif i == 3:
424 main.log.info(
425 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800426 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800427 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800428 # main.log.info( "DEBUG: after checkout cmd = "+
429 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400430 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800431 elif i == 4:
432 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
433 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800434 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400435 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800436 elif i == 5:
437 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800438 main.log.error(
439 self.name +
440 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800441 "Your local changes to the following files would" +
442 " be overwritten by checkout:" +
443 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800444 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500445 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800446 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800447 main.log.error(
448 self.name +
449 ": Git checkout error: \n" +
450 "You need to resolve your current index first:" +
451 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800452 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500453 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800454 elif i == 7:
455 main.log.info(
456 self.name +
457 ": Git checkout " + str( branch ) +
458 " - You are in 'detached HEAD' state. HEAD is now at " +
459 str( branch ) )
460 self.handle.expect( self.home + "\$" )
461 return main.TRUE
462 elif i == 8: # Already in detached HEAD on the specified commit
463 main.log.info(
464 self.name +
465 ": Git Checkout %s : Already on commit" % branch )
466 self.handle.expect( self.home + "\$" )
467 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400468 else:
kelvin8ec71442015-01-15 16:57:00 -0800469 main.log.error(
470 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800471 ": Git Checkout - Unexpected response, " +
472 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800473 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400474 return main.ERROR
475
476 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800477 main.log.error( self.name + ": EOF exception found" )
478 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400479 main.cleanup()
480 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800481 except Exception:
482 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400483 main.cleanup()
484 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400485
kelvin-onlabd3b64892015-01-20 13:26:24 -0800486 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800487 """
Jon Hall274b6642015-02-17 11:57:17 -0800488 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800489 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800490 """
Jon Hall45ec0922014-10-10 19:33:49 -0400491 try:
kelvin8ec71442015-01-15 16:57:00 -0800492 self.handle.sendline( "" )
493 self.handle.expect( "\$" )
494 self.handle.sendline(
495 "cd " +
496 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800497 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
498 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800499 # NOTE: for some reason there are backspaces inserted in this
500 # phrase when run from Jenkins on some tests
501 self.handle.expect( "never" )
502 self.handle.expect( "\$" )
503 response = ( self.name + ": \n" + str(
504 self.handle.before + self.handle.after ) )
505 self.handle.sendline( "cd " + self.home )
506 self.handle.expect( "\$" )
507 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400508 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500509 print line
510 if report:
Jon Hall82a7dad2015-04-30 16:26:08 -0700511 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800512 for line in lines[ 2:-1 ]:
513 # Bracket replacement is for Wiki-compliant
514 # formatting. '<' or '>' are interpreted
515 # as xml specific tags that cause errors
516 line = line.replace( "<", "[" )
517 line = line.replace( ">", "]" )
Jon Hall82a7dad2015-04-30 16:26:08 -0700518 #main.log.wiki( "\t" + line )
519 main.log.wiki( line + "<br /> " )
520 main.log.summary( line )
521 main.log.wiki( "</blockquote>" )
522 main.log.summary("\n")
kelvin8ec71442015-01-15 16:57:00 -0800523 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400524 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800525 main.log.error( self.name + ": EOF exception found" )
526 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400527 main.cleanup()
528 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800529 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800530 main.log.error( self.name + ": TIMEOUT exception found" )
531 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800532 main.cleanup()
533 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800534 except Exception:
535 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400536 main.cleanup()
537 main.exit()
538
kelvin-onlabd3b64892015-01-20 13:26:24 -0800539 def createCellFile( self, benchIp, fileName, mnIpAddrs,
cameron@onlab.us75900962015-03-30 13:22:49 -0700540 appString, *onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800541 """
andrewonlab94282092014-10-10 13:00:11 -0400542 Creates a cell file based on arguments
543 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800544 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400545 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800546 * File name of the cell file ( fileName )
547 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800548 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400549 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800550 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400551 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800552
andrewonlab94282092014-10-10 13:00:11 -0400553 NOTE: Assumes cells are located at:
554 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800555 """
556 # Variable initialization
kelvin-onlabd3b64892015-01-20 13:26:24 -0800557 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800558 # We want to create the cell file in the dependencies directory
559 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800560 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800561 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800562 cellFile = open( tempDirectory + fileName, 'w+' )
kelvin8ec71442015-01-15 16:57:00 -0800563
cameron@onlab.us75900962015-03-30 13:22:49 -0700564 # App string is hardcoded environment variables
kelvin8ec71442015-01-15 16:57:00 -0800565 # That you may wish to use by default on startup.
cameron@onlab.us75900962015-03-30 13:22:49 -0700566 # Note that you may not want certain apps listed
kelvin8ec71442015-01-15 16:57:00 -0800567 # on here.
cameron@onlab.us75900962015-03-30 13:22:49 -0700568 appString = "export ONOS_APPS=" + appString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800569 mnString = "export OCN="
kelvin-onlabf70fd542015-05-07 18:41:40 -0700570 if mnIpAddrs == "":
571 mnString = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800572 onosString = "export OC"
573 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800574
kelvin-onlabd3b64892015-01-20 13:26:24 -0800575 # Create ONOSNIC ip address prefix
576 tempOnosIp = onosIpAddrs[ 0 ]
577 tempList = []
578 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800579 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800580 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800581 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800582 nicAddr = ".".join( tempList ) + ".*"
583 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400584
585 try:
kelvin8ec71442015-01-15 16:57:00 -0800586 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800587 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400588
kelvin-onlabd3b64892015-01-20 13:26:24 -0800589 for arg in onosIpAddrs:
590 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800591 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400592 # export OC1="10.128.20.11"
593 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800594 cellFile.write( onosString + str( tempCount ) +
595 "=" + "\"" + arg + "\"" + "\n" )
596 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800597
kelvin-onlabd3b64892015-01-20 13:26:24 -0800598 cellFile.write( mnString + "\"" + mnIpAddrs + "\"" + "\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700599 cellFile.write( appString + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800600 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400601
kelvin8ec71442015-01-15 16:57:00 -0800602 # We use os.system to send the command to TestON cluster
603 # to account for the case in which TestON is not located
604 # on the same cluster as the ONOS bench
605 # Note that even if TestON is located on the same cluster
606 # as ONOS bench, you must setup passwordless ssh
607 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800608 os.system( "scp " + tempDirectory + fileName +
609 " admin@" + benchIp + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400610
andrewonlab2a6c9342014-10-16 13:40:15 -0400611 return main.TRUE
612
andrewonlab94282092014-10-10 13:00:11 -0400613 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800614 main.log.error( self.name + ": EOF exception found" )
615 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400616 main.cleanup()
617 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800618 except Exception:
619 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400620 main.cleanup()
621 main.exit()
622
kelvin-onlabd3b64892015-01-20 13:26:24 -0800623 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800624 """
andrewonlab95ca1462014-10-09 14:04:24 -0400625 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800626 """
andrewonlab95ca1462014-10-09 14:04:24 -0400627 try:
628 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800629 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400630 main.cleanup()
631 main.exit()
632 else:
kelvin8ec71442015-01-15 16:57:00 -0800633 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800634 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800635 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400636 # and that this driver will have to change accordingly
Cameron Franke9c94fb02015-01-21 10:20:20 -0800637 self.handle.expect(str(cellname))
kelvin-onlabd3b64892015-01-20 13:26:24 -0800638 handleBefore = self.handle.before
639 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800640 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800641 self.handle.sendline("")
642 self.handle.expect("\$")
kelvin-onlabd3b64892015-01-20 13:26:24 -0800643 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400644
kelvin-onlabd3b64892015-01-20 13:26:24 -0800645 main.log.info( "Cell call returned: " + handleBefore +
646 handleAfter + handleMore )
andrewonlab95ca1462014-10-09 14:04:24 -0400647
648 return main.TRUE
649
650 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800651 main.log.error( self.name + ": EOF exception found" )
652 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400653 main.cleanup()
654 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800655 except Exception:
656 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400657 main.cleanup()
658 main.exit()
659
kelvin-onlabd3b64892015-01-20 13:26:24 -0800660 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800661 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400662 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800663 """
664 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400665
andrewonlabc03bf6c2014-10-09 14:56:18 -0400666 try:
kelvin8ec71442015-01-15 16:57:00 -0800667 # Clean handle by sending empty and expecting $
668 self.handle.sendline( "" )
669 self.handle.expect( "\$" )
670 self.handle.sendline( "onos-verify-cell" )
671 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800672 handleBefore = self.handle.before
673 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800674 # Get the rest of the handle
675 self.handle.sendline( "" )
676 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800677 handleMore = self.handle.before
andrewonlabc03bf6c2014-10-09 14:56:18 -0400678
kelvin-onlabd3b64892015-01-20 13:26:24 -0800679 main.log.info( "Verify cell returned: " + handleBefore +
680 handleAfter + handleMore )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400681
682 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800683 except pexpect.ExceptionPexpect as e:
684 main.log.error( self.name + ": Pexpect exception found of type " +
685 str( type( e ) ) )
686 main.log.error ( e.get_trace() )
kelvin8ec71442015-01-15 16:57:00 -0800687 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400688 main.cleanup()
689 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800690 except Exception:
691 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400692 main.cleanup()
693 main.exit()
694
jenkins1e99e7b2015-04-02 18:15:39 -0700695 def onosCfgSet( self, ONOSIp, configName, configParam ):
696 """
697 Uses 'onos <node-ip> cfg set' to change a parameter value of an
698 application.
699
700 ex)
701 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
702
703 ONOSIp = '10.0.0.1'
704 configName = 'org.onosproject.myapp'
705 configParam = 'appSetting 1'
706
707 """
708 try:
709 cfgStr = ( "onos "+str(ONOSIp)+" cfg set "+
710 str(configName) + " " +
711 str(configParam)
712 )
713
714 self.handle.sendline( "" )
715 self.handle.expect( "\$" )
716 self.handle.sendline( cfgStr )
717 self.handle.expect( "\$" )
718
719 # TODO: Add meaningful assertion
720
721 return main.TRUE
722
723 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() )
727 main.log.error( self.name + ": " + self.handle.before )
728 main.cleanup()
729 main.exit()
730 except Exception:
731 main.log.exception( self.name + ": Uncaught exception!" )
732 main.cleanup()
733 main.exit()
734
kelvin-onlabd3b64892015-01-20 13:26:24 -0800735 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800736 """
andrewonlab05e362f2014-10-10 00:40:57 -0400737 Uses 'onos' command to send various ONOS CLI arguments.
738 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800739 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400740 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800741
742 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400743 CLI commands for ONOS. Try to use this function first
744 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800745 function.
746 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400747 by starting onos, and typing in 'onos' to enter the
748 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800749 available commands.
750 """
andrewonlab05e362f2014-10-10 00:40:57 -0400751 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800752 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800753 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400754 return main.FALSE
755 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800756 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400757 return main.FALSE
758
kelvin8ec71442015-01-15 16:57:00 -0800759 cmdstr = str( cmdstr )
760 self.handle.sendline( "" )
761 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400762
kelvin-onlabd3b64892015-01-20 13:26:24 -0800763 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800764 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400765
kelvin-onlabd3b64892015-01-20 13:26:24 -0800766 handleBefore = self.handle.before
Shreya Shaha73aaad2014-10-27 18:03:09 -0400767 print "handle_before = ", self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800768 # handleAfter = str( self.handle.after )
Jon Hall47a93fb2015-01-06 16:46:06 -0800769
kelvin8ec71442015-01-15 16:57:00 -0800770 # self.handle.sendline( "" )
771 # self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800772 # handleMore = str( self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400773
kelvin8ec71442015-01-15 16:57:00 -0800774 main.log.info( "Command sent successfully" )
andrewonlab05e362f2014-10-10 00:40:57 -0400775
kelvin8ec71442015-01-15 16:57:00 -0800776 # Obtain return handle that consists of result from
777 # the onos command. The string may need to be
778 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800779 # returnString = handleBefore + handleAfter
780 returnString = handleBefore
781 print "return_string = ", returnString
782 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400783
784 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800785 main.log.error( self.name + ": EOF exception found" )
786 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400787 main.cleanup()
788 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800789 except Exception:
790 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400791 main.cleanup()
792 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400793
kelvin-onlabd3b64892015-01-20 13:26:24 -0800794 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800795 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400796 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800797 If -f option is provided, it also forces an uninstall.
798 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400799 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800800 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400801 files to certain onos nodes
802
803 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800804 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400805 try:
andrewonlab114768a2014-11-14 12:44:44 -0500806 if options:
kelvin8ec71442015-01-15 16:57:00 -0800807 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500808 else:
kelvin8ec71442015-01-15 16:57:00 -0800809 self.handle.sendline( "onos-install " + node )
810 self.handle.expect( "onos-install " )
811 # NOTE: this timeout may need to change depending on the network
812 # and size of ONOS
813 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800814 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800815 "ONOS\sis\salready\sinstalled",
816 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400817
Jon Hall7993bfc2014-10-09 16:30:14 -0400818 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800819 main.log.warn( "Network is unreachable" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400820 return main.FALSE
821 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800822 main.log.info(
823 "ONOS was installed on " +
824 node +
825 " and started" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400826 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500827 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800828 main.log.info( "ONOS is already installed on " + node )
andrewonlabd9a73a72014-11-14 17:28:21 -0500829 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800830 elif i == 3:
831 main.log.info(
832 "Installation of ONOS on " +
833 node +
834 " timed out" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400835 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400836
837 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800838 main.log.error( self.name + ": EOF exception found" )
839 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400840 main.cleanup()
841 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800842 except Exception:
843 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400844 main.cleanup()
845 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400846
kelvin-onlabd3b64892015-01-20 13:26:24 -0800847 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800848 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400849 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400850 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800851 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400852 try:
kelvin8ec71442015-01-15 16:57:00 -0800853 self.handle.sendline( "" )
854 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800855 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800856 " start" )
857 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -0400858 "Job\sis\salready\srunning",
859 "start/running",
860 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800861 pexpect.TIMEOUT ], timeout=120 )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400862
863 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800864 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400865 return main.TRUE
866 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800867 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400868 return main.TRUE
869 else:
kelvin8ec71442015-01-15 16:57:00 -0800870 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400871 main.cleanup()
872 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400873 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800874 main.log.error( self.name + ": EOF exception found" )
875 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400876 main.cleanup()
877 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800878 except Exception:
879 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400880 main.cleanup()
881 main.exit()
882
kelvin-onlabd3b64892015-01-20 13:26:24 -0800883 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800884 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400885 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400886 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800887 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400888 try:
kelvin8ec71442015-01-15 16:57:00 -0800889 self.handle.sendline( "" )
890 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800891 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800892 " stop" )
893 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -0400894 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -0700895 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -0400896 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800897 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2b30bd32014-10-09 16:48:55 -0400898
899 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800900 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400901 return main.TRUE
902 elif i == 1:
Jon Hall65844a32015-03-09 19:09:37 -0700903 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800904 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -0400905 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -0700906 elif i == 2:
907 main.log.warn( "ONOS wasn't running" )
908 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -0400909 else:
kelvin8ec71442015-01-15 16:57:00 -0800910 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400911 return main.FALSE
912
913 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 )
andrewonlab2b30bd32014-10-09 16:48:55 -0400916 main.cleanup()
917 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800918 except Exception:
919 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400920 main.cleanup()
921 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800922
kelvin-onlabd3b64892015-01-20 13:26:24 -0800923 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -0800924 """
andrewonlabc8d47972014-10-09 16:52:36 -0400925 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -0800926 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -0400927 if needed
kelvin8ec71442015-01-15 16:57:00 -0800928 """
andrewonlabc8d47972014-10-09 16:52:36 -0400929 try:
kelvin8ec71442015-01-15 16:57:00 -0800930 self.handle.sendline( "" )
931 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800932 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800933 self.handle.expect( "\$" )
andrewonlabc8d47972014-10-09 16:52:36 -0400934
kelvin-onlabd3b64892015-01-20 13:26:24 -0800935 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
andrewonlab9dfd2082014-11-13 17:44:03 -0500936
kelvin8ec71442015-01-15 16:57:00 -0800937 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -0400938 return main.TRUE
939
940 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800941 main.log.error( self.name + ": EOF exception found" )
942 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -0400943 main.cleanup()
944 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800945 except Exception:
946 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -0400947 main.cleanup()
948 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400949
kelvin-onlabd3b64892015-01-20 13:26:24 -0800950 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800951 """
andrewonlabaedc8332014-12-04 12:43:03 -0500952 Issues the command 'onos-die <node-ip>'
953 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -0800954 """
andrewonlabaedc8332014-12-04 12:43:03 -0500955 try:
kelvin8ec71442015-01-15 16:57:00 -0800956 self.handle.sendline( "" )
957 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800958 cmdStr = "onos-kill " + str( nodeIp )
959 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800960 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -0500961 "Killing\sONOS",
962 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -0800963 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -0500964 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800965 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800966 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -0500967 return main.TRUE
968 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800969 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -0500970 return main.FALSE
971 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800972 main.log.error( self.name + ": EOF exception found" )
973 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -0500974 main.cleanup()
975 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800976 except Exception:
977 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -0500978 main.cleanup()
979 main.exit()
980
kelvin-onlabd3b64892015-01-20 13:26:24 -0800981 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800982 """
andrewonlabe8e56fd2014-10-09 17:12:44 -0400983 Calls the command: 'onos-kill [<node-ip>]'
984 "Remotely, and unceremoniously kills the ONOS instance running on
985 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -0800986 """
andrewonlabe8e56fd2014-10-09 17:12:44 -0400987 try:
kelvin8ec71442015-01-15 16:57:00 -0800988 self.handle.sendline( "" )
989 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800990 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800991 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -0400992 "\$",
993 "No\sroute\sto\shost",
994 "password:",
kelvin8ec71442015-01-15 16:57:00 -0800995 pexpect.TIMEOUT ], timeout=20 )
996
andrewonlabe8e56fd2014-10-09 17:12:44 -0400997 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800998 main.log.info(
999 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001000 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001001 return main.TRUE
1002 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001003 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001004 return main.FALSE
1005 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001006 main.log.info(
1007 "Passwordless login for host: " +
1008 str( nodeIp ) +
1009 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001010 return main.FALSE
1011 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001012 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001013 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001014
andrewonlabe8e56fd2014-10-09 17:12:44 -04001015 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001016 main.log.error( self.name + ": EOF exception found" )
1017 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001018 main.cleanup()
1019 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001020 except Exception:
1021 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001022 main.cleanup()
1023 main.exit()
1024
kelvin-onlabd3b64892015-01-20 13:26:24 -08001025 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001026 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001027 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001028 a cleaner environment.
1029
andrewonlab19fbdca2014-11-14 12:55:59 -05001030 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001031 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001032 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001033 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001034 try:
kelvin8ec71442015-01-15 16:57:00 -08001035 self.handle.sendline( "" )
1036 self.handle.expect( "\$" )
1037 self.handle.sendline( "onos-remove-raft-logs" )
1038 # Sometimes this command hangs
1039 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1040 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001041 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001042 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1043 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001044 if i == 1:
1045 return main.FALSE
shahshreya957feaa2015-03-23 16:08:29 -07001046 #self.handle.sendline( "" )
1047 #self.handle.expect( "\$" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001048 return main.TRUE
1049
1050 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001051 main.log.error( self.name + ": EOF exception found" )
1052 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001053 main.cleanup()
1054 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001055 except Exception:
1056 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001057 main.cleanup()
1058 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001059
kelvin-onlabd3b64892015-01-20 13:26:24 -08001060 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001061 """
1062 Calls the command 'onos-start-network [ <mininet-topo> ]
1063 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001064 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001065 cell."
andrewonlab94282092014-10-10 13:00:11 -04001066 * Specify mininet topology file name for mntopo
1067 * Topo files should be placed at:
1068 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001069
andrewonlab94282092014-10-10 13:00:11 -04001070 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001071 """
andrewonlab94282092014-10-10 13:00:11 -04001072 try:
1073 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001074 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001075 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001076
kelvin8ec71442015-01-15 16:57:00 -08001077 mntopo = str( mntopo )
1078 self.handle.sendline( "" )
1079 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001080
kelvin8ec71442015-01-15 16:57:00 -08001081 self.handle.sendline( "onos-start-network " + mntopo )
1082 self.handle.expect( "mininet>" )
1083 main.log.info( "Network started, entered mininet prompt" )
1084
1085 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001086
1087 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001088 main.log.error( self.name + ": EOF exception found" )
1089 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001090 main.cleanup()
1091 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001092 except Exception:
1093 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001094 main.cleanup()
1095 main.exit()
1096
Cameron Franke9c94fb02015-01-21 10:20:20 -08001097 def isup(self, node = "", timeout = 120):
kelvin8ec71442015-01-15 16:57:00 -08001098 """
1099 Run's onos-wait-for-start which only returns once ONOS is at run
Cameron Franke9c94fb02015-01-21 10:20:20 -08001100 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001101
Jon Hall7993bfc2014-10-09 16:30:14 -04001102 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001103 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001104 try:
Cameron Franke9c94fb02015-01-21 10:20:20 -08001105 self.handle.sendline("onos-wait-for-start " + node )
1106 self.handle.expect("onos-wait-for-start")
kelvin8ec71442015-01-15 16:57:00 -08001107 # NOTE: this timeout is arbitrary"
Cameron Franke9c94fb02015-01-21 10:20:20 -08001108 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001109 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001110 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001111 return main.TRUE
1112 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001113 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001114 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001115 main.log.error( "ONOS has not started yet" )
1116 self.handle.send( "\x03" ) # Control-C
1117 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001118 return main.FALSE
1119 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001120 main.log.error( self.name + ": EOF exception found" )
1121 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001122 main.cleanup()
1123 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001124 except Exception:
1125 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001126 main.cleanup()
1127 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001128
kelvin-onlabd3b64892015-01-20 13:26:24 -08001129 def pushTestIntentsShell(
1130 self,
1131 dpidSrc,
1132 dpidDst,
1133 numIntents,
1134 dirFile,
1135 onosIp,
1136 numMult="",
1137 appId="",
1138 report=True,
1139 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001140 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001141 Description:
kelvin8ec71442015-01-15 16:57:00 -08001142 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001143 better parallelize the results than the CLI
1144 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001145 * dpidSrc: specify source dpid
1146 * dpidDst: specify destination dpid
1147 * numIntents: specify number of intents to push
1148 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001149 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001150 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001151 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001152 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001153 """
1154 try:
1155 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001156 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001157 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001158 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001159 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001160 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001161
kelvin-onlabd3b64892015-01-20 13:26:24 -08001162 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1163 if not numMult:
1164 addIntents = addDpid + " " + str( numIntents )
1165 elif numMult:
1166 addIntents = addDpid + " " + str( numIntents ) + " " +\
1167 str( numMult )
1168 if appId:
1169 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001170 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001171 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001172
andrewonlabaedc8332014-12-04 12:43:03 -05001173 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001174 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001175 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001176 sendCmd = addApp + " &"
1177 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001178
kelvin-onlabd3b64892015-01-20 13:26:24 -08001179 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001180
1181 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001182 main.log.error( self.name + ": EOF exception found" )
1183 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001184 main.cleanup()
1185 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001186 except Exception:
1187 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001188 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001189 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001190
kelvin-onlabd3b64892015-01-20 13:26:24 -08001191 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001192 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001193 Definition:
1194 Loads a json topology output
1195 Return:
1196 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -08001197 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001198 import json
Jon Hall77f53ce2014-10-13 18:02:06 -04001199 try:
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001200 # either onos:topology or 'topology' will work in CLI
1201 topology = json.loads(topologyOutput)
1202 print topology
Jon Hall77f53ce2014-10-13 18:02:06 -04001203 return topology
1204 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001205 main.log.error( self.name + ": EOF exception found" )
1206 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001207 main.cleanup()
1208 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001209 except Exception:
1210 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001211 main.cleanup()
1212 main.exit()
1213
kelvin-onlabd3b64892015-01-20 13:26:24 -08001214 def checkStatus(
1215 self,
1216 topologyResult,
1217 numoswitch,
1218 numolink,
1219 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001220 """
Jon Hallefbd9792015-03-05 16:11:36 -08001221 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001222 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001223 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08001224
Jon Hall77f53ce2014-10-13 18:02:06 -04001225 Params: ip = ip used for the onos cli
1226 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001227 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001228 logLevel = level to log to.
1229 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001230
1231
kelvin-onlabd3b64892015-01-20 13:26:24 -08001232 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001233
Jon Hallefbd9792015-03-05 16:11:36 -08001234 Returns: main.TRUE if the number of switches and links are correct,
1235 main.FALSE if the number of switches and links is incorrect,
Jon Hall77f53ce2014-10-13 18:02:06 -04001236 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001237 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001238 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001239 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001240 if topology == {}:
1241 return main.ERROR
1242 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001243 # Is the number of switches is what we expected
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001244 devices = topology.get( 'deviceCount', False )
1245 links = topology.get( 'linkCount', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001246 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001247 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001248 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001249 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001250 linkCheck = ( int( links ) == int( numolink ) )
Jon Hallefbd9792015-03-05 16:11:36 -08001251 if switchCheck and linkCheck:
kelvin8ec71442015-01-15 16:57:00 -08001252 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001253 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001254 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001255 result = main.TRUE
1256 else:
1257 output = output + \
Jon Hall274b6642015-02-17 11:57:17 -08001258 "The number of links and switches does not match " + \
1259 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001260 result = main.FALSE
Jon Hallefbd9792015-03-05 16:11:36 -08001261 output = output + "\n ONOS sees %i devices" % int( devices )
1262 output = output + " (%i expected) " % int( numoswitch )
Jon Hall274b6642015-02-17 11:57:17 -08001263 output = output + "and %i links " % int( links )
1264 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001265 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001266 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001267 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001268 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001269 else:
kelvin8ec71442015-01-15 16:57:00 -08001270 main.log.info( output )
1271 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001272 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001273 main.log.error( self.name + ": EOF exception found" )
1274 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001275 main.cleanup()
1276 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001277 except Exception:
1278 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001279 main.cleanup()
1280 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001281
kelvin-onlabd3b64892015-01-20 13:26:24 -08001282 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001283 """
andrewonlab970399c2014-11-07 13:09:32 -05001284 Capture all packet activity and store in specified
1285 directory/file
1286
1287 Required:
1288 * interface: interface to capture
1289 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001290 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001291 try:
1292 self.handle.sendline( "" )
1293 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001294
Jon Hallfebb1c72015-03-05 13:30:09 -08001295 self.handle.sendline( "tshark -i " + str( interface ) +
1296 " -t e -w " + str( dirFile ) + " &" )
1297 self.handle.sendline( "\r" )
1298 self.handle.expect( "Capturing on" )
1299 self.handle.sendline( "\r" )
1300 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001301
Jon Hallfebb1c72015-03-05 13:30:09 -08001302 main.log.info( "Tshark started capturing files on " +
1303 str( interface ) + " and saving to directory: " +
1304 str( dirFile ) )
1305 except pexpect.EOF:
1306 main.log.error( self.name + ": EOF exception found" )
1307 main.log.error( self.name + ": " + self.handle.before )
1308 main.cleanup()
1309 main.exit()
1310 except Exception:
1311 main.log.exception( self.name + ": Uncaught exception!" )
1312 main.cleanup()
1313 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001314
kelvin-onlabd3b64892015-01-20 13:26:24 -08001315 def runOnosTopoCfg( self, instanceName, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001316 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001317 On ONOS bench, run this command:
Jon Halle94919c2015-03-23 11:42:57 -07001318 {ONOS_HOME}/tools/test/bin/onos-topo-cfg $OC1 filename
kelvin-onlabd3b64892015-01-20 13:26:24 -08001319 which starts the rest and copies
1320 the json file to the onos instance
kelvin8ec71442015-01-15 16:57:00 -08001321 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001322 try:
kelvin8ec71442015-01-15 16:57:00 -08001323 self.handle.sendline( "" )
1324 self.handle.expect( "\$" )
Jon Halle94919c2015-03-23 11:42:57 -07001325 self.handle.sendline( "cd " + self.home + "/tools/test/bin" )
kelvin8ec71442015-01-15 16:57:00 -08001326 self.handle.expect( "/bin$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001327 cmd = "./onos-topo-cfg " + instanceName + " " + jsonFile
shahshreyae6c7cf42014-11-26 16:39:01 -08001328 print "cmd = ", cmd
kelvin8ec71442015-01-15 16:57:00 -08001329 self.handle.sendline( cmd )
1330 self.handle.expect( "\$" )
1331 self.handle.sendline( "cd ~" )
1332 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -08001333 return main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -08001334 except pexpect.EOF:
1335 main.log.error( self.name + ": EOF exception found" )
1336 main.log.error( self.name + ": " + self.handle.before )
1337 main.cleanup()
1338 main.exit()
1339 except Exception:
1340 main.log.exception( self.name + ": Uncaught exception!" )
1341 main.cleanup()
1342 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001343
jenkins1e99e7b2015-04-02 18:15:39 -07001344 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001345 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001346 Required:
kelvin8ec71442015-01-15 16:57:00 -08001347 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001348 * directory to store results
1349 Optional:
1350 * interface - default: eth0
jenkins1e99e7b2015-04-02 18:15:39 -07001351 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001352 Description:
1353 Uses tshark command to grep specific group of packets
1354 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001355 The timestamp is hardcoded to be in epoch
1356 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001357 try:
1358 self.handle.sendline( "" )
1359 self.handle.expect( "\$" )
1360 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001361 if grepOptions:
1362 grepStr = "grep "+str(grepOptions)
1363 else:
1364 grepStr = "grep"
1365
Jon Hallfebb1c72015-03-05 13:30:09 -08001366 self.handle.sendline(
1367 "tshark -i " +
1368 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001369 " -t e | " +
1370 grepStr + " --line-buffered \"" +
Jon Hallfebb1c72015-03-05 13:30:09 -08001371 str(grep) +
1372 "\" >" +
1373 directory +
1374 " &" )
1375 self.handle.sendline( "\r" )
1376 self.handle.expect( "Capturing on" )
1377 self.handle.sendline( "\r" )
1378 self.handle.expect( "\$" )
1379 except pexpect.EOF:
1380 main.log.error( self.name + ": EOF exception found" )
1381 main.log.error( self.name + ": " + self.handle.before )
1382 main.cleanup()
1383 main.exit()
1384 except Exception:
1385 main.log.exception( self.name + ": Uncaught exception!" )
1386 main.cleanup()
1387 main.exit()
1388
kelvin-onlabd3b64892015-01-20 13:26:24 -08001389 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001390 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001391 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001392 """
1393 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001394 try:
1395 self.execute( cmd="sudo rm /tmp/wireshark*" )
1396 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001397 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1398 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001399 self.handle.sendline( "" )
1400 main.log.info( "Tshark stopped" )
1401 except pexpect.EOF:
1402 main.log.error( self.name + ": EOF exception found" )
1403 main.log.error( self.name + ": " + self.handle.before )
1404 main.cleanup()
1405 main.exit()
1406 except Exception:
1407 main.log.exception( self.name + ": Uncaught exception!" )
1408 main.cleanup()
1409 main.exit()
1410
kelvin8ec71442015-01-15 16:57:00 -08001411 def ptpd( self, args ):
1412 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001413 Initiate ptp with user-specified args.
1414 Required:
1415 * args: specify string of args after command
1416 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001417 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001418 try:
kelvin8ec71442015-01-15 16:57:00 -08001419 self.handle.sendline( "sudo ptpd " + str( args ) )
1420 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001421 "Multiple",
1422 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001423 "\$" ] )
1424 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001425
andrewonlab0c38a4a2014-10-28 18:35:35 -04001426 if i == 0:
1427 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001428 main.log.info( "ptpd returned an error: " +
1429 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001430 return handle
1431 elif i == 1:
1432 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001433 main.log.error( "ptpd returned an error: " +
1434 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001435 return handle
1436 else:
1437 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001438
andrewonlab0c38a4a2014-10-28 18:35:35 -04001439 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001440 main.log.error( self.name + ": EOF exception found" )
1441 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001442 main.cleanup()
1443 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001444 except Exception:
1445 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001446 main.cleanup()
1447 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001448
kelvin-onlabd3b64892015-01-20 13:26:24 -08001449 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001450 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001451 """
1452 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001453 Current implementation of ONOS deletes its karaf
1454 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001455 you may want to use this function to capture
1456 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001457 Localtime will be attached to the filename
1458
1459 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001460 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001461 copy.
kelvin8ec71442015-01-15 16:57:00 -08001462 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001463 For copying multiple files, leave copyFileName
1464 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001465 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001466 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001467 ex ) /tmp/
1468 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001469 * copyFileName: If you want to rename the log
1470 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001471 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001472 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001473 try:
kelvin8ec71442015-01-15 16:57:00 -08001474 localtime = time.strftime( '%x %X' )
1475 localtime = localtime.replace( "/", "" )
1476 localtime = localtime.replace( " ", "_" )
1477 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001478 if destDir[ -1: ] != "/":
1479 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001480
kelvin-onlabd3b64892015-01-20 13:26:24 -08001481 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001482 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1483 str( destDir ) + str( copyFileName ) +
1484 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001485 self.handle.expect( "cp" )
1486 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001487 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001488 self.handle.sendline( "cp " + str( logToCopy ) +
1489 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001490 self.handle.expect( "cp" )
1491 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001492
kelvin8ec71442015-01-15 16:57:00 -08001493 return self.handle.before
1494
1495 except pexpect.EOF:
1496 main.log.error( "Copying files failed" )
1497 main.log.error( self.name + ": EOF exception found" )
1498 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001499 except Exception:
1500 main.log.exception( "Copying files failed" )
1501
kelvin-onlabd3b64892015-01-20 13:26:24 -08001502 def checkLogs( self, onosIp ):
kelvin8ec71442015-01-15 16:57:00 -08001503 """
Jon Hall94fd0472014-12-08 11:52:42 -08001504 runs onos-check-logs on the given onos node
1505 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001506 """
Jon Hall94fd0472014-12-08 11:52:42 -08001507 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001508 cmd = "onos-check-logs " + str( onosIp )
kelvin8ec71442015-01-15 16:57:00 -08001509 self.handle.sendline( cmd )
1510 self.handle.expect( cmd )
1511 self.handle.expect( "\$" )
Jon Hall94fd0472014-12-08 11:52:42 -08001512 response = self.handle.before
1513 return response
1514 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001515 main.log.error( "Lost ssh connection" )
1516 main.log.error( self.name + ": EOF exception found" )
1517 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001518 except Exception:
1519 main.log.exception( self.name + ": Uncaught exception!" )
1520 main.cleanup()
1521 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001522
kelvin-onlabd3b64892015-01-20 13:26:24 -08001523 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001524 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001525 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001526 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001527 try:
kelvin8ec71442015-01-15 16:57:00 -08001528 self.handle.sendline( "" )
1529 self.handle.expect( "\$" )
1530 self.handle.sendline( "onos-service " + str( node ) +
1531 " status" )
1532 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001533 "start/running",
1534 "stop/waiting",
kelvin8ec71442015-01-15 16:57:00 -08001535 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001536
1537 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001538 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001539 return main.TRUE
1540 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001541 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001542 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001543 main.cleanup()
1544 main.exit()
1545 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001546 main.log.error( self.name + ": EOF exception found" )
1547 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001548 main.cleanup()
1549 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001550 except Exception:
1551 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001552 main.cleanup()
1553 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001554
Jon Hall63604932015-02-26 17:09:50 -08001555 def setIpTables( self, ip, port='', action='add', packet_type='',
1556 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001557 """
Jon Hall21270ac2015-02-16 17:59:55 -08001558 Description:
1559 add or remove iptables rule to DROP (default) packets from
1560 specific IP and PORT
1561 Usage:
1562 * specify action ('add' or 'remove')
1563 when removing, pass in the same argument as you would add. It will
1564 delete that specific rule.
1565 * specify the ip to block
1566 * specify the destination port to block (defaults to all ports)
1567 * optional packet type to block (default tcp)
1568 * optional iptables rule (default DROP)
1569 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001570 * States boolean toggles adding all supported tcp states to the
1571 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001572 Returns:
1573 main.TRUE on success or
1574 main.FALSE if given invalid input or
1575 main.ERROR if there is an error in response from iptables
1576 WARNING:
1577 * This function uses root privilege iptables command which may result
1578 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001579 """
Jon Hall21270ac2015-02-16 17:59:55 -08001580 import time
1581
1582 # NOTE*********
1583 # The strict checking methods of this driver function is intentional
1584 # to discourage any misuse or error of iptables, which can cause
1585 # severe network errors
1586 # *************
1587
1588 # NOTE: Sleep needed to give some time for rule to be added and
1589 # registered to the instance. If you are calling this function
1590 # multiple times this sleep will prevent any errors.
1591 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001592 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001593 try:
1594 # input validation
1595 action_type = action.lower()
1596 rule = rule.upper()
1597 direction = direction.upper()
1598 if action_type != 'add' and action_type != 'remove':
1599 main.log.error( "Invalid action type. Use 'add' or "
1600 "'remove' table rule" )
1601 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1602 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1603 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1604 "'ACCEPT' or 'LOG' only." )
1605 if direction != 'INPUT' and direction != 'OUTPUT':
1606 # NOTE currently only supports rules INPUT and OUPTUT
1607 main.log.error( "Invalid rule. Valid directions are"
1608 " 'OUTPUT' or 'INPUT'" )
1609 return main.FALSE
1610 return main.FALSE
1611 return main.FALSE
1612 if action_type == 'add':
1613 # -A is the 'append' action of iptables
1614 actionFlag = '-A'
1615 elif action_type == 'remove':
1616 # -D is the 'delete' rule of iptables
1617 actionFlag = '-D'
1618 self.handle.sendline( "" )
1619 self.handle.expect( "\$" )
1620 cmd = "sudo iptables " + actionFlag + " " +\
1621 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001622 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001623 # " -p " + str( packet_type ) +\
1624 if packet_type:
1625 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001626 if port:
1627 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001628 if states:
1629 cmd += " -m state --state="
1630 #FIXME- Allow user to configure which states to block
1631 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001632 cmd += " -j " + str( rule )
1633
1634 self.handle.sendline( cmd )
1635 self.handle.expect( "\$" )
1636 main.log.warn( self.handle.before )
1637
1638 info_string = "On " + str( self.name )
1639 info_string += " " + str( action_type )
1640 info_string += " iptable rule [ "
1641 info_string += " IP: " + str( ip )
1642 info_string += " Port: " + str( port )
1643 info_string += " Rule: " + str( rule )
1644 info_string += " Direction: " + str( direction ) + " ]"
1645 main.log.info( info_string )
1646 return main.TRUE
1647 except pexpect.TIMEOUT:
1648 main.log.exception( self.name + ": Timeout exception in "
1649 "setIpTables function" )
1650 return main.ERROR
1651 except pexpect.EOF:
1652 main.log.error( self.name + ": EOF exception found" )
1653 main.log.error( self.name + ": " + self.handle.before )
1654 main.cleanup()
1655 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001656 except Exception:
1657 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001658 main.cleanup()
1659 main.exit()
1660
Jon Hall0468b042015-02-19 19:08:21 -08001661 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001662 """
Jon Hall0468b042015-02-19 19:08:21 -08001663 This method is used by STS to check the status of the controller
1664 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001665 """
Jon Hall0468b042015-02-19 19:08:21 -08001666 import re
1667 try:
1668 self.handle.sendline( "" )
1669 self.handle.expect( "\$" )
1670 self.handle.sendline( "cd " + self.home )
1671 self.handle.expect( "\$" )
1672 self.handle.sendline( "service onos status" )
1673 self.handle.expect( "\$" )
1674 response = self.handle.before
1675 if re.search( "onos start/running", response ):
1676 # onos start/running, process 10457
1677 return 'RUNNING'
1678 # FIXME: Implement this case
1679 # elif re.search( pattern, response ):
1680 # return 'STARTING'
1681 elif re.search( "onos stop/", response ):
1682 # onos stop/waiting
1683 # FIXME handle this differently?: onos stop/pre-stop
1684 return 'STOPPED'
1685 # FIXME: Implement this case
1686 # elif re.search( pattern, response ):
1687 # return 'FROZEN'
1688 else:
1689 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001690 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001691 main.log.warn( response )
1692 return 'ERROR', "Unknown response: %s" % response
1693 except pexpect.TIMEOUT:
1694 main.log.exception( self.name + ": Timeout exception in "
1695 "setIpTables function" )
1696 return 'ERROR', "Pexpect Timeout"
1697 except pexpect.EOF:
1698 main.log.error( self.name + ": EOF exception found" )
1699 main.log.error( self.name + ": " + self.handle.before )
1700 main.cleanup()
1701 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001702 except Exception:
1703 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001704 main.cleanup()
1705 main.exit()
1706
andrew@onlab.us3b087132015-03-11 15:00:08 -07001707 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1708 '''
1709 Create/formats the LinkGraph.cfg file based on arguments
1710 -only creates a linear topology and connects islands
1711 -evenly distributes devices
1712 -must be called by ONOSbench
1713
1714 ONOSIpList - list of all of the node IPs to be used
1715
1716 deviceCount - number of switches to be assigned
1717 '''
1718 main.log.step("Creating link graph configuration file." )
1719 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
1720 tempFile = "/tmp/linkGraph.cfg"
1721
1722 linkGraph = open(tempFile, 'w+')
1723 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1724 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1725 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
1726
1727 clusterCount = len(ONOSIpList)
1728
1729 if type(deviceCount) is int or type(deviceCount) is str:
1730 deviceCount = int(deviceCount)
1731 switchList = [0]*(clusterCount+1)
1732 baselineSwitchCount = deviceCount/clusterCount
1733
1734 for node in range(1, clusterCount + 1):
1735 switchList[node] = baselineSwitchCount
1736
1737 for node in range(1, (deviceCount%clusterCount)+1):
1738 switchList[node] += 1
1739
1740 if type(deviceCount) is list:
1741 main.log.info("Using provided device distribution")
1742 switchList = [0]
1743 for i in deviceCount:
1744 switchList.append(int(i))
1745
1746 tempList = ['0']
1747 tempList.extend(ONOSIpList)
1748 ONOSIpList = tempList
1749
1750 myPort = 6
1751 lastSwitch = 0
1752 for node in range(1, clusterCount+1):
1753 if switchList[node] == 0:
1754 continue
1755
1756 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
1757
1758 if node > 1:
1759 #connect to last device on previous node
1760 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
1761 linkGraph.write(line)
1762
1763 lastSwitch = 0
1764 for switch in range (0, switchList[node]-1):
1765 line = ""
1766 line = ("\t" + str(switch) + ":" + str(myPort))
1767 line += " -- "
1768 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
1769 linkGraph.write(line)
1770 lastSwitch = switch+1
1771 lastIp = ONOSIpList[node]
1772
1773 #lastSwitch += 1
1774 if node < (clusterCount):
1775 #connect to first device on the next node
1776 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
1777 linkGraph.write(line)
1778
1779 linkGraph.write("}\n")
1780 linkGraph.close()
1781
1782 #SCP
1783 os.system( "scp " + tempFile + " admin@" + benchIp + ":" + linkGraphPath)
1784 main.log.info("linkGraph.cfg creation complete")
1785
cameron@onlab.us75900962015-03-30 13:22:49 -07001786 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001787
1788 '''
andrew@onlab.us3b087132015-03-11 15:00:08 -07001789 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
cameron@onlab.us75900962015-03-30 13:22:49 -07001790 deviceCount = number of switches to distribute, or list of values to use as custom distribution
1791 numPorts = number of ports per device. Defaults to 10 both in this function and in ONOS. Optional arg
andrew@onlab.us3b087132015-03-11 15:00:08 -07001792 '''
1793
cameron@onlab.us75900962015-03-30 13:22:49 -07001794 main.log.step("Configuring Null Device Provider" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001795 clusterCount = len(ONOSIpList)
1796
cameron@onlab.us75900962015-03-30 13:22:49 -07001797 try:
1798
1799 if type(deviceCount) is int or type(deviceCount) is str:
1800 main.log.step("Creating device distribution")
1801 deviceCount = int(deviceCount)
1802 switchList = [0]*(clusterCount+1)
1803 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001804
cameron@onlab.us75900962015-03-30 13:22:49 -07001805 for node in range(1, clusterCount + 1):
1806 switchList[node] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001807
cameron@onlab.us75900962015-03-30 13:22:49 -07001808 for node in range(1, (deviceCount%clusterCount)+1):
1809 switchList[node] += 1
1810
1811 if type(deviceCount) is list:
1812 main.log.info("Using provided device distribution")
1813
1814 if len(deviceCount) == clusterCount:
1815 switchList = ['0']
1816 switchList.extend(deviceCount)
1817
1818 if len(deviceCount) == (clusterCount + 1):
1819 if deviceCount[0] == '0' or deviceCount[0] == 0:
1820 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001821
cameron@onlab.us75900962015-03-30 13:22:49 -07001822 assert len(switchList) == (clusterCount + 1)
1823
1824 except AssertionError:
1825 main.log.error( "Bad device/Ip list match")
1826 except TypeError:
1827 main.log.exception( self.name + ": Object not as expected" )
1828 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07001829 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07001830 main.log.exception( self.name + ": Uncaught exception!" )
1831 main.cleanup()
1832 main.exit()
1833
andrew@onlab.us3b087132015-03-11 15:00:08 -07001834
1835 ONOSIp = [0]
1836 ONOSIp.extend(ONOSIpList)
1837
1838 devicesString = "devConfigs = "
1839 for node in range(1, len(ONOSIp)):
1840 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
1841 if node < clusterCount:
1842 devicesString += (",")
cameron@onlab.us75900962015-03-30 13:22:49 -07001843
1844 try:
1845 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
1846 self.handle.expect(":~")
1847 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
1848 self.handle.expect(":~")
andrew@onlab.us3b087132015-03-11 15:00:08 -07001849
cameron@onlab.us75900962015-03-30 13:22:49 -07001850 for i in range(10):
1851 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
1852 self.handle.expect(":~")
1853 verification = self.handle.before
1854 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
1855 break
1856 else:
1857 time.sleep(1)
andrew@onlab.us3b087132015-03-11 15:00:08 -07001858
cameron@onlab.us2cc8bf12015-04-02 14:12:30 -07001859 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
cameron@onlab.us75900962015-03-30 13:22:49 -07001860
1861 except AssertionError:
1862 main.log.error("Incorrect Config settings: " + verification)
Jon Hall77ba41c2015-04-06 10:25:40 -07001863 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07001864 main.log.exception( self.name + ": Uncaught exception!" )
1865 main.cleanup()
1866 main.exit()
1867
cameron@onlab.usc80a8c82015-04-15 14:57:37 -07001868 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001869 '''
cameron@onlab.us75900962015-03-30 13:22:49 -07001870 fileName default is currently the same as the default on ONOS, specify alternate file if
1871 you want to use a different topology file than linkGraph.cfg
andrew@onlab.us3b087132015-03-11 15:00:08 -07001872 '''
1873
andrew@onlab.us3b087132015-03-11 15:00:08 -07001874
cameron@onlab.us75900962015-03-30 13:22:49 -07001875 try:
1876 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
1877 self.handle.expect(":~")
1878 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
1879 self.handle.expect(":~")
1880
1881 for i in range(10):
1882 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
1883 self.handle.expect(":~")
1884 verification = self.handle.before
1885 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
1886 break
1887 else:
1888 time.sleep(1)
1889
1890 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
1891
1892 except pexpect.EOF:
1893 main.log.error( self.name + ": EOF exception found" )
1894 main.log.error( self.name + ": " + self.handle.before )
1895 main.cleanup()
1896 main.exit()
cameron@onlab.us75900962015-03-30 13:22:49 -07001897 except AssertionError:
1898 main.log.info("Settings did not post to ONOS")
1899 main.log.error(varification)
Jon Hall77ba41c2015-04-06 10:25:40 -07001900 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07001901 main.log.exception( self.name + ": Uncaught exception!" )
1902 main.log.error(varification)
1903 main.cleanup()
1904 main.exit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07001905
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001906 def getOnosIps(self):
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001907
1908 import os
kelvin-onlab0a28a742015-05-18 16:03:13 -07001909
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001910 # reads env for OC variables, also saves file with OC variables. If file and env conflict
1911 # priority goes to env. If file has OCs that are not in the env, the file OCs are used.
1912 # In other words, if the env is set, the test will use those values.
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001913
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001914 # returns a list of ip addresses for the onos nodes, will work with up to 7 nodes + OCN and OCI
1915 # returns in format [ OC1 ip, OC2 ...ect. , OCN, OCI ]
1916
1917 envONOSIps = {}
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001918
1919 x = 1
1920 while True:
1921 try:
1922 temp = os.environ[ 'OC' + str(x) ]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001923 except KeyError:
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001924 break
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001925 envONOSIps[ ("OC" + str(x)) ] = temp
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001926 x += 1
1927
1928 try:
1929 temp = os.environ[ 'OCN' ]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001930 envONOSIps[ "OCN" ] = temp
1931 except KeyError:
1932 main.log.info("OCN not set in env")
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001933
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001934 try:
1935 temp = os.environ[ 'OCI' ]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001936 envONOSIps[ "OCI" ] = temp
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001937 except:
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001938 main.log.error("OCI not set in env")
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001939
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001940 print(str(envONOSIps))
1941
1942 order = [ "OC1", "OC2", "OC3","OC4","OC5","OC6","OC7","OCN","OCI" ]
1943 ONOSIps = []
1944
kelvin-onlab0a28a742015-05-18 16:03:13 -07001945 try:
1946 if os.path.exists("myIps"):
1947 ipFile = open("myIps","r+")
1948 else:
1949 ipFile = open("myIps","w+")
1950
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001951 fileONOSIps = ipFile.readlines()
1952 ipFile.close()
1953
1954 print str(fileONOSIps)
1955
1956 if str(fileONOSIps) == "[]":
1957 ipFile = open("myIps","w+")
1958 for key in envONOSIps:
1959 ipFile.write(key+ "=" + envONOSIps[key] + "\n")
1960 ipFile.close()
1961 for i in order:
1962 if i in envONOSIps:
1963 ONOSIps.append(envONOSIps[i])
1964
1965 return ONOSIps
1966
1967 else:
1968 fileDict = {}
1969 for line in fileONOSIps:
1970 line = line.replace("\n","")
1971 line = line.split("=")
1972 key = line[0]
1973 value = line[1]
1974 fileDict[key] = value
1975
1976 for x in envONOSIps:
1977 if x in fileDict:
1978 if envONOSIps[x] == fileDict[x]:
1979 continue
1980 else:
1981 fileDict[x] = envONOSIps[x]
1982 else:
1983 fileDict[x] = envONOSIps[x]
1984
1985 ipFile = open("myIps","w+")
1986 for key in order:
1987 if key in fileDict:
1988 ipFile.write(key + "=" + fileDict[key] + "\n")
1989 ONOSIps.append(fileDict[key])
1990 ipFile.close()
1991
1992 return ONOSIps
1993
1994 except IOError as a:
1995 main.log.error(a)
1996
kelvin-onlab0a28a742015-05-18 16:03:13 -07001997 except Exception as a:
cameron@onlab.us966d1be2015-05-15 14:54:09 -07001998 main.log.error(a)
cameron@onlab.us59d29d92015-05-11 14:31:54 -07001999
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002000
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002001 def logReport(self, nodeIp, searchTerms, outputMode="s"):
2002 '''
2003 - accepts either a list or a string for "searchTerms" these
2004 terms will be searched for in the log and have their
2005 instances counted
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002006
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002007 - nodeIp is the ip of the node whos log is to be scanned
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002008
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002009 - output modes:
2010 "s" - Simple. Quiet output mode that just prints
2011 the occurances of each search term
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002012
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002013 "d" - Detailed. Prints occurances as well as the entire
2014 line for each of the last 5 occurances
2015 '''
2016 main.log.info("========================== Log Report ===========================\n")
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002017
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002018 if type(searchTerms) is str:
2019 searchTerms = [searchTerms]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002020
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002021 logLines = [ [" "] for i in range(len(searchTerms)) ]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002022
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002023 for term in range(len(searchTerms)):
2024 logLines[term][0] = searchTerms[term]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002025
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002026
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002027 for term in range(len(searchTerms)):
2028 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep " + searchTerms[term]
2029 self.handle.sendline(cmd)
2030 self.handle.expect(":~")
2031 before = (self.handle.before).splitlines()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002032
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002033 count = [searchTerms[term],0]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002034
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002035 for line in before:
2036 if searchTerms[term] in line and "grep" not in line:
2037 count[1] += 1
2038 if before.index(line) > ( len(before) - 7 ):
2039 logLines[term].append(line)
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002040
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002041 main.log.info( str(count[0]) + ": " + str(count[1]) )
2042 if term == len(searchTerms)-1:
2043 print("\n")
2044
2045 if outputMode != "s" and outputMode != "S":
2046 outputString = ""
2047 for i in logLines:
2048 outputString = i[0] + ": \n"
2049 for x in range(1,len(i)):
2050 outputString += ( i[x] + "\n" )
2051
2052 if outputString != (i[0] + ": \n"):
2053 main.log.info(outputString)
2054
2055 main.log.info("================================================================\n")
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002056
cameron@onlab.uscd4e8a22015-05-11 10:58:43 -07002057 def getOnosIpFromEnv(self):
2058
2059 import string
2060
2061 # returns a list of ip addresses for the onos nodes, will work with up to 7 nodes + OCN and OCI
2062 # returns in format [ 'x', OC1 ip, OC2 i... ect. ... , ONN ip ]
2063
2064 self.handle.sendline("env| grep OC")
2065 self.handle.expect(":~")
2066 rawOutput = self.handle.before
2067 print rawOutput
2068 print "-----------------------------"
2069 print repr(rawOutput)
2070 mpa = dict.fromkeys(range(32))
2071 translated = rawOutput.translate(mpa)
2072 print translated
2073
2074
2075 # create list with only the lines that have the needed IPs
2076 unparsedIps = []
2077
2078 # remove preceeding or trailing lines
2079 for line in rawOutput:
2080 if "OC" in line and "=" in line:
2081 unparsedIps.append(str(line))
2082
2083 # determine cluster size
2084 clusterCount = 0
2085 for line in unparsedIps:
2086 line = str(line)
2087 print line
2088 temp = line.replace("OC","")
2089 print("line index " + str(line.index("=")))
2090 OCindex = temp[0]
2091 for i in range(0, 7):
2092 if OCindex == str(i) and i > clusterCount:
2093 clusterCount == i
2094 print(clusterCount)
2095 # create list to hold ips such that OC1 is at list[1] and OCN and OCI are at the end (in that order)
2096 ONOSIps = ["x"] * (clusterCount + 3)
2097
2098 # populate list
2099 for line in unparsedIps:
2100 main.log.info(line)##########
2101 temp = str(line.replace("OC",""))
2102 main.log.info(str(list(temp)))
2103 OCindex = temp[0]
2104 main.log.info(OCindex)############
2105 if OCindex == "N":
2106 ONOSIps[ clusterCount + 1 ] = temp.replace("N=","")
2107
2108 if OCindex == "I":
2109 ONOSIps[ clusterCount + 2 ] = temp.replace("I=","")
2110
2111 else:
2112 ONOSIps[ int(OCindex) ] = temp.replace((OCindex + "=") ,"")
2113
2114 # validate
2115 for x in ONOSIps:
2116 if ONOSIps.index(x) != 0 and x == "x":
2117 main.log.error("ENV READ FAILURE, MISSING DATA: \n\n" + str(ONOSIps) + "\n\n")
2118
2119 return ONOSIps
2120
kelvin-onlab0a28a742015-05-18 16:03:13 -07002121
cameron@onlab.usc10e22c2015-05-13 13:07:28 -07002122 def onosErrorLog(self, nodeIp):
2123
2124 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep WARN"
2125 self.handle.sendline(cmd)
2126 self.handle.expect(":~")
2127 before = (self.handle.before).splitlines()
kelvin-onlab0a28a742015-05-18 16:03:13 -07002128
cameron@onlab.usc10e22c2015-05-13 13:07:28 -07002129 warnings = []
2130
2131 for line in before:
2132 if "WARN" in line and "grep" not in line:
2133 warnings.append(line)
2134 main.warnings[main.warnings[0]+1] = line
2135 main.warnings[0] += 1
2136 if main.warnings[0] >= 10:
2137 main.warnings[0] = 0
2138
2139 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep ERROR"
2140 self.handle.sendline(cmd)
2141 self.handle.expect(":~")
2142 before = (self.handle.before).splitlines()
2143
2144 errors = []
2145
2146 for line in before:
2147 if "ERROR" in line and "grep" not in line:
2148 errors.append(line)
2149 main.errors[main.errors[0]+1] = line
2150 main.errors[0] += 1
2151 if main.errors[0] >= 10:
2152 main.errors[0] = 0
2153
2154 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep Exept"
2155 self.handle.sendline(cmd)
2156 self.handle.expect(":~")
2157 before = (self.handle.before).splitlines()
2158
2159 exceptions = []
2160
2161 for line in before:
2162 if "Except" in line and "grep" not in line:
2163 exceptions.append(line)
2164 main.exceptions[main.errors[0]+1] = line
2165 main.exceptions[0] += 1
2166 if main.exceptions[0] >= 10:
2167 main.exceptions[0] = 0
2168
2169
2170
2171 ################################################################
2172
2173 msg1 = "WARNINGS: \n"
2174 for i in main.warnings:
2175 if type(i) is not int:
2176 msg1 += ( i + "\n")
2177
2178 msg2 = "ERRORS: \n"
2179 for i in main.errors:
2180 if type(i) is not int:
2181 msg2 += ( i + "\n")
2182
2183 msg3 = "EXCEPTIONS: \n"
2184 for i in main.exceptions:
2185 if type(i) is not int:
2186 msg3 += ( i + "\n")
2187
2188 main.log.info("===============================================================\n")
2189 main.log.info( "Warnings: " + str(len(warnings)))
2190 main.log.info( "Errors: " + str(len(errors)))
2191 main.log.info( "Exceptions: " + str(len(exceptions)))
2192 if len(warnings) > 0:
2193 main.log.info(msg1)
2194 if len(errors) > 0:
2195 main.log.info(msg2)
2196 if len(exceptions) > 0:
2197 main.log.info(msg3)
2198 main.log.info("===============================================================\n")