blob: d46e99aa1a55ec89e02a42c737304baa8cd62c5b [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
andrewonlabe8e56fd2014-10-09 17:12:44 -04002
kelvin8ec71442015-01-15 16:57:00 -08003"""
4This driver interacts with ONOS bench, the OSGi platform
5that configures the ONOS nodes. ( aka ONOS-next )
andrewonlabe8e56fd2014-10-09 17:12:44 -04006
kelvin8ec71442015-01-15 16:57:00 -08007Please follow the coding style demonstrated by existing
andrewonlabe8e56fd2014-10-09 17:12:44 -04008functions and document properly.
9
10If you are a contributor to the driver, please
11list your email here for future contact:
12
13jhall@onlab.us
14andrew@onlab.us
15
16OCT 9 2014
Jeremy Songsterae01bba2016-07-11 15:39:17 -070017Modified 2016 by ON.Lab
18
19Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
20the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
21or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
andrewonlabe8e56fd2014-10-09 17:12:44 -040022
kelvin8ec71442015-01-15 16:57:00 -080023"""
Jon Hall05b2b432014-10-08 19:53:25 -040024import time
Jon Hall6801cda2015-07-15 14:13:45 -070025import types
Jon Hall05b2b432014-10-08 19:53:25 -040026import pexpect
kelvin-onlaba4074292015-07-09 15:19:49 -070027import os
Jon Hall6c44c0b2016-04-20 15:21:00 -070028import re
29import subprocess
pingping-lin6d23d9e2015-02-02 16:54:24 -080030from requests.models import Response
Jon Hall05b2b432014-10-08 19:53:25 -040031from drivers.common.clidriver import CLI
32
kelvin8ec71442015-01-15 16:57:00 -080033class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040034
kelvin8ec71442015-01-15 16:57:00 -080035 def __init__( self ):
36 """
37 Initialize client
38 """
Jon Hallefbd9792015-03-05 16:11:36 -080039 self.name = None
40 self.home = None
41 self.handle = None
Jon Hall6c44c0b2016-04-20 15:21:00 -070042 self.nicAddr = None
kelvin8ec71442015-01-15 16:57:00 -080043 super( CLI, self ).__init__()
44
45 def connect( self, **connectargs ):
46 """
Jon Hall05b2b432014-10-08 19:53:25 -040047 Creates ssh handle for ONOS "bench".
kelvin-onlaba4074292015-07-09 15:19:49 -070048 NOTE:
49 The ip_address would come from the topo file using the host tag, the
50 value can be an environment variable as well as a "localhost" to get
51 the ip address needed to ssh to the "bench"
kelvin8ec71442015-01-15 16:57:00 -080052 """
Jon Hall05b2b432014-10-08 19:53:25 -040053 try:
54 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080055 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us3b087132015-03-11 15:00:08 -070056 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040057 for key in self.options:
58 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080059 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040060 break
Jon Hall274b6642015-02-17 11:57:17 -080061 if self.home is None or self.home == "":
Jon Halle94919c2015-03-23 11:42:57 -070062 self.home = "~/onos"
Jon Hall21270ac2015-02-16 17:59:55 -080063
kelvin8ec71442015-01-15 16:57:00 -080064 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070065
kelvin-onlabc2b79102015-07-14 11:41:20 -070066 # The 'nodes' tag is optional and it is not required in .topo file
kelvin-onlaba4074292015-07-09 15:19:49 -070067 for key in self.options:
68 if key == "nodes":
kelvin-onlabc2b79102015-07-14 11:41:20 -070069 # Maximum number of ONOS nodes to run, if there is any
kelvin-onlaba4074292015-07-09 15:19:49 -070070 self.maxNodes = int( self.options[ 'nodes' ] )
71 break
72 self.maxNodes = None
73
kelvin-onlabc2b79102015-07-14 11:41:20 -070074 if self.maxNodes == None or self.maxNodes == "":
75 self.maxNodes = 100
kelvin-onlaba4074292015-07-09 15:19:49 -070076
kelvin-onlabc2b79102015-07-14 11:41:20 -070077
78 # Grabs all OC environment variables based on max number of nodes
kelvin-onlaba4074292015-07-09 15:19:49 -070079 self.onosIps = {} # Dictionary of all possible ONOS ip
80
81 try:
82 if self.maxNodes:
kelvin-onlaba4074292015-07-09 15:19:49 -070083 for i in range( self.maxNodes ):
84 envString = "OC" + str( i + 1 )
kelvin-onlabc2b79102015-07-14 11:41:20 -070085 # If there is no more OC# then break the loop
86 if os.getenv( envString ):
87 self.onosIps[ envString ] = os.getenv( envString )
88 else:
89 self.maxNodes = len( self.onosIps )
90 main.log.info( self.name +
91 ": Created cluster data with " +
92 str( self.maxNodes ) +
93 " maximum number" +
94 " of nodes" )
95 break
kelvin-onlaba4074292015-07-09 15:19:49 -070096
97 if not self.onosIps:
98 main.log.info( "Could not read any environment variable"
99 + " please load a cell file with all" +
100 " onos IP" )
Jon Hall5cf14d52015-07-16 12:15:19 -0700101 self.maxNodes = None
kelvin-onlaba4074292015-07-09 15:19:49 -0700102 else:
103 main.log.info( self.name + ": Found " +
104 str( self.onosIps.values() ) +
105 " ONOS IPs" )
kelvin-onlaba4074292015-07-09 15:19:49 -0700106 except KeyError:
107 main.log.info( "Invalid environment variable" )
108 except Exception as inst:
109 main.log.error( "Uncaught exception: " + str( inst ) )
110
111 try:
112 if os.getenv( str( self.ip_address ) ) != None:
113 self.ip_address = os.getenv( str( self.ip_address ) )
114 else:
115 main.log.info( self.name +
116 ": Trying to connect to " +
117 self.ip_address )
kelvin-onlaba4074292015-07-09 15:19:49 -0700118 except KeyError:
119 main.log.info( "Invalid host name," +
120 " connecting to local host instead" )
121 self.ip_address = 'localhost'
122 except Exception as inst:
123 main.log.error( "Uncaught exception: " + str( inst ) )
124
kelvin8ec71442015-01-15 16:57:00 -0800125 self.handle = super( OnosDriver, self ).connect(
126 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -0800127 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800128 port=self.port,
129 pwd=self.pwd,
130 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -0400131
Jon Hall05b2b432014-10-08 19:53:25 -0400132 if self.handle:
Jon Hall0fc0d452015-07-14 09:49:58 -0700133 self.handle.sendline( "cd " + self.home )
134 self.handle.expect( "\$" )
Jon Hall05b2b432014-10-08 19:53:25 -0400135 return self.handle
kelvin8ec71442015-01-15 16:57:00 -0800136 else:
Jon Hall0fc0d452015-07-14 09:49:58 -0700137 main.log.info( "Failed to create ONOS handle" )
Jon Hall05b2b432014-10-08 19:53:25 -0400138 return main.FALSE
139 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800140 main.log.error( self.name + ": EOF exception found" )
141 main.log.error( self.name + ": " + self.handle.before )
Jon Hall05b2b432014-10-08 19:53:25 -0400142 main.cleanup()
143 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800144 except Exception:
145 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall05b2b432014-10-08 19:53:25 -0400146 main.cleanup()
147 main.exit()
148
kelvin8ec71442015-01-15 16:57:00 -0800149 def disconnect( self ):
150 """
Jon Hall05b2b432014-10-08 19:53:25 -0400151 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800152 """
Jon Halld61331b2015-02-17 16:35:47 -0800153 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -0400154 try:
Jon Hall61282e32015-03-19 11:34:11 -0700155 if self.handle:
156 self.handle.sendline( "" )
157 self.handle.expect( "\$" )
158 self.handle.sendline( "exit" )
159 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -0400160 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800161 main.log.error( self.name + ": EOF exception found" )
162 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700163 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700164 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700165 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800166 except Exception:
167 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -0400168 response = main.FALSE
169 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400170
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400171 def getEpochMs( self ):
172 """
173 Returns milliseconds since epoch
Jon Hall4ba53f02015-07-29 13:07:41 -0700174
175 When checking multiple nodes in a for loop,
176 around a hundred milliseconds of difference (ascending) is
177 generally acceptable due to calltime of the function.
178 Few seconds, however, is not and it means clocks
179 are off sync.
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400180 """
181 try:
182 self.handle.sendline( 'date +%s.%N' )
183 self.handle.expect( 'date \+\%s\.\%N' )
184 self.handle.expect( '\$' )
185 epochMs = self.handle.before
186 return epochMs
187 except Exception:
188 main.log.exception( 'Uncaught exception getting epoch time' )
189 main.cleanup()
190 main.exit()
191
Jon Hall6c44c0b2016-04-20 15:21:00 -0700192 def onosPackage( self, opTimeout=180 ):
kelvin8ec71442015-01-15 16:57:00 -0800193 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400194 Produce a self-contained tar.gz file that can be deployed
Jon Hall64af8502015-12-15 10:09:33 -0800195 and executed on any platform with Java 8 JRE.
kelvin8ec71442015-01-15 16:57:00 -0800196 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400197 try:
Jon Hall64af8502015-12-15 10:09:33 -0800198 ret = main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800199 self.handle.sendline( "onos-package" )
200 self.handle.expect( "onos-package" )
Jon Hall96451092016-05-04 09:42:30 -0700201 while True:
202 i = self.handle.expect( [ "Downloading",
203 "Unknown options",
204 "No such file or directory",
205 "tar.gz",
206 "\$" ],
207 opTimeout )
208 handle = str( self.handle.before + self.handle.after )
209 if i == 0:
210 # Give more time to download the file
211 continue # expect again
212 elif i == 1:
213 # Incorrect usage
214 main.log.error( "onos-package does not recognize the given options" )
215 ret = main.FALSE
216 continue # expect again
217 elif i == 2:
218 # File(s) not found
219 main.log.error( "onos-package could not find a file or directory" )
220 ret = main.FALSE
221 continue # expect again
222 elif i == 3:
223 # tar.gz
224 continue # expect again
225 elif i == 4:
226 # Prompt returned
227 break
Jon Hallc6793552016-01-19 14:18:37 -0800228 main.log.info( "onos-package command returned: " + handle )
kelvin8ec71442015-01-15 16:57:00 -0800229 # As long as the sendline does not time out,
230 # return true. However, be careful to interpret
231 # the results of the onos-package command return
Jon Hall64af8502015-12-15 10:09:33 -0800232 return ret
233 except pexpect.TIMEOUT:
234 main.log.exception( self.name + ": TIMEOUT exception found in onosPackage" )
235 main.log.error( self.name + ": " + self.handle.before )
236 return main.FALSE
andrewonlab7735d852014-10-09 13:02:47 -0400237 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800238 main.log.error( self.name + ": EOF exception found" )
239 main.log.error( self.name + ": " + self.handle.before )
Jon Hall64af8502015-12-15 10:09:33 -0800240 main.cleanup()
241 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800242 except Exception:
243 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400244 main.cleanup()
245 main.exit()
246
kelvin-onlabd3b64892015-01-20 13:26:24 -0800247 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800248 """
andrewonlab8790abb2014-11-06 13:51:54 -0500249 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800250 """
andrewonlab8790abb2014-11-06 13:51:54 -0500251 try:
kelvin8ec71442015-01-15 16:57:00 -0800252 self.handle.sendline( "onos-build" )
253 self.handle.expect( "onos-build" )
254 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800255 "BUILD SUCCESS",
256 "ERROR",
257 "BUILD FAILED" ],
258 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800259 handle = str( self.handle.before )
Jon Hall3b489db2015-10-05 14:38:37 -0700260 self.handle.expect( "\$" )
andrewonlab8790abb2014-11-06 13:51:54 -0500261
kelvin8ec71442015-01-15 16:57:00 -0800262 main.log.info( "onos-build command returned: " +
263 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500264
265 if i == 0:
266 return main.TRUE
267 else:
268 return handle
269
270 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800271 main.log.error( self.name + ": EOF exception found" )
272 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800273 except Exception:
274 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500275 main.cleanup()
276 main.exit()
277
shahshreya9f531fe2015-06-10 12:03:51 -0700278 def cleanInstall( self, skipTest=False, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800279 """
280 Runs mvn clean install in the root of the ONOS directory.
281 This will clean all ONOS artifacts then compile each module
shahshreya9f531fe2015-06-10 12:03:51 -0700282 Optional:
283 skipTest - Does "-DskipTests -Dcheckstyle.skip -U -T 1C" which
284 skip the test. This will make the building faster.
285 Disregarding the credibility of the build
kelvin8ec71442015-01-15 16:57:00 -0800286 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400287 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800288 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400289 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800290 main.log.info( "Running 'mvn clean install' on " +
291 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800292 ". This may take some time." )
293 self.handle.sendline( "cd " + self.home )
294 self.handle.expect( "\$" )
Jon Hallea7818b2014-10-09 14:30:59 -0400295
kelvin8ec71442015-01-15 16:57:00 -0800296 self.handle.sendline( "" )
297 self.handle.expect( "\$" )
shahshreya9f531fe2015-06-10 12:03:51 -0700298
299 if not skipTest:
300 self.handle.sendline( "mvn clean install" )
301 self.handle.expect( "mvn clean install" )
302 else:
303 self.handle.sendline( "mvn clean install -DskipTests" +
304 " -Dcheckstyle.skip -U -T 1C" )
305 self.handle.expect( "mvn clean install -DskipTests" +
306 " -Dcheckstyle.skip -U -T 1C" )
kelvin8ec71442015-01-15 16:57:00 -0800307 while True:
308 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800309 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800310 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400311 'BUILD\sFAILURE',
312 'BUILD\sSUCCESS',
Jon Halle94919c2015-03-23 11:42:57 -0700313 'onos\$', #TODO: fix this to be more generic?
Jon Hallde9d9aa2014-10-08 20:36:02 -0400314 'ONOS\$',
pingping-lin57a56ce2015-05-20 16:43:48 -0700315 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400316 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800317 main.log.error( self.name + ":There is insufficient memory \
318 for the Java Runtime Environment to continue." )
319 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400320 main.cleanup()
321 main.exit()
322 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800323 main.log.error( self.name + ": Build failure!" )
324 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400325 main.cleanup()
326 main.exit()
327 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800328 main.log.info( self.name + ": Build success!" )
Jon Halle94919c2015-03-23 11:42:57 -0700329 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800330 main.log.info( self.name + ": Build complete" )
331 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400332 for line in self.handle.before.splitlines():
333 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800334 main.log.info( line )
335 self.handle.sendline( "" )
336 self.handle.expect( "\$", timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400337 return main.TRUE
Jon Halle94919c2015-03-23 11:42:57 -0700338 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800339 main.log.error(
340 self.name +
341 ": mvn clean install TIMEOUT!" )
342 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400343 main.cleanup()
344 main.exit()
345 else:
Jon Hall274b6642015-02-17 11:57:17 -0800346 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800347 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800348 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400349 main.cleanup()
350 main.exit()
351 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800352 main.log.error( self.name + ": EOF exception found" )
353 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400354 main.cleanup()
355 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800356 except Exception:
357 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400358 main.cleanup()
359 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400360
Jon Hall3576f572016-08-23 10:01:07 -0700361 def buckBuild( self, timeout=180 ):
362 """
363 Build onos using buck.
364 """
365 try:
366 ret = main.TRUE
367 self.handle.sendline( "buck build onos" )
368 self.handle.expect( "buck build onos" )
369 output = ""
370 while True:
371 i = self.handle.expect( [ "This does not appear to be the root of a Buck project.",
372 "\n",
373 "BUILD FAILED",
374 "\$" ],
375 timeout=timeout )
376 output += str( self.handle.before + self.handle.after )
377 if i == 0:
378 main.log.error( "Wrong location" )
379 ret = main.FALSE
380 elif i == 1:
381 # end of a line, buck is still printing output
382 pass
383 elif i == 2:
384 # Build failed
385 main.log.error( "Build failed" )
386 ret = main.FALSE
387 elif i == 3:
388 # Prompt returned
389 break
390 main.log.debug( output )
Jon Hallf03af032016-09-23 15:28:50 -0700391 # FIXME: This is a workaround for a bug in buck see ONOS-5320
392 self.buckPackage( )
Jon Hall3576f572016-08-23 10:01:07 -0700393 return ret
394 except pexpect.TIMEOUT:
395 main.log.exception( self.name + ": TIMEOUT exception found" )
396 main.log.error( self.name + ": " + self.handle.before )
397 return main.FALSE
398 except pexpect.EOF:
399 main.log.error( self.name + ": EOF exception found" )
400 main.log.error( self.name + ": " + self.handle.before )
401 main.cleanup()
402 main.exit()
403 except Exception:
404 main.log.exception( "Failed to build and package ONOS" )
405 main.cleanup()
406 main.exit()
407
Jon Hallf03af032016-09-23 15:28:50 -0700408 def buckPackage( self, timeout=180 ):
409 """
410 Package onos using buck. This will not build the source and this rule
411 should be automatically run when building onos.
412 """
413 try:
414 ret = main.TRUE
415 self.handle.sendline( "buck build package" )
416 self.handle.expect( "buck build package" )
417 output = ""
418 while True:
419 i = self.handle.expect( [ "This does not appear to be the root of a Buck project.",
420 "\n",
421 "BUILD FAILED",
422 "\$" ],
423 timeout=timeout )
424 output += str( self.handle.before + self.handle.after )
425 if i == 0:
426 main.log.error( "Wrong location" )
427 ret = main.FALSE
428 elif i == 1:
429 # end of a line, buck is still printing output
430 pass
431 elif i == 2:
432 # Build failed
433 main.log.error( "Build failed" )
434 ret = main.FALSE
435 elif i == 3:
436 # Prompt returned
437 break
438 main.log.debug( output )
439 return ret
440 except pexpect.TIMEOUT:
441 main.log.exception( self.name + ": TIMEOUT exception found" )
442 main.log.error( self.name + ": " + self.handle.before )
443 return main.FALSE
444 except pexpect.EOF:
445 main.log.error( self.name + ": EOF exception found" )
446 main.log.error( self.name + ": " + self.handle.before )
447 main.cleanup()
448 main.exit()
449 except Exception:
450 main.log.exception( "Failed to package ONOS" )
451 main.cleanup()
452 main.exit()
453
454
Jon Hall61282e32015-03-19 11:34:11 -0700455 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800456 """
Jon Hallacabffd2014-10-09 12:36:53 -0400457 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800458
Jon Hall61282e32015-03-19 11:34:11 -0700459 If the fastForward boolean is set to true, only git pulls that can
460 be fast forwarded will be performed. IE if you have not local commits
461 in your branch.
462
Jon Hallacabffd2014-10-09 12:36:53 -0400463 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800464 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400465 for the purpose of pulling from other nodes if necessary.
466
Jon Hall47a93fb2015-01-06 16:46:06 -0800467 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400468 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800469 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400470 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400471
kelvin8ec71442015-01-15 16:57:00 -0800472 """
Jon Hallacabffd2014-10-09 12:36:53 -0400473 try:
kelvin8ec71442015-01-15 16:57:00 -0800474 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800475 self.handle.expect( self.home + "\$" )
Jon Hall61282e32015-03-19 11:34:11 -0700476 cmd = "git pull"
477 if comp1 != "":
478 cmd += ' ' + comp1
479 if fastForward:
480 cmd += ' ' + " --ff-only"
481 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800482 i = self.handle.expect(
483 [
484 'fatal',
485 'Username\sfor\s(.*):\s',
486 '\sfile(s*) changed,\s',
487 'Already up-to-date',
488 'Aborting',
489 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800490 'You asked me to pull without telling me which branch you',
491 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700492 'Please enter a commit message to explain why this merge',
493 'Found a swap file by the name',
494 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800495 pexpect.TIMEOUT ],
496 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800497 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700498 main.log.error( self.name + ": Git pull had some issue" )
499 output = self.handle.after
500 self.handle.expect( '\$' )
501 output += self.handle.before
502 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400503 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800504 elif i == 1:
505 main.log.error(
506 self.name +
507 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400508 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800509 elif i == 2:
510 main.log.info(
511 self.name +
512 ": Git Pull - pulling repository now" )
kelvin-onlaba1484582015-02-02 15:46:20 -0800513 self.handle.expect( self.home + "\$", 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800514 # So that only when git pull is done, we do mvn clean compile
515 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800516 elif i == 3:
517 main.log.info( self.name + ": Git Pull - Already up to date" )
Jon Hall47a93fb2015-01-06 16:46:06 -0800518 return i
kelvin8ec71442015-01-15 16:57:00 -0800519 elif i == 4:
520 main.log.info(
521 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800522 ": Git Pull - Aborting..." +
523 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400524 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800525 elif i == 5:
526 main.log.info(
527 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800528 ": Git Pull - You are not currently " +
529 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400530 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800531 elif i == 6:
532 main.log.info(
533 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800534 ": Git Pull - You have not configured an upstream " +
535 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400536 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800537 elif i == 7:
538 main.log.info(
539 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800540 ": Git Pull - Pull is not possible because " +
541 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400542 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800543 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700544 # NOTE: abandoning test since we can't reliably handle this
545 # there could be different default text editors and we
546 # also don't know if we actually want to make the commit
547 main.log.error( "Git pull resulted in a merge commit message" +
548 ". Exiting test!" )
549 main.cleanup()
550 main.exit()
551 elif i == 9: # Merge commit message but swap file exists
552 main.log.error( "Git pull resulted in a merge commit message" +
553 " but a swap file exists." )
554 try:
555 self.handle.send( 'A' ) # Abort
556 self.handle.expect( "\$" )
557 return main.ERROR
558 except Exception:
559 main.log.exception( "Couldn't exit editor prompt!")
560 main.cleanup()
561 main.exit()
562 elif i == 10: # In the middle of a merge commit
563 main.log.error( "Git branch is in the middle of a merge. " )
564 main.log.warn( self.handle.before + self.handle.after )
565 return main.ERROR
566 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800567 main.log.error( self.name + ": Git Pull - TIMEOUT" )
568 main.log.error(
569 self.name + " Response was: " + str(
570 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400571 return main.ERROR
572 else:
kelvin8ec71442015-01-15 16:57:00 -0800573 main.log.error(
574 self.name +
575 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400576 return main.ERROR
577 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800578 main.log.error( self.name + ": EOF exception found" )
579 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400580 main.cleanup()
581 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800582 except Exception:
583 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400584 main.cleanup()
585 main.exit()
586
kelvin-onlabd3b64892015-01-20 13:26:24 -0800587 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800588 """
Jon Hallacabffd2014-10-09 12:36:53 -0400589 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800590
Jon Hallacabffd2014-10-09 12:36:53 -0400591 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800592 If used as gitCheckout( "branch" ) it will do git checkout
593 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400594
595 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800596 branch of the ONOS repository. If it has any problems, it will return
597 main.ERROR.
598 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400599 successful then the function will return main.TRUE.
600
kelvin8ec71442015-01-15 16:57:00 -0800601 """
Jon Hallacabffd2014-10-09 12:36:53 -0400602 try:
kelvin8ec71442015-01-15 16:57:00 -0800603 self.handle.sendline( "cd " + self.home )
kelvin-onlaba1484582015-02-02 15:46:20 -0800604 self.handle.expect( self.home + "\$" )
Jon Hall274b6642015-02-17 11:57:17 -0800605 main.log.info( self.name +
606 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800607 cmd = "git checkout " + branch
608 self.handle.sendline( cmd )
609 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800610 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800611 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700612 'Username for (.*): ',
613 'Already on \'',
Jon Hall7a8354f2015-06-10 15:37:00 -0700614 'Switched to (a new )?branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800615 pexpect.TIMEOUT,
616 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800617 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800618 'error: you need to resolve your current index first',
619 "You are in 'detached HEAD' state.",
620 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800621 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800622 if i == 0:
623 main.log.error(
624 self.name +
625 ": Git checkout had some issue..." )
626 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400627 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800628 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800629 main.log.error(
630 self.name +
631 ": Git checkout asking for username." +
632 " Please configure your local git repository to be able " +
633 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800634 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400635 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800636 elif i == 2:
637 main.log.info(
638 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800639 ": Git Checkout %s : Already on this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800640 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800641 # main.log.info( "DEBUG: after checkout cmd = "+
642 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400643 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800644 elif i == 3:
645 main.log.info(
646 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800647 ": Git checkout %s - Switched to this branch" % branch )
kelvin-onlaba1484582015-02-02 15:46:20 -0800648 self.handle.expect( self.home + "\$" )
kelvin8ec71442015-01-15 16:57:00 -0800649 # main.log.info( "DEBUG: after checkout cmd = "+
650 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400651 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800652 elif i == 4:
653 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
654 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800655 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400656 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800657 elif i == 5:
658 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800659 main.log.error(
660 self.name +
661 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800662 "Your local changes to the following files would" +
663 " be overwritten by checkout:" +
664 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800665 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500666 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800667 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800668 main.log.error(
669 self.name +
670 ": Git checkout error: \n" +
671 "You need to resolve your current index first:" +
672 str( self.handle.before ) )
kelvin-onlaba1484582015-02-02 15:46:20 -0800673 self.handle.expect( self.home + "\$" )
Jon Hall81e29af2014-11-04 20:41:23 -0500674 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800675 elif i == 7:
676 main.log.info(
677 self.name +
678 ": Git checkout " + str( branch ) +
679 " - You are in 'detached HEAD' state. HEAD is now at " +
680 str( branch ) )
681 self.handle.expect( self.home + "\$" )
682 return main.TRUE
683 elif i == 8: # Already in detached HEAD on the specified commit
684 main.log.info(
685 self.name +
686 ": Git Checkout %s : Already on commit" % branch )
687 self.handle.expect( self.home + "\$" )
688 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400689 else:
kelvin8ec71442015-01-15 16:57:00 -0800690 main.log.error(
691 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800692 ": Git Checkout - Unexpected response, " +
693 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800694 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400695 return main.ERROR
696
697 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800698 main.log.error( self.name + ": EOF exception found" )
699 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400700 main.cleanup()
701 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800702 except Exception:
703 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400704 main.cleanup()
705 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400706
pingping-lin6d23d9e2015-02-02 16:54:24 -0800707 def getBranchName( self ):
pingping-linf30cf272015-05-29 15:54:07 -0700708 main.log.info( "self.home = " )
709 main.log.info( self.home )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800710 self.handle.sendline( "cd " + self.home )
pingping-lin9bf3d8f2015-05-29 16:05:28 -0700711 self.handle.expect( self.home + "\$" )
pingping-lin6d23d9e2015-02-02 16:54:24 -0800712 self.handle.sendline( "git name-rev --name-only HEAD" )
713 self.handle.expect( "git name-rev --name-only HEAD" )
714 self.handle.expect( "\$" )
715
716 lines = self.handle.before.splitlines()
717 if lines[1] == "master":
718 return "master"
719 elif lines[1] == "onos-1.0":
720 return "onos-1.0"
721 else:
722 main.log.info( lines[1] )
723 return "unexpected ONOS branch for SDN-IP test"
724
kelvin-onlabd3b64892015-01-20 13:26:24 -0800725 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800726 """
Jon Hall274b6642015-02-17 11:57:17 -0800727 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800728 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800729 """
Jon Hall45ec0922014-10-10 19:33:49 -0400730 try:
kelvin8ec71442015-01-15 16:57:00 -0800731 self.handle.sendline( "" )
732 self.handle.expect( "\$" )
733 self.handle.sendline(
734 "cd " +
735 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800736 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
737 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800738 # NOTE: for some reason there are backspaces inserted in this
739 # phrase when run from Jenkins on some tests
740 self.handle.expect( "never" )
741 self.handle.expect( "\$" )
742 response = ( self.name + ": \n" + str(
743 self.handle.before + self.handle.after ) )
744 self.handle.sendline( "cd " + self.home )
745 self.handle.expect( "\$" )
746 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400747 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500748 print line
749 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700750 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800751 for line in lines[ 2:-1 ]:
752 # Bracket replacement is for Wiki-compliant
753 # formatting. '<' or '>' are interpreted
754 # as xml specific tags that cause errors
755 line = line.replace( "<", "[" )
756 line = line.replace( ">", "]" )
pingping-lin763ee042015-05-20 17:45:30 -0700757 #main.log.wiki( "\t" + line )
758 main.log.wiki( line + "<br /> " )
759 main.log.summary( line )
760 main.log.wiki( "</blockquote>" )
761 main.log.summary("\n")
kelvin8ec71442015-01-15 16:57:00 -0800762 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400763 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800764 main.log.error( self.name + ": EOF exception found" )
765 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400766 main.cleanup()
767 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800768 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800769 main.log.error( self.name + ": TIMEOUT exception found" )
770 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800771 main.cleanup()
772 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800773 except Exception:
774 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400775 main.cleanup()
776 main.exit()
777
kelvin-onlabd3b64892015-01-20 13:26:24 -0800778 def createCellFile( self, benchIp, fileName, mnIpAddrs,
Pier88189b62016-09-07 17:01:53 -0700779 appString, onosIpAddrs, onosUser="sdn", useSSH=False ):
kelvin8ec71442015-01-15 16:57:00 -0800780 """
andrewonlab94282092014-10-10 13:00:11 -0400781 Creates a cell file based on arguments
782 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800783 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400784 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800785 * File name of the cell file ( fileName )
786 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800787 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400788 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800789 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400790 - Must be passed in as last arguments
Flavio Castrocc38a542016-03-03 13:15:46 -0800791 * ONOS USER (onosUser)
792 - optional argument to set ONOS_USER environment variable
kelvin8ec71442015-01-15 16:57:00 -0800793
andrewonlab94282092014-10-10 13:00:11 -0400794 NOTE: Assumes cells are located at:
795 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800796 """
797 # Variable initialization
Jon Hall6801cda2015-07-15 14:13:45 -0700798 cellDirectory = self.home + "/tools/test/cells/"
kelvin8ec71442015-01-15 16:57:00 -0800799 # We want to create the cell file in the dependencies directory
800 # of TestON first, then copy over to ONOS bench
kelvin-onlabd3b64892015-01-20 13:26:24 -0800801 tempDirectory = "/tmp/"
kelvin8ec71442015-01-15 16:57:00 -0800802 # Create the cell file in the directory for writing ( w+ )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800803 cellFile = open( tempDirectory + fileName, 'w+' )
Jon Hall6801cda2015-07-15 14:13:45 -0700804 if isinstance( onosIpAddrs, types.StringType ):
805 onosIpAddrs = [ onosIpAddrs ]
kelvin8ec71442015-01-15 16:57:00 -0800806
cameron@onlab.us75900962015-03-30 13:22:49 -0700807 # App string is hardcoded environment variables
kelvin8ec71442015-01-15 16:57:00 -0800808 # That you may wish to use by default on startup.
cameron@onlab.us75900962015-03-30 13:22:49 -0700809 # Note that you may not want certain apps listed
kelvin8ec71442015-01-15 16:57:00 -0800810 # on here.
cameron@onlab.us75900962015-03-30 13:22:49 -0700811 appString = "export ONOS_APPS=" + appString
Flavio Castrocc38a542016-03-03 13:15:46 -0800812 onosGroup = "export ONOS_GROUP=" + onosUser
813 onosUser = "export ONOS_USER=" + onosUser
Pier88189b62016-09-07 17:01:53 -0700814 if useSSH:
815 onosUseSSH = "export ONOS_USE_SSH=true"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800816 mnString = "export OCN="
kelvin-onlabf70fd542015-05-07 18:41:40 -0700817 if mnIpAddrs == "":
818 mnString = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800819 onosString = "export OC"
820 tempCount = 1
kelvin8ec71442015-01-15 16:57:00 -0800821
kelvin-onlabd3b64892015-01-20 13:26:24 -0800822 # Create ONOSNIC ip address prefix
kelvin-onlaba4074292015-07-09 15:19:49 -0700823 tempOnosIp = str( onosIpAddrs[ 0 ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800824 tempList = []
825 tempList = tempOnosIp.split( "." )
kelvin8ec71442015-01-15 16:57:00 -0800826 # Omit last element of list to format for NIC
kelvin-onlabd3b64892015-01-20 13:26:24 -0800827 tempList = tempList[ :-1 ]
kelvin8ec71442015-01-15 16:57:00 -0800828 # Structure the nic string ip
kelvin-onlabd3b64892015-01-20 13:26:24 -0800829 nicAddr = ".".join( tempList ) + ".*"
Jon Hall6c44c0b2016-04-20 15:21:00 -0700830 self.nicAddr = nicAddr
kelvin-onlabd3b64892015-01-20 13:26:24 -0800831 onosNicString = "export ONOS_NIC=" + nicAddr
andrewonlab94282092014-10-10 13:00:11 -0400832
833 try:
kelvin8ec71442015-01-15 16:57:00 -0800834 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800835 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400836
kelvin-onlabd3b64892015-01-20 13:26:24 -0800837 for arg in onosIpAddrs:
838 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800839 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400840 # export OC1="10.128.20.11"
841 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800842 cellFile.write( onosString + str( tempCount ) +
Jon Hall6f665652015-09-18 10:08:07 -0700843 "=\"" + arg + "\"\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800844 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800845
Jon Hall6f665652015-09-18 10:08:07 -0700846 cellFile.write( "export OCI=$OC1\n" )
847 cellFile.write( mnString + "\"" + mnIpAddrs + "\"\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700848 cellFile.write( appString + "\n" )
Flavio Castrocc38a542016-03-03 13:15:46 -0800849 cellFile.write( onosGroup + "\n" )
850 cellFile.write( onosUser + "\n" )
Pier88189b62016-09-07 17:01:53 -0700851 if useSSH:
852 cellFile.write( onosUseSSH + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800853 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400854
kelvin8ec71442015-01-15 16:57:00 -0800855 # We use os.system to send the command to TestON cluster
856 # to account for the case in which TestON is not located
857 # on the same cluster as the ONOS bench
858 # Note that even if TestON is located on the same cluster
859 # as ONOS bench, you must setup passwordless ssh
860 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabc2b79102015-07-14 11:41:20 -0700861 os.system( "scp " + tempDirectory + fileName + " " +
862 self.user_name + "@" + self.ip_address + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400863
andrewonlab2a6c9342014-10-16 13:40:15 -0400864 return main.TRUE
865
andrewonlab94282092014-10-10 13:00:11 -0400866 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800867 main.log.error( self.name + ": EOF exception found" )
868 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400869 main.cleanup()
870 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800871 except Exception:
872 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400873 main.cleanup()
874 main.exit()
875
kelvin-onlabd3b64892015-01-20 13:26:24 -0800876 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800877 """
andrewonlab95ca1462014-10-09 14:04:24 -0400878 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800879 """
Hari Krishna03f530e2015-07-10 17:28:27 -0700880 import re
andrewonlab95ca1462014-10-09 14:04:24 -0400881 try:
882 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800883 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400884 main.cleanup()
885 main.exit()
886 else:
kelvin8ec71442015-01-15 16:57:00 -0800887 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800888 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800889 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400890 # and that this driver will have to change accordingly
Jon Hall3b489db2015-10-05 14:38:37 -0700891 self.handle.expect( str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800892 handleBefore = self.handle.before
893 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800894 # Get the rest of the handle
Jon Hall3b489db2015-10-05 14:38:37 -0700895 self.handle.expect( "\$" )
Jon Hall439c8912016-04-15 02:22:03 -0700896 time.sleep(10)
kelvin-onlabd3b64892015-01-20 13:26:24 -0800897 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400898
Hari Krishna03f530e2015-07-10 17:28:27 -0700899 cell_result = handleBefore + handleAfter + handleMore
suibin zhang116647a2016-05-06 16:30:09 -0700900 #print cell_result
Hari Krishna03f530e2015-07-10 17:28:27 -0700901 if( re.search( "No such cell", cell_result ) ):
902 main.log.error( "Cell call returned: " + handleBefore +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800903 handleAfter + handleMore )
Hari Krishna03f530e2015-07-10 17:28:27 -0700904 main.cleanup()
905 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400906 return main.TRUE
andrewonlab95ca1462014-10-09 14:04:24 -0400907 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800908 main.log.error( self.name + ": EOF exception found" )
909 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400910 main.cleanup()
911 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800912 except Exception:
913 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400914 main.cleanup()
915 main.exit()
916
kelvin-onlabd3b64892015-01-20 13:26:24 -0800917 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800918 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400919 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800920 """
921 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400922
andrewonlabc03bf6c2014-10-09 14:56:18 -0400923 try:
kelvin8ec71442015-01-15 16:57:00 -0800924 # Clean handle by sending empty and expecting $
925 self.handle.sendline( "" )
926 self.handle.expect( "\$" )
927 self.handle.sendline( "onos-verify-cell" )
928 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800929 handleBefore = self.handle.before
930 handleAfter = self.handle.after
kelvin-onlabd3b64892015-01-20 13:26:24 -0800931 main.log.info( "Verify cell returned: " + handleBefore +
Jon Hall3b489db2015-10-05 14:38:37 -0700932 handleAfter )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400933 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800934 except pexpect.ExceptionPexpect as e:
Jon Hall3b489db2015-10-05 14:38:37 -0700935 main.log.exception( self.name + ": Pexpect exception found: " )
kelvin8ec71442015-01-15 16:57:00 -0800936 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400937 main.cleanup()
938 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800939 except Exception:
940 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400941 main.cleanup()
942 main.exit()
943
jenkins1e99e7b2015-04-02 18:15:39 -0700944 def onosCfgSet( self, ONOSIp, configName, configParam ):
945 """
946 Uses 'onos <node-ip> cfg set' to change a parameter value of an
Jon Hall4ba53f02015-07-29 13:07:41 -0700947 application.
948
jenkins1e99e7b2015-04-02 18:15:39 -0700949 ex)
950 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
jenkins1e99e7b2015-04-02 18:15:39 -0700951 ONOSIp = '10.0.0.1'
952 configName = 'org.onosproject.myapp'
953 configParam = 'appSetting 1'
jenkins1e99e7b2015-04-02 18:15:39 -0700954 """
Jon Hall72280bc2016-01-25 14:29:05 -0800955 try:
956 cfgStr = "onos {} cfg set {} {}".format( ONOSIp,
957 configName,
958 configParam )
959 self.handle.sendline( "" )
960 self.handle.expect( ":~" )
961 self.handle.sendline( cfgStr )
962 self.handle.expect("cfg set")
963 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700964
Jon Hall72280bc2016-01-25 14:29:05 -0800965 paramValue = configParam.split(" ")[1]
966 paramName = configParam.split(" ")[0]
Jon Hall4ba53f02015-07-29 13:07:41 -0700967
Jon Hall72280bc2016-01-25 14:29:05 -0800968 checkStr = 'onos {} cfg get " {} {} " '.format( ONOSIp, configName, paramName )
Jon Hall4ba53f02015-07-29 13:07:41 -0700969
Jon Hall72280bc2016-01-25 14:29:05 -0800970 self.handle.sendline( checkStr )
971 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700972
Jon Hall72280bc2016-01-25 14:29:05 -0800973 if "value=" + paramValue + "," in self.handle.before:
974 main.log.info("cfg " + configName + " successfully set to " + configParam)
975 return main.TRUE
976 except pexpect.ExceptionPexpect as e:
977 main.log.exception( self.name + ": Pexpect exception found: " )
978 main.log.error( self.name + ": " + self.handle.before )
979 main.cleanup()
980 main.exit()
981 except Exception:
982 main.log.exception( self.name + ": Uncaught exception!" )
983 main.cleanup()
984 main.exit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700985
kelvin-onlabd3b64892015-01-20 13:26:24 -0800986 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800987 """
andrewonlab05e362f2014-10-10 00:40:57 -0400988 Uses 'onos' command to send various ONOS CLI arguments.
989 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800990 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400991 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800992
993 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400994 CLI commands for ONOS. Try to use this function first
995 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800996 function.
997 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400998 by starting onos, and typing in 'onos' to enter the
999 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -08001000 available commands.
1001 """
andrewonlab05e362f2014-10-10 00:40:57 -04001002 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001003 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -08001004 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -04001005 return main.FALSE
1006 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -08001007 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -04001008 return main.FALSE
1009
kelvin8ec71442015-01-15 16:57:00 -08001010 cmdstr = str( cmdstr )
1011 self.handle.sendline( "" )
1012 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -04001013
kelvin-onlabd3b64892015-01-20 13:26:24 -08001014 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
kelvin8ec71442015-01-15 16:57:00 -08001015 self.handle.expect( "\$" )
andrewonlab05e362f2014-10-10 00:40:57 -04001016
kelvin-onlabd3b64892015-01-20 13:26:24 -08001017 handleBefore = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001018 main.log.info( "Command sent successfully" )
kelvin8ec71442015-01-15 16:57:00 -08001019 # Obtain return handle that consists of result from
1020 # the onos command. The string may need to be
1021 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001022 returnString = handleBefore
kelvin-onlabd3b64892015-01-20 13:26:24 -08001023 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -04001024 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001025 main.log.error( self.name + ": EOF exception found" )
1026 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -04001027 main.cleanup()
1028 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001029 except Exception:
1030 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -04001031 main.cleanup()
1032 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -04001033
Pier88189b62016-09-07 17:01:53 -07001034 def onosSecureSSH( self, userName="onos", userPWD="rocks", node=""):
1035 """
1036 Enables secure access to ONOS console
1037 by removing default users & keys.
1038
1039 onos-secure-ssh -u onos -p rocks node
1040
1041 Returns: main.TRUE on success and main.FALSE on failure
1042 """
1043
1044 try:
1045 self.handle.sendline( " onos-secure-ssh -u " + userName + " -p " + userPWD + " " + node )
1046
1047 # NOTE: this timeout may need to change depending on the network
1048 # and size of ONOS
1049 # TODO: Handle the other possible error
1050 i = self.handle.expect([ "Network\sis\sunreachable",
1051 "\$",
1052 pexpect.TIMEOUT ], timeout=180 )
1053 if i == 0:
1054 # can't reach ONOS node
1055 main.log.warn( "Network is unreachable" )
1056 self.handle.expect( "\$" )
1057 return main.FALSE
1058 elif i == 1:
1059 # Process started
1060 main.log.info(
1061 "Secure SSH performed on " +
1062 node)
1063 return main.TRUE
1064 except pexpect.EOF:
1065 main.log.error( self.name + ": EOF exception found" )
1066 main.log.error( self.name + ": " + self.handle.before )
1067 main.cleanup()
1068 main.exit()
1069 except Exception:
1070 main.log.exception( self.name + ": Uncaught exception!" )
1071 main.cleanup()
1072 main.exit()
1073
1074
kelvin-onlabd3b64892015-01-20 13:26:24 -08001075 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001076 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001077 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -08001078 If -f option is provided, it also forces an uninstall.
1079 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -04001080 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -08001081 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -04001082 files to certain onos nodes
1083
1084 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -08001085 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001086 try:
andrewonlab114768a2014-11-14 12:44:44 -05001087 if options:
kelvin8ec71442015-01-15 16:57:00 -08001088 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -05001089 else:
kelvin8ec71442015-01-15 16:57:00 -08001090 self.handle.sendline( "onos-install " + node )
1091 self.handle.expect( "onos-install " )
1092 # NOTE: this timeout may need to change depending on the network
1093 # and size of ONOS
1094 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001095 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -08001096 "ONOS\sis\salready\sinstalled",
Jeremyc72b2582016-02-26 18:27:38 -08001097 "already\sup-to-date",
Jon Hall3576f572016-08-23 10:01:07 -07001098 "does not exist",
Jeremyc72b2582016-02-26 18:27:38 -08001099 "\$",
Jon Hall6c44c0b2016-04-20 15:21:00 -07001100 pexpect.TIMEOUT ], timeout=180 )
Jon Hall7993bfc2014-10-09 16:30:14 -04001101 if i == 0:
Jon Hall3576f572016-08-23 10:01:07 -07001102 # can't reach ONOS node
kelvin8ec71442015-01-15 16:57:00 -08001103 main.log.warn( "Network is unreachable" )
Jon Hall3b489db2015-10-05 14:38:37 -07001104 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001105 return main.FALSE
1106 elif i == 1:
Jon Hall3576f572016-08-23 10:01:07 -07001107 # Process started
kelvin8ec71442015-01-15 16:57:00 -08001108 main.log.info(
1109 "ONOS was installed on " +
1110 node +
1111 " and started" )
Jon Hall3b489db2015-10-05 14:38:37 -07001112 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001113 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001114 elif i == 2 or i == 3:
1115 # same bits are already on ONOS node
Jeremyc72b2582016-02-26 18:27:38 -08001116 main.log.info( "ONOS is already installed on " + node )
1117 self.handle.expect( "\$" )
1118 return main.TRUE
1119 elif i == 4:
Jon Hall3576f572016-08-23 10:01:07 -07001120 # onos not packaged
1121 main.log.error( "ONOS package not found." )
1122 self.handle.expect( "\$" )
1123 return main.FALSE
1124 elif i == 5:
1125 # prompt
Jeremyc72b2582016-02-26 18:27:38 -08001126 main.log.info( "ONOS was installed on " + node )
1127 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001128 elif i == 6:
1129 # timeout
kelvin8ec71442015-01-15 16:57:00 -08001130 main.log.info(
1131 "Installation of ONOS on " +
1132 node +
1133 " timed out" )
Jon Hall3b489db2015-10-05 14:38:37 -07001134 self.handle.expect( "\$" )
Jon Hall53c5e662016-04-13 16:06:56 -07001135 main.log.warn( self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001136 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -04001137 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001138 main.log.error( self.name + ": EOF exception found" )
1139 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -04001140 main.cleanup()
1141 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001142 except Exception:
1143 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -04001144 main.cleanup()
1145 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -04001146
kelvin-onlabd3b64892015-01-20 13:26:24 -08001147 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001148 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001149 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001150 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001151 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001152 try:
kelvin8ec71442015-01-15 16:57:00 -08001153 self.handle.sendline( "" )
1154 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001155 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001156 " start" )
1157 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -04001158 "Job\sis\salready\srunning",
1159 "start/running",
Jeremyd0e9a6d2016-03-02 11:28:52 -08001160 "\$",
andrewonlab8d0d7d72014-10-09 16:33:15 -04001161 "Unknown\sinstance",
Jeremy Songster14c13572016-04-21 17:34:03 -07001162 pexpect.TIMEOUT ], timeout=180 )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001163 if i == 0:
Jon Halleab7a242016-03-04 10:20:43 -08001164 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001165 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001166 return main.TRUE
1167 elif i == 1:
Jon Halleab7a242016-03-04 10:20:43 -08001168 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001169 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001170 return main.TRUE
Jeremyd0e9a6d2016-03-02 11:28:52 -08001171 elif i == 2:
1172 main.log.info( "ONOS service started" )
1173 return main.TRUE
andrewonlab8d0d7d72014-10-09 16:33:15 -04001174 else:
Jon Halleab7a242016-03-04 10:20:43 -08001175 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001176 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001177 main.cleanup()
1178 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001179 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 )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001182 main.cleanup()
1183 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001184 except Exception:
1185 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001186 main.cleanup()
1187 main.exit()
1188
kelvin-onlabd3b64892015-01-20 13:26:24 -08001189 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001190 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001191 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001192 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001193 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001194 try:
kelvin8ec71442015-01-15 16:57:00 -08001195 self.handle.sendline( "" )
1196 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001197 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001198 " stop" )
1199 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -04001200 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -07001201 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -04001202 "Unknown\sinstance",
YPZhang77badfc2016-03-09 10:28:59 -08001203 "\$",
Jeremy Songster14c13572016-04-21 17:34:03 -07001204 pexpect.TIMEOUT ], timeout=180 )
andrewonlab2b30bd32014-10-09 16:48:55 -04001205 if i == 0:
YPZhang77badfc2016-03-09 10:28:59 -08001206 self.handle.expect( "\$" )
kelvin8ec71442015-01-15 16:57:00 -08001207 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001208 return main.TRUE
1209 elif i == 1:
YPZhang77badfc2016-03-09 10:28:59 -08001210 self.handle.expect( "\$" )
Jon Hall65844a32015-03-09 19:09:37 -07001211 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001212 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -04001213 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -07001214 elif i == 2:
YPZhang77badfc2016-03-09 10:28:59 -08001215 self.handle.expect( "\$" )
Jon Hall61282e32015-03-19 11:34:11 -07001216 main.log.warn( "ONOS wasn't running" )
1217 return main.TRUE
YPZhang77badfc2016-03-09 10:28:59 -08001218 elif i == 3:
1219 main.log.info( "ONOS service stopped" )
1220 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -04001221 else:
kelvin8ec71442015-01-15 16:57:00 -08001222 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001223 return main.FALSE
andrewonlab2b30bd32014-10-09 16:48:55 -04001224 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001225 main.log.error( self.name + ": EOF exception found" )
1226 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -04001227 main.cleanup()
1228 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001229 except Exception:
1230 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001231 main.cleanup()
1232 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001233
kelvin-onlabd3b64892015-01-20 13:26:24 -08001234 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -08001235 """
andrewonlabc8d47972014-10-09 16:52:36 -04001236 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -08001237 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -04001238 if needed
kelvin8ec71442015-01-15 16:57:00 -08001239 """
andrewonlabc8d47972014-10-09 16:52:36 -04001240 try:
kelvin8ec71442015-01-15 16:57:00 -08001241 self.handle.sendline( "" )
Jeremy Songster14c13572016-04-21 17:34:03 -07001242 self.handle.expect( "\$", timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001243 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
Jeremy Songster14c13572016-04-21 17:34:03 -07001244 self.handle.expect( "\$", timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001245 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
kelvin8ec71442015-01-15 16:57:00 -08001246 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -04001247 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -07001248 except pexpect.TIMEOUT:
1249 main.log.exception( self.name + ": Timeout in onosUninstall" )
1250 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -04001251 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001252 main.log.error( self.name + ": EOF exception found" )
1253 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -04001254 main.cleanup()
1255 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001256 except Exception:
1257 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -04001258 main.cleanup()
1259 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -04001260
kelvin-onlabd3b64892015-01-20 13:26:24 -08001261 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001262 """
andrewonlabaedc8332014-12-04 12:43:03 -05001263 Issues the command 'onos-die <node-ip>'
1264 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -08001265 """
andrewonlabaedc8332014-12-04 12:43:03 -05001266 try:
kelvin8ec71442015-01-15 16:57:00 -08001267 self.handle.sendline( "" )
1268 self.handle.expect( "\$" )
Jeremyf0aecdb2016-03-30 13:19:57 -07001269 cmdStr = "onos-die " + str( nodeIp )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001270 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001271 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001272 "Killing\sONOS",
1273 "ONOS\sprocess\sis\snot\srunning",
Jeremy Songster14c13572016-04-21 17:34:03 -07001274 pexpect.TIMEOUT ], timeout=60 )
andrewonlabaedc8332014-12-04 12:43:03 -05001275 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001276 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001277 " was killed and stopped" )
Jon Hall53c5e662016-04-13 16:06:56 -07001278 self.handle.sendline( "" )
1279 self.handle.expect( "\$" )
andrewonlabaedc8332014-12-04 12:43:03 -05001280 return main.TRUE
1281 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001282 main.log.info( "ONOS process was not running" )
Jon Hall53c5e662016-04-13 16:06:56 -07001283 self.handle.sendline( "" )
1284 self.handle.expect( "\$" )
andrewonlabaedc8332014-12-04 12:43:03 -05001285 return main.FALSE
1286 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001287 main.log.error( self.name + ": EOF exception found" )
1288 main.log.error( self.name + ": " + self.handle.before )
andrewonlabaedc8332014-12-04 12:43:03 -05001289 main.cleanup()
1290 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001291 except Exception:
1292 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -05001293 main.cleanup()
1294 main.exit()
1295
kelvin-onlabd3b64892015-01-20 13:26:24 -08001296 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001297 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001298 Calls the command: 'onos-kill [<node-ip>]'
1299 "Remotely, and unceremoniously kills the ONOS instance running on
1300 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001301 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001302 try:
kelvin8ec71442015-01-15 16:57:00 -08001303 self.handle.sendline( "" )
1304 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001305 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001306 i = self.handle.expect( [
andrewonlabe8e56fd2014-10-09 17:12:44 -04001307 "\$",
1308 "No\sroute\sto\shost",
1309 "password:",
Jeremy Songster14c13572016-04-21 17:34:03 -07001310 pexpect.TIMEOUT ], timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -08001311
andrewonlabe8e56fd2014-10-09 17:12:44 -04001312 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001313 main.log.info(
1314 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001315 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001316 return main.TRUE
1317 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001318 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001319 return main.FALSE
1320 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001321 main.log.info(
1322 "Passwordless login for host: " +
1323 str( nodeIp ) +
1324 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001325 return main.FALSE
1326 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001327 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001328 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001329
andrewonlabe8e56fd2014-10-09 17:12:44 -04001330 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001331 main.log.error( self.name + ": EOF exception found" )
1332 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001333 main.cleanup()
1334 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001335 except Exception:
1336 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001337 main.cleanup()
1338 main.exit()
1339
kelvin-onlabd3b64892015-01-20 13:26:24 -08001340 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001341 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001342 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001343 a cleaner environment.
1344
andrewonlab19fbdca2014-11-14 12:55:59 -05001345 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001346 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001347 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001348 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001349 try:
kelvin8ec71442015-01-15 16:57:00 -08001350 self.handle.sendline( "" )
1351 self.handle.expect( "\$" )
1352 self.handle.sendline( "onos-remove-raft-logs" )
1353 # Sometimes this command hangs
1354 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1355 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001356 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001357 i = self.handle.expect( [ "\$", pexpect.TIMEOUT ],
1358 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001359 if i == 1:
1360 return main.FALSE
andrewonlab19fbdca2014-11-14 12:55:59 -05001361 return main.TRUE
andrewonlab19fbdca2014-11-14 12:55:59 -05001362 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001363 main.log.error( self.name + ": EOF exception found" )
1364 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001365 main.cleanup()
1366 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001367 except Exception:
1368 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001369 main.cleanup()
1370 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001371
kelvin-onlabd3b64892015-01-20 13:26:24 -08001372 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001373 """
1374 Calls the command 'onos-start-network [ <mininet-topo> ]
1375 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001376 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001377 cell."
andrewonlab94282092014-10-10 13:00:11 -04001378 * Specify mininet topology file name for mntopo
1379 * Topo files should be placed at:
1380 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001381
andrewonlab94282092014-10-10 13:00:11 -04001382 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001383 """
andrewonlab94282092014-10-10 13:00:11 -04001384 try:
1385 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001386 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001387 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001388
kelvin8ec71442015-01-15 16:57:00 -08001389 mntopo = str( mntopo )
1390 self.handle.sendline( "" )
1391 self.handle.expect( "\$" )
andrewonlab94282092014-10-10 13:00:11 -04001392
kelvin8ec71442015-01-15 16:57:00 -08001393 self.handle.sendline( "onos-start-network " + mntopo )
1394 self.handle.expect( "mininet>" )
1395 main.log.info( "Network started, entered mininet prompt" )
1396
1397 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001398
1399 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001400 main.log.error( self.name + ": EOF exception found" )
1401 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001402 main.cleanup()
1403 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001404 except Exception:
1405 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001406 main.cleanup()
1407 main.exit()
1408
Jeremy Songster14c13572016-04-21 17:34:03 -07001409 def isup( self, node="", timeout=240 ):
kelvin8ec71442015-01-15 16:57:00 -08001410 """
1411 Run's onos-wait-for-start which only returns once ONOS is at run
Cameron Franke9c94fb02015-01-21 10:20:20 -08001412 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001413
Jon Hall7993bfc2014-10-09 16:30:14 -04001414 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001415 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001416 try:
Jon Hall3b489db2015-10-05 14:38:37 -07001417 self.handle.sendline( "onos-wait-for-start " + node )
1418 self.handle.expect( "onos-wait-for-start" )
kelvin8ec71442015-01-15 16:57:00 -08001419 # NOTE: this timeout is arbitrary"
Cameron Franke9c94fb02015-01-21 10:20:20 -08001420 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001421 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001422 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001423 return main.TRUE
1424 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001425 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001426 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001427 main.log.error( "ONOS has not started yet" )
1428 self.handle.send( "\x03" ) # Control-C
1429 self.handle.expect( "\$" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001430 return main.FALSE
1431 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001432 main.log.error( self.name + ": EOF exception found" )
1433 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001434 main.cleanup()
1435 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001436 except Exception:
1437 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001438 main.cleanup()
1439 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001440
kelvin-onlabd3b64892015-01-20 13:26:24 -08001441 def pushTestIntentsShell(
1442 self,
1443 dpidSrc,
1444 dpidDst,
1445 numIntents,
1446 dirFile,
1447 onosIp,
1448 numMult="",
1449 appId="",
1450 report=True,
1451 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001452 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001453 Description:
kelvin8ec71442015-01-15 16:57:00 -08001454 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001455 better parallelize the results than the CLI
1456 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001457 * dpidSrc: specify source dpid
1458 * dpidDst: specify destination dpid
1459 * numIntents: specify number of intents to push
1460 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001461 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001462 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001463 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001464 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001465 """
1466 try:
1467 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001468 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001469 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001470 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001471 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001472 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001473
kelvin-onlabd3b64892015-01-20 13:26:24 -08001474 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1475 if not numMult:
1476 addIntents = addDpid + " " + str( numIntents )
1477 elif numMult:
1478 addIntents = addDpid + " " + str( numIntents ) + " " +\
1479 str( numMult )
1480 if appId:
1481 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001482 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001483 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001484
andrewonlabaedc8332014-12-04 12:43:03 -05001485 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001486 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001487 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001488 sendCmd = addApp + " &"
1489 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001490
kelvin-onlabd3b64892015-01-20 13:26:24 -08001491 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001492
1493 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001494 main.log.error( self.name + ": EOF exception found" )
1495 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001496 main.cleanup()
1497 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001498 except Exception:
1499 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001500 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001501 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001502
kelvin-onlabd3b64892015-01-20 13:26:24 -08001503 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001504 """
andrewonlab970399c2014-11-07 13:09:32 -05001505 Capture all packet activity and store in specified
1506 directory/file
1507
1508 Required:
1509 * interface: interface to capture
1510 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001511 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001512 try:
1513 self.handle.sendline( "" )
1514 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001515
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001516 self.handle.sendline( "tshark -i " + str( interface ) + " -t e -w " + str( dirFile ) + " &" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001517 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001518 self.handle.expect( "Capturing on" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001519 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001520 self.handle.expect( "\$" )
andrewonlab970399c2014-11-07 13:09:32 -05001521
Jon Hallfebb1c72015-03-05 13:30:09 -08001522 main.log.info( "Tshark started capturing files on " +
1523 str( interface ) + " and saving to directory: " +
1524 str( dirFile ) )
1525 except pexpect.EOF:
1526 main.log.error( self.name + ": EOF exception found" )
1527 main.log.error( self.name + ": " + self.handle.before )
1528 main.cleanup()
1529 main.exit()
1530 except Exception:
1531 main.log.exception( self.name + ": Uncaught exception!" )
1532 main.cleanup()
1533 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001534
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001535 def onosTopoCfg( self, onosIp, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001536 """
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001537 Description:
1538 Execute onos-topo-cfg command
1539 Required:
1540 onosIp - IP of the onos node you want to send the json to
1541 jsonFile - File path of the json file
1542 Return:
1543 Returns main.TRUE if the command is successfull; Returns
1544 main.FALSE if there was an error
kelvin8ec71442015-01-15 16:57:00 -08001545 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001546 try:
kelvin8ec71442015-01-15 16:57:00 -08001547 self.handle.sendline( "" )
1548 self.handle.expect( "\$" )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001549 cmd = "onos-topo-cfg "
1550 self.handle.sendline( cmd + str( onosIp ) + " " + jsonFile )
1551 handle = self.handle.before
1552 print handle
1553 if "Error" in handle:
1554 main.log.error( self.name + ": " + self.handle.before )
1555 return main.FALSE
1556 else:
1557 self.handle.expect( "\$" )
1558 return main.TRUE
1559
Jon Hallfebb1c72015-03-05 13:30:09 -08001560 except pexpect.EOF:
1561 main.log.error( self.name + ": EOF exception found" )
1562 main.log.error( self.name + ": " + self.handle.before )
1563 main.cleanup()
1564 main.exit()
1565 except Exception:
1566 main.log.exception( self.name + ": Uncaught exception!" )
1567 main.cleanup()
1568 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001569
jenkins1e99e7b2015-04-02 18:15:39 -07001570 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001571 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001572 Required:
kelvin8ec71442015-01-15 16:57:00 -08001573 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001574 * directory to store results
1575 Optional:
1576 * interface - default: eth0
Jon Hall4ba53f02015-07-29 13:07:41 -07001577 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001578 Description:
1579 Uses tshark command to grep specific group of packets
1580 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001581 The timestamp is hardcoded to be in epoch
1582 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001583 try:
1584 self.handle.sendline( "" )
1585 self.handle.expect( "\$" )
1586 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001587 if grepOptions:
1588 grepStr = "grep "+str(grepOptions)
1589 else:
1590 grepStr = "grep"
Jon Hall4ba53f02015-07-29 13:07:41 -07001591
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001592 cmd = (
1593 "sudo tshark -i " +
Jon Hallfebb1c72015-03-05 13:30:09 -08001594 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001595 " -t e | " +
1596 grepStr + " --line-buffered \"" +
Jon Hallfebb1c72015-03-05 13:30:09 -08001597 str(grep) +
1598 "\" >" +
1599 directory +
1600 " &" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001601 self.handle.sendline(cmd)
1602 main.log.info(cmd)
Jon Hallfebb1c72015-03-05 13:30:09 -08001603 self.handle.expect( "Capturing on" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001604 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001605 self.handle.expect( "\$" )
1606 except pexpect.EOF:
1607 main.log.error( self.name + ": EOF exception found" )
1608 main.log.error( self.name + ": " + self.handle.before )
1609 main.cleanup()
1610 main.exit()
1611 except Exception:
1612 main.log.exception( self.name + ": Uncaught exception!" )
1613 main.cleanup()
1614 main.exit()
1615
kelvin-onlabd3b64892015-01-20 13:26:24 -08001616 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001617 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001618 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001619 """
1620 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001621 try:
1622 self.execute( cmd="sudo rm /tmp/wireshark*" )
1623 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001624 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1625 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001626 self.handle.sendline( "" )
1627 main.log.info( "Tshark stopped" )
1628 except pexpect.EOF:
1629 main.log.error( self.name + ": EOF exception found" )
1630 main.log.error( self.name + ": " + self.handle.before )
1631 main.cleanup()
1632 main.exit()
1633 except Exception:
1634 main.log.exception( self.name + ": Uncaught exception!" )
1635 main.cleanup()
1636 main.exit()
1637
kelvin8ec71442015-01-15 16:57:00 -08001638 def ptpd( self, args ):
1639 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001640 Initiate ptp with user-specified args.
1641 Required:
1642 * args: specify string of args after command
1643 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001644 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001645 try:
kelvin8ec71442015-01-15 16:57:00 -08001646 self.handle.sendline( "sudo ptpd " + str( args ) )
1647 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001648 "Multiple",
1649 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001650 "\$" ] )
1651 self.handle.expect( "\$" )
andrewonlabba44bcf2014-10-16 16:54:41 -04001652
andrewonlab0c38a4a2014-10-28 18:35:35 -04001653 if i == 0:
1654 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001655 main.log.info( "ptpd returned an error: " +
1656 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001657 return handle
1658 elif i == 1:
1659 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001660 main.log.error( "ptpd returned an error: " +
1661 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001662 return handle
1663 else:
1664 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001665
andrewonlab0c38a4a2014-10-28 18:35:35 -04001666 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001667 main.log.error( self.name + ": EOF exception found" )
1668 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001669 main.cleanup()
1670 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001671 except Exception:
1672 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001673 main.cleanup()
1674 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001675
Pier50f0bc62016-09-07 17:53:40 -07001676 def dumpONOSCmd(self, ONOSIp, CMD, destDir, filename, options=""):
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001677 """
Pier50f0bc62016-09-07 17:53:40 -07001678 Dump Cmd to a desired directory.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001679 For debugging purposes, you may want to use
Pier50f0bc62016-09-07 17:53:40 -07001680 this function to capture Cmd at a given point in time.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001681 Localtime will be attached to the filename
1682
1683 Required:
1684 * ONOSIp: the IP of the target ONOS instance
Pier50f0bc62016-09-07 17:53:40 -07001685 * CMD: the command to dump;
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001686 * destDir: specify directory to copy to.
1687 ex ) /tmp/
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001688 * fileName: Name of the file
Pier50f0bc62016-09-07 17:53:40 -07001689 * options: Options for ONOS command
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001690 """
1691
1692 localtime = time.strftime( '%x %X' )
1693 localtime = localtime.replace( "/", "" )
1694 localtime = localtime.replace( " ", "_" )
1695 localtime = localtime.replace( ":", "" )
1696 if destDir[ -1: ] != "/":
1697 destDir += "/"
Pier50f0bc62016-09-07 17:53:40 -07001698 cmd=CMD + " " + options + " > " + str( destDir ) + str( filename ) + localtime
1699 return self.onosCli(ONOSIp, cmd)
Flavio Castrob7718952016-05-18 08:53:41 -07001700
kelvin-onlabd3b64892015-01-20 13:26:24 -08001701 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001702 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001703 """
1704 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001705 Current implementation of ONOS deletes its karaf
1706 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001707 you may want to use this function to capture
1708 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001709 Localtime will be attached to the filename
1710
1711 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001712 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001713 copy.
kelvin8ec71442015-01-15 16:57:00 -08001714 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001715 For copying multiple files, leave copyFileName
1716 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001717 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001718 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001719 ex ) /tmp/
1720 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001721 * copyFileName: If you want to rename the log
1722 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001723 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001724 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001725 try:
Flavio Castro09ab59d2016-05-25 17:01:35 -07001726 localtime = time.strftime( '%H %M' )
kelvin8ec71442015-01-15 16:57:00 -08001727 localtime = localtime.replace( "/", "" )
1728 localtime = localtime.replace( " ", "_" )
1729 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001730 if destDir[ -1: ] != "/":
1731 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001732
kelvin-onlabd3b64892015-01-20 13:26:24 -08001733 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001734 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1735 str( destDir ) + str( copyFileName ) +
1736 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001737 self.handle.expect( "cp" )
1738 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001739 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001740 self.handle.sendline( "cp " + str( logToCopy ) +
1741 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001742 self.handle.expect( "cp" )
1743 self.handle.expect( "\$" )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001744
kelvin8ec71442015-01-15 16:57:00 -08001745 return self.handle.before
1746
1747 except pexpect.EOF:
1748 main.log.error( "Copying files failed" )
1749 main.log.error( self.name + ": EOF exception found" )
1750 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001751 except Exception:
1752 main.log.exception( "Copying files failed" )
1753
Jon Hall16b72c42015-05-20 10:23:36 -07001754 def checkLogs( self, onosIp, restart=False):
kelvin8ec71442015-01-15 16:57:00 -08001755 """
Jon Hall94fd0472014-12-08 11:52:42 -08001756 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001757 If restart is True, use the old version of onos-check-logs which
1758 does not print the full stacktrace, but shows the entire log file,
1759 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001760 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001761 """
Jon Hall94fd0472014-12-08 11:52:42 -08001762 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001763 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001764 if restart:
1765 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001766 self.handle.sendline( cmd )
1767 self.handle.expect( cmd )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001768 self.handle.expect( "\$ " )
Jon Hall94fd0472014-12-08 11:52:42 -08001769 response = self.handle.before
1770 return response
1771 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001772 main.log.error( "Lost ssh connection" )
1773 main.log.error( self.name + ": EOF exception found" )
1774 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001775 except Exception:
1776 main.log.exception( self.name + ": Uncaught exception!" )
1777 main.cleanup()
1778 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001779
kelvin-onlabd3b64892015-01-20 13:26:24 -08001780 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001781 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001782 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001783 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001784 try:
kelvin8ec71442015-01-15 16:57:00 -08001785 self.handle.sendline( "" )
1786 self.handle.expect( "\$" )
1787 self.handle.sendline( "onos-service " + str( node ) +
1788 " status" )
1789 i = self.handle.expect( [
You Wangef1e6572016-03-08 12:53:18 -08001790 "start/running",
You Wang7bd83062016-03-01 11:50:00 -08001791 "Running ...",
You Wangef1e6572016-03-08 12:53:18 -08001792 "stop/waiting",
You Wang7bd83062016-03-01 11:50:00 -08001793 "Not Running ...",
kelvin8ec71442015-01-15 16:57:00 -08001794 pexpect.TIMEOUT ], timeout=120 )
YPZhangfebf7302016-05-24 16:45:56 -07001795 self.handle.sendline( "" )
1796 self.handle.expect( "\$" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001797
You Wangef1e6572016-03-08 12:53:18 -08001798 if i == 0 or i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001799 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001800 return main.TRUE
You Wangef1e6572016-03-08 12:53:18 -08001801 elif i == 2 or i == 3:
kelvin8ec71442015-01-15 16:57:00 -08001802 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001803 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001804 main.cleanup()
1805 main.exit()
1806 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001807 main.log.error( self.name + ": EOF exception found" )
1808 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001809 main.cleanup()
1810 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001811 except Exception:
1812 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001813 main.cleanup()
1814 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001815
Jon Hall63604932015-02-26 17:09:50 -08001816 def setIpTables( self, ip, port='', action='add', packet_type='',
1817 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001818 """
Jon Hall21270ac2015-02-16 17:59:55 -08001819 Description:
1820 add or remove iptables rule to DROP (default) packets from
1821 specific IP and PORT
1822 Usage:
1823 * specify action ('add' or 'remove')
1824 when removing, pass in the same argument as you would add. It will
1825 delete that specific rule.
1826 * specify the ip to block
1827 * specify the destination port to block (defaults to all ports)
1828 * optional packet type to block (default tcp)
1829 * optional iptables rule (default DROP)
1830 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001831 * States boolean toggles adding all supported tcp states to the
1832 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001833 Returns:
1834 main.TRUE on success or
1835 main.FALSE if given invalid input or
1836 main.ERROR if there is an error in response from iptables
1837 WARNING:
1838 * This function uses root privilege iptables command which may result
1839 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001840 """
Jon Hall21270ac2015-02-16 17:59:55 -08001841
1842 # NOTE*********
1843 # The strict checking methods of this driver function is intentional
1844 # to discourage any misuse or error of iptables, which can cause
1845 # severe network errors
1846 # *************
1847
1848 # NOTE: Sleep needed to give some time for rule to be added and
1849 # registered to the instance. If you are calling this function
1850 # multiple times this sleep will prevent any errors.
1851 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001852 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001853 try:
1854 # input validation
1855 action_type = action.lower()
1856 rule = rule.upper()
1857 direction = direction.upper()
1858 if action_type != 'add' and action_type != 'remove':
1859 main.log.error( "Invalid action type. Use 'add' or "
1860 "'remove' table rule" )
1861 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1862 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1863 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1864 "'ACCEPT' or 'LOG' only." )
1865 if direction != 'INPUT' and direction != 'OUTPUT':
1866 # NOTE currently only supports rules INPUT and OUPTUT
1867 main.log.error( "Invalid rule. Valid directions are"
1868 " 'OUTPUT' or 'INPUT'" )
1869 return main.FALSE
1870 return main.FALSE
1871 return main.FALSE
1872 if action_type == 'add':
1873 # -A is the 'append' action of iptables
1874 actionFlag = '-A'
1875 elif action_type == 'remove':
1876 # -D is the 'delete' rule of iptables
1877 actionFlag = '-D'
1878 self.handle.sendline( "" )
1879 self.handle.expect( "\$" )
1880 cmd = "sudo iptables " + actionFlag + " " +\
1881 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001882 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001883 # " -p " + str( packet_type ) +\
1884 if packet_type:
1885 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001886 if port:
1887 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001888 if states:
1889 cmd += " -m state --state="
1890 #FIXME- Allow user to configure which states to block
1891 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001892 cmd += " -j " + str( rule )
1893
1894 self.handle.sendline( cmd )
1895 self.handle.expect( "\$" )
1896 main.log.warn( self.handle.before )
1897
1898 info_string = "On " + str( self.name )
1899 info_string += " " + str( action_type )
1900 info_string += " iptable rule [ "
1901 info_string += " IP: " + str( ip )
1902 info_string += " Port: " + str( port )
1903 info_string += " Rule: " + str( rule )
1904 info_string += " Direction: " + str( direction ) + " ]"
1905 main.log.info( info_string )
1906 return main.TRUE
1907 except pexpect.TIMEOUT:
1908 main.log.exception( self.name + ": Timeout exception in "
1909 "setIpTables function" )
1910 return main.ERROR
1911 except pexpect.EOF:
1912 main.log.error( self.name + ": EOF exception found" )
1913 main.log.error( self.name + ": " + self.handle.before )
1914 main.cleanup()
1915 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001916 except Exception:
1917 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001918 main.cleanup()
1919 main.exit()
1920
Jon Hall0468b042015-02-19 19:08:21 -08001921 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001922 """
Jon Hall0468b042015-02-19 19:08:21 -08001923 This method is used by STS to check the status of the controller
1924 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001925 """
Jon Hall0468b042015-02-19 19:08:21 -08001926 import re
1927 try:
1928 self.handle.sendline( "" )
1929 self.handle.expect( "\$" )
1930 self.handle.sendline( "cd " + self.home )
1931 self.handle.expect( "\$" )
1932 self.handle.sendline( "service onos status" )
1933 self.handle.expect( "\$" )
1934 response = self.handle.before
1935 if re.search( "onos start/running", response ):
1936 # onos start/running, process 10457
1937 return 'RUNNING'
1938 # FIXME: Implement this case
1939 # elif re.search( pattern, response ):
1940 # return 'STARTING'
1941 elif re.search( "onos stop/", response ):
1942 # onos stop/waiting
1943 # FIXME handle this differently?: onos stop/pre-stop
1944 return 'STOPPED'
1945 # FIXME: Implement this case
1946 # elif re.search( pattern, response ):
1947 # return 'FROZEN'
1948 else:
1949 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001950 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001951 main.log.warn( response )
1952 return 'ERROR', "Unknown response: %s" % response
1953 except pexpect.TIMEOUT:
1954 main.log.exception( self.name + ": Timeout exception in "
1955 "setIpTables function" )
1956 return 'ERROR', "Pexpect Timeout"
1957 except pexpect.EOF:
1958 main.log.error( self.name + ": EOF exception found" )
1959 main.log.error( self.name + ": " + self.handle.before )
1960 main.cleanup()
1961 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001962 except Exception:
1963 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001964 main.cleanup()
1965 main.exit()
1966
andrew@onlab.us3b087132015-03-11 15:00:08 -07001967 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1968 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001969 Create/formats the LinkGraph.cfg file based on arguments
1970 -only creates a linear topology and connects islands
1971 -evenly distributes devices
andrew@onlab.us3b087132015-03-11 15:00:08 -07001972 -must be called by ONOSbench
1973
Jon Hall4ba53f02015-07-29 13:07:41 -07001974 ONOSIpList - list of all of the node IPs to be used
1975
1976 deviceCount - number of switches to be assigned
andrew@onlab.us3b087132015-03-11 15:00:08 -07001977 '''
Jon Hall6509dbf2016-06-21 17:01:17 -07001978 main.log.info("Creating link graph configuration file." )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001979 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
Jon Hall4ba53f02015-07-29 13:07:41 -07001980 tempFile = "/tmp/linkGraph.cfg"
andrew@onlab.us3b087132015-03-11 15:00:08 -07001981
1982 linkGraph = open(tempFile, 'w+')
1983 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1984 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1985 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001986
andrew@onlab.us3b087132015-03-11 15:00:08 -07001987 clusterCount = len(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07001988
1989 if type(deviceCount) is int or type(deviceCount) is str:
andrew@onlab.us3b087132015-03-11 15:00:08 -07001990 deviceCount = int(deviceCount)
1991 switchList = [0]*(clusterCount+1)
1992 baselineSwitchCount = deviceCount/clusterCount
Jon Hall4ba53f02015-07-29 13:07:41 -07001993
andrew@onlab.us3b087132015-03-11 15:00:08 -07001994 for node in range(1, clusterCount + 1):
1995 switchList[node] = baselineSwitchCount
1996
1997 for node in range(1, (deviceCount%clusterCount)+1):
1998 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001999
andrew@onlab.us3b087132015-03-11 15:00:08 -07002000 if type(deviceCount) is list:
2001 main.log.info("Using provided device distribution")
2002 switchList = [0]
2003 for i in deviceCount:
2004 switchList.append(int(i))
2005
2006 tempList = ['0']
2007 tempList.extend(ONOSIpList)
2008 ONOSIpList = tempList
2009
2010 myPort = 6
2011 lastSwitch = 0
2012 for node in range(1, clusterCount+1):
2013 if switchList[node] == 0:
2014 continue
2015
2016 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07002017
andrew@onlab.us3b087132015-03-11 15:00:08 -07002018 if node > 1:
2019 #connect to last device on previous node
Jon Hall4ba53f02015-07-29 13:07:41 -07002020 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
2021 linkGraph.write(line)
2022
2023 lastSwitch = 0
2024 for switch in range (0, switchList[node]-1):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002025 line = ""
2026 line = ("\t" + str(switch) + ":" + str(myPort))
2027 line += " -- "
2028 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
2029 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07002030 lastSwitch = switch+1
andrew@onlab.us3b087132015-03-11 15:00:08 -07002031 lastIp = ONOSIpList[node]
Jon Hall4ba53f02015-07-29 13:07:41 -07002032
andrew@onlab.us3b087132015-03-11 15:00:08 -07002033 #lastSwitch += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07002034 if node < (clusterCount):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002035 #connect to first device on the next node
Jon Hall4ba53f02015-07-29 13:07:41 -07002036 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
andrew@onlab.us3b087132015-03-11 15:00:08 -07002037 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07002038
andrew@onlab.us3b087132015-03-11 15:00:08 -07002039 linkGraph.write("}\n")
2040 linkGraph.close()
2041
2042 #SCP
Jon Hall4ba53f02015-07-29 13:07:41 -07002043 os.system( "scp " + tempFile + " " + self.user_name + "@" + benchIp + ":" + linkGraphPath)
andrew@onlab.us3b087132015-03-11 15:00:08 -07002044 main.log.info("linkGraph.cfg creation complete")
2045
cameron@onlab.us75900962015-03-30 13:22:49 -07002046 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
Jon Hall4ba53f02015-07-29 13:07:41 -07002047
andrew@onlab.us3b087132015-03-11 15:00:08 -07002048 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002049 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
2050 deviceCount = number of switches to distribute, or list of values to use as custom distribution
cameron@onlab.us75900962015-03-30 13:22:49 -07002051 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 -07002052 '''
2053
Jon Hall6509dbf2016-06-21 17:01:17 -07002054 main.log.info("Configuring Null Device Provider" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002055 clusterCount = len(ONOSIpList)
2056
Jon Hall4ba53f02015-07-29 13:07:41 -07002057 try:
2058
cameron@onlab.us75900962015-03-30 13:22:49 -07002059 if type(deviceCount) is int or type(deviceCount) is str:
Jon Hall6509dbf2016-06-21 17:01:17 -07002060 main.log.info("Creating device distribution")
cameron@onlab.us75900962015-03-30 13:22:49 -07002061 deviceCount = int(deviceCount)
2062 switchList = [0]*(clusterCount+1)
2063 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002064
cameron@onlab.us75900962015-03-30 13:22:49 -07002065 for node in range(1, clusterCount + 1):
2066 switchList[node] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002067
cameron@onlab.us75900962015-03-30 13:22:49 -07002068 for node in range(1, (deviceCount%clusterCount)+1):
2069 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07002070
2071 if type(deviceCount) is list:
2072 main.log.info("Using provided device distribution")
2073
2074 if len(deviceCount) == clusterCount:
cameron@onlab.us75900962015-03-30 13:22:49 -07002075 switchList = ['0']
2076 switchList.extend(deviceCount)
Jon Hall4ba53f02015-07-29 13:07:41 -07002077
2078 if len(deviceCount) == (clusterCount + 1):
2079 if deviceCount[0] == '0' or deviceCount[0] == 0:
cameron@onlab.us75900962015-03-30 13:22:49 -07002080 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002081
cameron@onlab.us75900962015-03-30 13:22:49 -07002082 assert len(switchList) == (clusterCount + 1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002083
cameron@onlab.us75900962015-03-30 13:22:49 -07002084 except AssertionError:
Jon Hall4ba53f02015-07-29 13:07:41 -07002085 main.log.error( "Bad device/Ip list match")
cameron@onlab.us75900962015-03-30 13:22:49 -07002086 except TypeError:
2087 main.log.exception( self.name + ": Object not as expected" )
2088 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07002089 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002090 main.log.exception( self.name + ": Uncaught exception!" )
2091 main.cleanup()
2092 main.exit()
2093
andrew@onlab.us3b087132015-03-11 15:00:08 -07002094
2095 ONOSIp = [0]
2096 ONOSIp.extend(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07002097
andrew@onlab.us3b087132015-03-11 15:00:08 -07002098 devicesString = "devConfigs = "
2099 for node in range(1, len(ONOSIp)):
2100 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
2101 if node < clusterCount:
2102 devicesString += (",")
Jon Hall4ba53f02015-07-29 13:07:41 -07002103
2104 try:
cameron@onlab.us75900962015-03-30 13:22:49 -07002105 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
2106 self.handle.expect(":~")
2107 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
2108 self.handle.expect(":~")
andrew@onlab.us3b087132015-03-11 15:00:08 -07002109
cameron@onlab.us75900962015-03-30 13:22:49 -07002110 for i in range(10):
2111 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
2112 self.handle.expect(":~")
2113 verification = self.handle.before
2114 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
2115 break
2116 else:
2117 time.sleep(1)
andrew@onlab.us3b087132015-03-11 15:00:08 -07002118
cameron@onlab.us2cc8bf12015-04-02 14:12:30 -07002119 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002120
cameron@onlab.us75900962015-03-30 13:22:49 -07002121 except AssertionError:
2122 main.log.error("Incorrect Config settings: " + verification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002123 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002124 main.log.exception( self.name + ": Uncaught exception!" )
2125 main.cleanup()
2126 main.exit()
2127
Jon Hall4ba53f02015-07-29 13:07:41 -07002128 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002129 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002130 fileName default is currently the same as the default on ONOS, specify alternate file if
cameron@onlab.us75900962015-03-30 13:22:49 -07002131 you want to use a different topology file than linkGraph.cfg
andrew@onlab.us3b087132015-03-11 15:00:08 -07002132 '''
2133
Jon Hall4ba53f02015-07-29 13:07:41 -07002134
2135 try:
2136 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
2137 self.handle.expect(":~")
cameron@onlab.us75900962015-03-30 13:22:49 -07002138 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2139 self.handle.expect(":~")
Jon Hall4ba53f02015-07-29 13:07:41 -07002140
2141 for i in range(10):
2142 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
cameron@onlab.us75900962015-03-30 13:22:49 -07002143 self.handle.expect(":~")
2144 verification = self.handle.before
Jon Hall4ba53f02015-07-29 13:07:41 -07002145 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002146 break
Jon Hall4ba53f02015-07-29 13:07:41 -07002147 else:
cameron@onlab.us75900962015-03-30 13:22:49 -07002148 time.sleep(1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002149
cameron@onlab.us75900962015-03-30 13:22:49 -07002150 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002151
cameron@onlab.us75900962015-03-30 13:22:49 -07002152 except pexpect.EOF:
2153 main.log.error( self.name + ": EOF exception found" )
2154 main.log.error( self.name + ": " + self.handle.before )
2155 main.cleanup()
2156 main.exit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002157 except AssertionError:
2158 main.log.info("Settings did not post to ONOS")
Jon Hall4ba53f02015-07-29 13:07:41 -07002159 main.log.error(varification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002160 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002161 main.log.exception( self.name + ": Uncaught exception!" )
2162 main.log.error(varification)
2163 main.cleanup()
2164 main.exit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002165
kelvin-onlaba4074292015-07-09 15:19:49 -07002166 def getOnosIps( self ):
2167 """
2168 Get all onos IPs stored in
2169 """
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002170
kelvin-onlaba4074292015-07-09 15:19:49 -07002171 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002172
kelvin-onlaba4074292015-07-09 15:19:49 -07002173 def logReport( self, nodeIp, searchTerms, outputMode="s" ):
Jon Hallb4242222016-01-25 17:07:04 -08002174 """
2175 Searches the latest ONOS log file for the given search terms and
2176 prints the total occurances of each term. Returns to combined total of
2177 all occurances.
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002178
Jon Hallb4242222016-01-25 17:07:04 -08002179 Arguments:
2180 * nodeIp - The ip of the ONOS node where the log is located
2181 * searchTerms - A string to grep for or a list of strings to grep
2182 for in the ONOS log. Will print out the number of
2183 occurances for each term.
2184 Optional Arguments:
2185 * outputMode - 's' or 'd'. If 'd' will print the last 5 lines
2186 containing each search term as well as the total
2187 number of occurances of each term. Defaults to 's',
2188 which prints the simple output of just the number
2189 of occurances for each term.
2190 """
2191 try:
2192 main.log.info( " Log Report for {} ".format( nodeIp ).center( 70, '=' ) )
2193 if type( searchTerms ) is str:
2194 searchTerms = [searchTerms]
2195 numTerms = len( searchTerms )
2196 outputMode = outputMode.lower()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002197
Jon Hallb4242222016-01-25 17:07:04 -08002198 totalHits = 0
2199 logLines = []
2200 for termIndex in range( numTerms ):
2201 term = searchTerms[termIndex]
2202 logLines.append( [term] )
2203 cmd = "onos-ssh " + nodeIp + " cat /opt/onos/log/karaf.log | grep " + term
2204 self.handle.sendline( cmd )
2205 self.handle.expect( ":~" )
2206 before = self.handle.before.splitlines()
2207 count = 0
2208 for line in before:
2209 if term in line and "grep" not in line:
2210 count += 1
2211 if before.index( line ) > ( len( before ) - 7 ):
2212 logLines[termIndex].append( line )
2213 main.log.info( "{}: {}".format( term, count ) )
2214 totalHits += count
2215 if termIndex == numTerms - 1:
2216 print "\n"
2217 if outputMode != "s":
2218 outputString = ""
2219 for term in logLines:
2220 outputString = term[0] + ": \n"
2221 for line in range( 1, len( term ) ):
2222 outputString += ( "\t" + term[line] + "\n" )
2223 if outputString != ( term[0] + ": \n" ):
2224 main.log.info( outputString )
2225 main.log.info( "=" * 70 )
2226 return totalHits
2227 except pexpect.EOF:
2228 main.log.error( self.name + ": EOF exception found" )
2229 main.log.error( self.name + ": " + self.handle.before )
2230 main.cleanup()
2231 main.exit()
2232 except pexpect.TIMEOUT:
2233 main.log.error( self.name + ": TIMEOUT exception found" )
2234 main.log.error( self.name + ": " + self.handle.before )
2235 main.cleanup()
2236 main.exit()
2237 except Exception:
2238 main.log.exception( self.name + ": Uncaught exception!" )
2239 main.cleanup()
2240 main.exit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002241
2242 def copyMininetFile( self, fileName, localPath, userName, ip,
2243 mnPath='~/mininet/custom/', timeout = 60 ):
2244 """
2245 Description:
2246 Copy mininet topology file from dependency folder in the test folder
2247 and paste it to the mininet machine's mininet/custom folder
2248 Required:
2249 fileName - Name of the topology file to copy
2250 localPath - File path of the mininet topology file
2251 userName - User name of the mininet machine to send the file to
2252 ip - Ip address of the mininet machine
2253 Optional:
2254 mnPath - of the mininet directory to send the file to
2255 Return:
2256 Return main.TRUE if successfully copied the file otherwise
2257 return main.FALSE
2258 """
2259
2260 try:
2261 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2262 str( ip ) + ":" + mnPath + fileName
2263
2264 self.handle.sendline( "" )
2265 self.handle.expect( "\$" )
2266
2267 main.log.info( self.name + ": Execute: " + cmd )
2268
2269 self.handle.sendline( cmd )
2270
2271 i = self.handle.expect( [ 'No such file',
2272 "100%",
2273 pexpect.TIMEOUT ] )
2274
2275 if i == 0:
2276 main.log.error( self.name + ": File " + fileName +
2277 " does not exist!" )
2278 return main.FALSE
2279
2280 if i == 1:
2281 main.log.info( self.name + ": File " + fileName +
2282 " has been copied!" )
2283 self.handle.sendline( "" )
2284 self.handle.expect( "\$" )
2285 return main.TRUE
2286
2287 except pexpect.EOF:
2288 main.log.error( self.name + ": EOF exception found" )
2289 main.log.error( self.name + ": " + self.handle.before )
2290 main.cleanup()
2291 main.exit()
2292 except pexpect.TIMEOUT:
2293 main.log.error( self.name + ": TIMEOUT exception found" )
2294 main.log.error( self.name + ": " + self.handle.before )
2295 main.cleanup()
2296 main.exit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002297
2298 def jvmSet(self, memory=8):
Jon Hall4ba53f02015-07-29 13:07:41 -07002299
cameron@onlab.us78b89652015-07-08 15:21:03 -07002300 import os
2301
2302 homeDir = os.path.expanduser('~')
2303 filename = "/onos/tools/package/bin/onos-service"
2304
2305 serviceConfig = open(homeDir + filename, 'w+')
2306 serviceConfig.write("#!/bin/bash\n ")
2307 serviceConfig.write("#------------------------------------- \n ")
2308 serviceConfig.write("# Starts ONOS Apache Karaf container\n ")
2309 serviceConfig.write("#------------------------------------- \n ")
2310 serviceConfig.write("#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n ")
2311 serviceConfig.write("""export JAVA_OPTS="${JAVA_OPTS:--Xms""" + str(memory) + "G -Xmx" + str(memory) + """G}" \n """)
2312 serviceConfig.write("[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n")
2313 serviceConfig.write("""${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """)
2314 serviceConfig.close()
2315
Jon Hall6c44c0b2016-04-20 15:21:00 -07002316 def createDBFile( self, testData ):
Jon Hall4ba53f02015-07-29 13:07:41 -07002317
cameron@onlab.us78b89652015-07-08 15:21:03 -07002318 filename = main.TEST + "DB"
2319 DBString = ""
Jon Hall4ba53f02015-07-29 13:07:41 -07002320
cameron@onlab.us78b89652015-07-08 15:21:03 -07002321 for item in testData:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002322 if type( item ) is string:
cameron@onlab.us78b89652015-07-08 15:21:03 -07002323 item = "'" + item + "'"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002324 if testData.index( item ) < len( testData - 1 ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002325 item += ","
Jon Hall6c44c0b2016-04-20 15:21:00 -07002326 DBString += str( item )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002327
Jon Hall6c44c0b2016-04-20 15:21:00 -07002328 DBFile = open( filename, "a" )
2329 DBFile.write( DBString )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002330 DBFile.close()
2331
Jon Hall6c44c0b2016-04-20 15:21:00 -07002332 def verifySummary( self, ONOSIp, *deviceCount ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002333
Jon Hall6c44c0b2016-04-20 15:21:00 -07002334 self.handle.sendline( "onos " + ONOSIp + " summary" )
2335 self.handle.expect( ":~" )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002336
2337 summaryStr = self.handle.before
2338 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2339
2340 #passed = "SCC(s)=1" in summaryStr
2341 #if deviceCount:
2342 # passed = "devices=" + str(deviceCount) + "," not in summaryStr
2343
GlennRC772363b2015-08-25 13:05:57 -07002344 passed = False
cameron@onlab.us78b89652015-07-08 15:21:03 -07002345 if "SCC(s)=1," in summaryStr:
2346 passed = True
Jon Hall6c44c0b2016-04-20 15:21:00 -07002347 print "Summary is verifed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002348 else:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002349 print "Summary failed"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002350
2351 if deviceCount:
2352 print" ============================="
Jon Hall6c44c0b2016-04-20 15:21:00 -07002353 checkStr = "devices=" + str( deviceCount[0] ) + ","
cameron@onlab.us78b89652015-07-08 15:21:03 -07002354 print "Checkstr: " + checkStr
2355 if checkStr not in summaryStr:
2356 passed = False
Jon Hall6c44c0b2016-04-20 15:21:00 -07002357 print "Device count failed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002358 else:
2359 print "device count verified"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002360
2361 return passed
Jon Hall6c44c0b2016-04-20 15:21:00 -07002362
Jon Hall8f6d4622016-05-23 15:27:18 -07002363 def getIpAddr( self, iface=None ):
Jon Hall6c44c0b2016-04-20 15:21:00 -07002364 """
2365 Update self.ip_address with numerical ip address. If multiple IP's are
2366 located on the device, will attempt to use self.nicAddr to choose the
2367 right one. Defaults to 127.0.0.1 if no other address is found or cannot
2368 determine the correct address.
2369
2370 ONLY WORKS WITH IPV4 ADDRESSES
2371 """
2372 try:
Jon Hall8f6d4622016-05-23 15:27:18 -07002373 LOCALHOST = "127.0.0.1"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002374 ipPat = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
2375 pattern = re.compile( ipPat )
2376 match = re.search( pattern, self.ip_address )
2377 if self.nicAddr:
2378 nicPat = self.nicAddr.replace( ".", "\." ).replace( "\*", r"\d{1,3}" )
2379 nicPat = re.compile( nicPat )
2380 else:
2381 nicPat = None
2382 # IF self.ip_address is an ip address and matches
2383 # self.nicAddr: return self.ip_address
2384 if match:
2385 curIp = match.group(0)
2386 if nicPat:
2387 nicMatch = re.search( nicPat, curIp )
2388 if nicMatch:
2389 return self.ip_address
Jon Hall8f6d4622016-05-23 15:27:18 -07002390 # ELSE: IF iface, return ip of interface
2391 cmd = "ifconfig"
2392 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2393 if iface:
2394 cmd += " " + str( iface )
2395 raw = subprocess.check_output( cmd.split() )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002396 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2397 ips = re.findall( ifPat, raw )
Jon Hall8f6d4622016-05-23 15:27:18 -07002398 if iface:
2399 if ips:
2400 ip = ips[0]
2401 self.ip_address = ip
2402 return ip
2403 else:
2404 main.log.error( "Error finding ip, ifconfig output:".format( raw ) )
2405 # ELSE: attempt to get address matching nicPat.
Jon Hall6c44c0b2016-04-20 15:21:00 -07002406 if nicPat:
2407 for ip in ips:
2408 curMatch = re.search( nicPat, ip )
2409 if curMatch:
2410 self.ip_address = ip
2411 return ip
Jon Hall8f6d4622016-05-23 15:27:18 -07002412 else: # If only one non-localhost ip, return that
2413 tmpList = [ ip for ip in ips if ip is not LOCALHOST ]
Jon Hall6c44c0b2016-04-20 15:21:00 -07002414 if len(tmpList) == 1:
2415 curIp = tmpList[0]
2416 self.ip_address = curIp
2417 return curIp
Jon Hall8f6d4622016-05-23 15:27:18 -07002418 # Either no non-localhost IPs, or more than 1
Jon Hallebccd352016-05-20 15:17:17 -07002419 main.log.warn( "getIpAddr failed to find a public IP address" )
Jon Hall8f6d4622016-05-23 15:27:18 -07002420 return LOCALHOST
Jon Halle1790952016-05-23 15:51:46 -07002421 except subprocess.CalledProcessError:
Jon Hall8f6d4622016-05-23 15:27:18 -07002422 main.log.exception( "Error executing ifconfig" )
2423 except IndexError:
2424 main.log.exception( "Error getting IP Address" )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002425 except Exception:
2426 main.log.exception( "Uncaught exception" )
suibin zhang116647a2016-05-06 16:30:09 -07002427
2428 def startBasicONOS(self, nodeList, opSleep = 60, onosStartupSleep = 60):
2429
2430 '''
2431 Start onos cluster with defined nodes, but only with drivers app
2432
2433 '''
2434 import time
2435
2436 self.createCellFile( self.ip_address,
2437 "temp",
2438 self.ip_address,
2439 "drivers",
2440 nodeList )
2441
2442 main.log.info( self.name + ": Apply cell to environment" )
2443 cellResult = self.setCell( "temp" )
2444 verifyResult = self.verifyCell()
2445
2446 main.log.info( self.name + ": Creating ONOS package" )
2447 packageResult = self.onosPackage( opTimeout=opSleep )
2448
2449 main.log.info( self.name + ": Installing ONOS package" )
2450 for nd in nodeList:
2451 self.onosInstall( node=nd )
2452
2453 main.log.info( self.name + ": Starting ONOS service" )
2454 time.sleep( onosStartupSleep )
2455
2456 onosStatus = True
2457 for nd in nodeList:
2458 onosStatus = onosStatus & self.isup( node = nd )
2459 #print "onosStatus is: " + str( onosStatus )
2460
2461 return main.TRUE if onosStatus else main.FALSE
2462
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002463 def onosNetCfg( self, controllerIps, path, fileName ):
2464 """
2465 Push a specified json file to ONOS through the onos-netcfg service
2466
2467 Required:
2468 controllerIps - the Ips of the ONOS nodes in the cluster
2469 path - the location of the file to be sent
2470 fileName - name of the json file to be sent
2471
2472 Returns main.TRUE on successfully sending json file, and main.FALSE if
2473 there is an error.
2474 """
2475 try:
2476 cmd = "onos-netcfg {0} {1}{2}.json".format( controllerIps, path, fileName )
2477 main.log.info( "Sending: " + cmd )
2478 main.ONOSbench.handle.sendline( cmd )
2479 handle = self.handle.before
2480 print handle
2481 if "Error" in handle:
2482 main.log.error( self.name + ": " + self.handle.before )
2483 return main.FALSE
2484 else:
2485 self.handle.expect( "\$" )
2486 return main.TRUE
2487 except pexpect.EOF:
2488 main.log.error( self.name + ": EOF exception found" )
2489 main.log.error( self.name + ": " + self.handle.before )
2490 main.cleanup()
2491 main.exit()
2492 except Exception:
2493 main.log.exception( self.name + ": Uncaught exception!" )
2494 main.cleanup()
Jeremy Songsterae01bba2016-07-11 15:39:17 -07002495 main.exit()