blob: 261bd036f708dd4f4ed142e5beef378dae0ac142 [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 == "":
kelvin-onlaba1484582015-02-02 15:46:20 -080051 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:
93 main.log.exception( "Exception in discconect of " + self.name )
94 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',
180 'ONOS\$',
kelvin8ec71442015-01-15 16:57:00 -0800181 pexpect.TIMEOUT ], timeout=600 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400182 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800183 main.log.error( self.name + ":There is insufficient memory \
184 for the Java Runtime Environment to continue." )
185 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400186 main.cleanup()
187 main.exit()
188 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800189 main.log.error( self.name + ": Build failure!" )
190 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400191 main.cleanup()
192 main.exit()
193 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800194 main.log.info( self.name + ": Build success!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400195 elif i == 3:
kelvin8ec71442015-01-15 16:57:00 -0800196 main.log.info( self.name + ": Build complete" )
197 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400198 for line in self.handle.before.splitlines():
199 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800200 main.log.info( line )
201 self.handle.sendline( "" )
202 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400203 return main.TRUE
204 elif i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800205 main.log.error(
206 self.name +
207 ": mvn clean install TIMEOUT!" )
208 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400209 main.cleanup()
210 main.exit()
211 else:
Jon Hall274b6642015-02-17 11:57:17 -0800212 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800213 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800214 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400215 main.cleanup()
216 main.exit()
217 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800218 main.log.error( self.name + ": EOF exception found" )
219 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400220 main.cleanup()
221 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800222 except Exception:
223 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400224 main.cleanup()
225 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400226
Jon Hall61282e32015-03-19 11:34:11 -0700227 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800228 """
Jon Hallacabffd2014-10-09 12:36:53 -0400229 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800230
Jon Hall61282e32015-03-19 11:34:11 -0700231 If the fastForward boolean is set to true, only git pulls that can
232 be fast forwarded will be performed. IE if you have not local commits
233 in your branch.
234
Jon Hallacabffd2014-10-09 12:36:53 -0400235 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800236 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400237 for the purpose of pulling from other nodes if necessary.
238
Jon Hall47a93fb2015-01-06 16:46:06 -0800239 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400240 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800241 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400242 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400243
kelvin8ec71442015-01-15 16:57:00 -0800244 """
Jon Hallacabffd2014-10-09 12:36:53 -0400245 try:
kelvin8ec71442015-01-15 16:57:00 -0800246 # main.log.info( self.name + ": Stopping ONOS" )
247 # self.stop()
248 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800249 self.handle.expect( self.home + "\$" )
Jon Hall61282e32015-03-19 11:34:11 -0700250 cmd = "git pull"
251 if comp1 != "":
252 cmd += ' ' + comp1
253 if fastForward:
254 cmd += ' ' + " --ff-only"
255 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800256 i = self.handle.expect(
257 [
258 'fatal',
259 'Username\sfor\s(.*):\s',
260 '\sfile(s*) changed,\s',
261 'Already up-to-date',
262 'Aborting',
263 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800264 'You asked me to pull without telling me which branch you',
265 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700266 'Please enter a commit message to explain why this merge',
267 'Found a swap file by the name',
268 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800269 pexpect.TIMEOUT ],
270 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800271 # debug
kelvin-onlabd3b64892015-01-20 13:26:24 -0800272 # main.log.report( self.name +": DEBUG: \n"+
273 # "git pull response: " +
274 # str( self.handle.before ) + str( self.handle.after ) )
kelvin8ec71442015-01-15 16:57:00 -0800275 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700276 main.log.error( self.name + ": Git pull had some issue" )
277 output = self.handle.after
278 self.handle.expect( '\$' )
279 output += self.handle.before
280 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400281 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800282 elif i == 1:
283 main.log.error(
284 self.name +
285 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400286 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800287 elif i == 2:
288 main.log.info(
289 self.name +
290 ": Git Pull - pulling repository now" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800291 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800292 # So that only when git pull is done, we do mvn clean compile
293 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800294 elif i == 3:
295 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800296 return i
kelvin8ec71442015-01-15 16:57:00 -0800297 elif i == 4:
298 main.log.info(
299 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800300 ": Git Pull - Aborting..." +
301 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400302 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800303 elif i == 5:
304 main.log.info(
305 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800306 ": Git Pull - You are not currently " +
307 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400308 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800309 elif i == 6:
310 main.log.info(
311 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800312 ": Git Pull - You have not configured an upstream " +
313 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400314 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800315 elif i == 7:
316 main.log.info(
317 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800318 ": Git Pull - Pull is not possible because " +
319 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400320 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800321 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700322 # NOTE: abandoning test since we can't reliably handle this
323 # there could be different default text editors and we
324 # also don't know if we actually want to make the commit
325 main.log.error( "Git pull resulted in a merge commit message" +
326 ". Exiting test!" )
327 main.cleanup()
328 main.exit()
329 elif i == 9: # Merge commit message but swap file exists
330 main.log.error( "Git pull resulted in a merge commit message" +
331 " but a swap file exists." )
332 try:
333 self.handle.send( 'A' ) # Abort
334 self.handle.expect( "\$" )
335 return main.ERROR
336 except Exception:
337 main.log.exception( "Couldn't exit editor prompt!")
338 main.cleanup()
339 main.exit()
340 elif i == 10: # In the middle of a merge commit
341 main.log.error( "Git branch is in the middle of a merge. " )
342 main.log.warn( self.handle.before + self.handle.after )
343 return main.ERROR
344 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800345 main.log.error( self.name + ": Git Pull - TIMEOUT" )
346 main.log.error(
347 self.name + " Response was: " + str(
348 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400349 return main.ERROR
350 else:
kelvin8ec71442015-01-15 16:57:00 -0800351 main.log.error(
352 self.name +
353 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400354 return main.ERROR
355 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800356 main.log.error( self.name + ": EOF exception found" )
357 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400358 main.cleanup()
359 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800360 except Exception:
361 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400362 main.cleanup()
363 main.exit()
364
kelvin-onlabd3b64892015-01-20 13:26:24 -0800365 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800366 """
Jon Hallacabffd2014-10-09 12:36:53 -0400367 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800368
Jon Hallacabffd2014-10-09 12:36:53 -0400369 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800370 If used as gitCheckout( "branch" ) it will do git checkout
371 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400372
373 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800374 branch of the ONOS repository. If it has any problems, it will return
375 main.ERROR.
376 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400377 successful then the function will return main.TRUE.
378
kelvin8ec71442015-01-15 16:57:00 -0800379 """
Jon Hallacabffd2014-10-09 12:36:53 -0400380 try:
kelvin8ec71442015-01-15 16:57:00 -0800381 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800382 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800383 main.log.info( self.name +
384 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800385 cmd = "git checkout " + branch
386 self.handle.sendline( cmd )
387 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800388 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800389 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700390 'Username for (.*): ',
391 'Already on \'',
392 'Switched to branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800393 pexpect.TIMEOUT,
394 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800395 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800396 'error: you need to resolve your current index first',
397 "You are in 'detached HEAD' state.",
398 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800399 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800400 if i == 0:
401 main.log.error(
402 self.name +
403 ": Git checkout had some issue..." )
404 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400405 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800406 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800407 main.log.error(
408 self.name +
409 ": Git checkout asking for username." +
410 " Please configure your local git repository to be able " +
411 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800412 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400413 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800414 elif i == 2:
415 main.log.info(
416 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800417 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800418 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800419 # main.log.info( "DEBUG: after checkout cmd = "+
420 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400421 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800422 elif i == 3:
423 main.log.info(
424 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800425 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800426 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800427 # main.log.info( "DEBUG: after checkout cmd = "+
428 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400429 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800430 elif i == 4:
431 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
432 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800433 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400434 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800435 elif i == 5:
436 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800437 main.log.error(
438 self.name +
439 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800440 "Your local changes to the following files would" +
441 " be overwritten by checkout:" +
442 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800443 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500444 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800445 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800446 main.log.error(
447 self.name +
448 ": Git checkout error: \n" +
449 "You need to resolve your current index first:" +
450 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800451 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500452 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800453 elif i == 7:
454 main.log.info(
455 self.name +
456 ": Git checkout " + str( branch ) +
457 " - You are in 'detached HEAD' state. HEAD is now at " +
458 str( branch ) )
459 self.handle.expect( self.home + "\$" )
460 return main.TRUE
461 elif i == 8: # Already in detached HEAD on the specified commit
462 main.log.info(
463 self.name +
464 ": Git Checkout %s : Already on commit" % branch )
465 self.handle.expect( self.home + "\$" )
466 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400467 else:
kelvin8ec71442015-01-15 16:57:00 -0800468 main.log.error(
469 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800470 ": Git Checkout - Unexpected response, " +
471 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800472 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400473 return main.ERROR
474
475 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800476 main.log.error( self.name + ": EOF exception found" )
477 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400478 main.cleanup()
479 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800480 except Exception:
481 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400482 main.cleanup()
483 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400484
kelvin-onlabd3b64892015-01-20 13:26:24 -0800485 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800486 """
Jon Hall274b6642015-02-17 11:57:17 -0800487 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800488 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800489 """
Jon Hall45ec0922014-10-10 19:33:49 -0400490 try:
kelvin8ec71442015-01-15 16:57:00 -0800491 self.handle.sendline( "" )
492 self.handle.expect( "\$" )
493 self.handle.sendline(
494 "cd " +
495 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800496 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
497 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800498 # NOTE: for some reason there are backspaces inserted in this
499 # phrase when run from Jenkins on some tests
500 self.handle.expect( "never" )
501 self.handle.expect( "\$" )
502 response = ( self.name + ": \n" + str(
503 self.handle.before + self.handle.after ) )
504 self.handle.sendline( "cd " + self.home )
505 self.handle.expect( "\$" )
506 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400507 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500508 print line
509 if report:
kelvin8ec71442015-01-15 16:57:00 -0800510 for line in lines[ 2:-1 ]:
511 # Bracket replacement is for Wiki-compliant
512 # formatting. '<' or '>' are interpreted
513 # as xml specific tags that cause errors
514 line = line.replace( "<", "[" )
515 line = line.replace( ">", "]" )
516 main.log.report( "\t" + line )
517 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400518 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800519 main.log.error( self.name + ": EOF exception found" )
520 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400521 main.cleanup()
522 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800523 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800524 main.log.error( self.name + ": TIMEOUT exception found" )
525 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800526 main.cleanup()
527 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800528 except Exception:
529 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400530 main.cleanup()
531 main.exit()
532
kelvin-onlabd3b64892015-01-20 13:26:24 -0800533 def createCellFile( self, benchIp, fileName, mnIpAddrs,
Jon Hallefbd9792015-03-05 16:11:36 -0800534 extraFeatureString, *onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800535 """
andrewonlab94282092014-10-10 13:00:11 -0400536 Creates a cell file based on arguments
537 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800538 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400539 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800540 * File name of the cell file ( fileName )
541 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800542 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400543 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800544 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400545 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800546
andrewonlab94282092014-10-10 13:00:11 -0400547 NOTE: Assumes cells are located at:
548 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800549 """
550 # Variable initialization
kelvin-onlabd3b64892015-01-20 13:26:24 -0800551 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800552 # We want to create the cell file in the dependencies directory
553 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800554 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800555 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800556 cellFile = open( tempDirectory + fileName, 'w+' )
kelvin8ec71442015-01-15 16:57:00 -0800557
558 # Feature string is hardcoded environment variables
559 # That you may wish to use by default on startup.
560 # Note that you may not want certain features listed
561 # on here.
jenkinsbd0e7532015-02-10 14:32:54 -0800562 coreFeatureString = "export ONOS_FEATURES=" + extraFeatureString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800563 mnString = "export OCN="
564 onosString = "export OC"
565 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800566
kelvin-onlabd3b64892015-01-20 13:26:24 -0800567 # Create ONOSNIC ip address prefix
568 tempOnosIp = onosIpAddrs[ 0 ]
569 tempList = []
570 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800571 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800572 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800573 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800574 nicAddr = ".".join( tempList ) + ".*"
575 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400576
577 try:
kelvin8ec71442015-01-15 16:57:00 -0800578 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800579 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400580
kelvin-onlabd3b64892015-01-20 13:26:24 -0800581 for arg in onosIpAddrs:
582 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800583 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400584 # export OC1="10.128.20.11"
585 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800586 cellFile.write( onosString + str( tempCount ) +
587 "=" + "\"" + arg + "\"" + "\n" )
588 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800589
kelvin-onlabd3b64892015-01-20 13:26:24 -0800590 cellFile.write( mnString + "\"" + mnIpAddrs + "\"" + "\n" )
591 cellFile.write( coreFeatureString + "\n" )
592 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400593
kelvin8ec71442015-01-15 16:57:00 -0800594 # We use os.system to send the command to TestON cluster
595 # to account for the case in which TestON is not located
596 # on the same cluster as the ONOS bench
597 # Note that even if TestON is located on the same cluster
598 # as ONOS bench, you must setup passwordless ssh
599 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800600 os.system( "scp " + tempDirectory + fileName +
601 " admin@" + benchIp + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400602
andrewonlab2a6c9342014-10-16 13:40:15 -0400603 return main.TRUE
604
andrewonlab94282092014-10-10 13:00:11 -0400605 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800606 main.log.error( self.name + ": EOF exception found" )
607 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400608 main.cleanup()
609 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800610 except Exception:
611 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400612 main.cleanup()
613 main.exit()
614
kelvin-onlabd3b64892015-01-20 13:26:24 -0800615 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800616 """
andrewonlab95ca1462014-10-09 14:04:24 -0400617 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800618 """
andrewonlab95ca1462014-10-09 14:04:24 -0400619 try:
620 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800621 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400622 main.cleanup()
623 main.exit()
624 else:
kelvin8ec71442015-01-15 16:57:00 -0800625 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800626 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800627 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400628 # and that this driver will have to change accordingly
Cameron Franke9c94fb02015-01-21 10:20:20 -0800629 self.handle.expect(str(cellname))
kelvin-onlabd3b64892015-01-20 13:26:24 -0800630 handleBefore = self.handle.before
631 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800632 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800633 self.handle.sendline("")
634 self.handle.expect("\$")
kelvin-onlabd3b64892015-01-20 13:26:24 -0800635 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400636
kelvin-onlabd3b64892015-01-20 13:26:24 -0800637 main.log.info( "Cell call returned: " + handleBefore +
638 handleAfter + handleMore )
andrewonlab95ca1462014-10-09 14:04:24 -0400639
640 return main.TRUE
641
642 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800643 main.log.error( self.name + ": EOF exception found" )
644 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400645 main.cleanup()
646 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800647 except Exception:
648 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400649 main.cleanup()
650 main.exit()
651
kelvin-onlabd3b64892015-01-20 13:26:24 -0800652 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800653 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400654 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800655 """
656 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400657
andrewonlabc03bf6c2014-10-09 14:56:18 -0400658 try:
kelvin8ec71442015-01-15 16:57:00 -0800659 # Clean handle by sending empty and expecting $
660 self.handle.sendline( "" )
661 self.handle.expect( "\$" )
662 self.handle.sendline( "onos-verify-cell" )
663 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800664 handleBefore = self.handle.before
665 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800666 # Get the rest of the handle
667 self.handle.sendline( "" )
668 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800669 handleMore = self.handle.before
andrewonlabc03bf6c2014-10-09 14:56:18 -0400670
kelvin-onlabd3b64892015-01-20 13:26:24 -0800671 main.log.info( "Verify cell returned: " + handleBefore +
672 handleAfter + handleMore )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400673
674 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800675 except pexpect.ExceptionPexpect as e:
676 main.log.error( self.name + ": Pexpect exception found of type " +
677 str( type( e ) ) )
678 main.log.error ( e.get_trace() )
kelvin8ec71442015-01-15 16:57:00 -0800679 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400680 main.cleanup()
681 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800682 except Exception:
683 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400684 main.cleanup()
685 main.exit()
686
kelvin-onlabd3b64892015-01-20 13:26:24 -0800687 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800688 """
andrewonlab05e362f2014-10-10 00:40:57 -0400689 Uses 'onos' command to send various ONOS CLI arguments.
690 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800691 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400692 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800693
694 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400695 CLI commands for ONOS. Try to use this function first
696 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800697 function.
698 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400699 by starting onos, and typing in 'onos' to enter the
700 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800701 available commands.
702 """
andrewonlab05e362f2014-10-10 00:40:57 -0400703 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800704 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800705 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400706 return main.FALSE
707 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800708 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400709 return main.FALSE
710
kelvin8ec71442015-01-15 16:57:00 -0800711 cmdstr = str( cmdstr )
712 self.handle.sendline( "" )
713 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400714
kelvin-onlabd3b64892015-01-20 13:26:24 -0800715 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800716 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400717
kelvin-onlabd3b64892015-01-20 13:26:24 -0800718 handleBefore = self.handle.before
Shreya Shaha73aaad2014-10-27 18:03:09 -0400719 print "handle_before = ", self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800720 # handleAfter = str( self.handle.after )
Jon Hall47a93fb2015-01-06 16:46:06 -0800721
kelvin8ec71442015-01-15 16:57:00 -0800722 # self.handle.sendline( "" )
723 # self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800724 # handleMore = str( self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400725
kelvin8ec71442015-01-15 16:57:00 -0800726 main.log.info( "Command sent successfully" )
andrewonlab05e362f2014-10-10 00:40:57 -0400727
kelvin8ec71442015-01-15 16:57:00 -0800728 # Obtain return handle that consists of result from
729 # the onos command. The string may need to be
730 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800731 # returnString = handleBefore + handleAfter
732 returnString = handleBefore
733 print "return_string = ", returnString
734 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400735
736 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800737 main.log.error( self.name + ": EOF exception found" )
738 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400739 main.cleanup()
740 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800741 except Exception:
742 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400743 main.cleanup()
744 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400745
kelvin-onlabd3b64892015-01-20 13:26:24 -0800746 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800747 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400748 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800749 If -f option is provided, it also forces an uninstall.
750 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400751 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800752 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400753 files to certain onos nodes
754
755 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800756 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400757 try:
andrewonlab114768a2014-11-14 12:44:44 -0500758 if options:
kelvin8ec71442015-01-15 16:57:00 -0800759 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500760 else:
kelvin8ec71442015-01-15 16:57:00 -0800761 self.handle.sendline( "onos-install " + node )
762 self.handle.expect( "onos-install " )
763 # NOTE: this timeout may need to change depending on the network
764 # and size of ONOS
765 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800766 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800767 "ONOS\sis\salready\sinstalled",
768 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400769
Jon Hall7993bfc2014-10-09 16:30:14 -0400770 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800771 main.log.warn( "Network is unreachable" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400772 return main.FALSE
773 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800774 main.log.info(
775 "ONOS was installed on " +
776 node +
777 " and started" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400778 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500779 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800780 main.log.info( "ONOS is already installed on " + node )
andrewonlabd9a73a72014-11-14 17:28:21 -0500781 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800782 elif i == 3:
783 main.log.info(
784 "Installation of ONOS on " +
785 node +
786 " timed out" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400787 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400788
789 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800790 main.log.error( self.name + ": EOF exception found" )
791 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400792 main.cleanup()
793 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800794 except Exception:
795 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400796 main.cleanup()
797 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400798
kelvin-onlabd3b64892015-01-20 13:26:24 -0800799 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800800 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400801 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400802 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800803 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400804 try:
kelvin8ec71442015-01-15 16:57:00 -0800805 self.handle.sendline( "" )
806 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800807 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800808 " start" )
809 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -0400810 "Job\sis\salready\srunning",
811 "start/running",
812 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800813 pexpect.TIMEOUT ], timeout=120 )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400814
815 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800816 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400817 return main.TRUE
818 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800819 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400820 return main.TRUE
821 else:
kelvin8ec71442015-01-15 16:57:00 -0800822 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400823 main.cleanup()
824 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400825 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800826 main.log.error( self.name + ": EOF exception found" )
827 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400828 main.cleanup()
829 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800830 except Exception:
831 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400832 main.cleanup()
833 main.exit()
834
kelvin-onlabd3b64892015-01-20 13:26:24 -0800835 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800836 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400837 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400838 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800839 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400840 try:
kelvin8ec71442015-01-15 16:57:00 -0800841 self.handle.sendline( "" )
842 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800843 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800844 " stop" )
845 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -0400846 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -0700847 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -0400848 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800849 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2b30bd32014-10-09 16:48:55 -0400850
851 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800852 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400853 return main.TRUE
854 elif i == 1:
Jon Hall65844a32015-03-09 19:09:37 -0700855 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800856 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -0400857 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -0700858 elif i == 2:
859 main.log.warn( "ONOS wasn't running" )
860 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -0400861 else:
kelvin8ec71442015-01-15 16:57:00 -0800862 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400863 return main.FALSE
864
865 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800866 main.log.error( self.name + ": EOF exception found" )
867 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -0400868 main.cleanup()
869 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800870 except Exception:
871 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400872 main.cleanup()
873 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800874
kelvin-onlabd3b64892015-01-20 13:26:24 -0800875 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -0800876 """
andrewonlabc8d47972014-10-09 16:52:36 -0400877 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -0800878 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -0400879 if needed
kelvin8ec71442015-01-15 16:57:00 -0800880 """
andrewonlabc8d47972014-10-09 16:52:36 -0400881 try:
kelvin8ec71442015-01-15 16:57:00 -0800882 self.handle.sendline( "" )
883 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800884 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800885 self.handle.expect( "\$" )
andrewonlabc8d47972014-10-09 16:52:36 -0400886
kelvin-onlabd3b64892015-01-20 13:26:24 -0800887 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
andrewonlab9dfd2082014-11-13 17:44:03 -0500888
kelvin8ec71442015-01-15 16:57:00 -0800889 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -0400890 return main.TRUE
891
892 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800893 main.log.error( self.name + ": EOF exception found" )
894 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -0400895 main.cleanup()
896 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800897 except Exception:
898 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -0400899 main.cleanup()
900 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400901
kelvin-onlabd3b64892015-01-20 13:26:24 -0800902 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800903 """
andrewonlabaedc8332014-12-04 12:43:03 -0500904 Issues the command 'onos-die <node-ip>'
905 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -0800906 """
andrewonlabaedc8332014-12-04 12:43:03 -0500907 try:
kelvin8ec71442015-01-15 16:57:00 -0800908 self.handle.sendline( "" )
909 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800910 cmdStr = "onos-kill " + str( nodeIp )
911 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800912 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -0500913 "Killing\sONOS",
914 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -0800915 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -0500916 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800917 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800918 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -0500919 return main.TRUE
920 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800921 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -0500922 return main.FALSE
923 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800924 main.log.error( self.name + ": EOF exception found" )
925 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -0500926 main.cleanup()
927 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800928 except Exception:
929 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -0500930 main.cleanup()
931 main.exit()
932
kelvin-onlabd3b64892015-01-20 13:26:24 -0800933 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800934 """
andrewonlabe8e56fd2014-10-09 17:12:44 -0400935 Calls the command: 'onos-kill [<node-ip>]'
936 "Remotely, and unceremoniously kills the ONOS instance running on
937 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -0800938 """
andrewonlabe8e56fd2014-10-09 17:12:44 -0400939 try:
kelvin8ec71442015-01-15 16:57:00 -0800940 self.handle.sendline( "" )
941 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800942 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800943 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -0400944 "\$",
945 "No\sroute\sto\shost",
946 "password:",
kelvin8ec71442015-01-15 16:57:00 -0800947 pexpect.TIMEOUT ], timeout=20 )
948
andrewonlabe8e56fd2014-10-09 17:12:44 -0400949 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800950 main.log.info(
951 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800952 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400953 return main.TRUE
954 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800955 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400956 return main.FALSE
957 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800958 main.log.info(
959 "Passwordless login for host: " +
960 str( nodeIp ) +
961 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400962 return main.FALSE
963 else:
Jon Hallefbd9792015-03-05 16:11:36 -0800964 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400965 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -0800966
andrewonlabe8e56fd2014-10-09 17:12:44 -0400967 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800968 main.log.error( self.name + ": EOF exception found" )
969 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400970 main.cleanup()
971 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800972 except Exception:
973 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400974 main.cleanup()
975 main.exit()
976
kelvin-onlabd3b64892015-01-20 13:26:24 -0800977 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -0800978 """
andrewonlab19fbdca2014-11-14 12:55:59 -0500979 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -0500980 a cleaner environment.
981
andrewonlab19fbdca2014-11-14 12:55:59 -0500982 Description:
Jon Hallfcc88622014-11-25 13:09:54 -0500983 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -0500984 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -0800985 """
andrewonlab19fbdca2014-11-14 12:55:59 -0500986 try:
kelvin8ec71442015-01-15 16:57:00 -0800987 self.handle.sendline( "" )
988 self.handle.expect( "\$" )
989 self.handle.sendline( "onos-remove-raft-logs" )
990 # Sometimes this command hangs
991 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
992 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -0500993 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800994 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
995 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -0500996 if i == 1:
997 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -0800998 self.handle.sendline( "" )
999 self.handle.expect( "\$" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001000 return main.TRUE
1001
1002 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001003 main.log.error( self.name + ": EOF exception found" )
1004 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001005 main.cleanup()
1006 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001007 except Exception:
1008 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001009 main.cleanup()
1010 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001011
kelvin-onlabd3b64892015-01-20 13:26:24 -08001012 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001013 """
1014 Calls the command 'onos-start-network [ <mininet-topo> ]
1015 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001016 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001017 cell."
andrewonlab94282092014-10-10 13:00:11 -04001018 * Specify mininet topology file name for mntopo
1019 * Topo files should be placed at:
1020 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001021
andrewonlab94282092014-10-10 13:00:11 -04001022 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001023 """
andrewonlab94282092014-10-10 13:00:11 -04001024 try:
1025 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001026 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001027 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001028
kelvin8ec71442015-01-15 16:57:00 -08001029 mntopo = str( mntopo )
1030 self.handle.sendline( "" )
1031 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001032
kelvin8ec71442015-01-15 16:57:00 -08001033 self.handle.sendline( "onos-start-network " + mntopo )
1034 self.handle.expect( "mininet>" )
1035 main.log.info( "Network started, entered mininet prompt" )
1036
1037 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001038
1039 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001040 main.log.error( self.name + ": EOF exception found" )
1041 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001042 main.cleanup()
1043 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001044 except Exception:
1045 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001046 main.cleanup()
1047 main.exit()
1048
Cameron Franke9c94fb02015-01-21 10:20:20 -08001049 def isup(self, node = "", timeout = 120):
kelvin8ec71442015-01-15 16:57:00 -08001050 """
1051 Run's onos-wait-for-start which only returns once ONOS is at run
Cameron Franke9c94fb02015-01-21 10:20:20 -08001052 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001053
Jon Hall7993bfc2014-10-09 16:30:14 -04001054 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001055 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001056 try:
Cameron Franke9c94fb02015-01-21 10:20:20 -08001057 self.handle.sendline("onos-wait-for-start " + node )
1058 self.handle.expect("onos-wait-for-start")
kelvin8ec71442015-01-15 16:57:00 -08001059 # NOTE: this timeout is arbitrary"
Cameron Franke9c94fb02015-01-21 10:20:20 -08001060 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001061 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001062 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001063 return main.TRUE
1064 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001065 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001066 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001067 main.log.error( "ONOS has not started yet" )
1068 self.handle.send( "\x03" ) # Control-C
1069 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001070 return main.FALSE
1071 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001072 main.log.error( self.name + ": EOF exception found" )
1073 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001074 main.cleanup()
1075 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001076 except Exception:
1077 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001078 main.cleanup()
1079 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001080
kelvin-onlabd3b64892015-01-20 13:26:24 -08001081 def pushTestIntentsShell(
1082 self,
1083 dpidSrc,
1084 dpidDst,
1085 numIntents,
1086 dirFile,
1087 onosIp,
1088 numMult="",
1089 appId="",
1090 report=True,
1091 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001092 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001093 Description:
kelvin8ec71442015-01-15 16:57:00 -08001094 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001095 better parallelize the results than the CLI
1096 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001097 * dpidSrc: specify source dpid
1098 * dpidDst: specify destination dpid
1099 * numIntents: specify number of intents to push
1100 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001101 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001102 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001103 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001104 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001105 """
1106 try:
1107 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001108 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001109 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001110 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001111 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001112 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001113
kelvin-onlabd3b64892015-01-20 13:26:24 -08001114 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1115 if not numMult:
1116 addIntents = addDpid + " " + str( numIntents )
1117 elif numMult:
1118 addIntents = addDpid + " " + str( numIntents ) + " " +\
1119 str( numMult )
1120 if appId:
1121 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001122 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001123 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001124
andrewonlabaedc8332014-12-04 12:43:03 -05001125 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001126 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001127 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001128 sendCmd = addApp + " &"
1129 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001130
kelvin-onlabd3b64892015-01-20 13:26:24 -08001131 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001132
1133 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001134 main.log.error( self.name + ": EOF exception found" )
1135 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001136 main.cleanup()
1137 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001138 except Exception:
1139 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001140 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001141 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001142
kelvin-onlabd3b64892015-01-20 13:26:24 -08001143 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001144 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001145 Definition:
1146 Loads a json topology output
1147 Return:
1148 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -08001149 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001150 import json
Jon Hall77f53ce2014-10-13 18:02:06 -04001151 try:
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001152 # either onos:topology or 'topology' will work in CLI
1153 topology = json.loads(topologyOutput)
1154 print topology
Jon Hall77f53ce2014-10-13 18:02:06 -04001155 return topology
1156 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001157 main.log.error( self.name + ": EOF exception found" )
1158 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001159 main.cleanup()
1160 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001161 except Exception:
1162 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001163 main.cleanup()
1164 main.exit()
1165
kelvin-onlabd3b64892015-01-20 13:26:24 -08001166 def checkStatus(
1167 self,
1168 topologyResult,
1169 numoswitch,
1170 numolink,
1171 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001172 """
Jon Hallefbd9792015-03-05 16:11:36 -08001173 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001174 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001175 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08001176
Jon Hall77f53ce2014-10-13 18:02:06 -04001177 Params: ip = ip used for the onos cli
1178 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001179 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001180 logLevel = level to log to.
1181 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001182
1183
kelvin-onlabd3b64892015-01-20 13:26:24 -08001184 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001185
Jon Hallefbd9792015-03-05 16:11:36 -08001186 Returns: main.TRUE if the number of switches and links are correct,
1187 main.FALSE if the number of switches and links is incorrect,
Jon Hall77f53ce2014-10-13 18:02:06 -04001188 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001189 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001190 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001191 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001192 if topology == {}:
1193 return main.ERROR
1194 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001195 # Is the number of switches is what we expected
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001196 devices = topology.get( 'deviceCount', False )
1197 links = topology.get( 'linkCount', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001198 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001199 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001200 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001201 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001202 linkCheck = ( int( links ) == int( numolink ) )
Jon Hallefbd9792015-03-05 16:11:36 -08001203 if switchCheck and linkCheck:
kelvin8ec71442015-01-15 16:57:00 -08001204 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001205 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001206 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001207 result = main.TRUE
1208 else:
1209 output = output + \
Jon Hall274b6642015-02-17 11:57:17 -08001210 "The number of links and switches does not match " + \
1211 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001212 result = main.FALSE
Jon Hallefbd9792015-03-05 16:11:36 -08001213 output = output + "\n ONOS sees %i devices" % int( devices )
1214 output = output + " (%i expected) " % int( numoswitch )
Jon Hall274b6642015-02-17 11:57:17 -08001215 output = output + "and %i links " % int( links )
1216 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001217 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001218 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001219 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001220 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001221 else:
kelvin8ec71442015-01-15 16:57:00 -08001222 main.log.info( output )
1223 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001224 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001225 main.log.error( self.name + ": EOF exception found" )
1226 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001227 main.cleanup()
1228 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001229 except Exception:
1230 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001231 main.cleanup()
1232 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001233
kelvin-onlabd3b64892015-01-20 13:26:24 -08001234 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001235 """
andrewonlab970399c2014-11-07 13:09:32 -05001236 Capture all packet activity and store in specified
1237 directory/file
1238
1239 Required:
1240 * interface: interface to capture
1241 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001242 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001243 try:
1244 self.handle.sendline( "" )
1245 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001246
Jon Hallfebb1c72015-03-05 13:30:09 -08001247 self.handle.sendline( "tshark -i " + str( interface ) +
1248 " -t e -w " + str( dirFile ) + " &" )
1249 self.handle.sendline( "\r" )
1250 self.handle.expect( "Capturing on" )
1251 self.handle.sendline( "\r" )
1252 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001253
Jon Hallfebb1c72015-03-05 13:30:09 -08001254 main.log.info( "Tshark started capturing files on " +
1255 str( interface ) + " and saving to directory: " +
1256 str( dirFile ) )
1257 except pexpect.EOF:
1258 main.log.error( self.name + ": EOF exception found" )
1259 main.log.error( self.name + ": " + self.handle.before )
1260 main.cleanup()
1261 main.exit()
1262 except Exception:
1263 main.log.exception( self.name + ": Uncaught exception!" )
1264 main.cleanup()
1265 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001266
kelvin-onlabd3b64892015-01-20 13:26:24 -08001267 def runOnosTopoCfg( self, instanceName, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001268 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001269 On ONOS bench, run this command:
1270 ./~/ONOS/tools/test/bin/onos-topo-cfg $OC1 filename
1271 which starts the rest and copies
1272 the json file to the onos instance
kelvin8ec71442015-01-15 16:57:00 -08001273 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001274 try:
kelvin8ec71442015-01-15 16:57:00 -08001275 self.handle.sendline( "" )
1276 self.handle.expect( "\$" )
1277 self.handle.sendline( "cd ~/ONOS/tools/test/bin" )
1278 self.handle.expect( "/bin$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001279 cmd = "./onos-topo-cfg " + instanceName + " " + jsonFile
shahshreyae6c7cf42014-11-26 16:39:01 -08001280 print "cmd = ", cmd
kelvin8ec71442015-01-15 16:57:00 -08001281 self.handle.sendline( cmd )
1282 self.handle.expect( "\$" )
1283 self.handle.sendline( "cd ~" )
1284 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -08001285 return main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -08001286 except pexpect.EOF:
1287 main.log.error( self.name + ": EOF exception found" )
1288 main.log.error( self.name + ": " + self.handle.before )
1289 main.cleanup()
1290 main.exit()
1291 except Exception:
1292 main.log.exception( self.name + ": Uncaught exception!" )
1293 main.cleanup()
1294 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001295
kelvin-onlabd3b64892015-01-20 13:26:24 -08001296 def tsharkGrep( self, grep, directory, interface='eth0' ):
kelvin8ec71442015-01-15 16:57:00 -08001297 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001298 Required:
kelvin8ec71442015-01-15 16:57:00 -08001299 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001300 * directory to store results
1301 Optional:
1302 * interface - default: eth0
1303 Description:
1304 Uses tshark command to grep specific group of packets
1305 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001306 The timestamp is hardcoded to be in epoch
1307 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001308 try:
1309 self.handle.sendline( "" )
1310 self.handle.expect( "\$" )
1311 self.handle.sendline( "" )
1312 self.handle.sendline(
1313 "tshark -i " +
1314 str( interface ) +
1315 " -t e | grep --line-buffered \"" +
1316 str(grep) +
1317 "\" >" +
1318 directory +
1319 " &" )
1320 self.handle.sendline( "\r" )
1321 self.handle.expect( "Capturing on" )
1322 self.handle.sendline( "\r" )
1323 self.handle.expect( "\$" )
1324 except pexpect.EOF:
1325 main.log.error( self.name + ": EOF exception found" )
1326 main.log.error( self.name + ": " + self.handle.before )
1327 main.cleanup()
1328 main.exit()
1329 except Exception:
1330 main.log.exception( self.name + ": Uncaught exception!" )
1331 main.cleanup()
1332 main.exit()
1333
kelvin-onlabd3b64892015-01-20 13:26:24 -08001334 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001335 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001336 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001337 """
1338 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001339 try:
1340 self.execute( cmd="sudo rm /tmp/wireshark*" )
1341 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001342 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1343 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001344 self.handle.sendline( "" )
1345 main.log.info( "Tshark stopped" )
1346 except pexpect.EOF:
1347 main.log.error( self.name + ": EOF exception found" )
1348 main.log.error( self.name + ": " + self.handle.before )
1349 main.cleanup()
1350 main.exit()
1351 except Exception:
1352 main.log.exception( self.name + ": Uncaught exception!" )
1353 main.cleanup()
1354 main.exit()
1355
kelvin8ec71442015-01-15 16:57:00 -08001356 def ptpd( self, args ):
1357 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001358 Initiate ptp with user-specified args.
1359 Required:
1360 * args: specify string of args after command
1361 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001362 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001363 try:
kelvin8ec71442015-01-15 16:57:00 -08001364 self.handle.sendline( "sudo ptpd " + str( args ) )
1365 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001366 "Multiple",
1367 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001368 "\$" ] )
1369 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001370
andrewonlab0c38a4a2014-10-28 18:35:35 -04001371 if i == 0:
1372 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001373 main.log.info( "ptpd returned an error: " +
1374 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001375 return handle
1376 elif i == 1:
1377 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001378 main.log.error( "ptpd returned an error: " +
1379 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001380 return handle
1381 else:
1382 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001383
andrewonlab0c38a4a2014-10-28 18:35:35 -04001384 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001385 main.log.error( self.name + ": EOF exception found" )
1386 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001387 main.cleanup()
1388 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001389 except Exception:
1390 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001391 main.cleanup()
1392 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001393
kelvin-onlabd3b64892015-01-20 13:26:24 -08001394 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001395 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001396 """
1397 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001398 Current implementation of ONOS deletes its karaf
1399 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001400 you may want to use this function to capture
1401 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001402 Localtime will be attached to the filename
1403
1404 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001405 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001406 copy.
kelvin8ec71442015-01-15 16:57:00 -08001407 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001408 For copying multiple files, leave copyFileName
1409 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001410 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001411 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001412 ex ) /tmp/
1413 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001414 * copyFileName: If you want to rename the log
1415 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001416 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001417 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001418 try:
kelvin8ec71442015-01-15 16:57:00 -08001419 localtime = time.strftime( '%x %X' )
1420 localtime = localtime.replace( "/", "" )
1421 localtime = localtime.replace( " ", "_" )
1422 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001423 if destDir[ -1: ] != "/":
1424 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001425
kelvin-onlabd3b64892015-01-20 13:26:24 -08001426 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001427 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1428 str( destDir ) + str( copyFileName ) +
1429 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001430 self.handle.expect( "cp" )
1431 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001432 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001433 self.handle.sendline( "cp " + str( logToCopy ) +
1434 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001435 self.handle.expect( "cp" )
1436 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001437
kelvin8ec71442015-01-15 16:57:00 -08001438 return self.handle.before
1439
1440 except pexpect.EOF:
1441 main.log.error( "Copying files failed" )
1442 main.log.error( self.name + ": EOF exception found" )
1443 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001444 except Exception:
1445 main.log.exception( "Copying files failed" )
1446
kelvin-onlabd3b64892015-01-20 13:26:24 -08001447 def checkLogs( self, onosIp ):
kelvin8ec71442015-01-15 16:57:00 -08001448 """
Jon Hall94fd0472014-12-08 11:52:42 -08001449 runs onos-check-logs on the given onos node
1450 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001451 """
Jon Hall94fd0472014-12-08 11:52:42 -08001452 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001453 cmd = "onos-check-logs " + str( onosIp )
kelvin8ec71442015-01-15 16:57:00 -08001454 self.handle.sendline( cmd )
1455 self.handle.expect( cmd )
1456 self.handle.expect( "\$" )
Jon Hall94fd0472014-12-08 11:52:42 -08001457 response = self.handle.before
1458 return response
1459 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001460 main.log.error( "Lost ssh connection" )
1461 main.log.error( self.name + ": EOF exception found" )
1462 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001463 except Exception:
1464 main.log.exception( self.name + ": Uncaught exception!" )
1465 main.cleanup()
1466 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001467
kelvin-onlabd3b64892015-01-20 13:26:24 -08001468 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001469 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001470 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001471 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001472 try:
kelvin8ec71442015-01-15 16:57:00 -08001473 self.handle.sendline( "" )
1474 self.handle.expect( "\$" )
1475 self.handle.sendline( "onos-service " + str( node ) +
1476 " status" )
1477 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001478 "start/running",
1479 "stop/waiting",
kelvin8ec71442015-01-15 16:57:00 -08001480 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001481
1482 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001483 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001484 return main.TRUE
1485 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001486 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001487 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001488 main.cleanup()
1489 main.exit()
1490 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001491 main.log.error( self.name + ": EOF exception found" )
1492 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001493 main.cleanup()
1494 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001495 except Exception:
1496 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001497 main.cleanup()
1498 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001499
Jon Hall63604932015-02-26 17:09:50 -08001500 def setIpTables( self, ip, port='', action='add', packet_type='',
1501 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001502 """
Jon Hall21270ac2015-02-16 17:59:55 -08001503 Description:
1504 add or remove iptables rule to DROP (default) packets from
1505 specific IP and PORT
1506 Usage:
1507 * specify action ('add' or 'remove')
1508 when removing, pass in the same argument as you would add. It will
1509 delete that specific rule.
1510 * specify the ip to block
1511 * specify the destination port to block (defaults to all ports)
1512 * optional packet type to block (default tcp)
1513 * optional iptables rule (default DROP)
1514 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001515 * States boolean toggles adding all supported tcp states to the
1516 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001517 Returns:
1518 main.TRUE on success or
1519 main.FALSE if given invalid input or
1520 main.ERROR if there is an error in response from iptables
1521 WARNING:
1522 * This function uses root privilege iptables command which may result
1523 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001524 """
Jon Hall21270ac2015-02-16 17:59:55 -08001525 import time
1526
1527 # NOTE*********
1528 # The strict checking methods of this driver function is intentional
1529 # to discourage any misuse or error of iptables, which can cause
1530 # severe network errors
1531 # *************
1532
1533 # NOTE: Sleep needed to give some time for rule to be added and
1534 # registered to the instance. If you are calling this function
1535 # multiple times this sleep will prevent any errors.
1536 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001537 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001538 try:
1539 # input validation
1540 action_type = action.lower()
1541 rule = rule.upper()
1542 direction = direction.upper()
1543 if action_type != 'add' and action_type != 'remove':
1544 main.log.error( "Invalid action type. Use 'add' or "
1545 "'remove' table rule" )
1546 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1547 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1548 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1549 "'ACCEPT' or 'LOG' only." )
1550 if direction != 'INPUT' and direction != 'OUTPUT':
1551 # NOTE currently only supports rules INPUT and OUPTUT
1552 main.log.error( "Invalid rule. Valid directions are"
1553 " 'OUTPUT' or 'INPUT'" )
1554 return main.FALSE
1555 return main.FALSE
1556 return main.FALSE
1557 if action_type == 'add':
1558 # -A is the 'append' action of iptables
1559 actionFlag = '-A'
1560 elif action_type == 'remove':
1561 # -D is the 'delete' rule of iptables
1562 actionFlag = '-D'
1563 self.handle.sendline( "" )
1564 self.handle.expect( "\$" )
1565 cmd = "sudo iptables " + actionFlag + " " +\
1566 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001567 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001568 # " -p " + str( packet_type ) +\
1569 if packet_type:
1570 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001571 if port:
1572 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001573 if states:
1574 cmd += " -m state --state="
1575 #FIXME- Allow user to configure which states to block
1576 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001577 cmd += " -j " + str( rule )
1578
1579 self.handle.sendline( cmd )
1580 self.handle.expect( "\$" )
1581 main.log.warn( self.handle.before )
1582
1583 info_string = "On " + str( self.name )
1584 info_string += " " + str( action_type )
1585 info_string += " iptable rule [ "
1586 info_string += " IP: " + str( ip )
1587 info_string += " Port: " + str( port )
1588 info_string += " Rule: " + str( rule )
1589 info_string += " Direction: " + str( direction ) + " ]"
1590 main.log.info( info_string )
1591 return main.TRUE
1592 except pexpect.TIMEOUT:
1593 main.log.exception( self.name + ": Timeout exception in "
1594 "setIpTables function" )
1595 return main.ERROR
1596 except pexpect.EOF:
1597 main.log.error( self.name + ": EOF exception found" )
1598 main.log.error( self.name + ": " + self.handle.before )
1599 main.cleanup()
1600 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001601 except Exception:
1602 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001603 main.cleanup()
1604 main.exit()
1605
Jon Hall0468b042015-02-19 19:08:21 -08001606 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001607 """
Jon Hall0468b042015-02-19 19:08:21 -08001608 This method is used by STS to check the status of the controller
1609 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001610 """
Jon Hall0468b042015-02-19 19:08:21 -08001611 import re
1612 try:
1613 self.handle.sendline( "" )
1614 self.handle.expect( "\$" )
1615 self.handle.sendline( "cd " + self.home )
1616 self.handle.expect( "\$" )
1617 self.handle.sendline( "service onos status" )
1618 self.handle.expect( "\$" )
1619 response = self.handle.before
1620 if re.search( "onos start/running", response ):
1621 # onos start/running, process 10457
1622 return 'RUNNING'
1623 # FIXME: Implement this case
1624 # elif re.search( pattern, response ):
1625 # return 'STARTING'
1626 elif re.search( "onos stop/", response ):
1627 # onos stop/waiting
1628 # FIXME handle this differently?: onos stop/pre-stop
1629 return 'STOPPED'
1630 # FIXME: Implement this case
1631 # elif re.search( pattern, response ):
1632 # return 'FROZEN'
1633 else:
1634 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001635 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001636 main.log.warn( response )
1637 return 'ERROR', "Unknown response: %s" % response
1638 except pexpect.TIMEOUT:
1639 main.log.exception( self.name + ": Timeout exception in "
1640 "setIpTables function" )
1641 return 'ERROR', "Pexpect Timeout"
1642 except pexpect.EOF:
1643 main.log.error( self.name + ": EOF exception found" )
1644 main.log.error( self.name + ": " + self.handle.before )
1645 main.cleanup()
1646 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001647 except Exception:
1648 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001649 main.cleanup()
1650 main.exit()
1651
andrew@onlab.us3b087132015-03-11 15:00:08 -07001652 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1653 '''
1654 Create/formats the LinkGraph.cfg file based on arguments
1655 -only creates a linear topology and connects islands
1656 -evenly distributes devices
1657 -must be called by ONOSbench
1658
1659 ONOSIpList - list of all of the node IPs to be used
1660
1661 deviceCount - number of switches to be assigned
1662 '''
1663 main.log.step("Creating link graph configuration file." )
1664 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
1665 tempFile = "/tmp/linkGraph.cfg"
1666
1667 linkGraph = open(tempFile, 'w+')
1668 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1669 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1670 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
1671
1672 clusterCount = len(ONOSIpList)
1673
1674 if type(deviceCount) is int or type(deviceCount) is str:
1675 deviceCount = int(deviceCount)
1676 switchList = [0]*(clusterCount+1)
1677 baselineSwitchCount = deviceCount/clusterCount
1678
1679 for node in range(1, clusterCount + 1):
1680 switchList[node] = baselineSwitchCount
1681
1682 for node in range(1, (deviceCount%clusterCount)+1):
1683 switchList[node] += 1
1684
1685 if type(deviceCount) is list:
1686 main.log.info("Using provided device distribution")
1687 switchList = [0]
1688 for i in deviceCount:
1689 switchList.append(int(i))
1690
1691 tempList = ['0']
1692 tempList.extend(ONOSIpList)
1693 ONOSIpList = tempList
1694
1695 myPort = 6
1696 lastSwitch = 0
1697 for node in range(1, clusterCount+1):
1698 if switchList[node] == 0:
1699 continue
1700
1701 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
1702
1703 if node > 1:
1704 #connect to last device on previous node
1705 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
1706 linkGraph.write(line)
1707
1708 lastSwitch = 0
1709 for switch in range (0, switchList[node]-1):
1710 line = ""
1711 line = ("\t" + str(switch) + ":" + str(myPort))
1712 line += " -- "
1713 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
1714 linkGraph.write(line)
1715 lastSwitch = switch+1
1716 lastIp = ONOSIpList[node]
1717
1718 #lastSwitch += 1
1719 if node < (clusterCount):
1720 #connect to first device on the next node
1721 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
1722 linkGraph.write(line)
1723
1724 linkGraph.write("}\n")
1725 linkGraph.close()
1726
1727 #SCP
1728 os.system( "scp " + tempFile + " admin@" + benchIp + ":" + linkGraphPath)
1729 main.log.info("linkGraph.cfg creation complete")
1730
1731 def createNullDevProviderFile( self, benchIp, ONOSIpList, deviceCount, numPorts=10):
1732
1733 '''
1734 benchIp = Ip address of the test bench
1735 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
1736 deviceCount = number of switches to distribute
1737 numPorts = number of ports per device, when not specified in file it defaults to 10, optional arg
1738 '''
1739
1740 main.log.step("Creating null device provider configuration file." )
1741 nullDevicePath = self.home + "/tools/package/etc/org.onosproject.provider.nil.device.impl.NullDeviceProvider.cfg"
1742 tempFile = "/tmp/org.onosproject.provider.nil.device.impl.NullDeviceProvider.cfg"
1743 configFile = open(tempFile, 'w+')
1744 clusterCount = len(ONOSIpList)
1745
1746 if type(deviceCount) is int or type(deviceCount) is str:
1747 main.log.info("Creating device distribution")
1748 deviceCount = int(deviceCount)
1749 switchList = [0]*(clusterCount+1)
1750 baselineSwitchCount = deviceCount/clusterCount
1751
1752 for node in range(1, clusterCount + 1):
1753 switchList[node] = baselineSwitchCount
1754
1755 for node in range(1, (deviceCount%clusterCount)+1):
1756 switchList[node] += 1
1757
1758 if type(deviceCount) is list:
1759 main.log.info("Using provided device distribution")
1760 switchList = ['0']
1761 switchList.extend(deviceCount)
1762
1763 ONOSIp = [0]
1764 ONOSIp.extend(ONOSIpList)
1765
1766 devicesString = "devConfigs = "
1767 for node in range(1, len(ONOSIp)):
1768 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
1769 if node < clusterCount:
1770 devicesString += (",")
1771
1772 configFile.write(devicesString + "\n")
1773 if numPorts == 10:
1774 configFile.write("#numPorts = 10")
1775 else:
1776 configFile.write("numPorts = " + str(numPorts))
1777
1778 configFile.close()
1779 os.system( "scp " + tempFile + " admin@" + benchIp + ":" + nullDevicePath)
1780
1781 def createNullLinkProviderFile( self, benchIp, neighborIpList=0, eventRate=0, onNode=False):
1782 '''
1783 neighbor list is an optional list of neighbors to be written directly to the file
1784 onNode - bool, if true, alternate file path will be used to scp, inteneded
1785 for use on cell
1786 '''
1787
1788 main.log.step("Creating Null Link Provider config file")
1789 nullLinkPath = self.home + "/tools/package/etc/org.onosproject.provider.nil.link.impl.NullLinkProvider.cfg"
1790 if onNode == True:
1791 nullLinkPath = "/opt/onos/apache-karaf-3.0.2/etc/org.onosproject.provider.nil.link.impl.NullLinkProvider.cfg"
1792 tempFile = "/tmp/org.onosproject.provider.nil.link.impl.NullLinkProvider.cfg"
1793 configFile = open(tempFile, 'w+')
1794
1795 eventRate = int(eventRate)
1796
1797 if eventRate == 0:
1798 configFile.write("#eventRate = \n")
1799 else:
1800 configFile.write("eventRate = " + str(eventRate) + "\n")
1801
1802 configFile.write("#cfgFile = /tmp/foo.cfg #If enabled, points to the full path to the topology file.\n")
1803
1804 if neighborIpList != 0:
1805 configFile.write("neighbors = ")
1806 for n in range (0, len(neighborIpList)):
1807 configFile.write(neighborIpList[n])
1808 if n < (len(neighborIpList) - 1):
1809 configFile.write(",")
1810 else:
1811 configFile.write("#neighbors = ")
1812
1813 configFile.close()
1814 if onNode == False:
1815 os.system( "scp " + tempFile + " admin@" + benchIp + ":" + nullLinkPath)
1816 if onNode == True:
1817 os.system( "scp " + tempFile + " sdn@" + benchIp + ":" + nullLinkPath)
1818
1819
1820
1821