blob: bc71a7f111de31a92042f1446727d2bbf6f8b6f9 [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()
71 except:
kelvin-onlabd3b64892015-01-20 13:26:24 -080072 main.log.info( self.name + ":" * 30 )
Jon Hall05b2b432014-10-08 19:53:25 -040073 main.log.error( traceback.print_exc() )
kelvin-onlabd3b64892015-01-20 13:26:24 -080074 main.log.info( ":" * 30 )
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 Hall05b2b432014-10-08 19:53:25 -040082 response = ''
83 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 Hall05b2b432014-10-08 19:53:25 -040091 except:
kelvin8ec71442015-01-15 16:57:00 -080092 main.log.error( 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 )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400116 except:
kelvin8ec71442015-01-15 16:57:00 -0800117 main.log.error( "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 )
andrewonlab8790abb2014-11-06 13:51:54 -0500146 except:
kelvin8ec71442015-01-15 16:57:00 -0800147 main.log.error( "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' +
173 '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 " +
209 "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()
218 except:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800219 main.log.info( self.name + ":" * 60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400220 main.log.error( traceback.print_exc() )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800221 main.log.info( ":" * 60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400222 main.cleanup()
223 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400224
kelvin-onlabd3b64892015-01-20 13:26:24 -0800225 def gitPull( self, comp1="" ):
kelvin8ec71442015-01-15 16:57:00 -0800226 """
Jon Hallacabffd2014-10-09 12:36:53 -0400227 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800228
Jon Hallacabffd2014-10-09 12:36:53 -0400229 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800230 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400231 for the purpose of pulling from other nodes if necessary.
232
Jon Hall47a93fb2015-01-06 16:46:06 -0800233 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400234 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800235 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400236 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400237
kelvin8ec71442015-01-15 16:57:00 -0800238 """
Jon Hallacabffd2014-10-09 12:36:53 -0400239 try:
kelvin8ec71442015-01-15 16:57:00 -0800240 # main.log.info( self.name + ": Stopping ONOS" )
241 # self.stop()
242 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800243 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800244 if comp1 == "":
245 self.handle.sendline( "git pull" )
Jon Hallacabffd2014-10-09 12:36:53 -0400246 else:
kelvin8ec71442015-01-15 16:57:00 -0800247 self.handle.sendline( "git pull " + comp1 )
Jon Hall47a93fb2015-01-06 16:46:06 -0800248
kelvin-onlabd3b64892015-01-20 13:26:24 -0800249 i = self.handle.expect(
250 [
251 'fatal',
252 'Username\sfor\s(.*):\s',
253 '\sfile(s*) changed,\s',
254 'Already up-to-date',
255 'Aborting',
256 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800257 'You asked me to pull without telling me which branch you',
258 'Pull is not possible because you have unmerged files',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800259 pexpect.TIMEOUT ],
260 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800261 # debug
kelvin-onlabd3b64892015-01-20 13:26:24 -0800262 # main.log.report( self.name +": DEBUG: \n"+
263 # "git pull response: " +
264 # str( self.handle.before ) + str( self.handle.after ) )
kelvin8ec71442015-01-15 16:57:00 -0800265 if i == 0:
266 main.log.error( self.name + ": Git pull had some issue..." )
Jon Hallacabffd2014-10-09 12:36:53 -0400267 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800268 elif i == 1:
269 main.log.error(
270 self.name +
271 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400272 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800273 elif i == 2:
274 main.log.info(
275 self.name +
276 ": Git Pull - pulling repository now" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800277 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800278 # So that only when git pull is done, we do mvn clean compile
279 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800280 elif i == 3:
281 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800282 return i
kelvin8ec71442015-01-15 16:57:00 -0800283 elif i == 4:
284 main.log.info(
285 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800286 ": Git Pull - Aborting..." +
287 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400288 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800289 elif i == 5:
290 main.log.info(
291 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800292 ": Git Pull - You are not currently " +
293 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400294 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800295 elif i == 6:
296 main.log.info(
297 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800298 ": Git Pull - You have not configured an upstream " +
299 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400300 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800301 elif i == 7:
302 main.log.info(
303 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800304 ": Git Pull - Pull is not possible because " +
305 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400306 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800307 elif i == 8:
308 main.log.error( self.name + ": Git Pull - TIMEOUT" )
309 main.log.error(
310 self.name + " Response was: " + str(
311 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400312 return main.ERROR
313 else:
kelvin8ec71442015-01-15 16:57:00 -0800314 main.log.error(
315 self.name +
316 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400317 return main.ERROR
318 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800319 main.log.error( self.name + ": EOF exception found" )
320 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400321 main.cleanup()
322 main.exit()
323 except:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800324 main.log.info( self.name + ":" * 60 )
Jon Hallacabffd2014-10-09 12:36:53 -0400325 main.log.error( traceback.print_exc() )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800326 main.log.info( ":" * 80 )
Jon Hallacabffd2014-10-09 12:36:53 -0400327 main.cleanup()
328 main.exit()
329
kelvin-onlabd3b64892015-01-20 13:26:24 -0800330 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800331 """
Jon Hallacabffd2014-10-09 12:36:53 -0400332 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800333
Jon Hallacabffd2014-10-09 12:36:53 -0400334 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800335 If used as gitCheckout( "branch" ) it will do git checkout
336 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400337
338 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800339 branch of the ONOS repository. If it has any problems, it will return
340 main.ERROR.
341 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400342 successful then the function will return main.TRUE.
343
kelvin8ec71442015-01-15 16:57:00 -0800344 """
Jon Hallacabffd2014-10-09 12:36:53 -0400345 try:
kelvin8ec71442015-01-15 16:57:00 -0800346 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800347 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800348 main.log.info( self.name +
349 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800350 cmd = "git checkout " + branch
351 self.handle.sendline( cmd )
352 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800353 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800354 [ 'fatal',
355 'Username\sfor\s(.*):\s',
356 'Already\son\s\'',
357 'Switched\sto\sbranch\s\'' + str( branch ),
358 pexpect.TIMEOUT,
359 'error: Your local changes to the following files' +
360 'would be overwritten by checkout:',
361 'error: you need to resolve your current index first',
362 "You are in 'detached HEAD' state.",
363 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800364 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800365 if i == 0:
366 main.log.error(
367 self.name +
368 ": Git checkout had some issue..." )
369 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400370 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800371 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800372 main.log.error(
373 self.name +
374 ": Git checkout asking for username." +
375 " Please configure your local git repository to be able " +
376 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800377 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400378 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800379 elif i == 2:
380 main.log.info(
381 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800382 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800383 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800384 # main.log.info( "DEBUG: after checkout cmd = "+
385 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400386 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800387 elif i == 3:
388 main.log.info(
389 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800390 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800391 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800392 # main.log.info( "DEBUG: after checkout cmd = "+
393 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400394 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800395 elif i == 4:
396 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
397 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800398 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400399 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800400 elif i == 5:
401 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800402 main.log.error(
403 self.name +
404 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800405 "Your local changes to the following files would" +
406 " be overwritten by checkout:" +
407 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800408 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500409 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800410 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800411 main.log.error(
412 self.name +
413 ": Git checkout error: \n" +
414 "You need to resolve your current index first:" +
415 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800416 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500417 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800418 elif i == 7:
419 main.log.info(
420 self.name +
421 ": Git checkout " + str( branch ) +
422 " - You are in 'detached HEAD' state. HEAD is now at " +
423 str( branch ) )
424 self.handle.expect( self.home + "\$" )
425 return main.TRUE
426 elif i == 8: # Already in detached HEAD on the specified commit
427 main.log.info(
428 self.name +
429 ": Git Checkout %s : Already on commit" % branch )
430 self.handle.expect( self.home + "\$" )
431 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400432 else:
kelvin8ec71442015-01-15 16:57:00 -0800433 main.log.error(
434 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800435 ": Git Checkout - Unexpected response, " +
436 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800437 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400438 return main.ERROR
439
440 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800441 main.log.error( self.name + ": EOF exception found" )
442 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400443 main.cleanup()
444 main.exit()
445 except:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800446 main.log.info( self.name + ":" * 60 )
Jon Hallacabffd2014-10-09 12:36:53 -0400447 main.log.error( traceback.print_exc() )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800448 main.log.info( ":" * 80 )
Jon Hallacabffd2014-10-09 12:36:53 -0400449 main.cleanup()
450 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400451
kelvin-onlabd3b64892015-01-20 13:26:24 -0800452 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800453 """
Jon Hall274b6642015-02-17 11:57:17 -0800454 Writes the COMMIT number to the report to be parsed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800455 by Jenkins data collecter.
kelvin8ec71442015-01-15 16:57:00 -0800456 """
Jon Hall45ec0922014-10-10 19:33:49 -0400457 try:
kelvin8ec71442015-01-15 16:57:00 -0800458 self.handle.sendline( "" )
459 self.handle.expect( "\$" )
460 self.handle.sendline(
461 "cd " +
462 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800463 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
464 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800465 # NOTE: for some reason there are backspaces inserted in this
466 # phrase when run from Jenkins on some tests
467 self.handle.expect( "never" )
468 self.handle.expect( "\$" )
469 response = ( self.name + ": \n" + str(
470 self.handle.before + self.handle.after ) )
471 self.handle.sendline( "cd " + self.home )
472 self.handle.expect( "\$" )
473 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400474 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500475 print line
476 if report:
kelvin8ec71442015-01-15 16:57:00 -0800477 for line in lines[ 2:-1 ]:
478 # Bracket replacement is for Wiki-compliant
479 # formatting. '<' or '>' are interpreted
480 # as xml specific tags that cause errors
481 line = line.replace( "<", "[" )
482 line = line.replace( ">", "]" )
483 main.log.report( "\t" + line )
484 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400485 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800486 main.log.error( self.name + ": EOF exception found" )
487 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400488 main.cleanup()
489 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800490 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800491 main.log.error( self.name + ": TIMEOUT exception found" )
492 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800493 main.cleanup()
494 main.exit()
Jon Hall45ec0922014-10-10 19:33:49 -0400495 except:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800496 main.log.info( self.name + ":" * 60 )
Jon Hall45ec0922014-10-10 19:33:49 -0400497 main.log.error( traceback.print_exc() )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800498 main.log.info( ":" * 80 )
Jon Hall45ec0922014-10-10 19:33:49 -0400499 main.cleanup()
500 main.exit()
501
kelvin-onlabd3b64892015-01-20 13:26:24 -0800502 def createCellFile( self, benchIp, fileName, mnIpAddrs,
503 extraFeatureString, *onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800504 """
andrewonlab94282092014-10-10 13:00:11 -0400505 Creates a cell file based on arguments
506 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800507 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400508 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800509 * File name of the cell file ( fileName )
510 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800511 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400512 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800513 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400514 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800515
andrewonlab94282092014-10-10 13:00:11 -0400516 NOTE: Assumes cells are located at:
517 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800518 """
519 # Variable initialization
kelvin-onlabd3b64892015-01-20 13:26:24 -0800520 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800521 # We want to create the cell file in the dependencies directory
522 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800523 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800524 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800525 cellFile = open( tempDirectory + fileName, 'w+' )
kelvin8ec71442015-01-15 16:57:00 -0800526
527 # Feature string is hardcoded environment variables
528 # That you may wish to use by default on startup.
529 # Note that you may not want certain features listed
530 # on here.
jenkinsbd0e7532015-02-10 14:32:54 -0800531 coreFeatureString = "export ONOS_FEATURES=" + extraFeatureString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800532 mnString = "export OCN="
533 onosString = "export OC"
534 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800535
kelvin-onlabd3b64892015-01-20 13:26:24 -0800536 # Create ONOSNIC ip address prefix
537 tempOnosIp = onosIpAddrs[ 0 ]
538 tempList = []
539 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800540 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800541 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800542 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800543 nicAddr = ".".join( tempList ) + ".*"
544 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400545
546 try:
kelvin8ec71442015-01-15 16:57:00 -0800547 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800548 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400549
kelvin-onlabd3b64892015-01-20 13:26:24 -0800550 for arg in onosIpAddrs:
551 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800552 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400553 # export OC1="10.128.20.11"
554 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800555 cellFile.write( onosString + str( tempCount ) +
556 "=" + "\"" + arg + "\"" + "\n" )
557 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800558
kelvin-onlabd3b64892015-01-20 13:26:24 -0800559 cellFile.write( mnString + "\"" + mnIpAddrs + "\"" + "\n" )
560 cellFile.write( coreFeatureString + "\n" )
561 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400562
kelvin8ec71442015-01-15 16:57:00 -0800563 # We use os.system to send the command to TestON cluster
564 # to account for the case in which TestON is not located
565 # on the same cluster as the ONOS bench
566 # Note that even if TestON is located on the same cluster
567 # as ONOS bench, you must setup passwordless ssh
568 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800569 os.system( "scp " + tempDirectory + fileName +
570 " admin@" + benchIp + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400571
andrewonlab2a6c9342014-10-16 13:40:15 -0400572 return main.TRUE
573
andrewonlab94282092014-10-10 13:00:11 -0400574 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800575 main.log.error( self.name + ": EOF exception found" )
576 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400577 main.cleanup()
578 main.exit()
579 except:
kelvin8ec71442015-01-15 16:57:00 -0800580 main.log.info( self.name + ":::::::::" )
andrewonlab94282092014-10-10 13:00:11 -0400581 main.log.error( traceback.print_exc() )
kelvin8ec71442015-01-15 16:57:00 -0800582 main.log.info( ":::::::" )
andrewonlab94282092014-10-10 13:00:11 -0400583 main.cleanup()
584 main.exit()
585
kelvin-onlabd3b64892015-01-20 13:26:24 -0800586 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800587 """
andrewonlab95ca1462014-10-09 14:04:24 -0400588 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800589 """
andrewonlab95ca1462014-10-09 14:04:24 -0400590 try:
591 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800592 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400593 main.cleanup()
594 main.exit()
595 else:
kelvin8ec71442015-01-15 16:57:00 -0800596 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800597 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800598 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400599 # and that this driver will have to change accordingly
jenkinsbd0e7532015-02-10 14:32:54 -0800600 self.handle.expect( "ONOS_CELL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800601 handleBefore = self.handle.before
602 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800603 # Get the rest of the handle
604 self.handle.sendline( "" )
605 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800606 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400607
kelvin-onlabd3b64892015-01-20 13:26:24 -0800608 main.log.info( "Cell call returned: " + handleBefore +
609 handleAfter + handleMore )
andrewonlab95ca1462014-10-09 14:04:24 -0400610
611 return main.TRUE
612
613 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800614 main.log.error( self.name + ": EOF exception found" )
615 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400616 main.cleanup()
617 main.exit()
618 except:
kelvin8ec71442015-01-15 16:57:00 -0800619 main.log.info( self.name + " ::::::" )
620 main.log.error( traceback.print_exc() )
621 main.log.info( self.name + " ::::::" )
andrewonlab95ca1462014-10-09 14:04:24 -0400622 main.cleanup()
623 main.exit()
624
kelvin-onlabd3b64892015-01-20 13:26:24 -0800625 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800626 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400627 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800628 """
629 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400630
andrewonlabc03bf6c2014-10-09 14:56:18 -0400631 try:
kelvin8ec71442015-01-15 16:57:00 -0800632 # Clean handle by sending empty and expecting $
633 self.handle.sendline( "" )
634 self.handle.expect( "\$" )
635 self.handle.sendline( "onos-verify-cell" )
636 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800637 handleBefore = self.handle.before
638 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800639 # Get the rest of the handle
640 self.handle.sendline( "" )
641 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800642 handleMore = self.handle.before
andrewonlabc03bf6c2014-10-09 14:56:18 -0400643
kelvin-onlabd3b64892015-01-20 13:26:24 -0800644 main.log.info( "Verify cell returned: " + handleBefore +
645 handleAfter + handleMore )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400646
647 return main.TRUE
Jon Hall7993bfc2014-10-09 16:30:14 -0400648 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800649 main.log.error( self.name + ": EOF exception found" )
650 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
1495 def setIpTables( self, ip, port='', action='add', packet_type='tcp',
1496 direction='INPUT', rule='DROP' ):
1497 '''
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')
1510 Returns:
1511 main.TRUE on success or
1512 main.FALSE if given invalid input or
1513 main.ERROR if there is an error in response from iptables
1514 WARNING:
1515 * This function uses root privilege iptables command which may result
1516 in unwanted network errors. USE WITH CAUTION
1517 '''
1518 import time
1519
1520 # NOTE*********
1521 # The strict checking methods of this driver function is intentional
1522 # to discourage any misuse or error of iptables, which can cause
1523 # severe network errors
1524 # *************
1525
1526 # NOTE: Sleep needed to give some time for rule to be added and
1527 # registered to the instance. If you are calling this function
1528 # multiple times this sleep will prevent any errors.
1529 # DO NOT REMOVE
1530 time.sleep( 5 )
1531 try:
1532 # input validation
1533 action_type = action.lower()
1534 rule = rule.upper()
1535 direction = direction.upper()
1536 if action_type != 'add' and action_type != 'remove':
1537 main.log.error( "Invalid action type. Use 'add' or "
1538 "'remove' table rule" )
1539 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1540 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1541 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1542 "'ACCEPT' or 'LOG' only." )
1543 if direction != 'INPUT' and direction != 'OUTPUT':
1544 # NOTE currently only supports rules INPUT and OUPTUT
1545 main.log.error( "Invalid rule. Valid directions are"
1546 " 'OUTPUT' or 'INPUT'" )
1547 return main.FALSE
1548 return main.FALSE
1549 return main.FALSE
1550 if action_type == 'add':
1551 # -A is the 'append' action of iptables
1552 actionFlag = '-A'
1553 elif action_type == 'remove':
1554 # -D is the 'delete' rule of iptables
1555 actionFlag = '-D'
1556 self.handle.sendline( "" )
1557 self.handle.expect( "\$" )
1558 cmd = "sudo iptables " + actionFlag + " " +\
1559 direction +\
1560 " -p " + str( packet_type ) +\
1561 " -s " + str( ip )
1562 if port:
1563 cmd += " --dport " + str( port )
1564 cmd += " -j " + str( rule )
1565
1566 self.handle.sendline( cmd )
1567 self.handle.expect( "\$" )
1568 main.log.warn( self.handle.before )
1569
1570 info_string = "On " + str( self.name )
1571 info_string += " " + str( action_type )
1572 info_string += " iptable rule [ "
1573 info_string += " IP: " + str( ip )
1574 info_string += " Port: " + str( port )
1575 info_string += " Rule: " + str( rule )
1576 info_string += " Direction: " + str( direction ) + " ]"
1577 main.log.info( info_string )
1578 return main.TRUE
1579 except pexpect.TIMEOUT:
1580 main.log.exception( self.name + ": Timeout exception in "
1581 "setIpTables function" )
1582 return main.ERROR
1583 except pexpect.EOF:
1584 main.log.error( self.name + ": EOF exception found" )
1585 main.log.error( self.name + ": " + self.handle.before )
1586 main.cleanup()
1587 main.exit()
1588 except:
1589 main.log.exception( "Unknown error:")
1590 main.cleanup()
1591 main.exit()
1592