blob: cbe291b6a72023844d234337e3e27144c3bf973c [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
andrewonlabe8e56fd2014-10-09 17:12:44 -04002
kelvin8ec71442015-01-15 16:57:00 -08003"""
4This driver interacts with ONOS bench, the OSGi platform
5that configures the ONOS nodes. ( aka ONOS-next )
andrewonlabe8e56fd2014-10-09 17:12:44 -04006
kelvin8ec71442015-01-15 16:57:00 -08007Please follow the coding style demonstrated by existing
andrewonlabe8e56fd2014-10-09 17:12:44 -04008functions and document properly.
9
10If you are a contributor to the driver, please
11list your email here for future contact:
12
13jhall@onlab.us
14andrew@onlab.us
15
16OCT 9 2014
17
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab7735d852014-10-09 13:02:47 -040019import sys
Jon Hall05b2b432014-10-08 19:53:25 -040020import time
21import pexpect
kelvin-onlaba4074292015-07-09 15:19:49 -070022import os
andrewonlab7735d852014-10-09 13:02:47 -040023import os.path
pingping-lin6d23d9e2015-02-02 16:54:24 -080024from requests.models import Response
kelvin8ec71442015-01-15 16:57:00 -080025sys.path.append( "../" )
Jon Hall05b2b432014-10-08 19:53:25 -040026from drivers.common.clidriver import CLI
27
Jon Hall05b2b432014-10-08 19:53:25 -040028
kelvin8ec71442015-01-15 16:57:00 -080029class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040030
kelvin8ec71442015-01-15 16:57:00 -080031 def __init__( self ):
32 """
33 Initialize client
34 """
Jon Hallefbd9792015-03-05 16:11:36 -080035 self.name = None
36 self.home = None
37 self.handle = None
kelvin8ec71442015-01-15 16:57:00 -080038 super( CLI, self ).__init__()
39
40 def connect( self, **connectargs ):
41 """
Jon Hall05b2b432014-10-08 19:53:25 -040042 Creates ssh handle for ONOS "bench".
kelvin-onlaba4074292015-07-09 15:19:49 -070043 NOTE:
44 The ip_address would come from the topo file using the host tag, the
45 value can be an environment variable as well as a "localhost" to get
46 the ip address needed to ssh to the "bench"
kelvin8ec71442015-01-15 16:57:00 -080047 """
Jon Hall05b2b432014-10-08 19:53:25 -040048 try:
49 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080050 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us3b087132015-03-11 15:00:08 -070051 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040052 for key in self.options:
53 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080054 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040055 break
Jon Hall274b6642015-02-17 11:57:17 -080056 if self.home is None or self.home == "":
Jon Halle94919c2015-03-23 11:42:57 -070057 self.home = "~/onos"
Jon Hall21270ac2015-02-16 17:59:55 -080058
kelvin8ec71442015-01-15 16:57:00 -080059 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070060
61 for key in self.options:
62 if key == "nodes":
63 # Maximum number of ONOS nodes to run
64 self.maxNodes = int( self.options[ 'nodes' ] )
65 break
66 self.maxNodes = None
67
68
69 # Grabs all OC environment variables
70 self.onosIps = {} # Dictionary of all possible ONOS ip
71
72 try:
73 if self.maxNodes:
74 main.log.info( self.name + ": Creating cluster data with " +
75 str( self.maxNodes ) + " maximum number" +
76 " of nodes" )
77
78 for i in range( self.maxNodes ):
79 envString = "OC" + str( i + 1 )
80 self.onosIps[ envString ] = os.getenv( envString )
81
82 if not self.onosIps:
83 main.log.info( "Could not read any environment variable"
84 + " please load a cell file with all" +
85 " onos IP" )
86 else:
87 main.log.info( self.name + ": Found " +
88 str( self.onosIps.values() ) +
89 " ONOS IPs" )
90
91 except KeyError:
92 main.log.info( "Invalid environment variable" )
93 except Exception as inst:
94 main.log.error( "Uncaught exception: " + str( inst ) )
95
96 try:
97 if os.getenv( str( self.ip_address ) ) != None:
98 self.ip_address = os.getenv( str( self.ip_address ) )
99 else:
100 main.log.info( self.name +
101 ": Trying to connect to " +
102 self.ip_address )
103
104 except KeyError:
105 main.log.info( "Invalid host name," +
106 " connecting to local host instead" )
107 self.ip_address = 'localhost'
108 except Exception as inst:
109 main.log.error( "Uncaught exception: " + str( inst ) )
110
kelvin8ec71442015-01-15 16:57:00 -0800111 self.handle = super( OnosDriver, self ).connect(
112 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -0800113 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800114 port=self.port,
115 pwd=self.pwd,
116 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -0400117
kelvin8ec71442015-01-15 16:57:00 -0800118 self.handle.sendline( "cd " + self.home )
119 self.handle.expect( "\$" )
kelvin-onlaba4074292015-07-09 15:19:49 -0700120
Jon Hall05b2b432014-10-08 19:53:25 -0400121 if self.handle:
122 return self.handle
kelvin8ec71442015-01-15 16:57:00 -0800123 else:
124 main.log.info( "NO ONOS HANDLE" )
Jon Hall05b2b432014-10-08 19:53:25 -0400125 return main.FALSE
kelvin-onlaba4074292015-07-09 15:19:49 -0700126
Jon Hall05b2b432014-10-08 19:53:25 -0400127 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800128 main.log.error( self.name + ": EOF exception found" )
129 main.log.error( self.name + ": " + self.handle.before )
Jon Hall05b2b432014-10-08 19:53:25 -0400130 main.cleanup()
131 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800132 except Exception:
133 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall05b2b432014-10-08 19:53:25 -0400134 main.cleanup()
135 main.exit()
136
kelvin8ec71442015-01-15 16:57:00 -0800137 def disconnect( self ):
138 """
Jon Hall05b2b432014-10-08 19:53:25 -0400139 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800140 """
Jon Halld61331b2015-02-17 16:35:47 -0800141 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -0400142 try:
Jon Hall61282e32015-03-19 11:34:11 -0700143 if self.handle:
144 self.handle.sendline( "" )
145 self.handle.expect( "\$" )
146 self.handle.sendline( "exit" )
147 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -0400148 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800149 main.log.error( self.name + ": EOF exception found" )
150 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700151 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700152 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700153 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800154 except Exception:
155 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -0400156 response = main.FALSE
157 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400158
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400159 def getEpochMs( self ):
160 """
161 Returns milliseconds since epoch
162
163 When checking multiple nodes in a for loop,
164 around a hundred milliseconds of difference (ascending) is
165 generally acceptable due to calltime of the function.
166 Few seconds, however, is not and it means clocks
167 are off sync.
168 """
169 try:
170 self.handle.sendline( 'date +%s.%N' )
171 self.handle.expect( 'date \+\%s\.\%N' )
172 self.handle.expect( '\$' )
173 epochMs = self.handle.before
174 return epochMs
175 except Exception:
176 main.log.exception( 'Uncaught exception getting epoch time' )
177 main.cleanup()
178 main.exit()
179
pingping-lin57a56ce2015-05-20 16:43:48 -0700180 def onosPackage( self, opTimeout=30 ):
kelvin8ec71442015-01-15 16:57:00 -0800181 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400182 Produce a self-contained tar.gz file that can be deployed
kelvin8ec71442015-01-15 16:57:00 -0800183 and executed on any platform with Java 7 JRE.
184 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400185 try:
kelvin8ec71442015-01-15 16:57:00 -0800186 self.handle.sendline( "onos-package" )
187 self.handle.expect( "onos-package" )
pingping-lin57a56ce2015-05-20 16:43:48 -0700188 self.handle.expect( "tar.gz", opTimeout )
kelvin8ec71442015-01-15 16:57:00 -0800189 handle = str( self.handle.before )
190 main.log.info( "onos-package command returned: " +
191 handle )
192 # As long as the sendline does not time out,
193 # return true. However, be careful to interpret
194 # the results of the onos-package command return
andrewonlab0748d2a2014-10-09 13:24:17 -0400195 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400196
andrewonlab7735d852014-10-09 13:02:47 -0400197 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800198 main.log.error( self.name + ": EOF exception found" )
199 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800200 except Exception:
201 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400202 main.cleanup()
203 main.exit()
204
kelvin-onlabd3b64892015-01-20 13:26:24 -0800205 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800206 """
andrewonlab8790abb2014-11-06 13:51:54 -0500207 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800208 """
andrewonlab8790abb2014-11-06 13:51:54 -0500209 try:
kelvin8ec71442015-01-15 16:57:00 -0800210 self.handle.sendline( "onos-build" )
211 self.handle.expect( "onos-build" )
212 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800213 "BUILD SUCCESS",
214 "ERROR",
215 "BUILD FAILED" ],
216 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800217 handle = str( self.handle.before )
andrewonlab8790abb2014-11-06 13:51:54 -0500218
kelvin8ec71442015-01-15 16:57:00 -0800219 main.log.info( "onos-build command returned: " +
220 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500221
222 if i == 0:
223 return main.TRUE
224 else:
225 return handle
226
227 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800228 main.log.error( self.name + ": EOF exception found" )
229 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800230 except Exception:
231 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500232 main.cleanup()
233 main.exit()
234
shahshreya9f531fe2015-06-10 12:03:51 -0700235 def cleanInstall( self, skipTest=False, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800236 """
237 Runs mvn clean install in the root of the ONOS directory.
238 This will clean all ONOS artifacts then compile each module
shahshreya9f531fe2015-06-10 12:03:51 -0700239 Optional:
240 skipTest - Does "-DskipTests -Dcheckstyle.skip -U -T 1C" which
241 skip the test. This will make the building faster.
242 Disregarding the credibility of the build
kelvin8ec71442015-01-15 16:57:00 -0800243 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400244 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800245 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400246 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800247 main.log.info( "Running 'mvn clean install' on " +
248 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800249 ". This may take some time." )
250 self.handle.sendline( "cd " + self.home )
251 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400252
kelvin8ec71442015-01-15 16:57:00 -0800253 self.handle.sendline( "" )
254 self.handle.expect( "\$" )
shahshreya9f531fe2015-06-10 12:03:51 -0700255
256 if not skipTest:
257 self.handle.sendline( "mvn clean install" )
258 self.handle.expect( "mvn clean install" )
259 else:
260 self.handle.sendline( "mvn clean install -DskipTests" +
261 " -Dcheckstyle.skip -U -T 1C" )
262 self.handle.expect( "mvn clean install -DskipTests" +
263 " -Dcheckstyle.skip -U -T 1C" )
kelvin8ec71442015-01-15 16:57:00 -0800264 while True:
265 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800266 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800267 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400268 'BUILD\sFAILURE',
269 'BUILD\sSUCCESS',
Jon Halle94919c2015-03-23 11:42:57 -0700270 'onos\$', #TODO: fix this to be more generic?
Jon Hallde9d9aa2014-10-08 20:36:02 -0400271 'ONOS\$',
pingping-lin57a56ce2015-05-20 16:43:48 -0700272 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400273 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800274 main.log.error( self.name + ":There is insufficient memory \
275 for the Java Runtime Environment to continue." )
276 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400277 main.cleanup()
278 main.exit()
279 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800280 main.log.error( self.name + ": Build failure!" )
281 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400282 main.cleanup()
283 main.exit()
284 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800285 main.log.info( self.name + ": Build success!" )
Jon Halle94919c2015-03-23 11:42:57 -0700286 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800287 main.log.info( self.name + ": Build complete" )
288 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400289 for line in self.handle.before.splitlines():
290 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800291 main.log.info( line )
292 self.handle.sendline( "" )
293 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400294 return main.TRUE
Jon Halle94919c2015-03-23 11:42:57 -0700295 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800296 main.log.error(
297 self.name +
298 ": mvn clean install TIMEOUT!" )
299 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400300 main.cleanup()
301 main.exit()
302 else:
Jon Hall274b6642015-02-17 11:57:17 -0800303 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800304 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800305 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400306 main.cleanup()
307 main.exit()
308 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800309 main.log.error( self.name + ": EOF exception found" )
310 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400311 main.cleanup()
312 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800313 except Exception:
314 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400315 main.cleanup()
316 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400317
Jon Hall61282e32015-03-19 11:34:11 -0700318 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800319 """
Jon Hallacabffd2014-10-09 12:36:53 -0400320 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800321
Jon Hall61282e32015-03-19 11:34:11 -0700322 If the fastForward boolean is set to true, only git pulls that can
323 be fast forwarded will be performed. IE if you have not local commits
324 in your branch.
325
Jon Hallacabffd2014-10-09 12:36:53 -0400326 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800327 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400328 for the purpose of pulling from other nodes if necessary.
329
Jon Hall47a93fb2015-01-06 16:46:06 -0800330 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400331 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800332 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400333 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400334
kelvin8ec71442015-01-15 16:57:00 -0800335 """
Jon Hallacabffd2014-10-09 12:36:53 -0400336 try:
kelvin8ec71442015-01-15 16:57:00 -0800337 # main.log.info( self.name + ": Stopping ONOS" )
338 # self.stop()
339 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800340 self.handle.expect( self.home + "\$" )
Jon Hall61282e32015-03-19 11:34:11 -0700341 cmd = "git pull"
342 if comp1 != "":
343 cmd += ' ' + comp1
344 if fastForward:
345 cmd += ' ' + " --ff-only"
346 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800347 i = self.handle.expect(
348 [
349 'fatal',
350 'Username\sfor\s(.*):\s',
351 '\sfile(s*) changed,\s',
352 'Already up-to-date',
353 'Aborting',
354 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800355 'You asked me to pull without telling me which branch you',
356 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700357 'Please enter a commit message to explain why this merge',
358 'Found a swap file by the name',
359 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800360 pexpect.TIMEOUT ],
361 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800362 # debug
kelvin-onlabd3b64892015-01-20 13:26:24 -0800363 # main.log.report( self.name +": DEBUG: \n"+
364 # "git pull response: " +
365 # str( self.handle.before ) + str( self.handle.after ) )
kelvin8ec71442015-01-15 16:57:00 -0800366 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700367 main.log.error( self.name + ": Git pull had some issue" )
368 output = self.handle.after
369 self.handle.expect( '\$' )
370 output += self.handle.before
371 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400372 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800373 elif i == 1:
374 main.log.error(
375 self.name +
376 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400377 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800378 elif i == 2:
379 main.log.info(
380 self.name +
381 ": Git Pull - pulling repository now" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800382 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800383 # So that only when git pull is done, we do mvn clean compile
384 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800385 elif i == 3:
386 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800387 return i
kelvin8ec71442015-01-15 16:57:00 -0800388 elif i == 4:
389 main.log.info(
390 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800391 ": Git Pull - Aborting..." +
392 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400393 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800394 elif i == 5:
395 main.log.info(
396 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800397 ": Git Pull - You are not currently " +
398 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400399 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800400 elif i == 6:
401 main.log.info(
402 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800403 ": Git Pull - You have not configured an upstream " +
404 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400405 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800406 elif i == 7:
407 main.log.info(
408 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800409 ": Git Pull - Pull is not possible because " +
410 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400411 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800412 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700413 # NOTE: abandoning test since we can't reliably handle this
414 # there could be different default text editors and we
415 # also don't know if we actually want to make the commit
416 main.log.error( "Git pull resulted in a merge commit message" +
417 ". Exiting test!" )
418 main.cleanup()
419 main.exit()
420 elif i == 9: # Merge commit message but swap file exists
421 main.log.error( "Git pull resulted in a merge commit message" +
422 " but a swap file exists." )
423 try:
424 self.handle.send( 'A' ) # Abort
425 self.handle.expect( "\$" )
426 return main.ERROR
427 except Exception:
428 main.log.exception( "Couldn't exit editor prompt!")
429 main.cleanup()
430 main.exit()
431 elif i == 10: # In the middle of a merge commit
432 main.log.error( "Git branch is in the middle of a merge. " )
433 main.log.warn( self.handle.before + self.handle.after )
434 return main.ERROR
435 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800436 main.log.error( self.name + ": Git Pull - TIMEOUT" )
437 main.log.error(
438 self.name + " Response was: " + str(
439 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400440 return main.ERROR
441 else:
kelvin8ec71442015-01-15 16:57:00 -0800442 main.log.error(
443 self.name +
444 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400445 return main.ERROR
446 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800447 main.log.error( self.name + ": EOF exception found" )
448 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400449 main.cleanup()
450 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800451 except Exception:
452 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400453 main.cleanup()
454 main.exit()
455
kelvin-onlabd3b64892015-01-20 13:26:24 -0800456 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800457 """
Jon Hallacabffd2014-10-09 12:36:53 -0400458 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800459
Jon Hallacabffd2014-10-09 12:36:53 -0400460 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800461 If used as gitCheckout( "branch" ) it will do git checkout
462 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400463
464 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800465 branch of the ONOS repository. If it has any problems, it will return
466 main.ERROR.
467 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400468 successful then the function will return main.TRUE.
469
kelvin8ec71442015-01-15 16:57:00 -0800470 """
Jon Hallacabffd2014-10-09 12:36:53 -0400471 try:
kelvin8ec71442015-01-15 16:57:00 -0800472 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800473 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800474 main.log.info( self.name +
475 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800476 cmd = "git checkout " + branch
477 self.handle.sendline( cmd )
478 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800479 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800480 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700481 'Username for (.*): ',
482 'Already on \'',
Jon Hall7a8354f2015-06-10 15:37:00 -0700483 'Switched to (a new )?branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800484 pexpect.TIMEOUT,
485 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800486 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800487 'error: you need to resolve your current index first',
488 "You are in 'detached HEAD' state.",
489 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800490 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800491 if i == 0:
492 main.log.error(
493 self.name +
494 ": Git checkout had some issue..." )
495 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400496 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800497 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800498 main.log.error(
499 self.name +
500 ": Git checkout asking for username." +
501 " Please configure your local git repository to be able " +
502 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800503 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400504 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800505 elif i == 2:
506 main.log.info(
507 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800508 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800509 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800510 # main.log.info( "DEBUG: after checkout cmd = "+
511 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400512 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800513 elif i == 3:
514 main.log.info(
515 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800516 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800517 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800518 # main.log.info( "DEBUG: after checkout cmd = "+
519 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400520 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800521 elif i == 4:
522 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
523 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800524 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400525 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800526 elif i == 5:
527 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800528 main.log.error(
529 self.name +
530 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800531 "Your local changes to the following files would" +
532 " be overwritten by checkout:" +
533 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800534 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500535 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800536 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800537 main.log.error(
538 self.name +
539 ": Git checkout error: \n" +
540 "You need to resolve your current index first:" +
541 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800542 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500543 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800544 elif i == 7:
545 main.log.info(
546 self.name +
547 ": Git checkout " + str( branch ) +
548 " - You are in 'detached HEAD' state. HEAD is now at " +
549 str( branch ) )
550 self.handle.expect( self.home + "\$" )
551 return main.TRUE
552 elif i == 8: # Already in detached HEAD on the specified commit
553 main.log.info(
554 self.name +
555 ": Git Checkout %s : Already on commit" % branch )
556 self.handle.expect( self.home + "\$" )
557 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400558 else:
kelvin8ec71442015-01-15 16:57:00 -0800559 main.log.error(
560 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800561 ": Git Checkout - Unexpected response, " +
562 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800563 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400564 return main.ERROR
565
566 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800567 main.log.error( self.name + ": EOF exception found" )
568 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400569 main.cleanup()
570 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800571 except Exception:
572 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400573 main.cleanup()
574 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400575
pingping-lin6d23d9e2015-02-02 16:54:24 -0800576 def getBranchName( self ):
pingping-linf30cf272015-05-29 15:54:07 -0700577 main.log.info( "self.home = " )
578 main.log.info( self.home )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800579 self.handle.sendline( "cd " + self.home )
pingping-lin9bf3d8f2015-05-29 16:05:28 -0700580 self.handle.expect( self.home + "\$" )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800581 self.handle.sendline( "git name-rev --name-only HEAD" )
582 self.handle.expect( "git name-rev --name-only HEAD" )
583 self.handle.expect( "\$" )
584
585 lines = self.handle.before.splitlines()
586 if lines[1] == "master":
587 return "master"
588 elif lines[1] == "onos-1.0":
589 return "onos-1.0"
590 else:
591 main.log.info( lines[1] )
592 return "unexpected ONOS branch for SDN-IP test"
593
kelvin-onlabd3b64892015-01-20 13:26:24 -0800594 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800595 """
Jon Hall274b6642015-02-17 11:57:17 -0800596 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800597 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800598 """
Jon Hall45ec0922014-10-10 19:33:49 -0400599 try:
kelvin8ec71442015-01-15 16:57:00 -0800600 self.handle.sendline( "" )
601 self.handle.expect( "\$" )
602 self.handle.sendline(
603 "cd " +
604 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800605 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
606 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800607 # NOTE: for some reason there are backspaces inserted in this
608 # phrase when run from Jenkins on some tests
609 self.handle.expect( "never" )
610 self.handle.expect( "\$" )
611 response = ( self.name + ": \n" + str(
612 self.handle.before + self.handle.after ) )
613 self.handle.sendline( "cd " + self.home )
614 self.handle.expect( "\$" )
615 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400616 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500617 print line
618 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700619 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800620 for line in lines[ 2:-1 ]:
621 # Bracket replacement is for Wiki-compliant
622 # formatting. '<' or '>' are interpreted
623 # as xml specific tags that cause errors
624 line = line.replace( "<", "[" )
625 line = line.replace( ">", "]" )
pingping-lin763ee042015-05-20 17:45:30 -0700626 #main.log.wiki( "\t" + line )
627 main.log.wiki( line + "<br /> " )
628 main.log.summary( line )
629 main.log.wiki( "</blockquote>" )
630 main.log.summary("\n")
kelvin8ec71442015-01-15 16:57:00 -0800631 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400632 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800633 main.log.error( self.name + ": EOF exception found" )
634 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400635 main.cleanup()
636 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800637 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800638 main.log.error( self.name + ": TIMEOUT exception found" )
639 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800640 main.cleanup()
641 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800642 except Exception:
643 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400644 main.cleanup()
645 main.exit()
646
kelvin-onlabd3b64892015-01-20 13:26:24 -0800647 def createCellFile( self, benchIp, fileName, mnIpAddrs,
kelvin-onlaba4074292015-07-09 15:19:49 -0700648 appString, onosIpAddrs ):
kelvin8ec71442015-01-15 16:57:00 -0800649 """
andrewonlab94282092014-10-10 13:00:11 -0400650 Creates a cell file based on arguments
651 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800652 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400653 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800654 * File name of the cell file ( fileName )
655 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800656 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400657 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800658 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400659 - Must be passed in as last arguments
kelvin8ec71442015-01-15 16:57:00 -0800660
andrewonlab94282092014-10-10 13:00:11 -0400661 NOTE: Assumes cells are located at:
662 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800663 """
664 # Variable initialization
Jon Hallf57a5ef2015-07-07 17:56:16 -0700665 cellDirectory = os.environ["ONOS_ROOT"] + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800666 # We want to create the cell file in the dependencies directory
667 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800668 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800669 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800670 cellFile = open( tempDirectory + fileName, 'w+' )
kelvin8ec71442015-01-15 16:57:00 -0800671
cameron@onlab.us75900962015-03-30 13:22:49 -0700672 # App string is hardcoded environment variables
kelvin8ec71442015-01-15 16:57:00 -0800673 # That you may wish to use by default on startup.
cameron@onlab.us75900962015-03-30 13:22:49 -0700674 # Note that you may not want certain apps listed
kelvin8ec71442015-01-15 16:57:00 -0800675 # on here.
cameron@onlab.us75900962015-03-30 13:22:49 -0700676 appString = "export ONOS_APPS=" + appString
kelvin-onlabd3b64892015-01-20 13:26:24 -0800677 mnString = "export OCN="
kelvin-onlabf70fd542015-05-07 18:41:40 -0700678 if mnIpAddrs == "":
679 mnString = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800680 onosString = "export OC"
681 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800682
kelvin-onlabd3b64892015-01-20 13:26:24 -0800683 # Create ONOSNIC ip address prefix
kelvin-onlaba4074292015-07-09 15:19:49 -0700684 tempOnosIp = str( onosIpAddrs[ 0 ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800685 tempList = []
686 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800687 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800688 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800689 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800690 nicAddr = ".".join( tempList ) + ".*"
691 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400692
693 try:
kelvin8ec71442015-01-15 16:57:00 -0800694 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800695 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400696
kelvin-onlabd3b64892015-01-20 13:26:24 -0800697 for arg in onosIpAddrs:
698 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800699 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400700 # export OC1="10.128.20.11"
701 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800702 cellFile.write( onosString + str( tempCount ) +
703 "=" + "\"" + arg + "\"" + "\n" )
704 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800705
kelvin-onlabd3b64892015-01-20 13:26:24 -0800706 cellFile.write( mnString + "\"" + mnIpAddrs + "\"" + "\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700707 cellFile.write( appString + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800708 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400709
kelvin8ec71442015-01-15 16:57:00 -0800710 # We use os.system to send the command to TestON cluster
711 # to account for the case in which TestON is not located
712 # on the same cluster as the ONOS bench
713 # Note that even if TestON is located on the same cluster
714 # as ONOS bench, you must setup passwordless ssh
715 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800716 os.system( "scp " + tempDirectory + fileName +
717 " admin@" + benchIp + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400718
andrewonlab2a6c9342014-10-16 13:40:15 -0400719 return main.TRUE
720
andrewonlab94282092014-10-10 13:00:11 -0400721 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800722 main.log.error( self.name + ": EOF exception found" )
723 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400724 main.cleanup()
725 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800726 except Exception:
727 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400728 main.cleanup()
729 main.exit()
730
kelvin-onlabd3b64892015-01-20 13:26:24 -0800731 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800732 """
andrewonlab95ca1462014-10-09 14:04:24 -0400733 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800734 """
andrewonlab95ca1462014-10-09 14:04:24 -0400735 try:
736 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800737 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400738 main.cleanup()
739 main.exit()
740 else:
kelvin8ec71442015-01-15 16:57:00 -0800741 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800742 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800743 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400744 # and that this driver will have to change accordingly
Cameron Franke9c94fb02015-01-21 10:20:20 -0800745 self.handle.expect(str(cellname))
kelvin-onlabd3b64892015-01-20 13:26:24 -0800746 handleBefore = self.handle.before
747 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800748 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800749 self.handle.sendline("")
750 self.handle.expect("\$")
kelvin-onlabd3b64892015-01-20 13:26:24 -0800751 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400752
kelvin-onlabd3b64892015-01-20 13:26:24 -0800753 main.log.info( "Cell call returned: " + handleBefore +
754 handleAfter + handleMore )
andrewonlab95ca1462014-10-09 14:04:24 -0400755
756 return main.TRUE
757
758 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800759 main.log.error( self.name + ": EOF exception found" )
760 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400761 main.cleanup()
762 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800763 except Exception:
764 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400765 main.cleanup()
766 main.exit()
767
kelvin-onlabd3b64892015-01-20 13:26:24 -0800768 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800769 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400770 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800771 """
772 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400773
andrewonlabc03bf6c2014-10-09 14:56:18 -0400774 try:
kelvin8ec71442015-01-15 16:57:00 -0800775 # Clean handle by sending empty and expecting $
776 self.handle.sendline( "" )
777 self.handle.expect( "\$" )
778 self.handle.sendline( "onos-verify-cell" )
779 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800780 handleBefore = self.handle.before
781 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800782 # Get the rest of the handle
783 self.handle.sendline( "" )
784 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800785 handleMore = self.handle.before
andrewonlabc03bf6c2014-10-09 14:56:18 -0400786
kelvin-onlabd3b64892015-01-20 13:26:24 -0800787 main.log.info( "Verify cell returned: " + handleBefore +
788 handleAfter + handleMore )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400789
790 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800791 except pexpect.ExceptionPexpect as e:
792 main.log.error( self.name + ": Pexpect exception found of type " +
793 str( type( e ) ) )
794 main.log.error ( e.get_trace() )
kelvin8ec71442015-01-15 16:57:00 -0800795 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400796 main.cleanup()
797 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800798 except Exception:
799 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400800 main.cleanup()
801 main.exit()
802
jenkins1e99e7b2015-04-02 18:15:39 -0700803 def onosCfgSet( self, ONOSIp, configName, configParam ):
804 """
805 Uses 'onos <node-ip> cfg set' to change a parameter value of an
806 application.
807
808 ex)
809 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
jenkins1e99e7b2015-04-02 18:15:39 -0700810 ONOSIp = '10.0.0.1'
811 configName = 'org.onosproject.myapp'
812 configParam = 'appSetting 1'
jenkins1e99e7b2015-04-02 18:15:39 -0700813 """
cameron@onlab.us78b89652015-07-08 15:21:03 -0700814 for i in range(5):
815 try:
816 cfgStr = ( "onos "+str(ONOSIp)+" cfg set "+
817 str(configName) + " " +
818 str(configParam)
819 )
jenkins1e99e7b2015-04-02 18:15:39 -0700820
cameron@onlab.us78b89652015-07-08 15:21:03 -0700821 self.handle.sendline( "" )
822 self.handle.expect( ":~" )
823 self.handle.sendline( cfgStr )
824 self.handle.expect("cfg set")
825 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700826
cameron@onlab.us78b89652015-07-08 15:21:03 -0700827 paramValue = configParam.split(" ")[1]
828 paramName = configParam.split(" ")[0]
829
830 checkStr = ( "onos " + str(ONOSIp) + """ cfg get " """ + str(configName) + " " + paramName + """ " """)
jenkins1e99e7b2015-04-02 18:15:39 -0700831
cameron@onlab.us78b89652015-07-08 15:21:03 -0700832 self.handle.sendline( checkStr )
833 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700834
cameron@onlab.us78b89652015-07-08 15:21:03 -0700835 if "value=" + paramValue + "," in self.handle.before:
836 main.log.info("cfg " + configName + " successfully set to " + configParam)
837 return main.TRUE
838
839 except pexpect.ExceptionPexpect as e:
840 main.log.error( self.name + ": Pexpect exception found of type " +
841 str( type( e ) ) )
842 main.log.error ( e.get_trace() )
843 main.log.error( self.name + ": " + self.handle.before )
844 main.cleanup()
845 main.exit()
846 except Exception:
847 main.log.exception( self.name + ": Uncaught exception!" )
848 main.cleanup()
849 main.exit()
850
851 time.sleep(5)
852
853 main.log.error("CFG SET FAILURE: " + configName + " " + configParam )
854 main.ONOSbench.handle.sendline("onos $OC1 cfg get")
855 main.ONOSbench.handle.expect("\$")
856 print main.ONOSbench.handle.before
857 main.ONOSbench.logReport( ONOSIp, ["ERROR","WARN","EXCEPT"], "d")
858 return main.FALSE
859
860
kelvin-onlabd3b64892015-01-20 13:26:24 -0800861 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800862 """
andrewonlab05e362f2014-10-10 00:40:57 -0400863 Uses 'onos' command to send various ONOS CLI arguments.
864 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800865 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400866 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800867
868 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400869 CLI commands for ONOS. Try to use this function first
870 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800871 function.
872 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400873 by starting onos, and typing in 'onos' to enter the
874 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800875 available commands.
876 """
andrewonlab05e362f2014-10-10 00:40:57 -0400877 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800878 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800879 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400880 return main.FALSE
881 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800882 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400883 return main.FALSE
884
kelvin8ec71442015-01-15 16:57:00 -0800885 cmdstr = str( cmdstr )
886 self.handle.sendline( "" )
887 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400888
kelvin-onlabd3b64892015-01-20 13:26:24 -0800889 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -0800890 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -0400891
kelvin-onlabd3b64892015-01-20 13:26:24 -0800892 handleBefore = self.handle.before
Shreya Shaha73aaad2014-10-27 18:03:09 -0400893 print "handle_before = ", self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800894 # handleAfter = str( self.handle.after )
Jon Hall47a93fb2015-01-06 16:46:06 -0800895
kelvin8ec71442015-01-15 16:57:00 -0800896 # self.handle.sendline( "" )
897 # self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800898 # handleMore = str( self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400899
kelvin8ec71442015-01-15 16:57:00 -0800900 main.log.info( "Command sent successfully" )
andrewonlab05e362f2014-10-10 00:40:57 -0400901
kelvin8ec71442015-01-15 16:57:00 -0800902 # Obtain return handle that consists of result from
903 # the onos command. The string may need to be
904 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800905 # returnString = handleBefore + handleAfter
906 returnString = handleBefore
907 print "return_string = ", returnString
908 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400909
910 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800911 main.log.error( self.name + ": EOF exception found" )
912 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400913 main.cleanup()
914 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800915 except Exception:
916 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400917 main.cleanup()
918 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400919
kelvin-onlabd3b64892015-01-20 13:26:24 -0800920 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -0800921 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400922 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -0800923 If -f option is provided, it also forces an uninstall.
924 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -0400925 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -0800926 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -0400927 files to certain onos nodes
928
929 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -0800930 """
Jon Hall7993bfc2014-10-09 16:30:14 -0400931 try:
andrewonlab114768a2014-11-14 12:44:44 -0500932 if options:
kelvin8ec71442015-01-15 16:57:00 -0800933 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -0500934 else:
kelvin8ec71442015-01-15 16:57:00 -0800935 self.handle.sendline( "onos-install " + node )
936 self.handle.expect( "onos-install " )
937 # NOTE: this timeout may need to change depending on the network
938 # and size of ONOS
939 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800940 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -0800941 "ONOS\sis\salready\sinstalled",
942 pexpect.TIMEOUT ], timeout=60 )
Jon Hall7993bfc2014-10-09 16:30:14 -0400943
Jon Hall7993bfc2014-10-09 16:30:14 -0400944 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800945 main.log.warn( "Network is unreachable" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400946 return main.FALSE
947 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800948 main.log.info(
949 "ONOS was installed on " +
950 node +
951 " and started" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400952 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500953 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800954 main.log.info( "ONOS is already installed on " + node )
andrewonlabd9a73a72014-11-14 17:28:21 -0500955 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800956 elif i == 3:
957 main.log.info(
958 "Installation of ONOS on " +
959 node +
960 " timed out" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400961 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400962
963 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800964 main.log.error( self.name + ": EOF exception found" )
965 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400966 main.cleanup()
967 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800968 except Exception:
969 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400970 main.cleanup()
971 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400972
kelvin-onlabd3b64892015-01-20 13:26:24 -0800973 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -0800974 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400975 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400976 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -0800977 """
andrewonlab8d0d7d72014-10-09 16:33:15 -0400978 try:
kelvin8ec71442015-01-15 16:57:00 -0800979 self.handle.sendline( "" )
980 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800981 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -0800982 " start" )
983 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -0400984 "Job\sis\salready\srunning",
985 "start/running",
986 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -0800987 pexpect.TIMEOUT ], timeout=120 )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400988
989 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800990 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400991 return main.TRUE
992 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800993 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400994 return main.TRUE
995 else:
kelvin8ec71442015-01-15 16:57:00 -0800996 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -0400997 main.cleanup()
998 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400999 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001000 main.log.error( self.name + ": EOF exception found" )
1001 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001002 main.cleanup()
1003 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001004 except Exception:
1005 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001006 main.cleanup()
1007 main.exit()
1008
kelvin-onlabd3b64892015-01-20 13:26:24 -08001009 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001010 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001011 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001012 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001013 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001014 try:
kelvin8ec71442015-01-15 16:57:00 -08001015 self.handle.sendline( "" )
1016 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001017 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001018 " stop" )
1019 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -04001020 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -07001021 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -04001022 "Unknown\sinstance",
kelvin8ec71442015-01-15 16:57:00 -08001023 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2b30bd32014-10-09 16:48:55 -04001024
1025 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001026 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001027 return main.TRUE
1028 elif i == 1:
Jon Hall65844a32015-03-09 19:09:37 -07001029 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001030 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -04001031 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -07001032 elif i == 2:
1033 main.log.warn( "ONOS wasn't running" )
1034 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -04001035 else:
kelvin8ec71442015-01-15 16:57:00 -08001036 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001037 return main.FALSE
1038
1039 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001040 main.log.error( self.name + ": EOF exception found" )
1041 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -04001042 main.cleanup()
1043 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001044 except Exception:
1045 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001046 main.cleanup()
1047 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001048
kelvin-onlabd3b64892015-01-20 13:26:24 -08001049 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -08001050 """
andrewonlabc8d47972014-10-09 16:52:36 -04001051 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -08001052 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -04001053 if needed
kelvin8ec71442015-01-15 16:57:00 -08001054 """
andrewonlabc8d47972014-10-09 16:52:36 -04001055 try:
kelvin8ec71442015-01-15 16:57:00 -08001056 self.handle.sendline( "" )
pingping-lin763ee042015-05-20 17:45:30 -07001057 self.handle.expect( "\$", timeout=60 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001058 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001059 self.handle.expect( "\$" )
andrewonlabc8d47972014-10-09 16:52:36 -04001060
kelvin-onlabd3b64892015-01-20 13:26:24 -08001061 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
andrewonlab9dfd2082014-11-13 17:44:03 -05001062
kelvin8ec71442015-01-15 16:57:00 -08001063 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -04001064 return main.TRUE
1065
pingping-lin763ee042015-05-20 17:45:30 -07001066 except pexpect.TIMEOUT:
1067 main.log.exception( self.name + ": Timeout in onosUninstall" )
1068 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -04001069 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001070 main.log.error( self.name + ": EOF exception found" )
1071 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -04001072 main.cleanup()
1073 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001074 except Exception:
1075 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -04001076 main.cleanup()
1077 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -04001078
kelvin-onlabd3b64892015-01-20 13:26:24 -08001079 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001080 """
andrewonlabaedc8332014-12-04 12:43:03 -05001081 Issues the command 'onos-die <node-ip>'
1082 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -08001083 """
andrewonlabaedc8332014-12-04 12:43:03 -05001084 try:
kelvin8ec71442015-01-15 16:57:00 -08001085 self.handle.sendline( "" )
1086 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001087 cmdStr = "onos-kill " + str( nodeIp )
1088 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001089 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001090 "Killing\sONOS",
1091 "ONOS\sprocess\sis\snot\srunning",
kelvin8ec71442015-01-15 16:57:00 -08001092 pexpect.TIMEOUT ], timeout=20 )
andrewonlabaedc8332014-12-04 12:43:03 -05001093 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001094 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001095 " was killed and stopped" )
andrewonlabaedc8332014-12-04 12:43:03 -05001096 return main.TRUE
1097 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001098 main.log.info( "ONOS process was not running" )
andrewonlabaedc8332014-12-04 12:43:03 -05001099 return main.FALSE
1100 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001101 main.log.error( self.name + ": EOF exception found" )
1102 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -05001103 main.cleanup()
1104 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001105 except Exception:
1106 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -05001107 main.cleanup()
1108 main.exit()
1109
kelvin-onlabd3b64892015-01-20 13:26:24 -08001110 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001111 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001112 Calls the command: 'onos-kill [<node-ip>]'
1113 "Remotely, and unceremoniously kills the ONOS instance running on
1114 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001115 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001116 try:
kelvin8ec71442015-01-15 16:57:00 -08001117 self.handle.sendline( "" )
1118 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001119 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001120 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -04001121 "\$",
1122 "No\sroute\sto\shost",
1123 "password:",
kelvin8ec71442015-01-15 16:57:00 -08001124 pexpect.TIMEOUT ], timeout=20 )
1125
andrewonlabe8e56fd2014-10-09 17:12:44 -04001126 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001127 main.log.info(
1128 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001129 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001130 return main.TRUE
1131 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001132 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001133 return main.FALSE
1134 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001135 main.log.info(
1136 "Passwordless login for host: " +
1137 str( nodeIp ) +
1138 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001139 return main.FALSE
1140 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001141 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001142 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001143
andrewonlabe8e56fd2014-10-09 17:12:44 -04001144 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001145 main.log.error( self.name + ": EOF exception found" )
1146 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001147 main.cleanup()
1148 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001149 except Exception:
1150 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001151 main.cleanup()
1152 main.exit()
1153
kelvin-onlabd3b64892015-01-20 13:26:24 -08001154 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001155 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001156 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001157 a cleaner environment.
1158
andrewonlab19fbdca2014-11-14 12:55:59 -05001159 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001160 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001161 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001162 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001163 try:
kelvin8ec71442015-01-15 16:57:00 -08001164 self.handle.sendline( "" )
1165 self.handle.expect( "\$" )
1166 self.handle.sendline( "onos-remove-raft-logs" )
1167 # Sometimes this command hangs
1168 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1169 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001170 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001171 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1172 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001173 if i == 1:
1174 return main.FALSE
shahshreya957feaa2015-03-23 16:08:29 -07001175 #self.handle.sendline( "" )
1176 #self.handle.expect( "\$" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001177 return main.TRUE
1178
1179 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001180 main.log.error( self.name + ": EOF exception found" )
1181 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001182 main.cleanup()
1183 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001184 except Exception:
1185 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001186 main.cleanup()
1187 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001188
kelvin-onlabd3b64892015-01-20 13:26:24 -08001189 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001190 """
1191 Calls the command 'onos-start-network [ <mininet-topo> ]
1192 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001193 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001194 cell."
andrewonlab94282092014-10-10 13:00:11 -04001195 * Specify mininet topology file name for mntopo
1196 * Topo files should be placed at:
1197 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001198
andrewonlab94282092014-10-10 13:00:11 -04001199 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001200 """
andrewonlab94282092014-10-10 13:00:11 -04001201 try:
1202 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001203 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001204 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001205
kelvin8ec71442015-01-15 16:57:00 -08001206 mntopo = str( mntopo )
1207 self.handle.sendline( "" )
1208 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001209
kelvin8ec71442015-01-15 16:57:00 -08001210 self.handle.sendline( "onos-start-network " + mntopo )
1211 self.handle.expect( "mininet>" )
1212 main.log.info( "Network started, entered mininet prompt" )
1213
1214 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001215
1216 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001217 main.log.error( self.name + ": EOF exception found" )
1218 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001219 main.cleanup()
1220 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001221 except Exception:
1222 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001223 main.cleanup()
1224 main.exit()
1225
Cameron Franke9c94fb02015-01-21 10:20:20 -08001226 def isup(self, node = "", timeout = 120):
kelvin8ec71442015-01-15 16:57:00 -08001227 """
1228 Run's onos-wait-for-start which only returns once ONOS is at run
Cameron Franke9c94fb02015-01-21 10:20:20 -08001229 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001230
Jon Hall7993bfc2014-10-09 16:30:14 -04001231 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001232 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001233 try:
Cameron Franke9c94fb02015-01-21 10:20:20 -08001234 self.handle.sendline("onos-wait-for-start " + node )
1235 self.handle.expect("onos-wait-for-start")
kelvin8ec71442015-01-15 16:57:00 -08001236 # NOTE: this timeout is arbitrary"
Cameron Franke9c94fb02015-01-21 10:20:20 -08001237 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001238 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001239 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001240 return main.TRUE
1241 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001242 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001243 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001244 main.log.error( "ONOS has not started yet" )
1245 self.handle.send( "\x03" ) # Control-C
1246 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001247 return main.FALSE
1248 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001249 main.log.error( self.name + ": EOF exception found" )
1250 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001251 main.cleanup()
1252 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001253 except Exception:
1254 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001255 main.cleanup()
1256 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001257
kelvin-onlabd3b64892015-01-20 13:26:24 -08001258 def pushTestIntentsShell(
1259 self,
1260 dpidSrc,
1261 dpidDst,
1262 numIntents,
1263 dirFile,
1264 onosIp,
1265 numMult="",
1266 appId="",
1267 report=True,
1268 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001269 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001270 Description:
kelvin8ec71442015-01-15 16:57:00 -08001271 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001272 better parallelize the results than the CLI
1273 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001274 * dpidSrc: specify source dpid
1275 * dpidDst: specify destination dpid
1276 * numIntents: specify number of intents to push
1277 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001278 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001279 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001280 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001281 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001282 """
1283 try:
1284 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001285 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001286 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001287 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001288 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001289 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001290
kelvin-onlabd3b64892015-01-20 13:26:24 -08001291 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1292 if not numMult:
1293 addIntents = addDpid + " " + str( numIntents )
1294 elif numMult:
1295 addIntents = addDpid + " " + str( numIntents ) + " " +\
1296 str( numMult )
1297 if appId:
1298 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001299 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001300 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001301
andrewonlabaedc8332014-12-04 12:43:03 -05001302 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001303 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001304 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001305 sendCmd = addApp + " &"
1306 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001307
kelvin-onlabd3b64892015-01-20 13:26:24 -08001308 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001309
1310 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001311 main.log.error( self.name + ": EOF exception found" )
1312 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001313 main.cleanup()
1314 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001315 except Exception:
1316 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001317 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001318 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001319
kelvin-onlabd3b64892015-01-20 13:26:24 -08001320 def getTopology( self, topologyOutput ):
kelvin8ec71442015-01-15 16:57:00 -08001321 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001322 Definition:
1323 Loads a json topology output
1324 Return:
1325 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -08001326 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001327 import json
Jon Hall77f53ce2014-10-13 18:02:06 -04001328 try:
Hari Krishnaef1bd4e2015-03-12 16:55:30 -07001329 # either onos:topology or 'topology' will work in CLI
1330 topology = json.loads(topologyOutput)
1331 print topology
Jon Hall77f53ce2014-10-13 18:02:06 -04001332 return topology
1333 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001334 main.log.error( self.name + ": EOF exception found" )
1335 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001336 main.cleanup()
1337 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001338 except Exception:
1339 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001340 main.cleanup()
1341 main.exit()
1342
kelvin-onlabd3b64892015-01-20 13:26:24 -08001343 def checkStatus(
1344 self,
1345 topologyResult,
1346 numoswitch,
1347 numolink,
1348 logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001349 """
Jon Hallefbd9792015-03-05 16:11:36 -08001350 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001351 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001352 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08001353
Jon Hall77f53ce2014-10-13 18:02:06 -04001354 Params: ip = ip used for the onos cli
1355 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001356 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001357 logLevel = level to log to.
1358 Currently accepts 'info', 'warn' and 'report'
Jon Hall77f53ce2014-10-13 18:02:06 -04001359
1360
kelvin-onlabd3b64892015-01-20 13:26:24 -08001361 logLevel can
Jon Hall77f53ce2014-10-13 18:02:06 -04001362
Jon Hallefbd9792015-03-05 16:11:36 -08001363 Returns: main.TRUE if the number of switches and links are correct,
1364 main.FALSE if the number of switches and links is incorrect,
Jon Hall77f53ce2014-10-13 18:02:06 -04001365 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001366 """
Jon Hall77f53ce2014-10-13 18:02:06 -04001367 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001368 topology = self.getTopology( topologyResult )
Jon Hall77f53ce2014-10-13 18:02:06 -04001369 if topology == {}:
1370 return main.ERROR
1371 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001372 # Is the number of switches is what we expected
shahshreya234a1682015-05-27 15:41:56 -07001373 devices = topology.get( 'devices', False )
1374 links = topology.get( 'links', False )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001375 if not devices or not links:
Jon Hall77f53ce2014-10-13 18:02:06 -04001376 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001377 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001378 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001379 linkCheck = ( int( links ) == int( numolink ) )
Jon Hallefbd9792015-03-05 16:11:36 -08001380 if switchCheck and linkCheck:
kelvin8ec71442015-01-15 16:57:00 -08001381 # We expected the correct numbers
Jon Hall77f53ce2014-10-13 18:02:06 -04001382 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001383 + "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001384 result = main.TRUE
1385 else:
1386 output = output + \
Jon Hall274b6642015-02-17 11:57:17 -08001387 "The number of links and switches does not match " + \
1388 "what was expected"
Jon Hall77f53ce2014-10-13 18:02:06 -04001389 result = main.FALSE
Jon Hallefbd9792015-03-05 16:11:36 -08001390 output = output + "\n ONOS sees %i devices" % int( devices )
1391 output = output + " (%i expected) " % int( numoswitch )
Jon Hall274b6642015-02-17 11:57:17 -08001392 output = output + "and %i links " % int( links )
1393 output = output + "(%i expected)" % int( numolink )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001394 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001395 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001396 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001397 main.log.warn( output )
Jon Hall77f53ce2014-10-13 18:02:06 -04001398 else:
kelvin8ec71442015-01-15 16:57:00 -08001399 main.log.info( output )
1400 return result
Jon Hall77f53ce2014-10-13 18:02:06 -04001401 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001402 main.log.error( self.name + ": EOF exception found" )
1403 main.log.error( self.name + ": " + self.handle.before )
Jon Hall77f53ce2014-10-13 18:02:06 -04001404 main.cleanup()
1405 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001406 except Exception:
1407 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall77f53ce2014-10-13 18:02:06 -04001408 main.cleanup()
1409 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001410
kelvin-onlabd3b64892015-01-20 13:26:24 -08001411 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001412 """
andrewonlab970399c2014-11-07 13:09:32 -05001413 Capture all packet activity and store in specified
1414 directory/file
1415
1416 Required:
1417 * interface: interface to capture
1418 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001419 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001420 try:
1421 self.handle.sendline( "" )
1422 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001423
Jon Hallfebb1c72015-03-05 13:30:09 -08001424 self.handle.sendline( "tshark -i " + str( interface ) +
1425 " -t e -w " + str( dirFile ) + " &" )
1426 self.handle.sendline( "\r" )
1427 self.handle.expect( "Capturing on" )
1428 self.handle.sendline( "\r" )
1429 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001430
Jon Hallfebb1c72015-03-05 13:30:09 -08001431 main.log.info( "Tshark started capturing files on " +
1432 str( interface ) + " and saving to directory: " +
1433 str( dirFile ) )
1434 except pexpect.EOF:
1435 main.log.error( self.name + ": EOF exception found" )
1436 main.log.error( self.name + ": " + self.handle.before )
1437 main.cleanup()
1438 main.exit()
1439 except Exception:
1440 main.log.exception( self.name + ": Uncaught exception!" )
1441 main.cleanup()
1442 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001443
kelvin-onlabd3b64892015-01-20 13:26:24 -08001444 def runOnosTopoCfg( self, instanceName, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001445 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001446 On ONOS bench, run this command:
Jon Halle94919c2015-03-23 11:42:57 -07001447 {ONOS_HOME}/tools/test/bin/onos-topo-cfg $OC1 filename
kelvin-onlabd3b64892015-01-20 13:26:24 -08001448 which starts the rest and copies
1449 the json file to the onos instance
kelvin8ec71442015-01-15 16:57:00 -08001450 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001451 try:
kelvin8ec71442015-01-15 16:57:00 -08001452 self.handle.sendline( "" )
1453 self.handle.expect( "\$" )
Jon Halle94919c2015-03-23 11:42:57 -07001454 self.handle.sendline( "cd " + self.home + "/tools/test/bin" )
kelvin8ec71442015-01-15 16:57:00 -08001455 self.handle.expect( "/bin$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001456 cmd = "./onos-topo-cfg " + instanceName + " " + jsonFile
shahshreyae6c7cf42014-11-26 16:39:01 -08001457 print "cmd = ", cmd
kelvin8ec71442015-01-15 16:57:00 -08001458 self.handle.sendline( cmd )
1459 self.handle.expect( "\$" )
1460 self.handle.sendline( "cd ~" )
1461 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -08001462 return main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -08001463 except pexpect.EOF:
1464 main.log.error( self.name + ": EOF exception found" )
1465 main.log.error( self.name + ": " + self.handle.before )
1466 main.cleanup()
1467 main.exit()
1468 except Exception:
1469 main.log.exception( self.name + ": Uncaught exception!" )
1470 main.cleanup()
1471 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001472
jenkins1e99e7b2015-04-02 18:15:39 -07001473 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001474 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001475 Required:
kelvin8ec71442015-01-15 16:57:00 -08001476 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001477 * directory to store results
1478 Optional:
1479 * interface - default: eth0
jenkins1e99e7b2015-04-02 18:15:39 -07001480 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001481 Description:
1482 Uses tshark command to grep specific group of packets
1483 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001484 The timestamp is hardcoded to be in epoch
1485 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001486 try:
1487 self.handle.sendline( "" )
1488 self.handle.expect( "\$" )
1489 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001490 if grepOptions:
1491 grepStr = "grep "+str(grepOptions)
1492 else:
1493 grepStr = "grep"
1494
Jon Hallfebb1c72015-03-05 13:30:09 -08001495 self.handle.sendline(
1496 "tshark -i " +
1497 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001498 " -t e | " +
1499 grepStr + " --line-buffered \"" +
Jon Hallfebb1c72015-03-05 13:30:09 -08001500 str(grep) +
1501 "\" >" +
1502 directory +
1503 " &" )
1504 self.handle.sendline( "\r" )
1505 self.handle.expect( "Capturing on" )
1506 self.handle.sendline( "\r" )
1507 self.handle.expect( "\$" )
1508 except pexpect.EOF:
1509 main.log.error( self.name + ": EOF exception found" )
1510 main.log.error( self.name + ": " + self.handle.before )
1511 main.cleanup()
1512 main.exit()
1513 except Exception:
1514 main.log.exception( self.name + ": Uncaught exception!" )
1515 main.cleanup()
1516 main.exit()
1517
kelvin-onlabd3b64892015-01-20 13:26:24 -08001518 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001519 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001520 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001521 """
1522 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001523 try:
1524 self.execute( cmd="sudo rm /tmp/wireshark*" )
1525 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001526 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1527 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001528 self.handle.sendline( "" )
1529 main.log.info( "Tshark stopped" )
1530 except pexpect.EOF:
1531 main.log.error( self.name + ": EOF exception found" )
1532 main.log.error( self.name + ": " + self.handle.before )
1533 main.cleanup()
1534 main.exit()
1535 except Exception:
1536 main.log.exception( self.name + ": Uncaught exception!" )
1537 main.cleanup()
1538 main.exit()
1539
kelvin8ec71442015-01-15 16:57:00 -08001540 def ptpd( self, args ):
1541 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001542 Initiate ptp with user-specified args.
1543 Required:
1544 * args: specify string of args after command
1545 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001546 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001547 try:
kelvin8ec71442015-01-15 16:57:00 -08001548 self.handle.sendline( "sudo ptpd " + str( args ) )
1549 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001550 "Multiple",
1551 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001552 "\$" ] )
1553 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001554
andrewonlab0c38a4a2014-10-28 18:35:35 -04001555 if i == 0:
1556 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001557 main.log.info( "ptpd returned an error: " +
1558 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001559 return handle
1560 elif i == 1:
1561 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001562 main.log.error( "ptpd returned an error: " +
1563 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001564 return handle
1565 else:
1566 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001567
andrewonlab0c38a4a2014-10-28 18:35:35 -04001568 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001569 main.log.error( self.name + ": EOF exception found" )
1570 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001571 main.cleanup()
1572 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001573 except Exception:
1574 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001575 main.cleanup()
1576 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001577
kelvin-onlabd3b64892015-01-20 13:26:24 -08001578 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001579 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001580 """
1581 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001582 Current implementation of ONOS deletes its karaf
1583 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001584 you may want to use this function to capture
1585 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001586 Localtime will be attached to the filename
1587
1588 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001589 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001590 copy.
kelvin8ec71442015-01-15 16:57:00 -08001591 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001592 For copying multiple files, leave copyFileName
1593 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001594 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001595 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001596 ex ) /tmp/
1597 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001598 * copyFileName: If you want to rename the log
1599 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001600 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001601 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001602 try:
kelvin8ec71442015-01-15 16:57:00 -08001603 localtime = time.strftime( '%x %X' )
1604 localtime = localtime.replace( "/", "" )
1605 localtime = localtime.replace( " ", "_" )
1606 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001607 if destDir[ -1: ] != "/":
1608 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001609
kelvin-onlabd3b64892015-01-20 13:26:24 -08001610 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001611 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1612 str( destDir ) + str( copyFileName ) +
1613 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001614 self.handle.expect( "cp" )
1615 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001616 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001617 self.handle.sendline( "cp " + str( logToCopy ) +
1618 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001619 self.handle.expect( "cp" )
1620 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001621
kelvin8ec71442015-01-15 16:57:00 -08001622 return self.handle.before
1623
1624 except pexpect.EOF:
1625 main.log.error( "Copying files failed" )
1626 main.log.error( self.name + ": EOF exception found" )
1627 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001628 except Exception:
1629 main.log.exception( "Copying files failed" )
1630
Jon Hall16b72c42015-05-20 10:23:36 -07001631 def checkLogs( self, onosIp, restart=False):
kelvin8ec71442015-01-15 16:57:00 -08001632 """
Jon Hall94fd0472014-12-08 11:52:42 -08001633 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001634 If restart is True, use the old version of onos-check-logs which
1635 does not print the full stacktrace, but shows the entire log file,
1636 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001637 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001638 """
Jon Hall94fd0472014-12-08 11:52:42 -08001639 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001640 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001641 if restart:
1642 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001643 self.handle.sendline( cmd )
1644 self.handle.expect( cmd )
1645 self.handle.expect( "\$" )
Jon Hall94fd0472014-12-08 11:52:42 -08001646 response = self.handle.before
1647 return response
1648 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001649 main.log.error( "Lost ssh connection" )
1650 main.log.error( self.name + ": EOF exception found" )
1651 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001652 except Exception:
1653 main.log.exception( self.name + ": Uncaught exception!" )
1654 main.cleanup()
1655 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001656
kelvin-onlabd3b64892015-01-20 13:26:24 -08001657 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001658 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001659 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001660 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001661 try:
kelvin8ec71442015-01-15 16:57:00 -08001662 self.handle.sendline( "" )
1663 self.handle.expect( "\$" )
1664 self.handle.sendline( "onos-service " + str( node ) +
1665 " status" )
1666 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001667 "start/running",
1668 "stop/waiting",
kelvin8ec71442015-01-15 16:57:00 -08001669 pexpect.TIMEOUT ], timeout=120 )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001670
1671 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001672 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001673 return main.TRUE
1674 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001675 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001676 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001677 main.cleanup()
1678 main.exit()
1679 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001680 main.log.error( self.name + ": EOF exception found" )
1681 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001682 main.cleanup()
1683 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001684 except Exception:
1685 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001686 main.cleanup()
1687 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001688
Jon Hall63604932015-02-26 17:09:50 -08001689 def setIpTables( self, ip, port='', action='add', packet_type='',
1690 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001691 """
Jon Hall21270ac2015-02-16 17:59:55 -08001692 Description:
1693 add or remove iptables rule to DROP (default) packets from
1694 specific IP and PORT
1695 Usage:
1696 * specify action ('add' or 'remove')
1697 when removing, pass in the same argument as you would add. It will
1698 delete that specific rule.
1699 * specify the ip to block
1700 * specify the destination port to block (defaults to all ports)
1701 * optional packet type to block (default tcp)
1702 * optional iptables rule (default DROP)
1703 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001704 * States boolean toggles adding all supported tcp states to the
1705 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001706 Returns:
1707 main.TRUE on success or
1708 main.FALSE if given invalid input or
1709 main.ERROR if there is an error in response from iptables
1710 WARNING:
1711 * This function uses root privilege iptables command which may result
1712 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001713 """
Jon Hall21270ac2015-02-16 17:59:55 -08001714 import time
1715
1716 # NOTE*********
1717 # The strict checking methods of this driver function is intentional
1718 # to discourage any misuse or error of iptables, which can cause
1719 # severe network errors
1720 # *************
1721
1722 # NOTE: Sleep needed to give some time for rule to be added and
1723 # registered to the instance. If you are calling this function
1724 # multiple times this sleep will prevent any errors.
1725 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001726 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001727 try:
1728 # input validation
1729 action_type = action.lower()
1730 rule = rule.upper()
1731 direction = direction.upper()
1732 if action_type != 'add' and action_type != 'remove':
1733 main.log.error( "Invalid action type. Use 'add' or "
1734 "'remove' table rule" )
1735 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1736 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1737 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1738 "'ACCEPT' or 'LOG' only." )
1739 if direction != 'INPUT' and direction != 'OUTPUT':
1740 # NOTE currently only supports rules INPUT and OUPTUT
1741 main.log.error( "Invalid rule. Valid directions are"
1742 " 'OUTPUT' or 'INPUT'" )
1743 return main.FALSE
1744 return main.FALSE
1745 return main.FALSE
1746 if action_type == 'add':
1747 # -A is the 'append' action of iptables
1748 actionFlag = '-A'
1749 elif action_type == 'remove':
1750 # -D is the 'delete' rule of iptables
1751 actionFlag = '-D'
1752 self.handle.sendline( "" )
1753 self.handle.expect( "\$" )
1754 cmd = "sudo iptables " + actionFlag + " " +\
1755 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001756 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001757 # " -p " + str( packet_type ) +\
1758 if packet_type:
1759 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001760 if port:
1761 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001762 if states:
1763 cmd += " -m state --state="
1764 #FIXME- Allow user to configure which states to block
1765 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001766 cmd += " -j " + str( rule )
1767
1768 self.handle.sendline( cmd )
1769 self.handle.expect( "\$" )
1770 main.log.warn( self.handle.before )
1771
1772 info_string = "On " + str( self.name )
1773 info_string += " " + str( action_type )
1774 info_string += " iptable rule [ "
1775 info_string += " IP: " + str( ip )
1776 info_string += " Port: " + str( port )
1777 info_string += " Rule: " + str( rule )
1778 info_string += " Direction: " + str( direction ) + " ]"
1779 main.log.info( info_string )
1780 return main.TRUE
1781 except pexpect.TIMEOUT:
1782 main.log.exception( self.name + ": Timeout exception in "
1783 "setIpTables function" )
1784 return main.ERROR
1785 except pexpect.EOF:
1786 main.log.error( self.name + ": EOF exception found" )
1787 main.log.error( self.name + ": " + self.handle.before )
1788 main.cleanup()
1789 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001790 except Exception:
1791 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001792 main.cleanup()
1793 main.exit()
1794
Jon Hall0468b042015-02-19 19:08:21 -08001795 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001796 """
Jon Hall0468b042015-02-19 19:08:21 -08001797 This method is used by STS to check the status of the controller
1798 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001799 """
Jon Hall0468b042015-02-19 19:08:21 -08001800 import re
1801 try:
1802 self.handle.sendline( "" )
1803 self.handle.expect( "\$" )
1804 self.handle.sendline( "cd " + self.home )
1805 self.handle.expect( "\$" )
1806 self.handle.sendline( "service onos status" )
1807 self.handle.expect( "\$" )
1808 response = self.handle.before
1809 if re.search( "onos start/running", response ):
1810 # onos start/running, process 10457
1811 return 'RUNNING'
1812 # FIXME: Implement this case
1813 # elif re.search( pattern, response ):
1814 # return 'STARTING'
1815 elif re.search( "onos stop/", response ):
1816 # onos stop/waiting
1817 # FIXME handle this differently?: onos stop/pre-stop
1818 return 'STOPPED'
1819 # FIXME: Implement this case
1820 # elif re.search( pattern, response ):
1821 # return 'FROZEN'
1822 else:
1823 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001824 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001825 main.log.warn( response )
1826 return 'ERROR', "Unknown response: %s" % response
1827 except pexpect.TIMEOUT:
1828 main.log.exception( self.name + ": Timeout exception in "
1829 "setIpTables function" )
1830 return 'ERROR', "Pexpect Timeout"
1831 except pexpect.EOF:
1832 main.log.error( self.name + ": EOF exception found" )
1833 main.log.error( self.name + ": " + self.handle.before )
1834 main.cleanup()
1835 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001836 except Exception:
1837 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001838 main.cleanup()
1839 main.exit()
1840
andrew@onlab.us3b087132015-03-11 15:00:08 -07001841 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1842 '''
1843 Create/formats the LinkGraph.cfg file based on arguments
1844 -only creates a linear topology and connects islands
1845 -evenly distributes devices
1846 -must be called by ONOSbench
1847
1848 ONOSIpList - list of all of the node IPs to be used
1849
1850 deviceCount - number of switches to be assigned
1851 '''
1852 main.log.step("Creating link graph configuration file." )
1853 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
1854 tempFile = "/tmp/linkGraph.cfg"
1855
1856 linkGraph = open(tempFile, 'w+')
1857 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1858 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1859 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
1860
1861 clusterCount = len(ONOSIpList)
1862
1863 if type(deviceCount) is int or type(deviceCount) is str:
1864 deviceCount = int(deviceCount)
1865 switchList = [0]*(clusterCount+1)
1866 baselineSwitchCount = deviceCount/clusterCount
1867
1868 for node in range(1, clusterCount + 1):
1869 switchList[node] = baselineSwitchCount
1870
1871 for node in range(1, (deviceCount%clusterCount)+1):
1872 switchList[node] += 1
1873
1874 if type(deviceCount) is list:
1875 main.log.info("Using provided device distribution")
1876 switchList = [0]
1877 for i in deviceCount:
1878 switchList.append(int(i))
1879
1880 tempList = ['0']
1881 tempList.extend(ONOSIpList)
1882 ONOSIpList = tempList
1883
1884 myPort = 6
1885 lastSwitch = 0
1886 for node in range(1, clusterCount+1):
1887 if switchList[node] == 0:
1888 continue
1889
1890 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
1891
1892 if node > 1:
1893 #connect to last device on previous node
1894 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
1895 linkGraph.write(line)
1896
1897 lastSwitch = 0
1898 for switch in range (0, switchList[node]-1):
1899 line = ""
1900 line = ("\t" + str(switch) + ":" + str(myPort))
1901 line += " -- "
1902 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
1903 linkGraph.write(line)
1904 lastSwitch = switch+1
1905 lastIp = ONOSIpList[node]
1906
1907 #lastSwitch += 1
1908 if node < (clusterCount):
1909 #connect to first device on the next node
1910 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
1911 linkGraph.write(line)
1912
1913 linkGraph.write("}\n")
1914 linkGraph.close()
1915
1916 #SCP
1917 os.system( "scp " + tempFile + " admin@" + benchIp + ":" + linkGraphPath)
1918 main.log.info("linkGraph.cfg creation complete")
1919
cameron@onlab.us75900962015-03-30 13:22:49 -07001920 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001921
1922 '''
andrew@onlab.us3b087132015-03-11 15:00:08 -07001923 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
cameron@onlab.us75900962015-03-30 13:22:49 -07001924 deviceCount = number of switches to distribute, or list of values to use as custom distribution
1925 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 -07001926 '''
1927
cameron@onlab.us75900962015-03-30 13:22:49 -07001928 main.log.step("Configuring Null Device Provider" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001929 clusterCount = len(ONOSIpList)
1930
cameron@onlab.us75900962015-03-30 13:22:49 -07001931 try:
1932
1933 if type(deviceCount) is int or type(deviceCount) is str:
1934 main.log.step("Creating device distribution")
1935 deviceCount = int(deviceCount)
1936 switchList = [0]*(clusterCount+1)
1937 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001938
cameron@onlab.us75900962015-03-30 13:22:49 -07001939 for node in range(1, clusterCount + 1):
1940 switchList[node] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001941
cameron@onlab.us75900962015-03-30 13:22:49 -07001942 for node in range(1, (deviceCount%clusterCount)+1):
1943 switchList[node] += 1
1944
1945 if type(deviceCount) is list:
1946 main.log.info("Using provided device distribution")
1947
1948 if len(deviceCount) == clusterCount:
1949 switchList = ['0']
1950 switchList.extend(deviceCount)
1951
1952 if len(deviceCount) == (clusterCount + 1):
1953 if deviceCount[0] == '0' or deviceCount[0] == 0:
1954 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001955
cameron@onlab.us75900962015-03-30 13:22:49 -07001956 assert len(switchList) == (clusterCount + 1)
1957
1958 except AssertionError:
1959 main.log.error( "Bad device/Ip list match")
1960 except TypeError:
1961 main.log.exception( self.name + ": Object not as expected" )
1962 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07001963 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07001964 main.log.exception( self.name + ": Uncaught exception!" )
1965 main.cleanup()
1966 main.exit()
1967
andrew@onlab.us3b087132015-03-11 15:00:08 -07001968
1969 ONOSIp = [0]
1970 ONOSIp.extend(ONOSIpList)
1971
1972 devicesString = "devConfigs = "
1973 for node in range(1, len(ONOSIp)):
1974 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
1975 if node < clusterCount:
1976 devicesString += (",")
cameron@onlab.us75900962015-03-30 13:22:49 -07001977
1978 try:
1979 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
1980 self.handle.expect(":~")
1981 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
1982 self.handle.expect(":~")
andrew@onlab.us3b087132015-03-11 15:00:08 -07001983
cameron@onlab.us75900962015-03-30 13:22:49 -07001984 for i in range(10):
1985 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
1986 self.handle.expect(":~")
1987 verification = self.handle.before
1988 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
1989 break
1990 else:
1991 time.sleep(1)
andrew@onlab.us3b087132015-03-11 15:00:08 -07001992
cameron@onlab.us2cc8bf12015-04-02 14:12:30 -07001993 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
cameron@onlab.us75900962015-03-30 13:22:49 -07001994
1995 except AssertionError:
1996 main.log.error("Incorrect Config settings: " + verification)
Jon Hall77ba41c2015-04-06 10:25:40 -07001997 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07001998 main.log.exception( self.name + ": Uncaught exception!" )
1999 main.cleanup()
2000 main.exit()
2001
cameron@onlab.usc80a8c82015-04-15 14:57:37 -07002002 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002003 '''
cameron@onlab.us75900962015-03-30 13:22:49 -07002004 fileName default is currently the same as the default on ONOS, specify alternate file if
2005 you want to use a different topology file than linkGraph.cfg
andrew@onlab.us3b087132015-03-11 15:00:08 -07002006 '''
2007
andrew@onlab.us3b087132015-03-11 15:00:08 -07002008
cameron@onlab.us75900962015-03-30 13:22:49 -07002009 try:
2010 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
2011 self.handle.expect(":~")
2012 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2013 self.handle.expect(":~")
2014
2015 for i in range(10):
2016 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
2017 self.handle.expect(":~")
2018 verification = self.handle.before
2019 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
2020 break
2021 else:
2022 time.sleep(1)
2023
2024 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
2025
2026 except pexpect.EOF:
2027 main.log.error( self.name + ": EOF exception found" )
2028 main.log.error( self.name + ": " + self.handle.before )
2029 main.cleanup()
2030 main.exit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002031 except AssertionError:
2032 main.log.info("Settings did not post to ONOS")
2033 main.log.error(varification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002034 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002035 main.log.exception( self.name + ": Uncaught exception!" )
2036 main.log.error(varification)
2037 main.cleanup()
2038 main.exit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002039
kelvin-onlaba4074292015-07-09 15:19:49 -07002040 def getOnosIps( self ):
2041 """
2042 Get all onos IPs stored in
2043 """
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002044
kelvin-onlaba4074292015-07-09 15:19:49 -07002045 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002046
kelvin-onlaba4074292015-07-09 15:19:49 -07002047 def logReport( self, nodeIp, searchTerms, outputMode="s" ):
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002048 '''
2049 - accepts either a list or a string for "searchTerms" these
2050 terms will be searched for in the log and have their
2051 instances counted
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002052
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002053 - nodeIp is the ip of the node whos log is to be scanned
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002054
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002055 - output modes:
2056 "s" - Simple. Quiet output mode that just prints
cameron@onlab.us2e166212015-05-19 14:28:25 -07002057 the occurences of each search term
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002058
cameron@onlab.us2e166212015-05-19 14:28:25 -07002059 "d" - Detailed. Prints number of occurences as well as the entire
2060 line for each of the last 5 occurences
2061
2062 - returns total of the number of instances of all search terms
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002063 '''
2064 main.log.info("========================== Log Report ===========================\n")
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002065
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002066 if type(searchTerms) is str:
2067 searchTerms = [searchTerms]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002068
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002069 logLines = [ [" "] for i in range(len(searchTerms)) ]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002070
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002071 for term in range(len(searchTerms)):
2072 logLines[term][0] = searchTerms[term]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002073
cameron@onlab.us2e166212015-05-19 14:28:25 -07002074 totalHits = 0
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002075 for term in range(len(searchTerms)):
2076 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep " + searchTerms[term]
2077 self.handle.sendline(cmd)
2078 self.handle.expect(":~")
2079 before = (self.handle.before).splitlines()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002080
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002081 count = [searchTerms[term],0]
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002082
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002083 for line in before:
2084 if searchTerms[term] in line and "grep" not in line:
2085 count[1] += 1
2086 if before.index(line) > ( len(before) - 7 ):
2087 logLines[term].append(line)
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002088
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002089 main.log.info( str(count[0]) + ": " + str(count[1]) )
2090 if term == len(searchTerms)-1:
2091 print("\n")
cameron@onlab.us2e166212015-05-19 14:28:25 -07002092 totalHits += int(count[1])
2093
cameron@onlab.us966d1be2015-05-15 14:54:09 -07002094 if outputMode != "s" and outputMode != "S":
2095 outputString = ""
2096 for i in logLines:
2097 outputString = i[0] + ": \n"
2098 for x in range(1,len(i)):
2099 outputString += ( i[x] + "\n" )
2100
2101 if outputString != (i[0] + ": \n"):
2102 main.log.info(outputString)
2103
2104 main.log.info("================================================================\n")
cameron@onlab.us2e166212015-05-19 14:28:25 -07002105 return totalHits
pingping-lin763ee042015-05-20 17:45:30 -07002106
Hari Krishnaade11a72015-07-01 17:06:46 -07002107 def getOnosIPfromCell(self):
2108 '''
2109 Returns the ONOS node names and their IP addresses as defined in the cell and applied to shell environment
2110 Example output return: [['OC1', '10.128.40.41'], ['OC2', '10.128.40.42'], ['OC3', '10.128.40.43']]
2111 '''
2112 import re
2113 try:
2114 # Clean handle by sending empty and expecting $
2115 self.handle.sendline( "" )
2116 self.handle.expect( "\$" )
2117 self.handle.sendline( "cell" )
2118 self.handle.expect( "\$" )
2119 handleBefore = self.handle.before
2120 handleAfter = self.handle.after
2121 # Get the rest of the handle
2122 self.handle.sendline( "" )
2123 self.handle.expect( "\$" )
2124 handleMore = self.handle.before
2125 ipList = []
2126 cellOutput = handleBefore + handleAfter + handleMore
2127 cellOutput = cellOutput.replace("\r\r","")
2128 cellOutput = cellOutput.splitlines()
2129 for i in range( len(cellOutput) ):
2130 if( re.match( "OC", cellOutput[i] ) ):
2131 if( re.match( "OCI", cellOutput[i] ) or re.match( "OCN", cellOutput[i] ) ):
2132 continue
2133 else:
2134 onosIP = cellOutput[i].split("=")
Hari Krishnaacabd5a2015-07-01 17:10:19 -07002135 # below step could be changed to return only IP if node name is not needed.
Hari Krishnaade11a72015-07-01 17:06:46 -07002136 ipList.append(onosIP)
2137 return ipList
2138 except pexpect.ExceptionPexpect as e:
2139 main.log.error( self.name + ": Pexpect exception found of type " +
2140 str( type( e ) ) )
2141 main.log.error ( e.get_trace() )
2142 main.log.error( self.name + ": " + self.handle.before )
2143 main.cleanup()
2144 main.exit()
2145 except Exception:
2146 main.log.exception( self.name + ": Uncaught exception!" )
2147 main.cleanup()
2148 main.exit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002149
2150 def copyMininetFile( self, fileName, localPath, userName, ip,
2151 mnPath='~/mininet/custom/', timeout = 60 ):
2152 """
2153 Description:
2154 Copy mininet topology file from dependency folder in the test folder
2155 and paste it to the mininet machine's mininet/custom folder
2156 Required:
2157 fileName - Name of the topology file to copy
2158 localPath - File path of the mininet topology file
2159 userName - User name of the mininet machine to send the file to
2160 ip - Ip address of the mininet machine
2161 Optional:
2162 mnPath - of the mininet directory to send the file to
2163 Return:
2164 Return main.TRUE if successfully copied the file otherwise
2165 return main.FALSE
2166 """
2167
2168 try:
2169 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2170 str( ip ) + ":" + mnPath + fileName
2171
2172 self.handle.sendline( "" )
2173 self.handle.expect( "\$" )
2174
2175 main.log.info( self.name + ": Execute: " + cmd )
2176
2177 self.handle.sendline( cmd )
2178
2179 i = self.handle.expect( [ 'No such file',
2180 "100%",
2181 pexpect.TIMEOUT ] )
2182
2183 if i == 0:
2184 main.log.error( self.name + ": File " + fileName +
2185 " does not exist!" )
2186 return main.FALSE
2187
2188 if i == 1:
2189 main.log.info( self.name + ": File " + fileName +
2190 " has been copied!" )
2191 self.handle.sendline( "" )
2192 self.handle.expect( "\$" )
2193 return main.TRUE
2194
2195 except pexpect.EOF:
2196 main.log.error( self.name + ": EOF exception found" )
2197 main.log.error( self.name + ": " + self.handle.before )
2198 main.cleanup()
2199 main.exit()
2200 except pexpect.TIMEOUT:
2201 main.log.error( self.name + ": TIMEOUT exception found" )
2202 main.log.error( self.name + ": " + self.handle.before )
2203 main.cleanup()
2204 main.exit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002205
2206 def jvmSet(self, memory=8):
2207
2208 import os
2209
2210 homeDir = os.path.expanduser('~')
2211 filename = "/onos/tools/package/bin/onos-service"
2212
2213 serviceConfig = open(homeDir + filename, 'w+')
2214 serviceConfig.write("#!/bin/bash\n ")
2215 serviceConfig.write("#------------------------------------- \n ")
2216 serviceConfig.write("# Starts ONOS Apache Karaf container\n ")
2217 serviceConfig.write("#------------------------------------- \n ")
2218 serviceConfig.write("#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n ")
2219 serviceConfig.write("""export JAVA_OPTS="${JAVA_OPTS:--Xms""" + str(memory) + "G -Xmx" + str(memory) + """G}" \n """)
2220 serviceConfig.write("[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n")
2221 serviceConfig.write("""${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """)
2222 serviceConfig.close()
2223
2224 def createDBFile(self, testData):
2225
2226 filename = main.TEST + "DB"
2227 DBString = ""
2228
2229 for item in testData:
2230 if type(item) is string:
2231 item = "'" + item + "'"
2232 if testData.index(item) < len(testData-1):
2233 item += ","
2234 DBString += str(item)
2235
2236 DBFile = open(filename, "a")
2237 DBFile.write(DBString)
2238 DBFile.close()
2239
2240 def verifySummary(self, ONOSIp,*deviceCount):
2241
2242 self.handle.sendline("onos " + ONOSIp + " summary")
2243 self.handle.expect(":~")
2244
2245 summaryStr = self.handle.before
2246 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2247
2248 #passed = "SCC(s)=1" in summaryStr
2249 #if deviceCount:
2250 # passed = "devices=" + str(deviceCount) + "," not in summaryStr
2251
2252
2253 if "SCC(s)=1," in summaryStr:
2254 passed = True
2255 print("Summary is verifed")
2256 else:
2257 print("Summary failed")
2258
2259 if deviceCount:
2260 print" ============================="
2261 checkStr = "devices=" + str(deviceCount[0]) + ","
2262 print "Checkstr: " + checkStr
2263 if checkStr not in summaryStr:
2264 passed = False
2265 print("Device count failed")
2266 else:
2267 print "device count verified"
2268
2269 return passed