blob: 13ba642dde6074b3b5da1e232a782fb8d516da83 [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
GlennRC9cc3dd92015-12-10 12:21:25 -0800186 def onosPackage( self, opTimeout=120 ):
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
Jon Hall64af8502015-12-15 10:09:33 -0800189 and executed on any platform with Java 8 JRE.
kelvin8ec71442015-01-15 16:57:00 -0800190 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400191 try:
Jon Hall64af8502015-12-15 10:09:33 -0800192 ret = main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800193 self.handle.sendline( "onos-package" )
194 self.handle.expect( "onos-package" )
Jon Hallc6793552016-01-19 14:18:37 -0800195 i = self.handle.expect( [ "Downloading",
196 "tar.gz",
197 "\$",
198 "Unknown options" ],
199 opTimeout )
Jon Hall64af8502015-12-15 10:09:33 -0800200 handle = str( self.handle.before + self.handle.after )
201 if i == 0:
Jon Hallc6793552016-01-19 14:18:37 -0800202 # Give more time to download the file
203 self.handle.expect( "\$", opTimeout * 2 )
Jon Hall64af8502015-12-15 10:09:33 -0800204 handle += str( self.handle.before )
205 elif i == 1:
Jon Hallc6793552016-01-19 14:18:37 -0800206 self.handle.expect( "\$" )
207 handle += str( self.handle.before )
208 elif i == 2:
Jon Hall64af8502015-12-15 10:09:33 -0800209 # This seems to be harmless, but may be a problem
210 main.log.warn( "onos-package output not as expected" )
Jon Hallc6793552016-01-19 14:18:37 -0800211 elif i == 3:
Jon Hall64af8502015-12-15 10:09:33 -0800212 # Incorrect usage
213 main.log.error( "onos-package does not recognize the given options" )
214 self.handle.expect( "\$" )
215 handle += str( self.handle.before )
216 ret = main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -0800217 main.log.info( "onos-package command returned: " + handle )
kelvin8ec71442015-01-15 16:57:00 -0800218 # As long as the sendline does not time out,
219 # return true. However, be careful to interpret
220 # the results of the onos-package command return
Jon Hall64af8502015-12-15 10:09:33 -0800221 return ret
222 except pexpect.TIMEOUT:
223 main.log.exception( self.name + ": TIMEOUT exception found in onosPackage" )
224 main.log.error( self.name + ": " + self.handle.before )
225 return main.FALSE
andrewonlab7735d852014-10-09 13:02:47 -0400226 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800227 main.log.error( self.name + ": EOF exception found" )
228 main.log.error( self.name + ": " + self.handle.before )
Jon Hall64af8502015-12-15 10:09:33 -0800229 main.cleanup()
230 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800231 except Exception:
232 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400233 main.cleanup()
234 main.exit()
235
kelvin-onlabd3b64892015-01-20 13:26:24 -0800236 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800237 """
andrewonlab8790abb2014-11-06 13:51:54 -0500238 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800239 """
andrewonlab8790abb2014-11-06 13:51:54 -0500240 try:
kelvin8ec71442015-01-15 16:57:00 -0800241 self.handle.sendline( "onos-build" )
242 self.handle.expect( "onos-build" )
243 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800244 "BUILD SUCCESS",
245 "ERROR",
246 "BUILD FAILED" ],
247 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800248 handle = str( self.handle.before )
Jon Hall3b489db2015-10-05 14:38:37 -0700249 self.handle.expect( "\$" )
andrewonlab8790abb2014-11-06 13:51:54 -0500250
kelvin8ec71442015-01-15 16:57:00 -0800251 main.log.info( "onos-build command returned: " +
252 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500253
254 if i == 0:
255 return main.TRUE
256 else:
257 return handle
258
259 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800260 main.log.error( self.name + ": EOF exception found" )
261 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800262 except Exception:
263 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500264 main.cleanup()
265 main.exit()
266
shahshreya9f531fe2015-06-10 12:03:51 -0700267 def cleanInstall( self, skipTest=False, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800268 """
269 Runs mvn clean install in the root of the ONOS directory.
270 This will clean all ONOS artifacts then compile each module
shahshreya9f531fe2015-06-10 12:03:51 -0700271 Optional:
272 skipTest - Does "-DskipTests -Dcheckstyle.skip -U -T 1C" which
273 skip the test. This will make the building faster.
274 Disregarding the credibility of the build
kelvin8ec71442015-01-15 16:57:00 -0800275 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400276 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800277 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400278 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800279 main.log.info( "Running 'mvn clean install' on " +
280 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800281 ". This may take some time." )
282 self.handle.sendline( "cd " + self.home )
283 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400284
kelvin8ec71442015-01-15 16:57:00 -0800285 self.handle.sendline( "" )
286 self.handle.expect( "\$" )
shahshreya9f531fe2015-06-10 12:03:51 -0700287
288 if not skipTest:
289 self.handle.sendline( "mvn clean install" )
290 self.handle.expect( "mvn clean install" )
291 else:
292 self.handle.sendline( "mvn clean install -DskipTests" +
293 " -Dcheckstyle.skip -U -T 1C" )
294 self.handle.expect( "mvn clean install -DskipTests" +
295 " -Dcheckstyle.skip -U -T 1C" )
kelvin8ec71442015-01-15 16:57:00 -0800296 while True:
297 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800298 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800299 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400300 'BUILD\sFAILURE',
301 'BUILD\sSUCCESS',
Jon Halle94919c2015-03-23 11:42:57 -0700302 'onos\$', #TODO: fix this to be more generic?
Jon Hallde9d9aa2014-10-08 20:36:02 -0400303 'ONOS\$',
pingping-lin57a56ce2015-05-20 16:43:48 -0700304 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400305 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800306 main.log.error( self.name + ":There is insufficient memory \
307 for the Java Runtime Environment to continue." )
308 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400309 main.cleanup()
310 main.exit()
311 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800312 main.log.error( self.name + ": Build failure!" )
313 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400314 main.cleanup()
315 main.exit()
316 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800317 main.log.info( self.name + ": Build success!" )
Jon Halle94919c2015-03-23 11:42:57 -0700318 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800319 main.log.info( self.name + ": Build complete" )
320 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400321 for line in self.handle.before.splitlines():
322 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800323 main.log.info( line )
324 self.handle.sendline( "" )
325 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400326 return main.TRUE
Jon Halle94919c2015-03-23 11:42:57 -0700327 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800328 main.log.error(
329 self.name +
330 ": mvn clean install TIMEOUT!" )
331 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400332 main.cleanup()
333 main.exit()
334 else:
Jon Hall274b6642015-02-17 11:57:17 -0800335 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800336 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800337 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400338 main.cleanup()
339 main.exit()
340 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800341 main.log.error( self.name + ": EOF exception found" )
342 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400343 main.cleanup()
344 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800345 except Exception:
346 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400347 main.cleanup()
348 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400349
Jon Hall61282e32015-03-19 11:34:11 -0700350 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800351 """
Jon Hallacabffd2014-10-09 12:36:53 -0400352 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800353
Jon Hall61282e32015-03-19 11:34:11 -0700354 If the fastForward boolean is set to true, only git pulls that can
355 be fast forwarded will be performed. IE if you have not local commits
356 in your branch.
357
Jon Hallacabffd2014-10-09 12:36:53 -0400358 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800359 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400360 for the purpose of pulling from other nodes if necessary.
361
Jon Hall47a93fb2015-01-06 16:46:06 -0800362 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400363 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800364 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400365 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400366
kelvin8ec71442015-01-15 16:57:00 -0800367 """
Jon Hallacabffd2014-10-09 12:36:53 -0400368 try:
kelvin8ec71442015-01-15 16:57:00 -0800369 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800370 self.handle.expect( self.home + "\$" )
Jon Hall61282e32015-03-19 11:34:11 -0700371 cmd = "git pull"
372 if comp1 != "":
373 cmd += ' ' + comp1
374 if fastForward:
375 cmd += ' ' + " --ff-only"
376 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800377 i = self.handle.expect(
378 [
379 'fatal',
380 'Username\sfor\s(.*):\s',
381 '\sfile(s*) changed,\s',
382 'Already up-to-date',
383 'Aborting',
384 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800385 'You asked me to pull without telling me which branch you',
386 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700387 'Please enter a commit message to explain why this merge',
388 'Found a swap file by the name',
389 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800390 pexpect.TIMEOUT ],
391 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800392 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700393 main.log.error( self.name + ": Git pull had some issue" )
394 output = self.handle.after
395 self.handle.expect( '\$' )
396 output += self.handle.before
397 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400398 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800399 elif i == 1:
400 main.log.error(
401 self.name +
402 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400403 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800404 elif i == 2:
405 main.log.info(
406 self.name +
407 ": Git Pull - pulling repository now" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800408 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800409 # So that only when git pull is done, we do mvn clean compile
410 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800411 elif i == 3:
412 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800413 return i
kelvin8ec71442015-01-15 16:57:00 -0800414 elif i == 4:
415 main.log.info(
416 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800417 ": Git Pull - Aborting..." +
418 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400419 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800420 elif i == 5:
421 main.log.info(
422 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800423 ": Git Pull - You are not currently " +
424 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400425 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800426 elif i == 6:
427 main.log.info(
428 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800429 ": Git Pull - You have not configured an upstream " +
430 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400431 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800432 elif i == 7:
433 main.log.info(
434 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800435 ": Git Pull - Pull is not possible because " +
436 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400437 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800438 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700439 # NOTE: abandoning test since we can't reliably handle this
440 # there could be different default text editors and we
441 # also don't know if we actually want to make the commit
442 main.log.error( "Git pull resulted in a merge commit message" +
443 ". Exiting test!" )
444 main.cleanup()
445 main.exit()
446 elif i == 9: # Merge commit message but swap file exists
447 main.log.error( "Git pull resulted in a merge commit message" +
448 " but a swap file exists." )
449 try:
450 self.handle.send( 'A' ) # Abort
451 self.handle.expect( "\$" )
452 return main.ERROR
453 except Exception:
454 main.log.exception( "Couldn't exit editor prompt!")
455 main.cleanup()
456 main.exit()
457 elif i == 10: # In the middle of a merge commit
458 main.log.error( "Git branch is in the middle of a merge. " )
459 main.log.warn( self.handle.before + self.handle.after )
460 return main.ERROR
461 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800462 main.log.error( self.name + ": Git Pull - TIMEOUT" )
463 main.log.error(
464 self.name + " Response was: " + str(
465 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400466 return main.ERROR
467 else:
kelvin8ec71442015-01-15 16:57:00 -0800468 main.log.error(
469 self.name +
470 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400471 return main.ERROR
472 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800473 main.log.error( self.name + ": EOF exception found" )
474 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400475 main.cleanup()
476 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800477 except Exception:
478 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400479 main.cleanup()
480 main.exit()
481
kelvin-onlabd3b64892015-01-20 13:26:24 -0800482 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800483 """
Jon Hallacabffd2014-10-09 12:36:53 -0400484 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800485
Jon Hallacabffd2014-10-09 12:36:53 -0400486 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800487 If used as gitCheckout( "branch" ) it will do git checkout
488 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400489
490 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800491 branch of the ONOS repository. If it has any problems, it will return
492 main.ERROR.
493 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400494 successful then the function will return main.TRUE.
495
kelvin8ec71442015-01-15 16:57:00 -0800496 """
Jon Hallacabffd2014-10-09 12:36:53 -0400497 try:
kelvin8ec71442015-01-15 16:57:00 -0800498 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800499 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800500 main.log.info( self.name +
501 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800502 cmd = "git checkout " + branch
503 self.handle.sendline( cmd )
504 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800505 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800506 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700507 'Username for (.*): ',
508 'Already on \'',
Jon Hall7a8354f2015-06-10 15:37:00 -0700509 'Switched to (a new )?branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800510 pexpect.TIMEOUT,
511 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800512 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800513 'error: you need to resolve your current index first',
514 "You are in 'detached HEAD' state.",
515 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800516 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800517 if i == 0:
518 main.log.error(
519 self.name +
520 ": Git checkout had some issue..." )
521 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400522 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800523 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800524 main.log.error(
525 self.name +
526 ": Git checkout asking for username." +
527 " Please configure your local git repository to be able " +
528 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800529 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400530 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800531 elif i == 2:
532 main.log.info(
533 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800534 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800535 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800536 # main.log.info( "DEBUG: after checkout cmd = "+
537 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400538 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800539 elif i == 3:
540 main.log.info(
541 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800542 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800543 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800544 # main.log.info( "DEBUG: after checkout cmd = "+
545 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400546 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800547 elif i == 4:
548 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
549 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800550 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400551 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800552 elif i == 5:
553 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800554 main.log.error(
555 self.name +
556 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800557 "Your local changes to the following files would" +
558 " be overwritten by checkout:" +
559 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800560 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500561 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800562 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800563 main.log.error(
564 self.name +
565 ": Git checkout error: \n" +
566 "You need to resolve your current index first:" +
567 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800568 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500569 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800570 elif i == 7:
571 main.log.info(
572 self.name +
573 ": Git checkout " + str( branch ) +
574 " - You are in 'detached HEAD' state. HEAD is now at " +
575 str( branch ) )
576 self.handle.expect( self.home + "\$" )
577 return main.TRUE
578 elif i == 8: # Already in detached HEAD on the specified commit
579 main.log.info(
580 self.name +
581 ": Git Checkout %s : Already on commit" % branch )
582 self.handle.expect( self.home + "\$" )
583 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400584 else:
kelvin8ec71442015-01-15 16:57:00 -0800585 main.log.error(
586 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800587 ": Git Checkout - Unexpected response, " +
588 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800589 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400590 return main.ERROR
591
592 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800593 main.log.error( self.name + ": EOF exception found" )
594 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400595 main.cleanup()
596 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800597 except Exception:
598 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400599 main.cleanup()
600 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400601
pingping-lin6d23d9e2015-02-02 16:54:24 -0800602 def getBranchName( self ):
pingping-linf30cf272015-05-29 15:54:07 -0700603 main.log.info( "self.home = " )
604 main.log.info( self.home )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800605 self.handle.sendline( "cd " + self.home )
pingping-lin9bf3d8f2015-05-29 16:05:28 -0700606 self.handle.expect( self.home + "\$" )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800607 self.handle.sendline( "git name-rev --name-only HEAD" )
608 self.handle.expect( "git name-rev --name-only HEAD" )
609 self.handle.expect( "\$" )
610
611 lines = self.handle.before.splitlines()
612 if lines[1] == "master":
613 return "master"
614 elif lines[1] == "onos-1.0":
615 return "onos-1.0"
616 else:
617 main.log.info( lines[1] )
618 return "unexpected ONOS branch for SDN-IP test"
619
kelvin-onlabd3b64892015-01-20 13:26:24 -0800620 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800621 """
Jon Hall274b6642015-02-17 11:57:17 -0800622 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800623 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800624 """
Jon Hall45ec0922014-10-10 19:33:49 -0400625 try:
kelvin8ec71442015-01-15 16:57:00 -0800626 self.handle.sendline( "" )
627 self.handle.expect( "\$" )
628 self.handle.sendline(
629 "cd " +
630 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800631 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
632 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800633 # NOTE: for some reason there are backspaces inserted in this
634 # phrase when run from Jenkins on some tests
635 self.handle.expect( "never" )
636 self.handle.expect( "\$" )
637 response = ( self.name + ": \n" + str(
638 self.handle.before + self.handle.after ) )
639 self.handle.sendline( "cd " + self.home )
640 self.handle.expect( "\$" )
641 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400642 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500643 print line
644 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700645 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800646 for line in lines[ 2:-1 ]:
647 # Bracket replacement is for Wiki-compliant
648 # formatting. '<' or '>' are interpreted
649 # as xml specific tags that cause errors
650 line = line.replace( "<", "[" )
651 line = line.replace( ">", "]" )
pingping-lin763ee042015-05-20 17:45:30 -0700652 #main.log.wiki( "\t" + line )
653 main.log.wiki( line + "<br /> " )
654 main.log.summary( line )
655 main.log.wiki( "</blockquote>" )
656 main.log.summary("\n")
kelvin8ec71442015-01-15 16:57:00 -0800657 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400658 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800659 main.log.error( self.name + ": EOF exception found" )
660 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400661 main.cleanup()
662 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800663 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800664 main.log.error( self.name + ": TIMEOUT exception found" )
665 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800666 main.cleanup()
667 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800668 except Exception:
669 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400670 main.cleanup()
671 main.exit()
672
kelvin-onlabd3b64892015-01-20 13:26:24 -0800673 def createCellFile( self, benchIp, fileName, mnIpAddrs,
kelvin-onlaba4074292015-07-09 15:19:49 -0700674 appString, onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800675 """
andrewonlab94282092014-10-10 13:00:11 -0400676 Creates a cell file based on arguments
677 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800678 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400679 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800680 * File name of the cell file ( fileName )
681 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800682 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400683 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800684 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400685 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800686
andrewonlab94282092014-10-10 13:00:11 -0400687 NOTE: Assumes cells are located at:
688 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800689 """
690 # Variable initialization
Jon Hall6801cda2015-07-15 14:13:45 -0700691 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800692 # We want to create the cell file in the dependencies directory
693 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800694 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800695 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800696 cellFile = open( tempDirectory + fileName, 'w+' )
Jon Hall6801cda2015-07-15 14:13:45 -0700697 if isinstance( onosIpAddrs, types.StringType ):
698 onosIpAddrs = [ onosIpAddrs ]
kelvin8ec71442015-01-15 16:57:00 -0800699
cameron@onlab.us75900962015-03-30 13:22:49 -0700700 # App string is hardcoded environment variables
kelvin8ec71442015-01-15 16:57:00 -0800701 # That you may wish to use by default on startup.
cameron@onlab.us75900962015-03-30 13:22:49 -0700702 # Note that you may not want certain apps listed
kelvin8ec71442015-01-15 16:57:00 -0800703 # on here.
cameron@onlab.us75900962015-03-30 13:22:49 -0700704 appString = "export ONOS_APPS=" + appString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800705 mnString = "export OCN="
kelvin-onlabf70fd542015-05-07 18:41:40 -0700706 if mnIpAddrs == "":
707 mnString = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800708 onosString = "export OC"
709 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800710
kelvin-onlabd3b64892015-01-20 13:26:24 -0800711 # Create ONOSNIC ip address prefix
kelvin-onlaba4074292015-07-09 15:19:49 -0700712 tempOnosIp = str( onosIpAddrs[ 0 ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800713 tempList = []
714 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800715 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800716 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800717 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800718 nicAddr = ".".join( tempList ) + ".*"
719 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400720
721 try:
kelvin8ec71442015-01-15 16:57:00 -0800722 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800723 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400724
kelvin-onlabd3b64892015-01-20 13:26:24 -0800725 for arg in onosIpAddrs:
726 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800727 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400728 # export OC1="10.128.20.11"
729 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800730 cellFile.write( onosString + str( tempCount ) +
Jon Hall6f665652015-09-18 10:08:07 -0700731 "=\"" + arg + "\"\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800732 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800733
Jon Hall6f665652015-09-18 10:08:07 -0700734 cellFile.write( "export OCI=$OC1\n" )
735 cellFile.write( mnString + "\"" + mnIpAddrs + "\"\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700736 cellFile.write( appString + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800737 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400738
kelvin8ec71442015-01-15 16:57:00 -0800739 # We use os.system to send the command to TestON cluster
740 # to account for the case in which TestON is not located
741 # on the same cluster as the ONOS bench
742 # Note that even if TestON is located on the same cluster
743 # as ONOS bench, you must setup passwordless ssh
744 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabc2b79102015-07-14 11:41:20 -0700745 os.system( "scp " + tempDirectory + fileName + " " +
746 self.user_name + "@" + self.ip_address + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400747
andrewonlab2a6c9342014-10-16 13:40:15 -0400748 return main.TRUE
749
andrewonlab94282092014-10-10 13:00:11 -0400750 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800751 main.log.error( self.name + ": EOF exception found" )
752 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400753 main.cleanup()
754 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800755 except Exception:
756 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400757 main.cleanup()
758 main.exit()
759
kelvin-onlabd3b64892015-01-20 13:26:24 -0800760 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800761 """
andrewonlab95ca1462014-10-09 14:04:24 -0400762 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800763 """
Hari Krishna03f530e2015-07-10 17:28:27 -0700764 import re
andrewonlab95ca1462014-10-09 14:04:24 -0400765 try:
766 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800767 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400768 main.cleanup()
769 main.exit()
770 else:
kelvin8ec71442015-01-15 16:57:00 -0800771 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800772 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800773 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400774 # and that this driver will have to change accordingly
Jon Hall3b489db2015-10-05 14:38:37 -0700775 self.handle.expect( str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800776 handleBefore = self.handle.before
777 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800778 # Get the rest of the handle
Jon Hall3b489db2015-10-05 14:38:37 -0700779 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800780 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400781
Hari Krishna03f530e2015-07-10 17:28:27 -0700782 cell_result = handleBefore + handleAfter + handleMore
783 print cell_result
784 if( re.search( "No such cell", cell_result ) ):
785 main.log.error( "Cell call returned: " + handleBefore +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800786 handleAfter + handleMore )
Hari Krishna03f530e2015-07-10 17:28:27 -0700787 main.cleanup()
788 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400789 return main.TRUE
andrewonlab95ca1462014-10-09 14:04:24 -0400790 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800791 main.log.error( self.name + ": EOF exception found" )
792 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400793 main.cleanup()
794 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800795 except Exception:
796 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400797 main.cleanup()
798 main.exit()
799
kelvin-onlabd3b64892015-01-20 13:26:24 -0800800 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800801 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400802 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800803 """
804 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400805
andrewonlabc03bf6c2014-10-09 14:56:18 -0400806 try:
kelvin8ec71442015-01-15 16:57:00 -0800807 # Clean handle by sending empty and expecting $
808 self.handle.sendline( "" )
809 self.handle.expect( "\$" )
810 self.handle.sendline( "onos-verify-cell" )
811 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800812 handleBefore = self.handle.before
813 handleAfter = self.handle.after
kelvin-onlabd3b64892015-01-20 13:26:24 -0800814 main.log.info( "Verify cell returned: " + handleBefore +
Jon Hall3b489db2015-10-05 14:38:37 -0700815 handleAfter )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400816 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800817 except pexpect.ExceptionPexpect as e:
Jon Hall3b489db2015-10-05 14:38:37 -0700818 main.log.exception( self.name + ": Pexpect exception found: " )
kelvin8ec71442015-01-15 16:57:00 -0800819 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400820 main.cleanup()
821 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800822 except Exception:
823 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400824 main.cleanup()
825 main.exit()
826
jenkins1e99e7b2015-04-02 18:15:39 -0700827 def onosCfgSet( self, ONOSIp, configName, configParam ):
828 """
829 Uses 'onos <node-ip> cfg set' to change a parameter value of an
Jon Hall4ba53f02015-07-29 13:07:41 -0700830 application.
831
jenkins1e99e7b2015-04-02 18:15:39 -0700832 ex)
833 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
jenkins1e99e7b2015-04-02 18:15:39 -0700834 ONOSIp = '10.0.0.1'
835 configName = 'org.onosproject.myapp'
836 configParam = 'appSetting 1'
jenkins1e99e7b2015-04-02 18:15:39 -0700837 """
cameron@onlab.us78b89652015-07-08 15:21:03 -0700838 for i in range(5):
839 try:
840 cfgStr = ( "onos "+str(ONOSIp)+" cfg set "+
841 str(configName) + " " +
842 str(configParam)
843 )
jenkins1e99e7b2015-04-02 18:15:39 -0700844
cameron@onlab.us78b89652015-07-08 15:21:03 -0700845 self.handle.sendline( "" )
846 self.handle.expect( ":~" )
847 self.handle.sendline( cfgStr )
Jon Hall4ba53f02015-07-29 13:07:41 -0700848 self.handle.expect("cfg set")
cameron@onlab.us78b89652015-07-08 15:21:03 -0700849 self.handle.expect( ":~" )
Jon Hall4ba53f02015-07-29 13:07:41 -0700850
cameron@onlab.us78b89652015-07-08 15:21:03 -0700851 paramValue = configParam.split(" ")[1]
852 paramName = configParam.split(" ")[0]
Jon Hall4ba53f02015-07-29 13:07:41 -0700853
cameron@onlab.us78b89652015-07-08 15:21:03 -0700854 checkStr = ( "onos " + str(ONOSIp) + """ cfg get " """ + str(configName) + " " + paramName + """ " """)
jenkins1e99e7b2015-04-02 18:15:39 -0700855
cameron@onlab.us78b89652015-07-08 15:21:03 -0700856 self.handle.sendline( checkStr )
857 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700858
cameron@onlab.us78b89652015-07-08 15:21:03 -0700859 if "value=" + paramValue + "," in self.handle.before:
Jon Hall4ba53f02015-07-29 13:07:41 -0700860 main.log.info("cfg " + configName + " successfully set to " + configParam)
cameron@onlab.us78b89652015-07-08 15:21:03 -0700861 return main.TRUE
862
863 except pexpect.ExceptionPexpect as e:
Jon Hall3b489db2015-10-05 14:38:37 -0700864 main.log.exception( self.name + ": Pexpect exception found: " )
cameron@onlab.us78b89652015-07-08 15:21:03 -0700865 main.log.error( self.name + ": " + self.handle.before )
866 main.cleanup()
867 main.exit()
868 except Exception:
869 main.log.exception( self.name + ": Uncaught exception!" )
870 main.cleanup()
871 main.exit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700872
cameron@onlab.us78b89652015-07-08 15:21:03 -0700873 time.sleep(5)
874
875 main.log.error("CFG SET FAILURE: " + configName + " " + configParam )
Jon Hall4ba53f02015-07-29 13:07:41 -0700876 main.ONOSbench.handle.sendline("onos $OC1 cfg get")
cameron@onlab.us78b89652015-07-08 15:21:03 -0700877 main.ONOSbench.handle.expect("\$")
878 print main.ONOSbench.handle.before
879 main.ONOSbench.logReport( ONOSIp, ["ERROR","WARN","EXCEPT"], "d")
880 return main.FALSE
881
Jon Hall4ba53f02015-07-29 13:07:41 -0700882
kelvin-onlabd3b64892015-01-20 13:26:24 -0800883 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800884 """
andrewonlab05e362f2014-10-10 00:40:57 -0400885 Uses 'onos' command to send various ONOS CLI arguments.
886 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800887 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400888 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800889
890 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400891 CLI commands for ONOS. Try to use this function first
892 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800893 function.
894 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400895 by starting onos, and typing in 'onos' to enter the
896 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800897 available commands.
898 """
andrewonlab05e362f2014-10-10 00:40:57 -0400899 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800900 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800901 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400902 return main.FALSE
903 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800904 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400905 return main.FALSE
906
kelvin8ec71442015-01-15 16:57:00 -0800907 cmdstr = str( cmdstr )
908 self.handle.sendline( "" )
909 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400910
kelvin-onlabd3b64892015-01-20 13:26:24 -0800911 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800912 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400913
kelvin-onlabd3b64892015-01-20 13:26:24 -0800914 handleBefore = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800915 main.log.info( "Command sent successfully" )
kelvin8ec71442015-01-15 16:57:00 -0800916 # Obtain return handle that consists of result from
917 # the onos command. The string may need to be
918 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800919 returnString = handleBefore
kelvin-onlabd3b64892015-01-20 13:26:24 -0800920 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400921 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800922 main.log.error( self.name + ": EOF exception found" )
923 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400924 main.cleanup()
925 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800926 except Exception:
927 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400928 main.cleanup()
929 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400930
kelvin-onlabd3b64892015-01-20 13:26:24 -0800931 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800932 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400933 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800934 If -f option is provided, it also forces an uninstall.
935 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400936 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800937 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400938 files to certain onos nodes
939
940 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800941 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400942 try:
andrewonlab114768a2014-11-14 12:44:44 -0500943 if options:
kelvin8ec71442015-01-15 16:57:00 -0800944 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500945 else:
kelvin8ec71442015-01-15 16:57:00 -0800946 self.handle.sendline( "onos-install " + node )
947 self.handle.expect( "onos-install " )
948 # NOTE: this timeout may need to change depending on the network
949 # and size of ONOS
950 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800951 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800952 "ONOS\sis\salready\sinstalled",
953 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400954 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800955 main.log.warn( "Network is unreachable" )
Jon Hall3b489db2015-10-05 14:38:37 -0700956 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400957 return main.FALSE
958 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800959 main.log.info(
960 "ONOS was installed on " +
961 node +
962 " and started" )
Jon Hall3b489db2015-10-05 14:38:37 -0700963 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400964 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500965 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800966 main.log.info( "ONOS is already installed on " + node )
Jon Hall3b489db2015-10-05 14:38:37 -0700967 self.handle.expect( "\$" )
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 Hall3b489db2015-10-05 14:38:37 -0700974 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400975 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400976 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 )
Jon Hall3b489db2015-10-05 14:38:37 -07001001 self.handle.expect( "\$" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001002 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 )
Jon Hall3b489db2015-10-05 14:38:37 -07001037 self.handle.expect( "\$" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001038 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
andrewonlab2b30bd32014-10-09 16:48:55 -04001051 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001052 main.log.error( self.name + ": EOF exception found" )
1053 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -04001054 main.cleanup()
1055 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001056 except Exception:
1057 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001058 main.cleanup()
1059 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001060
kelvin-onlabd3b64892015-01-20 13:26:24 -08001061 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -08001062 """
andrewonlabc8d47972014-10-09 16:52:36 -04001063 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -08001064 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -04001065 if needed
kelvin8ec71442015-01-15 16:57:00 -08001066 """
andrewonlabc8d47972014-10-09 16:52:36 -04001067 try:
kelvin8ec71442015-01-15 16:57:00 -08001068 self.handle.sendline( "" )
pingping-lin763ee042015-05-20 17:45:30 -07001069 self.handle.expect( "\$", timeout=60 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001070 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
Jon Hall3b489db2015-10-05 14:38:37 -07001071 self.handle.expect( "\$", timeout=60 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001072 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
kelvin8ec71442015-01-15 16:57:00 -08001073 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -04001074 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -07001075 except pexpect.TIMEOUT:
1076 main.log.exception( self.name + ": Timeout in onosUninstall" )
1077 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -04001078 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001079 main.log.error( self.name + ": EOF exception found" )
1080 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -04001081 main.cleanup()
1082 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001083 except Exception:
1084 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -04001085 main.cleanup()
1086 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -04001087
kelvin-onlabd3b64892015-01-20 13:26:24 -08001088 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001089 """
andrewonlabaedc8332014-12-04 12:43:03 -05001090 Issues the command 'onos-die <node-ip>'
1091 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -08001092 """
andrewonlabaedc8332014-12-04 12:43:03 -05001093 try:
kelvin8ec71442015-01-15 16:57:00 -08001094 self.handle.sendline( "" )
1095 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001096 cmdStr = "onos-kill " + str( nodeIp )
1097 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001098 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001099 "Killing\sONOS",
1100 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -08001101 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -05001102 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001103 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001104 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -05001105 return main.TRUE
1106 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001107 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -05001108 return main.FALSE
1109 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001110 main.log.error( self.name + ": EOF exception found" )
1111 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -05001112 main.cleanup()
1113 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001114 except Exception:
1115 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -05001116 main.cleanup()
1117 main.exit()
1118
kelvin-onlabd3b64892015-01-20 13:26:24 -08001119 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001120 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001121 Calls the command: 'onos-kill [<node-ip>]'
1122 "Remotely, and unceremoniously kills the ONOS instance running on
1123 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001124 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001125 try:
kelvin8ec71442015-01-15 16:57:00 -08001126 self.handle.sendline( "" )
1127 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001128 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001129 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -04001130 "\$",
1131 "No\sroute\sto\shost",
1132 "password:",
kelvin8ec71442015-01-15 16:57:00 -08001133 pexpect.TIMEOUT ], timeout=20 )
1134
andrewonlabe8e56fd2014-10-09 17:12:44 -04001135 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001136 main.log.info(
1137 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001138 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001139 return main.TRUE
1140 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001141 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001142 return main.FALSE
1143 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001144 main.log.info(
1145 "Passwordless login for host: " +
1146 str( nodeIp ) +
1147 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001148 return main.FALSE
1149 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001150 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001151 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001152
andrewonlabe8e56fd2014-10-09 17:12:44 -04001153 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001154 main.log.error( self.name + ": EOF exception found" )
1155 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001156 main.cleanup()
1157 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001158 except Exception:
1159 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001160 main.cleanup()
1161 main.exit()
1162
kelvin-onlabd3b64892015-01-20 13:26:24 -08001163 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001164 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001165 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001166 a cleaner environment.
1167
andrewonlab19fbdca2014-11-14 12:55:59 -05001168 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001169 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001170 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001171 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001172 try:
kelvin8ec71442015-01-15 16:57:00 -08001173 self.handle.sendline( "" )
1174 self.handle.expect( "\$" )
1175 self.handle.sendline( "onos-remove-raft-logs" )
1176 # Sometimes this command hangs
1177 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1178 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001179 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001180 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1181 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001182 if i == 1:
1183 return main.FALSE
andrewonlab19fbdca2014-11-14 12:55:59 -05001184 return main.TRUE
andrewonlab19fbdca2014-11-14 12:55:59 -05001185 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001186 main.log.error( self.name + ": EOF exception found" )
1187 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001188 main.cleanup()
1189 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001190 except Exception:
1191 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001192 main.cleanup()
1193 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001194
kelvin-onlabd3b64892015-01-20 13:26:24 -08001195 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001196 """
1197 Calls the command 'onos-start-network [ <mininet-topo> ]
1198 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001199 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001200 cell."
andrewonlab94282092014-10-10 13:00:11 -04001201 * Specify mininet topology file name for mntopo
1202 * Topo files should be placed at:
1203 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001204
andrewonlab94282092014-10-10 13:00:11 -04001205 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001206 """
andrewonlab94282092014-10-10 13:00:11 -04001207 try:
1208 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001209 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001210 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001211
kelvin8ec71442015-01-15 16:57:00 -08001212 mntopo = str( mntopo )
1213 self.handle.sendline( "" )
1214 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001215
kelvin8ec71442015-01-15 16:57:00 -08001216 self.handle.sendline( "onos-start-network " + mntopo )
1217 self.handle.expect( "mininet>" )
1218 main.log.info( "Network started, entered mininet prompt" )
1219
1220 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001221
1222 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001223 main.log.error( self.name + ": EOF exception found" )
1224 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001225 main.cleanup()
1226 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001227 except Exception:
1228 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001229 main.cleanup()
1230 main.exit()
1231
Jon Hall3b489db2015-10-05 14:38:37 -07001232 def isup( self, node="", timeout=120 ):
kelvin8ec71442015-01-15 16:57:00 -08001233 """
1234 Run's onos-wait-for-start which only returns once ONOS is at run
Cameron Franke9c94fb02015-01-21 10:20:20 -08001235 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001236
Jon Hall7993bfc2014-10-09 16:30:14 -04001237 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001238 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001239 try:
Jon Hall3b489db2015-10-05 14:38:37 -07001240 self.handle.sendline( "onos-wait-for-start " + node )
1241 self.handle.expect( "onos-wait-for-start" )
kelvin8ec71442015-01-15 16:57:00 -08001242 # NOTE: this timeout is arbitrary"
Cameron Franke9c94fb02015-01-21 10:20:20 -08001243 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001244 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001245 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001246 return main.TRUE
1247 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001248 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001249 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001250 main.log.error( "ONOS has not started yet" )
1251 self.handle.send( "\x03" ) # Control-C
1252 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001253 return main.FALSE
1254 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001255 main.log.error( self.name + ": EOF exception found" )
1256 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001257 main.cleanup()
1258 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001259 except Exception:
1260 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001261 main.cleanup()
1262 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001263
kelvin-onlabd3b64892015-01-20 13:26:24 -08001264 def pushTestIntentsShell(
1265 self,
1266 dpidSrc,
1267 dpidDst,
1268 numIntents,
1269 dirFile,
1270 onosIp,
1271 numMult="",
1272 appId="",
1273 report=True,
1274 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001275 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001276 Description:
kelvin8ec71442015-01-15 16:57:00 -08001277 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001278 better parallelize the results than the CLI
1279 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001280 * dpidSrc: specify source dpid
1281 * dpidDst: specify destination dpid
1282 * numIntents: specify number of intents to push
1283 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001284 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001285 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001286 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001287 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001288 """
1289 try:
1290 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001291 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001292 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001293 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001294 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001295 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001296
kelvin-onlabd3b64892015-01-20 13:26:24 -08001297 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1298 if not numMult:
1299 addIntents = addDpid + " " + str( numIntents )
1300 elif numMult:
1301 addIntents = addDpid + " " + str( numIntents ) + " " +\
1302 str( numMult )
1303 if appId:
1304 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001305 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001306 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001307
andrewonlabaedc8332014-12-04 12:43:03 -05001308 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001309 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001310 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001311 sendCmd = addApp + " &"
1312 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001313
kelvin-onlabd3b64892015-01-20 13:26:24 -08001314 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001315
1316 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001317 main.log.error( self.name + ": EOF exception found" )
1318 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001319 main.cleanup()
1320 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001321 except Exception:
1322 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001323 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001324 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001325
kelvin-onlabd3b64892015-01-20 13:26:24 -08001326 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001327 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001328 Definition:
1329 Loads a json topology output
1330 Return:
1331 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -08001332 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001333 import json
Jon Hall77f53ce2014-10-13 18:02:06 -04001334 try:
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001335 # either onos:topology or 'topology' will work in CLI
1336 topology = json.loads(topologyOutput)
1337 print topology
Jon Hall77f53ce2014-10-13 18:02:06 -04001338 return topology
1339 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001340 main.log.error( self.name + ": EOF exception found" )
1341 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001342 main.cleanup()
1343 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001344 except Exception:
1345 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001346 main.cleanup()
1347 main.exit()
1348
kelvin-onlabd3b64892015-01-20 13:26:24 -08001349 def checkStatus(
1350 self,
1351 topologyResult,
1352 numoswitch,
1353 numolink,
1354 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001355 """
Jon Hallefbd9792015-03-05 16:11:36 -08001356 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001357 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001358 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08001359
Jon Hall77f53ce2014-10-13 18:02:06 -04001360 Params: ip = ip used for the onos cli
1361 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001362 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001363 logLevel = level to log to.
1364 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001365
1366
kelvin-onlabd3b64892015-01-20 13:26:24 -08001367 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001368
Jon Hallefbd9792015-03-05 16:11:36 -08001369 Returns: main.TRUE if the number of switches and links are correct,
1370 main.FALSE if the number of switches and links is incorrect,
Jon Hall77f53ce2014-10-13 18:02:06 -04001371 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001372 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001373 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001374 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001375 if topology == {}:
1376 return main.ERROR
1377 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001378 # Is the number of switches is what we expected
shahshreya234a1682015-05-27 15:41:56 -07001379 devices = topology.get( 'devices', False )
1380 links = topology.get( 'links', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001381 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001382 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001383 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001384 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001385 linkCheck = ( int( links ) == int( numolink ) )
Jon Hallefbd9792015-03-05 16:11:36 -08001386 if switchCheck and linkCheck:
kelvin8ec71442015-01-15 16:57:00 -08001387 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001388 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001389 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001390 result = main.TRUE
1391 else:
1392 output = output + \
Jon Hall274b6642015-02-17 11:57:17 -08001393 "The number of links and switches does not match " + \
1394 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001395 result = main.FALSE
Jon Hallefbd9792015-03-05 16:11:36 -08001396 output = output + "\n ONOS sees %i devices" % int( devices )
1397 output = output + " (%i expected) " % int( numoswitch )
Jon Hall274b6642015-02-17 11:57:17 -08001398 output = output + "and %i links " % int( links )
1399 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001400 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001401 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001402 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001403 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001404 else:
kelvin8ec71442015-01-15 16:57:00 -08001405 main.log.info( output )
1406 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001407 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001408 main.log.error( self.name + ": EOF exception found" )
1409 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001410 main.cleanup()
1411 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001412 except Exception:
1413 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001414 main.cleanup()
1415 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001416
kelvin-onlabd3b64892015-01-20 13:26:24 -08001417 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001418 """
andrewonlab970399c2014-11-07 13:09:32 -05001419 Capture all packet activity and store in specified
1420 directory/file
1421
1422 Required:
1423 * interface: interface to capture
1424 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001425 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001426 try:
1427 self.handle.sendline( "" )
1428 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001429
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001430 self.handle.sendline( "tshark -i " + str( interface ) + " -t e -w " + str( dirFile ) + " &" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001431 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001432 self.handle.expect( "Capturing on" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001433 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001434 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001435
Jon Hallfebb1c72015-03-05 13:30:09 -08001436 main.log.info( "Tshark started capturing files on " +
1437 str( interface ) + " and saving to directory: " +
1438 str( dirFile ) )
1439 except pexpect.EOF:
1440 main.log.error( self.name + ": EOF exception found" )
1441 main.log.error( self.name + ": " + self.handle.before )
1442 main.cleanup()
1443 main.exit()
1444 except Exception:
1445 main.log.exception( self.name + ": Uncaught exception!" )
1446 main.cleanup()
1447 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001448
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001449 def onosTopoCfg( self, onosIp, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001450 """
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001451 Description:
1452 Execute onos-topo-cfg command
1453 Required:
1454 onosIp - IP of the onos node you want to send the json to
1455 jsonFile - File path of the json file
1456 Return:
1457 Returns main.TRUE if the command is successfull; Returns
1458 main.FALSE if there was an error
kelvin8ec71442015-01-15 16:57:00 -08001459 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001460 try:
kelvin8ec71442015-01-15 16:57:00 -08001461 self.handle.sendline( "" )
1462 self.handle.expect( "\$" )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001463 cmd = "onos-topo-cfg "
1464 self.handle.sendline( cmd + str( onosIp ) + " " + jsonFile )
1465 handle = self.handle.before
1466 print handle
1467 if "Error" in handle:
1468 main.log.error( self.name + ": " + self.handle.before )
1469 return main.FALSE
1470 else:
1471 self.handle.expect( "\$" )
1472 return main.TRUE
1473
Jon Hallfebb1c72015-03-05 13:30:09 -08001474 except pexpect.EOF:
1475 main.log.error( self.name + ": EOF exception found" )
1476 main.log.error( self.name + ": " + self.handle.before )
1477 main.cleanup()
1478 main.exit()
1479 except Exception:
1480 main.log.exception( self.name + ": Uncaught exception!" )
1481 main.cleanup()
1482 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001483
jenkins1e99e7b2015-04-02 18:15:39 -07001484 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001485 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001486 Required:
kelvin8ec71442015-01-15 16:57:00 -08001487 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001488 * directory to store results
1489 Optional:
1490 * interface - default: eth0
Jon Hall4ba53f02015-07-29 13:07:41 -07001491 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001492 Description:
1493 Uses tshark command to grep specific group of packets
1494 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001495 The timestamp is hardcoded to be in epoch
1496 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001497 try:
1498 self.handle.sendline( "" )
1499 self.handle.expect( "\$" )
1500 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001501 if grepOptions:
1502 grepStr = "grep "+str(grepOptions)
1503 else:
1504 grepStr = "grep"
Jon Hall4ba53f02015-07-29 13:07:41 -07001505
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001506 cmd = (
1507 "sudo tshark -i " +
Jon Hallfebb1c72015-03-05 13:30:09 -08001508 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001509 " -t e | " +
1510 grepStr + " --line-buffered \"" +
Jon Hallfebb1c72015-03-05 13:30:09 -08001511 str(grep) +
1512 "\" >" +
1513 directory +
1514 " &" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001515 self.handle.sendline(cmd)
1516 main.log.info(cmd)
Jon Hallfebb1c72015-03-05 13:30:09 -08001517 self.handle.expect( "Capturing on" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001518 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001519 self.handle.expect( "\$" )
1520 except pexpect.EOF:
1521 main.log.error( self.name + ": EOF exception found" )
1522 main.log.error( self.name + ": " + self.handle.before )
1523 main.cleanup()
1524 main.exit()
1525 except Exception:
1526 main.log.exception( self.name + ": Uncaught exception!" )
1527 main.cleanup()
1528 main.exit()
1529
kelvin-onlabd3b64892015-01-20 13:26:24 -08001530 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001531 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001532 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001533 """
1534 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001535 try:
1536 self.execute( cmd="sudo rm /tmp/wireshark*" )
1537 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001538 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1539 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001540 self.handle.sendline( "" )
1541 main.log.info( "Tshark stopped" )
1542 except pexpect.EOF:
1543 main.log.error( self.name + ": EOF exception found" )
1544 main.log.error( self.name + ": " + self.handle.before )
1545 main.cleanup()
1546 main.exit()
1547 except Exception:
1548 main.log.exception( self.name + ": Uncaught exception!" )
1549 main.cleanup()
1550 main.exit()
1551
kelvin8ec71442015-01-15 16:57:00 -08001552 def ptpd( self, args ):
1553 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001554 Initiate ptp with user-specified args.
1555 Required:
1556 * args: specify string of args after command
1557 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001558 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001559 try:
kelvin8ec71442015-01-15 16:57:00 -08001560 self.handle.sendline( "sudo ptpd " + str( args ) )
1561 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001562 "Multiple",
1563 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001564 "\$" ] )
1565 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001566
andrewonlab0c38a4a2014-10-28 18:35:35 -04001567 if i == 0:
1568 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001569 main.log.info( "ptpd returned an error: " +
1570 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001571 return handle
1572 elif i == 1:
1573 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001574 main.log.error( "ptpd returned an error: " +
1575 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001576 return handle
1577 else:
1578 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001579
andrewonlab0c38a4a2014-10-28 18:35:35 -04001580 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001581 main.log.error( self.name + ": EOF exception found" )
1582 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001583 main.cleanup()
1584 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001585 except Exception:
1586 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001587 main.cleanup()
1588 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001589
kelvin-onlabd3b64892015-01-20 13:26:24 -08001590 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001591 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001592 """
1593 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001594 Current implementation of ONOS deletes its karaf
1595 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001596 you may want to use this function to capture
1597 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001598 Localtime will be attached to the filename
1599
1600 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001601 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001602 copy.
kelvin8ec71442015-01-15 16:57:00 -08001603 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001604 For copying multiple files, leave copyFileName
1605 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001606 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001607 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001608 ex ) /tmp/
1609 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001610 * copyFileName: If you want to rename the log
1611 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001612 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001613 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001614 try:
kelvin8ec71442015-01-15 16:57:00 -08001615 localtime = time.strftime( '%x %X' )
1616 localtime = localtime.replace( "/", "" )
1617 localtime = localtime.replace( " ", "_" )
1618 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001619 if destDir[ -1: ] != "/":
1620 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001621
kelvin-onlabd3b64892015-01-20 13:26:24 -08001622 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001623 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1624 str( destDir ) + str( copyFileName ) +
1625 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001626 self.handle.expect( "cp" )
1627 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001628 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001629 self.handle.sendline( "cp " + str( logToCopy ) +
1630 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001631 self.handle.expect( "cp" )
1632 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001633
kelvin8ec71442015-01-15 16:57:00 -08001634 return self.handle.before
1635
1636 except pexpect.EOF:
1637 main.log.error( "Copying files failed" )
1638 main.log.error( self.name + ": EOF exception found" )
1639 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001640 except Exception:
1641 main.log.exception( "Copying files failed" )
1642
Jon Hall16b72c42015-05-20 10:23:36 -07001643 def checkLogs( self, onosIp, restart=False):
kelvin8ec71442015-01-15 16:57:00 -08001644 """
Jon Hall94fd0472014-12-08 11:52:42 -08001645 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001646 If restart is True, use the old version of onos-check-logs which
1647 does not print the full stacktrace, but shows the entire log file,
1648 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001649 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001650 """
Jon Hall94fd0472014-12-08 11:52:42 -08001651 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001652 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001653 if restart:
1654 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001655 self.handle.sendline( cmd )
1656 self.handle.expect( cmd )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001657 self.handle.expect( "\$ " )
Jon Hall94fd0472014-12-08 11:52:42 -08001658 response = self.handle.before
1659 return response
1660 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001661 main.log.error( "Lost ssh connection" )
1662 main.log.error( self.name + ": EOF exception found" )
1663 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001664 except Exception:
1665 main.log.exception( self.name + ": Uncaught exception!" )
1666 main.cleanup()
1667 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001668
kelvin-onlabd3b64892015-01-20 13:26:24 -08001669 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001670 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001671 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001672 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001673 try:
kelvin8ec71442015-01-15 16:57:00 -08001674 self.handle.sendline( "" )
1675 self.handle.expect( "\$" )
1676 self.handle.sendline( "onos-service " + str( node ) +
1677 " status" )
1678 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001679 "start/running",
1680 "stop/waiting",
kelvin8ec71442015-01-15 16:57:00 -08001681 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001682
1683 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001684 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001685 return main.TRUE
1686 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001687 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001688 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001689 main.cleanup()
1690 main.exit()
1691 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001692 main.log.error( self.name + ": EOF exception found" )
1693 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001694 main.cleanup()
1695 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001696 except Exception:
1697 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001698 main.cleanup()
1699 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001700
Jon Hall63604932015-02-26 17:09:50 -08001701 def setIpTables( self, ip, port='', action='add', packet_type='',
1702 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001703 """
Jon Hall21270ac2015-02-16 17:59:55 -08001704 Description:
1705 add or remove iptables rule to DROP (default) packets from
1706 specific IP and PORT
1707 Usage:
1708 * specify action ('add' or 'remove')
1709 when removing, pass in the same argument as you would add. It will
1710 delete that specific rule.
1711 * specify the ip to block
1712 * specify the destination port to block (defaults to all ports)
1713 * optional packet type to block (default tcp)
1714 * optional iptables rule (default DROP)
1715 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001716 * States boolean toggles adding all supported tcp states to the
1717 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001718 Returns:
1719 main.TRUE on success or
1720 main.FALSE if given invalid input or
1721 main.ERROR if there is an error in response from iptables
1722 WARNING:
1723 * This function uses root privilege iptables command which may result
1724 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001725 """
Jon Hall21270ac2015-02-16 17:59:55 -08001726
1727 # NOTE*********
1728 # The strict checking methods of this driver function is intentional
1729 # to discourage any misuse or error of iptables, which can cause
1730 # severe network errors
1731 # *************
1732
1733 # NOTE: Sleep needed to give some time for rule to be added and
1734 # registered to the instance. If you are calling this function
1735 # multiple times this sleep will prevent any errors.
1736 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001737 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001738 try:
1739 # input validation
1740 action_type = action.lower()
1741 rule = rule.upper()
1742 direction = direction.upper()
1743 if action_type != 'add' and action_type != 'remove':
1744 main.log.error( "Invalid action type. Use 'add' or "
1745 "'remove' table rule" )
1746 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1747 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1748 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1749 "'ACCEPT' or 'LOG' only." )
1750 if direction != 'INPUT' and direction != 'OUTPUT':
1751 # NOTE currently only supports rules INPUT and OUPTUT
1752 main.log.error( "Invalid rule. Valid directions are"
1753 " 'OUTPUT' or 'INPUT'" )
1754 return main.FALSE
1755 return main.FALSE
1756 return main.FALSE
1757 if action_type == 'add':
1758 # -A is the 'append' action of iptables
1759 actionFlag = '-A'
1760 elif action_type == 'remove':
1761 # -D is the 'delete' rule of iptables
1762 actionFlag = '-D'
1763 self.handle.sendline( "" )
1764 self.handle.expect( "\$" )
1765 cmd = "sudo iptables " + actionFlag + " " +\
1766 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001767 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001768 # " -p " + str( packet_type ) +\
1769 if packet_type:
1770 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001771 if port:
1772 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001773 if states:
1774 cmd += " -m state --state="
1775 #FIXME- Allow user to configure which states to block
1776 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001777 cmd += " -j " + str( rule )
1778
1779 self.handle.sendline( cmd )
1780 self.handle.expect( "\$" )
1781 main.log.warn( self.handle.before )
1782
1783 info_string = "On " + str( self.name )
1784 info_string += " " + str( action_type )
1785 info_string += " iptable rule [ "
1786 info_string += " IP: " + str( ip )
1787 info_string += " Port: " + str( port )
1788 info_string += " Rule: " + str( rule )
1789 info_string += " Direction: " + str( direction ) + " ]"
1790 main.log.info( info_string )
1791 return main.TRUE
1792 except pexpect.TIMEOUT:
1793 main.log.exception( self.name + ": Timeout exception in "
1794 "setIpTables function" )
1795 return main.ERROR
1796 except pexpect.EOF:
1797 main.log.error( self.name + ": EOF exception found" )
1798 main.log.error( self.name + ": " + self.handle.before )
1799 main.cleanup()
1800 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001801 except Exception:
1802 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001803 main.cleanup()
1804 main.exit()
1805
Jon Hall0468b042015-02-19 19:08:21 -08001806 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001807 """
Jon Hall0468b042015-02-19 19:08:21 -08001808 This method is used by STS to check the status of the controller
1809 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001810 """
Jon Hall0468b042015-02-19 19:08:21 -08001811 import re
1812 try:
1813 self.handle.sendline( "" )
1814 self.handle.expect( "\$" )
1815 self.handle.sendline( "cd " + self.home )
1816 self.handle.expect( "\$" )
1817 self.handle.sendline( "service onos status" )
1818 self.handle.expect( "\$" )
1819 response = self.handle.before
1820 if re.search( "onos start/running", response ):
1821 # onos start/running, process 10457
1822 return 'RUNNING'
1823 # FIXME: Implement this case
1824 # elif re.search( pattern, response ):
1825 # return 'STARTING'
1826 elif re.search( "onos stop/", response ):
1827 # onos stop/waiting
1828 # FIXME handle this differently?: onos stop/pre-stop
1829 return 'STOPPED'
1830 # FIXME: Implement this case
1831 # elif re.search( pattern, response ):
1832 # return 'FROZEN'
1833 else:
1834 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001835 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001836 main.log.warn( response )
1837 return 'ERROR', "Unknown response: %s" % response
1838 except pexpect.TIMEOUT:
1839 main.log.exception( self.name + ": Timeout exception in "
1840 "setIpTables function" )
1841 return 'ERROR', "Pexpect Timeout"
1842 except pexpect.EOF:
1843 main.log.error( self.name + ": EOF exception found" )
1844 main.log.error( self.name + ": " + self.handle.before )
1845 main.cleanup()
1846 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001847 except Exception:
1848 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001849 main.cleanup()
1850 main.exit()
1851
andrew@onlab.us3b087132015-03-11 15:00:08 -07001852 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1853 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001854 Create/formats the LinkGraph.cfg file based on arguments
1855 -only creates a linear topology and connects islands
1856 -evenly distributes devices
andrew@onlab.us3b087132015-03-11 15:00:08 -07001857 -must be called by ONOSbench
1858
Jon Hall4ba53f02015-07-29 13:07:41 -07001859 ONOSIpList - list of all of the node IPs to be used
1860
1861 deviceCount - number of switches to be assigned
andrew@onlab.us3b087132015-03-11 15:00:08 -07001862 '''
1863 main.log.step("Creating link graph configuration file." )
1864 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
Jon Hall4ba53f02015-07-29 13:07:41 -07001865 tempFile = "/tmp/linkGraph.cfg"
andrew@onlab.us3b087132015-03-11 15:00:08 -07001866
1867 linkGraph = open(tempFile, 'w+')
1868 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1869 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1870 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001871
andrew@onlab.us3b087132015-03-11 15:00:08 -07001872 clusterCount = len(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07001873
1874 if type(deviceCount) is int or type(deviceCount) is str:
andrew@onlab.us3b087132015-03-11 15:00:08 -07001875 deviceCount = int(deviceCount)
1876 switchList = [0]*(clusterCount+1)
1877 baselineSwitchCount = deviceCount/clusterCount
Jon Hall4ba53f02015-07-29 13:07:41 -07001878
andrew@onlab.us3b087132015-03-11 15:00:08 -07001879 for node in range(1, clusterCount + 1):
1880 switchList[node] = baselineSwitchCount
1881
1882 for node in range(1, (deviceCount%clusterCount)+1):
1883 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001884
andrew@onlab.us3b087132015-03-11 15:00:08 -07001885 if type(deviceCount) is list:
1886 main.log.info("Using provided device distribution")
1887 switchList = [0]
1888 for i in deviceCount:
1889 switchList.append(int(i))
1890
1891 tempList = ['0']
1892 tempList.extend(ONOSIpList)
1893 ONOSIpList = tempList
1894
1895 myPort = 6
1896 lastSwitch = 0
1897 for node in range(1, clusterCount+1):
1898 if switchList[node] == 0:
1899 continue
1900
1901 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001902
andrew@onlab.us3b087132015-03-11 15:00:08 -07001903 if node > 1:
1904 #connect to last device on previous node
Jon Hall4ba53f02015-07-29 13:07:41 -07001905 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
1906 linkGraph.write(line)
1907
1908 lastSwitch = 0
1909 for switch in range (0, switchList[node]-1):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001910 line = ""
1911 line = ("\t" + str(switch) + ":" + str(myPort))
1912 line += " -- "
1913 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
1914 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07001915 lastSwitch = switch+1
andrew@onlab.us3b087132015-03-11 15:00:08 -07001916 lastIp = ONOSIpList[node]
Jon Hall4ba53f02015-07-29 13:07:41 -07001917
andrew@onlab.us3b087132015-03-11 15:00:08 -07001918 #lastSwitch += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001919 if node < (clusterCount):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001920 #connect to first device on the next node
Jon Hall4ba53f02015-07-29 13:07:41 -07001921 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
andrew@onlab.us3b087132015-03-11 15:00:08 -07001922 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07001923
andrew@onlab.us3b087132015-03-11 15:00:08 -07001924 linkGraph.write("}\n")
1925 linkGraph.close()
1926
1927 #SCP
Jon Hall4ba53f02015-07-29 13:07:41 -07001928 os.system( "scp " + tempFile + " " + self.user_name + "@" + benchIp + ":" + linkGraphPath)
andrew@onlab.us3b087132015-03-11 15:00:08 -07001929 main.log.info("linkGraph.cfg creation complete")
1930
cameron@onlab.us75900962015-03-30 13:22:49 -07001931 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
Jon Hall4ba53f02015-07-29 13:07:41 -07001932
andrew@onlab.us3b087132015-03-11 15:00:08 -07001933 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001934 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
1935 deviceCount = number of switches to distribute, or list of values to use as custom distribution
cameron@onlab.us75900962015-03-30 13:22:49 -07001936 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 -07001937 '''
1938
cameron@onlab.us75900962015-03-30 13:22:49 -07001939 main.log.step("Configuring Null Device Provider" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001940 clusterCount = len(ONOSIpList)
1941
Jon Hall4ba53f02015-07-29 13:07:41 -07001942 try:
1943
cameron@onlab.us75900962015-03-30 13:22:49 -07001944 if type(deviceCount) is int or type(deviceCount) is str:
1945 main.log.step("Creating device distribution")
1946 deviceCount = int(deviceCount)
1947 switchList = [0]*(clusterCount+1)
1948 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001949
cameron@onlab.us75900962015-03-30 13:22:49 -07001950 for node in range(1, clusterCount + 1):
1951 switchList[node] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001952
cameron@onlab.us75900962015-03-30 13:22:49 -07001953 for node in range(1, (deviceCount%clusterCount)+1):
1954 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001955
1956 if type(deviceCount) is list:
1957 main.log.info("Using provided device distribution")
1958
1959 if len(deviceCount) == clusterCount:
cameron@onlab.us75900962015-03-30 13:22:49 -07001960 switchList = ['0']
1961 switchList.extend(deviceCount)
Jon Hall4ba53f02015-07-29 13:07:41 -07001962
1963 if len(deviceCount) == (clusterCount + 1):
1964 if deviceCount[0] == '0' or deviceCount[0] == 0:
cameron@onlab.us75900962015-03-30 13:22:49 -07001965 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001966
cameron@onlab.us75900962015-03-30 13:22:49 -07001967 assert len(switchList) == (clusterCount + 1)
Jon Hall4ba53f02015-07-29 13:07:41 -07001968
cameron@onlab.us75900962015-03-30 13:22:49 -07001969 except AssertionError:
Jon Hall4ba53f02015-07-29 13:07:41 -07001970 main.log.error( "Bad device/Ip list match")
cameron@onlab.us75900962015-03-30 13:22:49 -07001971 except TypeError:
1972 main.log.exception( self.name + ": Object not as expected" )
1973 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07001974 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07001975 main.log.exception( self.name + ": Uncaught exception!" )
1976 main.cleanup()
1977 main.exit()
1978
andrew@onlab.us3b087132015-03-11 15:00:08 -07001979
1980 ONOSIp = [0]
1981 ONOSIp.extend(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07001982
andrew@onlab.us3b087132015-03-11 15:00:08 -07001983 devicesString = "devConfigs = "
1984 for node in range(1, len(ONOSIp)):
1985 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
1986 if node < clusterCount:
1987 devicesString += (",")
Jon Hall4ba53f02015-07-29 13:07:41 -07001988
1989 try:
cameron@onlab.us75900962015-03-30 13:22:49 -07001990 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
1991 self.handle.expect(":~")
1992 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
1993 self.handle.expect(":~")
andrew@onlab.us3b087132015-03-11 15:00:08 -07001994
cameron@onlab.us75900962015-03-30 13:22:49 -07001995 for i in range(10):
1996 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
1997 self.handle.expect(":~")
1998 verification = self.handle.before
1999 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
2000 break
2001 else:
2002 time.sleep(1)
andrew@onlab.us3b087132015-03-11 15:00:08 -07002003
cameron@onlab.us2cc8bf12015-04-02 14:12:30 -07002004 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002005
cameron@onlab.us75900962015-03-30 13:22:49 -07002006 except AssertionError:
2007 main.log.error("Incorrect Config settings: " + verification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002008 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002009 main.log.exception( self.name + ": Uncaught exception!" )
2010 main.cleanup()
2011 main.exit()
2012
Jon Hall4ba53f02015-07-29 13:07:41 -07002013 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002014 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002015 fileName default is currently the same as the default on ONOS, specify alternate file if
cameron@onlab.us75900962015-03-30 13:22:49 -07002016 you want to use a different topology file than linkGraph.cfg
andrew@onlab.us3b087132015-03-11 15:00:08 -07002017 '''
2018
Jon Hall4ba53f02015-07-29 13:07:41 -07002019
2020 try:
2021 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
2022 self.handle.expect(":~")
cameron@onlab.us75900962015-03-30 13:22:49 -07002023 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2024 self.handle.expect(":~")
Jon Hall4ba53f02015-07-29 13:07:41 -07002025
2026 for i in range(10):
2027 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
cameron@onlab.us75900962015-03-30 13:22:49 -07002028 self.handle.expect(":~")
2029 verification = self.handle.before
Jon Hall4ba53f02015-07-29 13:07:41 -07002030 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002031 break
Jon Hall4ba53f02015-07-29 13:07:41 -07002032 else:
cameron@onlab.us75900962015-03-30 13:22:49 -07002033 time.sleep(1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002034
cameron@onlab.us75900962015-03-30 13:22:49 -07002035 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002036
cameron@onlab.us75900962015-03-30 13:22:49 -07002037 except pexpect.EOF:
2038 main.log.error( self.name + ": EOF exception found" )
2039 main.log.error( self.name + ": " + self.handle.before )
2040 main.cleanup()
2041 main.exit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002042 except AssertionError:
2043 main.log.info("Settings did not post to ONOS")
Jon Hall4ba53f02015-07-29 13:07:41 -07002044 main.log.error(varification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002045 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002046 main.log.exception( self.name + ": Uncaught exception!" )
2047 main.log.error(varification)
2048 main.cleanup()
2049 main.exit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002050
kelvin-onlaba4074292015-07-09 15:19:49 -07002051 def getOnosIps( self ):
2052 """
2053 Get all onos IPs stored in
2054 """
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002055
kelvin-onlaba4074292015-07-09 15:19:49 -07002056 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002057
kelvin-onlaba4074292015-07-09 15:19:49 -07002058 def logReport( self, nodeIp, searchTerms, outputMode="s" ):
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002059 '''
2060 - accepts either a list or a string for "searchTerms" these
Jon Hall4ba53f02015-07-29 13:07:41 -07002061 terms will be searched for in the log and have their
2062 instances counted
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002063
Jon Hall4ba53f02015-07-29 13:07:41 -07002064 - nodeIp is the ip of the node whos log is to be scanned
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002065
Jon Hall4ba53f02015-07-29 13:07:41 -07002066 - output modes:
2067 "s" - Simple. Quiet output mode that just prints
2068 the occurences of each search term
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002069
cameron@onlab.us2e166212015-05-19 14:28:25 -07002070 "d" - Detailed. Prints number of occurences as well as the entire
Jon Hall4ba53f02015-07-29 13:07:41 -07002071 line for each of the last 5 occurences
cameron@onlab.us2e166212015-05-19 14:28:25 -07002072
2073 - returns total of the number of instances of all search terms
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002074 '''
2075 main.log.info("========================== Log Report ===========================\n")
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002076
Jon Hall4ba53f02015-07-29 13:07:41 -07002077 if type(searchTerms) is str:
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002078 searchTerms = [searchTerms]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002079
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002080 logLines = [ [" "] for i in range(len(searchTerms)) ]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002081
Jon Hall4ba53f02015-07-29 13:07:41 -07002082 for term in range(len(searchTerms)):
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002083 logLines[term][0] = searchTerms[term]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002084
Jon Hall4ba53f02015-07-29 13:07:41 -07002085 totalHits = 0
2086 for term in range(len(searchTerms)):
2087 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep " + searchTerms[term]
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002088 self.handle.sendline(cmd)
2089 self.handle.expect(":~")
2090 before = (self.handle.before).splitlines()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002091
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002092 count = [searchTerms[term],0]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002093
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002094 for line in before:
2095 if searchTerms[term] in line and "grep" not in line:
Jon Hall4ba53f02015-07-29 13:07:41 -07002096 count[1] += 1
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002097 if before.index(line) > ( len(before) - 7 ):
2098 logLines[term].append(line)
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002099
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002100 main.log.info( str(count[0]) + ": " + str(count[1]) )
Jon Hall4ba53f02015-07-29 13:07:41 -07002101 if term == len(searchTerms)-1:
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002102 print("\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07002103 totalHits += int(count[1])
cameron@onlab.us2e166212015-05-19 14:28:25 -07002104
Jon Hall4ba53f02015-07-29 13:07:41 -07002105 if outputMode != "s" and outputMode != "S":
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002106 outputString = ""
2107 for i in logLines:
Jon Hall4ba53f02015-07-29 13:07:41 -07002108 outputString = i[0] + ": \n"
2109 for x in range(1,len(i)):
2110 outputString += ( i[x] + "\n" )
2111
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002112 if outputString != (i[0] + ": \n"):
Jon Hall4ba53f02015-07-29 13:07:41 -07002113 main.log.info(outputString)
2114
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002115 main.log.info("================================================================\n")
Hari Krishnabe4b97b2015-07-15 12:19:43 -07002116 return totalHits
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002117
2118 def copyMininetFile( self, fileName, localPath, userName, ip,
2119 mnPath='~/mininet/custom/', timeout = 60 ):
2120 """
2121 Description:
2122 Copy mininet topology file from dependency folder in the test folder
2123 and paste it to the mininet machine's mininet/custom folder
2124 Required:
2125 fileName - Name of the topology file to copy
2126 localPath - File path of the mininet topology file
2127 userName - User name of the mininet machine to send the file to
2128 ip - Ip address of the mininet machine
2129 Optional:
2130 mnPath - of the mininet directory to send the file to
2131 Return:
2132 Return main.TRUE if successfully copied the file otherwise
2133 return main.FALSE
2134 """
2135
2136 try:
2137 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2138 str( ip ) + ":" + mnPath + fileName
2139
2140 self.handle.sendline( "" )
2141 self.handle.expect( "\$" )
2142
2143 main.log.info( self.name + ": Execute: " + cmd )
2144
2145 self.handle.sendline( cmd )
2146
2147 i = self.handle.expect( [ 'No such file',
2148 "100%",
2149 pexpect.TIMEOUT ] )
2150
2151 if i == 0:
2152 main.log.error( self.name + ": File " + fileName +
2153 " does not exist!" )
2154 return main.FALSE
2155
2156 if i == 1:
2157 main.log.info( self.name + ": File " + fileName +
2158 " has been copied!" )
2159 self.handle.sendline( "" )
2160 self.handle.expect( "\$" )
2161 return main.TRUE
2162
2163 except pexpect.EOF:
2164 main.log.error( self.name + ": EOF exception found" )
2165 main.log.error( self.name + ": " + self.handle.before )
2166 main.cleanup()
2167 main.exit()
2168 except pexpect.TIMEOUT:
2169 main.log.error( self.name + ": TIMEOUT exception found" )
2170 main.log.error( self.name + ": " + self.handle.before )
2171 main.cleanup()
2172 main.exit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002173
2174 def jvmSet(self, memory=8):
Jon Hall4ba53f02015-07-29 13:07:41 -07002175
cameron@onlab.us78b89652015-07-08 15:21:03 -07002176 import os
2177
2178 homeDir = os.path.expanduser('~')
2179 filename = "/onos/tools/package/bin/onos-service"
2180
2181 serviceConfig = open(homeDir + filename, 'w+')
2182 serviceConfig.write("#!/bin/bash\n ")
2183 serviceConfig.write("#------------------------------------- \n ")
2184 serviceConfig.write("# Starts ONOS Apache Karaf container\n ")
2185 serviceConfig.write("#------------------------------------- \n ")
2186 serviceConfig.write("#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n ")
2187 serviceConfig.write("""export JAVA_OPTS="${JAVA_OPTS:--Xms""" + str(memory) + "G -Xmx" + str(memory) + """G}" \n """)
2188 serviceConfig.write("[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n")
2189 serviceConfig.write("""${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """)
2190 serviceConfig.close()
2191
2192 def createDBFile(self, testData):
Jon Hall4ba53f02015-07-29 13:07:41 -07002193
cameron@onlab.us78b89652015-07-08 15:21:03 -07002194 filename = main.TEST + "DB"
2195 DBString = ""
Jon Hall4ba53f02015-07-29 13:07:41 -07002196
cameron@onlab.us78b89652015-07-08 15:21:03 -07002197 for item in testData:
Jon Hall4ba53f02015-07-29 13:07:41 -07002198 if type(item) is string:
cameron@onlab.us78b89652015-07-08 15:21:03 -07002199 item = "'" + item + "'"
2200 if testData.index(item) < len(testData-1):
2201 item += ","
Jon Hall4ba53f02015-07-29 13:07:41 -07002202 DBString += str(item)
cameron@onlab.us78b89652015-07-08 15:21:03 -07002203
2204 DBFile = open(filename, "a")
2205 DBFile.write(DBString)
2206 DBFile.close()
2207
Jon Hall4ba53f02015-07-29 13:07:41 -07002208 def verifySummary(self, ONOSIp,*deviceCount):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002209
2210 self.handle.sendline("onos " + ONOSIp + " summary")
2211 self.handle.expect(":~")
2212
2213 summaryStr = self.handle.before
2214 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2215
2216 #passed = "SCC(s)=1" in summaryStr
2217 #if deviceCount:
2218 # passed = "devices=" + str(deviceCount) + "," not in summaryStr
2219
GlennRC772363b2015-08-25 13:05:57 -07002220 passed = False
cameron@onlab.us78b89652015-07-08 15:21:03 -07002221 if "SCC(s)=1," in summaryStr:
2222 passed = True
2223 print("Summary is verifed")
Jon Hall4ba53f02015-07-29 13:07:41 -07002224 else:
2225 print("Summary failed")
cameron@onlab.us78b89652015-07-08 15:21:03 -07002226
2227 if deviceCount:
2228 print" ============================="
2229 checkStr = "devices=" + str(deviceCount[0]) + ","
2230 print "Checkstr: " + checkStr
2231 if checkStr not in summaryStr:
2232 passed = False
Jon Hall4ba53f02015-07-29 13:07:41 -07002233 print("Device count failed")
2234 else:
2235 print "device count verified"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002236
2237 return passed