blob: 644b9f73b89cf19abd4a28f65967d48bc7b853cd [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
Jeremy Songsterae01bba2016-07-11 15:39:17 -070017Modified 2016 by ON.Lab
18
19Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
20the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
21or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
andrewonlabe8e56fd2014-10-09 17:12:44 -040022
kelvin8ec71442015-01-15 16:57:00 -080023"""
Jon Hall05b2b432014-10-08 19:53:25 -040024import time
Jon Hall6801cda2015-07-15 14:13:45 -070025import types
Jon Hall05b2b432014-10-08 19:53:25 -040026import pexpect
kelvin-onlaba4074292015-07-09 15:19:49 -070027import os
Jon Hall6c44c0b2016-04-20 15:21:00 -070028import re
29import subprocess
pingping-lin6d23d9e2015-02-02 16:54:24 -080030from requests.models import Response
Jon Hall05b2b432014-10-08 19:53:25 -040031from drivers.common.clidriver import CLI
32
kelvin8ec71442015-01-15 16:57:00 -080033class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040034
kelvin8ec71442015-01-15 16:57:00 -080035 def __init__( self ):
36 """
37 Initialize client
38 """
Jon Hallefbd9792015-03-05 16:11:36 -080039 self.name = None
40 self.home = None
41 self.handle = None
Jon Hall6c44c0b2016-04-20 15:21:00 -070042 self.nicAddr = None
kelvin8ec71442015-01-15 16:57:00 -080043 super( CLI, self ).__init__()
44
45 def connect( self, **connectargs ):
46 """
Jon Hall05b2b432014-10-08 19:53:25 -040047 Creates ssh handle for ONOS "bench".
kelvin-onlaba4074292015-07-09 15:19:49 -070048 NOTE:
49 The ip_address would come from the topo file using the host tag, the
50 value can be an environment variable as well as a "localhost" to get
51 the ip address needed to ssh to the "bench"
kelvin8ec71442015-01-15 16:57:00 -080052 """
Jon Hall05b2b432014-10-08 19:53:25 -040053 try:
54 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080055 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us3b087132015-03-11 15:00:08 -070056 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040057 for key in self.options:
58 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080059 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040060 break
Jon Hall274b6642015-02-17 11:57:17 -080061 if self.home is None or self.home == "":
Jon Halle94919c2015-03-23 11:42:57 -070062 self.home = "~/onos"
Jon Hall21270ac2015-02-16 17:59:55 -080063
kelvin8ec71442015-01-15 16:57:00 -080064 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070065
kelvin-onlabc2b79102015-07-14 11:41:20 -070066 # The 'nodes' tag is optional and it is not required in .topo file
kelvin-onlaba4074292015-07-09 15:19:49 -070067 for key in self.options:
68 if key == "nodes":
kelvin-onlabc2b79102015-07-14 11:41:20 -070069 # Maximum number of ONOS nodes to run, if there is any
kelvin-onlaba4074292015-07-09 15:19:49 -070070 self.maxNodes = int( self.options[ 'nodes' ] )
71 break
72 self.maxNodes = None
73
kelvin-onlabc2b79102015-07-14 11:41:20 -070074 if self.maxNodes == None or self.maxNodes == "":
75 self.maxNodes = 100
kelvin-onlaba4074292015-07-09 15:19:49 -070076
kelvin-onlabc2b79102015-07-14 11:41:20 -070077
78 # Grabs all OC environment variables based on max number of nodes
kelvin-onlaba4074292015-07-09 15:19:49 -070079 self.onosIps = {} # Dictionary of all possible ONOS ip
80
81 try:
82 if self.maxNodes:
kelvin-onlaba4074292015-07-09 15:19:49 -070083 for i in range( self.maxNodes ):
84 envString = "OC" + str( i + 1 )
kelvin-onlabc2b79102015-07-14 11:41:20 -070085 # If there is no more OC# then break the loop
86 if os.getenv( envString ):
87 self.onosIps[ envString ] = os.getenv( envString )
88 else:
89 self.maxNodes = len( self.onosIps )
90 main.log.info( self.name +
91 ": Created cluster data with " +
92 str( self.maxNodes ) +
93 " maximum number" +
94 " of nodes" )
95 break
kelvin-onlaba4074292015-07-09 15:19:49 -070096
97 if not self.onosIps:
98 main.log.info( "Could not read any environment variable"
99 + " please load a cell file with all" +
100 " onos IP" )
Jon Hall5cf14d52015-07-16 12:15:19 -0700101 self.maxNodes = None
kelvin-onlaba4074292015-07-09 15:19:49 -0700102 else:
103 main.log.info( self.name + ": Found " +
104 str( self.onosIps.values() ) +
105 " ONOS IPs" )
kelvin-onlaba4074292015-07-09 15:19:49 -0700106 except KeyError:
107 main.log.info( "Invalid environment variable" )
108 except Exception as inst:
109 main.log.error( "Uncaught exception: " + str( inst ) )
110
111 try:
112 if os.getenv( str( self.ip_address ) ) != None:
113 self.ip_address = os.getenv( str( self.ip_address ) )
114 else:
115 main.log.info( self.name +
116 ": Trying to connect to " +
117 self.ip_address )
kelvin-onlaba4074292015-07-09 15:19:49 -0700118 except KeyError:
119 main.log.info( "Invalid host name," +
120 " connecting to local host instead" )
121 self.ip_address = 'localhost'
122 except Exception as inst:
123 main.log.error( "Uncaught exception: " + str( inst ) )
124
kelvin8ec71442015-01-15 16:57:00 -0800125 self.handle = super( OnosDriver, self ).connect(
126 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -0800127 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800128 port=self.port,
129 pwd=self.pwd,
130 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -0400131
Jon Hall05b2b432014-10-08 19:53:25 -0400132 if self.handle:
Jon Hall0fc0d452015-07-14 09:49:58 -0700133 self.handle.sendline( "cd " + self.home )
134 self.handle.expect( "\$" )
Jon Hall05b2b432014-10-08 19:53:25 -0400135 return self.handle
kelvin8ec71442015-01-15 16:57:00 -0800136 else:
Jon Hall0fc0d452015-07-14 09:49:58 -0700137 main.log.info( "Failed to create ONOS handle" )
Jon Hall05b2b432014-10-08 19:53:25 -0400138 return main.FALSE
139 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800140 main.log.error( self.name + ": EOF exception found" )
141 main.log.error( self.name + ": " + self.handle.before )
Jon Hall05b2b432014-10-08 19:53:25 -0400142 main.cleanup()
143 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800144 except Exception:
145 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall05b2b432014-10-08 19:53:25 -0400146 main.cleanup()
147 main.exit()
148
kelvin8ec71442015-01-15 16:57:00 -0800149 def disconnect( self ):
150 """
Jon Hall05b2b432014-10-08 19:53:25 -0400151 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800152 """
Jon Halld61331b2015-02-17 16:35:47 -0800153 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -0400154 try:
Jon Hall61282e32015-03-19 11:34:11 -0700155 if self.handle:
156 self.handle.sendline( "" )
157 self.handle.expect( "\$" )
158 self.handle.sendline( "exit" )
159 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -0400160 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800161 main.log.error( self.name + ": EOF exception found" )
162 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700163 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700164 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700165 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800166 except Exception:
167 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -0400168 response = main.FALSE
169 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400170
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400171 def getEpochMs( self ):
172 """
173 Returns milliseconds since epoch
Jon Hall4ba53f02015-07-29 13:07:41 -0700174
175 When checking multiple nodes in a for loop,
176 around a hundred milliseconds of difference (ascending) is
177 generally acceptable due to calltime of the function.
178 Few seconds, however, is not and it means clocks
179 are off sync.
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400180 """
181 try:
182 self.handle.sendline( 'date +%s.%N' )
183 self.handle.expect( 'date \+\%s\.\%N' )
184 self.handle.expect( '\$' )
185 epochMs = self.handle.before
186 return epochMs
187 except Exception:
188 main.log.exception( 'Uncaught exception getting epoch time' )
189 main.cleanup()
190 main.exit()
191
Jon Hall6c44c0b2016-04-20 15:21:00 -0700192 def onosPackage( self, opTimeout=180 ):
kelvin8ec71442015-01-15 16:57:00 -0800193 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400194 Produce a self-contained tar.gz file that can be deployed
Jon Hall64af8502015-12-15 10:09:33 -0800195 and executed on any platform with Java 8 JRE.
kelvin8ec71442015-01-15 16:57:00 -0800196 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400197 try:
Jon Hall64af8502015-12-15 10:09:33 -0800198 ret = main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800199 self.handle.sendline( "onos-package" )
200 self.handle.expect( "onos-package" )
Jon Hall96451092016-05-04 09:42:30 -0700201 while True:
202 i = self.handle.expect( [ "Downloading",
203 "Unknown options",
204 "No such file or directory",
205 "tar.gz",
206 "\$" ],
207 opTimeout )
208 handle = str( self.handle.before + self.handle.after )
209 if i == 0:
210 # Give more time to download the file
211 continue # expect again
212 elif i == 1:
213 # Incorrect usage
214 main.log.error( "onos-package does not recognize the given options" )
215 ret = main.FALSE
216 continue # expect again
217 elif i == 2:
218 # File(s) not found
219 main.log.error( "onos-package could not find a file or directory" )
220 ret = main.FALSE
221 continue # expect again
222 elif i == 3:
223 # tar.gz
224 continue # expect again
225 elif i == 4:
226 # Prompt returned
227 break
Jon Hallc6793552016-01-19 14:18:37 -0800228 main.log.info( "onos-package command returned: " + handle )
kelvin8ec71442015-01-15 16:57:00 -0800229 # As long as the sendline does not time out,
230 # return true. However, be careful to interpret
231 # the results of the onos-package command return
Jon Hall64af8502015-12-15 10:09:33 -0800232 return ret
233 except pexpect.TIMEOUT:
234 main.log.exception( self.name + ": TIMEOUT exception found in onosPackage" )
235 main.log.error( self.name + ": " + self.handle.before )
236 return main.FALSE
andrewonlab7735d852014-10-09 13:02:47 -0400237 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800238 main.log.error( self.name + ": EOF exception found" )
239 main.log.error( self.name + ": " + self.handle.before )
Jon Hall64af8502015-12-15 10:09:33 -0800240 main.cleanup()
241 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800242 except Exception:
243 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400244 main.cleanup()
245 main.exit()
246
kelvin-onlabd3b64892015-01-20 13:26:24 -0800247 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800248 """
andrewonlab8790abb2014-11-06 13:51:54 -0500249 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800250 """
andrewonlab8790abb2014-11-06 13:51:54 -0500251 try:
kelvin8ec71442015-01-15 16:57:00 -0800252 self.handle.sendline( "onos-build" )
253 self.handle.expect( "onos-build" )
254 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800255 "BUILD SUCCESS",
256 "ERROR",
257 "BUILD FAILED" ],
258 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800259 handle = str( self.handle.before )
Jon Hall3b489db2015-10-05 14:38:37 -0700260 self.handle.expect( "\$" )
andrewonlab8790abb2014-11-06 13:51:54 -0500261
kelvin8ec71442015-01-15 16:57:00 -0800262 main.log.info( "onos-build command returned: " +
263 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500264
265 if i == 0:
266 return main.TRUE
267 else:
268 return handle
269
270 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800271 main.log.error( self.name + ": EOF exception found" )
272 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800273 except Exception:
274 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500275 main.cleanup()
276 main.exit()
277
shahshreya9f531fe2015-06-10 12:03:51 -0700278 def cleanInstall( self, skipTest=False, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800279 """
280 Runs mvn clean install in the root of the ONOS directory.
281 This will clean all ONOS artifacts then compile each module
shahshreya9f531fe2015-06-10 12:03:51 -0700282 Optional:
283 skipTest - Does "-DskipTests -Dcheckstyle.skip -U -T 1C" which
284 skip the test. This will make the building faster.
285 Disregarding the credibility of the build
kelvin8ec71442015-01-15 16:57:00 -0800286 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400287 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800288 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400289 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800290 main.log.info( "Running 'mvn clean install' on " +
291 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800292 ". This may take some time." )
293 self.handle.sendline( "cd " + self.home )
294 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400295
kelvin8ec71442015-01-15 16:57:00 -0800296 self.handle.sendline( "" )
297 self.handle.expect( "\$" )
shahshreya9f531fe2015-06-10 12:03:51 -0700298
299 if not skipTest:
300 self.handle.sendline( "mvn clean install" )
301 self.handle.expect( "mvn clean install" )
302 else:
303 self.handle.sendline( "mvn clean install -DskipTests" +
304 " -Dcheckstyle.skip -U -T 1C" )
305 self.handle.expect( "mvn clean install -DskipTests" +
306 " -Dcheckstyle.skip -U -T 1C" )
kelvin8ec71442015-01-15 16:57:00 -0800307 while True:
308 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800309 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800310 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400311 'BUILD\sFAILURE',
312 'BUILD\sSUCCESS',
Jon Halle94919c2015-03-23 11:42:57 -0700313 'onos\$', #TODO: fix this to be more generic?
Jon Hallde9d9aa2014-10-08 20:36:02 -0400314 'ONOS\$',
pingping-lin57a56ce2015-05-20 16:43:48 -0700315 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400316 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800317 main.log.error( self.name + ":There is insufficient memory \
318 for the Java Runtime Environment to continue." )
319 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400320 main.cleanup()
321 main.exit()
322 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800323 main.log.error( self.name + ": Build failure!" )
324 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400325 main.cleanup()
326 main.exit()
327 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800328 main.log.info( self.name + ": Build success!" )
Jon Halle94919c2015-03-23 11:42:57 -0700329 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800330 main.log.info( self.name + ": Build complete" )
331 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400332 for line in self.handle.before.splitlines():
333 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800334 main.log.info( line )
335 self.handle.sendline( "" )
336 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400337 return main.TRUE
Jon Halle94919c2015-03-23 11:42:57 -0700338 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800339 main.log.error(
340 self.name +
341 ": mvn clean install TIMEOUT!" )
342 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400343 main.cleanup()
344 main.exit()
345 else:
Jon Hall274b6642015-02-17 11:57:17 -0800346 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800347 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800348 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400349 main.cleanup()
350 main.exit()
351 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800352 main.log.error( self.name + ": EOF exception found" )
353 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400354 main.cleanup()
355 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800356 except Exception:
357 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400358 main.cleanup()
359 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400360
Jon Hall3576f572016-08-23 10:01:07 -0700361 def buckBuild( self, timeout=180 ):
362 """
363 Build onos using buck.
364 """
365 try:
366 ret = main.TRUE
367 self.handle.sendline( "buck build onos" )
368 self.handle.expect( "buck build onos" )
369 output = ""
370 while True:
371 i = self.handle.expect( [ "This does not appear to be the root of a Buck project.",
372 "\n",
373 "BUILD FAILED",
374 "\$" ],
375 timeout=timeout )
376 output += str( self.handle.before + self.handle.after )
377 if i == 0:
378 main.log.error( "Wrong location" )
379 ret = main.FALSE
380 elif i == 1:
381 # end of a line, buck is still printing output
382 pass
383 elif i == 2:
384 # Build failed
385 main.log.error( "Build failed" )
386 ret = main.FALSE
387 elif i == 3:
388 # Prompt returned
389 break
390 main.log.debug( output )
391 return ret
392 except pexpect.TIMEOUT:
393 main.log.exception( self.name + ": TIMEOUT exception found" )
394 main.log.error( self.name + ": " + self.handle.before )
395 return main.FALSE
396 except pexpect.EOF:
397 main.log.error( self.name + ": EOF exception found" )
398 main.log.error( self.name + ": " + self.handle.before )
399 main.cleanup()
400 main.exit()
401 except Exception:
402 main.log.exception( "Failed to build and package ONOS" )
403 main.cleanup()
404 main.exit()
405
Jon Hall61282e32015-03-19 11:34:11 -0700406 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800407 """
Jon Hallacabffd2014-10-09 12:36:53 -0400408 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800409
Jon Hall61282e32015-03-19 11:34:11 -0700410 If the fastForward boolean is set to true, only git pulls that can
411 be fast forwarded will be performed. IE if you have not local commits
412 in your branch.
413
Jon Hallacabffd2014-10-09 12:36:53 -0400414 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800415 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400416 for the purpose of pulling from other nodes if necessary.
417
Jon Hall47a93fb2015-01-06 16:46:06 -0800418 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400419 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800420 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400421 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400422
kelvin8ec71442015-01-15 16:57:00 -0800423 """
Jon Hallacabffd2014-10-09 12:36:53 -0400424 try:
kelvin8ec71442015-01-15 16:57:00 -0800425 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800426 self.handle.expect( self.home + "\$" )
Jon Hall61282e32015-03-19 11:34:11 -0700427 cmd = "git pull"
428 if comp1 != "":
429 cmd += ' ' + comp1
430 if fastForward:
431 cmd += ' ' + " --ff-only"
432 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800433 i = self.handle.expect(
434 [
435 'fatal',
436 'Username\sfor\s(.*):\s',
437 '\sfile(s*) changed,\s',
438 'Already up-to-date',
439 'Aborting',
440 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800441 'You asked me to pull without telling me which branch you',
442 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700443 'Please enter a commit message to explain why this merge',
444 'Found a swap file by the name',
445 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800446 pexpect.TIMEOUT ],
447 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800448 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700449 main.log.error( self.name + ": Git pull had some issue" )
450 output = self.handle.after
451 self.handle.expect( '\$' )
452 output += self.handle.before
453 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400454 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800455 elif i == 1:
456 main.log.error(
457 self.name +
458 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400459 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800460 elif i == 2:
461 main.log.info(
462 self.name +
463 ": Git Pull - pulling repository now" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800464 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800465 # So that only when git pull is done, we do mvn clean compile
466 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800467 elif i == 3:
468 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800469 return i
kelvin8ec71442015-01-15 16:57:00 -0800470 elif i == 4:
471 main.log.info(
472 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800473 ": Git Pull - Aborting..." +
474 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400475 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800476 elif i == 5:
477 main.log.info(
478 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800479 ": Git Pull - You are not currently " +
480 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400481 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800482 elif i == 6:
483 main.log.info(
484 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800485 ": Git Pull - You have not configured an upstream " +
486 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400487 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800488 elif i == 7:
489 main.log.info(
490 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800491 ": Git Pull - Pull is not possible because " +
492 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400493 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800494 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700495 # NOTE: abandoning test since we can't reliably handle this
496 # there could be different default text editors and we
497 # also don't know if we actually want to make the commit
498 main.log.error( "Git pull resulted in a merge commit message" +
499 ". Exiting test!" )
500 main.cleanup()
501 main.exit()
502 elif i == 9: # Merge commit message but swap file exists
503 main.log.error( "Git pull resulted in a merge commit message" +
504 " but a swap file exists." )
505 try:
506 self.handle.send( 'A' ) # Abort
507 self.handle.expect( "\$" )
508 return main.ERROR
509 except Exception:
510 main.log.exception( "Couldn't exit editor prompt!")
511 main.cleanup()
512 main.exit()
513 elif i == 10: # In the middle of a merge commit
514 main.log.error( "Git branch is in the middle of a merge. " )
515 main.log.warn( self.handle.before + self.handle.after )
516 return main.ERROR
517 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800518 main.log.error( self.name + ": Git Pull - TIMEOUT" )
519 main.log.error(
520 self.name + " Response was: " + str(
521 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400522 return main.ERROR
523 else:
kelvin8ec71442015-01-15 16:57:00 -0800524 main.log.error(
525 self.name +
526 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400527 return main.ERROR
528 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800529 main.log.error( self.name + ": EOF exception found" )
530 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400531 main.cleanup()
532 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800533 except Exception:
534 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400535 main.cleanup()
536 main.exit()
537
kelvin-onlabd3b64892015-01-20 13:26:24 -0800538 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800539 """
Jon Hallacabffd2014-10-09 12:36:53 -0400540 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800541
Jon Hallacabffd2014-10-09 12:36:53 -0400542 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800543 If used as gitCheckout( "branch" ) it will do git checkout
544 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400545
546 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800547 branch of the ONOS repository. If it has any problems, it will return
548 main.ERROR.
549 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400550 successful then the function will return main.TRUE.
551
kelvin8ec71442015-01-15 16:57:00 -0800552 """
Jon Hallacabffd2014-10-09 12:36:53 -0400553 try:
kelvin8ec71442015-01-15 16:57:00 -0800554 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800555 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800556 main.log.info( self.name +
557 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800558 cmd = "git checkout " + branch
559 self.handle.sendline( cmd )
560 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800561 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800562 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700563 'Username for (.*): ',
564 'Already on \'',
Jon Hall7a8354f2015-06-10 15:37:00 -0700565 'Switched to (a new )?branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800566 pexpect.TIMEOUT,
567 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800568 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800569 'error: you need to resolve your current index first',
570 "You are in 'detached HEAD' state.",
571 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800572 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800573 if i == 0:
574 main.log.error(
575 self.name +
576 ": Git checkout had some issue..." )
577 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400578 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800579 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800580 main.log.error(
581 self.name +
582 ": Git checkout asking for username." +
583 " Please configure your local git repository to be able " +
584 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800585 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400586 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800587 elif i == 2:
588 main.log.info(
589 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800590 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800591 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800592 # main.log.info( "DEBUG: after checkout cmd = "+
593 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400594 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800595 elif i == 3:
596 main.log.info(
597 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800598 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800599 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800600 # main.log.info( "DEBUG: after checkout cmd = "+
601 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400602 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800603 elif i == 4:
604 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
605 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800606 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400607 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800608 elif i == 5:
609 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800610 main.log.error(
611 self.name +
612 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800613 "Your local changes to the following files would" +
614 " be overwritten by checkout:" +
615 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800616 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500617 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800618 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800619 main.log.error(
620 self.name +
621 ": Git checkout error: \n" +
622 "You need to resolve your current index first:" +
623 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800624 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500625 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800626 elif i == 7:
627 main.log.info(
628 self.name +
629 ": Git checkout " + str( branch ) +
630 " - You are in 'detached HEAD' state. HEAD is now at " +
631 str( branch ) )
632 self.handle.expect( self.home + "\$" )
633 return main.TRUE
634 elif i == 8: # Already in detached HEAD on the specified commit
635 main.log.info(
636 self.name +
637 ": Git Checkout %s : Already on commit" % branch )
638 self.handle.expect( self.home + "\$" )
639 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400640 else:
kelvin8ec71442015-01-15 16:57:00 -0800641 main.log.error(
642 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800643 ": Git Checkout - Unexpected response, " +
644 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800645 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400646 return main.ERROR
647
648 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800649 main.log.error( self.name + ": EOF exception found" )
650 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400651 main.cleanup()
652 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800653 except Exception:
654 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400655 main.cleanup()
656 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400657
pingping-lin6d23d9e2015-02-02 16:54:24 -0800658 def getBranchName( self ):
pingping-linf30cf272015-05-29 15:54:07 -0700659 main.log.info( "self.home = " )
660 main.log.info( self.home )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800661 self.handle.sendline( "cd " + self.home )
pingping-lin9bf3d8f2015-05-29 16:05:28 -0700662 self.handle.expect( self.home + "\$" )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800663 self.handle.sendline( "git name-rev --name-only HEAD" )
664 self.handle.expect( "git name-rev --name-only HEAD" )
665 self.handle.expect( "\$" )
666
667 lines = self.handle.before.splitlines()
668 if lines[1] == "master":
669 return "master"
670 elif lines[1] == "onos-1.0":
671 return "onos-1.0"
672 else:
673 main.log.info( lines[1] )
674 return "unexpected ONOS branch for SDN-IP test"
675
kelvin-onlabd3b64892015-01-20 13:26:24 -0800676 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800677 """
Jon Hall274b6642015-02-17 11:57:17 -0800678 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800679 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800680 """
Jon Hall45ec0922014-10-10 19:33:49 -0400681 try:
kelvin8ec71442015-01-15 16:57:00 -0800682 self.handle.sendline( "" )
683 self.handle.expect( "\$" )
684 self.handle.sendline(
685 "cd " +
686 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800687 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
688 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800689 # NOTE: for some reason there are backspaces inserted in this
690 # phrase when run from Jenkins on some tests
691 self.handle.expect( "never" )
692 self.handle.expect( "\$" )
693 response = ( self.name + ": \n" + str(
694 self.handle.before + self.handle.after ) )
695 self.handle.sendline( "cd " + self.home )
696 self.handle.expect( "\$" )
697 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400698 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500699 print line
700 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700701 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800702 for line in lines[ 2:-1 ]:
703 # Bracket replacement is for Wiki-compliant
704 # formatting. '<' or '>' are interpreted
705 # as xml specific tags that cause errors
706 line = line.replace( "<", "[" )
707 line = line.replace( ">", "]" )
pingping-lin763ee042015-05-20 17:45:30 -0700708 #main.log.wiki( "\t" + line )
709 main.log.wiki( line + "<br /> " )
710 main.log.summary( line )
711 main.log.wiki( "</blockquote>" )
712 main.log.summary("\n")
kelvin8ec71442015-01-15 16:57:00 -0800713 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400714 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800715 main.log.error( self.name + ": EOF exception found" )
716 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400717 main.cleanup()
718 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800719 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800720 main.log.error( self.name + ": TIMEOUT exception found" )
721 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800722 main.cleanup()
723 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800724 except Exception:
725 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400726 main.cleanup()
727 main.exit()
728
kelvin-onlabd3b64892015-01-20 13:26:24 -0800729 def createCellFile( self, benchIp, fileName, mnIpAddrs,
Pier88189b62016-09-07 17:01:53 -0700730 appString, onosIpAddrs, onosUser="sdn", useSSH=False ):
kelvin8ec71442015-01-15 16:57:00 -0800731 """
andrewonlab94282092014-10-10 13:00:11 -0400732 Creates a cell file based on arguments
733 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800734 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400735 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800736 * File name of the cell file ( fileName )
737 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800738 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400739 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800740 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400741 - Must be passed in as last arguments
Flavio Castrocc38a542016-03-03 13:15:46 -0800742 * ONOS USER (onosUser)
743 - optional argument to set ONOS_USER environment variable
kelvin8ec71442015-01-15 16:57:00 -0800744
andrewonlab94282092014-10-10 13:00:11 -0400745 NOTE: Assumes cells are located at:
746 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800747 """
748 # Variable initialization
Jon Hall6801cda2015-07-15 14:13:45 -0700749 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800750 # We want to create the cell file in the dependencies directory
751 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800752 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800753 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800754 cellFile = open( tempDirectory + fileName, 'w+' )
Jon Hall6801cda2015-07-15 14:13:45 -0700755 if isinstance( onosIpAddrs, types.StringType ):
756 onosIpAddrs = [ onosIpAddrs ]
kelvin8ec71442015-01-15 16:57:00 -0800757
cameron@onlab.us75900962015-03-30 13:22:49 -0700758 # App string is hardcoded environment variables
kelvin8ec71442015-01-15 16:57:00 -0800759 # That you may wish to use by default on startup.
cameron@onlab.us75900962015-03-30 13:22:49 -0700760 # Note that you may not want certain apps listed
kelvin8ec71442015-01-15 16:57:00 -0800761 # on here.
cameron@onlab.us75900962015-03-30 13:22:49 -0700762 appString = "export ONOS_APPS=" + appString
Flavio Castrocc38a542016-03-03 13:15:46 -0800763 onosGroup = "export ONOS_GROUP=" + onosUser
764 onosUser = "export ONOS_USER=" + onosUser
Chiyu Chengef109502016-11-21 15:51:38 -0800765 # FIXME: unset ONOS_USE_SSH when not using SSH?
Pier88189b62016-09-07 17:01:53 -0700766 if useSSH:
767 onosUseSSH = "export ONOS_USE_SSH=true"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800768 mnString = "export OCN="
kelvin-onlabf70fd542015-05-07 18:41:40 -0700769 if mnIpAddrs == "":
770 mnString = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800771 onosString = "export OC"
772 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800773
kelvin-onlabd3b64892015-01-20 13:26:24 -0800774 # Create ONOSNIC ip address prefix
kelvin-onlaba4074292015-07-09 15:19:49 -0700775 tempOnosIp = str( onosIpAddrs[ 0 ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800776 tempList = []
777 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800778 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800779 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800780 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800781 nicAddr = ".".join( tempList ) + ".*"
Jon Hall6c44c0b2016-04-20 15:21:00 -0700782 self.nicAddr = nicAddr
kelvin-onlabd3b64892015-01-20 13:26:24 -0800783 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400784
785 try:
kelvin8ec71442015-01-15 16:57:00 -0800786 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800787 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400788
kelvin-onlabd3b64892015-01-20 13:26:24 -0800789 for arg in onosIpAddrs:
790 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800791 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400792 # export OC1="10.128.20.11"
793 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800794 cellFile.write( onosString + str( tempCount ) +
Jon Hall6f665652015-09-18 10:08:07 -0700795 "=\"" + arg + "\"\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800796 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800797
Jon Hall6f665652015-09-18 10:08:07 -0700798 cellFile.write( "export OCI=$OC1\n" )
799 cellFile.write( mnString + "\"" + mnIpAddrs + "\"\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700800 cellFile.write( appString + "\n" )
Flavio Castrocc38a542016-03-03 13:15:46 -0800801 cellFile.write( onosGroup + "\n" )
802 cellFile.write( onosUser + "\n" )
Pier88189b62016-09-07 17:01:53 -0700803 if useSSH:
804 cellFile.write( onosUseSSH + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800805 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400806
kelvin8ec71442015-01-15 16:57:00 -0800807 # We use os.system to send the command to TestON cluster
808 # to account for the case in which TestON is not located
809 # on the same cluster as the ONOS bench
810 # Note that even if TestON is located on the same cluster
811 # as ONOS bench, you must setup passwordless ssh
812 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabc2b79102015-07-14 11:41:20 -0700813 os.system( "scp " + tempDirectory + fileName + " " +
814 self.user_name + "@" + self.ip_address + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400815
andrewonlab2a6c9342014-10-16 13:40:15 -0400816 return main.TRUE
817
andrewonlab94282092014-10-10 13:00:11 -0400818 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800819 main.log.error( self.name + ": EOF exception found" )
820 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400821 main.cleanup()
822 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800823 except Exception:
824 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400825 main.cleanup()
826 main.exit()
827
kelvin-onlabd3b64892015-01-20 13:26:24 -0800828 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800829 """
andrewonlab95ca1462014-10-09 14:04:24 -0400830 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800831 """
Hari Krishna03f530e2015-07-10 17:28:27 -0700832 import re
andrewonlab95ca1462014-10-09 14:04:24 -0400833 try:
834 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800835 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400836 main.cleanup()
837 main.exit()
838 else:
kelvin8ec71442015-01-15 16:57:00 -0800839 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800840 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800841 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400842 # and that this driver will have to change accordingly
Jon Hall3b489db2015-10-05 14:38:37 -0700843 self.handle.expect( str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800844 handleBefore = self.handle.before
845 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800846 # Get the rest of the handle
Jon Hall3b489db2015-10-05 14:38:37 -0700847 self.handle.expect( "\$" )
Jon Hall439c8912016-04-15 02:22:03 -0700848 time.sleep(10)
kelvin-onlabd3b64892015-01-20 13:26:24 -0800849 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400850
Hari Krishna03f530e2015-07-10 17:28:27 -0700851 cell_result = handleBefore + handleAfter + handleMore
suibin zhang116647a2016-05-06 16:30:09 -0700852 #print cell_result
Hari Krishna03f530e2015-07-10 17:28:27 -0700853 if( re.search( "No such cell", cell_result ) ):
854 main.log.error( "Cell call returned: " + handleBefore +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800855 handleAfter + handleMore )
Hari Krishna03f530e2015-07-10 17:28:27 -0700856 main.cleanup()
857 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400858 return main.TRUE
andrewonlab95ca1462014-10-09 14:04:24 -0400859 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800860 main.log.error( self.name + ": EOF exception found" )
861 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400862 main.cleanup()
863 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800864 except Exception:
865 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400866 main.cleanup()
867 main.exit()
868
kelvin-onlabd3b64892015-01-20 13:26:24 -0800869 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800870 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400871 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800872 """
873 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400874
andrewonlabc03bf6c2014-10-09 14:56:18 -0400875 try:
kelvin8ec71442015-01-15 16:57:00 -0800876 # Clean handle by sending empty and expecting $
877 self.handle.sendline( "" )
878 self.handle.expect( "\$" )
879 self.handle.sendline( "onos-verify-cell" )
880 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800881 handleBefore = self.handle.before
882 handleAfter = self.handle.after
kelvin-onlabd3b64892015-01-20 13:26:24 -0800883 main.log.info( "Verify cell returned: " + handleBefore +
Jon Hall3b489db2015-10-05 14:38:37 -0700884 handleAfter )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400885 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800886 except pexpect.ExceptionPexpect as e:
Jon Hall3b489db2015-10-05 14:38:37 -0700887 main.log.exception( self.name + ": Pexpect exception found: " )
kelvin8ec71442015-01-15 16:57:00 -0800888 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400889 main.cleanup()
890 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800891 except Exception:
892 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400893 main.cleanup()
894 main.exit()
895
jenkins1e99e7b2015-04-02 18:15:39 -0700896 def onosCfgSet( self, ONOSIp, configName, configParam ):
897 """
898 Uses 'onos <node-ip> cfg set' to change a parameter value of an
Jon Hall4ba53f02015-07-29 13:07:41 -0700899 application.
900
jenkins1e99e7b2015-04-02 18:15:39 -0700901 ex)
902 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
jenkins1e99e7b2015-04-02 18:15:39 -0700903 ONOSIp = '10.0.0.1'
904 configName = 'org.onosproject.myapp'
905 configParam = 'appSetting 1'
jenkins1e99e7b2015-04-02 18:15:39 -0700906 """
Jon Hall72280bc2016-01-25 14:29:05 -0800907 try:
908 cfgStr = "onos {} cfg set {} {}".format( ONOSIp,
909 configName,
910 configParam )
911 self.handle.sendline( "" )
912 self.handle.expect( ":~" )
913 self.handle.sendline( cfgStr )
914 self.handle.expect("cfg set")
915 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700916
Jon Hall72280bc2016-01-25 14:29:05 -0800917 paramValue = configParam.split(" ")[1]
918 paramName = configParam.split(" ")[0]
Jon Hall4ba53f02015-07-29 13:07:41 -0700919
Jon Hall72280bc2016-01-25 14:29:05 -0800920 checkStr = 'onos {} cfg get " {} {} " '.format( ONOSIp, configName, paramName )
Jon Hall4ba53f02015-07-29 13:07:41 -0700921
Jon Hall72280bc2016-01-25 14:29:05 -0800922 self.handle.sendline( checkStr )
923 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700924
Jon Hall72280bc2016-01-25 14:29:05 -0800925 if "value=" + paramValue + "," in self.handle.before:
926 main.log.info("cfg " + configName + " successfully set to " + configParam)
927 return main.TRUE
928 except pexpect.ExceptionPexpect as e:
929 main.log.exception( self.name + ": Pexpect exception found: " )
930 main.log.error( self.name + ": " + self.handle.before )
931 main.cleanup()
932 main.exit()
933 except Exception:
934 main.log.exception( self.name + ": Uncaught exception!" )
935 main.cleanup()
936 main.exit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700937
kelvin-onlabd3b64892015-01-20 13:26:24 -0800938 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800939 """
andrewonlab05e362f2014-10-10 00:40:57 -0400940 Uses 'onos' command to send various ONOS CLI arguments.
941 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800942 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400943 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800944
945 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400946 CLI commands for ONOS. Try to use this function first
947 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800948 function.
949 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400950 by starting onos, and typing in 'onos' to enter the
951 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800952 available commands.
953 """
andrewonlab05e362f2014-10-10 00:40:57 -0400954 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800955 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800956 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400957 return main.FALSE
958 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800959 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400960 return main.FALSE
961
kelvin8ec71442015-01-15 16:57:00 -0800962 cmdstr = str( cmdstr )
963 self.handle.sendline( "" )
964 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400965
kelvin-onlabd3b64892015-01-20 13:26:24 -0800966 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800967 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400968
kelvin-onlabd3b64892015-01-20 13:26:24 -0800969 handleBefore = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800970 main.log.info( "Command sent successfully" )
kelvin8ec71442015-01-15 16:57:00 -0800971 # Obtain return handle that consists of result from
972 # the onos command. The string may need to be
973 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800974 returnString = handleBefore
kelvin-onlabd3b64892015-01-20 13:26:24 -0800975 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400976 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800977 main.log.error( self.name + ": EOF exception found" )
978 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400979 main.cleanup()
980 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800981 except Exception:
982 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400983 main.cleanup()
984 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400985
Pier88189b62016-09-07 17:01:53 -0700986 def onosSecureSSH( self, userName="onos", userPWD="rocks", node=""):
987 """
988 Enables secure access to ONOS console
989 by removing default users & keys.
990
991 onos-secure-ssh -u onos -p rocks node
992
993 Returns: main.TRUE on success and main.FALSE on failure
994 """
995
996 try:
Chiyu Chengef109502016-11-21 15:51:38 -0800997 self.handle.sendline( "" )
998 self.handle.expect( "\$" )
Pier88189b62016-09-07 17:01:53 -0700999 self.handle.sendline( " onos-secure-ssh -u " + userName + " -p " + userPWD + " " + node )
1000
1001 # NOTE: this timeout may need to change depending on the network
1002 # and size of ONOS
1003 # TODO: Handle the other possible error
1004 i = self.handle.expect([ "Network\sis\sunreachable",
1005 "\$",
1006 pexpect.TIMEOUT ], timeout=180 )
1007 if i == 0:
1008 # can't reach ONOS node
1009 main.log.warn( "Network is unreachable" )
1010 self.handle.expect( "\$" )
1011 return main.FALSE
1012 elif i == 1:
1013 # Process started
1014 main.log.info(
1015 "Secure SSH performed on " +
1016 node)
1017 return main.TRUE
1018 except pexpect.EOF:
1019 main.log.error( self.name + ": EOF exception found" )
1020 main.log.error( self.name + ": " + self.handle.before )
1021 main.cleanup()
1022 main.exit()
1023 except Exception:
1024 main.log.exception( self.name + ": Uncaught exception!" )
1025 main.cleanup()
1026 main.exit()
1027
1028
kelvin-onlabd3b64892015-01-20 13:26:24 -08001029 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001030 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001031 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -08001032 If -f option is provided, it also forces an uninstall.
1033 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -04001034 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -08001035 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -04001036 files to certain onos nodes
1037
1038 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -08001039 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001040 try:
andrewonlab114768a2014-11-14 12:44:44 -05001041 if options:
kelvin8ec71442015-01-15 16:57:00 -08001042 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -05001043 else:
kelvin8ec71442015-01-15 16:57:00 -08001044 self.handle.sendline( "onos-install " + node )
1045 self.handle.expect( "onos-install " )
1046 # NOTE: this timeout may need to change depending on the network
1047 # and size of ONOS
1048 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001049 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -08001050 "ONOS\sis\salready\sinstalled",
Jeremyc72b2582016-02-26 18:27:38 -08001051 "already\sup-to-date",
Jon Hall3576f572016-08-23 10:01:07 -07001052 "does not exist",
Jeremyc72b2582016-02-26 18:27:38 -08001053 "\$",
Jon Hall6c44c0b2016-04-20 15:21:00 -07001054 pexpect.TIMEOUT ], timeout=180 )
Jon Hall7993bfc2014-10-09 16:30:14 -04001055 if i == 0:
Jon Hall3576f572016-08-23 10:01:07 -07001056 # can't reach ONOS node
kelvin8ec71442015-01-15 16:57:00 -08001057 main.log.warn( "Network is unreachable" )
Jon Hall3b489db2015-10-05 14:38:37 -07001058 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001059 return main.FALSE
1060 elif i == 1:
Jon Hall3576f572016-08-23 10:01:07 -07001061 # Process started
kelvin8ec71442015-01-15 16:57:00 -08001062 main.log.info(
1063 "ONOS was installed on " +
1064 node +
1065 " and started" )
Jon Hall3b489db2015-10-05 14:38:37 -07001066 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001067 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001068 elif i == 2 or i == 3:
1069 # same bits are already on ONOS node
Jeremyc72b2582016-02-26 18:27:38 -08001070 main.log.info( "ONOS is already installed on " + node )
1071 self.handle.expect( "\$" )
1072 return main.TRUE
1073 elif i == 4:
Jon Hall3576f572016-08-23 10:01:07 -07001074 # onos not packaged
1075 main.log.error( "ONOS package not found." )
1076 self.handle.expect( "\$" )
1077 return main.FALSE
1078 elif i == 5:
1079 # prompt
Jeremyc72b2582016-02-26 18:27:38 -08001080 main.log.info( "ONOS was installed on " + node )
1081 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001082 elif i == 6:
1083 # timeout
kelvin8ec71442015-01-15 16:57:00 -08001084 main.log.info(
1085 "Installation of ONOS on " +
1086 node +
1087 " timed out" )
Jon Hall3b489db2015-10-05 14:38:37 -07001088 self.handle.expect( "\$" )
Jon Hall53c5e662016-04-13 16:06:56 -07001089 main.log.warn( self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001090 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -04001091 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001092 main.log.error( self.name + ": EOF exception found" )
1093 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -04001094 main.cleanup()
1095 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001096 except Exception:
1097 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -04001098 main.cleanup()
1099 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -04001100
kelvin-onlabd3b64892015-01-20 13:26:24 -08001101 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001102 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001103 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001104 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001105 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001106 try:
kelvin8ec71442015-01-15 16:57:00 -08001107 self.handle.sendline( "" )
1108 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001109 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001110 " start" )
1111 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -04001112 "Job\sis\salready\srunning",
1113 "start/running",
Jeremyd0e9a6d2016-03-02 11:28:52 -08001114 "\$",
andrewonlab8d0d7d72014-10-09 16:33:15 -04001115 "Unknown\sinstance",
Jeremy Songster14c13572016-04-21 17:34:03 -07001116 pexpect.TIMEOUT ], timeout=180 )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001117 if i == 0:
Jon Halleab7a242016-03-04 10:20:43 -08001118 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001119 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001120 return main.TRUE
1121 elif i == 1:
Jon Halleab7a242016-03-04 10:20:43 -08001122 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001123 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001124 return main.TRUE
Jeremyd0e9a6d2016-03-02 11:28:52 -08001125 elif i == 2:
1126 main.log.info( "ONOS service started" )
1127 return main.TRUE
andrewonlab8d0d7d72014-10-09 16:33:15 -04001128 else:
Jon Halleab7a242016-03-04 10:20:43 -08001129 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001130 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001131 main.cleanup()
1132 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001133 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001134 main.log.error( self.name + ": EOF exception found" )
1135 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001136 main.cleanup()
1137 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001138 except Exception:
1139 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001140 main.cleanup()
1141 main.exit()
1142
kelvin-onlabd3b64892015-01-20 13:26:24 -08001143 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001144 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001145 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001146 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001147 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001148 try:
kelvin8ec71442015-01-15 16:57:00 -08001149 self.handle.sendline( "" )
1150 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001151 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001152 " stop" )
1153 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -04001154 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -07001155 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -04001156 "Unknown\sinstance",
YPZhang77badfc2016-03-09 10:28:59 -08001157 "\$",
Jeremy Songster14c13572016-04-21 17:34:03 -07001158 pexpect.TIMEOUT ], timeout=180 )
andrewonlab2b30bd32014-10-09 16:48:55 -04001159 if i == 0:
YPZhang77badfc2016-03-09 10:28:59 -08001160 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001161 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001162 return main.TRUE
1163 elif i == 1:
YPZhang77badfc2016-03-09 10:28:59 -08001164 self.handle.expect( "\$" )
Jon Hall65844a32015-03-09 19:09:37 -07001165 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001166 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -04001167 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -07001168 elif i == 2:
YPZhang77badfc2016-03-09 10:28:59 -08001169 self.handle.expect( "\$" )
Jon Hall61282e32015-03-19 11:34:11 -07001170 main.log.warn( "ONOS wasn't running" )
1171 return main.TRUE
YPZhang77badfc2016-03-09 10:28:59 -08001172 elif i == 3:
1173 main.log.info( "ONOS service stopped" )
1174 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -04001175 else:
kelvin8ec71442015-01-15 16:57:00 -08001176 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001177 return main.FALSE
andrewonlab2b30bd32014-10-09 16:48:55 -04001178 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001179 main.log.error( self.name + ": EOF exception found" )
1180 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -04001181 main.cleanup()
1182 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001183 except Exception:
1184 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001185 main.cleanup()
1186 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001187
kelvin-onlabd3b64892015-01-20 13:26:24 -08001188 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -08001189 """
andrewonlabc8d47972014-10-09 16:52:36 -04001190 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -08001191 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -04001192 if needed
kelvin8ec71442015-01-15 16:57:00 -08001193 """
andrewonlabc8d47972014-10-09 16:52:36 -04001194 try:
kelvin8ec71442015-01-15 16:57:00 -08001195 self.handle.sendline( "" )
Jeremy Songster14c13572016-04-21 17:34:03 -07001196 self.handle.expect( "\$", timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001197 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
Jeremy Songster14c13572016-04-21 17:34:03 -07001198 self.handle.expect( "\$", timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001199 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
kelvin8ec71442015-01-15 16:57:00 -08001200 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -04001201 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -07001202 except pexpect.TIMEOUT:
1203 main.log.exception( self.name + ": Timeout in onosUninstall" )
1204 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -04001205 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001206 main.log.error( self.name + ": EOF exception found" )
1207 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -04001208 main.cleanup()
1209 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001210 except Exception:
1211 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -04001212 main.cleanup()
1213 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -04001214
kelvin-onlabd3b64892015-01-20 13:26:24 -08001215 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001216 """
andrewonlabaedc8332014-12-04 12:43:03 -05001217 Issues the command 'onos-die <node-ip>'
1218 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -08001219 """
andrewonlabaedc8332014-12-04 12:43:03 -05001220 try:
kelvin8ec71442015-01-15 16:57:00 -08001221 self.handle.sendline( "" )
1222 self.handle.expect( "\$" )
Jeremyf0aecdb2016-03-30 13:19:57 -07001223 cmdStr = "onos-die " + str( nodeIp )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001224 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001225 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001226 "Killing\sONOS",
1227 "ONOS\sprocess\sis\snot\srunning",
Jeremy Songster14c13572016-04-21 17:34:03 -07001228 pexpect.TIMEOUT ], timeout=60 )
andrewonlabaedc8332014-12-04 12:43:03 -05001229 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001230 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001231 " was killed and stopped" )
Jon Hall53c5e662016-04-13 16:06:56 -07001232 self.handle.sendline( "" )
1233 self.handle.expect( "\$" )
andrewonlabaedc8332014-12-04 12:43:03 -05001234 return main.TRUE
1235 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001236 main.log.info( "ONOS process was not running" )
Jon Hall53c5e662016-04-13 16:06:56 -07001237 self.handle.sendline( "" )
1238 self.handle.expect( "\$" )
andrewonlabaedc8332014-12-04 12:43:03 -05001239 return main.FALSE
1240 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001241 main.log.error( self.name + ": EOF exception found" )
1242 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -05001243 main.cleanup()
1244 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001245 except Exception:
1246 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -05001247 main.cleanup()
1248 main.exit()
1249
kelvin-onlabd3b64892015-01-20 13:26:24 -08001250 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001251 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001252 Calls the command: 'onos-kill [<node-ip>]'
1253 "Remotely, and unceremoniously kills the ONOS instance running on
1254 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001255 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001256 try:
kelvin8ec71442015-01-15 16:57:00 -08001257 self.handle.sendline( "" )
1258 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001259 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001260 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -04001261 "\$",
1262 "No\sroute\sto\shost",
1263 "password:",
Jeremy Songster14c13572016-04-21 17:34:03 -07001264 pexpect.TIMEOUT ], timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -08001265
andrewonlabe8e56fd2014-10-09 17:12:44 -04001266 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001267 main.log.info(
1268 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001269 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001270 return main.TRUE
1271 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001272 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001273 return main.FALSE
1274 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001275 main.log.info(
1276 "Passwordless login for host: " +
1277 str( nodeIp ) +
1278 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001279 return main.FALSE
1280 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001281 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001282 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001283
andrewonlabe8e56fd2014-10-09 17:12:44 -04001284 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001285 main.log.error( self.name + ": EOF exception found" )
1286 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001287 main.cleanup()
1288 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001289 except Exception:
1290 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001291 main.cleanup()
1292 main.exit()
1293
kelvin-onlabd3b64892015-01-20 13:26:24 -08001294 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001295 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001296 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001297 a cleaner environment.
1298
andrewonlab19fbdca2014-11-14 12:55:59 -05001299 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001300 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001301 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001302 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001303 try:
kelvin8ec71442015-01-15 16:57:00 -08001304 self.handle.sendline( "" )
1305 self.handle.expect( "\$" )
1306 self.handle.sendline( "onos-remove-raft-logs" )
1307 # Sometimes this command hangs
1308 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1309 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001310 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001311 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1312 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001313 if i == 1:
1314 return main.FALSE
andrewonlab19fbdca2014-11-14 12:55:59 -05001315 return main.TRUE
andrewonlab19fbdca2014-11-14 12:55:59 -05001316 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001317 main.log.error( self.name + ": EOF exception found" )
1318 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001319 main.cleanup()
1320 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001321 except Exception:
1322 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001323 main.cleanup()
1324 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001325
kelvin-onlabd3b64892015-01-20 13:26:24 -08001326 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001327 """
1328 Calls the command 'onos-start-network [ <mininet-topo> ]
1329 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001330 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001331 cell."
andrewonlab94282092014-10-10 13:00:11 -04001332 * Specify mininet topology file name for mntopo
1333 * Topo files should be placed at:
1334 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001335
andrewonlab94282092014-10-10 13:00:11 -04001336 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001337 """
andrewonlab94282092014-10-10 13:00:11 -04001338 try:
1339 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001340 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001341 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001342
kelvin8ec71442015-01-15 16:57:00 -08001343 mntopo = str( mntopo )
1344 self.handle.sendline( "" )
1345 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001346
kelvin8ec71442015-01-15 16:57:00 -08001347 self.handle.sendline( "onos-start-network " + mntopo )
1348 self.handle.expect( "mininet>" )
1349 main.log.info( "Network started, entered mininet prompt" )
1350
1351 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001352
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 )
andrewonlab94282092014-10-10 13:00:11 -04001356 main.cleanup()
1357 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001358 except Exception:
1359 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001360 main.cleanup()
1361 main.exit()
1362
Jeremy Songster14c13572016-04-21 17:34:03 -07001363 def isup( self, node="", timeout=240 ):
kelvin8ec71442015-01-15 16:57:00 -08001364 """
1365 Run's onos-wait-for-start which only returns once ONOS is at run
Cameron Franke9c94fb02015-01-21 10:20:20 -08001366 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001367
Jon Hall7993bfc2014-10-09 16:30:14 -04001368 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001369 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001370 try:
Jon Hall3b489db2015-10-05 14:38:37 -07001371 self.handle.sendline( "onos-wait-for-start " + node )
1372 self.handle.expect( "onos-wait-for-start" )
kelvin8ec71442015-01-15 16:57:00 -08001373 # NOTE: this timeout is arbitrary"
Cameron Franke9c94fb02015-01-21 10:20:20 -08001374 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001375 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001376 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001377 return main.TRUE
1378 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001379 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001380 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001381 main.log.error( "ONOS has not started yet" )
1382 self.handle.send( "\x03" ) # Control-C
1383 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001384 return main.FALSE
1385 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001386 main.log.error( self.name + ": EOF exception found" )
1387 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001388 main.cleanup()
1389 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001390 except Exception:
1391 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001392 main.cleanup()
1393 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001394
kelvin-onlabd3b64892015-01-20 13:26:24 -08001395 def pushTestIntentsShell(
1396 self,
1397 dpidSrc,
1398 dpidDst,
1399 numIntents,
1400 dirFile,
1401 onosIp,
1402 numMult="",
1403 appId="",
1404 report=True,
1405 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001406 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001407 Description:
kelvin8ec71442015-01-15 16:57:00 -08001408 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001409 better parallelize the results than the CLI
1410 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001411 * dpidSrc: specify source dpid
1412 * dpidDst: specify destination dpid
1413 * numIntents: specify number of intents to push
1414 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001415 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001416 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001417 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001418 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001419 """
1420 try:
1421 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001422 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001423 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001424 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001425 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001426 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001427
kelvin-onlabd3b64892015-01-20 13:26:24 -08001428 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1429 if not numMult:
1430 addIntents = addDpid + " " + str( numIntents )
1431 elif numMult:
1432 addIntents = addDpid + " " + str( numIntents ) + " " +\
1433 str( numMult )
1434 if appId:
1435 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001436 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001437 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001438
andrewonlabaedc8332014-12-04 12:43:03 -05001439 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001440 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001441 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001442 sendCmd = addApp + " &"
1443 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001444
kelvin-onlabd3b64892015-01-20 13:26:24 -08001445 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001446
1447 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001448 main.log.error( self.name + ": EOF exception found" )
1449 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001450 main.cleanup()
1451 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001452 except Exception:
1453 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001454 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001455 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001456
kelvin-onlabd3b64892015-01-20 13:26:24 -08001457 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001458 """
andrewonlab970399c2014-11-07 13:09:32 -05001459 Capture all packet activity and store in specified
1460 directory/file
1461
1462 Required:
1463 * interface: interface to capture
1464 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001465 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001466 try:
1467 self.handle.sendline( "" )
1468 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001469
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001470 self.handle.sendline( "tshark -i " + str( interface ) + " -t e -w " + str( dirFile ) + " &" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001471 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001472 self.handle.expect( "Capturing on" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001473 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001474 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001475
Jon Hallfebb1c72015-03-05 13:30:09 -08001476 main.log.info( "Tshark started capturing files on " +
1477 str( interface ) + " and saving to directory: " +
1478 str( dirFile ) )
1479 except pexpect.EOF:
1480 main.log.error( self.name + ": EOF exception found" )
1481 main.log.error( self.name + ": " + self.handle.before )
1482 main.cleanup()
1483 main.exit()
1484 except Exception:
1485 main.log.exception( self.name + ": Uncaught exception!" )
1486 main.cleanup()
1487 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001488
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001489 def onosTopoCfg( self, onosIp, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001490 """
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001491 Description:
1492 Execute onos-topo-cfg command
1493 Required:
1494 onosIp - IP of the onos node you want to send the json to
1495 jsonFile - File path of the json file
1496 Return:
1497 Returns main.TRUE if the command is successfull; Returns
1498 main.FALSE if there was an error
kelvin8ec71442015-01-15 16:57:00 -08001499 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001500 try:
kelvin8ec71442015-01-15 16:57:00 -08001501 self.handle.sendline( "" )
1502 self.handle.expect( "\$" )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001503 cmd = "onos-topo-cfg "
1504 self.handle.sendline( cmd + str( onosIp ) + " " + jsonFile )
1505 handle = self.handle.before
1506 print handle
1507 if "Error" in handle:
1508 main.log.error( self.name + ": " + self.handle.before )
1509 return main.FALSE
1510 else:
1511 self.handle.expect( "\$" )
1512 return main.TRUE
1513
Jon Hallfebb1c72015-03-05 13:30:09 -08001514 except pexpect.EOF:
1515 main.log.error( self.name + ": EOF exception found" )
1516 main.log.error( self.name + ": " + self.handle.before )
1517 main.cleanup()
1518 main.exit()
1519 except Exception:
1520 main.log.exception( self.name + ": Uncaught exception!" )
1521 main.cleanup()
1522 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001523
jenkins1e99e7b2015-04-02 18:15:39 -07001524 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001525 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001526 Required:
kelvin8ec71442015-01-15 16:57:00 -08001527 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001528 * directory to store results
1529 Optional:
1530 * interface - default: eth0
Jon Hall4ba53f02015-07-29 13:07:41 -07001531 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001532 Description:
1533 Uses tshark command to grep specific group of packets
1534 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001535 The timestamp is hardcoded to be in epoch
1536 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001537 try:
1538 self.handle.sendline( "" )
1539 self.handle.expect( "\$" )
1540 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001541 if grepOptions:
1542 grepStr = "grep "+str(grepOptions)
1543 else:
1544 grepStr = "grep"
Jon Hall4ba53f02015-07-29 13:07:41 -07001545
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001546 cmd = (
1547 "sudo tshark -i " +
Jon Hallfebb1c72015-03-05 13:30:09 -08001548 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001549 " -t e | " +
1550 grepStr + " --line-buffered \"" +
Jon Hallfebb1c72015-03-05 13:30:09 -08001551 str(grep) +
1552 "\" >" +
1553 directory +
1554 " &" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001555 self.handle.sendline(cmd)
1556 main.log.info(cmd)
Jon Hallfebb1c72015-03-05 13:30:09 -08001557 self.handle.expect( "Capturing on" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001558 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001559 self.handle.expect( "\$" )
1560 except pexpect.EOF:
1561 main.log.error( self.name + ": EOF exception found" )
1562 main.log.error( self.name + ": " + self.handle.before )
1563 main.cleanup()
1564 main.exit()
1565 except Exception:
1566 main.log.exception( self.name + ": Uncaught exception!" )
1567 main.cleanup()
1568 main.exit()
1569
kelvin-onlabd3b64892015-01-20 13:26:24 -08001570 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001571 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001572 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001573 """
1574 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001575 try:
1576 self.execute( cmd="sudo rm /tmp/wireshark*" )
1577 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001578 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1579 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001580 self.handle.sendline( "" )
1581 main.log.info( "Tshark stopped" )
1582 except pexpect.EOF:
1583 main.log.error( self.name + ": EOF exception found" )
1584 main.log.error( self.name + ": " + self.handle.before )
1585 main.cleanup()
1586 main.exit()
1587 except Exception:
1588 main.log.exception( self.name + ": Uncaught exception!" )
1589 main.cleanup()
1590 main.exit()
1591
kelvin8ec71442015-01-15 16:57:00 -08001592 def ptpd( self, args ):
1593 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001594 Initiate ptp with user-specified args.
1595 Required:
1596 * args: specify string of args after command
1597 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001598 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001599 try:
kelvin8ec71442015-01-15 16:57:00 -08001600 self.handle.sendline( "sudo ptpd " + str( args ) )
1601 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001602 "Multiple",
1603 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001604 "\$" ] )
1605 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001606
andrewonlab0c38a4a2014-10-28 18:35:35 -04001607 if i == 0:
1608 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001609 main.log.info( "ptpd returned an error: " +
1610 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001611 return handle
1612 elif i == 1:
1613 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001614 main.log.error( "ptpd returned an error: " +
1615 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001616 return handle
1617 else:
1618 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001619
andrewonlab0c38a4a2014-10-28 18:35:35 -04001620 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001621 main.log.error( self.name + ": EOF exception found" )
1622 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001623 main.cleanup()
1624 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001625 except Exception:
1626 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001627 main.cleanup()
1628 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001629
Pier50f0bc62016-09-07 17:53:40 -07001630 def dumpONOSCmd(self, ONOSIp, CMD, destDir, filename, options=""):
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001631 """
Pier50f0bc62016-09-07 17:53:40 -07001632 Dump Cmd to a desired directory.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001633 For debugging purposes, you may want to use
Pier50f0bc62016-09-07 17:53:40 -07001634 this function to capture Cmd at a given point in time.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001635 Localtime will be attached to the filename
1636
1637 Required:
1638 * ONOSIp: the IP of the target ONOS instance
Pier50f0bc62016-09-07 17:53:40 -07001639 * CMD: the command to dump;
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001640 * destDir: specify directory to copy to.
1641 ex ) /tmp/
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001642 * fileName: Name of the file
Pier50f0bc62016-09-07 17:53:40 -07001643 * options: Options for ONOS command
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001644 """
1645
1646 localtime = time.strftime( '%x %X' )
1647 localtime = localtime.replace( "/", "" )
1648 localtime = localtime.replace( " ", "_" )
1649 localtime = localtime.replace( ":", "" )
1650 if destDir[ -1: ] != "/":
1651 destDir += "/"
Pier50f0bc62016-09-07 17:53:40 -07001652 cmd=CMD + " " + options + " > " + str( destDir ) + str( filename ) + localtime
1653 return self.onosCli(ONOSIp, cmd)
Flavio Castrob7718952016-05-18 08:53:41 -07001654
kelvin-onlabd3b64892015-01-20 13:26:24 -08001655 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001656 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001657 """
1658 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001659 Current implementation of ONOS deletes its karaf
1660 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001661 you may want to use this function to capture
1662 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001663 Localtime will be attached to the filename
1664
1665 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001666 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001667 copy.
kelvin8ec71442015-01-15 16:57:00 -08001668 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001669 For copying multiple files, leave copyFileName
1670 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001671 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001672 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001673 ex ) /tmp/
1674 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001675 * copyFileName: If you want to rename the log
1676 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001677 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001678 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001679 try:
Flavio Castro09ab59d2016-05-25 17:01:35 -07001680 localtime = time.strftime( '%H %M' )
kelvin8ec71442015-01-15 16:57:00 -08001681 localtime = localtime.replace( "/", "" )
1682 localtime = localtime.replace( " ", "_" )
1683 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001684 if destDir[ -1: ] != "/":
1685 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001686
kelvin-onlabd3b64892015-01-20 13:26:24 -08001687 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001688 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1689 str( destDir ) + str( copyFileName ) +
1690 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001691 self.handle.expect( "cp" )
1692 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001693 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001694 self.handle.sendline( "cp " + str( logToCopy ) +
1695 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001696 self.handle.expect( "cp" )
1697 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001698
kelvin8ec71442015-01-15 16:57:00 -08001699 return self.handle.before
1700
1701 except pexpect.EOF:
1702 main.log.error( "Copying files failed" )
1703 main.log.error( self.name + ": EOF exception found" )
1704 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001705 except Exception:
1706 main.log.exception( "Copying files failed" )
1707
Jon Hall16b72c42015-05-20 10:23:36 -07001708 def checkLogs( self, onosIp, restart=False):
kelvin8ec71442015-01-15 16:57:00 -08001709 """
Jon Hall94fd0472014-12-08 11:52:42 -08001710 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001711 If restart is True, use the old version of onos-check-logs which
1712 does not print the full stacktrace, but shows the entire log file,
1713 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001714 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001715 """
Jon Hall94fd0472014-12-08 11:52:42 -08001716 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001717 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001718 if restart:
1719 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001720 self.handle.sendline( cmd )
1721 self.handle.expect( cmd )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001722 self.handle.expect( "\$ " )
Jon Hall94fd0472014-12-08 11:52:42 -08001723 response = self.handle.before
1724 return response
1725 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001726 main.log.error( "Lost ssh connection" )
1727 main.log.error( self.name + ": EOF exception found" )
1728 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001729 except Exception:
1730 main.log.exception( self.name + ": Uncaught exception!" )
1731 main.cleanup()
1732 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001733
kelvin-onlabd3b64892015-01-20 13:26:24 -08001734 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001735 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001736 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001737 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001738 try:
kelvin8ec71442015-01-15 16:57:00 -08001739 self.handle.sendline( "" )
1740 self.handle.expect( "\$" )
1741 self.handle.sendline( "onos-service " + str( node ) +
1742 " status" )
1743 i = self.handle.expect( [
You Wangef1e6572016-03-08 12:53:18 -08001744 "start/running",
You Wang7bd83062016-03-01 11:50:00 -08001745 "Running ...",
You Wangef1e6572016-03-08 12:53:18 -08001746 "stop/waiting",
You Wang7bd83062016-03-01 11:50:00 -08001747 "Not Running ...",
kelvin8ec71442015-01-15 16:57:00 -08001748 pexpect.TIMEOUT ], timeout=120 )
YPZhangfebf7302016-05-24 16:45:56 -07001749 self.handle.sendline( "" )
1750 self.handle.expect( "\$" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001751
You Wangef1e6572016-03-08 12:53:18 -08001752 if i == 0 or i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001753 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001754 return main.TRUE
You Wangef1e6572016-03-08 12:53:18 -08001755 elif i == 2 or i == 3:
kelvin8ec71442015-01-15 16:57:00 -08001756 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001757 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001758 main.cleanup()
1759 main.exit()
1760 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001761 main.log.error( self.name + ": EOF exception found" )
1762 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001763 main.cleanup()
1764 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001765 except Exception:
1766 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001767 main.cleanup()
1768 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001769
Jon Hall63604932015-02-26 17:09:50 -08001770 def setIpTables( self, ip, port='', action='add', packet_type='',
1771 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001772 """
Jon Hall21270ac2015-02-16 17:59:55 -08001773 Description:
1774 add or remove iptables rule to DROP (default) packets from
1775 specific IP and PORT
1776 Usage:
1777 * specify action ('add' or 'remove')
1778 when removing, pass in the same argument as you would add. It will
1779 delete that specific rule.
1780 * specify the ip to block
1781 * specify the destination port to block (defaults to all ports)
1782 * optional packet type to block (default tcp)
1783 * optional iptables rule (default DROP)
1784 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001785 * States boolean toggles adding all supported tcp states to the
1786 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001787 Returns:
1788 main.TRUE on success or
1789 main.FALSE if given invalid input or
1790 main.ERROR if there is an error in response from iptables
1791 WARNING:
1792 * This function uses root privilege iptables command which may result
1793 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001794 """
Jon Hall21270ac2015-02-16 17:59:55 -08001795
1796 # NOTE*********
1797 # The strict checking methods of this driver function is intentional
1798 # to discourage any misuse or error of iptables, which can cause
1799 # severe network errors
1800 # *************
1801
1802 # NOTE: Sleep needed to give some time for rule to be added and
1803 # registered to the instance. If you are calling this function
1804 # multiple times this sleep will prevent any errors.
1805 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001806 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001807 try:
1808 # input validation
1809 action_type = action.lower()
1810 rule = rule.upper()
1811 direction = direction.upper()
1812 if action_type != 'add' and action_type != 'remove':
1813 main.log.error( "Invalid action type. Use 'add' or "
1814 "'remove' table rule" )
1815 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1816 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1817 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1818 "'ACCEPT' or 'LOG' only." )
1819 if direction != 'INPUT' and direction != 'OUTPUT':
1820 # NOTE currently only supports rules INPUT and OUPTUT
1821 main.log.error( "Invalid rule. Valid directions are"
1822 " 'OUTPUT' or 'INPUT'" )
1823 return main.FALSE
1824 return main.FALSE
1825 return main.FALSE
1826 if action_type == 'add':
1827 # -A is the 'append' action of iptables
1828 actionFlag = '-A'
1829 elif action_type == 'remove':
1830 # -D is the 'delete' rule of iptables
1831 actionFlag = '-D'
1832 self.handle.sendline( "" )
1833 self.handle.expect( "\$" )
1834 cmd = "sudo iptables " + actionFlag + " " +\
1835 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001836 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001837 # " -p " + str( packet_type ) +\
1838 if packet_type:
1839 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001840 if port:
1841 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001842 if states:
1843 cmd += " -m state --state="
1844 #FIXME- Allow user to configure which states to block
1845 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001846 cmd += " -j " + str( rule )
1847
1848 self.handle.sendline( cmd )
1849 self.handle.expect( "\$" )
1850 main.log.warn( self.handle.before )
1851
1852 info_string = "On " + str( self.name )
1853 info_string += " " + str( action_type )
1854 info_string += " iptable rule [ "
1855 info_string += " IP: " + str( ip )
1856 info_string += " Port: " + str( port )
1857 info_string += " Rule: " + str( rule )
1858 info_string += " Direction: " + str( direction ) + " ]"
1859 main.log.info( info_string )
1860 return main.TRUE
1861 except pexpect.TIMEOUT:
1862 main.log.exception( self.name + ": Timeout exception in "
1863 "setIpTables function" )
1864 return main.ERROR
1865 except pexpect.EOF:
1866 main.log.error( self.name + ": EOF exception found" )
1867 main.log.error( self.name + ": " + self.handle.before )
1868 main.cleanup()
1869 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001870 except Exception:
1871 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001872 main.cleanup()
1873 main.exit()
1874
Jon Hall0468b042015-02-19 19:08:21 -08001875 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001876 """
Jon Hall0468b042015-02-19 19:08:21 -08001877 This method is used by STS to check the status of the controller
1878 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001879 """
Jon Hall0468b042015-02-19 19:08:21 -08001880 import re
1881 try:
1882 self.handle.sendline( "" )
1883 self.handle.expect( "\$" )
1884 self.handle.sendline( "cd " + self.home )
1885 self.handle.expect( "\$" )
1886 self.handle.sendline( "service onos status" )
1887 self.handle.expect( "\$" )
1888 response = self.handle.before
1889 if re.search( "onos start/running", response ):
1890 # onos start/running, process 10457
1891 return 'RUNNING'
1892 # FIXME: Implement this case
1893 # elif re.search( pattern, response ):
1894 # return 'STARTING'
1895 elif re.search( "onos stop/", response ):
1896 # onos stop/waiting
1897 # FIXME handle this differently?: onos stop/pre-stop
1898 return 'STOPPED'
1899 # FIXME: Implement this case
1900 # elif re.search( pattern, response ):
1901 # return 'FROZEN'
1902 else:
1903 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001904 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001905 main.log.warn( response )
1906 return 'ERROR', "Unknown response: %s" % response
1907 except pexpect.TIMEOUT:
1908 main.log.exception( self.name + ": Timeout exception in "
1909 "setIpTables function" )
1910 return 'ERROR', "Pexpect Timeout"
1911 except pexpect.EOF:
1912 main.log.error( self.name + ": EOF exception found" )
1913 main.log.error( self.name + ": " + self.handle.before )
1914 main.cleanup()
1915 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001916 except Exception:
1917 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001918 main.cleanup()
1919 main.exit()
1920
andrew@onlab.us3b087132015-03-11 15:00:08 -07001921 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1922 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001923 Create/formats the LinkGraph.cfg file based on arguments
1924 -only creates a linear topology and connects islands
1925 -evenly distributes devices
andrew@onlab.us3b087132015-03-11 15:00:08 -07001926 -must be called by ONOSbench
1927
Jon Hall4ba53f02015-07-29 13:07:41 -07001928 ONOSIpList - list of all of the node IPs to be used
1929
1930 deviceCount - number of switches to be assigned
andrew@onlab.us3b087132015-03-11 15:00:08 -07001931 '''
Jon Hall6509dbf2016-06-21 17:01:17 -07001932 main.log.info("Creating link graph configuration file." )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001933 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
Jon Hall4ba53f02015-07-29 13:07:41 -07001934 tempFile = "/tmp/linkGraph.cfg"
andrew@onlab.us3b087132015-03-11 15:00:08 -07001935
1936 linkGraph = open(tempFile, 'w+')
1937 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1938 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1939 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001940
andrew@onlab.us3b087132015-03-11 15:00:08 -07001941 clusterCount = len(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07001942
1943 if type(deviceCount) is int or type(deviceCount) is str:
andrew@onlab.us3b087132015-03-11 15:00:08 -07001944 deviceCount = int(deviceCount)
1945 switchList = [0]*(clusterCount+1)
1946 baselineSwitchCount = deviceCount/clusterCount
Jon Hall4ba53f02015-07-29 13:07:41 -07001947
andrew@onlab.us3b087132015-03-11 15:00:08 -07001948 for node in range(1, clusterCount + 1):
1949 switchList[node] = baselineSwitchCount
1950
1951 for node in range(1, (deviceCount%clusterCount)+1):
1952 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001953
andrew@onlab.us3b087132015-03-11 15:00:08 -07001954 if type(deviceCount) is list:
1955 main.log.info("Using provided device distribution")
1956 switchList = [0]
1957 for i in deviceCount:
1958 switchList.append(int(i))
1959
1960 tempList = ['0']
1961 tempList.extend(ONOSIpList)
1962 ONOSIpList = tempList
1963
1964 myPort = 6
1965 lastSwitch = 0
1966 for node in range(1, clusterCount+1):
1967 if switchList[node] == 0:
1968 continue
1969
1970 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001971
andrew@onlab.us3b087132015-03-11 15:00:08 -07001972 if node > 1:
1973 #connect to last device on previous node
Jon Hall4ba53f02015-07-29 13:07:41 -07001974 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
1975 linkGraph.write(line)
1976
1977 lastSwitch = 0
1978 for switch in range (0, switchList[node]-1):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001979 line = ""
1980 line = ("\t" + str(switch) + ":" + str(myPort))
1981 line += " -- "
1982 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
1983 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07001984 lastSwitch = switch+1
andrew@onlab.us3b087132015-03-11 15:00:08 -07001985 lastIp = ONOSIpList[node]
Jon Hall4ba53f02015-07-29 13:07:41 -07001986
andrew@onlab.us3b087132015-03-11 15:00:08 -07001987 #lastSwitch += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001988 if node < (clusterCount):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001989 #connect to first device on the next node
Jon Hall4ba53f02015-07-29 13:07:41 -07001990 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
andrew@onlab.us3b087132015-03-11 15:00:08 -07001991 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07001992
andrew@onlab.us3b087132015-03-11 15:00:08 -07001993 linkGraph.write("}\n")
1994 linkGraph.close()
1995
1996 #SCP
Jon Hall4ba53f02015-07-29 13:07:41 -07001997 os.system( "scp " + tempFile + " " + self.user_name + "@" + benchIp + ":" + linkGraphPath)
andrew@onlab.us3b087132015-03-11 15:00:08 -07001998 main.log.info("linkGraph.cfg creation complete")
1999
cameron@onlab.us75900962015-03-30 13:22:49 -07002000 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
Jon Hall4ba53f02015-07-29 13:07:41 -07002001
andrew@onlab.us3b087132015-03-11 15:00:08 -07002002 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002003 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
2004 deviceCount = number of switches to distribute, or list of values to use as custom distribution
cameron@onlab.us75900962015-03-30 13:22:49 -07002005 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 -07002006 '''
2007
Jon Hall6509dbf2016-06-21 17:01:17 -07002008 main.log.info("Configuring Null Device Provider" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002009 clusterCount = len(ONOSIpList)
2010
Jon Hall4ba53f02015-07-29 13:07:41 -07002011 try:
2012
cameron@onlab.us75900962015-03-30 13:22:49 -07002013 if type(deviceCount) is int or type(deviceCount) is str:
Jon Hall6509dbf2016-06-21 17:01:17 -07002014 main.log.info("Creating device distribution")
cameron@onlab.us75900962015-03-30 13:22:49 -07002015 deviceCount = int(deviceCount)
2016 switchList = [0]*(clusterCount+1)
2017 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002018
cameron@onlab.us75900962015-03-30 13:22:49 -07002019 for node in range(1, clusterCount + 1):
2020 switchList[node] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002021
cameron@onlab.us75900962015-03-30 13:22:49 -07002022 for node in range(1, (deviceCount%clusterCount)+1):
2023 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07002024
2025 if type(deviceCount) is list:
2026 main.log.info("Using provided device distribution")
2027
2028 if len(deviceCount) == clusterCount:
cameron@onlab.us75900962015-03-30 13:22:49 -07002029 switchList = ['0']
2030 switchList.extend(deviceCount)
Jon Hall4ba53f02015-07-29 13:07:41 -07002031
2032 if len(deviceCount) == (clusterCount + 1):
2033 if deviceCount[0] == '0' or deviceCount[0] == 0:
cameron@onlab.us75900962015-03-30 13:22:49 -07002034 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002035
cameron@onlab.us75900962015-03-30 13:22:49 -07002036 assert len(switchList) == (clusterCount + 1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002037
cameron@onlab.us75900962015-03-30 13:22:49 -07002038 except AssertionError:
Jon Hall4ba53f02015-07-29 13:07:41 -07002039 main.log.error( "Bad device/Ip list match")
cameron@onlab.us75900962015-03-30 13:22:49 -07002040 except TypeError:
2041 main.log.exception( self.name + ": Object not as expected" )
2042 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07002043 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002044 main.log.exception( self.name + ": Uncaught exception!" )
2045 main.cleanup()
2046 main.exit()
2047
andrew@onlab.us3b087132015-03-11 15:00:08 -07002048
2049 ONOSIp = [0]
2050 ONOSIp.extend(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07002051
andrew@onlab.us3b087132015-03-11 15:00:08 -07002052 devicesString = "devConfigs = "
2053 for node in range(1, len(ONOSIp)):
2054 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
2055 if node < clusterCount:
2056 devicesString += (",")
Jon Hall4ba53f02015-07-29 13:07:41 -07002057
2058 try:
cameron@onlab.us75900962015-03-30 13:22:49 -07002059 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
2060 self.handle.expect(":~")
2061 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
2062 self.handle.expect(":~")
andrew@onlab.us3b087132015-03-11 15:00:08 -07002063
cameron@onlab.us75900962015-03-30 13:22:49 -07002064 for i in range(10):
2065 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
2066 self.handle.expect(":~")
2067 verification = self.handle.before
2068 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
2069 break
2070 else:
2071 time.sleep(1)
andrew@onlab.us3b087132015-03-11 15:00:08 -07002072
cameron@onlab.us2cc8bf12015-04-02 14:12:30 -07002073 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002074
cameron@onlab.us75900962015-03-30 13:22:49 -07002075 except AssertionError:
2076 main.log.error("Incorrect Config settings: " + verification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002077 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002078 main.log.exception( self.name + ": Uncaught exception!" )
2079 main.cleanup()
2080 main.exit()
2081
Jon Hall4ba53f02015-07-29 13:07:41 -07002082 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002083 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002084 fileName default is currently the same as the default on ONOS, specify alternate file if
cameron@onlab.us75900962015-03-30 13:22:49 -07002085 you want to use a different topology file than linkGraph.cfg
andrew@onlab.us3b087132015-03-11 15:00:08 -07002086 '''
2087
Jon Hall4ba53f02015-07-29 13:07:41 -07002088
2089 try:
2090 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
2091 self.handle.expect(":~")
cameron@onlab.us75900962015-03-30 13:22:49 -07002092 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2093 self.handle.expect(":~")
Jon Hall4ba53f02015-07-29 13:07:41 -07002094
2095 for i in range(10):
2096 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
cameron@onlab.us75900962015-03-30 13:22:49 -07002097 self.handle.expect(":~")
2098 verification = self.handle.before
Jon Hall4ba53f02015-07-29 13:07:41 -07002099 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002100 break
Jon Hall4ba53f02015-07-29 13:07:41 -07002101 else:
cameron@onlab.us75900962015-03-30 13:22:49 -07002102 time.sleep(1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002103
cameron@onlab.us75900962015-03-30 13:22:49 -07002104 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002105
cameron@onlab.us75900962015-03-30 13:22:49 -07002106 except pexpect.EOF:
2107 main.log.error( self.name + ": EOF exception found" )
2108 main.log.error( self.name + ": " + self.handle.before )
2109 main.cleanup()
2110 main.exit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002111 except AssertionError:
2112 main.log.info("Settings did not post to ONOS")
Jon Hall4ba53f02015-07-29 13:07:41 -07002113 main.log.error(varification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002114 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002115 main.log.exception( self.name + ": Uncaught exception!" )
2116 main.log.error(varification)
2117 main.cleanup()
2118 main.exit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002119
kelvin-onlaba4074292015-07-09 15:19:49 -07002120 def getOnosIps( self ):
2121 """
2122 Get all onos IPs stored in
2123 """
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002124
kelvin-onlaba4074292015-07-09 15:19:49 -07002125 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002126
kelvin-onlaba4074292015-07-09 15:19:49 -07002127 def logReport( self, nodeIp, searchTerms, outputMode="s" ):
Jon Hallb4242222016-01-25 17:07:04 -08002128 """
2129 Searches the latest ONOS log file for the given search terms and
2130 prints the total occurances of each term. Returns to combined total of
2131 all occurances.
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002132
Jon Hallb4242222016-01-25 17:07:04 -08002133 Arguments:
2134 * nodeIp - The ip of the ONOS node where the log is located
2135 * searchTerms - A string to grep for or a list of strings to grep
2136 for in the ONOS log. Will print out the number of
2137 occurances for each term.
2138 Optional Arguments:
2139 * outputMode - 's' or 'd'. If 'd' will print the last 5 lines
2140 containing each search term as well as the total
2141 number of occurances of each term. Defaults to 's',
2142 which prints the simple output of just the number
2143 of occurances for each term.
2144 """
2145 try:
2146 main.log.info( " Log Report for {} ".format( nodeIp ).center( 70, '=' ) )
2147 if type( searchTerms ) is str:
2148 searchTerms = [searchTerms]
2149 numTerms = len( searchTerms )
2150 outputMode = outputMode.lower()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002151
Jon Hallb4242222016-01-25 17:07:04 -08002152 totalHits = 0
2153 logLines = []
2154 for termIndex in range( numTerms ):
2155 term = searchTerms[termIndex]
2156 logLines.append( [term] )
2157 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep " + term
2158 self.handle.sendline( cmd )
2159 self.handle.expect( ":~" )
2160 before = self.handle.before.splitlines()
2161 count = 0
2162 for line in before:
2163 if term in line and "grep" not in line:
2164 count += 1
2165 if before.index( line ) > ( len( before ) - 7 ):
2166 logLines[termIndex].append( line )
2167 main.log.info( "{}: {}".format( term, count ) )
2168 totalHits += count
2169 if termIndex == numTerms - 1:
2170 print "\n"
2171 if outputMode != "s":
2172 outputString = ""
2173 for term in logLines:
2174 outputString = term[0] + ": \n"
2175 for line in range( 1, len( term ) ):
2176 outputString += ( "\t" + term[line] + "\n" )
2177 if outputString != ( term[0] + ": \n" ):
2178 main.log.info( outputString )
2179 main.log.info( "=" * 70 )
2180 return totalHits
2181 except pexpect.EOF:
2182 main.log.error( self.name + ": EOF exception found" )
2183 main.log.error( self.name + ": " + self.handle.before )
2184 main.cleanup()
2185 main.exit()
2186 except pexpect.TIMEOUT:
2187 main.log.error( self.name + ": TIMEOUT exception found" )
2188 main.log.error( self.name + ": " + self.handle.before )
2189 main.cleanup()
2190 main.exit()
2191 except Exception:
2192 main.log.exception( self.name + ": Uncaught exception!" )
2193 main.cleanup()
2194 main.exit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002195
2196 def copyMininetFile( self, fileName, localPath, userName, ip,
2197 mnPath='~/mininet/custom/', timeout = 60 ):
2198 """
2199 Description:
2200 Copy mininet topology file from dependency folder in the test folder
2201 and paste it to the mininet machine's mininet/custom folder
2202 Required:
2203 fileName - Name of the topology file to copy
2204 localPath - File path of the mininet topology file
2205 userName - User name of the mininet machine to send the file to
2206 ip - Ip address of the mininet machine
2207 Optional:
2208 mnPath - of the mininet directory to send the file to
2209 Return:
2210 Return main.TRUE if successfully copied the file otherwise
2211 return main.FALSE
2212 """
2213
2214 try:
2215 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2216 str( ip ) + ":" + mnPath + fileName
2217
2218 self.handle.sendline( "" )
2219 self.handle.expect( "\$" )
2220
2221 main.log.info( self.name + ": Execute: " + cmd )
2222
2223 self.handle.sendline( cmd )
2224
2225 i = self.handle.expect( [ 'No such file',
2226 "100%",
2227 pexpect.TIMEOUT ] )
2228
2229 if i == 0:
2230 main.log.error( self.name + ": File " + fileName +
2231 " does not exist!" )
2232 return main.FALSE
2233
2234 if i == 1:
2235 main.log.info( self.name + ": File " + fileName +
2236 " has been copied!" )
2237 self.handle.sendline( "" )
2238 self.handle.expect( "\$" )
2239 return main.TRUE
2240
2241 except pexpect.EOF:
2242 main.log.error( self.name + ": EOF exception found" )
2243 main.log.error( self.name + ": " + self.handle.before )
2244 main.cleanup()
2245 main.exit()
2246 except pexpect.TIMEOUT:
2247 main.log.error( self.name + ": TIMEOUT exception found" )
2248 main.log.error( self.name + ": " + self.handle.before )
2249 main.cleanup()
2250 main.exit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002251
2252 def jvmSet(self, memory=8):
Jon Hall4ba53f02015-07-29 13:07:41 -07002253
cameron@onlab.us78b89652015-07-08 15:21:03 -07002254 import os
2255
2256 homeDir = os.path.expanduser('~')
2257 filename = "/onos/tools/package/bin/onos-service"
2258
2259 serviceConfig = open(homeDir + filename, 'w+')
2260 serviceConfig.write("#!/bin/bash\n ")
2261 serviceConfig.write("#------------------------------------- \n ")
2262 serviceConfig.write("# Starts ONOS Apache Karaf container\n ")
2263 serviceConfig.write("#------------------------------------- \n ")
2264 serviceConfig.write("#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n ")
2265 serviceConfig.write("""export JAVA_OPTS="${JAVA_OPTS:--Xms""" + str(memory) + "G -Xmx" + str(memory) + """G}" \n """)
2266 serviceConfig.write("[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n")
2267 serviceConfig.write("""${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """)
2268 serviceConfig.close()
2269
Jon Hall6c44c0b2016-04-20 15:21:00 -07002270 def createDBFile( self, testData ):
Jon Hall4ba53f02015-07-29 13:07:41 -07002271
cameron@onlab.us78b89652015-07-08 15:21:03 -07002272 filename = main.TEST + "DB"
2273 DBString = ""
Jon Hall4ba53f02015-07-29 13:07:41 -07002274
cameron@onlab.us78b89652015-07-08 15:21:03 -07002275 for item in testData:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002276 if type( item ) is string:
cameron@onlab.us78b89652015-07-08 15:21:03 -07002277 item = "'" + item + "'"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002278 if testData.index( item ) < len( testData - 1 ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002279 item += ","
Jon Hall6c44c0b2016-04-20 15:21:00 -07002280 DBString += str( item )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002281
Jon Hall6c44c0b2016-04-20 15:21:00 -07002282 DBFile = open( filename, "a" )
2283 DBFile.write( DBString )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002284 DBFile.close()
2285
Jon Hall6c44c0b2016-04-20 15:21:00 -07002286 def verifySummary( self, ONOSIp, *deviceCount ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002287
Jon Hall6c44c0b2016-04-20 15:21:00 -07002288 self.handle.sendline( "onos " + ONOSIp + " summary" )
2289 self.handle.expect( ":~" )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002290
2291 summaryStr = self.handle.before
2292 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2293
2294 #passed = "SCC(s)=1" in summaryStr
2295 #if deviceCount:
2296 # passed = "devices=" + str(deviceCount) + "," not in summaryStr
2297
GlennRC772363b2015-08-25 13:05:57 -07002298 passed = False
cameron@onlab.us78b89652015-07-08 15:21:03 -07002299 if "SCC(s)=1," in summaryStr:
2300 passed = True
Jon Hall6c44c0b2016-04-20 15:21:00 -07002301 print "Summary is verifed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002302 else:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002303 print "Summary failed"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002304
2305 if deviceCount:
2306 print" ============================="
Jon Hall6c44c0b2016-04-20 15:21:00 -07002307 checkStr = "devices=" + str( deviceCount[0] ) + ","
cameron@onlab.us78b89652015-07-08 15:21:03 -07002308 print "Checkstr: " + checkStr
2309 if checkStr not in summaryStr:
2310 passed = False
Jon Hall6c44c0b2016-04-20 15:21:00 -07002311 print "Device count failed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002312 else:
2313 print "device count verified"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002314
2315 return passed
Jon Hall6c44c0b2016-04-20 15:21:00 -07002316
Jon Hall8f6d4622016-05-23 15:27:18 -07002317 def getIpAddr( self, iface=None ):
Jon Hall6c44c0b2016-04-20 15:21:00 -07002318 """
2319 Update self.ip_address with numerical ip address. If multiple IP's are
2320 located on the device, will attempt to use self.nicAddr to choose the
2321 right one. Defaults to 127.0.0.1 if no other address is found or cannot
2322 determine the correct address.
2323
2324 ONLY WORKS WITH IPV4 ADDRESSES
2325 """
2326 try:
Jon Hall8f6d4622016-05-23 15:27:18 -07002327 LOCALHOST = "127.0.0.1"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002328 ipPat = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
2329 pattern = re.compile( ipPat )
2330 match = re.search( pattern, self.ip_address )
2331 if self.nicAddr:
2332 nicPat = self.nicAddr.replace( ".", "\." ).replace( "\*", r"\d{1,3}" )
2333 nicPat = re.compile( nicPat )
2334 else:
2335 nicPat = None
2336 # IF self.ip_address is an ip address and matches
2337 # self.nicAddr: return self.ip_address
2338 if match:
2339 curIp = match.group(0)
2340 if nicPat:
2341 nicMatch = re.search( nicPat, curIp )
2342 if nicMatch:
2343 return self.ip_address
Jon Hall8f6d4622016-05-23 15:27:18 -07002344 # ELSE: IF iface, return ip of interface
2345 cmd = "ifconfig"
2346 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2347 if iface:
2348 cmd += " " + str( iface )
2349 raw = subprocess.check_output( cmd.split() )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002350 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2351 ips = re.findall( ifPat, raw )
Jon Hall8f6d4622016-05-23 15:27:18 -07002352 if iface:
2353 if ips:
2354 ip = ips[0]
2355 self.ip_address = ip
2356 return ip
2357 else:
2358 main.log.error( "Error finding ip, ifconfig output:".format( raw ) )
2359 # ELSE: attempt to get address matching nicPat.
Jon Hall6c44c0b2016-04-20 15:21:00 -07002360 if nicPat:
2361 for ip in ips:
2362 curMatch = re.search( nicPat, ip )
2363 if curMatch:
2364 self.ip_address = ip
2365 return ip
Jon Hall8f6d4622016-05-23 15:27:18 -07002366 else: # If only one non-localhost ip, return that
2367 tmpList = [ ip for ip in ips if ip is not LOCALHOST ]
Jon Hall6c44c0b2016-04-20 15:21:00 -07002368 if len(tmpList) == 1:
2369 curIp = tmpList[0]
2370 self.ip_address = curIp
2371 return curIp
Jon Hall8f6d4622016-05-23 15:27:18 -07002372 # Either no non-localhost IPs, or more than 1
Jon Hallebccd352016-05-20 15:17:17 -07002373 main.log.warn( "getIpAddr failed to find a public IP address" )
Jon Hall8f6d4622016-05-23 15:27:18 -07002374 return LOCALHOST
Jon Halle1790952016-05-23 15:51:46 -07002375 except subprocess.CalledProcessError:
Jon Hall8f6d4622016-05-23 15:27:18 -07002376 main.log.exception( "Error executing ifconfig" )
2377 except IndexError:
2378 main.log.exception( "Error getting IP Address" )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002379 except Exception:
2380 main.log.exception( "Uncaught exception" )
suibin zhang116647a2016-05-06 16:30:09 -07002381
You Wangd1bcaca2016-10-24 15:23:26 -07002382 def startBasicONOS( self, nodeList, opSleep=60, onosStartupSleep=60 ):
suibin zhang116647a2016-05-06 16:30:09 -07002383 '''
2384 Start onos cluster with defined nodes, but only with drivers app
suibin zhang116647a2016-05-06 16:30:09 -07002385 '''
2386 import time
2387
2388 self.createCellFile( self.ip_address,
2389 "temp",
2390 self.ip_address,
2391 "drivers",
2392 nodeList )
2393
2394 main.log.info( self.name + ": Apply cell to environment" )
2395 cellResult = self.setCell( "temp" )
2396 verifyResult = self.verifyCell()
2397
2398 main.log.info( self.name + ": Creating ONOS package" )
You Wangd1bcaca2016-10-24 15:23:26 -07002399 packageResult = self.buckBuild( timeout=opSleep )
suibin zhang116647a2016-05-06 16:30:09 -07002400
2401 main.log.info( self.name + ": Installing ONOS package" )
2402 for nd in nodeList:
2403 self.onosInstall( node=nd )
2404
2405 main.log.info( self.name + ": Starting ONOS service" )
2406 time.sleep( onosStartupSleep )
2407
2408 onosStatus = True
2409 for nd in nodeList:
2410 onosStatus = onosStatus & self.isup( node = nd )
2411 #print "onosStatus is: " + str( onosStatus )
2412
2413 return main.TRUE if onosStatus else main.FALSE
2414
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002415 def onosNetCfg( self, controllerIps, path, fileName ):
2416 """
2417 Push a specified json file to ONOS through the onos-netcfg service
2418
2419 Required:
2420 controllerIps - the Ips of the ONOS nodes in the cluster
2421 path - the location of the file to be sent
2422 fileName - name of the json file to be sent
2423
2424 Returns main.TRUE on successfully sending json file, and main.FALSE if
2425 there is an error.
2426 """
2427 try:
2428 cmd = "onos-netcfg {0} {1}{2}.json".format( controllerIps, path, fileName )
2429 main.log.info( "Sending: " + cmd )
2430 main.ONOSbench.handle.sendline( cmd )
2431 handle = self.handle.before
2432 print handle
2433 if "Error" in handle:
2434 main.log.error( self.name + ": " + self.handle.before )
2435 return main.FALSE
2436 else:
2437 self.handle.expect( "\$" )
2438 return main.TRUE
2439 except pexpect.EOF:
2440 main.log.error( self.name + ": EOF exception found" )
2441 main.log.error( self.name + ": " + self.handle.before )
2442 main.cleanup()
2443 main.exit()
2444 except Exception:
2445 main.log.exception( self.name + ": Uncaught exception!" )
2446 main.cleanup()
Jeremy Songsterae01bba2016-07-11 15:39:17 -07002447 main.exit()