blob: 04ef1f8eeafac8ef0ef81a26a8d7797f13718876 [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 ]
Jon Hall05b2b432014-10-08 19:53:25 -040045 self.home = "~/ONOS"
46 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:
kelvin8ec71442015-01-15 16:57:00 -080084 self.handle.sendline( "" )
85 self.handle.expect( "\$" )
86 self.handle.sendline( "exit" )
87 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -040088 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080089 main.log.error( self.name + ": EOF exception found" )
90 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -080091 except Exception:
92 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -040093 response = main.FALSE
94 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040095
kelvin-onlabd3b64892015-01-20 13:26:24 -080096 def onosPackage( self ):
kelvin8ec71442015-01-15 16:57:00 -080097 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040098 Produce a self-contained tar.gz file that can be deployed
kelvin8ec71442015-01-15 16:57:00 -080099 and executed on any platform with Java 7 JRE.
100 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400101 try:
kelvin8ec71442015-01-15 16:57:00 -0800102 self.handle.sendline( "onos-package" )
103 self.handle.expect( "onos-package" )
104 self.handle.expect( "tar.gz", timeout=30 )
105 handle = str( self.handle.before )
106 main.log.info( "onos-package command returned: " +
107 handle )
108 # As long as the sendline does not time out,
109 # return true. However, be careful to interpret
110 # the results of the onos-package command return
andrewonlab0748d2a2014-10-09 13:24:17 -0400111 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400112
andrewonlab7735d852014-10-09 13:02:47 -0400113 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800114 main.log.error( self.name + ": EOF exception found" )
115 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800116 except Exception:
117 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400118 main.cleanup()
119 main.exit()
120
kelvin-onlabd3b64892015-01-20 13:26:24 -0800121 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800122 """
andrewonlab8790abb2014-11-06 13:51:54 -0500123 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800124 """
andrewonlab8790abb2014-11-06 13:51:54 -0500125 try:
kelvin8ec71442015-01-15 16:57:00 -0800126 self.handle.sendline( "onos-build" )
127 self.handle.expect( "onos-build" )
128 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800129 "BUILD SUCCESS",
130 "ERROR",
131 "BUILD FAILED" ],
132 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800133 handle = str( self.handle.before )
andrewonlab8790abb2014-11-06 13:51:54 -0500134
kelvin8ec71442015-01-15 16:57:00 -0800135 main.log.info( "onos-build command returned: " +
136 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500137
138 if i == 0:
139 return main.TRUE
140 else:
141 return handle
142
143 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800144 main.log.error( self.name + ": EOF exception found" )
145 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800146 except Exception:
147 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500148 main.cleanup()
149 main.exit()
150
kelvin-onlabd3b64892015-01-20 13:26:24 -0800151 def cleanInstall( self ):
kelvin8ec71442015-01-15 16:57:00 -0800152 """
153 Runs mvn clean install in the root of the ONOS directory.
154 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400155
kelvin8ec71442015-01-15 16:57:00 -0800156 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400157 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800158 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400159 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800160 main.log.info( "Running 'mvn clean install' on " +
161 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800162 ". This may take some time." )
163 self.handle.sendline( "cd " + self.home )
164 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400165
kelvin8ec71442015-01-15 16:57:00 -0800166 self.handle.sendline( "" )
167 self.handle.expect( "\$" )
168 self.handle.sendline( "mvn clean install" )
169 self.handle.expect( "mvn clean install" )
170 while True:
171 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800172 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800173 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400174 'BUILD\sFAILURE',
175 'BUILD\sSUCCESS',
176 'ONOS\$',
kelvin8ec71442015-01-15 16:57:00 -0800177 pexpect.TIMEOUT ], timeout=600 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400178 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800179 main.log.error( self.name + ":There is insufficient memory \
180 for the Java Runtime Environment to continue." )
181 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400182 main.cleanup()
183 main.exit()
184 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800185 main.log.error( self.name + ": Build failure!" )
186 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400187 main.cleanup()
188 main.exit()
189 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800190 main.log.info( self.name + ": Build success!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400191 elif i == 3:
kelvin8ec71442015-01-15 16:57:00 -0800192 main.log.info( self.name + ": Build complete" )
193 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400194 for line in self.handle.before.splitlines():
195 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800196 main.log.info( line )
197 self.handle.sendline( "" )
198 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400199 return main.TRUE
200 elif i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800201 main.log.error(
202 self.name +
203 ": mvn clean install TIMEOUT!" )
204 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400205 main.cleanup()
206 main.exit()
207 else:
Jon Hall274b6642015-02-17 11:57:17 -0800208 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800209 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800210 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400211 main.cleanup()
212 main.exit()
213 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800214 main.log.error( self.name + ": EOF exception found" )
215 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400216 main.cleanup()
217 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800218 except Exception:
219 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400220 main.cleanup()
221 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400222
kelvin-onlabd3b64892015-01-20 13:26:24 -0800223 def gitPull( self, comp1="" ):
kelvin8ec71442015-01-15 16:57:00 -0800224 """
Jon Hallacabffd2014-10-09 12:36:53 -0400225 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800226
Jon Hallacabffd2014-10-09 12:36:53 -0400227 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800228 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400229 for the purpose of pulling from other nodes if necessary.
230
Jon Hall47a93fb2015-01-06 16:46:06 -0800231 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400232 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800233 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400234 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400235
kelvin8ec71442015-01-15 16:57:00 -0800236 """
Jon Hallacabffd2014-10-09 12:36:53 -0400237 try:
kelvin8ec71442015-01-15 16:57:00 -0800238 # main.log.info( self.name + ": Stopping ONOS" )
239 # self.stop()
240 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800241 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800242 if comp1 == "":
243 self.handle.sendline( "git pull" )
Jon Hallacabffd2014-10-09 12:36:53 -0400244 else:
kelvin8ec71442015-01-15 16:57:00 -0800245 self.handle.sendline( "git pull " + comp1 )
Jon Hall47a93fb2015-01-06 16:46:06 -0800246
kelvin-onlabd3b64892015-01-20 13:26:24 -0800247 i = self.handle.expect(
248 [
249 'fatal',
250 'Username\sfor\s(.*):\s',
251 '\sfile(s*) changed,\s',
252 'Already up-to-date',
253 'Aborting',
254 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800255 'You asked me to pull without telling me which branch you',
256 'Pull is not possible because you have unmerged files',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800257 pexpect.TIMEOUT ],
258 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800259 # debug
kelvin-onlabd3b64892015-01-20 13:26:24 -0800260 # main.log.report( self.name +": DEBUG: \n"+
261 # "git pull response: " +
262 # str( self.handle.before ) + str( self.handle.after ) )
kelvin8ec71442015-01-15 16:57:00 -0800263 if i == 0:
264 main.log.error( self.name + ": Git pull had some issue..." )
Jon Hallacabffd2014-10-09 12:36:53 -0400265 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800266 elif i == 1:
267 main.log.error(
268 self.name +
269 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400270 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800271 elif i == 2:
272 main.log.info(
273 self.name +
274 ": Git Pull - pulling repository now" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800275 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800276 # So that only when git pull is done, we do mvn clean compile
277 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800278 elif i == 3:
279 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800280 return i
kelvin8ec71442015-01-15 16:57:00 -0800281 elif i == 4:
282 main.log.info(
283 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800284 ": Git Pull - Aborting..." +
285 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400286 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800287 elif i == 5:
288 main.log.info(
289 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800290 ": Git Pull - You are not currently " +
291 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400292 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800293 elif i == 6:
294 main.log.info(
295 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800296 ": Git Pull - You have not configured an upstream " +
297 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400298 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800299 elif i == 7:
300 main.log.info(
301 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800302 ": Git Pull - Pull is not possible because " +
303 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400304 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800305 elif i == 8:
306 main.log.error( self.name + ": Git Pull - TIMEOUT" )
307 main.log.error(
308 self.name + " Response was: " + str(
309 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400310 return main.ERROR
311 else:
kelvin8ec71442015-01-15 16:57:00 -0800312 main.log.error(
313 self.name +
314 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400315 return main.ERROR
316 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800317 main.log.error( self.name + ": EOF exception found" )
318 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400319 main.cleanup()
320 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800321 except Exception:
322 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400323 main.cleanup()
324 main.exit()
325
kelvin-onlabd3b64892015-01-20 13:26:24 -0800326 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800327 """
Jon Hallacabffd2014-10-09 12:36:53 -0400328 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800329
Jon Hallacabffd2014-10-09 12:36:53 -0400330 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800331 If used as gitCheckout( "branch" ) it will do git checkout
332 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400333
334 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800335 branch of the ONOS repository. If it has any problems, it will return
336 main.ERROR.
337 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400338 successful then the function will return main.TRUE.
339
kelvin8ec71442015-01-15 16:57:00 -0800340 """
Jon Hallacabffd2014-10-09 12:36:53 -0400341 try:
kelvin8ec71442015-01-15 16:57:00 -0800342 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800343 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800344 main.log.info( self.name +
345 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800346 cmd = "git checkout " + branch
347 self.handle.sendline( cmd )
348 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800349 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800350 [ 'fatal',
351 'Username\sfor\s(.*):\s',
352 'Already\son\s\'',
353 'Switched\sto\sbranch\s\'' + str( branch ),
354 pexpect.TIMEOUT,
355 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800356 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800357 'error: you need to resolve your current index first',
358 "You are in 'detached HEAD' state.",
359 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800360 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800361 if i == 0:
362 main.log.error(
363 self.name +
364 ": Git checkout had some issue..." )
365 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400366 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800367 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800368 main.log.error(
369 self.name +
370 ": Git checkout asking for username." +
371 " Please configure your local git repository to be able " +
372 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800373 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400374 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800375 elif i == 2:
376 main.log.info(
377 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800378 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800379 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800380 # main.log.info( "DEBUG: after checkout cmd = "+
381 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400382 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800383 elif i == 3:
384 main.log.info(
385 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800386 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800387 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800388 # main.log.info( "DEBUG: after checkout cmd = "+
389 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400390 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800391 elif i == 4:
392 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
393 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800394 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400395 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800396 elif i == 5:
397 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800398 main.log.error(
399 self.name +
400 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800401 "Your local changes to the following files would" +
402 " be overwritten by checkout:" +
403 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800404 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500405 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800406 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800407 main.log.error(
408 self.name +
409 ": Git checkout error: \n" +
410 "You need to resolve your current index first:" +
411 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800412 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500413 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800414 elif i == 7:
415 main.log.info(
416 self.name +
417 ": Git checkout " + str( branch ) +
418 " - You are in 'detached HEAD' state. HEAD is now at " +
419 str( branch ) )
420 self.handle.expect( self.home + "\$" )
421 return main.TRUE
422 elif i == 8: # Already in detached HEAD on the specified commit
423 main.log.info(
424 self.name +
425 ": Git Checkout %s : Already on commit" % branch )
426 self.handle.expect( self.home + "\$" )
427 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400428 else:
kelvin8ec71442015-01-15 16:57:00 -0800429 main.log.error(
430 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800431 ": Git Checkout - Unexpected response, " +
432 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800433 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400434 return main.ERROR
435
436 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800437 main.log.error( self.name + ": EOF exception found" )
438 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400439 main.cleanup()
440 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800441 except Exception:
442 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400443 main.cleanup()
444 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400445
kelvin-onlabd3b64892015-01-20 13:26:24 -0800446 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800447 """
Jon Hall274b6642015-02-17 11:57:17 -0800448 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800449 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800450 """
Jon Hall45ec0922014-10-10 19:33:49 -0400451 try:
kelvin8ec71442015-01-15 16:57:00 -0800452 self.handle.sendline( "" )
453 self.handle.expect( "\$" )
454 self.handle.sendline(
455 "cd " +
456 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800457 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
458 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800459 # NOTE: for some reason there are backspaces inserted in this
460 # phrase when run from Jenkins on some tests
461 self.handle.expect( "never" )
462 self.handle.expect( "\$" )
463 response = ( self.name + ": \n" + str(
464 self.handle.before + self.handle.after ) )
465 self.handle.sendline( "cd " + self.home )
466 self.handle.expect( "\$" )
467 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400468 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500469 print line
470 if report:
kelvin8ec71442015-01-15 16:57:00 -0800471 for line in lines[ 2:-1 ]:
472 # Bracket replacement is for Wiki-compliant
473 # formatting. '<' or '>' are interpreted
474 # as xml specific tags that cause errors
475 line = line.replace( "<", "[" )
476 line = line.replace( ">", "]" )
477 main.log.report( "\t" + line )
478 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400479 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800480 main.log.error( self.name + ": EOF exception found" )
481 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400482 main.cleanup()
483 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800484 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800485 main.log.error( self.name + ": TIMEOUT exception found" )
486 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800487 main.cleanup()
488 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800489 except Exception:
490 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400491 main.cleanup()
492 main.exit()
493
kelvin-onlabd3b64892015-01-20 13:26:24 -0800494 def createCellFile( self, benchIp, fileName, mnIpAddrs,
Jon Hallefbd9792015-03-05 16:11:36 -0800495 extraFeatureString, *onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800496 """
andrewonlab94282092014-10-10 13:00:11 -0400497 Creates a cell file based on arguments
498 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800499 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400500 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800501 * File name of the cell file ( fileName )
502 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800503 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400504 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800505 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400506 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800507
andrewonlab94282092014-10-10 13:00:11 -0400508 NOTE: Assumes cells are located at:
509 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800510 """
511 # Variable initialization
kelvin-onlabd3b64892015-01-20 13:26:24 -0800512 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800513 # We want to create the cell file in the dependencies directory
514 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800515 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800516 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800517 cellFile = open( tempDirectory + fileName, 'w+' )
kelvin8ec71442015-01-15 16:57:00 -0800518
519 # Feature string is hardcoded environment variables
520 # That you may wish to use by default on startup.
521 # Note that you may not want certain features listed
522 # on here.
jenkinsbd0e7532015-02-10 14:32:54 -0800523 coreFeatureString = "export ONOS_FEATURES=" + extraFeatureString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800524 mnString = "export OCN="
525 onosString = "export OC"
526 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800527
kelvin-onlabd3b64892015-01-20 13:26:24 -0800528 # Create ONOSNIC ip address prefix
529 tempOnosIp = onosIpAddrs[ 0 ]
530 tempList = []
531 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800532 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800533 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800534 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800535 nicAddr = ".".join( tempList ) + ".*"
536 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400537
538 try:
kelvin8ec71442015-01-15 16:57:00 -0800539 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800540 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400541
kelvin-onlabd3b64892015-01-20 13:26:24 -0800542 for arg in onosIpAddrs:
543 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800544 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400545 # export OC1="10.128.20.11"
546 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800547 cellFile.write( onosString + str( tempCount ) +
548 "=" + "\"" + arg + "\"" + "\n" )
549 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800550
kelvin-onlabd3b64892015-01-20 13:26:24 -0800551 cellFile.write( mnString + "\"" + mnIpAddrs + "\"" + "\n" )
552 cellFile.write( coreFeatureString + "\n" )
553 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400554
kelvin8ec71442015-01-15 16:57:00 -0800555 # We use os.system to send the command to TestON cluster
556 # to account for the case in which TestON is not located
557 # on the same cluster as the ONOS bench
558 # Note that even if TestON is located on the same cluster
559 # as ONOS bench, you must setup passwordless ssh
560 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800561 os.system( "scp " + tempDirectory + fileName +
562 " admin@" + benchIp + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400563
andrewonlab2a6c9342014-10-16 13:40:15 -0400564 return main.TRUE
565
andrewonlab94282092014-10-10 13:00:11 -0400566 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800567 main.log.error( self.name + ": EOF exception found" )
568 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400569 main.cleanup()
570 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800571 except Exception:
572 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400573 main.cleanup()
574 main.exit()
575
kelvin-onlabd3b64892015-01-20 13:26:24 -0800576 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800577 """
andrewonlab95ca1462014-10-09 14:04:24 -0400578 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800579 """
andrewonlab95ca1462014-10-09 14:04:24 -0400580 try:
581 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800582 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400583 main.cleanup()
584 main.exit()
585 else:
kelvin8ec71442015-01-15 16:57:00 -0800586 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800587 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800588 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400589 # and that this driver will have to change accordingly
jenkinsbd0e7532015-02-10 14:32:54 -0800590 self.handle.expect( "ONOS_CELL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800591 handleBefore = self.handle.before
592 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800593 # Get the rest of the handle
594 self.handle.sendline( "" )
595 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800596 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400597
kelvin-onlabd3b64892015-01-20 13:26:24 -0800598 main.log.info( "Cell call returned: " + handleBefore +
599 handleAfter + handleMore )
andrewonlab95ca1462014-10-09 14:04:24 -0400600
601 return main.TRUE
602
603 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800604 main.log.error( self.name + ": EOF exception found" )
605 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400606 main.cleanup()
607 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800608 except Exception:
609 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400610 main.cleanup()
611 main.exit()
612
kelvin-onlabd3b64892015-01-20 13:26:24 -0800613 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800614 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400615 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800616 """
617 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400618
andrewonlabc03bf6c2014-10-09 14:56:18 -0400619 try:
kelvin8ec71442015-01-15 16:57:00 -0800620 # Clean handle by sending empty and expecting $
621 self.handle.sendline( "" )
622 self.handle.expect( "\$" )
623 self.handle.sendline( "onos-verify-cell" )
624 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800625 handleBefore = self.handle.before
626 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800627 # Get the rest of the handle
628 self.handle.sendline( "" )
629 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800630 handleMore = self.handle.before
andrewonlabc03bf6c2014-10-09 14:56:18 -0400631
kelvin-onlabd3b64892015-01-20 13:26:24 -0800632 main.log.info( "Verify cell returned: " + handleBefore +
633 handleAfter + handleMore )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400634
635 return main.TRUE
Jon Hall7993bfc2014-10-09 16:30:14 -0400636 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800637 main.log.error( self.name + ": EOF exception found" )
638 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400639 main.cleanup()
640 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800641 except Exception:
642 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400643 main.cleanup()
644 main.exit()
645
kelvin-onlabd3b64892015-01-20 13:26:24 -0800646 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800647 """
andrewonlab05e362f2014-10-10 00:40:57 -0400648 Uses 'onos' command to send various ONOS CLI arguments.
649 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800650 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400651 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800652
653 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400654 CLI commands for ONOS. Try to use this function first
655 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800656 function.
657 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400658 by starting onos, and typing in 'onos' to enter the
659 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800660 available commands.
661 """
andrewonlab05e362f2014-10-10 00:40:57 -0400662 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800663 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800664 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400665 return main.FALSE
666 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800667 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400668 return main.FALSE
669
kelvin8ec71442015-01-15 16:57:00 -0800670 cmdstr = str( cmdstr )
671 self.handle.sendline( "" )
672 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400673
kelvin-onlabd3b64892015-01-20 13:26:24 -0800674 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800675 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400676
kelvin-onlabd3b64892015-01-20 13:26:24 -0800677 handleBefore = self.handle.before
Shreya Shaha73aaad2014-10-27 18:03:09 -0400678 print "handle_before = ", self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800679 # handleAfter = str( self.handle.after )
Jon Hall47a93fb2015-01-06 16:46:06 -0800680
kelvin8ec71442015-01-15 16:57:00 -0800681 # self.handle.sendline( "" )
682 # self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800683 # handleMore = str( self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400684
kelvin8ec71442015-01-15 16:57:00 -0800685 main.log.info( "Command sent successfully" )
andrewonlab05e362f2014-10-10 00:40:57 -0400686
kelvin8ec71442015-01-15 16:57:00 -0800687 # Obtain return handle that consists of result from
688 # the onos command. The string may need to be
689 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800690 # returnString = handleBefore + handleAfter
691 returnString = handleBefore
692 print "return_string = ", returnString
693 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400694
695 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800696 main.log.error( self.name + ": EOF exception found" )
697 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400698 main.cleanup()
699 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800700 except Exception:
701 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400702 main.cleanup()
703 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400704
kelvin-onlabd3b64892015-01-20 13:26:24 -0800705 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800706 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400707 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800708 If -f option is provided, it also forces an uninstall.
709 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400710 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800711 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400712 files to certain onos nodes
713
714 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800715 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400716 try:
andrewonlab114768a2014-11-14 12:44:44 -0500717 if options:
kelvin8ec71442015-01-15 16:57:00 -0800718 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500719 else:
kelvin8ec71442015-01-15 16:57:00 -0800720 self.handle.sendline( "onos-install " + node )
721 self.handle.expect( "onos-install " )
722 # NOTE: this timeout may need to change depending on the network
723 # and size of ONOS
724 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800725 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800726 "ONOS\sis\salready\sinstalled",
727 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400728
Jon Hall7993bfc2014-10-09 16:30:14 -0400729 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800730 main.log.warn( "Network is unreachable" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400731 return main.FALSE
732 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800733 main.log.info(
734 "ONOS was installed on " +
735 node +
736 " and started" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400737 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500738 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800739 main.log.info( "ONOS is already installed on " + node )
andrewonlabd9a73a72014-11-14 17:28:21 -0500740 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800741 elif i == 3:
742 main.log.info(
743 "Installation of ONOS on " +
744 node +
745 " timed out" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400746 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400747
748 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800749 main.log.error( self.name + ": EOF exception found" )
750 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400751 main.cleanup()
752 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800753 except Exception:
754 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400755 main.cleanup()
756 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400757
kelvin-onlabd3b64892015-01-20 13:26:24 -0800758 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800759 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400760 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400761 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800762 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400763 try:
kelvin8ec71442015-01-15 16:57:00 -0800764 self.handle.sendline( "" )
765 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800766 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800767 " start" )
768 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -0400769 "Job\sis\salready\srunning",
770 "start/running",
771 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800772 pexpect.TIMEOUT ], timeout=120 )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400773
774 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800775 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400776 return main.TRUE
777 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800778 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400779 return main.TRUE
780 else:
kelvin8ec71442015-01-15 16:57:00 -0800781 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400782 main.cleanup()
783 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400784 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 )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400787 main.cleanup()
788 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800789 except Exception:
790 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400791 main.cleanup()
792 main.exit()
793
kelvin-onlabd3b64892015-01-20 13:26:24 -0800794 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800795 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400796 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400797 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800798 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400799 try:
kelvin8ec71442015-01-15 16:57:00 -0800800 self.handle.sendline( "" )
801 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800802 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800803 " stop" )
804 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -0400805 "stop/waiting",
806 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800807 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2b30bd32014-10-09 16:48:55 -0400808
809 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800810 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400811 return main.TRUE
812 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800813 main.log.info( "Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800814 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -0400815 return main.FALSE
816 else:
kelvin8ec71442015-01-15 16:57:00 -0800817 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400818 return main.FALSE
819
820 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800821 main.log.error( self.name + ": EOF exception found" )
822 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -0400823 main.cleanup()
824 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800825 except Exception:
826 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400827 main.cleanup()
828 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800829
kelvin-onlabd3b64892015-01-20 13:26:24 -0800830 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -0800831 """
andrewonlabc8d47972014-10-09 16:52:36 -0400832 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -0800833 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -0400834 if needed
kelvin8ec71442015-01-15 16:57:00 -0800835 """
andrewonlabc8d47972014-10-09 16:52:36 -0400836 try:
kelvin8ec71442015-01-15 16:57:00 -0800837 self.handle.sendline( "" )
838 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800839 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800840 self.handle.expect( "\$" )
andrewonlabc8d47972014-10-09 16:52:36 -0400841
kelvin-onlabd3b64892015-01-20 13:26:24 -0800842 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
andrewonlab9dfd2082014-11-13 17:44:03 -0500843
kelvin8ec71442015-01-15 16:57:00 -0800844 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -0400845 return main.TRUE
846
847 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800848 main.log.error( self.name + ": EOF exception found" )
849 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -0400850 main.cleanup()
851 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800852 except Exception:
853 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -0400854 main.cleanup()
855 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400856
kelvin-onlabd3b64892015-01-20 13:26:24 -0800857 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800858 """
andrewonlabaedc8332014-12-04 12:43:03 -0500859 Issues the command 'onos-die <node-ip>'
860 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -0800861 """
andrewonlabaedc8332014-12-04 12:43:03 -0500862 try:
kelvin8ec71442015-01-15 16:57:00 -0800863 self.handle.sendline( "" )
864 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800865 cmdStr = "onos-kill " + str( nodeIp )
866 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800867 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -0500868 "Killing\sONOS",
869 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -0800870 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -0500871 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800872 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800873 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -0500874 return main.TRUE
875 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800876 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -0500877 return main.FALSE
878 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800879 main.log.error( self.name + ": EOF exception found" )
880 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -0500881 main.cleanup()
882 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800883 except Exception:
884 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -0500885 main.cleanup()
886 main.exit()
887
kelvin-onlabd3b64892015-01-20 13:26:24 -0800888 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800889 """
andrewonlabe8e56fd2014-10-09 17:12:44 -0400890 Calls the command: 'onos-kill [<node-ip>]'
891 "Remotely, and unceremoniously kills the ONOS instance running on
892 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -0800893 """
andrewonlabe8e56fd2014-10-09 17:12:44 -0400894 try:
kelvin8ec71442015-01-15 16:57:00 -0800895 self.handle.sendline( "" )
896 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800897 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800898 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -0400899 "\$",
900 "No\sroute\sto\shost",
901 "password:",
kelvin8ec71442015-01-15 16:57:00 -0800902 pexpect.TIMEOUT ], timeout=20 )
903
andrewonlabe8e56fd2014-10-09 17:12:44 -0400904 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800905 main.log.info(
906 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800907 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400908 return main.TRUE
909 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800910 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400911 return main.FALSE
912 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800913 main.log.info(
914 "Passwordless login for host: " +
915 str( nodeIp ) +
916 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400917 return main.FALSE
918 else:
Jon Hallefbd9792015-03-05 16:11:36 -0800919 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400920 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -0800921
andrewonlabe8e56fd2014-10-09 17:12:44 -0400922 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800923 main.log.error( self.name + ": EOF exception found" )
924 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400925 main.cleanup()
926 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800927 except Exception:
928 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400929 main.cleanup()
930 main.exit()
931
kelvin-onlabd3b64892015-01-20 13:26:24 -0800932 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -0800933 """
andrewonlab19fbdca2014-11-14 12:55:59 -0500934 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -0500935 a cleaner environment.
936
andrewonlab19fbdca2014-11-14 12:55:59 -0500937 Description:
Jon Hallfcc88622014-11-25 13:09:54 -0500938 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -0500939 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -0800940 """
andrewonlab19fbdca2014-11-14 12:55:59 -0500941 try:
kelvin8ec71442015-01-15 16:57:00 -0800942 self.handle.sendline( "" )
943 self.handle.expect( "\$" )
944 self.handle.sendline( "onos-remove-raft-logs" )
945 # Sometimes this command hangs
946 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
947 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -0500948 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800949 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
950 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -0500951 if i == 1:
952 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -0800953 self.handle.sendline( "" )
954 self.handle.expect( "\$" )
andrewonlab19fbdca2014-11-14 12:55:59 -0500955 return main.TRUE
956
957 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800958 main.log.error( self.name + ": EOF exception found" )
959 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -0500960 main.cleanup()
961 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800962 except Exception:
963 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -0500964 main.cleanup()
965 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -0500966
kelvin-onlabd3b64892015-01-20 13:26:24 -0800967 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -0800968 """
969 Calls the command 'onos-start-network [ <mininet-topo> ]
970 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -0400971 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -0800972 cell."
andrewonlab94282092014-10-10 13:00:11 -0400973 * Specify mininet topology file name for mntopo
974 * Topo files should be placed at:
975 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -0800976
andrewonlab94282092014-10-10 13:00:11 -0400977 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -0800978 """
andrewonlab94282092014-10-10 13:00:11 -0400979 try:
980 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -0800981 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -0400982 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -0400983
kelvin8ec71442015-01-15 16:57:00 -0800984 mntopo = str( mntopo )
985 self.handle.sendline( "" )
986 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -0400987
kelvin8ec71442015-01-15 16:57:00 -0800988 self.handle.sendline( "onos-start-network " + mntopo )
989 self.handle.expect( "mininet>" )
990 main.log.info( "Network started, entered mininet prompt" )
991
992 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -0400993
994 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800995 main.log.error( self.name + ": EOF exception found" )
996 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400997 main.cleanup()
998 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800999 except Exception:
1000 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001001 main.cleanup()
1002 main.exit()
1003
kelvin8ec71442015-01-15 16:57:00 -08001004 def isup( self, node="" ):
1005 """
1006 Run's onos-wait-for-start which only returns once ONOS is at run
1007 level 100( ready for use )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001008
Jon Hall7993bfc2014-10-09 16:30:14 -04001009 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001010 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001011 try:
kelvin8ec71442015-01-15 16:57:00 -08001012 self.handle.sendline( "onos-wait-for-start " + node )
1013 self.handle.expect( "onos-wait-for-start" )
1014 # NOTE: this timeout is arbitrary"
1015 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ], timeout=120 )
Jon Hall7993bfc2014-10-09 16:30:14 -04001016 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001017 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001018 return main.TRUE
1019 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001020 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001021 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001022 main.log.error( "ONOS has not started yet" )
1023 self.handle.send( "\x03" ) # Control-C
1024 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001025 return main.FALSE
1026 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001027 main.log.error( self.name + ": EOF exception found" )
1028 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001029 main.cleanup()
1030 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001031 except Exception:
1032 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001033 main.cleanup()
1034 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001035
kelvin-onlabd3b64892015-01-20 13:26:24 -08001036 def pushTestIntentsShell(
1037 self,
1038 dpidSrc,
1039 dpidDst,
1040 numIntents,
1041 dirFile,
1042 onosIp,
1043 numMult="",
1044 appId="",
1045 report=True,
1046 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001047 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001048 Description:
kelvin8ec71442015-01-15 16:57:00 -08001049 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001050 better parallelize the results than the CLI
1051 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001052 * dpidSrc: specify source dpid
1053 * dpidDst: specify destination dpid
1054 * numIntents: specify number of intents to push
1055 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001056 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001057 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001058 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001059 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001060 """
1061 try:
1062 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001063 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001064 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001065 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001066 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001067 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001068
kelvin-onlabd3b64892015-01-20 13:26:24 -08001069 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1070 if not numMult:
1071 addIntents = addDpid + " " + str( numIntents )
1072 elif numMult:
1073 addIntents = addDpid + " " + str( numIntents ) + " " +\
1074 str( numMult )
1075 if appId:
1076 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001077 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001078 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001079
andrewonlabaedc8332014-12-04 12:43:03 -05001080 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001081 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001082 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001083 sendCmd = addApp + " &"
1084 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001085
kelvin-onlabd3b64892015-01-20 13:26:24 -08001086 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001087
1088 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001089 main.log.error( self.name + ": EOF exception found" )
1090 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001091 main.cleanup()
1092 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001093 except Exception:
1094 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001095 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001096 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001097
kelvin-onlabd3b64892015-01-20 13:26:24 -08001098 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001099 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001100 parses the onos:topology output
kelvin8ec71442015-01-15 16:57:00 -08001101 Returns: a topology dict populated by the key values found in
Jon Hall77f53ce2014-10-13 18:02:06 -04001102 the cli command.
kelvin8ec71442015-01-15 16:57:00 -08001103 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001104 try:
kelvin8ec71442015-01-15 16:57:00 -08001105 # call the cli to get the topology summary
1106 # cmdstr = "onos:topology"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001107 # cliResult = self.onosCli( ip, cmdstr )
1108 # print "cli_result = ", cliResult
Jon Hall77f53ce2014-10-13 18:02:06 -04001109
kelvin8ec71442015-01-15 16:57:00 -08001110 # Parse the output
Jon Hall77f53ce2014-10-13 18:02:06 -04001111 topology = {}
kelvin-onlabd3b64892015-01-20 13:26:24 -08001112 # for line in cliResult.split( "\n" ):
1113 for line in topologyOutput.splitlines():
kelvin8ec71442015-01-15 16:57:00 -08001114 if not line.startswith( "time=" ):
Jon Hall77f53ce2014-10-13 18:02:06 -04001115 continue
kelvin8ec71442015-01-15 16:57:00 -08001116 # else
1117 # print line
1118 for var in line.split( "," ):
1119 # print "'"+var+"'"
1120 # print "'"+var.strip()+"'"
1121 key, value = var.strip().split( "=" )
1122 topology[ key ] = value
1123 # print "topology = ", topology
1124 # devices = topology.get( 'devices', False )
1125 # print "devices = ", devices
1126 # links = topology.get( 'links', False )
1127 # print "links = ", links
1128 # SCCs = topology.get( 'SCC(s)', False )
1129 # print "SCCs = ", SCCs
1130 # paths = topology.get( 'paths', False )
1131 # print "paths = ", paths
Jon Hall77f53ce2014-10-13 18:02:06 -04001132
1133 return topology
1134 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001135 main.log.error( self.name + ": EOF exception found" )
1136 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001137 main.cleanup()
1138 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001139 except Exception:
1140 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001141 main.cleanup()
1142 main.exit()
1143
kelvin-onlabd3b64892015-01-20 13:26:24 -08001144 def checkStatus(
1145 self,
1146 topologyResult,
1147 numoswitch,
1148 numolink,
1149 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001150 """
Jon Hallefbd9792015-03-05 16:11:36 -08001151 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001152 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001153 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08001154
Jon Hall77f53ce2014-10-13 18:02:06 -04001155 Params: ip = ip used for the onos cli
1156 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001157 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001158 logLevel = level to log to.
1159 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001160
1161
kelvin-onlabd3b64892015-01-20 13:26:24 -08001162 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001163
Jon Hallefbd9792015-03-05 16:11:36 -08001164 Returns: main.TRUE if the number of switches and links are correct,
1165 main.FALSE if the number of switches and links is incorrect,
Jon Hall77f53ce2014-10-13 18:02:06 -04001166 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001167 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001168 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001169 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001170 if topology == {}:
1171 return main.ERROR
1172 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001173 # Is the number of switches is what we expected
1174 devices = topology.get( 'devices', False )
1175 links = topology.get( 'links', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001176 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001177 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001178 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001179 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001180 linkCheck = ( int( links ) == int( numolink ) )
Jon Hallefbd9792015-03-05 16:11:36 -08001181 if switchCheck and linkCheck:
kelvin8ec71442015-01-15 16:57:00 -08001182 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001183 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001184 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001185 result = main.TRUE
1186 else:
1187 output = output + \
Jon Hall274b6642015-02-17 11:57:17 -08001188 "The number of links and switches does not match " + \
1189 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001190 result = main.FALSE
Jon Hallefbd9792015-03-05 16:11:36 -08001191 output = output + "\n ONOS sees %i devices" % int( devices )
1192 output = output + " (%i expected) " % int( numoswitch )
Jon Hall274b6642015-02-17 11:57:17 -08001193 output = output + "and %i links " % int( links )
1194 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001195 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001196 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001197 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001198 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001199 else:
kelvin8ec71442015-01-15 16:57:00 -08001200 main.log.info( output )
1201 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001202 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001203 main.log.error( self.name + ": EOF exception found" )
1204 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001205 main.cleanup()
1206 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001207 except Exception:
1208 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001209 main.cleanup()
1210 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001211
kelvin-onlabd3b64892015-01-20 13:26:24 -08001212 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001213 """
andrewonlab970399c2014-11-07 13:09:32 -05001214 Capture all packet activity and store in specified
1215 directory/file
1216
1217 Required:
1218 * interface: interface to capture
1219 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001220 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001221 try:
1222 self.handle.sendline( "" )
1223 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001224
Jon Hallfebb1c72015-03-05 13:30:09 -08001225 self.handle.sendline( "tshark -i " + str( interface ) +
1226 " -t e -w " + str( dirFile ) + " &" )
1227 self.handle.sendline( "\r" )
1228 self.handle.expect( "Capturing on" )
1229 self.handle.sendline( "\r" )
1230 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001231
Jon Hallfebb1c72015-03-05 13:30:09 -08001232 main.log.info( "Tshark started capturing files on " +
1233 str( interface ) + " and saving to directory: " +
1234 str( dirFile ) )
1235 except pexpect.EOF:
1236 main.log.error( self.name + ": EOF exception found" )
1237 main.log.error( self.name + ": " + self.handle.before )
1238 main.cleanup()
1239 main.exit()
1240 except Exception:
1241 main.log.exception( self.name + ": Uncaught exception!" )
1242 main.cleanup()
1243 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001244
kelvin-onlabd3b64892015-01-20 13:26:24 -08001245 def runOnosTopoCfg( self, instanceName, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001246 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001247 On ONOS bench, run this command:
1248 ./~/ONOS/tools/test/bin/onos-topo-cfg $OC1 filename
1249 which starts the rest and copies
1250 the json file to the onos instance
kelvin8ec71442015-01-15 16:57:00 -08001251 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001252 try:
kelvin8ec71442015-01-15 16:57:00 -08001253 self.handle.sendline( "" )
1254 self.handle.expect( "\$" )
1255 self.handle.sendline( "cd ~/ONOS/tools/test/bin" )
1256 self.handle.expect( "/bin$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001257 cmd = "./onos-topo-cfg " + instanceName + " " + jsonFile
shahshreyae6c7cf42014-11-26 16:39:01 -08001258 print "cmd = ", cmd
kelvin8ec71442015-01-15 16:57:00 -08001259 self.handle.sendline( cmd )
1260 self.handle.expect( "\$" )
1261 self.handle.sendline( "cd ~" )
1262 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -08001263 return main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -08001264 except pexpect.EOF:
1265 main.log.error( self.name + ": EOF exception found" )
1266 main.log.error( self.name + ": " + self.handle.before )
1267 main.cleanup()
1268 main.exit()
1269 except Exception:
1270 main.log.exception( self.name + ": Uncaught exception!" )
1271 main.cleanup()
1272 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001273
kelvin-onlabd3b64892015-01-20 13:26:24 -08001274 def tsharkGrep( self, grep, directory, interface='eth0' ):
kelvin8ec71442015-01-15 16:57:00 -08001275 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001276 Required:
kelvin8ec71442015-01-15 16:57:00 -08001277 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001278 * directory to store results
1279 Optional:
1280 * interface - default: eth0
1281 Description:
1282 Uses tshark command to grep specific group of packets
1283 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001284 The timestamp is hardcoded to be in epoch
1285 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001286 try:
1287 self.handle.sendline( "" )
1288 self.handle.expect( "\$" )
1289 self.handle.sendline( "" )
1290 self.handle.sendline(
1291 "tshark -i " +
1292 str( interface ) +
1293 " -t e | grep --line-buffered \"" +
1294 str(grep) +
1295 "\" >" +
1296 directory +
1297 " &" )
1298 self.handle.sendline( "\r" )
1299 self.handle.expect( "Capturing on" )
1300 self.handle.sendline( "\r" )
1301 self.handle.expect( "\$" )
1302 except pexpect.EOF:
1303 main.log.error( self.name + ": EOF exception found" )
1304 main.log.error( self.name + ": " + self.handle.before )
1305 main.cleanup()
1306 main.exit()
1307 except Exception:
1308 main.log.exception( self.name + ": Uncaught exception!" )
1309 main.cleanup()
1310 main.exit()
1311
kelvin-onlabd3b64892015-01-20 13:26:24 -08001312 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001313 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001314 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001315 """
1316 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001317 try:
1318 self.execute( cmd="sudo rm /tmp/wireshark*" )
1319 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001320 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1321 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001322 self.handle.sendline( "" )
1323 main.log.info( "Tshark stopped" )
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
kelvin8ec71442015-01-15 16:57:00 -08001334 def ptpd( self, args ):
1335 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001336 Initiate ptp with user-specified args.
1337 Required:
1338 * args: specify string of args after command
1339 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001340 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001341 try:
kelvin8ec71442015-01-15 16:57:00 -08001342 self.handle.sendline( "sudo ptpd " + str( args ) )
1343 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001344 "Multiple",
1345 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001346 "\$" ] )
1347 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001348
andrewonlab0c38a4a2014-10-28 18:35:35 -04001349 if i == 0:
1350 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001351 main.log.info( "ptpd returned an error: " +
1352 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001353 return handle
1354 elif i == 1:
1355 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001356 main.log.error( "ptpd returned an error: " +
1357 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001358 return handle
1359 else:
1360 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001361
andrewonlab0c38a4a2014-10-28 18:35:35 -04001362 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001363 main.log.error( self.name + ": EOF exception found" )
1364 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001365 main.cleanup()
1366 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001367 except Exception:
1368 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001369 main.cleanup()
1370 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001371
kelvin-onlabd3b64892015-01-20 13:26:24 -08001372 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001373 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001374 """
1375 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001376 Current implementation of ONOS deletes its karaf
1377 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001378 you may want to use this function to capture
1379 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001380 Localtime will be attached to the filename
1381
1382 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001383 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001384 copy.
kelvin8ec71442015-01-15 16:57:00 -08001385 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001386 For copying multiple files, leave copyFileName
1387 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001388 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001389 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001390 ex ) /tmp/
1391 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001392 * copyFileName: If you want to rename the log
1393 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001394 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001395 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001396 try:
kelvin8ec71442015-01-15 16:57:00 -08001397 localtime = time.strftime( '%x %X' )
1398 localtime = localtime.replace( "/", "" )
1399 localtime = localtime.replace( " ", "_" )
1400 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001401 if destDir[ -1: ] != "/":
1402 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001403
kelvin-onlabd3b64892015-01-20 13:26:24 -08001404 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001405 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1406 str( destDir ) + str( copyFileName ) +
1407 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001408 self.handle.expect( "cp" )
1409 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001410 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001411 self.handle.sendline( "cp " + str( logToCopy ) +
1412 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001413 self.handle.expect( "cp" )
1414 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001415
kelvin8ec71442015-01-15 16:57:00 -08001416 return self.handle.before
1417
1418 except pexpect.EOF:
1419 main.log.error( "Copying files failed" )
1420 main.log.error( self.name + ": EOF exception found" )
1421 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001422 except Exception:
1423 main.log.exception( "Copying files failed" )
1424
kelvin-onlabd3b64892015-01-20 13:26:24 -08001425 def checkLogs( self, onosIp ):
kelvin8ec71442015-01-15 16:57:00 -08001426 """
Jon Hall94fd0472014-12-08 11:52:42 -08001427 runs onos-check-logs on the given onos node
1428 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001429 """
Jon Hall94fd0472014-12-08 11:52:42 -08001430 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001431 cmd = "onos-check-logs " + str( onosIp )
kelvin8ec71442015-01-15 16:57:00 -08001432 self.handle.sendline( cmd )
1433 self.handle.expect( cmd )
1434 self.handle.expect( "\$" )
Jon Hall94fd0472014-12-08 11:52:42 -08001435 response = self.handle.before
1436 return response
1437 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001438 main.log.error( "Lost ssh connection" )
1439 main.log.error( self.name + ": EOF exception found" )
1440 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001441 except Exception:
1442 main.log.exception( self.name + ": Uncaught exception!" )
1443 main.cleanup()
1444 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001445
kelvin-onlabd3b64892015-01-20 13:26:24 -08001446 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001447 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001448 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001449 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001450 try:
kelvin8ec71442015-01-15 16:57:00 -08001451 self.handle.sendline( "" )
1452 self.handle.expect( "\$" )
1453 self.handle.sendline( "onos-service " + str( node ) +
1454 " status" )
1455 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001456 "start/running",
1457 "stop/waiting",
kelvin8ec71442015-01-15 16:57:00 -08001458 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001459
1460 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001461 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001462 return main.TRUE
1463 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001464 main.log.info( "ONOS is stopped" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001465 return main.FALSE
1466 else:
kelvin8ec71442015-01-15 16:57:00 -08001467 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001468 main.cleanup()
1469 main.exit()
1470 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001471 main.log.error( self.name + ": EOF exception found" )
1472 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001473 main.cleanup()
1474 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001475 except Exception:
1476 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001477 main.cleanup()
1478 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001479
1480 def setIpTables( self, ip, port='', action='add', packet_type='tcp',
1481 direction='INPUT', rule='DROP' ):
Jon Hallefbd9792015-03-05 16:11:36 -08001482 """
Jon Hall21270ac2015-02-16 17:59:55 -08001483 Description:
1484 add or remove iptables rule to DROP (default) packets from
1485 specific IP and PORT
1486 Usage:
1487 * specify action ('add' or 'remove')
1488 when removing, pass in the same argument as you would add. It will
1489 delete that specific rule.
1490 * specify the ip to block
1491 * specify the destination port to block (defaults to all ports)
1492 * optional packet type to block (default tcp)
1493 * optional iptables rule (default DROP)
1494 * optional direction to block (default 'INPUT')
1495 Returns:
1496 main.TRUE on success or
1497 main.FALSE if given invalid input or
1498 main.ERROR if there is an error in response from iptables
1499 WARNING:
1500 * This function uses root privilege iptables command which may result
1501 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001502 """
Jon Hall21270ac2015-02-16 17:59:55 -08001503 import time
1504
1505 # NOTE*********
1506 # The strict checking methods of this driver function is intentional
1507 # to discourage any misuse or error of iptables, which can cause
1508 # severe network errors
1509 # *************
1510
1511 # NOTE: Sleep needed to give some time for rule to be added and
1512 # registered to the instance. If you are calling this function
1513 # multiple times this sleep will prevent any errors.
1514 # DO NOT REMOVE
1515 time.sleep( 5 )
1516 try:
1517 # input validation
1518 action_type = action.lower()
1519 rule = rule.upper()
1520 direction = direction.upper()
1521 if action_type != 'add' and action_type != 'remove':
1522 main.log.error( "Invalid action type. Use 'add' or "
1523 "'remove' table rule" )
1524 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1525 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1526 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1527 "'ACCEPT' or 'LOG' only." )
1528 if direction != 'INPUT' and direction != 'OUTPUT':
1529 # NOTE currently only supports rules INPUT and OUPTUT
1530 main.log.error( "Invalid rule. Valid directions are"
1531 " 'OUTPUT' or 'INPUT'" )
1532 return main.FALSE
1533 return main.FALSE
1534 return main.FALSE
1535 if action_type == 'add':
1536 # -A is the 'append' action of iptables
1537 actionFlag = '-A'
1538 elif action_type == 'remove':
1539 # -D is the 'delete' rule of iptables
1540 actionFlag = '-D'
1541 self.handle.sendline( "" )
1542 self.handle.expect( "\$" )
1543 cmd = "sudo iptables " + actionFlag + " " +\
1544 direction +\
1545 " -p " + str( packet_type ) +\
1546 " -s " + str( ip )
1547 if port:
1548 cmd += " --dport " + str( port )
1549 cmd += " -j " + str( rule )
1550
1551 self.handle.sendline( cmd )
1552 self.handle.expect( "\$" )
1553 main.log.warn( self.handle.before )
1554
1555 info_string = "On " + str( self.name )
1556 info_string += " " + str( action_type )
1557 info_string += " iptable rule [ "
1558 info_string += " IP: " + str( ip )
1559 info_string += " Port: " + str( port )
1560 info_string += " Rule: " + str( rule )
1561 info_string += " Direction: " + str( direction ) + " ]"
1562 main.log.info( info_string )
1563 return main.TRUE
1564 except pexpect.TIMEOUT:
1565 main.log.exception( self.name + ": Timeout exception in "
1566 "setIpTables function" )
1567 return main.ERROR
1568 except pexpect.EOF:
1569 main.log.error( self.name + ": EOF exception found" )
1570 main.log.error( self.name + ": " + self.handle.before )
1571 main.cleanup()
1572 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001573 except Exception:
1574 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001575 main.cleanup()
1576 main.exit()
1577
Jon Hall0468b042015-02-19 19:08:21 -08001578 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001579 """
Jon Hall0468b042015-02-19 19:08:21 -08001580 This method is used by STS to check the status of the controller
1581 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001582 """
Jon Hall0468b042015-02-19 19:08:21 -08001583 import re
1584 try:
1585 self.handle.sendline( "" )
1586 self.handle.expect( "\$" )
1587 self.handle.sendline( "cd " + self.home )
1588 self.handle.expect( "\$" )
1589 self.handle.sendline( "service onos status" )
1590 self.handle.expect( "\$" )
1591 response = self.handle.before
1592 if re.search( "onos start/running", response ):
1593 # onos start/running, process 10457
1594 return 'RUNNING'
1595 # FIXME: Implement this case
1596 # elif re.search( pattern, response ):
1597 # return 'STARTING'
1598 elif re.search( "onos stop/", response ):
1599 # onos stop/waiting
1600 # FIXME handle this differently?: onos stop/pre-stop
1601 return 'STOPPED'
1602 # FIXME: Implement this case
1603 # elif re.search( pattern, response ):
1604 # return 'FROZEN'
1605 else:
1606 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001607 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001608 main.log.warn( response )
1609 return 'ERROR', "Unknown response: %s" % response
1610 except pexpect.TIMEOUT:
1611 main.log.exception( self.name + ": Timeout exception in "
1612 "setIpTables function" )
1613 return 'ERROR', "Pexpect Timeout"
1614 except pexpect.EOF:
1615 main.log.error( self.name + ": EOF exception found" )
1616 main.log.error( self.name + ": " + self.handle.before )
1617 main.cleanup()
1618 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001619 except Exception:
1620 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001621 main.cleanup()
1622 main.exit()
1623