blob: dadc78d099ab94f06baea665e2878dbfadb04f15 [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
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070033
kelvin8ec71442015-01-15 16:57:00 -080034class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040035
kelvin8ec71442015-01-15 16:57:00 -080036 def __init__( self ):
37 """
38 Initialize client
39 """
Jon Hallefbd9792015-03-05 16:11:36 -080040 self.name = None
41 self.home = None
42 self.handle = None
Jon Hall6c44c0b2016-04-20 15:21:00 -070043 self.nicAddr = None
Devin Limdc78e202017-06-09 18:30:07 -070044 super( OnosDriver, self ).__init__()
kelvin8ec71442015-01-15 16:57:00 -080045
46 def connect( self, **connectargs ):
47 """
Jon Hall05b2b432014-10-08 19:53:25 -040048 Creates ssh handle for ONOS "bench".
kelvin-onlaba4074292015-07-09 15:19:49 -070049 NOTE:
50 The ip_address would come from the topo file using the host tag, the
51 value can be an environment variable as well as a "localhost" to get
52 the ip address needed to ssh to the "bench"
kelvin8ec71442015-01-15 16:57:00 -080053 """
Jon Hall05b2b432014-10-08 19:53:25 -040054 try:
Devin Limdc78e202017-06-09 18:30:07 -070055
Jon Hall05b2b432014-10-08 19:53:25 -040056 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080057 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us3b087132015-03-11 15:00:08 -070058 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040059 for key in self.options:
60 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080061 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040062 break
Jon Hall274b6642015-02-17 11:57:17 -080063 if self.home is None or self.home == "":
Jon Halle94919c2015-03-23 11:42:57 -070064 self.home = "~/onos"
Jon Hall21270ac2015-02-16 17:59:55 -080065
kelvin8ec71442015-01-15 16:57:00 -080066 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070067
kelvin-onlabc2b79102015-07-14 11:41:20 -070068 # The 'nodes' tag is optional and it is not required in .topo file
kelvin-onlaba4074292015-07-09 15:19:49 -070069 for key in self.options:
70 if key == "nodes":
kelvin-onlabc2b79102015-07-14 11:41:20 -070071 # Maximum number of ONOS nodes to run, if there is any
kelvin-onlaba4074292015-07-09 15:19:49 -070072 self.maxNodes = int( self.options[ 'nodes' ] )
73 break
74 self.maxNodes = None
75
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070076 if self.maxNodes is None or self.maxNodes == "":
kelvin-onlabc2b79102015-07-14 11:41:20 -070077 self.maxNodes = 100
kelvin-onlaba4074292015-07-09 15:19:49 -070078
kelvin-onlabc2b79102015-07-14 11:41:20 -070079 # Grabs all OC environment variables based on max number of nodes
kelvin-onlaba4074292015-07-09 15:19:49 -070080 self.onosIps = {} # Dictionary of all possible ONOS ip
81
82 try:
83 if self.maxNodes:
kelvin-onlaba4074292015-07-09 15:19:49 -070084 for i in range( self.maxNodes ):
85 envString = "OC" + str( i + 1 )
kelvin-onlabc2b79102015-07-14 11:41:20 -070086 # If there is no more OC# then break the loop
87 if os.getenv( envString ):
88 self.onosIps[ envString ] = os.getenv( envString )
89 else:
90 self.maxNodes = len( self.onosIps )
91 main.log.info( self.name +
92 ": Created cluster data with " +
93 str( self.maxNodes ) +
94 " maximum number" +
95 " of nodes" )
96 break
kelvin-onlaba4074292015-07-09 15:19:49 -070097
98 if not self.onosIps:
99 main.log.info( "Could not read any environment variable"
100 + " please load a cell file with all" +
101 " onos IP" )
Jon Hall5cf14d52015-07-16 12:15:19 -0700102 self.maxNodes = None
kelvin-onlaba4074292015-07-09 15:19:49 -0700103 else:
104 main.log.info( self.name + ": Found " +
105 str( self.onosIps.values() ) +
106 " ONOS IPs" )
kelvin-onlaba4074292015-07-09 15:19:49 -0700107 except KeyError:
108 main.log.info( "Invalid environment variable" )
109 except Exception as inst:
110 main.log.error( "Uncaught exception: " + str( inst ) )
111
112 try:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700113 if os.getenv( str( self.ip_address ) ) is not None:
kelvin-onlaba4074292015-07-09 15:19:49 -0700114 self.ip_address = os.getenv( str( self.ip_address ) )
115 else:
116 main.log.info( self.name +
117 ": Trying to connect to " +
118 self.ip_address )
kelvin-onlaba4074292015-07-09 15:19:49 -0700119 except KeyError:
120 main.log.info( "Invalid host name," +
121 " connecting to local host instead" )
122 self.ip_address = 'localhost'
123 except Exception as inst:
124 main.log.error( "Uncaught exception: " + str( inst ) )
125
kelvin8ec71442015-01-15 16:57:00 -0800126 self.handle = super( OnosDriver, self ).connect(
127 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -0800128 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800129 port=self.port,
130 pwd=self.pwd,
131 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -0400132
Jon Hall05b2b432014-10-08 19:53:25 -0400133 if self.handle:
Jon Hall0fc0d452015-07-14 09:49:58 -0700134 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700135 self.handle.expect( self.prompt )
Jon Hall05b2b432014-10-08 19:53:25 -0400136 return self.handle
kelvin8ec71442015-01-15 16:57:00 -0800137 else:
Jon Hall0fc0d452015-07-14 09:49:58 -0700138 main.log.info( "Failed to create ONOS handle" )
Jon Hall05b2b432014-10-08 19:53:25 -0400139 return main.FALSE
140 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800141 main.log.error( self.name + ": EOF exception found" )
142 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700143 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800144 except Exception:
145 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700146 main.cleanAndExit()
Jon Hall05b2b432014-10-08 19:53:25 -0400147
kelvin8ec71442015-01-15 16:57:00 -0800148 def disconnect( self ):
149 """
Jon Hall05b2b432014-10-08 19:53:25 -0400150 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800151 """
Jon Halld61331b2015-02-17 16:35:47 -0800152 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -0400153 try:
Jon Hall61282e32015-03-19 11:34:11 -0700154 if self.handle:
155 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700156 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700157 self.handle.sendline( "exit" )
158 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -0400159 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800160 main.log.error( self.name + ": EOF exception found" )
161 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700162 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700163 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700164 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800165 except Exception:
166 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -0400167 response = main.FALSE
168 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400169
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400170 def getEpochMs( self ):
171 """
172 Returns milliseconds since epoch
Jon Hall4ba53f02015-07-29 13:07:41 -0700173
174 When checking multiple nodes in a for loop,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700175 around a hundred milliseconds of difference ( ascending ) is
Jon Hall4ba53f02015-07-29 13:07:41 -0700176 generally acceptable due to calltime of the function.
177 Few seconds, however, is not and it means clocks
178 are off sync.
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400179 """
180 try:
181 self.handle.sendline( 'date +%s.%N' )
182 self.handle.expect( 'date \+\%s\.\%N' )
Devin Limdc78e202017-06-09 18:30:07 -0700183 self.handle.expect( self.prompt )
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400184 epochMs = self.handle.before
185 return epochMs
186 except Exception:
187 main.log.exception( 'Uncaught exception getting epoch time' )
Devin Lim44075962017-08-11 10:56:37 -0700188 main.cleanAndExit()
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400189
Jon Hall6c44c0b2016-04-20 15:21:00 -0700190 def onosPackage( self, opTimeout=180 ):
kelvin8ec71442015-01-15 16:57:00 -0800191 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400192 Produce a self-contained tar.gz file that can be deployed
Jon Hall64af8502015-12-15 10:09:33 -0800193 and executed on any platform with Java 8 JRE.
kelvin8ec71442015-01-15 16:57:00 -0800194 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400195 try:
Jon Hall64af8502015-12-15 10:09:33 -0800196 ret = main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800197 self.handle.sendline( "onos-package" )
198 self.handle.expect( "onos-package" )
Jon Hall96451092016-05-04 09:42:30 -0700199 while True:
200 i = self.handle.expect( [ "Downloading",
201 "Unknown options",
202 "No such file or directory",
203 "tar.gz",
Devin Limc20e79a2017-06-07 10:29:57 -0700204 self.prompt ],
Jon Hall96451092016-05-04 09:42:30 -0700205 opTimeout )
206 handle = str( self.handle.before + self.handle.after )
207 if i == 0:
208 # Give more time to download the file
209 continue # expect again
210 elif i == 1:
211 # Incorrect usage
212 main.log.error( "onos-package does not recognize the given options" )
213 ret = main.FALSE
214 continue # expect again
215 elif i == 2:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700216 # File( s ) not found
Jon Hall96451092016-05-04 09:42:30 -0700217 main.log.error( "onos-package could not find a file or directory" )
218 ret = main.FALSE
219 continue # expect again
220 elif i == 3:
221 # tar.gz
222 continue # expect again
223 elif i == 4:
224 # Prompt returned
225 break
Jon Hallc6793552016-01-19 14:18:37 -0800226 main.log.info( "onos-package command returned: " + handle )
kelvin8ec71442015-01-15 16:57:00 -0800227 # As long as the sendline does not time out,
228 # return true. However, be careful to interpret
229 # the results of the onos-package command return
Jon Hall64af8502015-12-15 10:09:33 -0800230 return ret
231 except pexpect.TIMEOUT:
232 main.log.exception( self.name + ": TIMEOUT exception found in onosPackage" )
233 main.log.error( self.name + ": " + self.handle.before )
234 return main.FALSE
andrewonlab7735d852014-10-09 13:02:47 -0400235 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800236 main.log.error( self.name + ": EOF exception found" )
237 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700238 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800239 except Exception:
240 main.log.exception( "Failed to package ONOS" )
Devin Lim44075962017-08-11 10:56:37 -0700241 main.cleanAndExit()
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400242
kelvin-onlabd3b64892015-01-20 13:26:24 -0800243 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800244 """
andrewonlab8790abb2014-11-06 13:51:54 -0500245 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800246 """
andrewonlab8790abb2014-11-06 13:51:54 -0500247 try:
kelvin8ec71442015-01-15 16:57:00 -0800248 self.handle.sendline( "onos-build" )
249 self.handle.expect( "onos-build" )
250 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800251 "BUILD SUCCESS",
252 "ERROR",
253 "BUILD FAILED" ],
254 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800255 handle = str( self.handle.before )
Devin Limc20e79a2017-06-07 10:29:57 -0700256 self.handle.expect( self.prompt )
andrewonlab8790abb2014-11-06 13:51:54 -0500257
kelvin8ec71442015-01-15 16:57:00 -0800258 main.log.info( "onos-build command returned: " +
259 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500260
261 if i == 0:
262 return main.TRUE
263 else:
264 return handle
265
266 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800267 main.log.error( self.name + ": EOF exception found" )
268 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800269 except Exception:
270 main.log.exception( "Failed to build ONOS" )
Devin Lim44075962017-08-11 10:56:37 -0700271 main.cleanAndExit()
andrewonlab8790abb2014-11-06 13:51:54 -0500272
shahshreya9f531fe2015-06-10 12:03:51 -0700273 def cleanInstall( self, skipTest=False, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800274 """
275 Runs mvn clean install in the root of the ONOS directory.
276 This will clean all ONOS artifacts then compile each module
shahshreya9f531fe2015-06-10 12:03:51 -0700277 Optional:
278 skipTest - Does "-DskipTests -Dcheckstyle.skip -U -T 1C" which
279 skip the test. This will make the building faster.
280 Disregarding the credibility of the build
kelvin8ec71442015-01-15 16:57:00 -0800281 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400282 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800283 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400284 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800285 main.log.info( "Running 'mvn clean install' on " +
286 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800287 ". This may take some time." )
288 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700289 self.handle.expect( self.prompt )
Jon Hallea7818b2014-10-09 14:30:59 -0400290
kelvin8ec71442015-01-15 16:57:00 -0800291 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700292 self.handle.expect( self.prompt )
shahshreya9f531fe2015-06-10 12:03:51 -0700293
294 if not skipTest:
295 self.handle.sendline( "mvn clean install" )
296 self.handle.expect( "mvn clean install" )
297 else:
298 self.handle.sendline( "mvn clean install -DskipTests" +
299 " -Dcheckstyle.skip -U -T 1C" )
300 self.handle.expect( "mvn clean install -DskipTests" +
301 " -Dcheckstyle.skip -U -T 1C" )
kelvin8ec71442015-01-15 16:57:00 -0800302 while True:
303 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800304 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800305 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400306 'BUILD\sFAILURE',
307 'BUILD\sSUCCESS',
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700308 'onos' + self.prompt, # TODO: fix this to be more generic?
Devin Limdc78e202017-06-09 18:30:07 -0700309 'ONOS' + self.prompt,
pingping-lin57a56ce2015-05-20 16:43:48 -0700310 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400311 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800312 main.log.error( self.name + ":There is insufficient memory \
313 for the Java Runtime Environment to continue." )
314 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700315
316 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400317 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800318 main.log.error( self.name + ": Build failure!" )
319 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700320
321 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400322 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800323 main.log.info( self.name + ": Build success!" )
Jon Halle94919c2015-03-23 11:42:57 -0700324 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800325 main.log.info( self.name + ": Build complete" )
326 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400327 for line in self.handle.before.splitlines():
328 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800329 main.log.info( line )
330 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700331 self.handle.expect( self.prompt, timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400332 return main.TRUE
Jon Halle94919c2015-03-23 11:42:57 -0700333 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800334 main.log.error(
335 self.name +
336 ": mvn clean install TIMEOUT!" )
337 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700338
339 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400340 else:
Jon Hall274b6642015-02-17 11:57:17 -0800341 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800342 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800343 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700344
345 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400346 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800347 main.log.error( self.name + ": EOF exception found" )
348 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700349 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800350 except Exception:
351 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700352 main.cleanAndExit()
Jon Hallacabffd2014-10-09 12:36:53 -0400353
Jon Hall3576f572016-08-23 10:01:07 -0700354 def buckBuild( self, timeout=180 ):
355 """
356 Build onos using buck.
357 """
358 try:
359 ret = main.TRUE
360 self.handle.sendline( "buck build onos" )
361 self.handle.expect( "buck build onos" )
362 output = ""
363 while True:
364 i = self.handle.expect( [ "This does not appear to be the root of a Buck project.",
365 "\n",
366 "BUILD FAILED",
Devin Limc20e79a2017-06-07 10:29:57 -0700367 self.prompt ],
Jon Hall3576f572016-08-23 10:01:07 -0700368 timeout=timeout )
369 output += str( self.handle.before + self.handle.after )
370 if i == 0:
371 main.log.error( "Wrong location" )
372 ret = main.FALSE
373 elif i == 1:
374 # end of a line, buck is still printing output
375 pass
376 elif i == 2:
377 # Build failed
378 main.log.error( "Build failed" )
379 ret = main.FALSE
380 elif i == 3:
381 # Prompt returned
382 break
383 main.log.debug( output )
384 return ret
385 except pexpect.TIMEOUT:
386 main.log.exception( self.name + ": TIMEOUT exception found" )
387 main.log.error( self.name + ": " + self.handle.before )
388 return main.FALSE
389 except pexpect.EOF:
390 main.log.error( self.name + ": EOF exception found" )
391 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700392 main.cleanAndExit()
Jon Hall3576f572016-08-23 10:01:07 -0700393 except Exception:
394 main.log.exception( "Failed to build and package ONOS" )
Devin Lim44075962017-08-11 10:56:37 -0700395 main.cleanAndExit()
Jon Hall3576f572016-08-23 10:01:07 -0700396
Jon Hall61282e32015-03-19 11:34:11 -0700397 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800398 """
Jon Hallacabffd2014-10-09 12:36:53 -0400399 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800400
Jon Hall61282e32015-03-19 11:34:11 -0700401 If the fastForward boolean is set to true, only git pulls that can
402 be fast forwarded will be performed. IE if you have not local commits
403 in your branch.
404
Jon Hallacabffd2014-10-09 12:36:53 -0400405 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800406 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400407 for the purpose of pulling from other nodes if necessary.
408
Jon Hall47a93fb2015-01-06 16:46:06 -0800409 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400410 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800411 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400412 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400413
kelvin8ec71442015-01-15 16:57:00 -0800414 """
Jon Hallacabffd2014-10-09 12:36:53 -0400415 try:
kelvin8ec71442015-01-15 16:57:00 -0800416 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700417 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700418 cmd = "git pull"
419 if comp1 != "":
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700420 cmd += ' ' + comp1
Jon Hall61282e32015-03-19 11:34:11 -0700421 if fastForward:
422 cmd += ' ' + " --ff-only"
423 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800424 i = self.handle.expect(
425 [
426 'fatal',
427 'Username\sfor\s(.*):\s',
428 '\sfile(s*) changed,\s',
429 'Already up-to-date',
430 'Aborting',
431 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800432 'You asked me to pull without telling me which branch you',
433 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700434 'Please enter a commit message to explain why this merge',
435 'Found a swap file by the name',
436 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800437 pexpect.TIMEOUT ],
438 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800439 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700440 main.log.error( self.name + ": Git pull had some issue" )
441 output = self.handle.after
Devin Limdc78e202017-06-09 18:30:07 -0700442 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700443 output += self.handle.before
444 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400445 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800446 elif i == 1:
447 main.log.error(
448 self.name +
449 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400450 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800451 elif i == 2:
452 main.log.info(
453 self.name +
454 ": Git Pull - pulling repository now" )
Devin Limc20e79a2017-06-07 10:29:57 -0700455 self.handle.expect( self.prompt, 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800456 # So that only when git pull is done, we do mvn clean compile
457 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800458 elif i == 3:
459 main.log.info( self.name + ": Git Pull - Already up to date" )
Devin Limc20e79a2017-06-07 10:29:57 -0700460 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800461 elif i == 4:
462 main.log.info(
463 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800464 ": Git Pull - Aborting..." +
465 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400466 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800467 elif i == 5:
468 main.log.info(
469 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800470 ": Git Pull - You are not currently " +
471 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400472 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800473 elif i == 6:
474 main.log.info(
475 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800476 ": Git Pull - You have not configured an upstream " +
477 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400478 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800479 elif i == 7:
480 main.log.info(
481 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800482 ": Git Pull - Pull is not possible because " +
483 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400484 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800485 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700486 # NOTE: abandoning test since we can't reliably handle this
487 # there could be different default text editors and we
488 # also don't know if we actually want to make the commit
489 main.log.error( "Git pull resulted in a merge commit message" +
490 ". Exiting test!" )
Devin Lim44075962017-08-11 10:56:37 -0700491
492 main.cleanAndExit()
Jon Hall61282e32015-03-19 11:34:11 -0700493 elif i == 9: # Merge commit message but swap file exists
494 main.log.error( "Git pull resulted in a merge commit message" +
495 " but a swap file exists." )
496 try:
497 self.handle.send( 'A' ) # Abort
Devin Limc20e79a2017-06-07 10:29:57 -0700498 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700499 return main.ERROR
500 except Exception:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700501 main.log.exception( "Couldn't exit editor prompt!" )
Devin Lim44075962017-08-11 10:56:37 -0700502
503 main.cleanAndExit()
Jon Hall61282e32015-03-19 11:34:11 -0700504 elif i == 10: # In the middle of a merge commit
505 main.log.error( "Git branch is in the middle of a merge. " )
506 main.log.warn( self.handle.before + self.handle.after )
507 return main.ERROR
508 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800509 main.log.error( self.name + ": Git Pull - TIMEOUT" )
510 main.log.error(
511 self.name + " Response was: " + str(
512 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400513 return main.ERROR
514 else:
kelvin8ec71442015-01-15 16:57:00 -0800515 main.log.error(
516 self.name +
517 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400518 return main.ERROR
519 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800520 main.log.error( self.name + ": EOF exception found" )
521 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700522 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800523 except Exception:
524 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700525 main.cleanAndExit()
Jon Hallacabffd2014-10-09 12:36:53 -0400526
kelvin-onlabd3b64892015-01-20 13:26:24 -0800527 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800528 """
Jon Hallacabffd2014-10-09 12:36:53 -0400529 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800530
Jon Hallacabffd2014-10-09 12:36:53 -0400531 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800532 If used as gitCheckout( "branch" ) it will do git checkout
533 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400534
535 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800536 branch of the ONOS repository. If it has any problems, it will return
537 main.ERROR.
538 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400539 successful then the function will return main.TRUE.
540
kelvin8ec71442015-01-15 16:57:00 -0800541 """
Jon Hallacabffd2014-10-09 12:36:53 -0400542 try:
kelvin8ec71442015-01-15 16:57:00 -0800543 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700544 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800545 main.log.info( self.name +
546 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800547 cmd = "git checkout " + branch
548 self.handle.sendline( cmd )
549 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800550 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800551 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700552 'Username for (.*): ',
553 'Already on \'',
Jon Hall7a8354f2015-06-10 15:37:00 -0700554 'Switched to (a new )?branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800555 pexpect.TIMEOUT,
556 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800557 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800558 'error: you need to resolve your current index first',
559 "You are in 'detached HEAD' state.",
560 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800561 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800562 if i == 0:
563 main.log.error(
564 self.name +
565 ": Git checkout had some issue..." )
566 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400567 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800568 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800569 main.log.error(
570 self.name +
571 ": Git checkout asking for username." +
572 " Please configure your local git repository to be able " +
573 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800574 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400575 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800576 elif i == 2:
577 main.log.info(
578 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800579 ": Git Checkout %s : Already on this branch" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700580 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800581 # main.log.info( "DEBUG: after checkout cmd = "+
582 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400583 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800584 elif i == 3:
585 main.log.info(
586 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800587 ": Git checkout %s - Switched to this branch" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700588 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800589 # main.log.info( "DEBUG: after checkout cmd = "+
590 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400591 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800592 elif i == 4:
593 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
594 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800595 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400596 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800597 elif i == 5:
598 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800599 main.log.error(
600 self.name +
601 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800602 "Your local changes to the following files would" +
603 " be overwritten by checkout:" +
604 str( self.handle.before ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700605 self.handle.expect( self.prompt )
Jon Hall81e29af2014-11-04 20:41:23 -0500606 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800607 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800608 main.log.error(
609 self.name +
610 ": Git checkout error: \n" +
611 "You need to resolve your current index first:" +
612 str( self.handle.before ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700613 self.handle.expect( self.prompt )
Jon Hall81e29af2014-11-04 20:41:23 -0500614 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800615 elif i == 7:
616 main.log.info(
617 self.name +
618 ": Git checkout " + str( branch ) +
619 " - You are in 'detached HEAD' state. HEAD is now at " +
620 str( branch ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700621 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800622 return main.TRUE
623 elif i == 8: # Already in detached HEAD on the specified commit
624 main.log.info(
625 self.name +
626 ": Git Checkout %s : Already on commit" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700627 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800628 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400629 else:
kelvin8ec71442015-01-15 16:57:00 -0800630 main.log.error(
631 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800632 ": Git Checkout - Unexpected response, " +
633 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800634 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400635 return main.ERROR
636
637 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800638 main.log.error( self.name + ": EOF exception found" )
639 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700640 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800641 except Exception:
642 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700643 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400644
pingping-lin6d23d9e2015-02-02 16:54:24 -0800645 def getBranchName( self ):
You Wang9cdf9a22017-05-01 13:44:18 -0700646 import re
647 try:
648 main.log.info( "self.home = " )
649 main.log.info( self.home )
650 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700651 self.handle.expect( self.prompt )
You Wang9cdf9a22017-05-01 13:44:18 -0700652 self.handle.sendline( "git name-rev --name-only HEAD" )
653 self.handle.expect( "git name-rev --name-only HEAD" )
Devin Limc20e79a2017-06-07 10:29:57 -0700654 self.handle.expect( self.prompt )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700655 lines = self.handle.before.splitlines()
656 if lines[ 1 ] == "master" or re.search( "^onos-\d+(\.\d+)+$", lines[ 1 ] ):
657 return lines[ 1 ]
You Wang9cdf9a22017-05-01 13:44:18 -0700658 else:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700659 main.log.info( lines[ 1 ] )
You Wang9cdf9a22017-05-01 13:44:18 -0700660 return "unexpected ONOS branch"
661 except pexpect.EOF:
662 main.log.error( self.name + ": EOF exception found" )
663 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700664 main.cleanAndExit()
You Wang9cdf9a22017-05-01 13:44:18 -0700665 except pexpect.TIMEOUT:
666 main.log.error( self.name + ": TIMEOUT exception found" )
667 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700668 main.cleanAndExit()
You Wang9cdf9a22017-05-01 13:44:18 -0700669 except Exception:
670 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700671 main.cleanAndExit()
pingping-lin6d23d9e2015-02-02 16:54:24 -0800672
kelvin-onlabd3b64892015-01-20 13:26:24 -0800673 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800674 """
Jon Hall274b6642015-02-17 11:57:17 -0800675 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800676 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800677 """
Jon Hall45ec0922014-10-10 19:33:49 -0400678 try:
kelvin8ec71442015-01-15 16:57:00 -0800679 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700680 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800681 self.handle.sendline(
682 "cd " +
683 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800684 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
685 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800686 # NOTE: for some reason there are backspaces inserted in this
687 # phrase when run from Jenkins on some tests
688 self.handle.expect( "never" )
Devin Limc20e79a2017-06-07 10:29:57 -0700689 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800690 response = ( self.name + ": \n" + str(
691 self.handle.before + self.handle.after ) )
692 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700693 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800694 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400695 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500696 print line
697 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700698 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800699 for line in lines[ 2:-1 ]:
700 # Bracket replacement is for Wiki-compliant
701 # formatting. '<' or '>' are interpreted
702 # as xml specific tags that cause errors
703 line = line.replace( "<", "[" )
704 line = line.replace( ">", "]" )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700705 # main.log.wiki( "\t" + line )
pingping-lin763ee042015-05-20 17:45:30 -0700706 main.log.wiki( line + "<br /> " )
707 main.log.summary( line )
708 main.log.wiki( "</blockquote>" )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700709 main.log.summary( "\n" )
kelvin8ec71442015-01-15 16:57:00 -0800710 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400711 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800712 main.log.error( self.name + ": EOF exception found" )
713 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700714 main.cleanAndExit()
Jon Hall368769f2014-11-19 15:43:35 -0800715 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800716 main.log.error( self.name + ": TIMEOUT exception found" )
717 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700718 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800719 except Exception:
720 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700721 main.cleanAndExit()
Jon Hall45ec0922014-10-10 19:33:49 -0400722
kelvin-onlabd3b64892015-01-20 13:26:24 -0800723 def createCellFile( self, benchIp, fileName, mnIpAddrs,
Jon Hall810b67d2016-12-05 10:19:01 -0800724 appString, onosIpAddrs, onosUser="sdn", useSSH=True ):
kelvin8ec71442015-01-15 16:57:00 -0800725 """
andrewonlab94282092014-10-10 13:00:11 -0400726 Creates a cell file based on arguments
727 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800728 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400729 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800730 * File name of the cell file ( fileName )
731 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800732 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400733 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800734 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400735 - Must be passed in as last arguments
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700736 * ONOS USER ( onosUser )
Flavio Castrocc38a542016-03-03 13:15:46 -0800737 - optional argument to set ONOS_USER environment variable
kelvin8ec71442015-01-15 16:57:00 -0800738
andrewonlab94282092014-10-10 13:00:11 -0400739 NOTE: Assumes cells are located at:
740 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800741 """
andrewonlab94282092014-10-10 13:00:11 -0400742 try:
Devin Lim461f0872017-06-05 16:49:33 -0700743
Jon Hall2c8959e2016-12-16 12:17:34 -0800744 # Variable initialization
745 cellDirectory = self.home + "/tools/test/cells/"
746 # We want to create the cell file in the dependencies directory
747 # of TestON first, then copy over to ONOS bench
748 tempDirectory = "/tmp/"
749 # Create the cell file in the directory for writing ( w+ )
750 cellFile = open( tempDirectory + fileName, 'w+' )
751 if isinstance( onosIpAddrs, types.StringType ):
752 onosIpAddrs = [ onosIpAddrs ]
753
754 # App string is hardcoded environment variables
755 # That you may wish to use by default on startup.
756 # Note that you may not want certain apps listed
757 # on here.
758 appString = "export ONOS_APPS=" + appString
759 onosGroup = "export ONOS_GROUP=" + onosUser
760 onosUser = "export ONOS_USER=" + onosUser
761 if useSSH:
762 onosUseSSH = "export ONOS_USE_SSH=true"
763 mnString = "export OCN="
764 if mnIpAddrs == "":
765 mnString = ""
766 onosString = "export OC"
767 tempCount = 1
768
769 # Create ONOSNIC ip address prefix
770 tempOnosIp = str( onosIpAddrs[ 0 ] )
771 tempList = []
772 tempList = tempOnosIp.split( "." )
773 # Omit last element of list to format for NIC
774 tempList = tempList[ :-1 ]
775 # Structure the nic string ip
776 nicAddr = ".".join( tempList ) + ".*"
777 self.nicAddr = nicAddr
778 onosNicString = "export ONOS_NIC=" + nicAddr
779
kelvin8ec71442015-01-15 16:57:00 -0800780 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800781 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400782
kelvin-onlabd3b64892015-01-20 13:26:24 -0800783 for arg in onosIpAddrs:
784 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800785 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400786 # export OC1="10.128.20.11"
787 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800788 cellFile.write( onosString + str( tempCount ) +
Jon Hall6f665652015-09-18 10:08:07 -0700789 "=\"" + arg + "\"\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800790 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800791
Jon Hall6f665652015-09-18 10:08:07 -0700792 cellFile.write( "export OCI=$OC1\n" )
793 cellFile.write( mnString + "\"" + mnIpAddrs + "\"\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700794 cellFile.write( appString + "\n" )
Flavio Castrocc38a542016-03-03 13:15:46 -0800795 cellFile.write( onosGroup + "\n" )
796 cellFile.write( onosUser + "\n" )
Pier88189b62016-09-07 17:01:53 -0700797 if useSSH:
798 cellFile.write( onosUseSSH + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800799 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400800
kelvin8ec71442015-01-15 16:57:00 -0800801 # We use os.system to send the command to TestON cluster
802 # to account for the case in which TestON is not located
803 # on the same cluster as the ONOS bench
804 # Note that even if TestON is located on the same cluster
805 # as ONOS bench, you must setup passwordless ssh
806 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabc2b79102015-07-14 11:41:20 -0700807 os.system( "scp " + tempDirectory + fileName + " " +
808 self.user_name + "@" + self.ip_address + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400809
andrewonlab2a6c9342014-10-16 13:40:15 -0400810 return main.TRUE
811
andrewonlab94282092014-10-10 13:00:11 -0400812 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800813 main.log.error( self.name + ": EOF exception found" )
814 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700815 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800816 except Exception:
817 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700818 main.cleanAndExit()
andrewonlab94282092014-10-10 13:00:11 -0400819
kelvin-onlabd3b64892015-01-20 13:26:24 -0800820 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800821 """
andrewonlab95ca1462014-10-09 14:04:24 -0400822 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800823 """
Hari Krishna03f530e2015-07-10 17:28:27 -0700824 import re
andrewonlab95ca1462014-10-09 14:04:24 -0400825 try:
826 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800827 main.log.error( "Must define cellname" )
Devin Lim44075962017-08-11 10:56:37 -0700828
829 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400830 else:
kelvin8ec71442015-01-15 16:57:00 -0800831 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800832 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800833 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400834 # and that this driver will have to change accordingly
Jon Hall3b489db2015-10-05 14:38:37 -0700835 self.handle.expect( str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800836 handleBefore = self.handle.before
837 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800838 # Get the rest of the handle
Devin Limc20e79a2017-06-07 10:29:57 -0700839 self.handle.expect( self.prompt )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700840 time.sleep( 10 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800841 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400842
Hari Krishna03f530e2015-07-10 17:28:27 -0700843 cell_result = handleBefore + handleAfter + handleMore
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700844 # print cell_result
Hari Krishna03f530e2015-07-10 17:28:27 -0700845 if( re.search( "No such cell", cell_result ) ):
846 main.log.error( "Cell call returned: " + handleBefore +
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700847 handleAfter + handleMore )
Devin Lim44075962017-08-11 10:56:37 -0700848
849 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400850 return main.TRUE
andrewonlab95ca1462014-10-09 14:04:24 -0400851 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800852 main.log.error( self.name + ": EOF exception found" )
853 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700854 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800855 except Exception:
856 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700857 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400858
kelvin-onlabd3b64892015-01-20 13:26:24 -0800859 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800860 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400861 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800862 """
863 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400864
andrewonlabc03bf6c2014-10-09 14:56:18 -0400865 try:
kelvin8ec71442015-01-15 16:57:00 -0800866 # Clean handle by sending empty and expecting $
867 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700868 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800869 self.handle.sendline( "onos-verify-cell" )
Devin Limc20e79a2017-06-07 10:29:57 -0700870 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800871 handleBefore = self.handle.before
872 handleAfter = self.handle.after
kelvin-onlabd3b64892015-01-20 13:26:24 -0800873 main.log.info( "Verify cell returned: " + handleBefore +
Jon Hall3b489db2015-10-05 14:38:37 -0700874 handleAfter )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400875 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800876 except pexpect.ExceptionPexpect as e:
Jon Hall3b489db2015-10-05 14:38:37 -0700877 main.log.exception( self.name + ": Pexpect exception found: " )
kelvin8ec71442015-01-15 16:57:00 -0800878 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700879 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800880 except Exception:
881 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700882 main.cleanAndExit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400883
jenkins1e99e7b2015-04-02 18:15:39 -0700884 def onosCfgSet( self, ONOSIp, configName, configParam ):
885 """
886 Uses 'onos <node-ip> cfg set' to change a parameter value of an
Jon Hall4ba53f02015-07-29 13:07:41 -0700887 application.
888
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700889 ex )
jenkins1e99e7b2015-04-02 18:15:39 -0700890 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
jenkins1e99e7b2015-04-02 18:15:39 -0700891 ONOSIp = '10.0.0.1'
892 configName = 'org.onosproject.myapp'
893 configParam = 'appSetting 1'
jenkins1e99e7b2015-04-02 18:15:39 -0700894 """
Jon Hall72280bc2016-01-25 14:29:05 -0800895 try:
896 cfgStr = "onos {} cfg set {} {}".format( ONOSIp,
897 configName,
898 configParam )
899 self.handle.sendline( "" )
900 self.handle.expect( ":~" )
901 self.handle.sendline( cfgStr )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700902 self.handle.expect( "cfg set" )
Jon Hall72280bc2016-01-25 14:29:05 -0800903 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700904
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700905 paramValue = configParam.split( " " )[ 1 ]
906 paramName = configParam.split( " " )[ 0 ]
Jon Hall4ba53f02015-07-29 13:07:41 -0700907
Jon Hall72280bc2016-01-25 14:29:05 -0800908 checkStr = 'onos {} cfg get " {} {} " '.format( ONOSIp, configName, paramName )
Jon Hall4ba53f02015-07-29 13:07:41 -0700909
Jon Hall72280bc2016-01-25 14:29:05 -0800910 self.handle.sendline( checkStr )
911 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700912
Jon Hall72280bc2016-01-25 14:29:05 -0800913 if "value=" + paramValue + "," in self.handle.before:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700914 main.log.info( "cfg " + configName + " successfully set to " + configParam )
Jon Hall72280bc2016-01-25 14:29:05 -0800915 return main.TRUE
916 except pexpect.ExceptionPexpect as e:
917 main.log.exception( self.name + ": Pexpect exception found: " )
918 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700919 main.cleanAndExit()
Jon Hall72280bc2016-01-25 14:29:05 -0800920 except Exception:
921 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700922 main.cleanAndExit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700923
kelvin-onlabd3b64892015-01-20 13:26:24 -0800924 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800925 """
andrewonlab05e362f2014-10-10 00:40:57 -0400926 Uses 'onos' command to send various ONOS CLI arguments.
927 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800928 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400929 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800930
931 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400932 CLI commands for ONOS. Try to use this function first
933 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800934 function.
935 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400936 by starting onos, and typing in 'onos' to enter the
937 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800938 available commands.
939 """
andrewonlab05e362f2014-10-10 00:40:57 -0400940 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800941 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800942 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400943 return main.FALSE
944 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800945 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400946 return main.FALSE
947
kelvin8ec71442015-01-15 16:57:00 -0800948 cmdstr = str( cmdstr )
949 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700950 self.handle.expect( self.prompt )
andrewonlab05e362f2014-10-10 00:40:57 -0400951
kelvin-onlabd3b64892015-01-20 13:26:24 -0800952 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
Devin Limc20e79a2017-06-07 10:29:57 -0700953 self.handle.expect( self.prompt )
andrewonlab05e362f2014-10-10 00:40:57 -0400954
kelvin-onlabd3b64892015-01-20 13:26:24 -0800955 handleBefore = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800956 main.log.info( "Command sent successfully" )
kelvin8ec71442015-01-15 16:57:00 -0800957 # Obtain return handle that consists of result from
958 # the onos command. The string may need to be
959 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800960 returnString = handleBefore
kelvin-onlabd3b64892015-01-20 13:26:24 -0800961 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400962 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800963 main.log.error( self.name + ": EOF exception found" )
964 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700965 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800966 except Exception:
967 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700968 main.cleanAndExit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400969
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700970 def onosSecureSSH( self, userName="onos", userPWD="rocks", node="" ):
Pier88189b62016-09-07 17:01:53 -0700971 """
972 Enables secure access to ONOS console
973 by removing default users & keys.
974
975 onos-secure-ssh -u onos -p rocks node
976
977 Returns: main.TRUE on success and main.FALSE on failure
978 """
Pier88189b62016-09-07 17:01:53 -0700979 try:
Chiyu Chengef109502016-11-21 15:51:38 -0800980 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700981 self.handle.expect( self.prompt )
Pier88189b62016-09-07 17:01:53 -0700982 self.handle.sendline( " onos-secure-ssh -u " + userName + " -p " + userPWD + " " + node )
983
984 # NOTE: this timeout may need to change depending on the network
985 # and size of ONOS
986 # TODO: Handle the other possible error
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700987 i = self.handle.expect( [ "Network\sis\sunreachable",
988 self.prompt,
989 pexpect.TIMEOUT ], timeout=180 )
Pier88189b62016-09-07 17:01:53 -0700990 if i == 0:
991 # can't reach ONOS node
992 main.log.warn( "Network is unreachable" )
Devin Limc20e79a2017-06-07 10:29:57 -0700993 self.handle.expect( self.prompt )
Pier88189b62016-09-07 17:01:53 -0700994 return main.FALSE
995 elif i == 1:
996 # Process started
997 main.log.info(
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700998 "Secure SSH performed on " +
999 node )
Pier88189b62016-09-07 17:01:53 -07001000 return main.TRUE
1001 except pexpect.EOF:
1002 main.log.error( self.name + ": EOF exception found" )
1003 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001004 main.cleanAndExit()
Pier88189b62016-09-07 17:01:53 -07001005 except Exception:
1006 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001007 main.cleanAndExit()
Pier88189b62016-09-07 17:01:53 -07001008
kelvin-onlabd3b64892015-01-20 13:26:24 -08001009 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001010 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001011 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -08001012 If -f option is provided, it also forces an uninstall.
1013 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -04001014 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -08001015 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -04001016 files to certain onos nodes
1017
1018 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -08001019 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001020 try:
andrewonlab114768a2014-11-14 12:44:44 -05001021 if options:
kelvin8ec71442015-01-15 16:57:00 -08001022 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -05001023 else:
kelvin8ec71442015-01-15 16:57:00 -08001024 self.handle.sendline( "onos-install " + node )
1025 self.handle.expect( "onos-install " )
1026 # NOTE: this timeout may need to change depending on the network
1027 # and size of ONOS
1028 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001029 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -08001030 "ONOS\sis\salready\sinstalled",
Jeremyc72b2582016-02-26 18:27:38 -08001031 "already\sup-to-date",
Jon Hall3576f572016-08-23 10:01:07 -07001032 "does not exist",
Devin Limc20e79a2017-06-07 10:29:57 -07001033 self.prompt,
Jon Hall6c44c0b2016-04-20 15:21:00 -07001034 pexpect.TIMEOUT ], timeout=180 )
Jon Hall7993bfc2014-10-09 16:30:14 -04001035 if i == 0:
Jon Hall3576f572016-08-23 10:01:07 -07001036 # can't reach ONOS node
kelvin8ec71442015-01-15 16:57:00 -08001037 main.log.warn( "Network is unreachable" )
Devin Limc20e79a2017-06-07 10:29:57 -07001038 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001039 return main.FALSE
1040 elif i == 1:
Jon Hall3576f572016-08-23 10:01:07 -07001041 # Process started
kelvin8ec71442015-01-15 16:57:00 -08001042 main.log.info(
1043 "ONOS was installed on " +
1044 node +
1045 " and started" )
Devin Limc20e79a2017-06-07 10:29:57 -07001046 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001047 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001048 elif i == 2 or i == 3:
1049 # same bits are already on ONOS node
Jeremyc72b2582016-02-26 18:27:38 -08001050 main.log.info( "ONOS is already installed on " + node )
Devin Limc20e79a2017-06-07 10:29:57 -07001051 self.handle.expect( self.prompt )
Jeremyc72b2582016-02-26 18:27:38 -08001052 return main.TRUE
1053 elif i == 4:
Jon Hall3576f572016-08-23 10:01:07 -07001054 # onos not packaged
1055 main.log.error( "ONOS package not found." )
Devin Limc20e79a2017-06-07 10:29:57 -07001056 self.handle.expect( self.prompt )
Jon Hall3576f572016-08-23 10:01:07 -07001057 return main.FALSE
1058 elif i == 5:
1059 # prompt
Jeremyc72b2582016-02-26 18:27:38 -08001060 main.log.info( "ONOS was installed on " + node )
1061 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001062 elif i == 6:
1063 # timeout
kelvin8ec71442015-01-15 16:57:00 -08001064 main.log.info(
1065 "Installation of ONOS on " +
1066 node +
1067 " timed out" )
Devin Limc20e79a2017-06-07 10:29:57 -07001068 self.handle.expect( self.prompt )
Jon Hall53c5e662016-04-13 16:06:56 -07001069 main.log.warn( self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001070 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -04001071 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001072 main.log.error( self.name + ": EOF exception found" )
1073 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001074 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001075 except Exception:
1076 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001077 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -04001078
kelvin-onlabd3b64892015-01-20 13:26:24 -08001079 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001080 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001081 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001082 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001083 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001084 try:
kelvin8ec71442015-01-15 16:57:00 -08001085 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001086 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001087 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001088 " start" )
1089 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -04001090 "Job\sis\salready\srunning",
1091 "start/running",
Devin Limc20e79a2017-06-07 10:29:57 -07001092 self.prompt,
andrewonlab8d0d7d72014-10-09 16:33:15 -04001093 "Unknown\sinstance",
Jeremy Songster14c13572016-04-21 17:34:03 -07001094 pexpect.TIMEOUT ], timeout=180 )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001095 if i == 0:
Devin Limc20e79a2017-06-07 10:29:57 -07001096 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001097 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001098 return main.TRUE
1099 elif i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001100 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001101 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001102 return main.TRUE
Jeremyd0e9a6d2016-03-02 11:28:52 -08001103 elif i == 2:
1104 main.log.info( "ONOS service started" )
1105 return main.TRUE
andrewonlab8d0d7d72014-10-09 16:33:15 -04001106 else:
Devin Limc20e79a2017-06-07 10:29:57 -07001107 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001108 main.log.error( "ONOS service failed to start" )
Devin Lim44075962017-08-11 10:56:37 -07001109
1110 main.cleanAndExit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001111 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001112 main.log.error( self.name + ": EOF exception found" )
1113 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001114 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001115 except Exception:
1116 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001117 main.cleanAndExit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001118
kelvin-onlabd3b64892015-01-20 13:26:24 -08001119 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001120 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001121 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001122 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001123 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001124 try:
kelvin8ec71442015-01-15 16:57:00 -08001125 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001126 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001127 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001128 " stop" )
1129 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -04001130 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -07001131 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -04001132 "Unknown\sinstance",
Devin Limc20e79a2017-06-07 10:29:57 -07001133 self.prompt,
Jeremy Songster14c13572016-04-21 17:34:03 -07001134 pexpect.TIMEOUT ], timeout=180 )
andrewonlab2b30bd32014-10-09 16:48:55 -04001135 if i == 0:
Devin Limc20e79a2017-06-07 10:29:57 -07001136 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001137 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001138 return main.TRUE
1139 elif i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001140 self.handle.expect( self.prompt )
Jon Hall65844a32015-03-09 19:09:37 -07001141 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001142 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -04001143 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -07001144 elif i == 2:
Devin Limc20e79a2017-06-07 10:29:57 -07001145 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -07001146 main.log.warn( "ONOS wasn't running" )
1147 return main.TRUE
YPZhang77badfc2016-03-09 10:28:59 -08001148 elif i == 3:
1149 main.log.info( "ONOS service stopped" )
1150 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -04001151 else:
kelvin8ec71442015-01-15 16:57:00 -08001152 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001153 return main.FALSE
andrewonlab2b30bd32014-10-09 16:48:55 -04001154 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001155 main.log.error( self.name + ": EOF exception found" )
1156 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001157 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001158 except Exception:
1159 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001160 main.cleanAndExit()
kelvin8ec71442015-01-15 16:57:00 -08001161
kelvin-onlabd3b64892015-01-20 13:26:24 -08001162 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -08001163 """
andrewonlabc8d47972014-10-09 16:52:36 -04001164 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -08001165 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -04001166 if needed
kelvin8ec71442015-01-15 16:57:00 -08001167 """
andrewonlabc8d47972014-10-09 16:52:36 -04001168 try:
kelvin8ec71442015-01-15 16:57:00 -08001169 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001170 self.handle.expect( self.prompt, timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001171 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
Devin Limc20e79a2017-06-07 10:29:57 -07001172 self.handle.expect( self.prompt, timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001173 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
kelvin8ec71442015-01-15 16:57:00 -08001174 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -04001175 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -07001176 except pexpect.TIMEOUT:
1177 main.log.exception( self.name + ": Timeout in onosUninstall" )
1178 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -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 )
Devin Lim44075962017-08-11 10:56:37 -07001182 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001183 except Exception:
1184 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001185 main.cleanAndExit()
andrewonlab2b30bd32014-10-09 16:48:55 -04001186
kelvin-onlabd3b64892015-01-20 13:26:24 -08001187 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001188 """
andrewonlabaedc8332014-12-04 12:43:03 -05001189 Issues the command 'onos-die <node-ip>'
1190 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -08001191 """
andrewonlabaedc8332014-12-04 12:43:03 -05001192 try:
kelvin8ec71442015-01-15 16:57:00 -08001193 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001194 self.handle.expect( self.prompt )
Jeremyf0aecdb2016-03-30 13:19:57 -07001195 cmdStr = "onos-die " + str( nodeIp )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001196 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001197 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001198 "Killing\sONOS",
1199 "ONOS\sprocess\sis\snot\srunning",
Jeremy Songster14c13572016-04-21 17:34:03 -07001200 pexpect.TIMEOUT ], timeout=60 )
andrewonlabaedc8332014-12-04 12:43:03 -05001201 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001202 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001203 " was killed and stopped" )
Jon Hall53c5e662016-04-13 16:06:56 -07001204 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001205 self.handle.expect( self.prompt )
andrewonlabaedc8332014-12-04 12:43:03 -05001206 return main.TRUE
1207 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001208 main.log.info( "ONOS process was not running" )
Jon Hall53c5e662016-04-13 16:06:56 -07001209 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001210 self.handle.expect( self.prompt )
andrewonlabaedc8332014-12-04 12:43:03 -05001211 return main.FALSE
1212 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001213 main.log.error( self.name + ": EOF exception found" )
1214 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001215 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001216 except Exception:
1217 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001218 main.cleanAndExit()
andrewonlabaedc8332014-12-04 12:43:03 -05001219
kelvin-onlabd3b64892015-01-20 13:26:24 -08001220 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001221 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001222 Calls the command: 'onos-kill [<node-ip>]'
1223 "Remotely, and unceremoniously kills the ONOS instance running on
1224 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001225 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001226 try:
kelvin8ec71442015-01-15 16:57:00 -08001227 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001228 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001229 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001230 i = self.handle.expect( [
Devin Limc20e79a2017-06-07 10:29:57 -07001231 self.prompt,
andrewonlabe8e56fd2014-10-09 17:12:44 -04001232 "No\sroute\sto\shost",
1233 "password:",
Jeremy Songster14c13572016-04-21 17:34:03 -07001234 pexpect.TIMEOUT ], timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -08001235
andrewonlabe8e56fd2014-10-09 17:12:44 -04001236 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001237 main.log.info(
1238 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001239 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001240 return main.TRUE
1241 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001242 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001243 return main.FALSE
1244 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001245 main.log.info(
1246 "Passwordless login for host: " +
1247 str( nodeIp ) +
1248 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001249 return main.FALSE
1250 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001251 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001252 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001253
andrewonlabe8e56fd2014-10-09 17:12:44 -04001254 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001255 main.log.error( self.name + ": EOF exception found" )
1256 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001257 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001258 except Exception:
1259 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001260 main.cleanAndExit()
andrewonlabe8e56fd2014-10-09 17:12:44 -04001261
kelvin-onlabd3b64892015-01-20 13:26:24 -08001262 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001263 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001264 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001265 a cleaner environment.
1266
andrewonlab19fbdca2014-11-14 12:55:59 -05001267 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001268 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001269 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001270 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001271 try:
kelvin8ec71442015-01-15 16:57:00 -08001272 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001273 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001274 self.handle.sendline( "onos-remove-raft-logs" )
1275 # Sometimes this command hangs
Devin Limc20e79a2017-06-07 10:29:57 -07001276 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
kelvin8ec71442015-01-15 16:57:00 -08001277 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001278 if i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001279 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
kelvin8ec71442015-01-15 16:57:00 -08001280 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001281 if i == 1:
1282 return main.FALSE
andrewonlab19fbdca2014-11-14 12:55:59 -05001283 return main.TRUE
andrewonlab19fbdca2014-11-14 12:55:59 -05001284 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001285 main.log.error( self.name + ": EOF exception found" )
1286 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001287 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001288 except Exception:
1289 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001290 main.cleanAndExit()
Jon Hallfcc88622014-11-25 13:09:54 -05001291
kelvin-onlabd3b64892015-01-20 13:26:24 -08001292 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001293 """
1294 Calls the command 'onos-start-network [ <mininet-topo> ]
1295 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001296 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001297 cell."
andrewonlab94282092014-10-10 13:00:11 -04001298 * Specify mininet topology file name for mntopo
1299 * Topo files should be placed at:
1300 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001301
andrewonlab94282092014-10-10 13:00:11 -04001302 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001303 """
andrewonlab94282092014-10-10 13:00:11 -04001304 try:
1305 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001306 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001307 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001308
kelvin8ec71442015-01-15 16:57:00 -08001309 mntopo = str( mntopo )
1310 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001311 self.handle.expect( self.prompt )
andrewonlab94282092014-10-10 13:00:11 -04001312
kelvin8ec71442015-01-15 16:57:00 -08001313 self.handle.sendline( "onos-start-network " + mntopo )
1314 self.handle.expect( "mininet>" )
1315 main.log.info( "Network started, entered mininet prompt" )
1316
1317 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001318
1319 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001320 main.log.error( self.name + ": EOF exception found" )
1321 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001322 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001323 except Exception:
1324 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001325 main.cleanAndExit()
andrewonlab94282092014-10-10 13:00:11 -04001326
Jeremy Songster14c13572016-04-21 17:34:03 -07001327 def isup( self, node="", timeout=240 ):
kelvin8ec71442015-01-15 16:57:00 -08001328 """
1329 Run's onos-wait-for-start which only returns once ONOS is at run
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001330 level 100( ready for use )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001331
Jon Hall7993bfc2014-10-09 16:30:14 -04001332 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001333 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001334 try:
Jon Hall3b489db2015-10-05 14:38:37 -07001335 self.handle.sendline( "onos-wait-for-start " + node )
1336 self.handle.expect( "onos-wait-for-start" )
kelvin8ec71442015-01-15 16:57:00 -08001337 # NOTE: this timeout is arbitrary"
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001338 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], timeout )
Jon Hall7993bfc2014-10-09 16:30:14 -04001339 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001340 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001341 return main.TRUE
1342 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001343 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001344 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001345 main.log.error( "ONOS has not started yet" )
1346 self.handle.send( "\x03" ) # Control-C
Devin Limc20e79a2017-06-07 10:29:57 -07001347 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001348 return main.FALSE
1349 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001350 main.log.error( self.name + ": EOF exception found" )
1351 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001352 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001353 except Exception:
1354 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001355 main.cleanAndExit()
andrewonlab05e362f2014-10-10 00:40:57 -04001356
Devin Lim142b5342017-07-20 15:22:39 -07001357 def preventAutoRespawn( self ):
1358 """
1359 Description:
1360 This will prevent ONOSservice to automatically
1361 respawn.
1362 """
1363 try:
1364 self.handle.sendline( "sed -i -e 's/^respawn$/#respawn/g' tools/package/init/onos.conf" )
1365 self.handle.expect( "\$" ) # $ from the command
1366 self.handle.sendline( "sed -i -e 's/^Restart=always/Restart=no/g' tools/package/init/onos.service" )
1367 self.handle.expect( "\$" ) # $ from the command
1368 self.handle.expect( "\$" ) # $ from the prompt
1369 except pexpect.EOF:
1370 main.log.error( self.name + ": EOF exception found" )
1371 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001372 main.cleanAndExit()
Devin Lim142b5342017-07-20 15:22:39 -07001373 except Exception:
1374 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001375 main.cleanAndExit()
Devin Lim142b5342017-07-20 15:22:39 -07001376
kelvin-onlabd3b64892015-01-20 13:26:24 -08001377 def pushTestIntentsShell(
1378 self,
1379 dpidSrc,
1380 dpidDst,
1381 numIntents,
1382 dirFile,
1383 onosIp,
1384 numMult="",
1385 appId="",
1386 report=True,
1387 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001388 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001389 Description:
kelvin8ec71442015-01-15 16:57:00 -08001390 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001391 better parallelize the results than the CLI
1392 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001393 * dpidSrc: specify source dpid
1394 * dpidDst: specify destination dpid
1395 * numIntents: specify number of intents to push
1396 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001397 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001398 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001399 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001400 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001401 """
1402 try:
1403 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001404 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001405 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001406 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001407 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001408 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001409
kelvin-onlabd3b64892015-01-20 13:26:24 -08001410 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1411 if not numMult:
1412 addIntents = addDpid + " " + str( numIntents )
1413 elif numMult:
1414 addIntents = addDpid + " " + str( numIntents ) + " " +\
1415 str( numMult )
1416 if appId:
1417 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001418 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001419 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001420
andrewonlabaedc8332014-12-04 12:43:03 -05001421 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001422 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001423 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001424 sendCmd = addApp + " &"
1425 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001426
kelvin-onlabd3b64892015-01-20 13:26:24 -08001427 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001428
1429 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001430 main.log.error( self.name + ": EOF exception found" )
1431 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001432 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001433 except Exception:
1434 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001435 main.cleanAndExit()
andrewonlab05e362f2014-10-10 00:40:57 -04001436
kelvin-onlabd3b64892015-01-20 13:26:24 -08001437 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001438 """
andrewonlab970399c2014-11-07 13:09:32 -05001439 Capture all packet activity and store in specified
1440 directory/file
1441
1442 Required:
1443 * interface: interface to capture
1444 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001445 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001446 try:
1447 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001448 self.handle.expect( self.prompt )
andrewonlab970399c2014-11-07 13:09:32 -05001449
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001450 self.handle.sendline( "tshark -i " + str( interface ) + " -t e -w " + str( dirFile ) + " &" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001451 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001452 self.handle.expect( "Capturing on" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001453 self.handle.sendline( "\n" )
Devin Limc20e79a2017-06-07 10:29:57 -07001454 self.handle.expect( self.prompt )
andrewonlab970399c2014-11-07 13:09:32 -05001455
Jon Hallfebb1c72015-03-05 13:30:09 -08001456 main.log.info( "Tshark started capturing files on " +
1457 str( interface ) + " and saving to directory: " +
1458 str( dirFile ) )
1459 except pexpect.EOF:
1460 main.log.error( self.name + ": EOF exception found" )
1461 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001462 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001463 except Exception:
1464 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001465 main.cleanAndExit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001466
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001467 def onosTopoCfg( self, onosIp, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001468 """
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001469 Description:
1470 Execute onos-topo-cfg command
1471 Required:
1472 onosIp - IP of the onos node you want to send the json to
1473 jsonFile - File path of the json file
1474 Return:
1475 Returns main.TRUE if the command is successfull; Returns
1476 main.FALSE if there was an error
kelvin8ec71442015-01-15 16:57:00 -08001477 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001478 try:
kelvin8ec71442015-01-15 16:57:00 -08001479 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001480 self.handle.expect( self.prompt )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001481 cmd = "onos-topo-cfg "
1482 self.handle.sendline( cmd + str( onosIp ) + " " + jsonFile )
1483 handle = self.handle.before
1484 print handle
1485 if "Error" in handle:
1486 main.log.error( self.name + ": " + self.handle.before )
1487 return main.FALSE
1488 else:
Devin Limc20e79a2017-06-07 10:29:57 -07001489 self.handle.expect( self.prompt )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001490 return main.TRUE
1491
Jon Hallfebb1c72015-03-05 13:30:09 -08001492 except pexpect.EOF:
1493 main.log.error( self.name + ": EOF exception found" )
1494 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001495 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001496 except Exception:
1497 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001498 main.cleanAndExit()
kelvin8ec71442015-01-15 16:57:00 -08001499
jenkins1e99e7b2015-04-02 18:15:39 -07001500 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001501 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001502 Required:
kelvin8ec71442015-01-15 16:57:00 -08001503 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001504 * directory to store results
1505 Optional:
1506 * interface - default: eth0
Jon Hall4ba53f02015-07-29 13:07:41 -07001507 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001508 Description:
1509 Uses tshark command to grep specific group of packets
1510 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001511 The timestamp is hardcoded to be in epoch
1512 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001513 try:
1514 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001515 self.handle.expect( self.prompt )
Jon Hallfebb1c72015-03-05 13:30:09 -08001516 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001517 if grepOptions:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001518 grepStr = "grep " + str( grepOptions )
jenkins1e99e7b2015-04-02 18:15:39 -07001519 else:
1520 grepStr = "grep"
Jon Hall4ba53f02015-07-29 13:07:41 -07001521
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001522 cmd = (
1523 "sudo tshark -i " +
Jon Hallfebb1c72015-03-05 13:30:09 -08001524 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001525 " -t e | " +
1526 grepStr + " --line-buffered \"" +
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001527 str( grep ) +
Jon Hallfebb1c72015-03-05 13:30:09 -08001528 "\" >" +
1529 directory +
1530 " &" )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001531 self.handle.sendline( cmd )
1532 main.log.info( cmd )
Jon Hallfebb1c72015-03-05 13:30:09 -08001533 self.handle.expect( "Capturing on" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001534 self.handle.sendline( "\n" )
Devin Limc20e79a2017-06-07 10:29:57 -07001535 self.handle.expect( self.prompt )
Jon Hallfebb1c72015-03-05 13:30:09 -08001536 except pexpect.EOF:
1537 main.log.error( self.name + ": EOF exception found" )
1538 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001539 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001540 except Exception:
1541 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001542 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001543
kelvin-onlabd3b64892015-01-20 13:26:24 -08001544 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001545 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001546 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001547 """
1548 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001549 try:
1550 self.execute( cmd="sudo rm /tmp/wireshark*" )
1551 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001552 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1553 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001554 self.handle.sendline( "" )
1555 main.log.info( "Tshark stopped" )
1556 except pexpect.EOF:
1557 main.log.error( self.name + ": EOF exception found" )
1558 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001559 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001560 except Exception:
1561 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001562 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001563
kelvin8ec71442015-01-15 16:57:00 -08001564 def ptpd( self, args ):
1565 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001566 Initiate ptp with user-specified args.
1567 Required:
1568 * args: specify string of args after command
1569 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001570 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001571 try:
kelvin8ec71442015-01-15 16:57:00 -08001572 self.handle.sendline( "sudo ptpd " + str( args ) )
1573 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001574 "Multiple",
1575 "Error",
Devin Limc20e79a2017-06-07 10:29:57 -07001576 self.prompt ] )
1577 self.handle.expect( self.prompt )
andrewonlabba44bcf2014-10-16 16:54:41 -04001578
andrewonlab0c38a4a2014-10-28 18:35:35 -04001579 if i == 0:
1580 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001581 main.log.info( "ptpd returned an error: " +
1582 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001583 return handle
1584 elif i == 1:
1585 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001586 main.log.error( "ptpd returned an error: " +
1587 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001588 return handle
1589 else:
1590 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001591
andrewonlab0c38a4a2014-10-28 18:35:35 -04001592 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001593 main.log.error( self.name + ": EOF exception found" )
1594 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001595 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001596 except Exception:
1597 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001598 main.cleanAndExit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001599
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001600 def dumpONOSCmd( self, ONOSIp, CMD, destDir, filename, options="" ):
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001601 """
Pier50f0bc62016-09-07 17:53:40 -07001602 Dump Cmd to a desired directory.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001603 For debugging purposes, you may want to use
Pier50f0bc62016-09-07 17:53:40 -07001604 this function to capture Cmd at a given point in time.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001605 Localtime will be attached to the filename
1606
1607 Required:
1608 * ONOSIp: the IP of the target ONOS instance
Pier50f0bc62016-09-07 17:53:40 -07001609 * CMD: the command to dump;
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001610 * destDir: specify directory to copy to.
1611 ex ) /tmp/
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001612 * fileName: Name of the file
Pier50f0bc62016-09-07 17:53:40 -07001613 * options: Options for ONOS command
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001614 """
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001615 localtime = time.strftime( '%x %X' )
1616 localtime = localtime.replace( "/", "" )
1617 localtime = localtime.replace( " ", "_" )
1618 localtime = localtime.replace( ":", "" )
1619 if destDir[ -1: ] != "/":
1620 destDir += "/"
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001621 cmd = CMD + " " + options + " > " + str( destDir ) + str( filename ) + localtime
1622 return self.onosCli( ONOSIp, cmd )
Flavio Castrob7718952016-05-18 08:53:41 -07001623
kelvin-onlabd3b64892015-01-20 13:26:24 -08001624 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001625 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001626 """
1627 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001628 Current implementation of ONOS deletes its karaf
1629 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001630 you may want to use this function to capture
1631 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001632 Localtime will be attached to the filename
1633
1634 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001635 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001636 copy.
kelvin8ec71442015-01-15 16:57:00 -08001637 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001638 For copying multiple files, leave copyFileName
1639 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001640 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001641 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001642 ex ) /tmp/
1643 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001644 * copyFileName: If you want to rename the log
1645 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001646 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001647 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001648 try:
Flavio Castro09ab59d2016-05-25 17:01:35 -07001649 localtime = time.strftime( '%H %M' )
kelvin8ec71442015-01-15 16:57:00 -08001650 localtime = localtime.replace( "/", "" )
1651 localtime = localtime.replace( " ", "_" )
1652 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001653 if destDir[ -1: ] != "/":
1654 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001655
kelvin-onlabd3b64892015-01-20 13:26:24 -08001656 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001657 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1658 str( destDir ) + str( copyFileName ) +
1659 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001660 self.handle.expect( "cp" )
Devin Limc20e79a2017-06-07 10:29:57 -07001661 self.handle.expect( self.prompt )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001662 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001663 self.handle.sendline( "cp " + str( logToCopy ) +
1664 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001665 self.handle.expect( "cp" )
Devin Limc20e79a2017-06-07 10:29:57 -07001666 self.handle.expect( self.prompt )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001667
kelvin8ec71442015-01-15 16:57:00 -08001668 return self.handle.before
1669
1670 except pexpect.EOF:
1671 main.log.error( "Copying files failed" )
1672 main.log.error( self.name + ": EOF exception found" )
1673 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001674 except Exception:
1675 main.log.exception( "Copying files failed" )
1676
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001677 def checkLogs( self, onosIp, restart=False ):
kelvin8ec71442015-01-15 16:57:00 -08001678 """
Jon Hall94fd0472014-12-08 11:52:42 -08001679 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001680 If restart is True, use the old version of onos-check-logs which
1681 does not print the full stacktrace, but shows the entire log file,
1682 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001683 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001684 """
Jon Hall94fd0472014-12-08 11:52:42 -08001685 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001686 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001687 if restart:
1688 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001689 self.handle.sendline( cmd )
1690 self.handle.expect( cmd )
Devin Limdc78e202017-06-09 18:30:07 -07001691 self.handle.expect( self.prompt + " " )
Jon Hall94fd0472014-12-08 11:52:42 -08001692 response = self.handle.before
1693 return response
1694 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001695 main.log.error( "Lost ssh connection" )
1696 main.log.error( self.name + ": EOF exception found" )
1697 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001698 except Exception:
1699 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001700 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08001701
kelvin-onlabd3b64892015-01-20 13:26:24 -08001702 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001703 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001704 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001705 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001706 try:
kelvin8ec71442015-01-15 16:57:00 -08001707 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001708 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001709 self.handle.sendline( "onos-service " + str( node ) +
1710 " status" )
1711 i = self.handle.expect( [
You Wangef1e6572016-03-08 12:53:18 -08001712 "start/running",
You Wang7bd83062016-03-01 11:50:00 -08001713 "Running ...",
You Wangef1e6572016-03-08 12:53:18 -08001714 "stop/waiting",
You Wang7bd83062016-03-01 11:50:00 -08001715 "Not Running ...",
kelvin8ec71442015-01-15 16:57:00 -08001716 pexpect.TIMEOUT ], timeout=120 )
YPZhangfebf7302016-05-24 16:45:56 -07001717 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001718 self.handle.expect( self.prompt )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001719
You Wangef1e6572016-03-08 12:53:18 -08001720 if i == 0 or i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001721 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001722 return main.TRUE
You Wangef1e6572016-03-08 12:53:18 -08001723 elif i == 2 or i == 3:
kelvin8ec71442015-01-15 16:57:00 -08001724 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001725 main.log.error( "ONOS service failed to check the status" )
Devin Lim44075962017-08-11 10:56:37 -07001726
1727 main.cleanAndExit()
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001728 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001729 main.log.error( self.name + ": EOF exception found" )
1730 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001731 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001732 except Exception:
1733 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001734 main.cleanAndExit()
Jon Hall21270ac2015-02-16 17:59:55 -08001735
Jon Hall63604932015-02-26 17:09:50 -08001736 def setIpTables( self, ip, port='', action='add', packet_type='',
1737 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001738 """
Jon Hall21270ac2015-02-16 17:59:55 -08001739 Description:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001740 add or remove iptables rule to DROP ( default ) packets from
Jon Hall21270ac2015-02-16 17:59:55 -08001741 specific IP and PORT
1742 Usage:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001743 * specify action ( 'add' or 'remove' )
Jon Hall21270ac2015-02-16 17:59:55 -08001744 when removing, pass in the same argument as you would add. It will
1745 delete that specific rule.
1746 * specify the ip to block
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001747 * specify the destination port to block ( defaults to all ports )
1748 * optional packet type to block ( default tcp )
1749 * optional iptables rule ( default DROP )
1750 * optional direction to block ( default 'INPUT' )
Jon Hall63604932015-02-26 17:09:50 -08001751 * States boolean toggles adding all supported tcp states to the
1752 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001753 Returns:
1754 main.TRUE on success or
1755 main.FALSE if given invalid input or
1756 main.ERROR if there is an error in response from iptables
1757 WARNING:
1758 * This function uses root privilege iptables command which may result
1759 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001760 """
Jon Hall21270ac2015-02-16 17:59:55 -08001761 # NOTE*********
1762 # The strict checking methods of this driver function is intentional
1763 # to discourage any misuse or error of iptables, which can cause
1764 # severe network errors
1765 # *************
1766
1767 # NOTE: Sleep needed to give some time for rule to be added and
1768 # registered to the instance. If you are calling this function
1769 # multiple times this sleep will prevent any errors.
1770 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001771 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001772 try:
1773 # input validation
1774 action_type = action.lower()
1775 rule = rule.upper()
1776 direction = direction.upper()
1777 if action_type != 'add' and action_type != 'remove':
1778 main.log.error( "Invalid action type. Use 'add' or "
1779 "'remove' table rule" )
1780 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1781 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1782 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1783 "'ACCEPT' or 'LOG' only." )
1784 if direction != 'INPUT' and direction != 'OUTPUT':
1785 # NOTE currently only supports rules INPUT and OUPTUT
1786 main.log.error( "Invalid rule. Valid directions are"
1787 " 'OUTPUT' or 'INPUT'" )
1788 return main.FALSE
1789 return main.FALSE
1790 return main.FALSE
1791 if action_type == 'add':
1792 # -A is the 'append' action of iptables
1793 actionFlag = '-A'
1794 elif action_type == 'remove':
1795 # -D is the 'delete' rule of iptables
1796 actionFlag = '-D'
1797 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001798 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001799 cmd = "sudo iptables " + actionFlag + " " +\
1800 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001801 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001802 if packet_type:
1803 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001804 if port:
1805 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001806 if states:
1807 cmd += " -m state --state="
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001808 # FIXME- Allow user to configure which states to block
Jon Hall63604932015-02-26 17:09:50 -08001809 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001810 cmd += " -j " + str( rule )
1811
1812 self.handle.sendline( cmd )
Devin Limc20e79a2017-06-07 10:29:57 -07001813 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001814 main.log.warn( self.handle.before )
1815
1816 info_string = "On " + str( self.name )
1817 info_string += " " + str( action_type )
1818 info_string += " iptable rule [ "
1819 info_string += " IP: " + str( ip )
1820 info_string += " Port: " + str( port )
1821 info_string += " Rule: " + str( rule )
1822 info_string += " Direction: " + str( direction ) + " ]"
1823 main.log.info( info_string )
1824 return main.TRUE
1825 except pexpect.TIMEOUT:
1826 main.log.exception( self.name + ": Timeout exception in "
1827 "setIpTables function" )
1828 return main.ERROR
1829 except pexpect.EOF:
1830 main.log.error( self.name + ": EOF exception found" )
1831 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001832 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001833 except Exception:
1834 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001835 main.cleanAndExit()
Jon Hall21270ac2015-02-16 17:59:55 -08001836
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001837 def detailed_status( self, log_filename ):
Jon Hallefbd9792015-03-05 16:11:36 -08001838 """
Jon Hall0468b042015-02-19 19:08:21 -08001839 This method is used by STS to check the status of the controller
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001840 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR ( and reason )
Jon Hallefbd9792015-03-05 16:11:36 -08001841 """
Jon Hall0468b042015-02-19 19:08:21 -08001842 import re
1843 try:
1844 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001845 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001846 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -07001847 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001848 self.handle.sendline( "service onos status" )
Devin Limc20e79a2017-06-07 10:29:57 -07001849 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001850 response = self.handle.before
1851 if re.search( "onos start/running", response ):
1852 # onos start/running, process 10457
1853 return 'RUNNING'
1854 # FIXME: Implement this case
1855 # elif re.search( pattern, response ):
1856 # return 'STARTING'
1857 elif re.search( "onos stop/", response ):
1858 # onos stop/waiting
1859 # FIXME handle this differently?: onos stop/pre-stop
1860 return 'STOPPED'
1861 # FIXME: Implement this case
1862 # elif re.search( pattern, response ):
1863 # return 'FROZEN'
1864 else:
1865 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001866 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001867 main.log.warn( response )
1868 return 'ERROR', "Unknown response: %s" % response
1869 except pexpect.TIMEOUT:
1870 main.log.exception( self.name + ": Timeout exception in "
1871 "setIpTables function" )
1872 return 'ERROR', "Pexpect Timeout"
1873 except pexpect.EOF:
1874 main.log.error( self.name + ": EOF exception found" )
1875 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001876 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001877 except Exception:
1878 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001879 main.cleanAndExit()
Jon Hall0468b042015-02-19 19:08:21 -08001880
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001881 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount ):
1882 """
Jon Hall4ba53f02015-07-29 13:07:41 -07001883 Create/formats the LinkGraph.cfg file based on arguments
1884 -only creates a linear topology and connects islands
1885 -evenly distributes devices
andrew@onlab.us3b087132015-03-11 15:00:08 -07001886 -must be called by ONOSbench
1887
Jon Hall4ba53f02015-07-29 13:07:41 -07001888 ONOSIpList - list of all of the node IPs to be used
1889
1890 deviceCount - number of switches to be assigned
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001891 """
1892 main.log.info( "Creating link graph configuration file." )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001893 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
Jon Hall4ba53f02015-07-29 13:07:41 -07001894 tempFile = "/tmp/linkGraph.cfg"
andrew@onlab.us3b087132015-03-11 15:00:08 -07001895
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001896 linkGraph = open( tempFile, 'w+' )
1897 linkGraph.write( "# NullLinkProvider topology description (config file).\n" )
1898 linkGraph.write( "# The NodeId is only added if the destination is another node's device.\n" )
1899 linkGraph.write( "# Bugs: Comments cannot be appended to a line to be read.\n" )
Jon Hall4ba53f02015-07-29 13:07:41 -07001900
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001901 clusterCount = len( ONOSIpList )
Jon Hall4ba53f02015-07-29 13:07:41 -07001902
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001903 if isinstance( deviceCount, int ) or isinstance( deviceCount, str ):
1904 deviceCount = int( deviceCount )
1905 switchList = [ 0 ] * ( clusterCount + 1 )
1906 baselineSwitchCount = deviceCount / clusterCount
Jon Hall4ba53f02015-07-29 13:07:41 -07001907
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001908 for node in range( 1, clusterCount + 1 ):
1909 switchList[ node ] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001910
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001911 for node in range( 1, ( deviceCount % clusterCount ) + 1 ):
1912 switchList[ node ] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001913
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001914 if isinstance( deviceCount, list ):
1915 main.log.info( "Using provided device distribution" )
1916 switchList = [ 0 ]
andrew@onlab.us3b087132015-03-11 15:00:08 -07001917 for i in deviceCount:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001918 switchList.append( int( i ) )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001919
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001920 tempList = [ '0' ]
1921 tempList.extend( ONOSIpList )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001922 ONOSIpList = tempList
1923
1924 myPort = 6
1925 lastSwitch = 0
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001926 for node in range( 1, clusterCount + 1 ):
1927 if switchList[ node ] == 0:
andrew@onlab.us3b087132015-03-11 15:00:08 -07001928 continue
1929
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001930 linkGraph.write( "graph " + ONOSIpList[ node ] + " {\n" )
Jon Hall4ba53f02015-07-29 13:07:41 -07001931
andrew@onlab.us3b087132015-03-11 15:00:08 -07001932 if node > 1:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001933 # connect to last device on previous node
1934 line = ( "\t0:5 -> " + str( lastSwitch ) + ":6:" + lastIp + "\n" ) # ONOSIpList[ node-1 ]
1935 linkGraph.write( line )
Jon Hall4ba53f02015-07-29 13:07:41 -07001936
1937 lastSwitch = 0
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001938 for switch in range( 0, switchList[ node ] - 1 ):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001939 line = ""
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001940 line = ( "\t" + str( switch ) + ":" + str( myPort ) )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001941 line += " -- "
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001942 line += ( str( switch + 1 ) + ":" + str( myPort - 1 ) + "\n" )
1943 linkGraph.write( line )
1944 lastSwitch = switch + 1
1945 lastIp = ONOSIpList[ node ]
Jon Hall4ba53f02015-07-29 13:07:41 -07001946
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001947 # lastSwitch += 1
1948 if node < ( clusterCount ):
1949 # connect to first device on the next node
1950 line = ( "\t" + str( lastSwitch ) + ":6 -> 0:5:" + ONOSIpList[ node + 1 ] + "\n" )
1951 linkGraph.write( line )
Jon Hall4ba53f02015-07-29 13:07:41 -07001952
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001953 linkGraph.write( "}\n" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001954 linkGraph.close()
1955
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001956 # SCP
1957 os.system( "scp " + tempFile + " " + self.user_name + "@" + benchIp + ":" + linkGraphPath )
1958 main.log.info( "linkGraph.cfg creation complete" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001959
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001960 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10 ):
1961 """
Jon Hall4ba53f02015-07-29 13:07:41 -07001962 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
1963 deviceCount = number of switches to distribute, or list of values to use as custom distribution
cameron@onlab.us75900962015-03-30 13:22:49 -07001964 numPorts = number of ports per device. Defaults to 10 both in this function and in ONOS. Optional arg
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001965 """
1966 main.log.info( "Configuring Null Device Provider" )
1967 clusterCount = len( ONOSIpList )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001968
Jon Hall4ba53f02015-07-29 13:07:41 -07001969 try:
1970
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001971 if isinstance( deviceCount, int ) or isinstance( deviceCount, str ):
1972 main.log.info( "Creating device distribution" )
1973 deviceCount = int( deviceCount )
1974 switchList = [ 0 ] * ( clusterCount + 1 )
1975 baselineSwitchCount = deviceCount / clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001976
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001977 for node in range( 1, clusterCount + 1 ):
1978 switchList[ node ] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001979
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001980 for node in range( 1, ( deviceCount % clusterCount ) + 1 ):
1981 switchList[ node ] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001982
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001983 if isinstance( deviceCount, list ):
1984 main.log.info( "Using provided device distribution" )
Jon Hall4ba53f02015-07-29 13:07:41 -07001985
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001986 if len( deviceCount ) == clusterCount:
1987 switchList = [ '0' ]
1988 switchList.extend( deviceCount )
Jon Hall4ba53f02015-07-29 13:07:41 -07001989
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001990 if len( deviceCount ) == ( clusterCount + 1 ):
1991 if deviceCount[ 0 ] == '0' or deviceCount[ 0 ] == 0:
cameron@onlab.us75900962015-03-30 13:22:49 -07001992 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001993
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001994 assert len( switchList ) == ( clusterCount + 1 )
Jon Hall4ba53f02015-07-29 13:07:41 -07001995
cameron@onlab.us75900962015-03-30 13:22:49 -07001996 except AssertionError:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07001997 main.log.error( "Bad device/Ip list match" )
cameron@onlab.us75900962015-03-30 13:22:49 -07001998 except TypeError:
1999 main.log.exception( self.name + ": Object not as expected" )
2000 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07002001 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002002 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002003 main.cleanAndExit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002004
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002005 ONOSIp = [ 0 ]
2006 ONOSIp.extend( ONOSIpList )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002007
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002008 devicesString = "devConfigs = "
2009 for node in range( 1, len( ONOSIp ) ):
2010 devicesString += ( ONOSIp[ node ] + ":" + str( switchList[ node ] ) )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002011 if node < clusterCount:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002012 devicesString += ( "," )
Jon Hall4ba53f02015-07-29 13:07:41 -07002013
2014 try:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002015 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
2016 self.handle.expect( ":~" )
2017 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str( numPorts ) )
2018 self.handle.expect( ":~" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002019
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002020 for i in range( 10 ):
2021 self.handle.sendline( "onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider" )
2022 self.handle.expect( ":~" )
cameron@onlab.us75900962015-03-30 13:22:49 -07002023 verification = self.handle.before
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002024 if ( " value=" + str( numPorts ) ) in verification and ( " value=" + devicesString ) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002025 break
2026 else:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002027 time.sleep( 1 )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002028
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002029 assert ( "value=" + str( numPorts ) ) in verification and ( " value=" + devicesString ) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002030
cameron@onlab.us75900962015-03-30 13:22:49 -07002031 except AssertionError:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002032 main.log.error( "Incorrect Config settings: " + verification )
Jon Hall77ba41c2015-04-06 10:25:40 -07002033 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002034 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002035 main.cleanAndExit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002036
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002037 def configNullLink( self, fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0 ):
2038 """
Jon Hall4ba53f02015-07-29 13:07:41 -07002039 fileName default is currently the same as the default on ONOS, specify alternate file if
cameron@onlab.us75900962015-03-30 13:22:49 -07002040 you want to use a different topology file than linkGraph.cfg
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002041 """
Jon Hall4ba53f02015-07-29 13:07:41 -07002042 try:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002043 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str( eventRate ) )
2044 self.handle.expect( ":~" )
2045 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2046 self.handle.expect( ":~" )
Jon Hall4ba53f02015-07-29 13:07:41 -07002047
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002048 for i in range( 10 ):
2049 self.handle.sendline( "onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider" )
2050 self.handle.expect( ":~" )
cameron@onlab.us75900962015-03-30 13:22:49 -07002051 verification = self.handle.before
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002052 if ( " value=" + str( eventRate ) ) in verification and ( " value=" + fileName ) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002053 break
Jon Hall4ba53f02015-07-29 13:07:41 -07002054 else:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002055 time.sleep( 1 )
Jon Hall4ba53f02015-07-29 13:07:41 -07002056
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002057 assert ( "value=" + str( eventRate ) ) in verification and ( " value=" + fileName ) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002058
cameron@onlab.us75900962015-03-30 13:22:49 -07002059 except pexpect.EOF:
2060 main.log.error( self.name + ": EOF exception found" )
2061 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002062 main.cleanAndExit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002063 except AssertionError:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002064 main.log.info( "Settings did not post to ONOS" )
2065 main.log.error( varification )
Jon Hall77ba41c2015-04-06 10:25:40 -07002066 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002067 main.log.exception( self.name + ": Uncaught exception!" )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002068 main.log.error( varification )
Devin Lim44075962017-08-11 10:56:37 -07002069 main.cleanAndExit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002070
kelvin-onlaba4074292015-07-09 15:19:49 -07002071 def getOnosIps( self ):
2072 """
2073 Get all onos IPs stored in
2074 """
kelvin-onlaba4074292015-07-09 15:19:49 -07002075 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002076
Chiyu Chengec63bde2016-11-17 18:11:36 -08002077 def listLog( self, nodeIp ):
2078 """
2079 Get a list of all the karaf log names
2080 """
2081 try:
2082 cmd = "onos-ssh " + nodeIp + " ls -tr /opt/onos/log"
2083 self.handle.sendline( cmd )
2084 self.handle.expect( ":~" )
2085 before = self.handle.before.splitlines()
2086 logNames = []
2087 for word in before:
2088 if 'karaf.log' in word:
2089 logNames.append( word )
2090 return logNames
2091 except pexpect.EOF:
2092 main.log.error( self.name + ": EOF exception found" )
2093 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002094 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08002095 except pexpect.TIMEOUT:
2096 main.log.error( self.name + ": TIMEOUT exception found" )
2097 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002098 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08002099 except Exception:
2100 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002101 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08002102
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002103 def logReport( self, nodeIp, searchTerms, outputMode="s", startStr=None, endStr=None ):
Jon Hallb4242222016-01-25 17:07:04 -08002104 """
2105 Searches the latest ONOS log file for the given search terms and
2106 prints the total occurances of each term. Returns to combined total of
2107 all occurances.
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002108
Jon Hallb4242222016-01-25 17:07:04 -08002109 Arguments:
2110 * nodeIp - The ip of the ONOS node where the log is located
2111 * searchTerms - A string to grep for or a list of strings to grep
2112 for in the ONOS log. Will print out the number of
2113 occurances for each term.
2114 Optional Arguments:
2115 * outputMode - 's' or 'd'. If 'd' will print the last 5 lines
2116 containing each search term as well as the total
2117 number of occurances of each term. Defaults to 's',
2118 which prints the simple output of just the number
2119 of occurances for each term.
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002120 * startStr - the start string to be given to stream editor command
2121 as the start point for extraction of data
2122 * endStr - the end string to be given to stream editor command as
2123 the end point for extraction of data
Jon Hallb4242222016-01-25 17:07:04 -08002124 """
2125 try:
2126 main.log.info( " Log Report for {} ".format( nodeIp ).center( 70, '=' ) )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002127 if isinstance( searchTerms, str ):
2128 searchTerms = [ searchTerms ]
Jon Hallb4242222016-01-25 17:07:04 -08002129 numTerms = len( searchTerms )
2130 outputMode = outputMode.lower()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002131
Jon Hallb4242222016-01-25 17:07:04 -08002132 totalHits = 0
2133 logLines = []
2134 for termIndex in range( numTerms ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002135 term = searchTerms[ termIndex ]
2136 logLines.append( [ term ] )
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002137 if startStr and endStr:
2138 cmd = "onos-ssh {} \"sed -n '/{}/,/{}/p' /opt/onos/log/karaf.log | grep {}\"".format( nodeIp,
2139 startStr,
2140 endStr,
2141 term )
2142 else:
2143 cmd = "onos-ssh {} cat /opt/onos/log/karaf.log | grep {}".format( nodeIp,
2144 term )
Jon Hallb4242222016-01-25 17:07:04 -08002145 self.handle.sendline( cmd )
2146 self.handle.expect( ":~" )
2147 before = self.handle.before.splitlines()
2148 count = 0
2149 for line in before:
2150 if term in line and "grep" not in line:
2151 count += 1
2152 if before.index( line ) > ( len( before ) - 7 ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002153 logLines[ termIndex ].append( line )
Jon Hallb4242222016-01-25 17:07:04 -08002154 main.log.info( "{}: {}".format( term, count ) )
2155 totalHits += count
2156 if termIndex == numTerms - 1:
2157 print "\n"
2158 if outputMode != "s":
2159 outputString = ""
2160 for term in logLines:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002161 outputString = term[ 0 ] + ": \n"
Jon Hallb4242222016-01-25 17:07:04 -08002162 for line in range( 1, len( term ) ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002163 outputString += ( "\t" + term[ line ] + "\n" )
2164 if outputString != ( term[ 0 ] + ": \n" ):
Jon Hallb4242222016-01-25 17:07:04 -08002165 main.log.info( outputString )
2166 main.log.info( "=" * 70 )
2167 return totalHits
2168 except pexpect.EOF:
2169 main.log.error( self.name + ": EOF exception found" )
2170 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002171 main.cleanAndExit()
Jon Hallb4242222016-01-25 17:07:04 -08002172 except pexpect.TIMEOUT:
2173 main.log.error( self.name + ": TIMEOUT exception found" )
2174 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002175 main.cleanAndExit()
Jon Hallb4242222016-01-25 17:07:04 -08002176 except Exception:
2177 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002178 main.cleanAndExit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002179
2180 def copyMininetFile( self, fileName, localPath, userName, ip,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002181 mnPath='~/mininet/custom/', timeout=60 ):
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002182 """
2183 Description:
2184 Copy mininet topology file from dependency folder in the test folder
2185 and paste it to the mininet machine's mininet/custom folder
2186 Required:
2187 fileName - Name of the topology file to copy
2188 localPath - File path of the mininet topology file
2189 userName - User name of the mininet machine to send the file to
2190 ip - Ip address of the mininet machine
2191 Optional:
2192 mnPath - of the mininet directory to send the file to
2193 Return:
2194 Return main.TRUE if successfully copied the file otherwise
2195 return main.FALSE
2196 """
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002197 try:
2198 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2199 str( ip ) + ":" + mnPath + fileName
2200
2201 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07002202 self.handle.expect( self.prompt )
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002203
2204 main.log.info( self.name + ": Execute: " + cmd )
2205
2206 self.handle.sendline( cmd )
2207
2208 i = self.handle.expect( [ 'No such file',
2209 "100%",
2210 pexpect.TIMEOUT ] )
2211
2212 if i == 0:
2213 main.log.error( self.name + ": File " + fileName +
2214 " does not exist!" )
2215 return main.FALSE
2216
2217 if i == 1:
2218 main.log.info( self.name + ": File " + fileName +
2219 " has been copied!" )
2220 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07002221 self.handle.expect( self.prompt )
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002222 return main.TRUE
2223
2224 except pexpect.EOF:
2225 main.log.error( self.name + ": EOF exception found" )
2226 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002227 main.cleanAndExit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002228 except pexpect.TIMEOUT:
2229 main.log.error( self.name + ": TIMEOUT exception found" )
2230 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002231 main.cleanAndExit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002232
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002233 def jvmSet( self, memory=8 ):
Jon Hall4ba53f02015-07-29 13:07:41 -07002234
cameron@onlab.us78b89652015-07-08 15:21:03 -07002235 import os
2236
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002237 homeDir = os.path.expanduser( '~' )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002238 filename = "/onos/tools/package/bin/onos-service"
2239
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002240 serviceConfig = open( homeDir + filename, 'w+' )
2241 serviceConfig.write( "#!/bin/bash\n " )
2242 serviceConfig.write( "#------------------------------------- \n " )
2243 serviceConfig.write( "# Starts ONOS Apache Karaf container\n " )
2244 serviceConfig.write( "#------------------------------------- \n " )
2245 serviceConfig.write( "#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n " )
2246 serviceConfig.write( """export JAVA_OPTS="${ JAVA_OPTS:--Xms""" + str(memory) + "G -Xmx" + str(memory) + """G }" \n """ )
2247 serviceConfig.write( "[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n" )
2248 serviceConfig.write( """${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """ )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002249 serviceConfig.close()
2250
Jon Hall6c44c0b2016-04-20 15:21:00 -07002251 def createDBFile( self, testData ):
Jon Hall4ba53f02015-07-29 13:07:41 -07002252
cameron@onlab.us78b89652015-07-08 15:21:03 -07002253 filename = main.TEST + "DB"
2254 DBString = ""
Jon Hall4ba53f02015-07-29 13:07:41 -07002255
cameron@onlab.us78b89652015-07-08 15:21:03 -07002256 for item in testData:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002257 if isinstance( item, string ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002258 item = "'" + item + "'"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002259 if testData.index( item ) < len( testData - 1 ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002260 item += ","
Jon Hall6c44c0b2016-04-20 15:21:00 -07002261 DBString += str( item )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002262
Jon Hall6c44c0b2016-04-20 15:21:00 -07002263 DBFile = open( filename, "a" )
2264 DBFile.write( DBString )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002265 DBFile.close()
2266
Jon Hall6c44c0b2016-04-20 15:21:00 -07002267 def verifySummary( self, ONOSIp, *deviceCount ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002268
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002269 self.handle.sendline( "onos " + ONOSIp + " summary" )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002270 self.handle.expect( ":~" )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002271
2272 summaryStr = self.handle.before
2273 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2274
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002275 # passed = "SCC(s)=1" in summaryStr
2276 # if deviceCount:
2277 # passed = "devices=" + str( deviceCount ) + "," not in summaryStr
cameron@onlab.us78b89652015-07-08 15:21:03 -07002278
GlennRC772363b2015-08-25 13:05:57 -07002279 passed = False
cameron@onlab.us78b89652015-07-08 15:21:03 -07002280 if "SCC(s)=1," in summaryStr:
2281 passed = True
Jon Hall6c44c0b2016-04-20 15:21:00 -07002282 print "Summary is verifed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002283 else:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002284 print "Summary failed"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002285
2286 if deviceCount:
2287 print" ============================="
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002288 checkStr = "devices=" + str( deviceCount[ 0 ] ) + ","
cameron@onlab.us78b89652015-07-08 15:21:03 -07002289 print "Checkstr: " + checkStr
2290 if checkStr not in summaryStr:
2291 passed = False
Jon Hall6c44c0b2016-04-20 15:21:00 -07002292 print "Device count failed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002293 else:
2294 print "device count verified"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002295
2296 return passed
Jon Hall6c44c0b2016-04-20 15:21:00 -07002297
Jon Hall8f6d4622016-05-23 15:27:18 -07002298 def getIpAddr( self, iface=None ):
Jon Hall6c44c0b2016-04-20 15:21:00 -07002299 """
2300 Update self.ip_address with numerical ip address. If multiple IP's are
2301 located on the device, will attempt to use self.nicAddr to choose the
2302 right one. Defaults to 127.0.0.1 if no other address is found or cannot
2303 determine the correct address.
2304
2305 ONLY WORKS WITH IPV4 ADDRESSES
2306 """
2307 try:
Jon Hall8f6d4622016-05-23 15:27:18 -07002308 LOCALHOST = "127.0.0.1"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002309 ipPat = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
2310 pattern = re.compile( ipPat )
2311 match = re.search( pattern, self.ip_address )
2312 if self.nicAddr:
2313 nicPat = self.nicAddr.replace( ".", "\." ).replace( "\*", r"\d{1,3}" )
2314 nicPat = re.compile( nicPat )
2315 else:
2316 nicPat = None
2317 # IF self.ip_address is an ip address and matches
2318 # self.nicAddr: return self.ip_address
2319 if match:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002320 curIp = match.group( 0 )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002321 if nicPat:
2322 nicMatch = re.search( nicPat, curIp )
2323 if nicMatch:
2324 return self.ip_address
Jon Hall8f6d4622016-05-23 15:27:18 -07002325 # ELSE: IF iface, return ip of interface
2326 cmd = "ifconfig"
2327 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2328 if iface:
2329 cmd += " " + str( iface )
2330 raw = subprocess.check_output( cmd.split() )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002331 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2332 ips = re.findall( ifPat, raw )
Jon Hall8f6d4622016-05-23 15:27:18 -07002333 if iface:
2334 if ips:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002335 ip = ips[ 0 ]
Jon Hall8f6d4622016-05-23 15:27:18 -07002336 self.ip_address = ip
2337 return ip
2338 else:
2339 main.log.error( "Error finding ip, ifconfig output:".format( raw ) )
2340 # ELSE: attempt to get address matching nicPat.
Jon Hall6c44c0b2016-04-20 15:21:00 -07002341 if nicPat:
2342 for ip in ips:
2343 curMatch = re.search( nicPat, ip )
2344 if curMatch:
2345 self.ip_address = ip
2346 return ip
Jon Hall8f6d4622016-05-23 15:27:18 -07002347 else: # If only one non-localhost ip, return that
2348 tmpList = [ ip for ip in ips if ip is not LOCALHOST ]
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002349 if len( tmpList ) == 1:
2350 curIp = tmpList[ 0 ]
Jon Hall6c44c0b2016-04-20 15:21:00 -07002351 self.ip_address = curIp
2352 return curIp
Jon Hall8f6d4622016-05-23 15:27:18 -07002353 # Either no non-localhost IPs, or more than 1
Jon Hallebccd352016-05-20 15:17:17 -07002354 main.log.warn( "getIpAddr failed to find a public IP address" )
Jon Hall8f6d4622016-05-23 15:27:18 -07002355 return LOCALHOST
Jon Halle1790952016-05-23 15:51:46 -07002356 except subprocess.CalledProcessError:
Jon Hall8f6d4622016-05-23 15:27:18 -07002357 main.log.exception( "Error executing ifconfig" )
2358 except IndexError:
2359 main.log.exception( "Error getting IP Address" )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002360 except Exception:
2361 main.log.exception( "Uncaught exception" )
suibin zhang116647a2016-05-06 16:30:09 -07002362
Devin Lim461f0872017-06-05 16:49:33 -07002363 def startBasicONOS( self, nodeList, opSleep=60, onosStartupSleep=30, onosUser="sdn" ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002364 """
suibin zhang116647a2016-05-06 16:30:09 -07002365 Start onos cluster with defined nodes, but only with drivers app
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002366 """
suibin zhang116647a2016-05-06 16:30:09 -07002367 import time
2368
2369 self.createCellFile( self.ip_address,
2370 "temp",
2371 self.ip_address,
2372 "drivers",
Devin Lim461f0872017-06-05 16:49:33 -07002373 nodeList, onosUser )
suibin zhang116647a2016-05-06 16:30:09 -07002374
2375 main.log.info( self.name + ": Apply cell to environment" )
2376 cellResult = self.setCell( "temp" )
2377 verifyResult = self.verifyCell()
2378
2379 main.log.info( self.name + ": Creating ONOS package" )
You Wangd1bcaca2016-10-24 15:23:26 -07002380 packageResult = self.buckBuild( timeout=opSleep )
suibin zhang116647a2016-05-06 16:30:09 -07002381
You Wangc669d212017-01-25 11:09:48 -08002382 main.log.info( self.name + ": Uninstalling ONOS" )
2383 for nd in nodeList:
2384 self.onosUninstall( nodeIp=nd )
2385
suibin zhang116647a2016-05-06 16:30:09 -07002386 main.log.info( self.name + ": Installing ONOS package" )
2387 for nd in nodeList:
You Wangc669d212017-01-25 11:09:48 -08002388 self.onosInstall( node=nd )
2389
2390 main.log.info( self.name + ": Set up ONOS secure SSH" )
2391 for nd in nodeList:
2392 self.onosSecureSSH( node=nd )
suibin zhang116647a2016-05-06 16:30:09 -07002393
2394 main.log.info( self.name + ": Starting ONOS service" )
2395 time.sleep( onosStartupSleep )
2396
2397 onosStatus = True
2398 for nd in nodeList:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002399 onosStatus = onosStatus & self.isup( node=nd )
2400 # print "onosStatus is: " + str( onosStatus )
suibin zhang116647a2016-05-06 16:30:09 -07002401
2402 return main.TRUE if onosStatus else main.FALSE
2403
Devin Lim02075272017-07-10 15:33:21 -07002404 def onosNetCfg( self, controllerIp, path, fileName ):
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002405 """
2406 Push a specified json file to ONOS through the onos-netcfg service
2407
2408 Required:
Devin Lim02075272017-07-10 15:33:21 -07002409 controllerIp - the Ip of the ONOS node in the cluster
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002410 path - the location of the file to be sent
2411 fileName - name of the json file to be sent
2412
2413 Returns main.TRUE on successfully sending json file, and main.FALSE if
2414 there is an error.
2415 """
2416 try:
Devin Lim02075272017-07-10 15:33:21 -07002417 cmd = "onos-netcfg {0} {1}{2}".format( controllerIp, path, fileName )
2418 main.log.info( "Sending: " + cmd )
2419 main.ONOSbench.handle.sendline( cmd )
Jeremy Ronquillo3008aa32017-07-07 15:38:57 -07002420 main.ONOSbench.handle.expect( self.prompt )
Devin Lim02075272017-07-10 15:33:21 -07002421 handle = self.handle.before
Jeremy Ronquillo3008aa32017-07-07 15:38:57 -07002422 if "Error" in handle or "No such file or directory" in handle or "curl: " in handle:
2423 main.log.error( self.name + ": " + handle + self.handle.after )
Devin Lim02075272017-07-10 15:33:21 -07002424 return main.FALSE
Devin Lim752dd7b2017-06-27 14:40:03 -07002425 return main.TRUE
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002426 except pexpect.EOF:
2427 main.log.error( self.name + ": EOF exception found" )
2428 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002429 main.cleanAndExit()
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002430 except Exception:
2431 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002432 main.cleanAndExit()