blob: 1c7d32938d226aab5f2c3f9206350d9635039745 [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
andrewonlabe8e56fd2014-10-09 17:12:44 -04002
kelvin8ec71442015-01-15 16:57:00 -08003"""
4This driver interacts with ONOS bench, the OSGi platform
5that configures the ONOS nodes. ( aka ONOS-next )
andrewonlabe8e56fd2014-10-09 17:12:44 -04006
kelvin8ec71442015-01-15 16:57:00 -08007Please follow the coding style demonstrated by existing
andrewonlabe8e56fd2014-10-09 17:12:44 -04008functions and document properly.
9
10If you are a contributor to the driver, please
11list your email here for future contact:
12
13jhall@onlab.us
14andrew@onlab.us
15
16OCT 9 2014
17
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab7735d852014-10-09 13:02:47 -040019import sys
Jon Hall05b2b432014-10-08 19:53:25 -040020import time
21import pexpect
andrewonlab7735d852014-10-09 13:02:47 -040022import os.path
kelvin8ec71442015-01-15 16:57:00 -080023sys.path.append( "../" )
Jon Hall05b2b432014-10-08 19:53:25 -040024from drivers.common.clidriver import CLI
25
Jon Hall05b2b432014-10-08 19:53:25 -040026
kelvin8ec71442015-01-15 16:57:00 -080027class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040028
kelvin8ec71442015-01-15 16:57:00 -080029 def __init__( self ):
30 """
31 Initialize client
32 """
Jon Hallefbd9792015-03-05 16:11:36 -080033 self.name = None
34 self.home = None
35 self.handle = None
kelvin8ec71442015-01-15 16:57:00 -080036 super( CLI, self ).__init__()
37
38 def connect( self, **connectargs ):
39 """
Jon Hall05b2b432014-10-08 19:53:25 -040040 Creates ssh handle for ONOS "bench".
kelvin8ec71442015-01-15 16:57:00 -080041 """
Jon Hall05b2b432014-10-08 19:53:25 -040042 try:
43 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080044 vars( self )[ key ] = connectargs[ key ]
Jon Hall05b2b432014-10-08 19:53:25 -040045 self.home = "~/ONOS"
46 for key in self.options:
47 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080048 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040049 break
Jon Hall274b6642015-02-17 11:57:17 -080050 if self.home is None or self.home == "":
kelvin-onlaba1484582015-02-02 15:46:20 -080051 self.home = "~/ONOS"
Jon Hall21270ac2015-02-16 17:59:55 -080052
kelvin8ec71442015-01-15 16:57:00 -080053 self.name = self.options[ 'name' ]
54 self.handle = super( OnosDriver, self ).connect(
55 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -080056 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -080057 port=self.port,
58 pwd=self.pwd,
59 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -040060
kelvin8ec71442015-01-15 16:57:00 -080061 self.handle.sendline( "cd " + self.home )
62 self.handle.expect( "\$" )
Jon Hall05b2b432014-10-08 19:53:25 -040063 if self.handle:
64 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080065 else:
66 main.log.info( "NO ONOS HANDLE" )
Jon Hall05b2b432014-10-08 19:53:25 -040067 return main.FALSE
68 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080069 main.log.error( self.name + ": EOF exception found" )
70 main.log.error( self.name + ": " + self.handle.before )
Jon Hall05b2b432014-10-08 19:53:25 -040071 main.cleanup()
72 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080073 except Exception:
74 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall05b2b432014-10-08 19:53:25 -040075 main.cleanup()
76 main.exit()
77
kelvin8ec71442015-01-15 16:57:00 -080078 def disconnect( self ):
79 """
Jon Hall05b2b432014-10-08 19:53:25 -040080 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080081 """
Jon Halld61331b2015-02-17 16:35:47 -080082 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -040083 try:
Jon Hall61282e32015-03-19 11:34:11 -070084 if self.handle:
85 self.handle.sendline( "" )
86 self.handle.expect( "\$" )
87 self.handle.sendline( "exit" )
88 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -040089 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080090 main.log.error( self.name + ": EOF exception found" )
91 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -070092 except ValueError:
93 main.log.exception( "Exception in discconect of " + self.name )
94 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -080095 except Exception:
96 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -040097 response = main.FALSE
98 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040099
kelvin-onlabd3b64892015-01-20 13:26:24 -0800100 def onosPackage( self ):
kelvin8ec71442015-01-15 16:57:00 -0800101 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400102 Produce a self-contained tar.gz file that can be deployed
kelvin8ec71442015-01-15 16:57:00 -0800103 and executed on any platform with Java 7 JRE.
104 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400105 try:
kelvin8ec71442015-01-15 16:57:00 -0800106 self.handle.sendline( "onos-package" )
107 self.handle.expect( "onos-package" )
108 self.handle.expect( "tar.gz", timeout=30 )
109 handle = str( self.handle.before )
110 main.log.info( "onos-package command returned: " +
111 handle )
112 # As long as the sendline does not time out,
113 # return true. However, be careful to interpret
114 # the results of the onos-package command return
andrewonlab0748d2a2014-10-09 13:24:17 -0400115 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400116
andrewonlab7735d852014-10-09 13:02:47 -0400117 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800118 main.log.error( self.name + ": EOF exception found" )
119 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800120 except Exception:
121 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400122 main.cleanup()
123 main.exit()
124
kelvin-onlabd3b64892015-01-20 13:26:24 -0800125 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800126 """
andrewonlab8790abb2014-11-06 13:51:54 -0500127 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800128 """
andrewonlab8790abb2014-11-06 13:51:54 -0500129 try:
kelvin8ec71442015-01-15 16:57:00 -0800130 self.handle.sendline( "onos-build" )
131 self.handle.expect( "onos-build" )
132 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800133 "BUILD SUCCESS",
134 "ERROR",
135 "BUILD FAILED" ],
136 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800137 handle = str( self.handle.before )
andrewonlab8790abb2014-11-06 13:51:54 -0500138
kelvin8ec71442015-01-15 16:57:00 -0800139 main.log.info( "onos-build command returned: " +
140 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500141
142 if i == 0:
143 return main.TRUE
144 else:
145 return handle
146
147 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800148 main.log.error( self.name + ": EOF exception found" )
149 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800150 except Exception:
151 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500152 main.cleanup()
153 main.exit()
154
kelvin-onlabd3b64892015-01-20 13:26:24 -0800155 def cleanInstall( self ):
kelvin8ec71442015-01-15 16:57:00 -0800156 """
157 Runs mvn clean install in the root of the ONOS directory.
158 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400159
kelvin8ec71442015-01-15 16:57:00 -0800160 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400161 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800162 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400163 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800164 main.log.info( "Running 'mvn clean install' on " +
165 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800166 ". This may take some time." )
167 self.handle.sendline( "cd " + self.home )
168 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400169
kelvin8ec71442015-01-15 16:57:00 -0800170 self.handle.sendline( "" )
171 self.handle.expect( "\$" )
172 self.handle.sendline( "mvn clean install" )
173 self.handle.expect( "mvn clean install" )
174 while True:
175 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800176 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800177 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400178 'BUILD\sFAILURE',
179 'BUILD\sSUCCESS',
180 'ONOS\$',
kelvin8ec71442015-01-15 16:57:00 -0800181 pexpect.TIMEOUT ], timeout=600 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400182 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800183 main.log.error( self.name + ":There is insufficient memory \
184 for the Java Runtime Environment to continue." )
185 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400186 main.cleanup()
187 main.exit()
188 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800189 main.log.error( self.name + ": Build failure!" )
190 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400191 main.cleanup()
192 main.exit()
193 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800194 main.log.info( self.name + ": Build success!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400195 elif i == 3:
kelvin8ec71442015-01-15 16:57:00 -0800196 main.log.info( self.name + ": Build complete" )
197 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400198 for line in self.handle.before.splitlines():
199 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800200 main.log.info( line )
201 self.handle.sendline( "" )
202 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400203 return main.TRUE
204 elif i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800205 main.log.error(
206 self.name +
207 ": mvn clean install TIMEOUT!" )
208 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400209 main.cleanup()
210 main.exit()
211 else:
Jon Hall274b6642015-02-17 11:57:17 -0800212 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800213 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800214 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400215 main.cleanup()
216 main.exit()
217 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800218 main.log.error( self.name + ": EOF exception found" )
219 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400220 main.cleanup()
221 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800222 except Exception:
223 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400224 main.cleanup()
225 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400226
Jon Hall61282e32015-03-19 11:34:11 -0700227 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800228 """
Jon Hallacabffd2014-10-09 12:36:53 -0400229 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800230
Jon Hall61282e32015-03-19 11:34:11 -0700231 If the fastForward boolean is set to true, only git pulls that can
232 be fast forwarded will be performed. IE if you have not local commits
233 in your branch.
234
Jon Hallacabffd2014-10-09 12:36:53 -0400235 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800236 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400237 for the purpose of pulling from other nodes if necessary.
238
Jon Hall47a93fb2015-01-06 16:46:06 -0800239 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400240 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800241 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400242 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400243
kelvin8ec71442015-01-15 16:57:00 -0800244 """
Jon Hallacabffd2014-10-09 12:36:53 -0400245 try:
kelvin8ec71442015-01-15 16:57:00 -0800246 # main.log.info( self.name + ": Stopping ONOS" )
247 # self.stop()
248 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800249 self.handle.expect( self.home + "\$" )
Jon Hall61282e32015-03-19 11:34:11 -0700250 cmd = "git pull"
251 if comp1 != "":
252 cmd += ' ' + comp1
253 if fastForward:
254 cmd += ' ' + " --ff-only"
255 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800256 i = self.handle.expect(
257 [
258 'fatal',
259 'Username\sfor\s(.*):\s',
260 '\sfile(s*) changed,\s',
261 'Already up-to-date',
262 'Aborting',
263 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800264 'You asked me to pull without telling me which branch you',
265 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700266 'Please enter a commit message to explain why this merge',
267 'Found a swap file by the name',
268 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800269 pexpect.TIMEOUT ],
270 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800271 # debug
kelvin-onlabd3b64892015-01-20 13:26:24 -0800272 # main.log.report( self.name +": DEBUG: \n"+
273 # "git pull response: " +
274 # str( self.handle.before ) + str( self.handle.after ) )
kelvin8ec71442015-01-15 16:57:00 -0800275 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700276 main.log.error( self.name + ": Git pull had some issue" )
277 output = self.handle.after
278 self.handle.expect( '\$' )
279 output += self.handle.before
280 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400281 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800282 elif i == 1:
283 main.log.error(
284 self.name +
285 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400286 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800287 elif i == 2:
288 main.log.info(
289 self.name +
290 ": Git Pull - pulling repository now" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800291 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800292 # So that only when git pull is done, we do mvn clean compile
293 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800294 elif i == 3:
295 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800296 return i
kelvin8ec71442015-01-15 16:57:00 -0800297 elif i == 4:
298 main.log.info(
299 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800300 ": Git Pull - Aborting..." +
301 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400302 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800303 elif i == 5:
304 main.log.info(
305 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800306 ": Git Pull - You are not currently " +
307 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400308 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800309 elif i == 6:
310 main.log.info(
311 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800312 ": Git Pull - You have not configured an upstream " +
313 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400314 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800315 elif i == 7:
316 main.log.info(
317 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800318 ": Git Pull - Pull is not possible because " +
319 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400320 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800321 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700322 # NOTE: abandoning test since we can't reliably handle this
323 # there could be different default text editors and we
324 # also don't know if we actually want to make the commit
325 main.log.error( "Git pull resulted in a merge commit message" +
326 ". Exiting test!" )
327 main.cleanup()
328 main.exit()
329 elif i == 9: # Merge commit message but swap file exists
330 main.log.error( "Git pull resulted in a merge commit message" +
331 " but a swap file exists." )
332 try:
333 self.handle.send( 'A' ) # Abort
334 self.handle.expect( "\$" )
335 return main.ERROR
336 except Exception:
337 main.log.exception( "Couldn't exit editor prompt!")
338 main.cleanup()
339 main.exit()
340 elif i == 10: # In the middle of a merge commit
341 main.log.error( "Git branch is in the middle of a merge. " )
342 main.log.warn( self.handle.before + self.handle.after )
343 return main.ERROR
344 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800345 main.log.error( self.name + ": Git Pull - TIMEOUT" )
346 main.log.error(
347 self.name + " Response was: " + str(
348 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400349 return main.ERROR
350 else:
kelvin8ec71442015-01-15 16:57:00 -0800351 main.log.error(
352 self.name +
353 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400354 return main.ERROR
355 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800356 main.log.error( self.name + ": EOF exception found" )
357 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400358 main.cleanup()
359 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800360 except Exception:
361 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400362 main.cleanup()
363 main.exit()
364
kelvin-onlabd3b64892015-01-20 13:26:24 -0800365 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800366 """
Jon Hallacabffd2014-10-09 12:36:53 -0400367 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800368
Jon Hallacabffd2014-10-09 12:36:53 -0400369 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800370 If used as gitCheckout( "branch" ) it will do git checkout
371 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400372
373 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800374 branch of the ONOS repository. If it has any problems, it will return
375 main.ERROR.
376 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400377 successful then the function will return main.TRUE.
378
kelvin8ec71442015-01-15 16:57:00 -0800379 """
Jon Hallacabffd2014-10-09 12:36:53 -0400380 try:
kelvin8ec71442015-01-15 16:57:00 -0800381 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800382 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800383 main.log.info( self.name +
384 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800385 cmd = "git checkout " + branch
386 self.handle.sendline( cmd )
387 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800388 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800389 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700390 'Username for (.*): ',
391 'Already on \'',
392 'Switched to branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800393 pexpect.TIMEOUT,
394 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800395 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800396 'error: you need to resolve your current index first',
397 "You are in 'detached HEAD' state.",
398 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800399 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800400 if i == 0:
401 main.log.error(
402 self.name +
403 ": Git checkout had some issue..." )
404 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400405 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800406 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800407 main.log.error(
408 self.name +
409 ": Git checkout asking for username." +
410 " Please configure your local git repository to be able " +
411 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800412 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400413 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800414 elif i == 2:
415 main.log.info(
416 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800417 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800418 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800419 # main.log.info( "DEBUG: after checkout cmd = "+
420 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400421 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800422 elif i == 3:
423 main.log.info(
424 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800425 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800426 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800427 # main.log.info( "DEBUG: after checkout cmd = "+
428 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400429 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800430 elif i == 4:
431 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
432 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800433 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400434 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800435 elif i == 5:
436 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800437 main.log.error(
438 self.name +
439 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800440 "Your local changes to the following files would" +
441 " be overwritten by checkout:" +
442 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800443 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500444 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800445 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800446 main.log.error(
447 self.name +
448 ": Git checkout error: \n" +
449 "You need to resolve your current index first:" +
450 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800451 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500452 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800453 elif i == 7:
454 main.log.info(
455 self.name +
456 ": Git checkout " + str( branch ) +
457 " - You are in 'detached HEAD' state. HEAD is now at " +
458 str( branch ) )
459 self.handle.expect( self.home + "\$" )
460 return main.TRUE
461 elif i == 8: # Already in detached HEAD on the specified commit
462 main.log.info(
463 self.name +
464 ": Git Checkout %s : Already on commit" % branch )
465 self.handle.expect( self.home + "\$" )
466 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400467 else:
kelvin8ec71442015-01-15 16:57:00 -0800468 main.log.error(
469 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800470 ": Git Checkout - Unexpected response, " +
471 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800472 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400473 return main.ERROR
474
475 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800476 main.log.error( self.name + ": EOF exception found" )
477 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400478 main.cleanup()
479 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800480 except Exception:
481 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400482 main.cleanup()
483 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400484
kelvin-onlabd3b64892015-01-20 13:26:24 -0800485 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800486 """
Jon Hall274b6642015-02-17 11:57:17 -0800487 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800488 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800489 """
Jon Hall45ec0922014-10-10 19:33:49 -0400490 try:
kelvin8ec71442015-01-15 16:57:00 -0800491 self.handle.sendline( "" )
492 self.handle.expect( "\$" )
493 self.handle.sendline(
494 "cd " +
495 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800496 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
497 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800498 # NOTE: for some reason there are backspaces inserted in this
499 # phrase when run from Jenkins on some tests
500 self.handle.expect( "never" )
501 self.handle.expect( "\$" )
502 response = ( self.name + ": \n" + str(
503 self.handle.before + self.handle.after ) )
504 self.handle.sendline( "cd " + self.home )
505 self.handle.expect( "\$" )
506 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400507 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500508 print line
509 if report:
kelvin8ec71442015-01-15 16:57:00 -0800510 for line in lines[ 2:-1 ]:
511 # Bracket replacement is for Wiki-compliant
512 # formatting. '<' or '>' are interpreted
513 # as xml specific tags that cause errors
514 line = line.replace( "<", "[" )
515 line = line.replace( ">", "]" )
516 main.log.report( "\t" + line )
517 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400518 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800519 main.log.error( self.name + ": EOF exception found" )
520 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400521 main.cleanup()
522 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800523 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800524 main.log.error( self.name + ": TIMEOUT exception found" )
525 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800526 main.cleanup()
527 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800528 except Exception:
529 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400530 main.cleanup()
531 main.exit()
532
kelvin-onlabd3b64892015-01-20 13:26:24 -0800533 def createCellFile( self, benchIp, fileName, mnIpAddrs,
Jon Hallefbd9792015-03-05 16:11:36 -0800534 extraFeatureString, *onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800535 """
andrewonlab94282092014-10-10 13:00:11 -0400536 Creates a cell file based on arguments
537 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800538 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400539 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800540 * File name of the cell file ( fileName )
541 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800542 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400543 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800544 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400545 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800546
andrewonlab94282092014-10-10 13:00:11 -0400547 NOTE: Assumes cells are located at:
548 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800549 """
550 # Variable initialization
kelvin-onlabd3b64892015-01-20 13:26:24 -0800551 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800552 # We want to create the cell file in the dependencies directory
553 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800554 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800555 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800556 cellFile = open( tempDirectory + fileName, 'w+' )
kelvin8ec71442015-01-15 16:57:00 -0800557
558 # Feature string is hardcoded environment variables
559 # That you may wish to use by default on startup.
560 # Note that you may not want certain features listed
561 # on here.
jenkinsbd0e7532015-02-10 14:32:54 -0800562 coreFeatureString = "export ONOS_FEATURES=" + extraFeatureString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800563 mnString = "export OCN="
564 onosString = "export OC"
565 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800566
kelvin-onlabd3b64892015-01-20 13:26:24 -0800567 # Create ONOSNIC ip address prefix
568 tempOnosIp = onosIpAddrs[ 0 ]
569 tempList = []
570 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800571 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800572 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800573 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800574 nicAddr = ".".join( tempList ) + ".*"
575 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400576
577 try:
kelvin8ec71442015-01-15 16:57:00 -0800578 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800579 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400580
kelvin-onlabd3b64892015-01-20 13:26:24 -0800581 for arg in onosIpAddrs:
582 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800583 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400584 # export OC1="10.128.20.11"
585 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800586 cellFile.write( onosString + str( tempCount ) +
587 "=" + "\"" + arg + "\"" + "\n" )
588 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800589
kelvin-onlabd3b64892015-01-20 13:26:24 -0800590 cellFile.write( mnString + "\"" + mnIpAddrs + "\"" + "\n" )
591 cellFile.write( coreFeatureString + "\n" )
592 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400593
kelvin8ec71442015-01-15 16:57:00 -0800594 # We use os.system to send the command to TestON cluster
595 # to account for the case in which TestON is not located
596 # on the same cluster as the ONOS bench
597 # Note that even if TestON is located on the same cluster
598 # as ONOS bench, you must setup passwordless ssh
599 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800600 os.system( "scp " + tempDirectory + fileName +
601 " admin@" + benchIp + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400602
andrewonlab2a6c9342014-10-16 13:40:15 -0400603 return main.TRUE
604
andrewonlab94282092014-10-10 13:00:11 -0400605 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800606 main.log.error( self.name + ": EOF exception found" )
607 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400608 main.cleanup()
609 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800610 except Exception:
611 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400612 main.cleanup()
613 main.exit()
614
kelvin-onlabd3b64892015-01-20 13:26:24 -0800615 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800616 """
andrewonlab95ca1462014-10-09 14:04:24 -0400617 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800618 """
andrewonlab95ca1462014-10-09 14:04:24 -0400619 try:
620 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800621 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400622 main.cleanup()
623 main.exit()
624 else:
kelvin8ec71442015-01-15 16:57:00 -0800625 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800626 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800627 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400628 # and that this driver will have to change accordingly
jenkinsbd0e7532015-02-10 14:32:54 -0800629 self.handle.expect( "ONOS_CELL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800630 handleBefore = self.handle.before
631 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800632 # Get the rest of the handle
633 self.handle.sendline( "" )
634 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800635 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400636
kelvin-onlabd3b64892015-01-20 13:26:24 -0800637 main.log.info( "Cell call returned: " + handleBefore +
638 handleAfter + handleMore )
andrewonlab95ca1462014-10-09 14:04:24 -0400639
640 return main.TRUE
641
642 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800643 main.log.error( self.name + ": EOF exception found" )
644 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400645 main.cleanup()
646 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800647 except Exception:
648 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400649 main.cleanup()
650 main.exit()
651
kelvin-onlabd3b64892015-01-20 13:26:24 -0800652 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800653 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400654 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800655 """
656 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400657
andrewonlabc03bf6c2014-10-09 14:56:18 -0400658 try:
kelvin8ec71442015-01-15 16:57:00 -0800659 # Clean handle by sending empty and expecting $
660 self.handle.sendline( "" )
661 self.handle.expect( "\$" )
662 self.handle.sendline( "onos-verify-cell" )
663 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800664 handleBefore = self.handle.before
665 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800666 # Get the rest of the handle
667 self.handle.sendline( "" )
668 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800669 handleMore = self.handle.before
andrewonlabc03bf6c2014-10-09 14:56:18 -0400670
kelvin-onlabd3b64892015-01-20 13:26:24 -0800671 main.log.info( "Verify cell returned: " + handleBefore +
672 handleAfter + handleMore )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400673
674 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800675 except pexpect.ExceptionPexpect as e:
676 main.log.error( self.name + ": Pexpect exception found of type " +
677 str( type( e ) ) )
678 main.log.error ( e.get_trace() )
kelvin8ec71442015-01-15 16:57:00 -0800679 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400680 main.cleanup()
681 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800682 except Exception:
683 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400684 main.cleanup()
685 main.exit()
686
kelvin-onlabd3b64892015-01-20 13:26:24 -0800687 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800688 """
andrewonlab05e362f2014-10-10 00:40:57 -0400689 Uses 'onos' command to send various ONOS CLI arguments.
690 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800691 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400692 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800693
694 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400695 CLI commands for ONOS. Try to use this function first
696 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800697 function.
698 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400699 by starting onos, and typing in 'onos' to enter the
700 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800701 available commands.
702 """
andrewonlab05e362f2014-10-10 00:40:57 -0400703 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800704 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800705 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400706 return main.FALSE
707 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800708 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400709 return main.FALSE
710
kelvin8ec71442015-01-15 16:57:00 -0800711 cmdstr = str( cmdstr )
712 self.handle.sendline( "" )
713 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400714
kelvin-onlabd3b64892015-01-20 13:26:24 -0800715 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800716 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400717
kelvin-onlabd3b64892015-01-20 13:26:24 -0800718 handleBefore = self.handle.before
Shreya Shaha73aaad2014-10-27 18:03:09 -0400719 print "handle_before = ", self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800720 # handleAfter = str( self.handle.after )
Jon Hall47a93fb2015-01-06 16:46:06 -0800721
kelvin8ec71442015-01-15 16:57:00 -0800722 # self.handle.sendline( "" )
723 # self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800724 # handleMore = str( self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400725
kelvin8ec71442015-01-15 16:57:00 -0800726 main.log.info( "Command sent successfully" )
andrewonlab05e362f2014-10-10 00:40:57 -0400727
kelvin8ec71442015-01-15 16:57:00 -0800728 # Obtain return handle that consists of result from
729 # the onos command. The string may need to be
730 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800731 # returnString = handleBefore + handleAfter
732 returnString = handleBefore
733 print "return_string = ", returnString
734 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400735
736 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800737 main.log.error( self.name + ": EOF exception found" )
738 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400739 main.cleanup()
740 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800741 except Exception:
742 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400743 main.cleanup()
744 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400745
kelvin-onlabd3b64892015-01-20 13:26:24 -0800746 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800747 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400748 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800749 If -f option is provided, it also forces an uninstall.
750 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400751 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800752 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400753 files to certain onos nodes
754
755 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800756 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400757 try:
andrewonlab114768a2014-11-14 12:44:44 -0500758 if options:
kelvin8ec71442015-01-15 16:57:00 -0800759 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500760 else:
kelvin8ec71442015-01-15 16:57:00 -0800761 self.handle.sendline( "onos-install " + node )
762 self.handle.expect( "onos-install " )
763 # NOTE: this timeout may need to change depending on the network
764 # and size of ONOS
765 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800766 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800767 "ONOS\sis\salready\sinstalled",
768 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400769
Jon Hall7993bfc2014-10-09 16:30:14 -0400770 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800771 main.log.warn( "Network is unreachable" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400772 return main.FALSE
773 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800774 main.log.info(
775 "ONOS was installed on " +
776 node +
777 " and started" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400778 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500779 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800780 main.log.info( "ONOS is already installed on " + node )
andrewonlabd9a73a72014-11-14 17:28:21 -0500781 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800782 elif i == 3:
783 main.log.info(
784 "Installation of ONOS on " +
785 node +
786 " timed out" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400787 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400788
789 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800790 main.log.error( self.name + ": EOF exception found" )
791 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400792 main.cleanup()
793 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800794 except Exception:
795 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400796 main.cleanup()
797 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400798
kelvin-onlabd3b64892015-01-20 13:26:24 -0800799 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800800 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400801 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400802 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800803 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400804 try:
kelvin8ec71442015-01-15 16:57:00 -0800805 self.handle.sendline( "" )
806 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800807 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800808 " start" )
809 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -0400810 "Job\sis\salready\srunning",
811 "start/running",
812 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800813 pexpect.TIMEOUT ], timeout=120 )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400814
815 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800816 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400817 return main.TRUE
818 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800819 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400820 return main.TRUE
821 else:
kelvin8ec71442015-01-15 16:57:00 -0800822 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400823 main.cleanup()
824 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400825 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800826 main.log.error( self.name + ": EOF exception found" )
827 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400828 main.cleanup()
829 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800830 except Exception:
831 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400832 main.cleanup()
833 main.exit()
834
kelvin-onlabd3b64892015-01-20 13:26:24 -0800835 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800836 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400837 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400838 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800839 """
andrewonlab2b30bd32014-10-09 16:48:55 -0400840 try:
kelvin8ec71442015-01-15 16:57:00 -0800841 self.handle.sendline( "" )
842 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800843 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800844 " stop" )
845 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -0400846 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -0700847 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -0400848 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800849 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2b30bd32014-10-09 16:48:55 -0400850
851 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800852 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400853 return main.TRUE
854 elif i == 1:
Jon Hall65844a32015-03-09 19:09:37 -0700855 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800856 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -0400857 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -0700858 elif i == 2:
859 main.log.warn( "ONOS wasn't running" )
860 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -0400861 else:
kelvin8ec71442015-01-15 16:57:00 -0800862 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400863 return main.FALSE
864
865 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800866 main.log.error( self.name + ": EOF exception found" )
867 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -0400868 main.cleanup()
869 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800870 except Exception:
871 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -0400872 main.cleanup()
873 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800874
kelvin-onlabd3b64892015-01-20 13:26:24 -0800875 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -0800876 """
andrewonlabc8d47972014-10-09 16:52:36 -0400877 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -0800878 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -0400879 if needed
kelvin8ec71442015-01-15 16:57:00 -0800880 """
andrewonlabc8d47972014-10-09 16:52:36 -0400881 try:
kelvin8ec71442015-01-15 16:57:00 -0800882 self.handle.sendline( "" )
883 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800884 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800885 self.handle.expect( "\$" )
andrewonlabc8d47972014-10-09 16:52:36 -0400886
kelvin-onlabd3b64892015-01-20 13:26:24 -0800887 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
andrewonlab9dfd2082014-11-13 17:44:03 -0500888
kelvin8ec71442015-01-15 16:57:00 -0800889 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -0400890 return main.TRUE
891
892 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800893 main.log.error( self.name + ": EOF exception found" )
894 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -0400895 main.cleanup()
896 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800897 except Exception:
898 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -0400899 main.cleanup()
900 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400901
kelvin-onlabd3b64892015-01-20 13:26:24 -0800902 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800903 """
andrewonlabaedc8332014-12-04 12:43:03 -0500904 Issues the command 'onos-die <node-ip>'
905 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -0800906 """
andrewonlabaedc8332014-12-04 12:43:03 -0500907 try:
kelvin8ec71442015-01-15 16:57:00 -0800908 self.handle.sendline( "" )
909 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800910 cmdStr = "onos-kill " + str( nodeIp )
911 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800912 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -0500913 "Killing\sONOS",
914 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -0800915 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -0500916 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800917 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800918 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -0500919 return main.TRUE
920 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800921 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -0500922 return main.FALSE
923 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800924 main.log.error( self.name + ": EOF exception found" )
925 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -0500926 main.cleanup()
927 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800928 except Exception:
929 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -0500930 main.cleanup()
931 main.exit()
932
kelvin-onlabd3b64892015-01-20 13:26:24 -0800933 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800934 """
andrewonlabe8e56fd2014-10-09 17:12:44 -0400935 Calls the command: 'onos-kill [<node-ip>]'
936 "Remotely, and unceremoniously kills the ONOS instance running on
937 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -0800938 """
andrewonlabe8e56fd2014-10-09 17:12:44 -0400939 try:
kelvin8ec71442015-01-15 16:57:00 -0800940 self.handle.sendline( "" )
941 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800942 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800943 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -0400944 "\$",
945 "No\sroute\sto\shost",
946 "password:",
kelvin8ec71442015-01-15 16:57:00 -0800947 pexpect.TIMEOUT ], timeout=20 )
948
andrewonlabe8e56fd2014-10-09 17:12:44 -0400949 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800950 main.log.info(
951 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800952 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400953 return main.TRUE
954 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800955 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400956 return main.FALSE
957 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800958 main.log.info(
959 "Passwordless login for host: " +
960 str( nodeIp ) +
961 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400962 return main.FALSE
963 else:
Jon Hallefbd9792015-03-05 16:11:36 -0800964 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400965 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -0800966
andrewonlabe8e56fd2014-10-09 17:12:44 -0400967 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800968 main.log.error( self.name + ": EOF exception found" )
969 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400970 main.cleanup()
971 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800972 except Exception:
973 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -0400974 main.cleanup()
975 main.exit()
976
kelvin-onlabd3b64892015-01-20 13:26:24 -0800977 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -0800978 """
andrewonlab19fbdca2014-11-14 12:55:59 -0500979 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -0500980 a cleaner environment.
981
andrewonlab19fbdca2014-11-14 12:55:59 -0500982 Description:
Jon Hallfcc88622014-11-25 13:09:54 -0500983 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -0500984 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -0800985 """
andrewonlab19fbdca2014-11-14 12:55:59 -0500986 try:
kelvin8ec71442015-01-15 16:57:00 -0800987 self.handle.sendline( "" )
988 self.handle.expect( "\$" )
989 self.handle.sendline( "onos-remove-raft-logs" )
990 # Sometimes this command hangs
991 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
992 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -0500993 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800994 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
995 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -0500996 if i == 1:
997 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -0800998 self.handle.sendline( "" )
999 self.handle.expect( "\$" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001000 return main.TRUE
1001
1002 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001003 main.log.error( self.name + ": EOF exception found" )
1004 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001005 main.cleanup()
1006 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001007 except Exception:
1008 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001009 main.cleanup()
1010 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001011
kelvin-onlabd3b64892015-01-20 13:26:24 -08001012 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001013 """
1014 Calls the command 'onos-start-network [ <mininet-topo> ]
1015 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001016 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001017 cell."
andrewonlab94282092014-10-10 13:00:11 -04001018 * Specify mininet topology file name for mntopo
1019 * Topo files should be placed at:
1020 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001021
andrewonlab94282092014-10-10 13:00:11 -04001022 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001023 """
andrewonlab94282092014-10-10 13:00:11 -04001024 try:
1025 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001026 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001027 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001028
kelvin8ec71442015-01-15 16:57:00 -08001029 mntopo = str( mntopo )
1030 self.handle.sendline( "" )
1031 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001032
kelvin8ec71442015-01-15 16:57:00 -08001033 self.handle.sendline( "onos-start-network " + mntopo )
1034 self.handle.expect( "mininet>" )
1035 main.log.info( "Network started, entered mininet prompt" )
1036
1037 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001038
1039 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001040 main.log.error( self.name + ": EOF exception found" )
1041 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001042 main.cleanup()
1043 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001044 except Exception:
1045 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001046 main.cleanup()
1047 main.exit()
1048
kelvin8ec71442015-01-15 16:57:00 -08001049 def isup( self, node="" ):
1050 """
1051 Run's onos-wait-for-start which only returns once ONOS is at run
1052 level 100( ready for use )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001053
Jon Hall7993bfc2014-10-09 16:30:14 -04001054 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001055 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001056 try:
kelvin8ec71442015-01-15 16:57:00 -08001057 self.handle.sendline( "onos-wait-for-start " + node )
1058 self.handle.expect( "onos-wait-for-start" )
1059 # NOTE: this timeout is arbitrary"
1060 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ], timeout=120 )
Jon Hall7993bfc2014-10-09 16:30:14 -04001061 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001062 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001063 return main.TRUE
1064 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001065 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001066 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001067 main.log.error( "ONOS has not started yet" )
1068 self.handle.send( "\x03" ) # Control-C
1069 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001070 return main.FALSE
1071 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001072 main.log.error( self.name + ": EOF exception found" )
1073 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001074 main.cleanup()
1075 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001076 except Exception:
1077 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001078 main.cleanup()
1079 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001080
kelvin-onlabd3b64892015-01-20 13:26:24 -08001081 def pushTestIntentsShell(
1082 self,
1083 dpidSrc,
1084 dpidDst,
1085 numIntents,
1086 dirFile,
1087 onosIp,
1088 numMult="",
1089 appId="",
1090 report=True,
1091 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001092 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001093 Description:
kelvin8ec71442015-01-15 16:57:00 -08001094 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001095 better parallelize the results than the CLI
1096 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001097 * dpidSrc: specify source dpid
1098 * dpidDst: specify destination dpid
1099 * numIntents: specify number of intents to push
1100 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001101 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001102 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001103 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001104 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001105 """
1106 try:
1107 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001108 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001109 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001110 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001111 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001112 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001113
kelvin-onlabd3b64892015-01-20 13:26:24 -08001114 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1115 if not numMult:
1116 addIntents = addDpid + " " + str( numIntents )
1117 elif numMult:
1118 addIntents = addDpid + " " + str( numIntents ) + " " +\
1119 str( numMult )
1120 if appId:
1121 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001122 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001123 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001124
andrewonlabaedc8332014-12-04 12:43:03 -05001125 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001126 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001127 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001128 sendCmd = addApp + " &"
1129 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001130
kelvin-onlabd3b64892015-01-20 13:26:24 -08001131 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001132
1133 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001134 main.log.error( self.name + ": EOF exception found" )
1135 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001136 main.cleanup()
1137 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001138 except Exception:
1139 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001140 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001141 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001142
kelvin-onlabd3b64892015-01-20 13:26:24 -08001143 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001144 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001145 parses the onos:topology output
kelvin8ec71442015-01-15 16:57:00 -08001146 Returns: a topology dict populated by the key values found in
Jon Hall77f53ce2014-10-13 18:02:06 -04001147 the cli command.
kelvin8ec71442015-01-15 16:57:00 -08001148 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001149 try:
kelvin8ec71442015-01-15 16:57:00 -08001150 # call the cli to get the topology summary
1151 # cmdstr = "onos:topology"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001152 # cliResult = self.onosCli( ip, cmdstr )
1153 # print "cli_result = ", cliResult
Jon Hall77f53ce2014-10-13 18:02:06 -04001154
kelvin8ec71442015-01-15 16:57:00 -08001155 # Parse the output
Jon Hall77f53ce2014-10-13 18:02:06 -04001156 topology = {}
kelvin-onlabd3b64892015-01-20 13:26:24 -08001157 # for line in cliResult.split( "\n" ):
1158 for line in topologyOutput.splitlines():
kelvin8ec71442015-01-15 16:57:00 -08001159 if not line.startswith( "time=" ):
Jon Hall77f53ce2014-10-13 18:02:06 -04001160 continue
kelvin8ec71442015-01-15 16:57:00 -08001161 # else
1162 # print line
1163 for var in line.split( "," ):
1164 # print "'"+var+"'"
1165 # print "'"+var.strip()+"'"
1166 key, value = var.strip().split( "=" )
1167 topology[ key ] = value
1168 # print "topology = ", topology
1169 # devices = topology.get( 'devices', False )
1170 # print "devices = ", devices
1171 # links = topology.get( 'links', False )
1172 # print "links = ", links
1173 # SCCs = topology.get( 'SCC(s)', False )
1174 # print "SCCs = ", SCCs
1175 # paths = topology.get( 'paths', False )
1176 # print "paths = ", paths
Jon Hall77f53ce2014-10-13 18:02:06 -04001177
1178 return topology
1179 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001180 main.log.error( self.name + ": EOF exception found" )
1181 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001182 main.cleanup()
1183 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001184 except Exception:
1185 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001186 main.cleanup()
1187 main.exit()
1188
kelvin-onlabd3b64892015-01-20 13:26:24 -08001189 def checkStatus(
1190 self,
1191 topologyResult,
1192 numoswitch,
1193 numolink,
1194 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001195 """
Jon Hallefbd9792015-03-05 16:11:36 -08001196 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001197 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001198 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08001199
Jon Hall77f53ce2014-10-13 18:02:06 -04001200 Params: ip = ip used for the onos cli
1201 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001202 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001203 logLevel = level to log to.
1204 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001205
1206
kelvin-onlabd3b64892015-01-20 13:26:24 -08001207 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001208
Jon Hallefbd9792015-03-05 16:11:36 -08001209 Returns: main.TRUE if the number of switches and links are correct,
1210 main.FALSE if the number of switches and links is incorrect,
Jon Hall77f53ce2014-10-13 18:02:06 -04001211 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001212 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001213 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001214 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001215 if topology == {}:
1216 return main.ERROR
1217 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001218 # Is the number of switches is what we expected
1219 devices = topology.get( 'devices', False )
1220 links = topology.get( 'links', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001221 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001222 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001223 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001224 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001225 linkCheck = ( int( links ) == int( numolink ) )
Jon Hallefbd9792015-03-05 16:11:36 -08001226 if switchCheck and linkCheck:
kelvin8ec71442015-01-15 16:57:00 -08001227 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001228 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001229 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001230 result = main.TRUE
1231 else:
1232 output = output + \
Jon Hall274b6642015-02-17 11:57:17 -08001233 "The number of links and switches does not match " + \
1234 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001235 result = main.FALSE
Jon Hallefbd9792015-03-05 16:11:36 -08001236 output = output + "\n ONOS sees %i devices" % int( devices )
1237 output = output + " (%i expected) " % int( numoswitch )
Jon Hall274b6642015-02-17 11:57:17 -08001238 output = output + "and %i links " % int( links )
1239 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001240 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001241 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001242 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001243 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001244 else:
kelvin8ec71442015-01-15 16:57:00 -08001245 main.log.info( output )
1246 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001247 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001248 main.log.error( self.name + ": EOF exception found" )
1249 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001250 main.cleanup()
1251 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001252 except Exception:
1253 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001254 main.cleanup()
1255 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001256
kelvin-onlabd3b64892015-01-20 13:26:24 -08001257 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001258 """
andrewonlab970399c2014-11-07 13:09:32 -05001259 Capture all packet activity and store in specified
1260 directory/file
1261
1262 Required:
1263 * interface: interface to capture
1264 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001265 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001266 try:
1267 self.handle.sendline( "" )
1268 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001269
Jon Hallfebb1c72015-03-05 13:30:09 -08001270 self.handle.sendline( "tshark -i " + str( interface ) +
1271 " -t e -w " + str( dirFile ) + " &" )
1272 self.handle.sendline( "\r" )
1273 self.handle.expect( "Capturing on" )
1274 self.handle.sendline( "\r" )
1275 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001276
Jon Hallfebb1c72015-03-05 13:30:09 -08001277 main.log.info( "Tshark started capturing files on " +
1278 str( interface ) + " and saving to directory: " +
1279 str( dirFile ) )
1280 except pexpect.EOF:
1281 main.log.error( self.name + ": EOF exception found" )
1282 main.log.error( self.name + ": " + self.handle.before )
1283 main.cleanup()
1284 main.exit()
1285 except Exception:
1286 main.log.exception( self.name + ": Uncaught exception!" )
1287 main.cleanup()
1288 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001289
kelvin-onlabd3b64892015-01-20 13:26:24 -08001290 def runOnosTopoCfg( self, instanceName, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001291 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001292 On ONOS bench, run this command:
1293 ./~/ONOS/tools/test/bin/onos-topo-cfg $OC1 filename
1294 which starts the rest and copies
1295 the json file to the onos instance
kelvin8ec71442015-01-15 16:57:00 -08001296 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001297 try:
kelvin8ec71442015-01-15 16:57:00 -08001298 self.handle.sendline( "" )
1299 self.handle.expect( "\$" )
1300 self.handle.sendline( "cd ~/ONOS/tools/test/bin" )
1301 self.handle.expect( "/bin$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001302 cmd = "./onos-topo-cfg " + instanceName + " " + jsonFile
shahshreyae6c7cf42014-11-26 16:39:01 -08001303 print "cmd = ", cmd
kelvin8ec71442015-01-15 16:57:00 -08001304 self.handle.sendline( cmd )
1305 self.handle.expect( "\$" )
1306 self.handle.sendline( "cd ~" )
1307 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -08001308 return main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -08001309 except pexpect.EOF:
1310 main.log.error( self.name + ": EOF exception found" )
1311 main.log.error( self.name + ": " + self.handle.before )
1312 main.cleanup()
1313 main.exit()
1314 except Exception:
1315 main.log.exception( self.name + ": Uncaught exception!" )
1316 main.cleanup()
1317 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001318
kelvin-onlabd3b64892015-01-20 13:26:24 -08001319 def tsharkGrep( self, grep, directory, interface='eth0' ):
kelvin8ec71442015-01-15 16:57:00 -08001320 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001321 Required:
kelvin8ec71442015-01-15 16:57:00 -08001322 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001323 * directory to store results
1324 Optional:
1325 * interface - default: eth0
1326 Description:
1327 Uses tshark command to grep specific group of packets
1328 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001329 The timestamp is hardcoded to be in epoch
1330 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001331 try:
1332 self.handle.sendline( "" )
1333 self.handle.expect( "\$" )
1334 self.handle.sendline( "" )
1335 self.handle.sendline(
1336 "tshark -i " +
1337 str( interface ) +
1338 " -t e | grep --line-buffered \"" +
1339 str(grep) +
1340 "\" >" +
1341 directory +
1342 " &" )
1343 self.handle.sendline( "\r" )
1344 self.handle.expect( "Capturing on" )
1345 self.handle.sendline( "\r" )
1346 self.handle.expect( "\$" )
1347 except pexpect.EOF:
1348 main.log.error( self.name + ": EOF exception found" )
1349 main.log.error( self.name + ": " + self.handle.before )
1350 main.cleanup()
1351 main.exit()
1352 except Exception:
1353 main.log.exception( self.name + ": Uncaught exception!" )
1354 main.cleanup()
1355 main.exit()
1356
kelvin-onlabd3b64892015-01-20 13:26:24 -08001357 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001358 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001359 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001360 """
1361 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001362 try:
1363 self.execute( cmd="sudo rm /tmp/wireshark*" )
1364 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001365 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1366 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001367 self.handle.sendline( "" )
1368 main.log.info( "Tshark stopped" )
1369 except pexpect.EOF:
1370 main.log.error( self.name + ": EOF exception found" )
1371 main.log.error( self.name + ": " + self.handle.before )
1372 main.cleanup()
1373 main.exit()
1374 except Exception:
1375 main.log.exception( self.name + ": Uncaught exception!" )
1376 main.cleanup()
1377 main.exit()
1378
kelvin8ec71442015-01-15 16:57:00 -08001379 def ptpd( self, args ):
1380 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001381 Initiate ptp with user-specified args.
1382 Required:
1383 * args: specify string of args after command
1384 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001385 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001386 try:
kelvin8ec71442015-01-15 16:57:00 -08001387 self.handle.sendline( "sudo ptpd " + str( args ) )
1388 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001389 "Multiple",
1390 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001391 "\$" ] )
1392 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001393
andrewonlab0c38a4a2014-10-28 18:35:35 -04001394 if i == 0:
1395 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001396 main.log.info( "ptpd returned an error: " +
1397 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001398 return handle
1399 elif i == 1:
1400 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001401 main.log.error( "ptpd returned an error: " +
1402 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001403 return handle
1404 else:
1405 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001406
andrewonlab0c38a4a2014-10-28 18:35:35 -04001407 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001408 main.log.error( self.name + ": EOF exception found" )
1409 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001410 main.cleanup()
1411 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001412 except Exception:
1413 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001414 main.cleanup()
1415 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001416
kelvin-onlabd3b64892015-01-20 13:26:24 -08001417 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001418 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001419 """
1420 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001421 Current implementation of ONOS deletes its karaf
1422 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001423 you may want to use this function to capture
1424 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001425 Localtime will be attached to the filename
1426
1427 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001428 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001429 copy.
kelvin8ec71442015-01-15 16:57:00 -08001430 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001431 For copying multiple files, leave copyFileName
1432 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001433 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001434 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001435 ex ) /tmp/
1436 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001437 * copyFileName: If you want to rename the log
1438 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001439 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001440 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001441 try:
kelvin8ec71442015-01-15 16:57:00 -08001442 localtime = time.strftime( '%x %X' )
1443 localtime = localtime.replace( "/", "" )
1444 localtime = localtime.replace( " ", "_" )
1445 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001446 if destDir[ -1: ] != "/":
1447 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001448
kelvin-onlabd3b64892015-01-20 13:26:24 -08001449 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001450 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1451 str( destDir ) + str( copyFileName ) +
1452 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001453 self.handle.expect( "cp" )
1454 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001455 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001456 self.handle.sendline( "cp " + str( logToCopy ) +
1457 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001458 self.handle.expect( "cp" )
1459 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001460
kelvin8ec71442015-01-15 16:57:00 -08001461 return self.handle.before
1462
1463 except pexpect.EOF:
1464 main.log.error( "Copying files failed" )
1465 main.log.error( self.name + ": EOF exception found" )
1466 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001467 except Exception:
1468 main.log.exception( "Copying files failed" )
1469
kelvin-onlabd3b64892015-01-20 13:26:24 -08001470 def checkLogs( self, onosIp ):
kelvin8ec71442015-01-15 16:57:00 -08001471 """
Jon Hall94fd0472014-12-08 11:52:42 -08001472 runs onos-check-logs on the given onos node
1473 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001474 """
Jon Hall94fd0472014-12-08 11:52:42 -08001475 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001476 cmd = "onos-check-logs " + str( onosIp )
kelvin8ec71442015-01-15 16:57:00 -08001477 self.handle.sendline( cmd )
1478 self.handle.expect( cmd )
1479 self.handle.expect( "\$" )
Jon Hall94fd0472014-12-08 11:52:42 -08001480 response = self.handle.before
1481 return response
1482 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001483 main.log.error( "Lost ssh connection" )
1484 main.log.error( self.name + ": EOF exception found" )
1485 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001486 except Exception:
1487 main.log.exception( self.name + ": Uncaught exception!" )
1488 main.cleanup()
1489 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001490
kelvin-onlabd3b64892015-01-20 13:26:24 -08001491 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001492 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001493 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001494 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001495 try:
kelvin8ec71442015-01-15 16:57:00 -08001496 self.handle.sendline( "" )
1497 self.handle.expect( "\$" )
1498 self.handle.sendline( "onos-service " + str( node ) +
1499 " status" )
1500 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001501 "start/running",
1502 "stop/waiting",
kelvin8ec71442015-01-15 16:57:00 -08001503 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001504
1505 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001506 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001507 return main.TRUE
1508 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001509 main.log.info( "ONOS is stopped" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001510 return main.FALSE
1511 else:
kelvin8ec71442015-01-15 16:57:00 -08001512 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001513 main.cleanup()
1514 main.exit()
1515 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001516 main.log.error( self.name + ": EOF exception found" )
1517 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001518 main.cleanup()
1519 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001520 except Exception:
1521 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001522 main.cleanup()
1523 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001524
Jon Hall63604932015-02-26 17:09:50 -08001525 def setIpTables( self, ip, port='', action='add', packet_type='',
1526 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001527 """
Jon Hall21270ac2015-02-16 17:59:55 -08001528 Description:
1529 add or remove iptables rule to DROP (default) packets from
1530 specific IP and PORT
1531 Usage:
1532 * specify action ('add' or 'remove')
1533 when removing, pass in the same argument as you would add. It will
1534 delete that specific rule.
1535 * specify the ip to block
1536 * specify the destination port to block (defaults to all ports)
1537 * optional packet type to block (default tcp)
1538 * optional iptables rule (default DROP)
1539 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001540 * States boolean toggles adding all supported tcp states to the
1541 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001542 Returns:
1543 main.TRUE on success or
1544 main.FALSE if given invalid input or
1545 main.ERROR if there is an error in response from iptables
1546 WARNING:
1547 * This function uses root privilege iptables command which may result
1548 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001549 """
Jon Hall21270ac2015-02-16 17:59:55 -08001550 import time
1551
1552 # NOTE*********
1553 # The strict checking methods of this driver function is intentional
1554 # to discourage any misuse or error of iptables, which can cause
1555 # severe network errors
1556 # *************
1557
1558 # NOTE: Sleep needed to give some time for rule to be added and
1559 # registered to the instance. If you are calling this function
1560 # multiple times this sleep will prevent any errors.
1561 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001562 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001563 try:
1564 # input validation
1565 action_type = action.lower()
1566 rule = rule.upper()
1567 direction = direction.upper()
1568 if action_type != 'add' and action_type != 'remove':
1569 main.log.error( "Invalid action type. Use 'add' or "
1570 "'remove' table rule" )
1571 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1572 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1573 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1574 "'ACCEPT' or 'LOG' only." )
1575 if direction != 'INPUT' and direction != 'OUTPUT':
1576 # NOTE currently only supports rules INPUT and OUPTUT
1577 main.log.error( "Invalid rule. Valid directions are"
1578 " 'OUTPUT' or 'INPUT'" )
1579 return main.FALSE
1580 return main.FALSE
1581 return main.FALSE
1582 if action_type == 'add':
1583 # -A is the 'append' action of iptables
1584 actionFlag = '-A'
1585 elif action_type == 'remove':
1586 # -D is the 'delete' rule of iptables
1587 actionFlag = '-D'
1588 self.handle.sendline( "" )
1589 self.handle.expect( "\$" )
1590 cmd = "sudo iptables " + actionFlag + " " +\
1591 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001592 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001593 # " -p " + str( packet_type ) +\
1594 if packet_type:
1595 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001596 if port:
1597 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001598 if states:
1599 cmd += " -m state --state="
1600 #FIXME- Allow user to configure which states to block
1601 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001602 cmd += " -j " + str( rule )
1603
1604 self.handle.sendline( cmd )
1605 self.handle.expect( "\$" )
1606 main.log.warn( self.handle.before )
1607
1608 info_string = "On " + str( self.name )
1609 info_string += " " + str( action_type )
1610 info_string += " iptable rule [ "
1611 info_string += " IP: " + str( ip )
1612 info_string += " Port: " + str( port )
1613 info_string += " Rule: " + str( rule )
1614 info_string += " Direction: " + str( direction ) + " ]"
1615 main.log.info( info_string )
1616 return main.TRUE
1617 except pexpect.TIMEOUT:
1618 main.log.exception( self.name + ": Timeout exception in "
1619 "setIpTables function" )
1620 return main.ERROR
1621 except pexpect.EOF:
1622 main.log.error( self.name + ": EOF exception found" )
1623 main.log.error( self.name + ": " + self.handle.before )
1624 main.cleanup()
1625 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001626 except Exception:
1627 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001628 main.cleanup()
1629 main.exit()
1630
Jon Hall0468b042015-02-19 19:08:21 -08001631 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001632 """
Jon Hall0468b042015-02-19 19:08:21 -08001633 This method is used by STS to check the status of the controller
1634 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001635 """
Jon Hall0468b042015-02-19 19:08:21 -08001636 import re
1637 try:
1638 self.handle.sendline( "" )
1639 self.handle.expect( "\$" )
1640 self.handle.sendline( "cd " + self.home )
1641 self.handle.expect( "\$" )
1642 self.handle.sendline( "service onos status" )
1643 self.handle.expect( "\$" )
1644 response = self.handle.before
1645 if re.search( "onos start/running", response ):
1646 # onos start/running, process 10457
1647 return 'RUNNING'
1648 # FIXME: Implement this case
1649 # elif re.search( pattern, response ):
1650 # return 'STARTING'
1651 elif re.search( "onos stop/", response ):
1652 # onos stop/waiting
1653 # FIXME handle this differently?: onos stop/pre-stop
1654 return 'STOPPED'
1655 # FIXME: Implement this case
1656 # elif re.search( pattern, response ):
1657 # return 'FROZEN'
1658 else:
1659 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001660 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001661 main.log.warn( response )
1662 return 'ERROR', "Unknown response: %s" % response
1663 except pexpect.TIMEOUT:
1664 main.log.exception( self.name + ": Timeout exception in "
1665 "setIpTables function" )
1666 return 'ERROR', "Pexpect Timeout"
1667 except pexpect.EOF:
1668 main.log.error( self.name + ": EOF exception found" )
1669 main.log.error( self.name + ": " + self.handle.before )
1670 main.cleanup()
1671 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001672 except Exception:
1673 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001674 main.cleanup()
1675 main.exit()
1676