blob: 935f627779d00664604cfe19bbcf1614faeef27a [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,
Flavio Castrocc38a542016-03-03 13:15:46 -0800674 appString, onosIpAddrs, onosUser="sdn" ):
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
Flavio Castrocc38a542016-03-03 13:15:46 -0800686 * ONOS USER (onosUser)
687 - optional argument to set ONOS_USER environment variable
kelvin8ec71442015-01-15 16:57:00 -0800688
andrewonlab94282092014-10-10 13:00:11 -0400689 NOTE: Assumes cells are located at:
690 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800691 """
692 # Variable initialization
Jon Hall6801cda2015-07-15 14:13:45 -0700693 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800694 # We want to create the cell file in the dependencies directory
695 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800696 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800697 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800698 cellFile = open( tempDirectory + fileName, 'w+' )
Jon Hall6801cda2015-07-15 14:13:45 -0700699 if isinstance( onosIpAddrs, types.StringType ):
700 onosIpAddrs = [ onosIpAddrs ]
kelvin8ec71442015-01-15 16:57:00 -0800701
cameron@onlab.us75900962015-03-30 13:22:49 -0700702 # App string is hardcoded environment variables
kelvin8ec71442015-01-15 16:57:00 -0800703 # That you may wish to use by default on startup.
cameron@onlab.us75900962015-03-30 13:22:49 -0700704 # Note that you may not want certain apps listed
kelvin8ec71442015-01-15 16:57:00 -0800705 # on here.
cameron@onlab.us75900962015-03-30 13:22:49 -0700706 appString = "export ONOS_APPS=" + appString
Flavio Castrocc38a542016-03-03 13:15:46 -0800707 onosGroup = "export ONOS_GROUP=" + onosUser
708 onosUser = "export ONOS_USER=" + onosUser
kelvin-onlabd3b64892015-01-20 13:26:24 -0800709 mnString = "export OCN="
kelvin-onlabf70fd542015-05-07 18:41:40 -0700710 if mnIpAddrs == "":
711 mnString = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800712 onosString = "export OC"
713 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800714
kelvin-onlabd3b64892015-01-20 13:26:24 -0800715 # Create ONOSNIC ip address prefix
kelvin-onlaba4074292015-07-09 15:19:49 -0700716 tempOnosIp = str( onosIpAddrs[ 0 ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800717 tempList = []
718 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800719 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800720 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800721 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800722 nicAddr = ".".join( tempList ) + ".*"
723 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400724
725 try:
kelvin8ec71442015-01-15 16:57:00 -0800726 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800727 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400728
kelvin-onlabd3b64892015-01-20 13:26:24 -0800729 for arg in onosIpAddrs:
730 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800731 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400732 # export OC1="10.128.20.11"
733 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800734 cellFile.write( onosString + str( tempCount ) +
Jon Hall6f665652015-09-18 10:08:07 -0700735 "=\"" + arg + "\"\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800736 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800737
Jon Hall6f665652015-09-18 10:08:07 -0700738 cellFile.write( "export OCI=$OC1\n" )
739 cellFile.write( mnString + "\"" + mnIpAddrs + "\"\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700740 cellFile.write( appString + "\n" )
Flavio Castrocc38a542016-03-03 13:15:46 -0800741 cellFile.write( onosGroup + "\n" )
742 cellFile.write( onosUser + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800743 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400744
kelvin8ec71442015-01-15 16:57:00 -0800745 # We use os.system to send the command to TestON cluster
746 # to account for the case in which TestON is not located
747 # on the same cluster as the ONOS bench
748 # Note that even if TestON is located on the same cluster
749 # as ONOS bench, you must setup passwordless ssh
750 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabc2b79102015-07-14 11:41:20 -0700751 os.system( "scp " + tempDirectory + fileName + " " +
752 self.user_name + "@" + self.ip_address + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400753
andrewonlab2a6c9342014-10-16 13:40:15 -0400754 return main.TRUE
755
andrewonlab94282092014-10-10 13:00:11 -0400756 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800757 main.log.error( self.name + ": EOF exception found" )
758 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400759 main.cleanup()
760 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800761 except Exception:
762 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400763 main.cleanup()
764 main.exit()
765
kelvin-onlabd3b64892015-01-20 13:26:24 -0800766 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800767 """
andrewonlab95ca1462014-10-09 14:04:24 -0400768 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800769 """
Hari Krishna03f530e2015-07-10 17:28:27 -0700770 import re
andrewonlab95ca1462014-10-09 14:04:24 -0400771 try:
772 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800773 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400774 main.cleanup()
775 main.exit()
776 else:
kelvin8ec71442015-01-15 16:57:00 -0800777 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800778 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800779 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400780 # and that this driver will have to change accordingly
Jon Hall3b489db2015-10-05 14:38:37 -0700781 self.handle.expect( str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800782 handleBefore = self.handle.before
783 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800784 # Get the rest of the handle
Jon Hall3b489db2015-10-05 14:38:37 -0700785 self.handle.expect( "\$" )
Jon Hall439c8912016-04-15 02:22:03 -0700786 time.sleep(10)
kelvin-onlabd3b64892015-01-20 13:26:24 -0800787 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400788
Hari Krishna03f530e2015-07-10 17:28:27 -0700789 cell_result = handleBefore + handleAfter + handleMore
790 print cell_result
791 if( re.search( "No such cell", cell_result ) ):
792 main.log.error( "Cell call returned: " + handleBefore +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800793 handleAfter + handleMore )
Hari Krishna03f530e2015-07-10 17:28:27 -0700794 main.cleanup()
795 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400796 return main.TRUE
andrewonlab95ca1462014-10-09 14:04:24 -0400797 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800798 main.log.error( self.name + ": EOF exception found" )
799 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400800 main.cleanup()
801 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800802 except Exception:
803 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400804 main.cleanup()
805 main.exit()
806
kelvin-onlabd3b64892015-01-20 13:26:24 -0800807 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800808 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400809 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800810 """
811 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400812
andrewonlabc03bf6c2014-10-09 14:56:18 -0400813 try:
kelvin8ec71442015-01-15 16:57:00 -0800814 # Clean handle by sending empty and expecting $
815 self.handle.sendline( "" )
816 self.handle.expect( "\$" )
817 self.handle.sendline( "onos-verify-cell" )
818 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800819 handleBefore = self.handle.before
820 handleAfter = self.handle.after
kelvin-onlabd3b64892015-01-20 13:26:24 -0800821 main.log.info( "Verify cell returned: " + handleBefore +
Jon Hall3b489db2015-10-05 14:38:37 -0700822 handleAfter )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400823 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800824 except pexpect.ExceptionPexpect as e:
Jon Hall3b489db2015-10-05 14:38:37 -0700825 main.log.exception( self.name + ": Pexpect exception found: " )
kelvin8ec71442015-01-15 16:57:00 -0800826 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400827 main.cleanup()
828 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800829 except Exception:
830 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400831 main.cleanup()
832 main.exit()
833
jenkins1e99e7b2015-04-02 18:15:39 -0700834 def onosCfgSet( self, ONOSIp, configName, configParam ):
835 """
836 Uses 'onos <node-ip> cfg set' to change a parameter value of an
Jon Hall4ba53f02015-07-29 13:07:41 -0700837 application.
838
jenkins1e99e7b2015-04-02 18:15:39 -0700839 ex)
840 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
jenkins1e99e7b2015-04-02 18:15:39 -0700841 ONOSIp = '10.0.0.1'
842 configName = 'org.onosproject.myapp'
843 configParam = 'appSetting 1'
jenkins1e99e7b2015-04-02 18:15:39 -0700844 """
Jon Hall72280bc2016-01-25 14:29:05 -0800845 try:
846 cfgStr = "onos {} cfg set {} {}".format( ONOSIp,
847 configName,
848 configParam )
849 self.handle.sendline( "" )
850 self.handle.expect( ":~" )
851 self.handle.sendline( cfgStr )
852 self.handle.expect("cfg set")
853 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700854
Jon Hall72280bc2016-01-25 14:29:05 -0800855 paramValue = configParam.split(" ")[1]
856 paramName = configParam.split(" ")[0]
Jon Hall4ba53f02015-07-29 13:07:41 -0700857
Jon Hall72280bc2016-01-25 14:29:05 -0800858 checkStr = 'onos {} cfg get " {} {} " '.format( ONOSIp, configName, paramName )
Jon Hall4ba53f02015-07-29 13:07:41 -0700859
Jon Hall72280bc2016-01-25 14:29:05 -0800860 self.handle.sendline( checkStr )
861 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700862
Jon Hall72280bc2016-01-25 14:29:05 -0800863 if "value=" + paramValue + "," in self.handle.before:
864 main.log.info("cfg " + configName + " successfully set to " + configParam)
865 return main.TRUE
866 except pexpect.ExceptionPexpect as e:
867 main.log.exception( self.name + ": Pexpect exception found: " )
868 main.log.error( self.name + ": " + self.handle.before )
869 main.cleanup()
870 main.exit()
871 except Exception:
872 main.log.exception( self.name + ": Uncaught exception!" )
873 main.cleanup()
874 main.exit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700875
kelvin-onlabd3b64892015-01-20 13:26:24 -0800876 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800877 """
andrewonlab05e362f2014-10-10 00:40:57 -0400878 Uses 'onos' command to send various ONOS CLI arguments.
879 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800880 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400881 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800882
883 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400884 CLI commands for ONOS. Try to use this function first
885 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800886 function.
887 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400888 by starting onos, and typing in 'onos' to enter the
889 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800890 available commands.
891 """
andrewonlab05e362f2014-10-10 00:40:57 -0400892 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800893 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800894 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400895 return main.FALSE
896 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800897 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400898 return main.FALSE
899
kelvin8ec71442015-01-15 16:57:00 -0800900 cmdstr = str( cmdstr )
901 self.handle.sendline( "" )
902 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400903
kelvin-onlabd3b64892015-01-20 13:26:24 -0800904 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800905 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400906
kelvin-onlabd3b64892015-01-20 13:26:24 -0800907 handleBefore = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800908 main.log.info( "Command sent successfully" )
kelvin8ec71442015-01-15 16:57:00 -0800909 # Obtain return handle that consists of result from
910 # the onos command. The string may need to be
911 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800912 returnString = handleBefore
kelvin-onlabd3b64892015-01-20 13:26:24 -0800913 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400914 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800915 main.log.error( self.name + ": EOF exception found" )
916 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400917 main.cleanup()
918 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800919 except Exception:
920 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400921 main.cleanup()
922 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400923
kelvin-onlabd3b64892015-01-20 13:26:24 -0800924 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800925 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400926 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800927 If -f option is provided, it also forces an uninstall.
928 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400929 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800930 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400931 files to certain onos nodes
932
933 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800934 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400935 try:
andrewonlab114768a2014-11-14 12:44:44 -0500936 if options:
kelvin8ec71442015-01-15 16:57:00 -0800937 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500938 else:
kelvin8ec71442015-01-15 16:57:00 -0800939 self.handle.sendline( "onos-install " + node )
940 self.handle.expect( "onos-install " )
941 # NOTE: this timeout may need to change depending on the network
942 # and size of ONOS
943 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800944 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800945 "ONOS\sis\salready\sinstalled",
Jeremyc72b2582016-02-26 18:27:38 -0800946 "already\sup-to-date",
947 "\$",
kelvin8ec71442015-01-15 16:57:00 -0800948 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400949 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800950 main.log.warn( "Network is unreachable" )
Jon Hall3b489db2015-10-05 14:38:37 -0700951 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400952 return main.FALSE
953 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800954 main.log.info(
955 "ONOS was installed on " +
956 node +
957 " and started" )
Jon Hall3b489db2015-10-05 14:38:37 -0700958 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400959 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500960 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800961 main.log.info( "ONOS is already installed on " + node )
Jon Hall3b489db2015-10-05 14:38:37 -0700962 self.handle.expect( "\$" )
andrewonlabd9a73a72014-11-14 17:28:21 -0500963 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800964 elif i == 3:
Jeremyc72b2582016-02-26 18:27:38 -0800965 main.log.info( "ONOS is already installed on " + node )
966 self.handle.expect( "\$" )
967 return main.TRUE
968 elif i == 4:
969 main.log.info( "ONOS was installed on " + node )
970 return main.TRUE
971 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800972 main.log.info(
973 "Installation of ONOS on " +
974 node +
975 " timed out" )
Jon Hall3b489db2015-10-05 14:38:37 -0700976 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400977 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400978 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800979 main.log.error( self.name + ": EOF exception found" )
980 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400981 main.cleanup()
982 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800983 except Exception:
984 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400985 main.cleanup()
986 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400987
kelvin-onlabd3b64892015-01-20 13:26:24 -0800988 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800989 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400990 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400991 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800992 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400993 try:
kelvin8ec71442015-01-15 16:57:00 -0800994 self.handle.sendline( "" )
995 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800996 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800997 " start" )
998 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -0400999 "Job\sis\salready\srunning",
1000 "start/running",
Jeremyd0e9a6d2016-03-02 11:28:52 -08001001 "\$",
andrewonlab8d0d7d72014-10-09 16:33:15 -04001002 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -08001003 pexpect.TIMEOUT ], timeout=120 )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001004 if i == 0:
Jon Halleab7a242016-03-04 10:20:43 -08001005 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001006 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001007 return main.TRUE
1008 elif i == 1:
Jon Halleab7a242016-03-04 10:20:43 -08001009 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001010 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001011 return main.TRUE
Jeremyd0e9a6d2016-03-02 11:28:52 -08001012 elif i == 2:
1013 main.log.info( "ONOS service started" )
1014 return main.TRUE
andrewonlab8d0d7d72014-10-09 16:33:15 -04001015 else:
Jon Halleab7a242016-03-04 10:20:43 -08001016 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001017 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001018 main.cleanup()
1019 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001020 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001021 main.log.error( self.name + ": EOF exception found" )
1022 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001023 main.cleanup()
1024 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001025 except Exception:
1026 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001027 main.cleanup()
1028 main.exit()
1029
kelvin-onlabd3b64892015-01-20 13:26:24 -08001030 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001031 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001032 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001033 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001034 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001035 try:
kelvin8ec71442015-01-15 16:57:00 -08001036 self.handle.sendline( "" )
1037 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001038 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001039 " stop" )
1040 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -04001041 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -07001042 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -04001043 "Unknown\sinstance",
YPZhang77badfc2016-03-09 10:28:59 -08001044 "\$",
kelvin8ec71442015-01-15 16:57:00 -08001045 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2b30bd32014-10-09 16:48:55 -04001046 if i == 0:
YPZhang77badfc2016-03-09 10:28:59 -08001047 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001048 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001049 return main.TRUE
1050 elif i == 1:
YPZhang77badfc2016-03-09 10:28:59 -08001051 self.handle.expect( "\$" )
Jon Hall65844a32015-03-09 19:09:37 -07001052 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001053 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -04001054 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -07001055 elif i == 2:
YPZhang77badfc2016-03-09 10:28:59 -08001056 self.handle.expect( "\$" )
Jon Hall61282e32015-03-19 11:34:11 -07001057 main.log.warn( "ONOS wasn't running" )
1058 return main.TRUE
YPZhang77badfc2016-03-09 10:28:59 -08001059 elif i == 3:
1060 main.log.info( "ONOS service stopped" )
1061 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -04001062 else:
kelvin8ec71442015-01-15 16:57:00 -08001063 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001064 return main.FALSE
andrewonlab2b30bd32014-10-09 16:48:55 -04001065 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001066 main.log.error( self.name + ": EOF exception found" )
1067 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -04001068 main.cleanup()
1069 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001070 except Exception:
1071 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001072 main.cleanup()
1073 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001074
kelvin-onlabd3b64892015-01-20 13:26:24 -08001075 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -08001076 """
andrewonlabc8d47972014-10-09 16:52:36 -04001077 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -08001078 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -04001079 if needed
kelvin8ec71442015-01-15 16:57:00 -08001080 """
andrewonlabc8d47972014-10-09 16:52:36 -04001081 try:
kelvin8ec71442015-01-15 16:57:00 -08001082 self.handle.sendline( "" )
pingping-lin763ee042015-05-20 17:45:30 -07001083 self.handle.expect( "\$", timeout=60 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001084 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
Jon Hall3b489db2015-10-05 14:38:37 -07001085 self.handle.expect( "\$", timeout=60 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001086 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
kelvin8ec71442015-01-15 16:57:00 -08001087 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -04001088 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -07001089 except pexpect.TIMEOUT:
1090 main.log.exception( self.name + ": Timeout in onosUninstall" )
1091 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -04001092 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001093 main.log.error( self.name + ": EOF exception found" )
1094 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -04001095 main.cleanup()
1096 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001097 except Exception:
1098 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -04001099 main.cleanup()
1100 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -04001101
kelvin-onlabd3b64892015-01-20 13:26:24 -08001102 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001103 """
andrewonlabaedc8332014-12-04 12:43:03 -05001104 Issues the command 'onos-die <node-ip>'
1105 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -08001106 """
andrewonlabaedc8332014-12-04 12:43:03 -05001107 try:
kelvin8ec71442015-01-15 16:57:00 -08001108 self.handle.sendline( "" )
1109 self.handle.expect( "\$" )
Jeremyf0aecdb2016-03-30 13:19:57 -07001110 cmdStr = "onos-die " + str( nodeIp )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001111 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001112 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001113 "Killing\sONOS",
1114 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -08001115 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -05001116 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001117 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001118 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -05001119 return main.TRUE
1120 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001121 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -05001122 return main.FALSE
1123 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001124 main.log.error( self.name + ": EOF exception found" )
1125 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -05001126 main.cleanup()
1127 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001128 except Exception:
1129 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -05001130 main.cleanup()
1131 main.exit()
1132
kelvin-onlabd3b64892015-01-20 13:26:24 -08001133 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001134 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001135 Calls the command: 'onos-kill [<node-ip>]'
1136 "Remotely, and unceremoniously kills the ONOS instance running on
1137 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001138 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001139 try:
kelvin8ec71442015-01-15 16:57:00 -08001140 self.handle.sendline( "" )
1141 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001142 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001143 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -04001144 "\$",
1145 "No\sroute\sto\shost",
1146 "password:",
kelvin8ec71442015-01-15 16:57:00 -08001147 pexpect.TIMEOUT ], timeout=20 )
1148
andrewonlabe8e56fd2014-10-09 17:12:44 -04001149 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001150 main.log.info(
1151 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001152 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001153 return main.TRUE
1154 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001155 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001156 return main.FALSE
1157 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001158 main.log.info(
1159 "Passwordless login for host: " +
1160 str( nodeIp ) +
1161 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001162 return main.FALSE
1163 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001164 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001165 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001166
andrewonlabe8e56fd2014-10-09 17:12:44 -04001167 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001168 main.log.error( self.name + ": EOF exception found" )
1169 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001170 main.cleanup()
1171 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001172 except Exception:
1173 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001174 main.cleanup()
1175 main.exit()
1176
kelvin-onlabd3b64892015-01-20 13:26:24 -08001177 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001178 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001179 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001180 a cleaner environment.
1181
andrewonlab19fbdca2014-11-14 12:55:59 -05001182 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001183 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001184 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001185 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001186 try:
kelvin8ec71442015-01-15 16:57:00 -08001187 self.handle.sendline( "" )
1188 self.handle.expect( "\$" )
1189 self.handle.sendline( "onos-remove-raft-logs" )
1190 # Sometimes this command hangs
1191 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1192 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001193 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001194 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1195 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001196 if i == 1:
1197 return main.FALSE
andrewonlab19fbdca2014-11-14 12:55:59 -05001198 return main.TRUE
andrewonlab19fbdca2014-11-14 12:55:59 -05001199 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001200 main.log.error( self.name + ": EOF exception found" )
1201 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001202 main.cleanup()
1203 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001204 except Exception:
1205 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001206 main.cleanup()
1207 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001208
kelvin-onlabd3b64892015-01-20 13:26:24 -08001209 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001210 """
1211 Calls the command 'onos-start-network [ <mininet-topo> ]
1212 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001213 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001214 cell."
andrewonlab94282092014-10-10 13:00:11 -04001215 * Specify mininet topology file name for mntopo
1216 * Topo files should be placed at:
1217 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001218
andrewonlab94282092014-10-10 13:00:11 -04001219 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001220 """
andrewonlab94282092014-10-10 13:00:11 -04001221 try:
1222 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001223 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001224 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001225
kelvin8ec71442015-01-15 16:57:00 -08001226 mntopo = str( mntopo )
1227 self.handle.sendline( "" )
1228 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001229
kelvin8ec71442015-01-15 16:57:00 -08001230 self.handle.sendline( "onos-start-network " + mntopo )
1231 self.handle.expect( "mininet>" )
1232 main.log.info( "Network started, entered mininet prompt" )
1233
1234 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001235
1236 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001237 main.log.error( self.name + ": EOF exception found" )
1238 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001239 main.cleanup()
1240 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001241 except Exception:
1242 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001243 main.cleanup()
1244 main.exit()
1245
Jon Hall3b489db2015-10-05 14:38:37 -07001246 def isup( self, node="", timeout=120 ):
kelvin8ec71442015-01-15 16:57:00 -08001247 """
1248 Run's onos-wait-for-start which only returns once ONOS is at run
Cameron Franke9c94fb02015-01-21 10:20:20 -08001249 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001250
Jon Hall7993bfc2014-10-09 16:30:14 -04001251 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001252 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001253 try:
Jon Hall3b489db2015-10-05 14:38:37 -07001254 self.handle.sendline( "onos-wait-for-start " + node )
1255 self.handle.expect( "onos-wait-for-start" )
kelvin8ec71442015-01-15 16:57:00 -08001256 # NOTE: this timeout is arbitrary"
Cameron Franke9c94fb02015-01-21 10:20:20 -08001257 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001258 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001259 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001260 return main.TRUE
1261 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001262 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001263 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001264 main.log.error( "ONOS has not started yet" )
1265 self.handle.send( "\x03" ) # Control-C
1266 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001267 return main.FALSE
1268 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001269 main.log.error( self.name + ": EOF exception found" )
1270 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001271 main.cleanup()
1272 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001273 except Exception:
1274 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001275 main.cleanup()
1276 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001277
kelvin-onlabd3b64892015-01-20 13:26:24 -08001278 def pushTestIntentsShell(
1279 self,
1280 dpidSrc,
1281 dpidDst,
1282 numIntents,
1283 dirFile,
1284 onosIp,
1285 numMult="",
1286 appId="",
1287 report=True,
1288 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001289 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001290 Description:
kelvin8ec71442015-01-15 16:57:00 -08001291 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001292 better parallelize the results than the CLI
1293 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001294 * dpidSrc: specify source dpid
1295 * dpidDst: specify destination dpid
1296 * numIntents: specify number of intents to push
1297 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001298 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001299 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001300 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001301 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001302 """
1303 try:
1304 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001305 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001306 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001307 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001308 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001309 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001310
kelvin-onlabd3b64892015-01-20 13:26:24 -08001311 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1312 if not numMult:
1313 addIntents = addDpid + " " + str( numIntents )
1314 elif numMult:
1315 addIntents = addDpid + " " + str( numIntents ) + " " +\
1316 str( numMult )
1317 if appId:
1318 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001319 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001320 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001321
andrewonlabaedc8332014-12-04 12:43:03 -05001322 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001323 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001324 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001325 sendCmd = addApp + " &"
1326 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001327
kelvin-onlabd3b64892015-01-20 13:26:24 -08001328 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001329
1330 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001331 main.log.error( self.name + ": EOF exception found" )
1332 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001333 main.cleanup()
1334 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001335 except Exception:
1336 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001337 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001338 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001339
kelvin-onlabd3b64892015-01-20 13:26:24 -08001340 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001341 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001342 Definition:
1343 Loads a json topology output
1344 Return:
1345 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -08001346 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001347 import json
Jon Hall77f53ce2014-10-13 18:02:06 -04001348 try:
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001349 # either onos:topology or 'topology' will work in CLI
1350 topology = json.loads(topologyOutput)
1351 print topology
Jon Hall77f53ce2014-10-13 18:02:06 -04001352 return topology
1353 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001354 main.log.error( self.name + ": EOF exception found" )
1355 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001356 main.cleanup()
1357 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001358 except Exception:
1359 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001360 main.cleanup()
1361 main.exit()
1362
kelvin-onlabd3b64892015-01-20 13:26:24 -08001363 def checkStatus(
1364 self,
1365 topologyResult,
1366 numoswitch,
1367 numolink,
1368 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001369 """
Jon Hallefbd9792015-03-05 16:11:36 -08001370 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001371 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001372 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08001373
Jon Hall77f53ce2014-10-13 18:02:06 -04001374 Params: ip = ip used for the onos cli
1375 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001376 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001377 logLevel = level to log to.
1378 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001379
1380
kelvin-onlabd3b64892015-01-20 13:26:24 -08001381 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001382
Jon Hallefbd9792015-03-05 16:11:36 -08001383 Returns: main.TRUE if the number of switches and links are correct,
1384 main.FALSE if the number of switches and links is incorrect,
Jon Hall77f53ce2014-10-13 18:02:06 -04001385 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001386 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001387 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001388 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001389 if topology == {}:
1390 return main.ERROR
1391 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001392 # Is the number of switches is what we expected
shahshreya234a1682015-05-27 15:41:56 -07001393 devices = topology.get( 'devices', False )
1394 links = topology.get( 'links', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001395 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001396 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001397 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001398 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001399 linkCheck = ( int( links ) == int( numolink ) )
Jon Hallefbd9792015-03-05 16:11:36 -08001400 if switchCheck and linkCheck:
kelvin8ec71442015-01-15 16:57:00 -08001401 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001402 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001403 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001404 result = main.TRUE
1405 else:
1406 output = output + \
Jon Hall274b6642015-02-17 11:57:17 -08001407 "The number of links and switches does not match " + \
1408 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001409 result = main.FALSE
Jon Hallefbd9792015-03-05 16:11:36 -08001410 output = output + "\n ONOS sees %i devices" % int( devices )
1411 output = output + " (%i expected) " % int( numoswitch )
Jon Hall274b6642015-02-17 11:57:17 -08001412 output = output + "and %i links " % int( links )
1413 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001414 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001415 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001416 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001417 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001418 else:
kelvin8ec71442015-01-15 16:57:00 -08001419 main.log.info( output )
1420 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001421 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001422 main.log.error( self.name + ": EOF exception found" )
1423 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001424 main.cleanup()
1425 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001426 except Exception:
1427 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001428 main.cleanup()
1429 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001430
kelvin-onlabd3b64892015-01-20 13:26:24 -08001431 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001432 """
andrewonlab970399c2014-11-07 13:09:32 -05001433 Capture all packet activity and store in specified
1434 directory/file
1435
1436 Required:
1437 * interface: interface to capture
1438 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001439 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001440 try:
1441 self.handle.sendline( "" )
1442 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001443
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001444 self.handle.sendline( "tshark -i " + str( interface ) + " -t e -w " + str( dirFile ) + " &" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001445 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001446 self.handle.expect( "Capturing on" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001447 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001448 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001449
Jon Hallfebb1c72015-03-05 13:30:09 -08001450 main.log.info( "Tshark started capturing files on " +
1451 str( interface ) + " and saving to directory: " +
1452 str( dirFile ) )
1453 except pexpect.EOF:
1454 main.log.error( self.name + ": EOF exception found" )
1455 main.log.error( self.name + ": " + self.handle.before )
1456 main.cleanup()
1457 main.exit()
1458 except Exception:
1459 main.log.exception( self.name + ": Uncaught exception!" )
1460 main.cleanup()
1461 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001462
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001463 def onosTopoCfg( self, onosIp, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001464 """
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001465 Description:
1466 Execute onos-topo-cfg command
1467 Required:
1468 onosIp - IP of the onos node you want to send the json to
1469 jsonFile - File path of the json file
1470 Return:
1471 Returns main.TRUE if the command is successfull; Returns
1472 main.FALSE if there was an error
kelvin8ec71442015-01-15 16:57:00 -08001473 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001474 try:
kelvin8ec71442015-01-15 16:57:00 -08001475 self.handle.sendline( "" )
1476 self.handle.expect( "\$" )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001477 cmd = "onos-topo-cfg "
1478 self.handle.sendline( cmd + str( onosIp ) + " " + jsonFile )
1479 handle = self.handle.before
1480 print handle
1481 if "Error" in handle:
1482 main.log.error( self.name + ": " + self.handle.before )
1483 return main.FALSE
1484 else:
1485 self.handle.expect( "\$" )
1486 return main.TRUE
1487
Jon Hallfebb1c72015-03-05 13:30:09 -08001488 except pexpect.EOF:
1489 main.log.error( self.name + ": EOF exception found" )
1490 main.log.error( self.name + ": " + self.handle.before )
1491 main.cleanup()
1492 main.exit()
1493 except Exception:
1494 main.log.exception( self.name + ": Uncaught exception!" )
1495 main.cleanup()
1496 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001497
jenkins1e99e7b2015-04-02 18:15:39 -07001498 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001499 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001500 Required:
kelvin8ec71442015-01-15 16:57:00 -08001501 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001502 * directory to store results
1503 Optional:
1504 * interface - default: eth0
Jon Hall4ba53f02015-07-29 13:07:41 -07001505 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001506 Description:
1507 Uses tshark command to grep specific group of packets
1508 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001509 The timestamp is hardcoded to be in epoch
1510 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001511 try:
1512 self.handle.sendline( "" )
1513 self.handle.expect( "\$" )
1514 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001515 if grepOptions:
1516 grepStr = "grep "+str(grepOptions)
1517 else:
1518 grepStr = "grep"
Jon Hall4ba53f02015-07-29 13:07:41 -07001519
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001520 cmd = (
1521 "sudo tshark -i " +
Jon Hallfebb1c72015-03-05 13:30:09 -08001522 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001523 " -t e | " +
1524 grepStr + " --line-buffered \"" +
Jon Hallfebb1c72015-03-05 13:30:09 -08001525 str(grep) +
1526 "\" >" +
1527 directory +
1528 " &" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001529 self.handle.sendline(cmd)
1530 main.log.info(cmd)
Jon Hallfebb1c72015-03-05 13:30:09 -08001531 self.handle.expect( "Capturing on" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001532 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001533 self.handle.expect( "\$" )
1534 except pexpect.EOF:
1535 main.log.error( self.name + ": EOF exception found" )
1536 main.log.error( self.name + ": " + self.handle.before )
1537 main.cleanup()
1538 main.exit()
1539 except Exception:
1540 main.log.exception( self.name + ": Uncaught exception!" )
1541 main.cleanup()
1542 main.exit()
1543
kelvin-onlabd3b64892015-01-20 13:26:24 -08001544 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001545 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001546 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001547 """
1548 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001549 try:
1550 self.execute( cmd="sudo rm /tmp/wireshark*" )
1551 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001552 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1553 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001554 self.handle.sendline( "" )
1555 main.log.info( "Tshark stopped" )
1556 except pexpect.EOF:
1557 main.log.error( self.name + ": EOF exception found" )
1558 main.log.error( self.name + ": " + self.handle.before )
1559 main.cleanup()
1560 main.exit()
1561 except Exception:
1562 main.log.exception( self.name + ": Uncaught exception!" )
1563 main.cleanup()
1564 main.exit()
1565
kelvin8ec71442015-01-15 16:57:00 -08001566 def ptpd( self, args ):
1567 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001568 Initiate ptp with user-specified args.
1569 Required:
1570 * args: specify string of args after command
1571 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001572 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001573 try:
kelvin8ec71442015-01-15 16:57:00 -08001574 self.handle.sendline( "sudo ptpd " + str( args ) )
1575 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001576 "Multiple",
1577 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001578 "\$" ] )
1579 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001580
andrewonlab0c38a4a2014-10-28 18:35:35 -04001581 if i == 0:
1582 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001583 main.log.info( "ptpd returned an error: " +
1584 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001585 return handle
1586 elif i == 1:
1587 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001588 main.log.error( "ptpd returned an error: " +
1589 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001590 return handle
1591 else:
1592 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001593
andrewonlab0c38a4a2014-10-28 18:35:35 -04001594 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001595 main.log.error( self.name + ": EOF exception found" )
1596 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001597 main.cleanup()
1598 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001599 except Exception:
1600 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001601 main.cleanup()
1602 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001603
kelvin-onlabd3b64892015-01-20 13:26:24 -08001604 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001605 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001606 """
1607 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001608 Current implementation of ONOS deletes its karaf
1609 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001610 you may want to use this function to capture
1611 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001612 Localtime will be attached to the filename
1613
1614 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001615 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001616 copy.
kelvin8ec71442015-01-15 16:57:00 -08001617 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001618 For copying multiple files, leave copyFileName
1619 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001620 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001621 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001622 ex ) /tmp/
1623 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001624 * copyFileName: If you want to rename the log
1625 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001626 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001627 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001628 try:
kelvin8ec71442015-01-15 16:57:00 -08001629 localtime = time.strftime( '%x %X' )
1630 localtime = localtime.replace( "/", "" )
1631 localtime = localtime.replace( " ", "_" )
1632 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001633 if destDir[ -1: ] != "/":
1634 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001635
kelvin-onlabd3b64892015-01-20 13:26:24 -08001636 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001637 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1638 str( destDir ) + str( copyFileName ) +
1639 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001640 self.handle.expect( "cp" )
1641 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001642 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001643 self.handle.sendline( "cp " + str( logToCopy ) +
1644 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001645 self.handle.expect( "cp" )
1646 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001647
kelvin8ec71442015-01-15 16:57:00 -08001648 return self.handle.before
1649
1650 except pexpect.EOF:
1651 main.log.error( "Copying files failed" )
1652 main.log.error( self.name + ": EOF exception found" )
1653 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001654 except Exception:
1655 main.log.exception( "Copying files failed" )
1656
Jon Hall16b72c42015-05-20 10:23:36 -07001657 def checkLogs( self, onosIp, restart=False):
kelvin8ec71442015-01-15 16:57:00 -08001658 """
Jon Hall94fd0472014-12-08 11:52:42 -08001659 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001660 If restart is True, use the old version of onos-check-logs which
1661 does not print the full stacktrace, but shows the entire log file,
1662 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001663 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001664 """
Jon Hall94fd0472014-12-08 11:52:42 -08001665 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001666 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001667 if restart:
1668 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001669 self.handle.sendline( cmd )
1670 self.handle.expect( cmd )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001671 self.handle.expect( "\$ " )
Jon Hall94fd0472014-12-08 11:52:42 -08001672 response = self.handle.before
1673 return response
1674 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001675 main.log.error( "Lost ssh connection" )
1676 main.log.error( self.name + ": EOF exception found" )
1677 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001678 except Exception:
1679 main.log.exception( self.name + ": Uncaught exception!" )
1680 main.cleanup()
1681 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001682
kelvin-onlabd3b64892015-01-20 13:26:24 -08001683 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001684 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001685 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001686 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001687 try:
kelvin8ec71442015-01-15 16:57:00 -08001688 self.handle.sendline( "" )
1689 self.handle.expect( "\$" )
1690 self.handle.sendline( "onos-service " + str( node ) +
1691 " status" )
1692 i = self.handle.expect( [
You Wangef1e6572016-03-08 12:53:18 -08001693 "start/running",
You Wang7bd83062016-03-01 11:50:00 -08001694 "Running ...",
You Wangef1e6572016-03-08 12:53:18 -08001695 "stop/waiting",
You Wang7bd83062016-03-01 11:50:00 -08001696 "Not Running ...",
kelvin8ec71442015-01-15 16:57:00 -08001697 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001698
You Wangef1e6572016-03-08 12:53:18 -08001699 if i == 0 or i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001700 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001701 return main.TRUE
You Wangef1e6572016-03-08 12:53:18 -08001702 elif i == 2 or i == 3:
kelvin8ec71442015-01-15 16:57:00 -08001703 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001704 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001705 main.cleanup()
1706 main.exit()
1707 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001708 main.log.error( self.name + ": EOF exception found" )
1709 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001710 main.cleanup()
1711 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001712 except Exception:
1713 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001714 main.cleanup()
1715 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001716
Jon Hall63604932015-02-26 17:09:50 -08001717 def setIpTables( self, ip, port='', action='add', packet_type='',
1718 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001719 """
Jon Hall21270ac2015-02-16 17:59:55 -08001720 Description:
1721 add or remove iptables rule to DROP (default) packets from
1722 specific IP and PORT
1723 Usage:
1724 * specify action ('add' or 'remove')
1725 when removing, pass in the same argument as you would add. It will
1726 delete that specific rule.
1727 * specify the ip to block
1728 * specify the destination port to block (defaults to all ports)
1729 * optional packet type to block (default tcp)
1730 * optional iptables rule (default DROP)
1731 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001732 * States boolean toggles adding all supported tcp states to the
1733 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001734 Returns:
1735 main.TRUE on success or
1736 main.FALSE if given invalid input or
1737 main.ERROR if there is an error in response from iptables
1738 WARNING:
1739 * This function uses root privilege iptables command which may result
1740 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001741 """
Jon Hall21270ac2015-02-16 17:59:55 -08001742
1743 # NOTE*********
1744 # The strict checking methods of this driver function is intentional
1745 # to discourage any misuse or error of iptables, which can cause
1746 # severe network errors
1747 # *************
1748
1749 # NOTE: Sleep needed to give some time for rule to be added and
1750 # registered to the instance. If you are calling this function
1751 # multiple times this sleep will prevent any errors.
1752 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001753 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001754 try:
1755 # input validation
1756 action_type = action.lower()
1757 rule = rule.upper()
1758 direction = direction.upper()
1759 if action_type != 'add' and action_type != 'remove':
1760 main.log.error( "Invalid action type. Use 'add' or "
1761 "'remove' table rule" )
1762 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1763 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1764 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1765 "'ACCEPT' or 'LOG' only." )
1766 if direction != 'INPUT' and direction != 'OUTPUT':
1767 # NOTE currently only supports rules INPUT and OUPTUT
1768 main.log.error( "Invalid rule. Valid directions are"
1769 " 'OUTPUT' or 'INPUT'" )
1770 return main.FALSE
1771 return main.FALSE
1772 return main.FALSE
1773 if action_type == 'add':
1774 # -A is the 'append' action of iptables
1775 actionFlag = '-A'
1776 elif action_type == 'remove':
1777 # -D is the 'delete' rule of iptables
1778 actionFlag = '-D'
1779 self.handle.sendline( "" )
1780 self.handle.expect( "\$" )
1781 cmd = "sudo iptables " + actionFlag + " " +\
1782 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001783 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001784 # " -p " + str( packet_type ) +\
1785 if packet_type:
1786 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001787 if port:
1788 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001789 if states:
1790 cmd += " -m state --state="
1791 #FIXME- Allow user to configure which states to block
1792 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001793 cmd += " -j " + str( rule )
1794
1795 self.handle.sendline( cmd )
1796 self.handle.expect( "\$" )
1797 main.log.warn( self.handle.before )
1798
1799 info_string = "On " + str( self.name )
1800 info_string += " " + str( action_type )
1801 info_string += " iptable rule [ "
1802 info_string += " IP: " + str( ip )
1803 info_string += " Port: " + str( port )
1804 info_string += " Rule: " + str( rule )
1805 info_string += " Direction: " + str( direction ) + " ]"
1806 main.log.info( info_string )
1807 return main.TRUE
1808 except pexpect.TIMEOUT:
1809 main.log.exception( self.name + ": Timeout exception in "
1810 "setIpTables function" )
1811 return main.ERROR
1812 except pexpect.EOF:
1813 main.log.error( self.name + ": EOF exception found" )
1814 main.log.error( self.name + ": " + self.handle.before )
1815 main.cleanup()
1816 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001817 except Exception:
1818 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001819 main.cleanup()
1820 main.exit()
1821
Jon Hall0468b042015-02-19 19:08:21 -08001822 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001823 """
Jon Hall0468b042015-02-19 19:08:21 -08001824 This method is used by STS to check the status of the controller
1825 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001826 """
Jon Hall0468b042015-02-19 19:08:21 -08001827 import re
1828 try:
1829 self.handle.sendline( "" )
1830 self.handle.expect( "\$" )
1831 self.handle.sendline( "cd " + self.home )
1832 self.handle.expect( "\$" )
1833 self.handle.sendline( "service onos status" )
1834 self.handle.expect( "\$" )
1835 response = self.handle.before
1836 if re.search( "onos start/running", response ):
1837 # onos start/running, process 10457
1838 return 'RUNNING'
1839 # FIXME: Implement this case
1840 # elif re.search( pattern, response ):
1841 # return 'STARTING'
1842 elif re.search( "onos stop/", response ):
1843 # onos stop/waiting
1844 # FIXME handle this differently?: onos stop/pre-stop
1845 return 'STOPPED'
1846 # FIXME: Implement this case
1847 # elif re.search( pattern, response ):
1848 # return 'FROZEN'
1849 else:
1850 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001851 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001852 main.log.warn( response )
1853 return 'ERROR', "Unknown response: %s" % response
1854 except pexpect.TIMEOUT:
1855 main.log.exception( self.name + ": Timeout exception in "
1856 "setIpTables function" )
1857 return 'ERROR', "Pexpect Timeout"
1858 except pexpect.EOF:
1859 main.log.error( self.name + ": EOF exception found" )
1860 main.log.error( self.name + ": " + self.handle.before )
1861 main.cleanup()
1862 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001863 except Exception:
1864 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001865 main.cleanup()
1866 main.exit()
1867
andrew@onlab.us3b087132015-03-11 15:00:08 -07001868 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1869 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001870 Create/formats the LinkGraph.cfg file based on arguments
1871 -only creates a linear topology and connects islands
1872 -evenly distributes devices
andrew@onlab.us3b087132015-03-11 15:00:08 -07001873 -must be called by ONOSbench
1874
Jon Hall4ba53f02015-07-29 13:07:41 -07001875 ONOSIpList - list of all of the node IPs to be used
1876
1877 deviceCount - number of switches to be assigned
andrew@onlab.us3b087132015-03-11 15:00:08 -07001878 '''
1879 main.log.step("Creating link graph configuration file." )
1880 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
Jon Hall4ba53f02015-07-29 13:07:41 -07001881 tempFile = "/tmp/linkGraph.cfg"
andrew@onlab.us3b087132015-03-11 15:00:08 -07001882
1883 linkGraph = open(tempFile, 'w+')
1884 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1885 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1886 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001887
andrew@onlab.us3b087132015-03-11 15:00:08 -07001888 clusterCount = len(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07001889
1890 if type(deviceCount) is int or type(deviceCount) is str:
andrew@onlab.us3b087132015-03-11 15:00:08 -07001891 deviceCount = int(deviceCount)
1892 switchList = [0]*(clusterCount+1)
1893 baselineSwitchCount = deviceCount/clusterCount
Jon Hall4ba53f02015-07-29 13:07:41 -07001894
andrew@onlab.us3b087132015-03-11 15:00:08 -07001895 for node in range(1, clusterCount + 1):
1896 switchList[node] = baselineSwitchCount
1897
1898 for node in range(1, (deviceCount%clusterCount)+1):
1899 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001900
andrew@onlab.us3b087132015-03-11 15:00:08 -07001901 if type(deviceCount) is list:
1902 main.log.info("Using provided device distribution")
1903 switchList = [0]
1904 for i in deviceCount:
1905 switchList.append(int(i))
1906
1907 tempList = ['0']
1908 tempList.extend(ONOSIpList)
1909 ONOSIpList = tempList
1910
1911 myPort = 6
1912 lastSwitch = 0
1913 for node in range(1, clusterCount+1):
1914 if switchList[node] == 0:
1915 continue
1916
1917 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001918
andrew@onlab.us3b087132015-03-11 15:00:08 -07001919 if node > 1:
1920 #connect to last device on previous node
Jon Hall4ba53f02015-07-29 13:07:41 -07001921 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
1922 linkGraph.write(line)
1923
1924 lastSwitch = 0
1925 for switch in range (0, switchList[node]-1):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001926 line = ""
1927 line = ("\t" + str(switch) + ":" + str(myPort))
1928 line += " -- "
1929 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
1930 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07001931 lastSwitch = switch+1
andrew@onlab.us3b087132015-03-11 15:00:08 -07001932 lastIp = ONOSIpList[node]
Jon Hall4ba53f02015-07-29 13:07:41 -07001933
andrew@onlab.us3b087132015-03-11 15:00:08 -07001934 #lastSwitch += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001935 if node < (clusterCount):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001936 #connect to first device on the next node
Jon Hall4ba53f02015-07-29 13:07:41 -07001937 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
andrew@onlab.us3b087132015-03-11 15:00:08 -07001938 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07001939
andrew@onlab.us3b087132015-03-11 15:00:08 -07001940 linkGraph.write("}\n")
1941 linkGraph.close()
1942
1943 #SCP
Jon Hall4ba53f02015-07-29 13:07:41 -07001944 os.system( "scp " + tempFile + " " + self.user_name + "@" + benchIp + ":" + linkGraphPath)
andrew@onlab.us3b087132015-03-11 15:00:08 -07001945 main.log.info("linkGraph.cfg creation complete")
1946
cameron@onlab.us75900962015-03-30 13:22:49 -07001947 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
Jon Hall4ba53f02015-07-29 13:07:41 -07001948
andrew@onlab.us3b087132015-03-11 15:00:08 -07001949 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001950 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
1951 deviceCount = number of switches to distribute, or list of values to use as custom distribution
cameron@onlab.us75900962015-03-30 13:22:49 -07001952 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 -07001953 '''
1954
cameron@onlab.us75900962015-03-30 13:22:49 -07001955 main.log.step("Configuring Null Device Provider" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001956 clusterCount = len(ONOSIpList)
1957
Jon Hall4ba53f02015-07-29 13:07:41 -07001958 try:
1959
cameron@onlab.us75900962015-03-30 13:22:49 -07001960 if type(deviceCount) is int or type(deviceCount) is str:
1961 main.log.step("Creating device distribution")
1962 deviceCount = int(deviceCount)
1963 switchList = [0]*(clusterCount+1)
1964 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001965
cameron@onlab.us75900962015-03-30 13:22:49 -07001966 for node in range(1, clusterCount + 1):
1967 switchList[node] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001968
cameron@onlab.us75900962015-03-30 13:22:49 -07001969 for node in range(1, (deviceCount%clusterCount)+1):
1970 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001971
1972 if type(deviceCount) is list:
1973 main.log.info("Using provided device distribution")
1974
1975 if len(deviceCount) == clusterCount:
cameron@onlab.us75900962015-03-30 13:22:49 -07001976 switchList = ['0']
1977 switchList.extend(deviceCount)
Jon Hall4ba53f02015-07-29 13:07:41 -07001978
1979 if len(deviceCount) == (clusterCount + 1):
1980 if deviceCount[0] == '0' or deviceCount[0] == 0:
cameron@onlab.us75900962015-03-30 13:22:49 -07001981 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001982
cameron@onlab.us75900962015-03-30 13:22:49 -07001983 assert len(switchList) == (clusterCount + 1)
Jon Hall4ba53f02015-07-29 13:07:41 -07001984
cameron@onlab.us75900962015-03-30 13:22:49 -07001985 except AssertionError:
Jon Hall4ba53f02015-07-29 13:07:41 -07001986 main.log.error( "Bad device/Ip list match")
cameron@onlab.us75900962015-03-30 13:22:49 -07001987 except TypeError:
1988 main.log.exception( self.name + ": Object not as expected" )
1989 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07001990 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07001991 main.log.exception( self.name + ": Uncaught exception!" )
1992 main.cleanup()
1993 main.exit()
1994
andrew@onlab.us3b087132015-03-11 15:00:08 -07001995
1996 ONOSIp = [0]
1997 ONOSIp.extend(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07001998
andrew@onlab.us3b087132015-03-11 15:00:08 -07001999 devicesString = "devConfigs = "
2000 for node in range(1, len(ONOSIp)):
2001 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
2002 if node < clusterCount:
2003 devicesString += (",")
Jon Hall4ba53f02015-07-29 13:07:41 -07002004
2005 try:
cameron@onlab.us75900962015-03-30 13:22:49 -07002006 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
2007 self.handle.expect(":~")
2008 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
2009 self.handle.expect(":~")
andrew@onlab.us3b087132015-03-11 15:00:08 -07002010
cameron@onlab.us75900962015-03-30 13:22:49 -07002011 for i in range(10):
2012 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
2013 self.handle.expect(":~")
2014 verification = self.handle.before
2015 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
2016 break
2017 else:
2018 time.sleep(1)
andrew@onlab.us3b087132015-03-11 15:00:08 -07002019
cameron@onlab.us2cc8bf12015-04-02 14:12:30 -07002020 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002021
cameron@onlab.us75900962015-03-30 13:22:49 -07002022 except AssertionError:
2023 main.log.error("Incorrect Config settings: " + verification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002024 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002025 main.log.exception( self.name + ": Uncaught exception!" )
2026 main.cleanup()
2027 main.exit()
2028
Jon Hall4ba53f02015-07-29 13:07:41 -07002029 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002030 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002031 fileName default is currently the same as the default on ONOS, specify alternate file if
cameron@onlab.us75900962015-03-30 13:22:49 -07002032 you want to use a different topology file than linkGraph.cfg
andrew@onlab.us3b087132015-03-11 15:00:08 -07002033 '''
2034
Jon Hall4ba53f02015-07-29 13:07:41 -07002035
2036 try:
2037 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
2038 self.handle.expect(":~")
cameron@onlab.us75900962015-03-30 13:22:49 -07002039 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2040 self.handle.expect(":~")
Jon Hall4ba53f02015-07-29 13:07:41 -07002041
2042 for i in range(10):
2043 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
cameron@onlab.us75900962015-03-30 13:22:49 -07002044 self.handle.expect(":~")
2045 verification = self.handle.before
Jon Hall4ba53f02015-07-29 13:07:41 -07002046 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002047 break
Jon Hall4ba53f02015-07-29 13:07:41 -07002048 else:
cameron@onlab.us75900962015-03-30 13:22:49 -07002049 time.sleep(1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002050
cameron@onlab.us75900962015-03-30 13:22:49 -07002051 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002052
cameron@onlab.us75900962015-03-30 13:22:49 -07002053 except pexpect.EOF:
2054 main.log.error( self.name + ": EOF exception found" )
2055 main.log.error( self.name + ": " + self.handle.before )
2056 main.cleanup()
2057 main.exit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002058 except AssertionError:
2059 main.log.info("Settings did not post to ONOS")
Jon Hall4ba53f02015-07-29 13:07:41 -07002060 main.log.error(varification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002061 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002062 main.log.exception( self.name + ": Uncaught exception!" )
2063 main.log.error(varification)
2064 main.cleanup()
2065 main.exit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002066
kelvin-onlaba4074292015-07-09 15:19:49 -07002067 def getOnosIps( self ):
2068 """
2069 Get all onos IPs stored in
2070 """
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002071
kelvin-onlaba4074292015-07-09 15:19:49 -07002072 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002073
kelvin-onlaba4074292015-07-09 15:19:49 -07002074 def logReport( self, nodeIp, searchTerms, outputMode="s" ):
Jon Hallb4242222016-01-25 17:07:04 -08002075 """
2076 Searches the latest ONOS log file for the given search terms and
2077 prints the total occurances of each term. Returns to combined total of
2078 all occurances.
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002079
Jon Hallb4242222016-01-25 17:07:04 -08002080 Arguments:
2081 * nodeIp - The ip of the ONOS node where the log is located
2082 * searchTerms - A string to grep for or a list of strings to grep
2083 for in the ONOS log. Will print out the number of
2084 occurances for each term.
2085 Optional Arguments:
2086 * outputMode - 's' or 'd'. If 'd' will print the last 5 lines
2087 containing each search term as well as the total
2088 number of occurances of each term. Defaults to 's',
2089 which prints the simple output of just the number
2090 of occurances for each term.
2091 """
2092 try:
2093 main.log.info( " Log Report for {} ".format( nodeIp ).center( 70, '=' ) )
2094 if type( searchTerms ) is str:
2095 searchTerms = [searchTerms]
2096 numTerms = len( searchTerms )
2097 outputMode = outputMode.lower()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002098
Jon Hallb4242222016-01-25 17:07:04 -08002099 totalHits = 0
2100 logLines = []
2101 for termIndex in range( numTerms ):
2102 term = searchTerms[termIndex]
2103 logLines.append( [term] )
2104 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep " + term
2105 self.handle.sendline( cmd )
2106 self.handle.expect( ":~" )
2107 before = self.handle.before.splitlines()
2108 count = 0
2109 for line in before:
2110 if term in line and "grep" not in line:
2111 count += 1
2112 if before.index( line ) > ( len( before ) - 7 ):
2113 logLines[termIndex].append( line )
2114 main.log.info( "{}: {}".format( term, count ) )
2115 totalHits += count
2116 if termIndex == numTerms - 1:
2117 print "\n"
2118 if outputMode != "s":
2119 outputString = ""
2120 for term in logLines:
2121 outputString = term[0] + ": \n"
2122 for line in range( 1, len( term ) ):
2123 outputString += ( "\t" + term[line] + "\n" )
2124 if outputString != ( term[0] + ": \n" ):
2125 main.log.info( outputString )
2126 main.log.info( "=" * 70 )
2127 return totalHits
2128 except pexpect.EOF:
2129 main.log.error( self.name + ": EOF exception found" )
2130 main.log.error( self.name + ": " + self.handle.before )
2131 main.cleanup()
2132 main.exit()
2133 except pexpect.TIMEOUT:
2134 main.log.error( self.name + ": TIMEOUT exception found" )
2135 main.log.error( self.name + ": " + self.handle.before )
2136 main.cleanup()
2137 main.exit()
2138 except Exception:
2139 main.log.exception( self.name + ": Uncaught exception!" )
2140 main.cleanup()
2141 main.exit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002142
2143 def copyMininetFile( self, fileName, localPath, userName, ip,
2144 mnPath='~/mininet/custom/', timeout = 60 ):
2145 """
2146 Description:
2147 Copy mininet topology file from dependency folder in the test folder
2148 and paste it to the mininet machine's mininet/custom folder
2149 Required:
2150 fileName - Name of the topology file to copy
2151 localPath - File path of the mininet topology file
2152 userName - User name of the mininet machine to send the file to
2153 ip - Ip address of the mininet machine
2154 Optional:
2155 mnPath - of the mininet directory to send the file to
2156 Return:
2157 Return main.TRUE if successfully copied the file otherwise
2158 return main.FALSE
2159 """
2160
2161 try:
2162 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2163 str( ip ) + ":" + mnPath + fileName
2164
2165 self.handle.sendline( "" )
2166 self.handle.expect( "\$" )
2167
2168 main.log.info( self.name + ": Execute: " + cmd )
2169
2170 self.handle.sendline( cmd )
2171
2172 i = self.handle.expect( [ 'No such file',
2173 "100%",
2174 pexpect.TIMEOUT ] )
2175
2176 if i == 0:
2177 main.log.error( self.name + ": File " + fileName +
2178 " does not exist!" )
2179 return main.FALSE
2180
2181 if i == 1:
2182 main.log.info( self.name + ": File " + fileName +
2183 " has been copied!" )
2184 self.handle.sendline( "" )
2185 self.handle.expect( "\$" )
2186 return main.TRUE
2187
2188 except pexpect.EOF:
2189 main.log.error( self.name + ": EOF exception found" )
2190 main.log.error( self.name + ": " + self.handle.before )
2191 main.cleanup()
2192 main.exit()
2193 except pexpect.TIMEOUT:
2194 main.log.error( self.name + ": TIMEOUT exception found" )
2195 main.log.error( self.name + ": " + self.handle.before )
2196 main.cleanup()
2197 main.exit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002198
2199 def jvmSet(self, memory=8):
Jon Hall4ba53f02015-07-29 13:07:41 -07002200
cameron@onlab.us78b89652015-07-08 15:21:03 -07002201 import os
2202
2203 homeDir = os.path.expanduser('~')
2204 filename = "/onos/tools/package/bin/onos-service"
2205
2206 serviceConfig = open(homeDir + filename, 'w+')
2207 serviceConfig.write("#!/bin/bash\n ")
2208 serviceConfig.write("#------------------------------------- \n ")
2209 serviceConfig.write("# Starts ONOS Apache Karaf container\n ")
2210 serviceConfig.write("#------------------------------------- \n ")
2211 serviceConfig.write("#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n ")
2212 serviceConfig.write("""export JAVA_OPTS="${JAVA_OPTS:--Xms""" + str(memory) + "G -Xmx" + str(memory) + """G}" \n """)
2213 serviceConfig.write("[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n")
2214 serviceConfig.write("""${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """)
2215 serviceConfig.close()
2216
2217 def createDBFile(self, testData):
Jon Hall4ba53f02015-07-29 13:07:41 -07002218
cameron@onlab.us78b89652015-07-08 15:21:03 -07002219 filename = main.TEST + "DB"
2220 DBString = ""
Jon Hall4ba53f02015-07-29 13:07:41 -07002221
cameron@onlab.us78b89652015-07-08 15:21:03 -07002222 for item in testData:
Jon Hall4ba53f02015-07-29 13:07:41 -07002223 if type(item) is string:
cameron@onlab.us78b89652015-07-08 15:21:03 -07002224 item = "'" + item + "'"
2225 if testData.index(item) < len(testData-1):
2226 item += ","
Jon Hall4ba53f02015-07-29 13:07:41 -07002227 DBString += str(item)
cameron@onlab.us78b89652015-07-08 15:21:03 -07002228
2229 DBFile = open(filename, "a")
2230 DBFile.write(DBString)
2231 DBFile.close()
2232
Jon Hall4ba53f02015-07-29 13:07:41 -07002233 def verifySummary(self, ONOSIp,*deviceCount):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002234
2235 self.handle.sendline("onos " + ONOSIp + " summary")
2236 self.handle.expect(":~")
2237
2238 summaryStr = self.handle.before
2239 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2240
2241 #passed = "SCC(s)=1" in summaryStr
2242 #if deviceCount:
2243 # passed = "devices=" + str(deviceCount) + "," not in summaryStr
2244
GlennRC772363b2015-08-25 13:05:57 -07002245 passed = False
cameron@onlab.us78b89652015-07-08 15:21:03 -07002246 if "SCC(s)=1," in summaryStr:
2247 passed = True
2248 print("Summary is verifed")
Jon Hall4ba53f02015-07-29 13:07:41 -07002249 else:
2250 print("Summary failed")
cameron@onlab.us78b89652015-07-08 15:21:03 -07002251
2252 if deviceCount:
2253 print" ============================="
2254 checkStr = "devices=" + str(deviceCount[0]) + ","
2255 print "Checkstr: " + checkStr
2256 if checkStr not in summaryStr:
2257 passed = False
Jon Hall4ba53f02015-07-29 13:07:41 -07002258 print("Device count failed")
2259 else:
2260 print "device count verified"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002261
2262 return passed