blob: 2b37186a6eb08e1d01e20d54c7aa1ae6b91cb668 [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"""
Jon Hall05b2b432014-10-08 19:53:25 -040019import time
Jon Hall6801cda2015-07-15 14:13:45 -070020import types
Jon Hall05b2b432014-10-08 19:53:25 -040021import pexpect
kelvin-onlaba4074292015-07-09 15:19:49 -070022import os
andrewonlab7735d852014-10-09 13:02:47 -040023import os.path
pingping-lin6d23d9e2015-02-02 16:54:24 -080024from requests.models import Response
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 """
Jon Hallefbd9792015-03-05 16:11:36 -080034 self.name = None
35 self.home = None
36 self.handle = None
kelvin8ec71442015-01-15 16:57:00 -080037 super( CLI, self ).__init__()
38
39 def connect( self, **connectargs ):
40 """
Jon Hall05b2b432014-10-08 19:53:25 -040041 Creates ssh handle for ONOS "bench".
kelvin-onlaba4074292015-07-09 15:19:49 -070042 NOTE:
43 The ip_address would come from the topo file using the host tag, the
44 value can be an environment variable as well as a "localhost" to get
45 the ip address needed to ssh to the "bench"
kelvin8ec71442015-01-15 16:57:00 -080046 """
Jon Hall05b2b432014-10-08 19:53:25 -040047 try:
48 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080049 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us3b087132015-03-11 15:00:08 -070050 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040051 for key in self.options:
52 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080053 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040054 break
Jon Hall274b6642015-02-17 11:57:17 -080055 if self.home is None or self.home == "":
Jon Halle94919c2015-03-23 11:42:57 -070056 self.home = "~/onos"
Jon Hall21270ac2015-02-16 17:59:55 -080057
kelvin8ec71442015-01-15 16:57:00 -080058 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070059
kelvin-onlabc2b79102015-07-14 11:41:20 -070060 # The 'nodes' tag is optional and it is not required in .topo file
kelvin-onlaba4074292015-07-09 15:19:49 -070061 for key in self.options:
62 if key == "nodes":
kelvin-onlabc2b79102015-07-14 11:41:20 -070063 # Maximum number of ONOS nodes to run, if there is any
kelvin-onlaba4074292015-07-09 15:19:49 -070064 self.maxNodes = int( self.options[ 'nodes' ] )
65 break
66 self.maxNodes = None
67
kelvin-onlabc2b79102015-07-14 11:41:20 -070068 if self.maxNodes == None or self.maxNodes == "":
69 self.maxNodes = 100
kelvin-onlaba4074292015-07-09 15:19:49 -070070
kelvin-onlabc2b79102015-07-14 11:41:20 -070071
72 # Grabs all OC environment variables based on max number of nodes
kelvin-onlaba4074292015-07-09 15:19:49 -070073 self.onosIps = {} # Dictionary of all possible ONOS ip
74
75 try:
76 if self.maxNodes:
kelvin-onlaba4074292015-07-09 15:19:49 -070077 for i in range( self.maxNodes ):
78 envString = "OC" + str( i + 1 )
kelvin-onlabc2b79102015-07-14 11:41:20 -070079 # If there is no more OC# then break the loop
80 if os.getenv( envString ):
81 self.onosIps[ envString ] = os.getenv( envString )
82 else:
83 self.maxNodes = len( self.onosIps )
84 main.log.info( self.name +
85 ": Created cluster data with " +
86 str( self.maxNodes ) +
87 " maximum number" +
88 " of nodes" )
89 break
kelvin-onlaba4074292015-07-09 15:19:49 -070090
91 if not self.onosIps:
92 main.log.info( "Could not read any environment variable"
93 + " please load a cell file with all" +
94 " onos IP" )
Jon Hall5cf14d52015-07-16 12:15:19 -070095 self.maxNodes = None
kelvin-onlaba4074292015-07-09 15:19:49 -070096 else:
97 main.log.info( self.name + ": Found " +
98 str( self.onosIps.values() ) +
99 " ONOS IPs" )
kelvin-onlaba4074292015-07-09 15:19:49 -0700100 except KeyError:
101 main.log.info( "Invalid environment variable" )
102 except Exception as inst:
103 main.log.error( "Uncaught exception: " + str( inst ) )
104
105 try:
106 if os.getenv( str( self.ip_address ) ) != None:
107 self.ip_address = os.getenv( str( self.ip_address ) )
108 else:
109 main.log.info( self.name +
110 ": Trying to connect to " +
111 self.ip_address )
kelvin-onlaba4074292015-07-09 15:19:49 -0700112 except KeyError:
113 main.log.info( "Invalid host name," +
114 " connecting to local host instead" )
115 self.ip_address = 'localhost'
116 except Exception as inst:
117 main.log.error( "Uncaught exception: " + str( inst ) )
118
kelvin8ec71442015-01-15 16:57:00 -0800119 self.handle = super( OnosDriver, self ).connect(
120 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -0800121 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800122 port=self.port,
123 pwd=self.pwd,
124 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -0400125
Jon Hall05b2b432014-10-08 19:53:25 -0400126 if self.handle:
Jon Hall0fc0d452015-07-14 09:49:58 -0700127 self.handle.sendline( "cd " + self.home )
128 self.handle.expect( "\$" )
Jon Hall05b2b432014-10-08 19:53:25 -0400129 return self.handle
kelvin8ec71442015-01-15 16:57:00 -0800130 else:
Jon Hall0fc0d452015-07-14 09:49:58 -0700131 main.log.info( "Failed to create ONOS handle" )
Jon Hall05b2b432014-10-08 19:53:25 -0400132 return main.FALSE
133 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800134 main.log.error( self.name + ": EOF exception found" )
135 main.log.error( self.name + ": " + self.handle.before )
Jon Hall05b2b432014-10-08 19:53:25 -0400136 main.cleanup()
137 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800138 except Exception:
139 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall05b2b432014-10-08 19:53:25 -0400140 main.cleanup()
141 main.exit()
142
kelvin8ec71442015-01-15 16:57:00 -0800143 def disconnect( self ):
144 """
Jon Hall05b2b432014-10-08 19:53:25 -0400145 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800146 """
Jon Halld61331b2015-02-17 16:35:47 -0800147 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -0400148 try:
Jon Hall61282e32015-03-19 11:34:11 -0700149 if self.handle:
150 self.handle.sendline( "" )
151 self.handle.expect( "\$" )
152 self.handle.sendline( "exit" )
153 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -0400154 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800155 main.log.error( self.name + ": EOF exception found" )
156 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700157 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700158 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700159 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800160 except Exception:
161 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -0400162 response = main.FALSE
163 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400164
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400165 def getEpochMs( self ):
166 """
167 Returns milliseconds since epoch
Jon Hall4ba53f02015-07-29 13:07:41 -0700168
169 When checking multiple nodes in a for loop,
170 around a hundred milliseconds of difference (ascending) is
171 generally acceptable due to calltime of the function.
172 Few seconds, however, is not and it means clocks
173 are off sync.
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400174 """
175 try:
176 self.handle.sendline( 'date +%s.%N' )
177 self.handle.expect( 'date \+\%s\.\%N' )
178 self.handle.expect( '\$' )
179 epochMs = self.handle.before
180 return epochMs
181 except Exception:
182 main.log.exception( 'Uncaught exception getting epoch time' )
183 main.cleanup()
184 main.exit()
185
pingping-lin57a56ce2015-05-20 16:43:48 -0700186 def onosPackage( self, opTimeout=30 ):
kelvin8ec71442015-01-15 16:57:00 -0800187 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400188 Produce a self-contained tar.gz file that can be deployed
kelvin8ec71442015-01-15 16:57:00 -0800189 and executed on any platform with Java 7 JRE.
190 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400191 try:
kelvin8ec71442015-01-15 16:57:00 -0800192 self.handle.sendline( "onos-package" )
193 self.handle.expect( "onos-package" )
pingping-lin57a56ce2015-05-20 16:43:48 -0700194 self.handle.expect( "tar.gz", opTimeout )
kelvin8ec71442015-01-15 16:57:00 -0800195 handle = str( self.handle.before )
196 main.log.info( "onos-package command returned: " +
197 handle )
198 # As long as the sendline does not time out,
199 # return true. However, be careful to interpret
200 # the results of the onos-package command return
andrewonlab0748d2a2014-10-09 13:24:17 -0400201 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400202
andrewonlab7735d852014-10-09 13:02:47 -0400203 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800204 main.log.error( self.name + ": EOF exception found" )
205 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800206 except Exception:
207 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400208 main.cleanup()
209 main.exit()
210
kelvin-onlabd3b64892015-01-20 13:26:24 -0800211 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800212 """
andrewonlab8790abb2014-11-06 13:51:54 -0500213 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800214 """
andrewonlab8790abb2014-11-06 13:51:54 -0500215 try:
kelvin8ec71442015-01-15 16:57:00 -0800216 self.handle.sendline( "onos-build" )
217 self.handle.expect( "onos-build" )
218 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800219 "BUILD SUCCESS",
220 "ERROR",
221 "BUILD FAILED" ],
222 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800223 handle = str( self.handle.before )
andrewonlab8790abb2014-11-06 13:51:54 -0500224
kelvin8ec71442015-01-15 16:57:00 -0800225 main.log.info( "onos-build command returned: " +
226 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500227
228 if i == 0:
229 return main.TRUE
230 else:
231 return handle
232
233 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800234 main.log.error( self.name + ": EOF exception found" )
235 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800236 except Exception:
237 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500238 main.cleanup()
239 main.exit()
240
shahshreya9f531fe2015-06-10 12:03:51 -0700241 def cleanInstall( self, skipTest=False, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800242 """
243 Runs mvn clean install in the root of the ONOS directory.
244 This will clean all ONOS artifacts then compile each module
shahshreya9f531fe2015-06-10 12:03:51 -0700245 Optional:
246 skipTest - Does "-DskipTests -Dcheckstyle.skip -U -T 1C" which
247 skip the test. This will make the building faster.
248 Disregarding the credibility of the build
kelvin8ec71442015-01-15 16:57:00 -0800249 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400250 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800251 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400252 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800253 main.log.info( "Running 'mvn clean install' on " +
254 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800255 ". This may take some time." )
256 self.handle.sendline( "cd " + self.home )
257 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400258
kelvin8ec71442015-01-15 16:57:00 -0800259 self.handle.sendline( "" )
260 self.handle.expect( "\$" )
shahshreya9f531fe2015-06-10 12:03:51 -0700261
262 if not skipTest:
263 self.handle.sendline( "mvn clean install" )
264 self.handle.expect( "mvn clean install" )
265 else:
266 self.handle.sendline( "mvn clean install -DskipTests" +
267 " -Dcheckstyle.skip -U -T 1C" )
268 self.handle.expect( "mvn clean install -DskipTests" +
269 " -Dcheckstyle.skip -U -T 1C" )
kelvin8ec71442015-01-15 16:57:00 -0800270 while True:
271 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800272 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800273 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400274 'BUILD\sFAILURE',
275 'BUILD\sSUCCESS',
Jon Halle94919c2015-03-23 11:42:57 -0700276 'onos\$', #TODO: fix this to be more generic?
Jon Hallde9d9aa2014-10-08 20:36:02 -0400277 'ONOS\$',
pingping-lin57a56ce2015-05-20 16:43:48 -0700278 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400279 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800280 main.log.error( self.name + ":There is insufficient memory \
281 for the Java Runtime Environment to continue." )
282 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400283 main.cleanup()
284 main.exit()
285 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800286 main.log.error( self.name + ": Build failure!" )
287 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400288 main.cleanup()
289 main.exit()
290 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800291 main.log.info( self.name + ": Build success!" )
Jon Halle94919c2015-03-23 11:42:57 -0700292 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800293 main.log.info( self.name + ": Build complete" )
294 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400295 for line in self.handle.before.splitlines():
296 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800297 main.log.info( line )
298 self.handle.sendline( "" )
299 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400300 return main.TRUE
Jon Halle94919c2015-03-23 11:42:57 -0700301 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800302 main.log.error(
303 self.name +
304 ": mvn clean install TIMEOUT!" )
305 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400306 main.cleanup()
307 main.exit()
308 else:
Jon Hall274b6642015-02-17 11:57:17 -0800309 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800310 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800311 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400312 main.cleanup()
313 main.exit()
314 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800315 main.log.error( self.name + ": EOF exception found" )
316 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400317 main.cleanup()
318 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800319 except Exception:
320 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400321 main.cleanup()
322 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400323
Jon Hall61282e32015-03-19 11:34:11 -0700324 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800325 """
Jon Hallacabffd2014-10-09 12:36:53 -0400326 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800327
Jon Hall61282e32015-03-19 11:34:11 -0700328 If the fastForward boolean is set to true, only git pulls that can
329 be fast forwarded will be performed. IE if you have not local commits
330 in your branch.
331
Jon Hallacabffd2014-10-09 12:36:53 -0400332 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800333 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400334 for the purpose of pulling from other nodes if necessary.
335
Jon Hall47a93fb2015-01-06 16:46:06 -0800336 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400337 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800338 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400339 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400340
kelvin8ec71442015-01-15 16:57:00 -0800341 """
Jon Hallacabffd2014-10-09 12:36:53 -0400342 try:
kelvin8ec71442015-01-15 16:57:00 -0800343 # main.log.info( self.name + ": Stopping ONOS" )
344 # self.stop()
345 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800346 self.handle.expect( self.home + "\$" )
Jon Hall61282e32015-03-19 11:34:11 -0700347 cmd = "git pull"
348 if comp1 != "":
349 cmd += ' ' + comp1
350 if fastForward:
351 cmd += ' ' + " --ff-only"
352 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800353 i = self.handle.expect(
354 [
355 'fatal',
356 'Username\sfor\s(.*):\s',
357 '\sfile(s*) changed,\s',
358 'Already up-to-date',
359 'Aborting',
360 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800361 'You asked me to pull without telling me which branch you',
362 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700363 'Please enter a commit message to explain why this merge',
364 'Found a swap file by the name',
365 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800366 pexpect.TIMEOUT ],
367 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800368 # debug
kelvin-onlabd3b64892015-01-20 13:26:24 -0800369 # main.log.report( self.name +": DEBUG: \n"+
370 # "git pull response: " +
371 # str( self.handle.before ) + str( self.handle.after ) )
kelvin8ec71442015-01-15 16:57:00 -0800372 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700373 main.log.error( self.name + ": Git pull had some issue" )
374 output = self.handle.after
375 self.handle.expect( '\$' )
376 output += self.handle.before
377 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400378 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800379 elif i == 1:
380 main.log.error(
381 self.name +
382 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400383 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800384 elif i == 2:
385 main.log.info(
386 self.name +
387 ": Git Pull - pulling repository now" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800388 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800389 # So that only when git pull is done, we do mvn clean compile
390 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800391 elif i == 3:
392 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800393 return i
kelvin8ec71442015-01-15 16:57:00 -0800394 elif i == 4:
395 main.log.info(
396 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800397 ": Git Pull - Aborting..." +
398 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400399 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800400 elif i == 5:
401 main.log.info(
402 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800403 ": Git Pull - You are not currently " +
404 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400405 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800406 elif i == 6:
407 main.log.info(
408 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800409 ": Git Pull - You have not configured an upstream " +
410 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400411 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800412 elif i == 7:
413 main.log.info(
414 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800415 ": Git Pull - Pull is not possible because " +
416 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400417 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800418 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700419 # NOTE: abandoning test since we can't reliably handle this
420 # there could be different default text editors and we
421 # also don't know if we actually want to make the commit
422 main.log.error( "Git pull resulted in a merge commit message" +
423 ". Exiting test!" )
424 main.cleanup()
425 main.exit()
426 elif i == 9: # Merge commit message but swap file exists
427 main.log.error( "Git pull resulted in a merge commit message" +
428 " but a swap file exists." )
429 try:
430 self.handle.send( 'A' ) # Abort
431 self.handle.expect( "\$" )
432 return main.ERROR
433 except Exception:
434 main.log.exception( "Couldn't exit editor prompt!")
435 main.cleanup()
436 main.exit()
437 elif i == 10: # In the middle of a merge commit
438 main.log.error( "Git branch is in the middle of a merge. " )
439 main.log.warn( self.handle.before + self.handle.after )
440 return main.ERROR
441 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800442 main.log.error( self.name + ": Git Pull - TIMEOUT" )
443 main.log.error(
444 self.name + " Response was: " + str(
445 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400446 return main.ERROR
447 else:
kelvin8ec71442015-01-15 16:57:00 -0800448 main.log.error(
449 self.name +
450 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400451 return main.ERROR
452 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800453 main.log.error( self.name + ": EOF exception found" )
454 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400455 main.cleanup()
456 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800457 except Exception:
458 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400459 main.cleanup()
460 main.exit()
461
kelvin-onlabd3b64892015-01-20 13:26:24 -0800462 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800463 """
Jon Hallacabffd2014-10-09 12:36:53 -0400464 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800465
Jon Hallacabffd2014-10-09 12:36:53 -0400466 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800467 If used as gitCheckout( "branch" ) it will do git checkout
468 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400469
470 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800471 branch of the ONOS repository. If it has any problems, it will return
472 main.ERROR.
473 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400474 successful then the function will return main.TRUE.
475
kelvin8ec71442015-01-15 16:57:00 -0800476 """
Jon Hallacabffd2014-10-09 12:36:53 -0400477 try:
kelvin8ec71442015-01-15 16:57:00 -0800478 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800479 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800480 main.log.info( self.name +
481 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800482 cmd = "git checkout " + branch
483 self.handle.sendline( cmd )
484 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800485 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800486 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700487 'Username for (.*): ',
488 'Already on \'',
Jon Hall7a8354f2015-06-10 15:37:00 -0700489 'Switched to (a new )?branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800490 pexpect.TIMEOUT,
491 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800492 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800493 'error: you need to resolve your current index first',
494 "You are in 'detached HEAD' state.",
495 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800496 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800497 if i == 0:
498 main.log.error(
499 self.name +
500 ": Git checkout had some issue..." )
501 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400502 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800503 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800504 main.log.error(
505 self.name +
506 ": Git checkout asking for username." +
507 " Please configure your local git repository to be able " +
508 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800509 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400510 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800511 elif i == 2:
512 main.log.info(
513 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800514 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800515 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800516 # main.log.info( "DEBUG: after checkout cmd = "+
517 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400518 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800519 elif i == 3:
520 main.log.info(
521 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800522 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800523 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800524 # main.log.info( "DEBUG: after checkout cmd = "+
525 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400526 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800527 elif i == 4:
528 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
529 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800530 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400531 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800532 elif i == 5:
533 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800534 main.log.error(
535 self.name +
536 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800537 "Your local changes to the following files would" +
538 " be overwritten by checkout:" +
539 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800540 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500541 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800542 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800543 main.log.error(
544 self.name +
545 ": Git checkout error: \n" +
546 "You need to resolve your current index first:" +
547 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800548 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500549 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800550 elif i == 7:
551 main.log.info(
552 self.name +
553 ": Git checkout " + str( branch ) +
554 " - You are in 'detached HEAD' state. HEAD is now at " +
555 str( branch ) )
556 self.handle.expect( self.home + "\$" )
557 return main.TRUE
558 elif i == 8: # Already in detached HEAD on the specified commit
559 main.log.info(
560 self.name +
561 ": Git Checkout %s : Already on commit" % branch )
562 self.handle.expect( self.home + "\$" )
563 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400564 else:
kelvin8ec71442015-01-15 16:57:00 -0800565 main.log.error(
566 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800567 ": Git Checkout - Unexpected response, " +
568 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800569 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400570 return main.ERROR
571
572 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800573 main.log.error( self.name + ": EOF exception found" )
574 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400575 main.cleanup()
576 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800577 except Exception:
578 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400579 main.cleanup()
580 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400581
pingping-lin6d23d9e2015-02-02 16:54:24 -0800582 def getBranchName( self ):
pingping-linf30cf272015-05-29 15:54:07 -0700583 main.log.info( "self.home = " )
584 main.log.info( self.home )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800585 self.handle.sendline( "cd " + self.home )
pingping-lin9bf3d8f2015-05-29 16:05:28 -0700586 self.handle.expect( self.home + "\$" )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800587 self.handle.sendline( "git name-rev --name-only HEAD" )
588 self.handle.expect( "git name-rev --name-only HEAD" )
589 self.handle.expect( "\$" )
590
591 lines = self.handle.before.splitlines()
592 if lines[1] == "master":
593 return "master"
594 elif lines[1] == "onos-1.0":
595 return "onos-1.0"
596 else:
597 main.log.info( lines[1] )
598 return "unexpected ONOS branch for SDN-IP test"
599
kelvin-onlabd3b64892015-01-20 13:26:24 -0800600 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800601 """
Jon Hall274b6642015-02-17 11:57:17 -0800602 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800603 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800604 """
Jon Hall45ec0922014-10-10 19:33:49 -0400605 try:
kelvin8ec71442015-01-15 16:57:00 -0800606 self.handle.sendline( "" )
607 self.handle.expect( "\$" )
608 self.handle.sendline(
609 "cd " +
610 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800611 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
612 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800613 # NOTE: for some reason there are backspaces inserted in this
614 # phrase when run from Jenkins on some tests
615 self.handle.expect( "never" )
616 self.handle.expect( "\$" )
617 response = ( self.name + ": \n" + str(
618 self.handle.before + self.handle.after ) )
619 self.handle.sendline( "cd " + self.home )
620 self.handle.expect( "\$" )
621 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400622 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500623 print line
624 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700625 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800626 for line in lines[ 2:-1 ]:
627 # Bracket replacement is for Wiki-compliant
628 # formatting. '<' or '>' are interpreted
629 # as xml specific tags that cause errors
630 line = line.replace( "<", "[" )
631 line = line.replace( ">", "]" )
pingping-lin763ee042015-05-20 17:45:30 -0700632 #main.log.wiki( "\t" + line )
633 main.log.wiki( line + "<br /> " )
634 main.log.summary( line )
635 main.log.wiki( "</blockquote>" )
636 main.log.summary("\n")
kelvin8ec71442015-01-15 16:57:00 -0800637 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400638 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800639 main.log.error( self.name + ": EOF exception found" )
640 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400641 main.cleanup()
642 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800643 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800644 main.log.error( self.name + ": TIMEOUT exception found" )
645 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800646 main.cleanup()
647 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800648 except Exception:
649 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400650 main.cleanup()
651 main.exit()
652
kelvin-onlabd3b64892015-01-20 13:26:24 -0800653 def createCellFile( self, benchIp, fileName, mnIpAddrs,
kelvin-onlaba4074292015-07-09 15:19:49 -0700654 appString, onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800655 """
andrewonlab94282092014-10-10 13:00:11 -0400656 Creates a cell file based on arguments
657 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800658 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400659 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800660 * File name of the cell file ( fileName )
661 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800662 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400663 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800664 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400665 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800666
andrewonlab94282092014-10-10 13:00:11 -0400667 NOTE: Assumes cells are located at:
668 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800669 """
670 # Variable initialization
Jon Hall6801cda2015-07-15 14:13:45 -0700671 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800672 # We want to create the cell file in the dependencies directory
673 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800674 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800675 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800676 cellFile = open( tempDirectory + fileName, 'w+' )
Jon Hall6801cda2015-07-15 14:13:45 -0700677 if isinstance( onosIpAddrs, types.StringType ):
678 onosIpAddrs = [ onosIpAddrs ]
kelvin8ec71442015-01-15 16:57:00 -0800679
cameron@onlab.us75900962015-03-30 13:22:49 -0700680 # App string is hardcoded environment variables
kelvin8ec71442015-01-15 16:57:00 -0800681 # That you may wish to use by default on startup.
cameron@onlab.us75900962015-03-30 13:22:49 -0700682 # Note that you may not want certain apps listed
kelvin8ec71442015-01-15 16:57:00 -0800683 # on here.
cameron@onlab.us75900962015-03-30 13:22:49 -0700684 appString = "export ONOS_APPS=" + appString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800685 mnString = "export OCN="
kelvin-onlabf70fd542015-05-07 18:41:40 -0700686 if mnIpAddrs == "":
687 mnString = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800688 onosString = "export OC"
689 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800690
kelvin-onlabd3b64892015-01-20 13:26:24 -0800691 # Create ONOSNIC ip address prefix
kelvin-onlaba4074292015-07-09 15:19:49 -0700692 tempOnosIp = str( onosIpAddrs[ 0 ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800693 tempList = []
694 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800695 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800696 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800697 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800698 nicAddr = ".".join( tempList ) + ".*"
699 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400700
701 try:
kelvin8ec71442015-01-15 16:57:00 -0800702 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800703 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400704
kelvin-onlabd3b64892015-01-20 13:26:24 -0800705 for arg in onosIpAddrs:
706 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800707 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400708 # export OC1="10.128.20.11"
709 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800710 cellFile.write( onosString + str( tempCount ) +
711 "=" + "\"" + arg + "\"" + "\n" )
712 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800713
kelvin-onlabd3b64892015-01-20 13:26:24 -0800714 cellFile.write( mnString + "\"" + mnIpAddrs + "\"" + "\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700715 cellFile.write( appString + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800716 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400717
kelvin8ec71442015-01-15 16:57:00 -0800718 # We use os.system to send the command to TestON cluster
719 # to account for the case in which TestON is not located
720 # on the same cluster as the ONOS bench
721 # Note that even if TestON is located on the same cluster
722 # as ONOS bench, you must setup passwordless ssh
723 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabc2b79102015-07-14 11:41:20 -0700724 os.system( "scp " + tempDirectory + fileName + " " +
725 self.user_name + "@" + self.ip_address + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400726
andrewonlab2a6c9342014-10-16 13:40:15 -0400727 return main.TRUE
728
andrewonlab94282092014-10-10 13:00:11 -0400729 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800730 main.log.error( self.name + ": EOF exception found" )
731 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400732 main.cleanup()
733 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800734 except Exception:
735 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400736 main.cleanup()
737 main.exit()
738
kelvin-onlabd3b64892015-01-20 13:26:24 -0800739 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800740 """
andrewonlab95ca1462014-10-09 14:04:24 -0400741 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800742 """
Hari Krishna03f530e2015-07-10 17:28:27 -0700743 import re
andrewonlab95ca1462014-10-09 14:04:24 -0400744 try:
745 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800746 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400747 main.cleanup()
748 main.exit()
749 else:
kelvin8ec71442015-01-15 16:57:00 -0800750 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800751 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800752 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400753 # and that this driver will have to change accordingly
Cameron Franke9c94fb02015-01-21 10:20:20 -0800754 self.handle.expect(str(cellname))
kelvin-onlabd3b64892015-01-20 13:26:24 -0800755 handleBefore = self.handle.before
756 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800757 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800758 self.handle.sendline("")
759 self.handle.expect("\$")
kelvin-onlabd3b64892015-01-20 13:26:24 -0800760 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400761
Hari Krishna03f530e2015-07-10 17:28:27 -0700762 cell_result = handleBefore + handleAfter + handleMore
763 print cell_result
764 if( re.search( "No such cell", cell_result ) ):
765 main.log.error( "Cell call returned: " + handleBefore +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800766 handleAfter + handleMore )
Hari Krishna03f530e2015-07-10 17:28:27 -0700767 main.cleanup()
768 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400769 return main.TRUE
770
771 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800772 main.log.error( self.name + ": EOF exception found" )
773 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400774 main.cleanup()
775 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800776 except Exception:
777 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400778 main.cleanup()
779 main.exit()
780
kelvin-onlabd3b64892015-01-20 13:26:24 -0800781 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800782 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400783 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800784 """
785 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400786
andrewonlabc03bf6c2014-10-09 14:56:18 -0400787 try:
kelvin8ec71442015-01-15 16:57:00 -0800788 # Clean handle by sending empty and expecting $
789 self.handle.sendline( "" )
790 self.handle.expect( "\$" )
791 self.handle.sendline( "onos-verify-cell" )
792 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800793 handleBefore = self.handle.before
794 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800795 # Get the rest of the handle
796 self.handle.sendline( "" )
797 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800798 handleMore = self.handle.before
andrewonlabc03bf6c2014-10-09 14:56:18 -0400799
kelvin-onlabd3b64892015-01-20 13:26:24 -0800800 main.log.info( "Verify cell returned: " + handleBefore +
801 handleAfter + handleMore )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400802
803 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800804 except pexpect.ExceptionPexpect as e:
805 main.log.error( self.name + ": Pexpect exception found of type " +
806 str( type( e ) ) )
807 main.log.error ( e.get_trace() )
kelvin8ec71442015-01-15 16:57:00 -0800808 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400809 main.cleanup()
810 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800811 except Exception:
812 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400813 main.cleanup()
814 main.exit()
815
jenkins1e99e7b2015-04-02 18:15:39 -0700816 def onosCfgSet( self, ONOSIp, configName, configParam ):
817 """
818 Uses 'onos <node-ip> cfg set' to change a parameter value of an
Jon Hall4ba53f02015-07-29 13:07:41 -0700819 application.
820
jenkins1e99e7b2015-04-02 18:15:39 -0700821 ex)
822 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
jenkins1e99e7b2015-04-02 18:15:39 -0700823 ONOSIp = '10.0.0.1'
824 configName = 'org.onosproject.myapp'
825 configParam = 'appSetting 1'
jenkins1e99e7b2015-04-02 18:15:39 -0700826 """
cameron@onlab.us78b89652015-07-08 15:21:03 -0700827 for i in range(5):
828 try:
829 cfgStr = ( "onos "+str(ONOSIp)+" cfg set "+
830 str(configName) + " " +
831 str(configParam)
832 )
jenkins1e99e7b2015-04-02 18:15:39 -0700833
cameron@onlab.us78b89652015-07-08 15:21:03 -0700834 self.handle.sendline( "" )
835 self.handle.expect( ":~" )
836 self.handle.sendline( cfgStr )
Jon Hall4ba53f02015-07-29 13:07:41 -0700837 self.handle.expect("cfg set")
cameron@onlab.us78b89652015-07-08 15:21:03 -0700838 self.handle.expect( ":~" )
Jon Hall4ba53f02015-07-29 13:07:41 -0700839
cameron@onlab.us78b89652015-07-08 15:21:03 -0700840 paramValue = configParam.split(" ")[1]
841 paramName = configParam.split(" ")[0]
Jon Hall4ba53f02015-07-29 13:07:41 -0700842
cameron@onlab.us78b89652015-07-08 15:21:03 -0700843 checkStr = ( "onos " + str(ONOSIp) + """ cfg get " """ + str(configName) + " " + paramName + """ " """)
jenkins1e99e7b2015-04-02 18:15:39 -0700844
cameron@onlab.us78b89652015-07-08 15:21:03 -0700845 self.handle.sendline( checkStr )
846 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700847
cameron@onlab.us78b89652015-07-08 15:21:03 -0700848 if "value=" + paramValue + "," in self.handle.before:
Jon Hall4ba53f02015-07-29 13:07:41 -0700849 main.log.info("cfg " + configName + " successfully set to " + configParam)
cameron@onlab.us78b89652015-07-08 15:21:03 -0700850 return main.TRUE
851
852 except pexpect.ExceptionPexpect as e:
853 main.log.error( self.name + ": Pexpect exception found of type " +
854 str( type( e ) ) )
855 main.log.error ( e.get_trace() )
856 main.log.error( self.name + ": " + self.handle.before )
857 main.cleanup()
858 main.exit()
859 except Exception:
860 main.log.exception( self.name + ": Uncaught exception!" )
861 main.cleanup()
862 main.exit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700863
cameron@onlab.us78b89652015-07-08 15:21:03 -0700864 time.sleep(5)
865
866 main.log.error("CFG SET FAILURE: " + configName + " " + configParam )
Jon Hall4ba53f02015-07-29 13:07:41 -0700867 main.ONOSbench.handle.sendline("onos $OC1 cfg get")
cameron@onlab.us78b89652015-07-08 15:21:03 -0700868 main.ONOSbench.handle.expect("\$")
869 print main.ONOSbench.handle.before
870 main.ONOSbench.logReport( ONOSIp, ["ERROR","WARN","EXCEPT"], "d")
871 return main.FALSE
872
Jon Hall4ba53f02015-07-29 13:07:41 -0700873
kelvin-onlabd3b64892015-01-20 13:26:24 -0800874 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800875 """
andrewonlab05e362f2014-10-10 00:40:57 -0400876 Uses 'onos' command to send various ONOS CLI arguments.
877 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800878 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400879 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800880
881 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400882 CLI commands for ONOS. Try to use this function first
883 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800884 function.
885 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400886 by starting onos, and typing in 'onos' to enter the
887 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800888 available commands.
889 """
andrewonlab05e362f2014-10-10 00:40:57 -0400890 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800891 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800892 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400893 return main.FALSE
894 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800895 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400896 return main.FALSE
897
kelvin8ec71442015-01-15 16:57:00 -0800898 cmdstr = str( cmdstr )
899 self.handle.sendline( "" )
900 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400901
kelvin-onlabd3b64892015-01-20 13:26:24 -0800902 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800903 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400904
kelvin-onlabd3b64892015-01-20 13:26:24 -0800905 handleBefore = self.handle.before
Shreya Shaha73aaad2014-10-27 18:03:09 -0400906 print "handle_before = ", self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800907 # handleAfter = str( self.handle.after )
Jon Hall47a93fb2015-01-06 16:46:06 -0800908
kelvin8ec71442015-01-15 16:57:00 -0800909 # self.handle.sendline( "" )
910 # self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800911 # handleMore = str( self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400912
kelvin8ec71442015-01-15 16:57:00 -0800913 main.log.info( "Command sent successfully" )
andrewonlab05e362f2014-10-10 00:40:57 -0400914
kelvin8ec71442015-01-15 16:57:00 -0800915 # Obtain return handle that consists of result from
916 # the onos command. The string may need to be
917 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800918 # returnString = handleBefore + handleAfter
919 returnString = handleBefore
920 print "return_string = ", returnString
921 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400922
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 )
andrewonlab05e362f2014-10-10 00:40:57 -0400926 main.cleanup()
927 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800928 except Exception:
929 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400930 main.cleanup()
931 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400932
kelvin-onlabd3b64892015-01-20 13:26:24 -0800933 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800934 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400935 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800936 If -f option is provided, it also forces an uninstall.
937 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400938 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800939 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400940 files to certain onos nodes
941
942 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800943 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400944 try:
andrewonlab114768a2014-11-14 12:44:44 -0500945 if options:
kelvin8ec71442015-01-15 16:57:00 -0800946 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500947 else:
kelvin8ec71442015-01-15 16:57:00 -0800948 self.handle.sendline( "onos-install " + node )
949 self.handle.expect( "onos-install " )
950 # NOTE: this timeout may need to change depending on the network
951 # and size of ONOS
952 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800953 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800954 "ONOS\sis\salready\sinstalled",
955 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400956
Jon Hall7993bfc2014-10-09 16:30:14 -0400957 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800958 main.log.warn( "Network is unreachable" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400959 return main.FALSE
960 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800961 main.log.info(
962 "ONOS was installed on " +
963 node +
964 " and started" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400965 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500966 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800967 main.log.info( "ONOS is already installed on " + node )
andrewonlabd9a73a72014-11-14 17:28:21 -0500968 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800969 elif i == 3:
970 main.log.info(
971 "Installation of ONOS on " +
972 node +
973 " timed out" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400974 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400975
976 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800977 main.log.error( self.name + ": EOF exception found" )
978 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400979 main.cleanup()
980 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800981 except Exception:
982 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400983 main.cleanup()
984 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400985
kelvin-onlabd3b64892015-01-20 13:26:24 -0800986 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800987 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400988 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400989 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800990 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400991 try:
kelvin8ec71442015-01-15 16:57:00 -0800992 self.handle.sendline( "" )
993 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800994 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800995 " start" )
996 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -0400997 "Job\sis\salready\srunning",
998 "start/running",
999 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -08001000 pexpect.TIMEOUT ], timeout=120 )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001001
1002 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001003 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001004 return main.TRUE
1005 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001006 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001007 return main.TRUE
1008 else:
kelvin8ec71442015-01-15 16:57:00 -08001009 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001010 main.cleanup()
1011 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001012 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001013 main.log.error( self.name + ": EOF exception found" )
1014 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001015 main.cleanup()
1016 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001017 except Exception:
1018 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001019 main.cleanup()
1020 main.exit()
1021
kelvin-onlabd3b64892015-01-20 13:26:24 -08001022 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001023 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001024 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001025 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001026 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001027 try:
kelvin8ec71442015-01-15 16:57:00 -08001028 self.handle.sendline( "" )
1029 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001030 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001031 " stop" )
1032 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -04001033 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -07001034 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -04001035 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -08001036 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2b30bd32014-10-09 16:48:55 -04001037
1038 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001039 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001040 return main.TRUE
1041 elif i == 1:
Jon Hall65844a32015-03-09 19:09:37 -07001042 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001043 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -04001044 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -07001045 elif i == 2:
1046 main.log.warn( "ONOS wasn't running" )
1047 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -04001048 else:
kelvin8ec71442015-01-15 16:57:00 -08001049 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001050 return main.FALSE
1051
1052 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001053 main.log.error( self.name + ": EOF exception found" )
1054 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -04001055 main.cleanup()
1056 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001057 except Exception:
1058 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001059 main.cleanup()
1060 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001061
kelvin-onlabd3b64892015-01-20 13:26:24 -08001062 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -08001063 """
andrewonlabc8d47972014-10-09 16:52:36 -04001064 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -08001065 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -04001066 if needed
kelvin8ec71442015-01-15 16:57:00 -08001067 """
andrewonlabc8d47972014-10-09 16:52:36 -04001068 try:
kelvin8ec71442015-01-15 16:57:00 -08001069 self.handle.sendline( "" )
pingping-lin763ee042015-05-20 17:45:30 -07001070 self.handle.expect( "\$", timeout=60 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001071 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001072 self.handle.expect( "\$" )
andrewonlabc8d47972014-10-09 16:52:36 -04001073
kelvin-onlabd3b64892015-01-20 13:26:24 -08001074 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
andrewonlab9dfd2082014-11-13 17:44:03 -05001075
kelvin8ec71442015-01-15 16:57:00 -08001076 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -04001077 return main.TRUE
1078
pingping-lin763ee042015-05-20 17:45:30 -07001079 except pexpect.TIMEOUT:
1080 main.log.exception( self.name + ": Timeout in onosUninstall" )
1081 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -04001082 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001083 main.log.error( self.name + ": EOF exception found" )
1084 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -04001085 main.cleanup()
1086 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001087 except Exception:
1088 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -04001089 main.cleanup()
1090 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -04001091
kelvin-onlabd3b64892015-01-20 13:26:24 -08001092 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001093 """
andrewonlabaedc8332014-12-04 12:43:03 -05001094 Issues the command 'onos-die <node-ip>'
1095 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -08001096 """
andrewonlabaedc8332014-12-04 12:43:03 -05001097 try:
kelvin8ec71442015-01-15 16:57:00 -08001098 self.handle.sendline( "" )
1099 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001100 cmdStr = "onos-kill " + str( nodeIp )
1101 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001102 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001103 "Killing\sONOS",
1104 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -08001105 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -05001106 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001107 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001108 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -05001109 return main.TRUE
1110 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001111 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -05001112 return main.FALSE
1113 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001114 main.log.error( self.name + ": EOF exception found" )
1115 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -05001116 main.cleanup()
1117 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001118 except Exception:
1119 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -05001120 main.cleanup()
1121 main.exit()
1122
kelvin-onlabd3b64892015-01-20 13:26:24 -08001123 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001124 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001125 Calls the command: 'onos-kill [<node-ip>]'
1126 "Remotely, and unceremoniously kills the ONOS instance running on
1127 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001128 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001129 try:
kelvin8ec71442015-01-15 16:57:00 -08001130 self.handle.sendline( "" )
1131 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001132 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001133 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -04001134 "\$",
1135 "No\sroute\sto\shost",
1136 "password:",
kelvin8ec71442015-01-15 16:57:00 -08001137 pexpect.TIMEOUT ], timeout=20 )
1138
andrewonlabe8e56fd2014-10-09 17:12:44 -04001139 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001140 main.log.info(
1141 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001142 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001143 return main.TRUE
1144 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001145 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001146 return main.FALSE
1147 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001148 main.log.info(
1149 "Passwordless login for host: " +
1150 str( nodeIp ) +
1151 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001152 return main.FALSE
1153 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001154 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001155 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001156
andrewonlabe8e56fd2014-10-09 17:12:44 -04001157 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001158 main.log.error( self.name + ": EOF exception found" )
1159 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001160 main.cleanup()
1161 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001162 except Exception:
1163 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001164 main.cleanup()
1165 main.exit()
1166
kelvin-onlabd3b64892015-01-20 13:26:24 -08001167 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001168 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001169 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001170 a cleaner environment.
1171
andrewonlab19fbdca2014-11-14 12:55:59 -05001172 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001173 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001174 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001175 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001176 try:
kelvin8ec71442015-01-15 16:57:00 -08001177 self.handle.sendline( "" )
1178 self.handle.expect( "\$" )
1179 self.handle.sendline( "onos-remove-raft-logs" )
1180 # Sometimes this command hangs
1181 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1182 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001183 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001184 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1185 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001186 if i == 1:
1187 return main.FALSE
shahshreya957feaa2015-03-23 16:08:29 -07001188 #self.handle.sendline( "" )
1189 #self.handle.expect( "\$" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001190 return main.TRUE
1191
1192 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001193 main.log.error( self.name + ": EOF exception found" )
1194 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001195 main.cleanup()
1196 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001197 except Exception:
1198 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001199 main.cleanup()
1200 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001201
kelvin-onlabd3b64892015-01-20 13:26:24 -08001202 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001203 """
1204 Calls the command 'onos-start-network [ <mininet-topo> ]
1205 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001206 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001207 cell."
andrewonlab94282092014-10-10 13:00:11 -04001208 * Specify mininet topology file name for mntopo
1209 * Topo files should be placed at:
1210 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001211
andrewonlab94282092014-10-10 13:00:11 -04001212 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001213 """
andrewonlab94282092014-10-10 13:00:11 -04001214 try:
1215 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001216 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001217 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001218
kelvin8ec71442015-01-15 16:57:00 -08001219 mntopo = str( mntopo )
1220 self.handle.sendline( "" )
1221 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001222
kelvin8ec71442015-01-15 16:57:00 -08001223 self.handle.sendline( "onos-start-network " + mntopo )
1224 self.handle.expect( "mininet>" )
1225 main.log.info( "Network started, entered mininet prompt" )
1226
1227 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001228
1229 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001230 main.log.error( self.name + ": EOF exception found" )
1231 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001232 main.cleanup()
1233 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001234 except Exception:
1235 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001236 main.cleanup()
1237 main.exit()
1238
Cameron Franke9c94fb02015-01-21 10:20:20 -08001239 def isup(self, node = "", timeout = 120):
kelvin8ec71442015-01-15 16:57:00 -08001240 """
1241 Run's onos-wait-for-start which only returns once ONOS is at run
Cameron Franke9c94fb02015-01-21 10:20:20 -08001242 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001243
Jon Hall7993bfc2014-10-09 16:30:14 -04001244 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001245 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001246 try:
Cameron Franke9c94fb02015-01-21 10:20:20 -08001247 self.handle.sendline("onos-wait-for-start " + node )
1248 self.handle.expect("onos-wait-for-start")
kelvin8ec71442015-01-15 16:57:00 -08001249 # NOTE: this timeout is arbitrary"
Cameron Franke9c94fb02015-01-21 10:20:20 -08001250 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001251 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001252 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001253 return main.TRUE
1254 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001255 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001256 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001257 main.log.error( "ONOS has not started yet" )
1258 self.handle.send( "\x03" ) # Control-C
1259 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001260 return main.FALSE
1261 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001262 main.log.error( self.name + ": EOF exception found" )
1263 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001264 main.cleanup()
1265 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001266 except Exception:
1267 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001268 main.cleanup()
1269 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001270
kelvin-onlabd3b64892015-01-20 13:26:24 -08001271 def pushTestIntentsShell(
1272 self,
1273 dpidSrc,
1274 dpidDst,
1275 numIntents,
1276 dirFile,
1277 onosIp,
1278 numMult="",
1279 appId="",
1280 report=True,
1281 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001282 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001283 Description:
kelvin8ec71442015-01-15 16:57:00 -08001284 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001285 better parallelize the results than the CLI
1286 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001287 * dpidSrc: specify source dpid
1288 * dpidDst: specify destination dpid
1289 * numIntents: specify number of intents to push
1290 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001291 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001292 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001293 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001294 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001295 """
1296 try:
1297 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001298 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001299 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001300 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001301 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001302 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001303
kelvin-onlabd3b64892015-01-20 13:26:24 -08001304 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1305 if not numMult:
1306 addIntents = addDpid + " " + str( numIntents )
1307 elif numMult:
1308 addIntents = addDpid + " " + str( numIntents ) + " " +\
1309 str( numMult )
1310 if appId:
1311 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001312 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001313 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001314
andrewonlabaedc8332014-12-04 12:43:03 -05001315 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001316 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001317 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001318 sendCmd = addApp + " &"
1319 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001320
kelvin-onlabd3b64892015-01-20 13:26:24 -08001321 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001322
1323 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001324 main.log.error( self.name + ": EOF exception found" )
1325 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001326 main.cleanup()
1327 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001328 except Exception:
1329 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001330 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001331 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001332
kelvin-onlabd3b64892015-01-20 13:26:24 -08001333 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001334 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001335 Definition:
1336 Loads a json topology output
1337 Return:
1338 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -08001339 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001340 import json
Jon Hall77f53ce2014-10-13 18:02:06 -04001341 try:
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001342 # either onos:topology or 'topology' will work in CLI
1343 topology = json.loads(topologyOutput)
1344 print topology
Jon Hall77f53ce2014-10-13 18:02:06 -04001345 return topology
1346 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001347 main.log.error( self.name + ": EOF exception found" )
1348 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001349 main.cleanup()
1350 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001351 except Exception:
1352 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001353 main.cleanup()
1354 main.exit()
1355
kelvin-onlabd3b64892015-01-20 13:26:24 -08001356 def checkStatus(
1357 self,
1358 topologyResult,
1359 numoswitch,
1360 numolink,
1361 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001362 """
Jon Hallefbd9792015-03-05 16:11:36 -08001363 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001364 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001365 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08001366
Jon Hall77f53ce2014-10-13 18:02:06 -04001367 Params: ip = ip used for the onos cli
1368 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001369 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001370 logLevel = level to log to.
1371 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001372
1373
kelvin-onlabd3b64892015-01-20 13:26:24 -08001374 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001375
Jon Hallefbd9792015-03-05 16:11:36 -08001376 Returns: main.TRUE if the number of switches and links are correct,
1377 main.FALSE if the number of switches and links is incorrect,
Jon Hall77f53ce2014-10-13 18:02:06 -04001378 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001379 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001380 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001381 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001382 if topology == {}:
1383 return main.ERROR
1384 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001385 # Is the number of switches is what we expected
shahshreya234a1682015-05-27 15:41:56 -07001386 devices = topology.get( 'devices', False )
1387 links = topology.get( 'links', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001388 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001389 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001390 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001391 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001392 linkCheck = ( int( links ) == int( numolink ) )
Jon Hallefbd9792015-03-05 16:11:36 -08001393 if switchCheck and linkCheck:
kelvin8ec71442015-01-15 16:57:00 -08001394 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001395 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001396 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001397 result = main.TRUE
1398 else:
1399 output = output + \
Jon Hall274b6642015-02-17 11:57:17 -08001400 "The number of links and switches does not match " + \
1401 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001402 result = main.FALSE
Jon Hallefbd9792015-03-05 16:11:36 -08001403 output = output + "\n ONOS sees %i devices" % int( devices )
1404 output = output + " (%i expected) " % int( numoswitch )
Jon Hall274b6642015-02-17 11:57:17 -08001405 output = output + "and %i links " % int( links )
1406 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001407 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001408 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001409 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001410 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001411 else:
kelvin8ec71442015-01-15 16:57:00 -08001412 main.log.info( output )
1413 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001414 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001415 main.log.error( self.name + ": EOF exception found" )
1416 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001417 main.cleanup()
1418 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001419 except Exception:
1420 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001421 main.cleanup()
1422 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001423
kelvin-onlabd3b64892015-01-20 13:26:24 -08001424 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001425 """
andrewonlab970399c2014-11-07 13:09:32 -05001426 Capture all packet activity and store in specified
1427 directory/file
1428
1429 Required:
1430 * interface: interface to capture
1431 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001432 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001433 try:
1434 self.handle.sendline( "" )
1435 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001436
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001437 self.handle.sendline( "tshark -i " + str( interface ) + " -t e -w " + str( dirFile ) + " &" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001438 self.handle.sendline( "\r" )
1439 self.handle.expect( "Capturing on" )
1440 self.handle.sendline( "\r" )
1441 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001442
Jon Hallfebb1c72015-03-05 13:30:09 -08001443 main.log.info( "Tshark started capturing files on " +
1444 str( interface ) + " and saving to directory: " +
1445 str( dirFile ) )
1446 except pexpect.EOF:
1447 main.log.error( self.name + ": EOF exception found" )
1448 main.log.error( self.name + ": " + self.handle.before )
1449 main.cleanup()
1450 main.exit()
1451 except Exception:
1452 main.log.exception( self.name + ": Uncaught exception!" )
1453 main.cleanup()
1454 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001455
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001456 def onosTopoCfg( self, onosIp, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001457 """
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001458 Description:
1459 Execute onos-topo-cfg command
1460 Required:
1461 onosIp - IP of the onos node you want to send the json to
1462 jsonFile - File path of the json file
1463 Return:
1464 Returns main.TRUE if the command is successfull; Returns
1465 main.FALSE if there was an error
kelvin8ec71442015-01-15 16:57:00 -08001466 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001467 try:
kelvin8ec71442015-01-15 16:57:00 -08001468 self.handle.sendline( "" )
1469 self.handle.expect( "\$" )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001470 cmd = "onos-topo-cfg "
1471 self.handle.sendline( cmd + str( onosIp ) + " " + jsonFile )
1472 handle = self.handle.before
1473 print handle
1474 if "Error" in handle:
1475 main.log.error( self.name + ": " + self.handle.before )
1476 return main.FALSE
1477 else:
1478 self.handle.expect( "\$" )
1479 return main.TRUE
1480
Jon Hallfebb1c72015-03-05 13:30:09 -08001481 except pexpect.EOF:
1482 main.log.error( self.name + ": EOF exception found" )
1483 main.log.error( self.name + ": " + self.handle.before )
1484 main.cleanup()
1485 main.exit()
1486 except Exception:
1487 main.log.exception( self.name + ": Uncaught exception!" )
1488 main.cleanup()
1489 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001490
jenkins1e99e7b2015-04-02 18:15:39 -07001491 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001492 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001493 Required:
kelvin8ec71442015-01-15 16:57:00 -08001494 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001495 * directory to store results
1496 Optional:
1497 * interface - default: eth0
Jon Hall4ba53f02015-07-29 13:07:41 -07001498 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001499 Description:
1500 Uses tshark command to grep specific group of packets
1501 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001502 The timestamp is hardcoded to be in epoch
1503 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001504 try:
1505 self.handle.sendline( "" )
1506 self.handle.expect( "\$" )
1507 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001508 if grepOptions:
1509 grepStr = "grep "+str(grepOptions)
1510 else:
1511 grepStr = "grep"
Jon Hall4ba53f02015-07-29 13:07:41 -07001512
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001513 cmd = (
1514 "sudo tshark -i " +
Jon Hallfebb1c72015-03-05 13:30:09 -08001515 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001516 " -t e | " +
1517 grepStr + " --line-buffered \"" +
Jon Hallfebb1c72015-03-05 13:30:09 -08001518 str(grep) +
1519 "\" >" +
1520 directory +
1521 " &" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001522 self.handle.sendline(cmd)
1523 main.log.info(cmd)
Jon Hallfebb1c72015-03-05 13:30:09 -08001524 self.handle.expect( "Capturing on" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001525 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001526 self.handle.expect( "\$" )
1527 except pexpect.EOF:
1528 main.log.error( self.name + ": EOF exception found" )
1529 main.log.error( self.name + ": " + self.handle.before )
1530 main.cleanup()
1531 main.exit()
1532 except Exception:
1533 main.log.exception( self.name + ": Uncaught exception!" )
1534 main.cleanup()
1535 main.exit()
1536
kelvin-onlabd3b64892015-01-20 13:26:24 -08001537 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001538 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001539 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001540 """
1541 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001542 try:
1543 self.execute( cmd="sudo rm /tmp/wireshark*" )
1544 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001545 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1546 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001547 self.handle.sendline( "" )
1548 main.log.info( "Tshark stopped" )
1549 except pexpect.EOF:
1550 main.log.error( self.name + ": EOF exception found" )
1551 main.log.error( self.name + ": " + self.handle.before )
1552 main.cleanup()
1553 main.exit()
1554 except Exception:
1555 main.log.exception( self.name + ": Uncaught exception!" )
1556 main.cleanup()
1557 main.exit()
1558
kelvin8ec71442015-01-15 16:57:00 -08001559 def ptpd( self, args ):
1560 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001561 Initiate ptp with user-specified args.
1562 Required:
1563 * args: specify string of args after command
1564 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001565 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001566 try:
kelvin8ec71442015-01-15 16:57:00 -08001567 self.handle.sendline( "sudo ptpd " + str( args ) )
1568 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001569 "Multiple",
1570 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001571 "\$" ] )
1572 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001573
andrewonlab0c38a4a2014-10-28 18:35:35 -04001574 if i == 0:
1575 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001576 main.log.info( "ptpd returned an error: " +
1577 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001578 return handle
1579 elif i == 1:
1580 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001581 main.log.error( "ptpd returned an error: " +
1582 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001583 return handle
1584 else:
1585 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001586
andrewonlab0c38a4a2014-10-28 18:35:35 -04001587 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001588 main.log.error( self.name + ": EOF exception found" )
1589 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001590 main.cleanup()
1591 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001592 except Exception:
1593 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001594 main.cleanup()
1595 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001596
kelvin-onlabd3b64892015-01-20 13:26:24 -08001597 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001598 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001599 """
1600 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001601 Current implementation of ONOS deletes its karaf
1602 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001603 you may want to use this function to capture
1604 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001605 Localtime will be attached to the filename
1606
1607 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001608 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001609 copy.
kelvin8ec71442015-01-15 16:57:00 -08001610 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001611 For copying multiple files, leave copyFileName
1612 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001613 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001614 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001615 ex ) /tmp/
1616 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001617 * copyFileName: If you want to rename the log
1618 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001619 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001620 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001621 try:
kelvin8ec71442015-01-15 16:57:00 -08001622 localtime = time.strftime( '%x %X' )
1623 localtime = localtime.replace( "/", "" )
1624 localtime = localtime.replace( " ", "_" )
1625 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001626 if destDir[ -1: ] != "/":
1627 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001628
kelvin-onlabd3b64892015-01-20 13:26:24 -08001629 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001630 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1631 str( destDir ) + str( copyFileName ) +
1632 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001633 self.handle.expect( "cp" )
1634 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001635 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001636 self.handle.sendline( "cp " + str( logToCopy ) +
1637 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001638 self.handle.expect( "cp" )
1639 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001640
kelvin8ec71442015-01-15 16:57:00 -08001641 return self.handle.before
1642
1643 except pexpect.EOF:
1644 main.log.error( "Copying files failed" )
1645 main.log.error( self.name + ": EOF exception found" )
1646 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001647 except Exception:
1648 main.log.exception( "Copying files failed" )
1649
Jon Hall16b72c42015-05-20 10:23:36 -07001650 def checkLogs( self, onosIp, restart=False):
kelvin8ec71442015-01-15 16:57:00 -08001651 """
Jon Hall94fd0472014-12-08 11:52:42 -08001652 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001653 If restart is True, use the old version of onos-check-logs which
1654 does not print the full stacktrace, but shows the entire log file,
1655 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001656 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001657 """
Jon Hall94fd0472014-12-08 11:52:42 -08001658 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001659 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001660 if restart:
1661 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001662 self.handle.sendline( cmd )
1663 self.handle.expect( cmd )
1664 self.handle.expect( "\$" )
Jon Hall94fd0472014-12-08 11:52:42 -08001665 response = self.handle.before
1666 return response
1667 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001668 main.log.error( "Lost ssh connection" )
1669 main.log.error( self.name + ": EOF exception found" )
1670 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001671 except Exception:
1672 main.log.exception( self.name + ": Uncaught exception!" )
1673 main.cleanup()
1674 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001675
kelvin-onlabd3b64892015-01-20 13:26:24 -08001676 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001677 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001678 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001679 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001680 try:
kelvin8ec71442015-01-15 16:57:00 -08001681 self.handle.sendline( "" )
1682 self.handle.expect( "\$" )
1683 self.handle.sendline( "onos-service " + str( node ) +
1684 " status" )
1685 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001686 "start/running",
1687 "stop/waiting",
kelvin8ec71442015-01-15 16:57:00 -08001688 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001689
1690 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001691 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001692 return main.TRUE
1693 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001694 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001695 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001696 main.cleanup()
1697 main.exit()
1698 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001699 main.log.error( self.name + ": EOF exception found" )
1700 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001701 main.cleanup()
1702 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001703 except Exception:
1704 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001705 main.cleanup()
1706 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001707
Jon Hall63604932015-02-26 17:09:50 -08001708 def setIpTables( self, ip, port='', action='add', packet_type='',
1709 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001710 """
Jon Hall21270ac2015-02-16 17:59:55 -08001711 Description:
1712 add or remove iptables rule to DROP (default) packets from
1713 specific IP and PORT
1714 Usage:
1715 * specify action ('add' or 'remove')
1716 when removing, pass in the same argument as you would add. It will
1717 delete that specific rule.
1718 * specify the ip to block
1719 * specify the destination port to block (defaults to all ports)
1720 * optional packet type to block (default tcp)
1721 * optional iptables rule (default DROP)
1722 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001723 * States boolean toggles adding all supported tcp states to the
1724 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001725 Returns:
1726 main.TRUE on success or
1727 main.FALSE if given invalid input or
1728 main.ERROR if there is an error in response from iptables
1729 WARNING:
1730 * This function uses root privilege iptables command which may result
1731 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001732 """
Jon Hall21270ac2015-02-16 17:59:55 -08001733 import time
1734
1735 # NOTE*********
1736 # The strict checking methods of this driver function is intentional
1737 # to discourage any misuse or error of iptables, which can cause
1738 # severe network errors
1739 # *************
1740
1741 # NOTE: Sleep needed to give some time for rule to be added and
1742 # registered to the instance. If you are calling this function
1743 # multiple times this sleep will prevent any errors.
1744 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001745 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001746 try:
1747 # input validation
1748 action_type = action.lower()
1749 rule = rule.upper()
1750 direction = direction.upper()
1751 if action_type != 'add' and action_type != 'remove':
1752 main.log.error( "Invalid action type. Use 'add' or "
1753 "'remove' table rule" )
1754 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1755 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1756 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1757 "'ACCEPT' or 'LOG' only." )
1758 if direction != 'INPUT' and direction != 'OUTPUT':
1759 # NOTE currently only supports rules INPUT and OUPTUT
1760 main.log.error( "Invalid rule. Valid directions are"
1761 " 'OUTPUT' or 'INPUT'" )
1762 return main.FALSE
1763 return main.FALSE
1764 return main.FALSE
1765 if action_type == 'add':
1766 # -A is the 'append' action of iptables
1767 actionFlag = '-A'
1768 elif action_type == 'remove':
1769 # -D is the 'delete' rule of iptables
1770 actionFlag = '-D'
1771 self.handle.sendline( "" )
1772 self.handle.expect( "\$" )
1773 cmd = "sudo iptables " + actionFlag + " " +\
1774 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001775 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001776 # " -p " + str( packet_type ) +\
1777 if packet_type:
1778 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001779 if port:
1780 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001781 if states:
1782 cmd += " -m state --state="
1783 #FIXME- Allow user to configure which states to block
1784 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001785 cmd += " -j " + str( rule )
1786
1787 self.handle.sendline( cmd )
1788 self.handle.expect( "\$" )
1789 main.log.warn( self.handle.before )
1790
1791 info_string = "On " + str( self.name )
1792 info_string += " " + str( action_type )
1793 info_string += " iptable rule [ "
1794 info_string += " IP: " + str( ip )
1795 info_string += " Port: " + str( port )
1796 info_string += " Rule: " + str( rule )
1797 info_string += " Direction: " + str( direction ) + " ]"
1798 main.log.info( info_string )
1799 return main.TRUE
1800 except pexpect.TIMEOUT:
1801 main.log.exception( self.name + ": Timeout exception in "
1802 "setIpTables function" )
1803 return main.ERROR
1804 except pexpect.EOF:
1805 main.log.error( self.name + ": EOF exception found" )
1806 main.log.error( self.name + ": " + self.handle.before )
1807 main.cleanup()
1808 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001809 except Exception:
1810 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001811 main.cleanup()
1812 main.exit()
1813
Jon Hall0468b042015-02-19 19:08:21 -08001814 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001815 """
Jon Hall0468b042015-02-19 19:08:21 -08001816 This method is used by STS to check the status of the controller
1817 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001818 """
Jon Hall0468b042015-02-19 19:08:21 -08001819 import re
1820 try:
1821 self.handle.sendline( "" )
1822 self.handle.expect( "\$" )
1823 self.handle.sendline( "cd " + self.home )
1824 self.handle.expect( "\$" )
1825 self.handle.sendline( "service onos status" )
1826 self.handle.expect( "\$" )
1827 response = self.handle.before
1828 if re.search( "onos start/running", response ):
1829 # onos start/running, process 10457
1830 return 'RUNNING'
1831 # FIXME: Implement this case
1832 # elif re.search( pattern, response ):
1833 # return 'STARTING'
1834 elif re.search( "onos stop/", response ):
1835 # onos stop/waiting
1836 # FIXME handle this differently?: onos stop/pre-stop
1837 return 'STOPPED'
1838 # FIXME: Implement this case
1839 # elif re.search( pattern, response ):
1840 # return 'FROZEN'
1841 else:
1842 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001843 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001844 main.log.warn( response )
1845 return 'ERROR', "Unknown response: %s" % response
1846 except pexpect.TIMEOUT:
1847 main.log.exception( self.name + ": Timeout exception in "
1848 "setIpTables function" )
1849 return 'ERROR', "Pexpect Timeout"
1850 except pexpect.EOF:
1851 main.log.error( self.name + ": EOF exception found" )
1852 main.log.error( self.name + ": " + self.handle.before )
1853 main.cleanup()
1854 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001855 except Exception:
1856 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001857 main.cleanup()
1858 main.exit()
1859
andrew@onlab.us3b087132015-03-11 15:00:08 -07001860 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1861 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001862 Create/formats the LinkGraph.cfg file based on arguments
1863 -only creates a linear topology and connects islands
1864 -evenly distributes devices
andrew@onlab.us3b087132015-03-11 15:00:08 -07001865 -must be called by ONOSbench
1866
Jon Hall4ba53f02015-07-29 13:07:41 -07001867 ONOSIpList - list of all of the node IPs to be used
1868
1869 deviceCount - number of switches to be assigned
andrew@onlab.us3b087132015-03-11 15:00:08 -07001870 '''
1871 main.log.step("Creating link graph configuration file." )
1872 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
Jon Hall4ba53f02015-07-29 13:07:41 -07001873 tempFile = "/tmp/linkGraph.cfg"
andrew@onlab.us3b087132015-03-11 15:00:08 -07001874
1875 linkGraph = open(tempFile, 'w+')
1876 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1877 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1878 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001879
andrew@onlab.us3b087132015-03-11 15:00:08 -07001880 clusterCount = len(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07001881
1882 if type(deviceCount) is int or type(deviceCount) is str:
andrew@onlab.us3b087132015-03-11 15:00:08 -07001883 deviceCount = int(deviceCount)
1884 switchList = [0]*(clusterCount+1)
1885 baselineSwitchCount = deviceCount/clusterCount
Jon Hall4ba53f02015-07-29 13:07:41 -07001886
andrew@onlab.us3b087132015-03-11 15:00:08 -07001887 for node in range(1, clusterCount + 1):
1888 switchList[node] = baselineSwitchCount
1889
1890 for node in range(1, (deviceCount%clusterCount)+1):
1891 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001892
andrew@onlab.us3b087132015-03-11 15:00:08 -07001893 if type(deviceCount) is list:
1894 main.log.info("Using provided device distribution")
1895 switchList = [0]
1896 for i in deviceCount:
1897 switchList.append(int(i))
1898
1899 tempList = ['0']
1900 tempList.extend(ONOSIpList)
1901 ONOSIpList = tempList
1902
1903 myPort = 6
1904 lastSwitch = 0
1905 for node in range(1, clusterCount+1):
1906 if switchList[node] == 0:
1907 continue
1908
1909 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001910
andrew@onlab.us3b087132015-03-11 15:00:08 -07001911 if node > 1:
1912 #connect to last device on previous node
Jon Hall4ba53f02015-07-29 13:07:41 -07001913 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
1914 linkGraph.write(line)
1915
1916 lastSwitch = 0
1917 for switch in range (0, switchList[node]-1):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001918 line = ""
1919 line = ("\t" + str(switch) + ":" + str(myPort))
1920 line += " -- "
1921 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
1922 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07001923 lastSwitch = switch+1
andrew@onlab.us3b087132015-03-11 15:00:08 -07001924 lastIp = ONOSIpList[node]
Jon Hall4ba53f02015-07-29 13:07:41 -07001925
andrew@onlab.us3b087132015-03-11 15:00:08 -07001926 #lastSwitch += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001927 if node < (clusterCount):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001928 #connect to first device on the next node
Jon Hall4ba53f02015-07-29 13:07:41 -07001929 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
andrew@onlab.us3b087132015-03-11 15:00:08 -07001930 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07001931
andrew@onlab.us3b087132015-03-11 15:00:08 -07001932 linkGraph.write("}\n")
1933 linkGraph.close()
1934
1935 #SCP
Jon Hall4ba53f02015-07-29 13:07:41 -07001936 os.system( "scp " + tempFile + " " + self.user_name + "@" + benchIp + ":" + linkGraphPath)
andrew@onlab.us3b087132015-03-11 15:00:08 -07001937 main.log.info("linkGraph.cfg creation complete")
1938
cameron@onlab.us75900962015-03-30 13:22:49 -07001939 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
Jon Hall4ba53f02015-07-29 13:07:41 -07001940
andrew@onlab.us3b087132015-03-11 15:00:08 -07001941 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001942 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
1943 deviceCount = number of switches to distribute, or list of values to use as custom distribution
cameron@onlab.us75900962015-03-30 13:22:49 -07001944 numPorts = number of ports per device. Defaults to 10 both in this function and in ONOS. Optional arg
andrew@onlab.us3b087132015-03-11 15:00:08 -07001945 '''
1946
cameron@onlab.us75900962015-03-30 13:22:49 -07001947 main.log.step("Configuring Null Device Provider" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001948 clusterCount = len(ONOSIpList)
1949
Jon Hall4ba53f02015-07-29 13:07:41 -07001950 try:
1951
cameron@onlab.us75900962015-03-30 13:22:49 -07001952 if type(deviceCount) is int or type(deviceCount) is str:
1953 main.log.step("Creating device distribution")
1954 deviceCount = int(deviceCount)
1955 switchList = [0]*(clusterCount+1)
1956 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001957
cameron@onlab.us75900962015-03-30 13:22:49 -07001958 for node in range(1, clusterCount + 1):
1959 switchList[node] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001960
cameron@onlab.us75900962015-03-30 13:22:49 -07001961 for node in range(1, (deviceCount%clusterCount)+1):
1962 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001963
1964 if type(deviceCount) is list:
1965 main.log.info("Using provided device distribution")
1966
1967 if len(deviceCount) == clusterCount:
cameron@onlab.us75900962015-03-30 13:22:49 -07001968 switchList = ['0']
1969 switchList.extend(deviceCount)
Jon Hall4ba53f02015-07-29 13:07:41 -07001970
1971 if len(deviceCount) == (clusterCount + 1):
1972 if deviceCount[0] == '0' or deviceCount[0] == 0:
cameron@onlab.us75900962015-03-30 13:22:49 -07001973 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001974
cameron@onlab.us75900962015-03-30 13:22:49 -07001975 assert len(switchList) == (clusterCount + 1)
Jon Hall4ba53f02015-07-29 13:07:41 -07001976
cameron@onlab.us75900962015-03-30 13:22:49 -07001977 except AssertionError:
Jon Hall4ba53f02015-07-29 13:07:41 -07001978 main.log.error( "Bad device/Ip list match")
cameron@onlab.us75900962015-03-30 13:22:49 -07001979 except TypeError:
1980 main.log.exception( self.name + ": Object not as expected" )
1981 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07001982 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07001983 main.log.exception( self.name + ": Uncaught exception!" )
1984 main.cleanup()
1985 main.exit()
1986
andrew@onlab.us3b087132015-03-11 15:00:08 -07001987
1988 ONOSIp = [0]
1989 ONOSIp.extend(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07001990
andrew@onlab.us3b087132015-03-11 15:00:08 -07001991 devicesString = "devConfigs = "
1992 for node in range(1, len(ONOSIp)):
1993 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
1994 if node < clusterCount:
1995 devicesString += (",")
Jon Hall4ba53f02015-07-29 13:07:41 -07001996
1997 try:
cameron@onlab.us75900962015-03-30 13:22:49 -07001998 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
1999 self.handle.expect(":~")
2000 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
2001 self.handle.expect(":~")
andrew@onlab.us3b087132015-03-11 15:00:08 -07002002
cameron@onlab.us75900962015-03-30 13:22:49 -07002003 for i in range(10):
2004 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
2005 self.handle.expect(":~")
2006 verification = self.handle.before
2007 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
2008 break
2009 else:
2010 time.sleep(1)
andrew@onlab.us3b087132015-03-11 15:00:08 -07002011
cameron@onlab.us2cc8bf12015-04-02 14:12:30 -07002012 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002013
cameron@onlab.us75900962015-03-30 13:22:49 -07002014 except AssertionError:
2015 main.log.error("Incorrect Config settings: " + verification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002016 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002017 main.log.exception( self.name + ": Uncaught exception!" )
2018 main.cleanup()
2019 main.exit()
2020
Jon Hall4ba53f02015-07-29 13:07:41 -07002021 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002022 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002023 fileName default is currently the same as the default on ONOS, specify alternate file if
cameron@onlab.us75900962015-03-30 13:22:49 -07002024 you want to use a different topology file than linkGraph.cfg
andrew@onlab.us3b087132015-03-11 15:00:08 -07002025 '''
2026
Jon Hall4ba53f02015-07-29 13:07:41 -07002027
2028 try:
2029 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
2030 self.handle.expect(":~")
cameron@onlab.us75900962015-03-30 13:22:49 -07002031 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2032 self.handle.expect(":~")
Jon Hall4ba53f02015-07-29 13:07:41 -07002033
2034 for i in range(10):
2035 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
cameron@onlab.us75900962015-03-30 13:22:49 -07002036 self.handle.expect(":~")
2037 verification = self.handle.before
Jon Hall4ba53f02015-07-29 13:07:41 -07002038 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002039 break
Jon Hall4ba53f02015-07-29 13:07:41 -07002040 else:
cameron@onlab.us75900962015-03-30 13:22:49 -07002041 time.sleep(1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002042
cameron@onlab.us75900962015-03-30 13:22:49 -07002043 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002044
cameron@onlab.us75900962015-03-30 13:22:49 -07002045 except pexpect.EOF:
2046 main.log.error( self.name + ": EOF exception found" )
2047 main.log.error( self.name + ": " + self.handle.before )
2048 main.cleanup()
2049 main.exit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002050 except AssertionError:
2051 main.log.info("Settings did not post to ONOS")
Jon Hall4ba53f02015-07-29 13:07:41 -07002052 main.log.error(varification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002053 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002054 main.log.exception( self.name + ": Uncaught exception!" )
2055 main.log.error(varification)
2056 main.cleanup()
2057 main.exit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002058
kelvin-onlaba4074292015-07-09 15:19:49 -07002059 def getOnosIps( self ):
2060 """
2061 Get all onos IPs stored in
2062 """
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002063
kelvin-onlaba4074292015-07-09 15:19:49 -07002064 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002065
kelvin-onlaba4074292015-07-09 15:19:49 -07002066 def logReport( self, nodeIp, searchTerms, outputMode="s" ):
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002067 '''
2068 - accepts either a list or a string for "searchTerms" these
Jon Hall4ba53f02015-07-29 13:07:41 -07002069 terms will be searched for in the log and have their
2070 instances counted
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002071
Jon Hall4ba53f02015-07-29 13:07:41 -07002072 - nodeIp is the ip of the node whos log is to be scanned
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002073
Jon Hall4ba53f02015-07-29 13:07:41 -07002074 - output modes:
2075 "s" - Simple. Quiet output mode that just prints
2076 the occurences of each search term
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002077
cameron@onlab.us2e166212015-05-19 14:28:25 -07002078 "d" - Detailed. Prints number of occurences as well as the entire
Jon Hall4ba53f02015-07-29 13:07:41 -07002079 line for each of the last 5 occurences
cameron@onlab.us2e166212015-05-19 14:28:25 -07002080
2081 - returns total of the number of instances of all search terms
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002082 '''
2083 main.log.info("========================== Log Report ===========================\n")
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002084
Jon Hall4ba53f02015-07-29 13:07:41 -07002085 if type(searchTerms) is str:
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002086 searchTerms = [searchTerms]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002087
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002088 logLines = [ [" "] for i in range(len(searchTerms)) ]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002089
Jon Hall4ba53f02015-07-29 13:07:41 -07002090 for term in range(len(searchTerms)):
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002091 logLines[term][0] = searchTerms[term]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002092
Jon Hall4ba53f02015-07-29 13:07:41 -07002093 totalHits = 0
2094 for term in range(len(searchTerms)):
2095 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep " + searchTerms[term]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002096 self.handle.sendline(cmd)
2097 self.handle.expect(":~")
2098 before = (self.handle.before).splitlines()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002099
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002100 count = [searchTerms[term],0]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002101
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002102 for line in before:
2103 if searchTerms[term] in line and "grep" not in line:
Jon Hall4ba53f02015-07-29 13:07:41 -07002104 count[1] += 1
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002105 if before.index(line) > ( len(before) - 7 ):
2106 logLines[term].append(line)
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002107
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002108 main.log.info( str(count[0]) + ": " + str(count[1]) )
Jon Hall4ba53f02015-07-29 13:07:41 -07002109 if term == len(searchTerms)-1:
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002110 print("\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07002111 totalHits += int(count[1])
cameron@onlab.us2e166212015-05-19 14:28:25 -07002112
Jon Hall4ba53f02015-07-29 13:07:41 -07002113 if outputMode != "s" and outputMode != "S":
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002114 outputString = ""
2115 for i in logLines:
Jon Hall4ba53f02015-07-29 13:07:41 -07002116 outputString = i[0] + ": \n"
2117 for x in range(1,len(i)):
2118 outputString += ( i[x] + "\n" )
2119
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002120 if outputString != (i[0] + ": \n"):
Jon Hall4ba53f02015-07-29 13:07:41 -07002121 main.log.info(outputString)
2122
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002123 main.log.info("================================================================\n")
Hari Krishnabe4b97b2015-07-15 12:19:43 -07002124 return totalHits
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002125
2126 def copyMininetFile( self, fileName, localPath, userName, ip,
2127 mnPath='~/mininet/custom/', timeout = 60 ):
2128 """
2129 Description:
2130 Copy mininet topology file from dependency folder in the test folder
2131 and paste it to the mininet machine's mininet/custom folder
2132 Required:
2133 fileName - Name of the topology file to copy
2134 localPath - File path of the mininet topology file
2135 userName - User name of the mininet machine to send the file to
2136 ip - Ip address of the mininet machine
2137 Optional:
2138 mnPath - of the mininet directory to send the file to
2139 Return:
2140 Return main.TRUE if successfully copied the file otherwise
2141 return main.FALSE
2142 """
2143
2144 try:
2145 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2146 str( ip ) + ":" + mnPath + fileName
2147
2148 self.handle.sendline( "" )
2149 self.handle.expect( "\$" )
2150
2151 main.log.info( self.name + ": Execute: " + cmd )
2152
2153 self.handle.sendline( cmd )
2154
2155 i = self.handle.expect( [ 'No such file',
2156 "100%",
2157 pexpect.TIMEOUT ] )
2158
2159 if i == 0:
2160 main.log.error( self.name + ": File " + fileName +
2161 " does not exist!" )
2162 return main.FALSE
2163
2164 if i == 1:
2165 main.log.info( self.name + ": File " + fileName +
2166 " has been copied!" )
2167 self.handle.sendline( "" )
2168 self.handle.expect( "\$" )
2169 return main.TRUE
2170
2171 except pexpect.EOF:
2172 main.log.error( self.name + ": EOF exception found" )
2173 main.log.error( self.name + ": " + self.handle.before )
2174 main.cleanup()
2175 main.exit()
2176 except pexpect.TIMEOUT:
2177 main.log.error( self.name + ": TIMEOUT exception found" )
2178 main.log.error( self.name + ": " + self.handle.before )
2179 main.cleanup()
2180 main.exit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002181
2182 def jvmSet(self, memory=8):
Jon Hall4ba53f02015-07-29 13:07:41 -07002183
cameron@onlab.us78b89652015-07-08 15:21:03 -07002184 import os
2185
2186 homeDir = os.path.expanduser('~')
2187 filename = "/onos/tools/package/bin/onos-service"
2188
2189 serviceConfig = open(homeDir + filename, 'w+')
2190 serviceConfig.write("#!/bin/bash\n ")
2191 serviceConfig.write("#------------------------------------- \n ")
2192 serviceConfig.write("# Starts ONOS Apache Karaf container\n ")
2193 serviceConfig.write("#------------------------------------- \n ")
2194 serviceConfig.write("#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n ")
2195 serviceConfig.write("""export JAVA_OPTS="${JAVA_OPTS:--Xms""" + str(memory) + "G -Xmx" + str(memory) + """G}" \n """)
2196 serviceConfig.write("[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n")
2197 serviceConfig.write("""${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """)
2198 serviceConfig.close()
2199
2200 def createDBFile(self, testData):
Jon Hall4ba53f02015-07-29 13:07:41 -07002201
cameron@onlab.us78b89652015-07-08 15:21:03 -07002202 filename = main.TEST + "DB"
2203 DBString = ""
Jon Hall4ba53f02015-07-29 13:07:41 -07002204
cameron@onlab.us78b89652015-07-08 15:21:03 -07002205 for item in testData:
Jon Hall4ba53f02015-07-29 13:07:41 -07002206 if type(item) is string:
cameron@onlab.us78b89652015-07-08 15:21:03 -07002207 item = "'" + item + "'"
2208 if testData.index(item) < len(testData-1):
2209 item += ","
Jon Hall4ba53f02015-07-29 13:07:41 -07002210 DBString += str(item)
cameron@onlab.us78b89652015-07-08 15:21:03 -07002211
2212 DBFile = open(filename, "a")
2213 DBFile.write(DBString)
2214 DBFile.close()
2215
Jon Hall4ba53f02015-07-29 13:07:41 -07002216 def verifySummary(self, ONOSIp,*deviceCount):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002217
2218 self.handle.sendline("onos " + ONOSIp + " summary")
2219 self.handle.expect(":~")
2220
2221 summaryStr = self.handle.before
2222 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2223
2224 #passed = "SCC(s)=1" in summaryStr
2225 #if deviceCount:
2226 # passed = "devices=" + str(deviceCount) + "," not in summaryStr
2227
GlennRC772363b2015-08-25 13:05:57 -07002228 passed = False
cameron@onlab.us78b89652015-07-08 15:21:03 -07002229 if "SCC(s)=1," in summaryStr:
2230 passed = True
2231 print("Summary is verifed")
Jon Hall4ba53f02015-07-29 13:07:41 -07002232 else:
2233 print("Summary failed")
cameron@onlab.us78b89652015-07-08 15:21:03 -07002234
2235 if deviceCount:
2236 print" ============================="
2237 checkStr = "devices=" + str(deviceCount[0]) + ","
2238 print "Checkstr: " + checkStr
2239 if checkStr not in summaryStr:
2240 passed = False
Jon Hall4ba53f02015-07-29 13:07:41 -07002241 print("Device count failed")
2242 else:
2243 print "device count verified"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002244
2245 return passed