blob: 55a5c0eb507215fa082745df49ebe96040a5cb17 [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
Jon Hall05b2b432014-10-08 19:53:25 -040022import traceback
andrewonlab7735d852014-10-09 13:02:47 -040023import os.path
kelvin8ec71442015-01-15 16:57:00 -080024sys.path.append( "../" )
Jon Hall05b2b432014-10-08 19:53:25 -040025from drivers.common.clidriver import CLI
26
Jon Hall05b2b432014-10-08 19:53:25 -040027
kelvin8ec71442015-01-15 16:57:00 -080028class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040029
kelvin8ec71442015-01-15 16:57:00 -080030 def __init__( self ):
31 """
32 Initialize client
33 """
34 super( CLI, self ).__init__()
35
36 def connect( self, **connectargs ):
37 """
Jon Hall05b2b432014-10-08 19:53:25 -040038 Creates ssh handle for ONOS "bench".
kelvin8ec71442015-01-15 16:57:00 -080039 """
Jon Hall05b2b432014-10-08 19:53:25 -040040 try:
41 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080042 vars( self )[ key ] = connectargs[ key ]
Jon Hall05b2b432014-10-08 19:53:25 -040043 self.home = "~/ONOS"
44 for key in self.options:
45 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080046 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040047 break
Jon Hall274b6642015-02-17 11:57:17 -080048 if self.home is None or self.home == "":
kelvin-onlaba1484582015-02-02 15:46:20 -080049 self.home = "~/ONOS"
Jon Hall21270ac2015-02-16 17:59:55 -080050
kelvin8ec71442015-01-15 16:57:00 -080051 self.name = self.options[ 'name' ]
52 self.handle = super( OnosDriver, self ).connect(
53 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -080054 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -080055 port=self.port,
56 pwd=self.pwd,
57 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -040058
kelvin8ec71442015-01-15 16:57:00 -080059 self.handle.sendline( "cd " + self.home )
60 self.handle.expect( "\$" )
Jon Hall05b2b432014-10-08 19:53:25 -040061 if self.handle:
62 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080063 else:
64 main.log.info( "NO ONOS HANDLE" )
Jon Hall05b2b432014-10-08 19:53:25 -040065 return main.FALSE
66 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080067 main.log.error( self.name + ": EOF exception found" )
68 main.log.error( self.name + ": " + self.handle.before )
Jon Hall05b2b432014-10-08 19:53:25 -040069 main.cleanup()
70 main.exit()
Jon Hall63604932015-02-26 17:09:50 -080071 except Exception as e:
72 main.log.exception( "Uncaught exception!" )
Jon Hall05b2b432014-10-08 19:53:25 -040073 main.cleanup()
74 main.exit()
75
kelvin8ec71442015-01-15 16:57:00 -080076 def disconnect( self ):
77 """
Jon Hall05b2b432014-10-08 19:53:25 -040078 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080079 """
Jon Halld61331b2015-02-17 16:35:47 -080080 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -040081 try:
kelvin8ec71442015-01-15 16:57:00 -080082 self.handle.sendline( "" )
83 self.handle.expect( "\$" )
84 self.handle.sendline( "exit" )
85 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -040086 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080087 main.log.error( self.name + ": EOF exception found" )
88 main.log.error( self.name + ": " + self.handle.before )
Jon Hall05b2b432014-10-08 19:53:25 -040089 except:
kelvin8ec71442015-01-15 16:57:00 -080090 main.log.error( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -040091 response = main.FALSE
92 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040093
kelvin-onlabd3b64892015-01-20 13:26:24 -080094 def onosPackage( self ):
kelvin8ec71442015-01-15 16:57:00 -080095 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040096 Produce a self-contained tar.gz file that can be deployed
kelvin8ec71442015-01-15 16:57:00 -080097 and executed on any platform with Java 7 JRE.
98 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040099 try:
kelvin8ec71442015-01-15 16:57:00 -0800100 self.handle.sendline( "onos-package" )
101 self.handle.expect( "onos-package" )
102 self.handle.expect( "tar.gz", timeout=30 )
103 handle = str( self.handle.before )
104 main.log.info( "onos-package command returned: " +
105 handle )
106 # As long as the sendline does not time out,
107 # return true. However, be careful to interpret
108 # the results of the onos-package command return
andrewonlab0748d2a2014-10-09 13:24:17 -0400109 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400110
andrewonlab7735d852014-10-09 13:02:47 -0400111 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800112 main.log.error( self.name + ": EOF exception found" )
113 main.log.error( self.name + ": " + self.handle.before )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400114 except:
kelvin8ec71442015-01-15 16:57:00 -0800115 main.log.error( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400116 main.cleanup()
117 main.exit()
118
kelvin-onlabd3b64892015-01-20 13:26:24 -0800119 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800120 """
andrewonlab8790abb2014-11-06 13:51:54 -0500121 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800122 """
andrewonlab8790abb2014-11-06 13:51:54 -0500123 try:
kelvin8ec71442015-01-15 16:57:00 -0800124 self.handle.sendline( "onos-build" )
125 self.handle.expect( "onos-build" )
126 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800127 "BUILD SUCCESS",
128 "ERROR",
129 "BUILD FAILED" ],
130 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800131 handle = str( self.handle.before )
andrewonlab8790abb2014-11-06 13:51:54 -0500132
kelvin8ec71442015-01-15 16:57:00 -0800133 main.log.info( "onos-build command returned: " +
134 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500135
136 if i == 0:
137 return main.TRUE
138 else:
139 return handle
140
141 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800142 main.log.error( self.name + ": EOF exception found" )
143 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8790abb2014-11-06 13:51:54 -0500144 except:
kelvin8ec71442015-01-15 16:57:00 -0800145 main.log.error( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500146 main.cleanup()
147 main.exit()
148
kelvin-onlabd3b64892015-01-20 13:26:24 -0800149 def cleanInstall( self ):
kelvin8ec71442015-01-15 16:57:00 -0800150 """
151 Runs mvn clean install in the root of the ONOS directory.
152 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400153
kelvin8ec71442015-01-15 16:57:00 -0800154 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400155 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800156 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400157 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800158 main.log.info( "Running 'mvn clean install' on " +
159 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800160 ". This may take some time." )
161 self.handle.sendline( "cd " + self.home )
162 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400163
kelvin8ec71442015-01-15 16:57:00 -0800164 self.handle.sendline( "" )
165 self.handle.expect( "\$" )
166 self.handle.sendline( "mvn clean install" )
167 self.handle.expect( "mvn clean install" )
168 while True:
169 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800170 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
171 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400172 'BUILD\sFAILURE',
173 'BUILD\sSUCCESS',
174 'ONOS\$',
kelvin8ec71442015-01-15 16:57:00 -0800175 pexpect.TIMEOUT ], timeout=600 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400176 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800177 main.log.error( self.name + ":There is insufficient memory \
178 for the Java Runtime Environment to continue." )
179 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400180 main.cleanup()
181 main.exit()
182 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800183 main.log.error( self.name + ": Build failure!" )
184 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400185 main.cleanup()
186 main.exit()
187 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800188 main.log.info( self.name + ": Build success!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400189 elif i == 3:
kelvin8ec71442015-01-15 16:57:00 -0800190 main.log.info( self.name + ": Build complete" )
191 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400192 for line in self.handle.before.splitlines():
193 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800194 main.log.info( line )
195 self.handle.sendline( "" )
196 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400197 return main.TRUE
198 elif i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800199 main.log.error(
200 self.name +
201 ": mvn clean install TIMEOUT!" )
202 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400203 main.cleanup()
204 main.exit()
205 else:
Jon Hall274b6642015-02-17 11:57:17 -0800206 main.log.error( self.name + ": unexpected response from " +
207 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800208 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400209 main.cleanup()
210 main.exit()
211 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800212 main.log.error( self.name + ": EOF exception found" )
213 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400214 main.cleanup()
215 main.exit()
216 except:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800217 main.log.info( self.name + ":" * 60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400218 main.log.error( traceback.print_exc() )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800219 main.log.info( ":" * 60 )
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()
321 except:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800322 main.log.info( self.name + ":" * 60 )
Jon Hallacabffd2014-10-09 12:36:53 -0400323 main.log.error( traceback.print_exc() )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800324 main.log.info( ":" * 80 )
Jon Hallacabffd2014-10-09 12:36:53 -0400325 main.cleanup()
326 main.exit()
327
kelvin-onlabd3b64892015-01-20 13:26:24 -0800328 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800329 """
Jon Hallacabffd2014-10-09 12:36:53 -0400330 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800331
Jon Hallacabffd2014-10-09 12:36:53 -0400332 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800333 If used as gitCheckout( "branch" ) it will do git checkout
334 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400335
336 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800337 branch of the ONOS repository. If it has any problems, it will return
338 main.ERROR.
339 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400340 successful then the function will return main.TRUE.
341
kelvin8ec71442015-01-15 16:57:00 -0800342 """
Jon Hallacabffd2014-10-09 12:36:53 -0400343 try:
kelvin8ec71442015-01-15 16:57:00 -0800344 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800345 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800346 main.log.info( self.name +
347 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800348 cmd = "git checkout " + branch
349 self.handle.sendline( cmd )
350 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800351 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800352 [ 'fatal',
353 'Username\sfor\s(.*):\s',
354 'Already\son\s\'',
355 'Switched\sto\sbranch\s\'' + str( branch ),
356 pexpect.TIMEOUT,
357 'error: Your local changes to the following files' +
358 'would be overwritten by checkout:',
359 'error: you need to resolve your current index first',
360 "You are in 'detached HEAD' state.",
361 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800362 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800363 if i == 0:
364 main.log.error(
365 self.name +
366 ": Git checkout had some issue..." )
367 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400368 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800369 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800370 main.log.error(
371 self.name +
372 ": Git checkout asking for username." +
373 " Please configure your local git repository to be able " +
374 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800375 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400376 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800377 elif i == 2:
378 main.log.info(
379 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800380 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800381 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800382 # main.log.info( "DEBUG: after checkout cmd = "+
383 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400384 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800385 elif i == 3:
386 main.log.info(
387 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800388 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800389 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800390 # main.log.info( "DEBUG: after checkout cmd = "+
391 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400392 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800393 elif i == 4:
394 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
395 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800396 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400397 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800398 elif i == 5:
399 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800400 main.log.error(
401 self.name +
402 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800403 "Your local changes to the following files would" +
404 " be overwritten by checkout:" +
405 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800406 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500407 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800408 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800409 main.log.error(
410 self.name +
411 ": Git checkout error: \n" +
412 "You need to resolve your current index first:" +
413 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800414 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500415 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800416 elif i == 7:
417 main.log.info(
418 self.name +
419 ": Git checkout " + str( branch ) +
420 " - You are in 'detached HEAD' state. HEAD is now at " +
421 str( branch ) )
422 self.handle.expect( self.home + "\$" )
423 return main.TRUE
424 elif i == 8: # Already in detached HEAD on the specified commit
425 main.log.info(
426 self.name +
427 ": Git Checkout %s : Already on commit" % branch )
428 self.handle.expect( self.home + "\$" )
429 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400430 else:
kelvin8ec71442015-01-15 16:57:00 -0800431 main.log.error(
432 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800433 ": Git Checkout - Unexpected response, " +
434 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800435 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400436 return main.ERROR
437
438 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800439 main.log.error( self.name + ": EOF exception found" )
440 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400441 main.cleanup()
442 main.exit()
443 except:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800444 main.log.info( self.name + ":" * 60 )
Jon Hallacabffd2014-10-09 12:36:53 -0400445 main.log.error( traceback.print_exc() )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800446 main.log.info( ":" * 80 )
Jon Hallacabffd2014-10-09 12:36:53 -0400447 main.cleanup()
448 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400449
kelvin-onlabd3b64892015-01-20 13:26:24 -0800450 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800451 """
Jon Hall274b6642015-02-17 11:57:17 -0800452 Writes the COMMIT number to the report to be parsed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800453 by Jenkins data collecter.
kelvin8ec71442015-01-15 16:57:00 -0800454 """
Jon Hall45ec0922014-10-10 19:33:49 -0400455 try:
kelvin8ec71442015-01-15 16:57:00 -0800456 self.handle.sendline( "" )
457 self.handle.expect( "\$" )
458 self.handle.sendline(
459 "cd " +
460 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800461 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
462 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800463 # NOTE: for some reason there are backspaces inserted in this
464 # phrase when run from Jenkins on some tests
465 self.handle.expect( "never" )
466 self.handle.expect( "\$" )
467 response = ( self.name + ": \n" + str(
468 self.handle.before + self.handle.after ) )
469 self.handle.sendline( "cd " + self.home )
470 self.handle.expect( "\$" )
471 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400472 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500473 print line
474 if report:
kelvin8ec71442015-01-15 16:57:00 -0800475 for line in lines[ 2:-1 ]:
476 # Bracket replacement is for Wiki-compliant
477 # formatting. '<' or '>' are interpreted
478 # as xml specific tags that cause errors
479 line = line.replace( "<", "[" )
480 line = line.replace( ">", "]" )
481 main.log.report( "\t" + line )
482 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400483 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800484 main.log.error( self.name + ": EOF exception found" )
485 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400486 main.cleanup()
487 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800488 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800489 main.log.error( self.name + ": TIMEOUT exception found" )
490 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800491 main.cleanup()
492 main.exit()
Jon Hall45ec0922014-10-10 19:33:49 -0400493 except:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800494 main.log.info( self.name + ":" * 60 )
Jon Hall45ec0922014-10-10 19:33:49 -0400495 main.log.error( traceback.print_exc() )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800496 main.log.info( ":" * 80 )
Jon Hall45ec0922014-10-10 19:33:49 -0400497 main.cleanup()
498 main.exit()
499
kelvin-onlabd3b64892015-01-20 13:26:24 -0800500 def createCellFile( self, benchIp, fileName, mnIpAddrs,
501 extraFeatureString, *onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800502 """
andrewonlab94282092014-10-10 13:00:11 -0400503 Creates a cell file based on arguments
504 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800505 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400506 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800507 * File name of the cell file ( fileName )
508 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800509 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400510 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800511 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400512 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800513
andrewonlab94282092014-10-10 13:00:11 -0400514 NOTE: Assumes cells are located at:
515 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800516 """
517 # Variable initialization
kelvin-onlabd3b64892015-01-20 13:26:24 -0800518 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800519 # We want to create the cell file in the dependencies directory
520 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800521 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800522 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800523 cellFile = open( tempDirectory + fileName, 'w+' )
kelvin8ec71442015-01-15 16:57:00 -0800524
525 # Feature string is hardcoded environment variables
526 # That you may wish to use by default on startup.
527 # Note that you may not want certain features listed
528 # on here.
jenkinsbd0e7532015-02-10 14:32:54 -0800529 coreFeatureString = "export ONOS_FEATURES=" + extraFeatureString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800530 mnString = "export OCN="
531 onosString = "export OC"
532 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800533
kelvin-onlabd3b64892015-01-20 13:26:24 -0800534 # Create ONOSNIC ip address prefix
535 tempOnosIp = onosIpAddrs[ 0 ]
536 tempList = []
537 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800538 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800539 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800540 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800541 nicAddr = ".".join( tempList ) + ".*"
542 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400543
544 try:
kelvin8ec71442015-01-15 16:57:00 -0800545 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800546 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400547
kelvin-onlabd3b64892015-01-20 13:26:24 -0800548 for arg in onosIpAddrs:
549 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800550 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400551 # export OC1="10.128.20.11"
552 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800553 cellFile.write( onosString + str( tempCount ) +
554 "=" + "\"" + arg + "\"" + "\n" )
555 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800556
kelvin-onlabd3b64892015-01-20 13:26:24 -0800557 cellFile.write( mnString + "\"" + mnIpAddrs + "\"" + "\n" )
558 cellFile.write( coreFeatureString + "\n" )
559 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400560
kelvin8ec71442015-01-15 16:57:00 -0800561 # We use os.system to send the command to TestON cluster
562 # to account for the case in which TestON is not located
563 # on the same cluster as the ONOS bench
564 # Note that even if TestON is located on the same cluster
565 # as ONOS bench, you must setup passwordless ssh
566 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800567 os.system( "scp " + tempDirectory + fileName +
568 " admin@" + benchIp + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400569
andrewonlab2a6c9342014-10-16 13:40:15 -0400570 return main.TRUE
571
andrewonlab94282092014-10-10 13:00:11 -0400572 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800573 main.log.error( self.name + ": EOF exception found" )
574 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400575 main.cleanup()
576 main.exit()
577 except:
kelvin8ec71442015-01-15 16:57:00 -0800578 main.log.info( self.name + ":::::::::" )
andrewonlab94282092014-10-10 13:00:11 -0400579 main.log.error( traceback.print_exc() )
kelvin8ec71442015-01-15 16:57:00 -0800580 main.log.info( ":::::::" )
andrewonlab94282092014-10-10 13:00:11 -0400581 main.cleanup()
582 main.exit()
583
kelvin-onlabd3b64892015-01-20 13:26:24 -0800584 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800585 """
andrewonlab95ca1462014-10-09 14:04:24 -0400586 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800587 """
andrewonlab95ca1462014-10-09 14:04:24 -0400588 try:
589 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800590 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400591 main.cleanup()
592 main.exit()
593 else:
kelvin8ec71442015-01-15 16:57:00 -0800594 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800595 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800596 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400597 # and that this driver will have to change accordingly
jenkinsbd0e7532015-02-10 14:32:54 -0800598 self.handle.expect( "ONOS_CELL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800599 handleBefore = self.handle.before
600 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800601 # Get the rest of the handle
602 self.handle.sendline( "" )
603 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800604 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400605
kelvin-onlabd3b64892015-01-20 13:26:24 -0800606 main.log.info( "Cell call returned: " + handleBefore +
607 handleAfter + handleMore )
andrewonlab95ca1462014-10-09 14:04:24 -0400608
609 return main.TRUE
610
611 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800612 main.log.error( self.name + ": EOF exception found" )
613 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400614 main.cleanup()
615 main.exit()
616 except:
kelvin8ec71442015-01-15 16:57:00 -0800617 main.log.info( self.name + " ::::::" )
618 main.log.error( traceback.print_exc() )
619 main.log.info( self.name + " ::::::" )
andrewonlab95ca1462014-10-09 14:04:24 -0400620 main.cleanup()
621 main.exit()
622
kelvin-onlabd3b64892015-01-20 13:26:24 -0800623 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800624 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400625 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800626 """
627 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400628
andrewonlabc03bf6c2014-10-09 14:56:18 -0400629 try:
kelvin8ec71442015-01-15 16:57:00 -0800630 # Clean handle by sending empty and expecting $
631 self.handle.sendline( "" )
632 self.handle.expect( "\$" )
633 self.handle.sendline( "onos-verify-cell" )
634 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800635 handleBefore = self.handle.before
636 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800637 # Get the rest of the handle
638 self.handle.sendline( "" )
639 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800640 handleMore = self.handle.before
andrewonlabc03bf6c2014-10-09 14:56:18 -0400641
kelvin-onlabd3b64892015-01-20 13:26:24 -0800642 main.log.info( "Verify cell returned: " + handleBefore +
643 handleAfter + handleMore )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400644
645 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800646 except pexpect.ExceptionPexpect as e:
647 main.log.error( self.name + ": Pexpect exception found of type " +
648 str( type( e ) ) )
649 main.log.error ( e.get_trace() )
kelvin8ec71442015-01-15 16:57:00 -0800650 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400651 main.cleanup()
652 main.exit()
653 except:
kelvin8ec71442015-01-15 16:57:00 -0800654 main.log.info( self.name + " ::::::" )
655 main.log.error( traceback.print_exc() )
656 main.log.info( self.name + " ::::::" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400657 main.cleanup()
658 main.exit()
659
kelvin-onlabd3b64892015-01-20 13:26:24 -0800660 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800661 """
andrewonlab05e362f2014-10-10 00:40:57 -0400662 Uses 'onos' command to send various ONOS CLI arguments.
663 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800664 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400665 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800666
667 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400668 CLI commands for ONOS. Try to use this function first
669 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800670 function.
671 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400672 by starting onos, and typing in 'onos' to enter the
673 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800674 available commands.
675 """
andrewonlab05e362f2014-10-10 00:40:57 -0400676 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800677 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800678 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400679 return main.FALSE
680 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800681 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400682 return main.FALSE
683
kelvin8ec71442015-01-15 16:57:00 -0800684 cmdstr = str( cmdstr )
685 self.handle.sendline( "" )
686 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400687
kelvin-onlabd3b64892015-01-20 13:26:24 -0800688 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800689 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400690
kelvin-onlabd3b64892015-01-20 13:26:24 -0800691 handleBefore = self.handle.before
Shreya Shaha73aaad2014-10-27 18:03:09 -0400692 print "handle_before = ", self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800693 # handleAfter = str( self.handle.after )
Jon Hall47a93fb2015-01-06 16:46:06 -0800694
kelvin8ec71442015-01-15 16:57:00 -0800695 # self.handle.sendline( "" )
696 # self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800697 # handleMore = str( self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400698
kelvin8ec71442015-01-15 16:57:00 -0800699 main.log.info( "Command sent successfully" )
andrewonlab05e362f2014-10-10 00:40:57 -0400700
kelvin8ec71442015-01-15 16:57:00 -0800701 # Obtain return handle that consists of result from
702 # the onos command. The string may need to be
703 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800704 # returnString = handleBefore + handleAfter
705 returnString = handleBefore
706 print "return_string = ", returnString
707 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400708
709 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800710 main.log.error( self.name + ": EOF exception found" )
711 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400712 main.cleanup()
713 main.exit()
714 except:
kelvin8ec71442015-01-15 16:57:00 -0800715 main.log.info( self.name + " ::::::" )
716 main.log.error( traceback.print_exc() )
717 main.log.info( self.name + " ::::::" )
andrewonlab05e362f2014-10-10 00:40:57 -0400718 main.cleanup()
719 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400720
kelvin-onlabd3b64892015-01-20 13:26:24 -0800721 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800722 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400723 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800724 If -f option is provided, it also forces an uninstall.
725 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400726 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800727 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400728 files to certain onos nodes
729
730 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800731 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400732 try:
andrewonlab114768a2014-11-14 12:44:44 -0500733 if options:
kelvin8ec71442015-01-15 16:57:00 -0800734 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500735 else:
kelvin8ec71442015-01-15 16:57:00 -0800736 self.handle.sendline( "onos-install " + node )
737 self.handle.expect( "onos-install " )
738 # NOTE: this timeout may need to change depending on the network
739 # and size of ONOS
740 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800741 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800742 "ONOS\sis\salready\sinstalled",
743 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400744
Jon Hall7993bfc2014-10-09 16:30:14 -0400745 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800746 main.log.warn( "Network is unreachable" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400747 return main.FALSE
748 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800749 main.log.info(
750 "ONOS was installed on " +
751 node +
752 " and started" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400753 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500754 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800755 main.log.info( "ONOS is already installed on " + node )
andrewonlabd9a73a72014-11-14 17:28:21 -0500756 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800757 elif i == 3:
758 main.log.info(
759 "Installation of ONOS on " +
760 node +
761 " timed out" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400762 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400763
764 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800765 main.log.error( self.name + ": EOF exception found" )
766 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400767 main.cleanup()
768 main.exit()
769 except:
kelvin8ec71442015-01-15 16:57:00 -0800770 main.log.info( self.name + " ::::::" )
771 main.log.error( traceback.print_exc() )
772 main.log.info( self.name + " ::::::" )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400773 main.cleanup()
774 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400775
kelvin-onlabd3b64892015-01-20 13:26:24 -0800776 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800777 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400778 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400779 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800780 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400781 try:
kelvin8ec71442015-01-15 16:57:00 -0800782 self.handle.sendline( "" )
783 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800784 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800785 " start" )
786 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -0400787 "Job\sis\salready\srunning",
788 "start/running",
789 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800790 pexpect.TIMEOUT ], timeout=120 )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400791
792 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800793 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400794 return main.TRUE
795 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800796 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400797 return main.TRUE
798 else:
kelvin8ec71442015-01-15 16:57:00 -0800799 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400800 main.cleanup()
801 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400802 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800803 main.log.error( self.name + ": EOF exception found" )
804 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400805 main.cleanup()
806 main.exit()
807 except:
kelvin8ec71442015-01-15 16:57:00 -0800808 main.log.info( self.name + " ::::::" )
809 main.log.error( traceback.print_exc() )
810 main.log.info( self.name + " ::::::" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400811 main.cleanup()
812 main.exit()
813
kelvin-onlabd3b64892015-01-20 13:26:24 -0800814 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800815 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400816 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400817 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800818 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400819 try:
kelvin8ec71442015-01-15 16:57:00 -0800820 self.handle.sendline( "" )
821 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800822 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800823 " stop" )
824 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -0400825 "stop/waiting",
826 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800827 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2b30bd32014-10-09 16:48:55 -0400828
829 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800830 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400831 return main.TRUE
832 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800833 main.log.info( "Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800834 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -0400835 return main.FALSE
836 else:
kelvin8ec71442015-01-15 16:57:00 -0800837 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400838 return main.FALSE
839
840 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800841 main.log.error( self.name + ": EOF exception found" )
842 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -0400843 main.cleanup()
844 main.exit()
845 except:
kelvin8ec71442015-01-15 16:57:00 -0800846 main.log.info( self.name + " ::::::" )
847 main.log.error( traceback.print_exc() )
848 main.log.info( self.name + " ::::::" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400849 main.cleanup()
850 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800851
kelvin-onlabd3b64892015-01-20 13:26:24 -0800852 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -0800853 """
andrewonlabc8d47972014-10-09 16:52:36 -0400854 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -0800855 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -0400856 if needed
kelvin8ec71442015-01-15 16:57:00 -0800857 """
andrewonlabc8d47972014-10-09 16:52:36 -0400858 try:
kelvin8ec71442015-01-15 16:57:00 -0800859 self.handle.sendline( "" )
860 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800861 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800862 self.handle.expect( "\$" )
andrewonlabc8d47972014-10-09 16:52:36 -0400863
kelvin-onlabd3b64892015-01-20 13:26:24 -0800864 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
andrewonlab9dfd2082014-11-13 17:44:03 -0500865
kelvin8ec71442015-01-15 16:57:00 -0800866 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -0400867 return main.TRUE
868
869 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800870 main.log.error( self.name + ": EOF exception found" )
871 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -0400872 main.cleanup()
873 main.exit()
874 except:
kelvin8ec71442015-01-15 16:57:00 -0800875 main.log.info( self.name + " ::::::" )
876 main.log.error( traceback.print_exc() )
877 main.log.info( self.name + " ::::::" )
andrewonlabc8d47972014-10-09 16:52:36 -0400878 main.cleanup()
879 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400880
kelvin-onlabd3b64892015-01-20 13:26:24 -0800881 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800882 """
andrewonlabaedc8332014-12-04 12:43:03 -0500883 Issues the command 'onos-die <node-ip>'
884 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -0800885 """
andrewonlabaedc8332014-12-04 12:43:03 -0500886 try:
kelvin8ec71442015-01-15 16:57:00 -0800887 self.handle.sendline( "" )
888 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800889 cmdStr = "onos-kill " + str( nodeIp )
890 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800891 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -0500892 "Killing\sONOS",
893 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -0800894 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -0500895 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800896 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800897 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -0500898 return main.TRUE
899 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800900 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -0500901 return main.FALSE
902 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800903 main.log.error( self.name + ": EOF exception found" )
904 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -0500905 main.cleanup()
906 main.exit()
907 except:
kelvin8ec71442015-01-15 16:57:00 -0800908 main.log.info( self.name + " ::::::" )
909 main.log.error( traceback.print_exc() )
910 main.log.info( self.name + " ::::::" )
andrewonlabaedc8332014-12-04 12:43:03 -0500911 main.cleanup()
912 main.exit()
913
kelvin-onlabd3b64892015-01-20 13:26:24 -0800914 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800915 """
andrewonlabe8e56fd2014-10-09 17:12:44 -0400916 Calls the command: 'onos-kill [<node-ip>]'
917 "Remotely, and unceremoniously kills the ONOS instance running on
918 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -0800919 """
andrewonlabe8e56fd2014-10-09 17:12:44 -0400920 try:
kelvin8ec71442015-01-15 16:57:00 -0800921 self.handle.sendline( "" )
922 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800923 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800924 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -0400925 "\$",
926 "No\sroute\sto\shost",
927 "password:",
kelvin8ec71442015-01-15 16:57:00 -0800928 pexpect.TIMEOUT ], timeout=20 )
929
andrewonlabe8e56fd2014-10-09 17:12:44 -0400930 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800931 main.log.info(
932 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800933 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400934 return main.TRUE
935 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800936 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400937 return main.FALSE
938 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800939 main.log.info(
940 "Passwordless login for host: " +
941 str( nodeIp ) +
942 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400943 return main.FALSE
944 else:
kelvin8ec71442015-01-15 16:57:00 -0800945 main.log.info( "ONOS instasnce was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400946 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -0800947
andrewonlabe8e56fd2014-10-09 17:12:44 -0400948 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800949 main.log.error( self.name + ": EOF exception found" )
950 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400951 main.cleanup()
952 main.exit()
953 except:
kelvin8ec71442015-01-15 16:57:00 -0800954 main.log.info( self.name + " ::::::" )
955 main.log.error( traceback.print_exc() )
956 main.log.info( self.name + " ::::::" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400957 main.cleanup()
958 main.exit()
959
kelvin-onlabd3b64892015-01-20 13:26:24 -0800960 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -0800961 """
andrewonlab19fbdca2014-11-14 12:55:59 -0500962 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -0500963 a cleaner environment.
964
andrewonlab19fbdca2014-11-14 12:55:59 -0500965 Description:
Jon Hallfcc88622014-11-25 13:09:54 -0500966 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -0500967 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -0800968 """
andrewonlab19fbdca2014-11-14 12:55:59 -0500969 try:
kelvin8ec71442015-01-15 16:57:00 -0800970 self.handle.sendline( "" )
971 self.handle.expect( "\$" )
972 self.handle.sendline( "onos-remove-raft-logs" )
973 # Sometimes this command hangs
974 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
975 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -0500976 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800977 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
978 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -0500979 if i == 1:
980 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -0800981 self.handle.sendline( "" )
982 self.handle.expect( "\$" )
andrewonlab19fbdca2014-11-14 12:55:59 -0500983 return main.TRUE
984
985 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800986 main.log.error( self.name + ": EOF exception found" )
987 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -0500988 main.cleanup()
989 main.exit()
990 except:
kelvin8ec71442015-01-15 16:57:00 -0800991 main.log.info( self.name + " ::::::" )
992 main.log.error( traceback.print_exc() )
993 main.log.info( self.name + " ::::::" )
andrewonlab19fbdca2014-11-14 12:55:59 -0500994 main.cleanup()
995 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -0500996
kelvin-onlabd3b64892015-01-20 13:26:24 -0800997 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -0800998 """
999 Calls the command 'onos-start-network [ <mininet-topo> ]
1000 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001001 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001002 cell."
andrewonlab94282092014-10-10 13:00:11 -04001003 * Specify mininet topology file name for mntopo
1004 * Topo files should be placed at:
1005 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001006
andrewonlab94282092014-10-10 13:00:11 -04001007 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001008 """
andrewonlab94282092014-10-10 13:00:11 -04001009 try:
1010 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001011 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001012 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001013
kelvin8ec71442015-01-15 16:57:00 -08001014 mntopo = str( mntopo )
1015 self.handle.sendline( "" )
1016 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001017
kelvin8ec71442015-01-15 16:57:00 -08001018 self.handle.sendline( "onos-start-network " + mntopo )
1019 self.handle.expect( "mininet>" )
1020 main.log.info( "Network started, entered mininet prompt" )
1021
1022 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001023
1024 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001025 main.log.error( self.name + ": EOF exception found" )
1026 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001027 main.cleanup()
1028 main.exit()
1029 except:
kelvin8ec71442015-01-15 16:57:00 -08001030 main.log.info( self.name + " ::::::" )
1031 main.log.error( traceback.print_exc() )
1032 main.log.info( self.name + " ::::::" )
andrewonlab94282092014-10-10 13:00:11 -04001033 main.cleanup()
1034 main.exit()
1035
kelvin8ec71442015-01-15 16:57:00 -08001036 def isup( self, node="" ):
1037 """
1038 Run's onos-wait-for-start which only returns once ONOS is at run
1039 level 100( ready for use )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001040
Jon Hall7993bfc2014-10-09 16:30:14 -04001041 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001042 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001043 try:
kelvin8ec71442015-01-15 16:57:00 -08001044 self.handle.sendline( "onos-wait-for-start " + node )
1045 self.handle.expect( "onos-wait-for-start" )
1046 # NOTE: this timeout is arbitrary"
1047 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ], timeout=120 )
Jon Hall7993bfc2014-10-09 16:30:14 -04001048 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001049 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001050 return main.TRUE
1051 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001052 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001053 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001054 main.log.error( "ONOS has not started yet" )
1055 self.handle.send( "\x03" ) # Control-C
1056 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001057 return main.FALSE
1058 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001059 main.log.error( self.name + ": EOF exception found" )
1060 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001061 main.cleanup()
1062 main.exit()
1063 except:
kelvin8ec71442015-01-15 16:57:00 -08001064 main.log.info( self.name + " ::::::" )
1065 main.log.error( traceback.print_exc() )
1066 main.log.info( self.name + " ::::::" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001067 main.cleanup()
1068 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001069
kelvin-onlabd3b64892015-01-20 13:26:24 -08001070 def pushTestIntentsShell(
1071 self,
1072 dpidSrc,
1073 dpidDst,
1074 numIntents,
1075 dirFile,
1076 onosIp,
1077 numMult="",
1078 appId="",
1079 report=True,
1080 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001081 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001082 Description:
kelvin8ec71442015-01-15 16:57:00 -08001083 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001084 better parallelize the results than the CLI
1085 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001086 * dpidSrc: specify source dpid
1087 * dpidDst: specify destination dpid
1088 * numIntents: specify number of intents to push
1089 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001090 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001091 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001092 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001093 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001094 """
1095 try:
1096 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001097 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001098 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001099 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001100 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001101 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001102
kelvin-onlabd3b64892015-01-20 13:26:24 -08001103 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1104 if not numMult:
1105 addIntents = addDpid + " " + str( numIntents )
1106 elif numMult:
1107 addIntents = addDpid + " " + str( numIntents ) + " " +\
1108 str( numMult )
1109 if appId:
1110 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001111 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001112 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001113
andrewonlabaedc8332014-12-04 12:43:03 -05001114 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001115 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001116 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001117 sendCmd = addApp + " &"
1118 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001119
kelvin-onlabd3b64892015-01-20 13:26:24 -08001120 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001121
1122 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001123 main.log.error( self.name + ": EOF exception found" )
1124 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001125 main.cleanup()
1126 main.exit()
1127 except:
kelvin8ec71442015-01-15 16:57:00 -08001128 main.log.info( self.name + " ::::::" )
1129 main.log.error( traceback.print_exc() )
1130 main.log.info( self.name + " ::::::" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001131 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001132 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001133
kelvin-onlabd3b64892015-01-20 13:26:24 -08001134 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001135 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001136 parses the onos:topology output
kelvin8ec71442015-01-15 16:57:00 -08001137 Returns: a topology dict populated by the key values found in
Jon Hall77f53ce2014-10-13 18:02:06 -04001138 the cli command.
kelvin8ec71442015-01-15 16:57:00 -08001139 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001140 try:
kelvin8ec71442015-01-15 16:57:00 -08001141 # call the cli to get the topology summary
1142 # cmdstr = "onos:topology"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001143 # cliResult = self.onosCli( ip, cmdstr )
1144 # print "cli_result = ", cliResult
Jon Hall77f53ce2014-10-13 18:02:06 -04001145
kelvin8ec71442015-01-15 16:57:00 -08001146 # Parse the output
Jon Hall77f53ce2014-10-13 18:02:06 -04001147 topology = {}
kelvin-onlabd3b64892015-01-20 13:26:24 -08001148 # for line in cliResult.split( "\n" ):
1149 for line in topologyOutput.splitlines():
kelvin8ec71442015-01-15 16:57:00 -08001150 if not line.startswith( "time=" ):
Jon Hall77f53ce2014-10-13 18:02:06 -04001151 continue
kelvin8ec71442015-01-15 16:57:00 -08001152 # else
1153 # print line
1154 for var in line.split( "," ):
1155 # print "'"+var+"'"
1156 # print "'"+var.strip()+"'"
1157 key, value = var.strip().split( "=" )
1158 topology[ key ] = value
1159 # print "topology = ", topology
1160 # devices = topology.get( 'devices', False )
1161 # print "devices = ", devices
1162 # links = topology.get( 'links', False )
1163 # print "links = ", links
1164 # SCCs = topology.get( 'SCC(s)', False )
1165 # print "SCCs = ", SCCs
1166 # paths = topology.get( 'paths', False )
1167 # print "paths = ", paths
Jon Hall77f53ce2014-10-13 18:02:06 -04001168
1169 return topology
1170 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001171 main.log.error( self.name + ": EOF exception found" )
1172 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001173 main.cleanup()
1174 main.exit()
1175 except:
kelvin8ec71442015-01-15 16:57:00 -08001176 main.log.info( self.name + " ::::::" )
1177 main.log.error( traceback.print_exc() )
1178 main.log.info( self.name + " ::::::" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001179 main.cleanup()
1180 main.exit()
1181
kelvin-onlabd3b64892015-01-20 13:26:24 -08001182 def checkStatus(
1183 self,
1184 topologyResult,
1185 numoswitch,
1186 numolink,
1187 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001188 """
1189 Checks the number of swithes & links that ONOS sees against the
1190 supplied values. By default this will report to main.log, but the
Jon Hall77f53ce2014-10-13 18:02:06 -04001191 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001192
Jon Hall77f53ce2014-10-13 18:02:06 -04001193 Params: ip = ip used for the onos cli
1194 numoswitch = expected number of switches
1195 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001196 logLevel = level to log to.
1197 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001198
1199
kelvin-onlabd3b64892015-01-20 13:26:24 -08001200 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001201
kelvin8ec71442015-01-15 16:57:00 -08001202 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall77f53ce2014-10-13 18:02:06 -04001203 main.FALSE if the numer of switches and links is incorrect,
1204 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001205 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001206 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001207 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001208 if topology == {}:
1209 return main.ERROR
1210 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001211 # Is the number of switches is what we expected
1212 devices = topology.get( 'devices', False )
1213 links = topology.get( 'links', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001214 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001215 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001216 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001217 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001218 linkCheck = ( int( links ) == int( numolink ) )
1219 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001220 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001221 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001222 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001223 result = main.TRUE
1224 else:
1225 output = output + \
Jon Hall274b6642015-02-17 11:57:17 -08001226 "The number of links and switches does not match " + \
1227 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001228 result = main.FALSE
Jon Hall274b6642015-02-17 11:57:17 -08001229 output = output + "\n ONOS sees %i devices" % int ( devices )
1230 output = output + " (%i expected) " % int( numoswitch )
1231 output = output + "and %i links " % int( links )
1232 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001233 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001234 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001235 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001236 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001237 else:
kelvin8ec71442015-01-15 16:57:00 -08001238 main.log.info( output )
1239 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001240 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001241 main.log.error( self.name + ": EOF exception found" )
1242 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001243 main.cleanup()
1244 main.exit()
1245 except:
kelvin8ec71442015-01-15 16:57:00 -08001246 main.log.info( self.name + " ::::::" )
1247 main.log.error( traceback.print_exc() )
1248 main.log.info( self.name + " ::::::" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001249 main.cleanup()
1250 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001251
kelvin-onlabd3b64892015-01-20 13:26:24 -08001252 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001253 """
andrewonlab970399c2014-11-07 13:09:32 -05001254 Capture all packet activity and store in specified
1255 directory/file
1256
1257 Required:
1258 * interface: interface to capture
1259 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001260 """
1261 self.handle.sendline( "" )
1262 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001263
kelvin8ec71442015-01-15 16:57:00 -08001264 self.handle.sendline( "tshark -i " + str( interface ) +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001265 " -t e -w " + str( dirFile ) + " &" )
kelvin8ec71442015-01-15 16:57:00 -08001266 self.handle.sendline( "\r" )
1267 self.handle.expect( "Capturing on" )
1268 self.handle.sendline( "\r" )
1269 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001270
kelvin8ec71442015-01-15 16:57:00 -08001271 main.log.info( "Tshark started capturing files on " +
1272 str( interface ) + " and saving to directory: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001273 str( dirFile ) )
Shreya Shaha73aaad2014-10-27 18:03:09 -04001274
kelvin-onlabd3b64892015-01-20 13:26:24 -08001275 def runOnosTopoCfg( self, instanceName, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001276 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001277 On ONOS bench, run this command:
1278 ./~/ONOS/tools/test/bin/onos-topo-cfg $OC1 filename
1279 which starts the rest and copies
1280 the json file to the onos instance
kelvin8ec71442015-01-15 16:57:00 -08001281 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001282 try:
kelvin8ec71442015-01-15 16:57:00 -08001283 self.handle.sendline( "" )
1284 self.handle.expect( "\$" )
1285 self.handle.sendline( "cd ~/ONOS/tools/test/bin" )
1286 self.handle.expect( "/bin$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001287 cmd = "./onos-topo-cfg " + instanceName + " " + jsonFile
shahshreyae6c7cf42014-11-26 16:39:01 -08001288 print "cmd = ", cmd
kelvin8ec71442015-01-15 16:57:00 -08001289 self.handle.sendline( cmd )
1290 self.handle.expect( "\$" )
1291 self.handle.sendline( "cd ~" )
1292 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -08001293 return main.TRUE
1294 except:
1295 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001296
kelvin-onlabd3b64892015-01-20 13:26:24 -08001297 def tsharkGrep( self, grep, directory, interface='eth0' ):
kelvin8ec71442015-01-15 16:57:00 -08001298 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001299 Required:
kelvin8ec71442015-01-15 16:57:00 -08001300 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001301 * directory to store results
1302 Optional:
1303 * interface - default: eth0
1304 Description:
1305 Uses tshark command to grep specific group of packets
1306 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001307 The timestamp is hardcoded to be in epoch
1308 """
1309 self.handle.sendline( "" )
1310 self.handle.expect( "\$" )
1311 self.handle.sendline( "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001312 self.handle.sendline(
1313 "tshark -i " +
1314 str( interface ) +
1315 " -t e | grep --line-buffered \"" +
1316 str(grep) +
1317 "\" >" +
1318 directory +
1319 " &" )
kelvin8ec71442015-01-15 16:57:00 -08001320 self.handle.sendline( "\r" )
1321 self.handle.expect( "Capturing on" )
1322 self.handle.sendline( "\r" )
1323 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001324
kelvin-onlabd3b64892015-01-20 13:26:24 -08001325 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001326 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001327 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001328 """
1329 # Remove all pcap from previous captures
1330 self.execute( cmd="sudo rm /tmp/wireshark*" )
1331 self.handle.sendline( "" )
1332 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\" |" +
1333 " grep -v grep | awk '{print $2}'`" )
1334 self.handle.sendline( "" )
1335 main.log.info( "Tshark stopped" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001336
kelvin8ec71442015-01-15 16:57:00 -08001337 def ptpd( self, args ):
1338 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001339 Initiate ptp with user-specified args.
1340 Required:
1341 * args: specify string of args after command
1342 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001343 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001344 try:
kelvin8ec71442015-01-15 16:57:00 -08001345 self.handle.sendline( "sudo ptpd " + str( args ) )
1346 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001347 "Multiple",
1348 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001349 "\$" ] )
1350 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001351
andrewonlab0c38a4a2014-10-28 18:35:35 -04001352 if i == 0:
1353 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001354 main.log.info( "ptpd returned an error: " +
1355 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001356 return handle
1357 elif i == 1:
1358 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001359 main.log.error( "ptpd returned an error: " +
1360 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001361 return handle
1362 else:
1363 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001364
andrewonlab0c38a4a2014-10-28 18:35:35 -04001365 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001366 main.log.error( self.name + ": EOF exception found" )
1367 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001368 main.cleanup()
1369 main.exit()
1370 except:
kelvin8ec71442015-01-15 16:57:00 -08001371 main.log.info( self.name + " ::::::" )
1372 main.log.error( traceback.print_exc() )
1373 main.log.info( self.name + " ::::::" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001374 main.cleanup()
1375 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001376
kelvin-onlabd3b64892015-01-20 13:26:24 -08001377 def cpLogsToDir( self, logToCopy,
1378 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001379 """
1380 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001381 Current implementation of ONOS deletes its karaf
1382 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001383 you may want to use this function to capture
1384 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001385 Localtime will be attached to the filename
1386
1387 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001388 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001389 copy.
kelvin8ec71442015-01-15 16:57:00 -08001390 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001391 For copying multiple files, leave copyFileName
1392 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001393 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001394 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001395 ex ) /tmp/
1396 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001397 * copyFileName: If you want to rename the log
1398 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001399 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001400 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001401 try:
kelvin8ec71442015-01-15 16:57:00 -08001402 localtime = time.strftime( '%x %X' )
1403 localtime = localtime.replace( "/", "" )
1404 localtime = localtime.replace( " ", "_" )
1405 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001406 if destDir[ -1: ] != "/":
1407 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001408
kelvin-onlabd3b64892015-01-20 13:26:24 -08001409 if copyFileName:
1410 self.handle.sendline(
1411 "cp " +
1412 str( logToCopy ) +
1413 " " +
1414 str( destDir ) +
1415 str( copyFileName ) +
1416 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001417 self.handle.expect( "cp" )
1418 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001419 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001420 self.handle.sendline( "cp " + str( logToCopy ) +
1421 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001422 self.handle.expect( "cp" )
1423 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001424
kelvin8ec71442015-01-15 16:57:00 -08001425 return self.handle.before
1426
1427 except pexpect.EOF:
1428 main.log.error( "Copying files failed" )
1429 main.log.error( self.name + ": EOF exception found" )
1430 main.log.error( self.name + ": " + self.handle.before )
1431 except:
1432 main.log.error( "Copying files failed" )
1433 main.log.info( self.name + " ::::::" )
1434 main.log.error( traceback.print_exc() )
1435 main.log.info( self.name + " ::::::" )
1436
kelvin-onlabd3b64892015-01-20 13:26:24 -08001437 def checkLogs( self, onosIp ):
kelvin8ec71442015-01-15 16:57:00 -08001438 """
Jon Hall94fd0472014-12-08 11:52:42 -08001439 runs onos-check-logs on the given onos node
1440 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001441 """
Jon Hall94fd0472014-12-08 11:52:42 -08001442 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001443 cmd = "onos-check-logs " + str( onosIp )
kelvin8ec71442015-01-15 16:57:00 -08001444 self.handle.sendline( cmd )
1445 self.handle.expect( cmd )
1446 self.handle.expect( "\$" )
Jon Hall94fd0472014-12-08 11:52:42 -08001447 response = self.handle.before
1448 return response
1449 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001450 main.log.error( "Lost ssh connection" )
1451 main.log.error( self.name + ": EOF exception found" )
1452 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001453 except:
kelvin8ec71442015-01-15 16:57:00 -08001454 main.log.error( "Some error in check_logs:" )
1455 main.log.info( self.name + " ::::::" )
1456 main.log.error( traceback.print_exc() )
1457 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001458
kelvin-onlabd3b64892015-01-20 13:26:24 -08001459 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001460 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001461 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001462 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001463 try:
kelvin8ec71442015-01-15 16:57:00 -08001464 self.handle.sendline( "" )
1465 self.handle.expect( "\$" )
1466 self.handle.sendline( "onos-service " + str( node ) +
1467 " status" )
1468 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001469 "start/running",
1470 "stop/waiting",
kelvin8ec71442015-01-15 16:57:00 -08001471 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001472
1473 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001474 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001475 return main.TRUE
1476 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001477 main.log.info( "ONOS is stopped" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001478 return main.FALSE
1479 else:
kelvin8ec71442015-01-15 16:57:00 -08001480 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001481 main.cleanup()
1482 main.exit()
1483 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001484 main.log.error( self.name + ": EOF exception found" )
1485 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001486 main.cleanup()
1487 main.exit()
1488 except:
kelvin8ec71442015-01-15 16:57:00 -08001489 main.log.info( self.name + " ::::::" )
1490 main.log.error( traceback.print_exc() )
1491 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001492 main.cleanup()
1493 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001494
Jon Hall63604932015-02-26 17:09:50 -08001495 def setIpTables( self, ip, port='', action='add', packet_type='',
1496 direction='INPUT', rule='DROP', states=True ):
Jon Hall21270ac2015-02-16 17:59:55 -08001497 '''
1498 Description:
1499 add or remove iptables rule to DROP (default) packets from
1500 specific IP and PORT
1501 Usage:
1502 * specify action ('add' or 'remove')
1503 when removing, pass in the same argument as you would add. It will
1504 delete that specific rule.
1505 * specify the ip to block
1506 * specify the destination port to block (defaults to all ports)
1507 * optional packet type to block (default tcp)
1508 * optional iptables rule (default DROP)
1509 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001510 * States boolean toggles adding all supported tcp states to the
1511 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001512 Returns:
1513 main.TRUE on success or
1514 main.FALSE if given invalid input or
1515 main.ERROR if there is an error in response from iptables
1516 WARNING:
1517 * This function uses root privilege iptables command which may result
1518 in unwanted network errors. USE WITH CAUTION
1519 '''
1520 import time
1521
1522 # NOTE*********
1523 # The strict checking methods of this driver function is intentional
1524 # to discourage any misuse or error of iptables, which can cause
1525 # severe network errors
1526 # *************
1527
1528 # NOTE: Sleep needed to give some time for rule to be added and
1529 # registered to the instance. If you are calling this function
1530 # multiple times this sleep will prevent any errors.
1531 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001532 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001533 try:
1534 # input validation
1535 action_type = action.lower()
1536 rule = rule.upper()
1537 direction = direction.upper()
1538 if action_type != 'add' and action_type != 'remove':
1539 main.log.error( "Invalid action type. Use 'add' or "
1540 "'remove' table rule" )
1541 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1542 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1543 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1544 "'ACCEPT' or 'LOG' only." )
1545 if direction != 'INPUT' and direction != 'OUTPUT':
1546 # NOTE currently only supports rules INPUT and OUPTUT
1547 main.log.error( "Invalid rule. Valid directions are"
1548 " 'OUTPUT' or 'INPUT'" )
1549 return main.FALSE
1550 return main.FALSE
1551 return main.FALSE
1552 if action_type == 'add':
1553 # -A is the 'append' action of iptables
1554 actionFlag = '-A'
1555 elif action_type == 'remove':
1556 # -D is the 'delete' rule of iptables
1557 actionFlag = '-D'
1558 self.handle.sendline( "" )
1559 self.handle.expect( "\$" )
1560 cmd = "sudo iptables " + actionFlag + " " +\
1561 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001562 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001563 # " -p " + str( packet_type ) +\
1564 if packet_type:
1565 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001566 if port:
1567 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001568 if states:
1569 cmd += " -m state --state="
1570 #FIXME- Allow user to configure which states to block
1571 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001572 cmd += " -j " + str( rule )
1573
1574 self.handle.sendline( cmd )
1575 self.handle.expect( "\$" )
1576 main.log.warn( self.handle.before )
1577
1578 info_string = "On " + str( self.name )
1579 info_string += " " + str( action_type )
1580 info_string += " iptable rule [ "
1581 info_string += " IP: " + str( ip )
1582 info_string += " Port: " + str( port )
1583 info_string += " Rule: " + str( rule )
1584 info_string += " Direction: " + str( direction ) + " ]"
1585 main.log.info( info_string )
1586 return main.TRUE
1587 except pexpect.TIMEOUT:
1588 main.log.exception( self.name + ": Timeout exception in "
1589 "setIpTables function" )
1590 return main.ERROR
1591 except pexpect.EOF:
1592 main.log.error( self.name + ": EOF exception found" )
1593 main.log.error( self.name + ": " + self.handle.before )
1594 main.cleanup()
1595 main.exit()
1596 except:
1597 main.log.exception( "Unknown error:")
1598 main.cleanup()
1599 main.exit()
1600
Jon Hall0468b042015-02-19 19:08:21 -08001601 def detailed_status(self, log_filename):
1602 '''
1603 This method is used by STS to check the status of the controller
1604 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
1605 '''
1606 import re
1607 try:
1608 self.handle.sendline( "" )
1609 self.handle.expect( "\$" )
1610 self.handle.sendline( "cd " + self.home )
1611 self.handle.expect( "\$" )
1612 self.handle.sendline( "service onos status" )
1613 self.handle.expect( "\$" )
1614 response = self.handle.before
1615 if re.search( "onos start/running", response ):
1616 # onos start/running, process 10457
1617 return 'RUNNING'
1618 # FIXME: Implement this case
1619 # elif re.search( pattern, response ):
1620 # return 'STARTING'
1621 elif re.search( "onos stop/", response ):
1622 # onos stop/waiting
1623 # FIXME handle this differently?: onos stop/pre-stop
1624 return 'STOPPED'
1625 # FIXME: Implement this case
1626 # elif re.search( pattern, response ):
1627 # return 'FROZEN'
1628 else:
1629 main.log.warn( self.name +
1630 " WARNING: status recieved unknown response" )
1631 main.log.warn( response )
1632 return 'ERROR', "Unknown response: %s" % response
1633 except pexpect.TIMEOUT:
1634 main.log.exception( self.name + ": Timeout exception in "
1635 "setIpTables function" )
1636 return 'ERROR', "Pexpect Timeout"
1637 except pexpect.EOF:
1638 main.log.error( self.name + ": EOF exception found" )
1639 main.log.error( self.name + ": " + self.handle.before )
1640 main.cleanup()
1641 main.exit()
1642 except:
1643 main.log.exception( "Unknown error:")
1644 main.cleanup()
1645 main.exit()
1646