blob: 6923a29dbf61b0ca833c28dd73afb277e68ccb9f [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
andrewonlabe8e56fd2014-10-09 17:12:44 -04002
kelvin8ec71442015-01-15 16:57:00 -08003"""
4This driver interacts with ONOS bench, the OSGi platform
5that configures the ONOS nodes. ( aka ONOS-next )
andrewonlabe8e56fd2014-10-09 17:12:44 -04006
kelvin8ec71442015-01-15 16:57:00 -08007Please follow the coding style demonstrated by existing
andrewonlabe8e56fd2014-10-09 17:12:44 -04008functions and document properly.
9
10If you are a contributor to the driver, please
11list your email here for future contact:
12
13jhall@onlab.us
14andrew@onlab.us
15
16OCT 9 2014
Jeremy Songsterae01bba2016-07-11 15:39:17 -070017Modified 2016 by ON.Lab
18
19Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
20the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
21or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
andrewonlabe8e56fd2014-10-09 17:12:44 -040022
kelvin8ec71442015-01-15 16:57:00 -080023"""
Jon Hall05b2b432014-10-08 19:53:25 -040024import time
Jon Hall6801cda2015-07-15 14:13:45 -070025import types
Jon Hall05b2b432014-10-08 19:53:25 -040026import pexpect
kelvin-onlaba4074292015-07-09 15:19:49 -070027import os
Jon Hall6c44c0b2016-04-20 15:21:00 -070028import re
29import subprocess
pingping-lin6d23d9e2015-02-02 16:54:24 -080030from requests.models import Response
Jon Hall05b2b432014-10-08 19:53:25 -040031from drivers.common.clidriver import CLI
32
kelvin8ec71442015-01-15 16:57:00 -080033class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040034
kelvin8ec71442015-01-15 16:57:00 -080035 def __init__( self ):
36 """
37 Initialize client
38 """
Jon Hallefbd9792015-03-05 16:11:36 -080039 self.name = None
40 self.home = None
41 self.handle = None
Jon Hall6c44c0b2016-04-20 15:21:00 -070042 self.nicAddr = None
Devin Limdc78e202017-06-09 18:30:07 -070043 super( OnosDriver, self ).__init__()
kelvin8ec71442015-01-15 16:57:00 -080044
45 def connect( self, **connectargs ):
46 """
Jon Hall05b2b432014-10-08 19:53:25 -040047 Creates ssh handle for ONOS "bench".
kelvin-onlaba4074292015-07-09 15:19:49 -070048 NOTE:
49 The ip_address would come from the topo file using the host tag, the
50 value can be an environment variable as well as a "localhost" to get
51 the ip address needed to ssh to the "bench"
kelvin8ec71442015-01-15 16:57:00 -080052 """
Jon Hall05b2b432014-10-08 19:53:25 -040053 try:
Devin Limdc78e202017-06-09 18:30:07 -070054
Jon Hall05b2b432014-10-08 19:53:25 -040055 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080056 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us3b087132015-03-11 15:00:08 -070057 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040058 for key in self.options:
59 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080060 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040061 break
Jon Hall274b6642015-02-17 11:57:17 -080062 if self.home is None or self.home == "":
Jon Halle94919c2015-03-23 11:42:57 -070063 self.home = "~/onos"
Jon Hall21270ac2015-02-16 17:59:55 -080064
kelvin8ec71442015-01-15 16:57:00 -080065 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070066
kelvin-onlabc2b79102015-07-14 11:41:20 -070067 # The 'nodes' tag is optional and it is not required in .topo file
kelvin-onlaba4074292015-07-09 15:19:49 -070068 for key in self.options:
69 if key == "nodes":
kelvin-onlabc2b79102015-07-14 11:41:20 -070070 # Maximum number of ONOS nodes to run, if there is any
kelvin-onlaba4074292015-07-09 15:19:49 -070071 self.maxNodes = int( self.options[ 'nodes' ] )
72 break
73 self.maxNodes = None
74
kelvin-onlabc2b79102015-07-14 11:41:20 -070075 if self.maxNodes == None or self.maxNodes == "":
76 self.maxNodes = 100
kelvin-onlaba4074292015-07-09 15:19:49 -070077
kelvin-onlabc2b79102015-07-14 11:41:20 -070078
79 # 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:
113 if os.getenv( str( self.ip_address ) ) != None:
114 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 )
Jon Hall05b2b432014-10-08 19:53:25 -0400143 main.cleanup()
144 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800145 except Exception:
146 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall05b2b432014-10-08 19:53:25 -0400147 main.cleanup()
148 main.exit()
149
kelvin8ec71442015-01-15 16:57:00 -0800150 def disconnect( self ):
151 """
Jon Hall05b2b432014-10-08 19:53:25 -0400152 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800153 """
Jon Halld61331b2015-02-17 16:35:47 -0800154 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -0400155 try:
Jon Hall61282e32015-03-19 11:34:11 -0700156 if self.handle:
157 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700158 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700159 self.handle.sendline( "exit" )
160 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -0400161 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800162 main.log.error( self.name + ": EOF exception found" )
163 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700164 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700165 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700166 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800167 except Exception:
168 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -0400169 response = main.FALSE
170 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400171
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400172 def getEpochMs( self ):
173 """
174 Returns milliseconds since epoch
Jon Hall4ba53f02015-07-29 13:07:41 -0700175
176 When checking multiple nodes in a for loop,
177 around a hundred milliseconds of difference (ascending) is
178 generally acceptable due to calltime of the function.
179 Few seconds, however, is not and it means clocks
180 are off sync.
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400181 """
182 try:
183 self.handle.sendline( 'date +%s.%N' )
184 self.handle.expect( 'date \+\%s\.\%N' )
Devin Limdc78e202017-06-09 18:30:07 -0700185 self.handle.expect( self.prompt )
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400186 epochMs = self.handle.before
187 return epochMs
188 except Exception:
189 main.log.exception( 'Uncaught exception getting epoch time' )
190 main.cleanup()
191 main.exit()
192
Jon Hall6c44c0b2016-04-20 15:21:00 -0700193 def onosPackage( self, opTimeout=180 ):
kelvin8ec71442015-01-15 16:57:00 -0800194 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400195 Produce a self-contained tar.gz file that can be deployed
Jon Hall64af8502015-12-15 10:09:33 -0800196 and executed on any platform with Java 8 JRE.
kelvin8ec71442015-01-15 16:57:00 -0800197 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400198 try:
Jon Hall64af8502015-12-15 10:09:33 -0800199 ret = main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800200 self.handle.sendline( "onos-package" )
201 self.handle.expect( "onos-package" )
Jon Hall96451092016-05-04 09:42:30 -0700202 while True:
203 i = self.handle.expect( [ "Downloading",
204 "Unknown options",
205 "No such file or directory",
206 "tar.gz",
Devin Limc20e79a2017-06-07 10:29:57 -0700207 self.prompt ],
Jon Hall96451092016-05-04 09:42:30 -0700208 opTimeout )
209 handle = str( self.handle.before + self.handle.after )
210 if i == 0:
211 # Give more time to download the file
212 continue # expect again
213 elif i == 1:
214 # Incorrect usage
215 main.log.error( "onos-package does not recognize the given options" )
216 ret = main.FALSE
217 continue # expect again
218 elif i == 2:
219 # File(s) not found
220 main.log.error( "onos-package could not find a file or directory" )
221 ret = main.FALSE
222 continue # expect again
223 elif i == 3:
224 # tar.gz
225 continue # expect again
226 elif i == 4:
227 # Prompt returned
228 break
Jon Hallc6793552016-01-19 14:18:37 -0800229 main.log.info( "onos-package command returned: " + handle )
kelvin8ec71442015-01-15 16:57:00 -0800230 # As long as the sendline does not time out,
231 # return true. However, be careful to interpret
232 # the results of the onos-package command return
Jon Hall64af8502015-12-15 10:09:33 -0800233 return ret
234 except pexpect.TIMEOUT:
235 main.log.exception( self.name + ": TIMEOUT exception found in onosPackage" )
236 main.log.error( self.name + ": " + self.handle.before )
237 return main.FALSE
andrewonlab7735d852014-10-09 13:02:47 -0400238 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800239 main.log.error( self.name + ": EOF exception found" )
240 main.log.error( self.name + ": " + self.handle.before )
Jon Hall64af8502015-12-15 10:09:33 -0800241 main.cleanup()
242 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800243 except Exception:
244 main.log.exception( "Failed to package ONOS" )
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400245 main.cleanup()
246 main.exit()
247
kelvin-onlabd3b64892015-01-20 13:26:24 -0800248 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800249 """
andrewonlab8790abb2014-11-06 13:51:54 -0500250 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800251 """
andrewonlab8790abb2014-11-06 13:51:54 -0500252 try:
kelvin8ec71442015-01-15 16:57:00 -0800253 self.handle.sendline( "onos-build" )
254 self.handle.expect( "onos-build" )
255 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800256 "BUILD SUCCESS",
257 "ERROR",
258 "BUILD FAILED" ],
259 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800260 handle = str( self.handle.before )
Devin Limc20e79a2017-06-07 10:29:57 -0700261 self.handle.expect( self.prompt )
andrewonlab8790abb2014-11-06 13:51:54 -0500262
kelvin8ec71442015-01-15 16:57:00 -0800263 main.log.info( "onos-build command returned: " +
264 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500265
266 if i == 0:
267 return main.TRUE
268 else:
269 return handle
270
271 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800272 main.log.error( self.name + ": EOF exception found" )
273 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800274 except Exception:
275 main.log.exception( "Failed to build ONOS" )
andrewonlab8790abb2014-11-06 13:51:54 -0500276 main.cleanup()
277 main.exit()
278
shahshreya9f531fe2015-06-10 12:03:51 -0700279 def cleanInstall( self, skipTest=False, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800280 """
281 Runs mvn clean install in the root of the ONOS directory.
282 This will clean all ONOS artifacts then compile each module
shahshreya9f531fe2015-06-10 12:03:51 -0700283 Optional:
284 skipTest - Does "-DskipTests -Dcheckstyle.skip -U -T 1C" which
285 skip the test. This will make the building faster.
286 Disregarding the credibility of the build
kelvin8ec71442015-01-15 16:57:00 -0800287 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400288 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800289 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400290 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800291 main.log.info( "Running 'mvn clean install' on " +
292 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800293 ". This may take some time." )
294 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700295 self.handle.expect( self.prompt )
Jon Hallea7818b2014-10-09 14:30:59 -0400296
kelvin8ec71442015-01-15 16:57:00 -0800297 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700298 self.handle.expect( self.prompt )
shahshreya9f531fe2015-06-10 12:03:51 -0700299
300 if not skipTest:
301 self.handle.sendline( "mvn clean install" )
302 self.handle.expect( "mvn clean install" )
303 else:
304 self.handle.sendline( "mvn clean install -DskipTests" +
305 " -Dcheckstyle.skip -U -T 1C" )
306 self.handle.expect( "mvn clean install -DskipTests" +
307 " -Dcheckstyle.skip -U -T 1C" )
kelvin8ec71442015-01-15 16:57:00 -0800308 while True:
309 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800310 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800311 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400312 'BUILD\sFAILURE',
313 'BUILD\sSUCCESS',
Devin Limdc78e202017-06-09 18:30:07 -0700314 'onos' + self.prompt, #TODO: fix this to be more generic?
315 'ONOS' + self.prompt,
pingping-lin57a56ce2015-05-20 16:43:48 -0700316 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400317 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800318 main.log.error( self.name + ":There is insufficient memory \
319 for the Java Runtime Environment to continue." )
320 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400321 main.cleanup()
322 main.exit()
323 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800324 main.log.error( self.name + ": Build failure!" )
325 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400326 main.cleanup()
327 main.exit()
328 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800329 main.log.info( self.name + ": Build success!" )
Jon Halle94919c2015-03-23 11:42:57 -0700330 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800331 main.log.info( self.name + ": Build complete" )
332 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400333 for line in self.handle.before.splitlines():
334 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800335 main.log.info( line )
336 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700337 self.handle.expect( self.prompt, timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400338 return main.TRUE
Jon Halle94919c2015-03-23 11:42:57 -0700339 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800340 main.log.error(
341 self.name +
342 ": mvn clean install TIMEOUT!" )
343 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400344 main.cleanup()
345 main.exit()
346 else:
Jon Hall274b6642015-02-17 11:57:17 -0800347 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800348 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800349 # return main.FALSE
Jon Hallde9d9aa2014-10-08 20:36:02 -0400350 main.cleanup()
351 main.exit()
352 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800353 main.log.error( self.name + ": EOF exception found" )
354 main.log.error( self.name + ": " + self.handle.before )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400355 main.cleanup()
356 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800357 except Exception:
358 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400359 main.cleanup()
360 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400361
Jon Hall3576f572016-08-23 10:01:07 -0700362 def buckBuild( self, timeout=180 ):
363 """
364 Build onos using buck.
365 """
366 try:
367 ret = main.TRUE
368 self.handle.sendline( "buck build onos" )
369 self.handle.expect( "buck build onos" )
370 output = ""
371 while True:
372 i = self.handle.expect( [ "This does not appear to be the root of a Buck project.",
373 "\n",
374 "BUILD FAILED",
Devin Limc20e79a2017-06-07 10:29:57 -0700375 self.prompt ],
Jon Hall3576f572016-08-23 10:01:07 -0700376 timeout=timeout )
377 output += str( self.handle.before + self.handle.after )
378 if i == 0:
379 main.log.error( "Wrong location" )
380 ret = main.FALSE
381 elif i == 1:
382 # end of a line, buck is still printing output
383 pass
384 elif i == 2:
385 # Build failed
386 main.log.error( "Build failed" )
387 ret = main.FALSE
388 elif i == 3:
389 # Prompt returned
390 break
391 main.log.debug( output )
392 return ret
393 except pexpect.TIMEOUT:
394 main.log.exception( self.name + ": TIMEOUT exception found" )
395 main.log.error( self.name + ": " + self.handle.before )
396 return main.FALSE
397 except pexpect.EOF:
398 main.log.error( self.name + ": EOF exception found" )
399 main.log.error( self.name + ": " + self.handle.before )
400 main.cleanup()
401 main.exit()
402 except Exception:
403 main.log.exception( "Failed to build and package ONOS" )
404 main.cleanup()
405 main.exit()
406
Jon Hall61282e32015-03-19 11:34:11 -0700407 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800408 """
Jon Hallacabffd2014-10-09 12:36:53 -0400409 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800410
Jon Hall61282e32015-03-19 11:34:11 -0700411 If the fastForward boolean is set to true, only git pulls that can
412 be fast forwarded will be performed. IE if you have not local commits
413 in your branch.
414
Jon Hallacabffd2014-10-09 12:36:53 -0400415 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800416 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400417 for the purpose of pulling from other nodes if necessary.
418
Jon Hall47a93fb2015-01-06 16:46:06 -0800419 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400420 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800421 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400422 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400423
kelvin8ec71442015-01-15 16:57:00 -0800424 """
Jon Hallacabffd2014-10-09 12:36:53 -0400425 try:
kelvin8ec71442015-01-15 16:57:00 -0800426 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700427 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700428 cmd = "git pull"
429 if comp1 != "":
430 cmd += ' ' + comp1
431 if fastForward:
432 cmd += ' ' + " --ff-only"
433 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800434 i = self.handle.expect(
435 [
436 'fatal',
437 'Username\sfor\s(.*):\s',
438 '\sfile(s*) changed,\s',
439 'Already up-to-date',
440 'Aborting',
441 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800442 'You asked me to pull without telling me which branch you',
443 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700444 'Please enter a commit message to explain why this merge',
445 'Found a swap file by the name',
446 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800447 pexpect.TIMEOUT ],
448 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800449 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700450 main.log.error( self.name + ": Git pull had some issue" )
451 output = self.handle.after
Devin Limdc78e202017-06-09 18:30:07 -0700452 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700453 output += self.handle.before
454 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400455 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800456 elif i == 1:
457 main.log.error(
458 self.name +
459 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400460 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800461 elif i == 2:
462 main.log.info(
463 self.name +
464 ": Git Pull - pulling repository now" )
Devin Limc20e79a2017-06-07 10:29:57 -0700465 self.handle.expect( self.prompt, 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800466 # So that only when git pull is done, we do mvn clean compile
467 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800468 elif i == 3:
469 main.log.info( self.name + ": Git Pull - Already up to date" )
Devin Limc20e79a2017-06-07 10:29:57 -0700470 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800471 elif i == 4:
472 main.log.info(
473 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800474 ": Git Pull - Aborting..." +
475 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400476 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800477 elif i == 5:
478 main.log.info(
479 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800480 ": Git Pull - You are not currently " +
481 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400482 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800483 elif i == 6:
484 main.log.info(
485 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800486 ": Git Pull - You have not configured an upstream " +
487 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400488 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800489 elif i == 7:
490 main.log.info(
491 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800492 ": Git Pull - Pull is not possible because " +
493 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400494 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800495 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700496 # NOTE: abandoning test since we can't reliably handle this
497 # there could be different default text editors and we
498 # also don't know if we actually want to make the commit
499 main.log.error( "Git pull resulted in a merge commit message" +
500 ". Exiting test!" )
501 main.cleanup()
502 main.exit()
503 elif i == 9: # Merge commit message but swap file exists
504 main.log.error( "Git pull resulted in a merge commit message" +
505 " but a swap file exists." )
506 try:
507 self.handle.send( 'A' ) # Abort
Devin Limc20e79a2017-06-07 10:29:57 -0700508 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700509 return main.ERROR
510 except Exception:
511 main.log.exception( "Couldn't exit editor prompt!")
512 main.cleanup()
513 main.exit()
514 elif i == 10: # In the middle of a merge commit
515 main.log.error( "Git branch is in the middle of a merge. " )
516 main.log.warn( self.handle.before + self.handle.after )
517 return main.ERROR
518 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800519 main.log.error( self.name + ": Git Pull - TIMEOUT" )
520 main.log.error(
521 self.name + " Response was: " + str(
522 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400523 return main.ERROR
524 else:
kelvin8ec71442015-01-15 16:57:00 -0800525 main.log.error(
526 self.name +
527 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400528 return main.ERROR
529 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800530 main.log.error( self.name + ": EOF exception found" )
531 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400532 main.cleanup()
533 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800534 except Exception:
535 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400536 main.cleanup()
537 main.exit()
538
kelvin-onlabd3b64892015-01-20 13:26:24 -0800539 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800540 """
Jon Hallacabffd2014-10-09 12:36:53 -0400541 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800542
Jon Hallacabffd2014-10-09 12:36:53 -0400543 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800544 If used as gitCheckout( "branch" ) it will do git checkout
545 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400546
547 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800548 branch of the ONOS repository. If it has any problems, it will return
549 main.ERROR.
550 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400551 successful then the function will return main.TRUE.
552
kelvin8ec71442015-01-15 16:57:00 -0800553 """
Jon Hallacabffd2014-10-09 12:36:53 -0400554 try:
kelvin8ec71442015-01-15 16:57:00 -0800555 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700556 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800557 main.log.info( self.name +
558 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800559 cmd = "git checkout " + branch
560 self.handle.sendline( cmd )
561 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800562 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800563 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700564 'Username for (.*): ',
565 'Already on \'',
Jon Hall7a8354f2015-06-10 15:37:00 -0700566 'Switched to (a new )?branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800567 pexpect.TIMEOUT,
568 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800569 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800570 'error: you need to resolve your current index first',
571 "You are in 'detached HEAD' state.",
572 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800573 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800574 if i == 0:
575 main.log.error(
576 self.name +
577 ": Git checkout had some issue..." )
578 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400579 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800580 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800581 main.log.error(
582 self.name +
583 ": Git checkout asking for username." +
584 " Please configure your local git repository to be able " +
585 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800586 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400587 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800588 elif i == 2:
589 main.log.info(
590 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800591 ": Git Checkout %s : Already on this branch" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700592 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800593 # main.log.info( "DEBUG: after checkout cmd = "+
594 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400595 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800596 elif i == 3:
597 main.log.info(
598 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800599 ": Git checkout %s - Switched to this branch" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700600 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800601 # main.log.info( "DEBUG: after checkout cmd = "+
602 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400603 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800604 elif i == 4:
605 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
606 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800607 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400608 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800609 elif i == 5:
610 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800611 main.log.error(
612 self.name +
613 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800614 "Your local changes to the following files would" +
615 " be overwritten by checkout:" +
616 str( self.handle.before ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700617 self.handle.expect( self.prompt )
Jon Hall81e29af2014-11-04 20:41:23 -0500618 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800619 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800620 main.log.error(
621 self.name +
622 ": Git checkout error: \n" +
623 "You need to resolve your current index first:" +
624 str( self.handle.before ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700625 self.handle.expect( self.prompt )
Jon Hall81e29af2014-11-04 20:41:23 -0500626 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800627 elif i == 7:
628 main.log.info(
629 self.name +
630 ": Git checkout " + str( branch ) +
631 " - You are in 'detached HEAD' state. HEAD is now at " +
632 str( branch ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700633 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800634 return main.TRUE
635 elif i == 8: # Already in detached HEAD on the specified commit
636 main.log.info(
637 self.name +
638 ": Git Checkout %s : Already on commit" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700639 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800640 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400641 else:
kelvin8ec71442015-01-15 16:57:00 -0800642 main.log.error(
643 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800644 ": Git Checkout - Unexpected response, " +
645 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800646 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400647 return main.ERROR
648
649 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800650 main.log.error( self.name + ": EOF exception found" )
651 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400652 main.cleanup()
653 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800654 except Exception:
655 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400656 main.cleanup()
657 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400658
pingping-lin6d23d9e2015-02-02 16:54:24 -0800659 def getBranchName( self ):
You Wang9cdf9a22017-05-01 13:44:18 -0700660 import re
661 try:
662 main.log.info( "self.home = " )
663 main.log.info( self.home )
664 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700665 self.handle.expect( self.prompt )
You Wang9cdf9a22017-05-01 13:44:18 -0700666 self.handle.sendline( "git name-rev --name-only HEAD" )
667 self.handle.expect( "git name-rev --name-only HEAD" )
Devin Limc20e79a2017-06-07 10:29:57 -0700668 self.handle.expect( self.prompt )
You Wang9cdf9a22017-05-01 13:44:18 -0700669 lines = self.handle.before.splitlines()
670 if lines[1] == "master" or re.search( "^onos-\d+(\.\d+)+$", lines[1] ):
671 return lines[1]
672 else:
673 main.log.info( lines[1] )
674 return "unexpected ONOS branch"
675 except pexpect.EOF:
676 main.log.error( self.name + ": EOF exception found" )
677 main.log.error( self.name + ": " + self.handle.before )
678 main.cleanup()
679 main.exit()
680 except pexpect.TIMEOUT:
681 main.log.error( self.name + ": TIMEOUT exception found" )
682 main.log.error( self.name + ": " + self.handle.before )
683 main.cleanup()
684 main.exit()
685 except Exception:
686 main.log.exception( self.name + ": Uncaught exception!" )
687 main.cleanup()
688 main.exit()
pingping-lin6d23d9e2015-02-02 16:54:24 -0800689
kelvin-onlabd3b64892015-01-20 13:26:24 -0800690 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800691 """
Jon Hall274b6642015-02-17 11:57:17 -0800692 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800693 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800694 """
Jon Hall45ec0922014-10-10 19:33:49 -0400695 try:
kelvin8ec71442015-01-15 16:57:00 -0800696 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700697 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800698 self.handle.sendline(
699 "cd " +
700 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800701 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
702 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800703 # NOTE: for some reason there are backspaces inserted in this
704 # phrase when run from Jenkins on some tests
705 self.handle.expect( "never" )
Devin Limc20e79a2017-06-07 10:29:57 -0700706 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800707 response = ( self.name + ": \n" + str(
708 self.handle.before + self.handle.after ) )
709 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700710 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800711 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400712 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500713 print line
714 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700715 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800716 for line in lines[ 2:-1 ]:
717 # Bracket replacement is for Wiki-compliant
718 # formatting. '<' or '>' are interpreted
719 # as xml specific tags that cause errors
720 line = line.replace( "<", "[" )
721 line = line.replace( ">", "]" )
pingping-lin763ee042015-05-20 17:45:30 -0700722 #main.log.wiki( "\t" + line )
723 main.log.wiki( line + "<br /> " )
724 main.log.summary( line )
725 main.log.wiki( "</blockquote>" )
726 main.log.summary("\n")
kelvin8ec71442015-01-15 16:57:00 -0800727 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400728 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800729 main.log.error( self.name + ": EOF exception found" )
730 main.log.error( self.name + ": " + self.handle.before )
Jon Hall45ec0922014-10-10 19:33:49 -0400731 main.cleanup()
732 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800733 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800734 main.log.error( self.name + ": TIMEOUT exception found" )
735 main.log.error( self.name + ": " + self.handle.before )
Jon Hall368769f2014-11-19 15:43:35 -0800736 main.cleanup()
737 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800738 except Exception:
739 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall45ec0922014-10-10 19:33:49 -0400740 main.cleanup()
741 main.exit()
742
kelvin-onlabd3b64892015-01-20 13:26:24 -0800743 def createCellFile( self, benchIp, fileName, mnIpAddrs,
Jon Hall810b67d2016-12-05 10:19:01 -0800744 appString, onosIpAddrs, onosUser="sdn", useSSH=True ):
kelvin8ec71442015-01-15 16:57:00 -0800745 """
andrewonlab94282092014-10-10 13:00:11 -0400746 Creates a cell file based on arguments
747 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800748 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400749 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800750 * File name of the cell file ( fileName )
751 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800752 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400753 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800754 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400755 - Must be passed in as last arguments
Flavio Castrocc38a542016-03-03 13:15:46 -0800756 * ONOS USER (onosUser)
757 - optional argument to set ONOS_USER environment variable
kelvin8ec71442015-01-15 16:57:00 -0800758
andrewonlab94282092014-10-10 13:00:11 -0400759 NOTE: Assumes cells are located at:
760 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800761 """
andrewonlab94282092014-10-10 13:00:11 -0400762 try:
Devin Lim461f0872017-06-05 16:49:33 -0700763
Jon Hall2c8959e2016-12-16 12:17:34 -0800764 # Variable initialization
765 cellDirectory = self.home + "/tools/test/cells/"
766 # We want to create the cell file in the dependencies directory
767 # of TestON first, then copy over to ONOS bench
768 tempDirectory = "/tmp/"
769 # Create the cell file in the directory for writing ( w+ )
770 cellFile = open( tempDirectory + fileName, 'w+' )
771 if isinstance( onosIpAddrs, types.StringType ):
772 onosIpAddrs = [ onosIpAddrs ]
773
774 # App string is hardcoded environment variables
775 # That you may wish to use by default on startup.
776 # Note that you may not want certain apps listed
777 # on here.
778 appString = "export ONOS_APPS=" + appString
779 onosGroup = "export ONOS_GROUP=" + onosUser
780 onosUser = "export ONOS_USER=" + onosUser
781 if useSSH:
782 onosUseSSH = "export ONOS_USE_SSH=true"
783 mnString = "export OCN="
784 if mnIpAddrs == "":
785 mnString = ""
786 onosString = "export OC"
787 tempCount = 1
788
789 # Create ONOSNIC ip address prefix
790 tempOnosIp = str( onosIpAddrs[ 0 ] )
791 tempList = []
792 tempList = tempOnosIp.split( "." )
793 # Omit last element of list to format for NIC
794 tempList = tempList[ :-1 ]
795 # Structure the nic string ip
796 nicAddr = ".".join( tempList ) + ".*"
797 self.nicAddr = nicAddr
798 onosNicString = "export ONOS_NIC=" + nicAddr
799
kelvin8ec71442015-01-15 16:57:00 -0800800 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800801 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400802
kelvin-onlabd3b64892015-01-20 13:26:24 -0800803 for arg in onosIpAddrs:
804 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800805 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400806 # export OC1="10.128.20.11"
807 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800808 cellFile.write( onosString + str( tempCount ) +
Jon Hall6f665652015-09-18 10:08:07 -0700809 "=\"" + arg + "\"\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800810 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800811
Jon Hall6f665652015-09-18 10:08:07 -0700812 cellFile.write( "export OCI=$OC1\n" )
813 cellFile.write( mnString + "\"" + mnIpAddrs + "\"\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700814 cellFile.write( appString + "\n" )
Flavio Castrocc38a542016-03-03 13:15:46 -0800815 cellFile.write( onosGroup + "\n" )
816 cellFile.write( onosUser + "\n" )
Pier88189b62016-09-07 17:01:53 -0700817 if useSSH:
818 cellFile.write( onosUseSSH + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800819 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400820
kelvin8ec71442015-01-15 16:57:00 -0800821 # We use os.system to send the command to TestON cluster
822 # to account for the case in which TestON is not located
823 # on the same cluster as the ONOS bench
824 # Note that even if TestON is located on the same cluster
825 # as ONOS bench, you must setup passwordless ssh
826 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabc2b79102015-07-14 11:41:20 -0700827 os.system( "scp " + tempDirectory + fileName + " " +
828 self.user_name + "@" + self.ip_address + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400829
andrewonlab2a6c9342014-10-16 13:40:15 -0400830 return main.TRUE
831
andrewonlab94282092014-10-10 13:00:11 -0400832 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800833 main.log.error( self.name + ": EOF exception found" )
834 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -0400835 main.cleanup()
836 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800837 except Exception:
838 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -0400839 main.cleanup()
840 main.exit()
841
kelvin-onlabd3b64892015-01-20 13:26:24 -0800842 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800843 """
andrewonlab95ca1462014-10-09 14:04:24 -0400844 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800845 """
Hari Krishna03f530e2015-07-10 17:28:27 -0700846 import re
andrewonlab95ca1462014-10-09 14:04:24 -0400847 try:
848 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800849 main.log.error( "Must define cellname" )
andrewonlab95ca1462014-10-09 14:04:24 -0400850 main.cleanup()
851 main.exit()
852 else:
kelvin8ec71442015-01-15 16:57:00 -0800853 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800854 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800855 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400856 # and that this driver will have to change accordingly
Jon Hall3b489db2015-10-05 14:38:37 -0700857 self.handle.expect( str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800858 handleBefore = self.handle.before
859 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800860 # Get the rest of the handle
Devin Limc20e79a2017-06-07 10:29:57 -0700861 self.handle.expect( self.prompt )
Jon Hall439c8912016-04-15 02:22:03 -0700862 time.sleep(10)
kelvin-onlabd3b64892015-01-20 13:26:24 -0800863 handleMore = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400864
Hari Krishna03f530e2015-07-10 17:28:27 -0700865 cell_result = handleBefore + handleAfter + handleMore
suibin zhang116647a2016-05-06 16:30:09 -0700866 #print cell_result
Hari Krishna03f530e2015-07-10 17:28:27 -0700867 if( re.search( "No such cell", cell_result ) ):
868 main.log.error( "Cell call returned: " + handleBefore +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800869 handleAfter + handleMore )
Hari Krishna03f530e2015-07-10 17:28:27 -0700870 main.cleanup()
871 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400872 return main.TRUE
andrewonlab95ca1462014-10-09 14:04:24 -0400873 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800874 main.log.error( self.name + ": EOF exception found" )
875 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ca1462014-10-09 14:04:24 -0400876 main.cleanup()
877 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800878 except Exception:
879 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ca1462014-10-09 14:04:24 -0400880 main.cleanup()
881 main.exit()
882
kelvin-onlabd3b64892015-01-20 13:26:24 -0800883 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800884 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400885 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800886 """
887 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400888
andrewonlabc03bf6c2014-10-09 14:56:18 -0400889 try:
kelvin8ec71442015-01-15 16:57:00 -0800890 # Clean handle by sending empty and expecting $
891 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700892 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800893 self.handle.sendline( "onos-verify-cell" )
Devin Limc20e79a2017-06-07 10:29:57 -0700894 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800895 handleBefore = self.handle.before
896 handleAfter = self.handle.after
kelvin-onlabd3b64892015-01-20 13:26:24 -0800897 main.log.info( "Verify cell returned: " + handleBefore +
Jon Hall3b489db2015-10-05 14:38:37 -0700898 handleAfter )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400899 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800900 except pexpect.ExceptionPexpect as e:
Jon Hall3b489db2015-10-05 14:38:37 -0700901 main.log.exception( self.name + ": Pexpect exception found: " )
kelvin8ec71442015-01-15 16:57:00 -0800902 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -0400903 main.cleanup()
904 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800905 except Exception:
906 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -0400907 main.cleanup()
908 main.exit()
909
jenkins1e99e7b2015-04-02 18:15:39 -0700910 def onosCfgSet( self, ONOSIp, configName, configParam ):
911 """
912 Uses 'onos <node-ip> cfg set' to change a parameter value of an
Jon Hall4ba53f02015-07-29 13:07:41 -0700913 application.
914
jenkins1e99e7b2015-04-02 18:15:39 -0700915 ex)
916 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
jenkins1e99e7b2015-04-02 18:15:39 -0700917 ONOSIp = '10.0.0.1'
918 configName = 'org.onosproject.myapp'
919 configParam = 'appSetting 1'
jenkins1e99e7b2015-04-02 18:15:39 -0700920 """
Jon Hall72280bc2016-01-25 14:29:05 -0800921 try:
922 cfgStr = "onos {} cfg set {} {}".format( ONOSIp,
923 configName,
924 configParam )
925 self.handle.sendline( "" )
926 self.handle.expect( ":~" )
927 self.handle.sendline( cfgStr )
928 self.handle.expect("cfg set")
929 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700930
Jon Hall72280bc2016-01-25 14:29:05 -0800931 paramValue = configParam.split(" ")[1]
932 paramName = configParam.split(" ")[0]
Jon Hall4ba53f02015-07-29 13:07:41 -0700933
Jon Hall72280bc2016-01-25 14:29:05 -0800934 checkStr = 'onos {} cfg get " {} {} " '.format( ONOSIp, configName, paramName )
Jon Hall4ba53f02015-07-29 13:07:41 -0700935
Jon Hall72280bc2016-01-25 14:29:05 -0800936 self.handle.sendline( checkStr )
937 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700938
Jon Hall72280bc2016-01-25 14:29:05 -0800939 if "value=" + paramValue + "," in self.handle.before:
940 main.log.info("cfg " + configName + " successfully set to " + configParam)
941 return main.TRUE
942 except pexpect.ExceptionPexpect as e:
943 main.log.exception( self.name + ": Pexpect exception found: " )
944 main.log.error( self.name + ": " + self.handle.before )
945 main.cleanup()
946 main.exit()
947 except Exception:
948 main.log.exception( self.name + ": Uncaught exception!" )
949 main.cleanup()
950 main.exit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700951
kelvin-onlabd3b64892015-01-20 13:26:24 -0800952 def onosCli( self, ONOSIp, cmdstr ):
kelvin8ec71442015-01-15 16:57:00 -0800953 """
andrewonlab05e362f2014-10-10 00:40:57 -0400954 Uses 'onos' command to send various ONOS CLI arguments.
955 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800956 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400957 * cmdstr: specify the command string to send
kelvin8ec71442015-01-15 16:57:00 -0800958
959 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400960 CLI commands for ONOS. Try to use this function first
961 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800962 function.
963 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400964 by starting onos, and typing in 'onos' to enter the
965 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800966 available commands.
967 """
andrewonlab05e362f2014-10-10 00:40:57 -0400968 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800969 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800970 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400971 return main.FALSE
972 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800973 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400974 return main.FALSE
975
kelvin8ec71442015-01-15 16:57:00 -0800976 cmdstr = str( cmdstr )
977 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700978 self.handle.expect( self.prompt )
andrewonlab05e362f2014-10-10 00:40:57 -0400979
kelvin-onlabd3b64892015-01-20 13:26:24 -0800980 self.handle.sendline( "onos -w " + ONOSIp + " " + cmdstr )
Devin Limc20e79a2017-06-07 10:29:57 -0700981 self.handle.expect( self.prompt )
andrewonlab05e362f2014-10-10 00:40:57 -0400982
kelvin-onlabd3b64892015-01-20 13:26:24 -0800983 handleBefore = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800984 main.log.info( "Command sent successfully" )
kelvin8ec71442015-01-15 16:57:00 -0800985 # Obtain return handle that consists of result from
986 # the onos command. The string may need to be
987 # configured further.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800988 returnString = handleBefore
kelvin-onlabd3b64892015-01-20 13:26:24 -0800989 return returnString
andrewonlab05e362f2014-10-10 00:40:57 -0400990 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800991 main.log.error( self.name + ": EOF exception found" )
992 main.log.error( self.name + ": " + self.handle.before )
andrewonlab05e362f2014-10-10 00:40:57 -0400993 main.cleanup()
994 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800995 except Exception:
996 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab05e362f2014-10-10 00:40:57 -0400997 main.cleanup()
998 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400999
Pier88189b62016-09-07 17:01:53 -07001000 def onosSecureSSH( self, userName="onos", userPWD="rocks", node=""):
1001 """
1002 Enables secure access to ONOS console
1003 by removing default users & keys.
1004
1005 onos-secure-ssh -u onos -p rocks node
1006
1007 Returns: main.TRUE on success and main.FALSE on failure
1008 """
1009
1010 try:
Chiyu Chengef109502016-11-21 15:51:38 -08001011 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001012 self.handle.expect( self.prompt )
Pier88189b62016-09-07 17:01:53 -07001013 self.handle.sendline( " onos-secure-ssh -u " + userName + " -p " + userPWD + " " + node )
1014
1015 # NOTE: this timeout may need to change depending on the network
1016 # and size of ONOS
1017 # TODO: Handle the other possible error
1018 i = self.handle.expect([ "Network\sis\sunreachable",
Devin Limc20e79a2017-06-07 10:29:57 -07001019 self.prompt,
Pier88189b62016-09-07 17:01:53 -07001020 pexpect.TIMEOUT ], timeout=180 )
1021 if i == 0:
1022 # can't reach ONOS node
1023 main.log.warn( "Network is unreachable" )
Devin Limc20e79a2017-06-07 10:29:57 -07001024 self.handle.expect( self.prompt )
Pier88189b62016-09-07 17:01:53 -07001025 return main.FALSE
1026 elif i == 1:
1027 # Process started
1028 main.log.info(
1029 "Secure SSH performed on " +
1030 node)
1031 return main.TRUE
1032 except pexpect.EOF:
1033 main.log.error( self.name + ": EOF exception found" )
1034 main.log.error( self.name + ": " + self.handle.before )
1035 main.cleanup()
1036 main.exit()
1037 except Exception:
1038 main.log.exception( self.name + ": Uncaught exception!" )
1039 main.cleanup()
1040 main.exit()
1041
1042
kelvin-onlabd3b64892015-01-20 13:26:24 -08001043 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001044 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001045 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -08001046 If -f option is provided, it also forces an uninstall.
1047 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -04001048 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -08001049 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -04001050 files to certain onos nodes
1051
1052 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -08001053 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001054 try:
andrewonlab114768a2014-11-14 12:44:44 -05001055 if options:
kelvin8ec71442015-01-15 16:57:00 -08001056 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -05001057 else:
kelvin8ec71442015-01-15 16:57:00 -08001058 self.handle.sendline( "onos-install " + node )
1059 self.handle.expect( "onos-install " )
1060 # NOTE: this timeout may need to change depending on the network
1061 # and size of ONOS
1062 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001063 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -08001064 "ONOS\sis\salready\sinstalled",
Jeremyc72b2582016-02-26 18:27:38 -08001065 "already\sup-to-date",
Jon Hall3576f572016-08-23 10:01:07 -07001066 "does not exist",
Devin Limc20e79a2017-06-07 10:29:57 -07001067 self.prompt,
Jon Hall6c44c0b2016-04-20 15:21:00 -07001068 pexpect.TIMEOUT ], timeout=180 )
Jon Hall7993bfc2014-10-09 16:30:14 -04001069 if i == 0:
Jon Hall3576f572016-08-23 10:01:07 -07001070 # can't reach ONOS node
kelvin8ec71442015-01-15 16:57:00 -08001071 main.log.warn( "Network is unreachable" )
Devin Limc20e79a2017-06-07 10:29:57 -07001072 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001073 return main.FALSE
1074 elif i == 1:
Jon Hall3576f572016-08-23 10:01:07 -07001075 # Process started
kelvin8ec71442015-01-15 16:57:00 -08001076 main.log.info(
1077 "ONOS was installed on " +
1078 node +
1079 " and started" )
Devin Limc20e79a2017-06-07 10:29:57 -07001080 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001081 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001082 elif i == 2 or i == 3:
1083 # same bits are already on ONOS node
Jeremyc72b2582016-02-26 18:27:38 -08001084 main.log.info( "ONOS is already installed on " + node )
Devin Limc20e79a2017-06-07 10:29:57 -07001085 self.handle.expect( self.prompt )
Jeremyc72b2582016-02-26 18:27:38 -08001086 return main.TRUE
1087 elif i == 4:
Jon Hall3576f572016-08-23 10:01:07 -07001088 # onos not packaged
1089 main.log.error( "ONOS package not found." )
Devin Limc20e79a2017-06-07 10:29:57 -07001090 self.handle.expect( self.prompt )
Jon Hall3576f572016-08-23 10:01:07 -07001091 return main.FALSE
1092 elif i == 5:
1093 # prompt
Jeremyc72b2582016-02-26 18:27:38 -08001094 main.log.info( "ONOS was installed on " + node )
1095 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001096 elif i == 6:
1097 # timeout
kelvin8ec71442015-01-15 16:57:00 -08001098 main.log.info(
1099 "Installation of ONOS on " +
1100 node +
1101 " timed out" )
Devin Limc20e79a2017-06-07 10:29:57 -07001102 self.handle.expect( self.prompt )
Jon Hall53c5e662016-04-13 16:06:56 -07001103 main.log.warn( self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001104 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -04001105 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001106 main.log.error( self.name + ": EOF exception found" )
1107 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc03bf6c2014-10-09 14:56:18 -04001108 main.cleanup()
1109 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001110 except Exception:
1111 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc03bf6c2014-10-09 14:56:18 -04001112 main.cleanup()
1113 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -04001114
kelvin-onlabd3b64892015-01-20 13:26:24 -08001115 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001116 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001117 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001118 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001119 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001120 try:
kelvin8ec71442015-01-15 16:57:00 -08001121 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001122 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001123 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001124 " start" )
1125 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -04001126 "Job\sis\salready\srunning",
1127 "start/running",
Devin Limc20e79a2017-06-07 10:29:57 -07001128 self.prompt,
andrewonlab8d0d7d72014-10-09 16:33:15 -04001129 "Unknown\sinstance",
Jeremy Songster14c13572016-04-21 17:34:03 -07001130 pexpect.TIMEOUT ], timeout=180 )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001131 if i == 0:
Devin Limc20e79a2017-06-07 10:29:57 -07001132 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001133 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001134 return main.TRUE
1135 elif i == 1:
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 started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001138 return main.TRUE
Jeremyd0e9a6d2016-03-02 11:28:52 -08001139 elif i == 2:
1140 main.log.info( "ONOS service started" )
1141 return main.TRUE
andrewonlab8d0d7d72014-10-09 16:33:15 -04001142 else:
Devin Limc20e79a2017-06-07 10:29:57 -07001143 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001144 main.log.error( "ONOS service failed to start" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001145 main.cleanup()
1146 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001147 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001148 main.log.error( self.name + ": EOF exception found" )
1149 main.log.error( self.name + ": " + self.handle.before )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001150 main.cleanup()
1151 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001152 except Exception:
1153 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001154 main.cleanup()
1155 main.exit()
1156
kelvin-onlabd3b64892015-01-20 13:26:24 -08001157 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001158 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001159 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001160 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001161 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001162 try:
kelvin8ec71442015-01-15 16:57:00 -08001163 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001164 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001165 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001166 " stop" )
1167 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -04001168 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -07001169 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -04001170 "Unknown\sinstance",
Devin Limc20e79a2017-06-07 10:29:57 -07001171 self.prompt,
Jeremy Songster14c13572016-04-21 17:34:03 -07001172 pexpect.TIMEOUT ], timeout=180 )
andrewonlab2b30bd32014-10-09 16:48:55 -04001173 if i == 0:
Devin Limc20e79a2017-06-07 10:29:57 -07001174 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001175 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001176 return main.TRUE
1177 elif i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001178 self.handle.expect( self.prompt )
Jon Hall65844a32015-03-09 19:09:37 -07001179 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001180 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -04001181 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -07001182 elif i == 2:
Devin Limc20e79a2017-06-07 10:29:57 -07001183 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -07001184 main.log.warn( "ONOS wasn't running" )
1185 return main.TRUE
YPZhang77badfc2016-03-09 10:28:59 -08001186 elif i == 3:
1187 main.log.info( "ONOS service stopped" )
1188 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -04001189 else:
kelvin8ec71442015-01-15 16:57:00 -08001190 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001191 return main.FALSE
andrewonlab2b30bd32014-10-09 16:48:55 -04001192 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001193 main.log.error( self.name + ": EOF exception found" )
1194 main.log.error( self.name + ": " + self.handle.before )
andrewonlab2b30bd32014-10-09 16:48:55 -04001195 main.cleanup()
1196 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001197 except Exception:
1198 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001199 main.cleanup()
1200 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001201
kelvin-onlabd3b64892015-01-20 13:26:24 -08001202 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -08001203 """
andrewonlabc8d47972014-10-09 16:52:36 -04001204 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -08001205 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -04001206 if needed
kelvin8ec71442015-01-15 16:57:00 -08001207 """
andrewonlabc8d47972014-10-09 16:52:36 -04001208 try:
kelvin8ec71442015-01-15 16:57:00 -08001209 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001210 self.handle.expect( self.prompt, timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001211 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
Devin Limc20e79a2017-06-07 10:29:57 -07001212 self.handle.expect( self.prompt, timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001213 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
kelvin8ec71442015-01-15 16:57:00 -08001214 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -04001215 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -07001216 except pexpect.TIMEOUT:
1217 main.log.exception( self.name + ": Timeout in onosUninstall" )
1218 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -04001219 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001220 main.log.error( self.name + ": EOF exception found" )
1221 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc8d47972014-10-09 16:52:36 -04001222 main.cleanup()
1223 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001224 except Exception:
1225 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc8d47972014-10-09 16:52:36 -04001226 main.cleanup()
1227 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -04001228
kelvin-onlabd3b64892015-01-20 13:26:24 -08001229 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001230 """
andrewonlabaedc8332014-12-04 12:43:03 -05001231 Issues the command 'onos-die <node-ip>'
1232 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -08001233 """
andrewonlabaedc8332014-12-04 12:43:03 -05001234 try:
kelvin8ec71442015-01-15 16:57:00 -08001235 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001236 self.handle.expect( self.prompt )
Jeremyf0aecdb2016-03-30 13:19:57 -07001237 cmdStr = "onos-die " + str( nodeIp )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001238 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001239 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001240 "Killing\sONOS",
1241 "ONOS\sprocess\sis\snot\srunning",
Jeremy Songster14c13572016-04-21 17:34:03 -07001242 pexpect.TIMEOUT ], timeout=60 )
andrewonlabaedc8332014-12-04 12:43:03 -05001243 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001244 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001245 " was killed and stopped" )
Jon Hall53c5e662016-04-13 16:06:56 -07001246 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001247 self.handle.expect( self.prompt )
andrewonlabaedc8332014-12-04 12:43:03 -05001248 return main.TRUE
1249 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001250 main.log.info( "ONOS process was not running" )
Jon Hall53c5e662016-04-13 16:06:56 -07001251 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001252 self.handle.expect( self.prompt )
andrewonlabaedc8332014-12-04 12:43:03 -05001253 return main.FALSE
1254 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 )
andrewonlabaedc8332014-12-04 12:43:03 -05001257 main.cleanup()
1258 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001259 except Exception:
1260 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabaedc8332014-12-04 12:43:03 -05001261 main.cleanup()
1262 main.exit()
1263
kelvin-onlabd3b64892015-01-20 13:26:24 -08001264 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001265 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001266 Calls the command: 'onos-kill [<node-ip>]'
1267 "Remotely, and unceremoniously kills the ONOS instance running on
1268 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001269 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001270 try:
kelvin8ec71442015-01-15 16:57:00 -08001271 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001272 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001273 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001274 i = self.handle.expect( [
Devin Limc20e79a2017-06-07 10:29:57 -07001275 self.prompt,
andrewonlabe8e56fd2014-10-09 17:12:44 -04001276 "No\sroute\sto\shost",
1277 "password:",
Jeremy Songster14c13572016-04-21 17:34:03 -07001278 pexpect.TIMEOUT ], timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -08001279
andrewonlabe8e56fd2014-10-09 17:12:44 -04001280 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001281 main.log.info(
1282 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001283 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001284 return main.TRUE
1285 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001286 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001287 return main.FALSE
1288 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001289 main.log.info(
1290 "Passwordless login for host: " +
1291 str( nodeIp ) +
1292 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001293 return main.FALSE
1294 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001295 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001296 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001297
andrewonlabe8e56fd2014-10-09 17:12:44 -04001298 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001299 main.log.error( self.name + ": EOF exception found" )
1300 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001301 main.cleanup()
1302 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001303 except Exception:
1304 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001305 main.cleanup()
1306 main.exit()
1307
kelvin-onlabd3b64892015-01-20 13:26:24 -08001308 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001309 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001310 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001311 a cleaner environment.
1312
andrewonlab19fbdca2014-11-14 12:55:59 -05001313 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001314 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001315 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001316 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001317 try:
kelvin8ec71442015-01-15 16:57:00 -08001318 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001319 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001320 self.handle.sendline( "onos-remove-raft-logs" )
1321 # Sometimes this command hangs
Devin Limc20e79a2017-06-07 10:29:57 -07001322 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
kelvin8ec71442015-01-15 16:57:00 -08001323 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001324 if i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001325 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
kelvin8ec71442015-01-15 16:57:00 -08001326 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001327 if i == 1:
1328 return main.FALSE
andrewonlab19fbdca2014-11-14 12:55:59 -05001329 return main.TRUE
andrewonlab19fbdca2014-11-14 12:55:59 -05001330 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001331 main.log.error( self.name + ": EOF exception found" )
1332 main.log.error( self.name + ": " + self.handle.before )
andrewonlab19fbdca2014-11-14 12:55:59 -05001333 main.cleanup()
1334 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001335 except Exception:
1336 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab19fbdca2014-11-14 12:55:59 -05001337 main.cleanup()
1338 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -05001339
kelvin-onlabd3b64892015-01-20 13:26:24 -08001340 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001341 """
1342 Calls the command 'onos-start-network [ <mininet-topo> ]
1343 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001344 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001345 cell."
andrewonlab94282092014-10-10 13:00:11 -04001346 * Specify mininet topology file name for mntopo
1347 * Topo files should be placed at:
1348 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001349
andrewonlab94282092014-10-10 13:00:11 -04001350 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001351 """
andrewonlab94282092014-10-10 13:00:11 -04001352 try:
1353 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001354 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001355 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001356
kelvin8ec71442015-01-15 16:57:00 -08001357 mntopo = str( mntopo )
1358 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001359 self.handle.expect( self.prompt )
andrewonlab94282092014-10-10 13:00:11 -04001360
kelvin8ec71442015-01-15 16:57:00 -08001361 self.handle.sendline( "onos-start-network " + mntopo )
1362 self.handle.expect( "mininet>" )
1363 main.log.info( "Network started, entered mininet prompt" )
1364
1365 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001366
1367 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001368 main.log.error( self.name + ": EOF exception found" )
1369 main.log.error( self.name + ": " + self.handle.before )
andrewonlab94282092014-10-10 13:00:11 -04001370 main.cleanup()
1371 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001372 except Exception:
1373 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab94282092014-10-10 13:00:11 -04001374 main.cleanup()
1375 main.exit()
1376
Jeremy Songster14c13572016-04-21 17:34:03 -07001377 def isup( self, node="", timeout=240 ):
kelvin8ec71442015-01-15 16:57:00 -08001378 """
1379 Run's onos-wait-for-start which only returns once ONOS is at run
Cameron Franke9c94fb02015-01-21 10:20:20 -08001380 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001381
Jon Hall7993bfc2014-10-09 16:30:14 -04001382 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001383 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001384 try:
Jon Hall3b489db2015-10-05 14:38:37 -07001385 self.handle.sendline( "onos-wait-for-start " + node )
1386 self.handle.expect( "onos-wait-for-start" )
kelvin8ec71442015-01-15 16:57:00 -08001387 # NOTE: this timeout is arbitrary"
Devin Limc20e79a2017-06-07 10:29:57 -07001388 i = self.handle.expect([self.prompt, pexpect.TIMEOUT], timeout)
Jon Hall7993bfc2014-10-09 16:30:14 -04001389 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001390 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001391 return main.TRUE
1392 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001393 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001394 # we will kill it on timeout
kelvin8ec71442015-01-15 16:57:00 -08001395 main.log.error( "ONOS has not started yet" )
1396 self.handle.send( "\x03" ) # Control-C
Devin Limc20e79a2017-06-07 10:29:57 -07001397 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001398 return main.FALSE
1399 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001400 main.log.error( self.name + ": EOF exception found" )
1401 main.log.error( self.name + ": " + self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001402 main.cleanup()
1403 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001404 except Exception:
1405 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001406 main.cleanup()
1407 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001408
kelvin-onlabd3b64892015-01-20 13:26:24 -08001409 def pushTestIntentsShell(
1410 self,
1411 dpidSrc,
1412 dpidDst,
1413 numIntents,
1414 dirFile,
1415 onosIp,
1416 numMult="",
1417 appId="",
1418 report=True,
1419 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001420 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001421 Description:
kelvin8ec71442015-01-15 16:57:00 -08001422 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001423 better parallelize the results than the CLI
1424 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001425 * dpidSrc: specify source dpid
1426 * dpidDst: specify destination dpid
1427 * numIntents: specify number of intents to push
1428 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001429 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001430 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001431 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001432 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001433 """
1434 try:
1435 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001436 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001437 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001438 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001439 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001440 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001441
kelvin-onlabd3b64892015-01-20 13:26:24 -08001442 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1443 if not numMult:
1444 addIntents = addDpid + " " + str( numIntents )
1445 elif numMult:
1446 addIntents = addDpid + " " + str( numIntents ) + " " +\
1447 str( numMult )
1448 if appId:
1449 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001450 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001451 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001452
andrewonlabaedc8332014-12-04 12:43:03 -05001453 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001454 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001455 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001456 sendCmd = addApp + " &"
1457 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001458
kelvin-onlabd3b64892015-01-20 13:26:24 -08001459 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001460
1461 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001462 main.log.error( self.name + ": EOF exception found" )
1463 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001464 main.cleanup()
1465 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001466 except Exception:
1467 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001468 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001469 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001470
kelvin-onlabd3b64892015-01-20 13:26:24 -08001471 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001472 """
andrewonlab970399c2014-11-07 13:09:32 -05001473 Capture all packet activity and store in specified
1474 directory/file
1475
1476 Required:
1477 * interface: interface to capture
1478 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001479 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001480 try:
1481 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001482 self.handle.expect( self.prompt )
andrewonlab970399c2014-11-07 13:09:32 -05001483
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001484 self.handle.sendline( "tshark -i " + str( interface ) + " -t e -w " + str( dirFile ) + " &" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001485 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001486 self.handle.expect( "Capturing on" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001487 self.handle.sendline( "\n" )
Devin Limc20e79a2017-06-07 10:29:57 -07001488 self.handle.expect( self.prompt )
andrewonlab970399c2014-11-07 13:09:32 -05001489
Jon Hallfebb1c72015-03-05 13:30:09 -08001490 main.log.info( "Tshark started capturing files on " +
1491 str( interface ) + " and saving to directory: " +
1492 str( dirFile ) )
1493 except pexpect.EOF:
1494 main.log.error( self.name + ": EOF exception found" )
1495 main.log.error( self.name + ": " + self.handle.before )
1496 main.cleanup()
1497 main.exit()
1498 except Exception:
1499 main.log.exception( self.name + ": Uncaught exception!" )
1500 main.cleanup()
1501 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001502
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001503 def onosTopoCfg( self, onosIp, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001504 """
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001505 Description:
1506 Execute onos-topo-cfg command
1507 Required:
1508 onosIp - IP of the onos node you want to send the json to
1509 jsonFile - File path of the json file
1510 Return:
1511 Returns main.TRUE if the command is successfull; Returns
1512 main.FALSE if there was an error
kelvin8ec71442015-01-15 16:57:00 -08001513 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001514 try:
kelvin8ec71442015-01-15 16:57:00 -08001515 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001516 self.handle.expect( self.prompt )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001517 cmd = "onos-topo-cfg "
1518 self.handle.sendline( cmd + str( onosIp ) + " " + jsonFile )
1519 handle = self.handle.before
1520 print handle
1521 if "Error" in handle:
1522 main.log.error( self.name + ": " + self.handle.before )
1523 return main.FALSE
1524 else:
Devin Limc20e79a2017-06-07 10:29:57 -07001525 self.handle.expect( self.prompt )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001526 return main.TRUE
1527
Jon Hallfebb1c72015-03-05 13:30:09 -08001528 except pexpect.EOF:
1529 main.log.error( self.name + ": EOF exception found" )
1530 main.log.error( self.name + ": " + self.handle.before )
1531 main.cleanup()
1532 main.exit()
1533 except Exception:
1534 main.log.exception( self.name + ": Uncaught exception!" )
1535 main.cleanup()
1536 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001537
jenkins1e99e7b2015-04-02 18:15:39 -07001538 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001539 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001540 Required:
kelvin8ec71442015-01-15 16:57:00 -08001541 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001542 * directory to store results
1543 Optional:
1544 * interface - default: eth0
Jon Hall4ba53f02015-07-29 13:07:41 -07001545 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001546 Description:
1547 Uses tshark command to grep specific group of packets
1548 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001549 The timestamp is hardcoded to be in epoch
1550 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001551 try:
1552 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001553 self.handle.expect( self.prompt )
Jon Hallfebb1c72015-03-05 13:30:09 -08001554 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001555 if grepOptions:
1556 grepStr = "grep "+str(grepOptions)
1557 else:
1558 grepStr = "grep"
Jon Hall4ba53f02015-07-29 13:07:41 -07001559
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001560 cmd = (
1561 "sudo tshark -i " +
Jon Hallfebb1c72015-03-05 13:30:09 -08001562 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001563 " -t e | " +
1564 grepStr + " --line-buffered \"" +
Jon Hallfebb1c72015-03-05 13:30:09 -08001565 str(grep) +
1566 "\" >" +
1567 directory +
1568 " &" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001569 self.handle.sendline(cmd)
1570 main.log.info(cmd)
Jon Hallfebb1c72015-03-05 13:30:09 -08001571 self.handle.expect( "Capturing on" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001572 self.handle.sendline( "\n" )
Devin Limc20e79a2017-06-07 10:29:57 -07001573 self.handle.expect( self.prompt )
Jon Hallfebb1c72015-03-05 13:30:09 -08001574 except pexpect.EOF:
1575 main.log.error( self.name + ": EOF exception found" )
1576 main.log.error( self.name + ": " + self.handle.before )
1577 main.cleanup()
1578 main.exit()
1579 except Exception:
1580 main.log.exception( self.name + ": Uncaught exception!" )
1581 main.cleanup()
1582 main.exit()
1583
kelvin-onlabd3b64892015-01-20 13:26:24 -08001584 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001585 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001586 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001587 """
1588 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001589 try:
1590 self.execute( cmd="sudo rm /tmp/wireshark*" )
1591 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001592 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1593 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001594 self.handle.sendline( "" )
1595 main.log.info( "Tshark stopped" )
1596 except pexpect.EOF:
1597 main.log.error( self.name + ": EOF exception found" )
1598 main.log.error( self.name + ": " + self.handle.before )
1599 main.cleanup()
1600 main.exit()
1601 except Exception:
1602 main.log.exception( self.name + ": Uncaught exception!" )
1603 main.cleanup()
1604 main.exit()
1605
kelvin8ec71442015-01-15 16:57:00 -08001606 def ptpd( self, args ):
1607 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001608 Initiate ptp with user-specified args.
1609 Required:
1610 * args: specify string of args after command
1611 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001612 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001613 try:
kelvin8ec71442015-01-15 16:57:00 -08001614 self.handle.sendline( "sudo ptpd " + str( args ) )
1615 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001616 "Multiple",
1617 "Error",
Devin Limc20e79a2017-06-07 10:29:57 -07001618 self.prompt ] )
1619 self.handle.expect( self.prompt )
andrewonlabba44bcf2014-10-16 16:54:41 -04001620
andrewonlab0c38a4a2014-10-28 18:35:35 -04001621 if i == 0:
1622 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001623 main.log.info( "ptpd returned an error: " +
1624 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001625 return handle
1626 elif i == 1:
1627 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001628 main.log.error( "ptpd returned an error: " +
1629 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001630 return handle
1631 else:
1632 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001633
andrewonlab0c38a4a2014-10-28 18:35:35 -04001634 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001635 main.log.error( self.name + ": EOF exception found" )
1636 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001637 main.cleanup()
1638 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001639 except Exception:
1640 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001641 main.cleanup()
1642 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001643
Pier50f0bc62016-09-07 17:53:40 -07001644 def dumpONOSCmd(self, ONOSIp, CMD, destDir, filename, options=""):
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001645 """
Pier50f0bc62016-09-07 17:53:40 -07001646 Dump Cmd to a desired directory.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001647 For debugging purposes, you may want to use
Pier50f0bc62016-09-07 17:53:40 -07001648 this function to capture Cmd at a given point in time.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001649 Localtime will be attached to the filename
1650
1651 Required:
1652 * ONOSIp: the IP of the target ONOS instance
Pier50f0bc62016-09-07 17:53:40 -07001653 * CMD: the command to dump;
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001654 * destDir: specify directory to copy to.
1655 ex ) /tmp/
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001656 * fileName: Name of the file
Pier50f0bc62016-09-07 17:53:40 -07001657 * options: Options for ONOS command
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001658 """
1659
1660 localtime = time.strftime( '%x %X' )
1661 localtime = localtime.replace( "/", "" )
1662 localtime = localtime.replace( " ", "_" )
1663 localtime = localtime.replace( ":", "" )
1664 if destDir[ -1: ] != "/":
1665 destDir += "/"
Pier50f0bc62016-09-07 17:53:40 -07001666 cmd=CMD + " " + options + " > " + str( destDir ) + str( filename ) + localtime
1667 return self.onosCli(ONOSIp, cmd)
Flavio Castrob7718952016-05-18 08:53:41 -07001668
kelvin-onlabd3b64892015-01-20 13:26:24 -08001669 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001670 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001671 """
1672 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001673 Current implementation of ONOS deletes its karaf
1674 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001675 you may want to use this function to capture
1676 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001677 Localtime will be attached to the filename
1678
1679 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001680 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001681 copy.
kelvin8ec71442015-01-15 16:57:00 -08001682 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001683 For copying multiple files, leave copyFileName
1684 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001685 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001686 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001687 ex ) /tmp/
1688 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001689 * copyFileName: If you want to rename the log
1690 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001691 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001692 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001693 try:
Flavio Castro09ab59d2016-05-25 17:01:35 -07001694 localtime = time.strftime( '%H %M' )
kelvin8ec71442015-01-15 16:57:00 -08001695 localtime = localtime.replace( "/", "" )
1696 localtime = localtime.replace( " ", "_" )
1697 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001698 if destDir[ -1: ] != "/":
1699 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001700
kelvin-onlabd3b64892015-01-20 13:26:24 -08001701 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001702 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1703 str( destDir ) + str( copyFileName ) +
1704 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001705 self.handle.expect( "cp" )
Devin Limc20e79a2017-06-07 10:29:57 -07001706 self.handle.expect( self.prompt )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001707 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001708 self.handle.sendline( "cp " + str( logToCopy ) +
1709 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001710 self.handle.expect( "cp" )
Devin Limc20e79a2017-06-07 10:29:57 -07001711 self.handle.expect( self.prompt )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001712
kelvin8ec71442015-01-15 16:57:00 -08001713 return self.handle.before
1714
1715 except pexpect.EOF:
1716 main.log.error( "Copying files failed" )
1717 main.log.error( self.name + ": EOF exception found" )
1718 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001719 except Exception:
1720 main.log.exception( "Copying files failed" )
1721
Jon Hall16b72c42015-05-20 10:23:36 -07001722 def checkLogs( self, onosIp, restart=False):
kelvin8ec71442015-01-15 16:57:00 -08001723 """
Jon Hall94fd0472014-12-08 11:52:42 -08001724 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001725 If restart is True, use the old version of onos-check-logs which
1726 does not print the full stacktrace, but shows the entire log file,
1727 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001728 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001729 """
Jon Hall94fd0472014-12-08 11:52:42 -08001730 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001731 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001732 if restart:
1733 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001734 self.handle.sendline( cmd )
1735 self.handle.expect( cmd )
Devin Limdc78e202017-06-09 18:30:07 -07001736 self.handle.expect( self.prompt + " " )
Jon Hall94fd0472014-12-08 11:52:42 -08001737 response = self.handle.before
1738 return response
1739 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001740 main.log.error( "Lost ssh connection" )
1741 main.log.error( self.name + ": EOF exception found" )
1742 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001743 except Exception:
1744 main.log.exception( self.name + ": Uncaught exception!" )
1745 main.cleanup()
1746 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001747
kelvin-onlabd3b64892015-01-20 13:26:24 -08001748 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001749 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001750 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001751 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001752 try:
kelvin8ec71442015-01-15 16:57:00 -08001753 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001754 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001755 self.handle.sendline( "onos-service " + str( node ) +
1756 " status" )
1757 i = self.handle.expect( [
You Wangef1e6572016-03-08 12:53:18 -08001758 "start/running",
You Wang7bd83062016-03-01 11:50:00 -08001759 "Running ...",
You Wangef1e6572016-03-08 12:53:18 -08001760 "stop/waiting",
You Wang7bd83062016-03-01 11:50:00 -08001761 "Not Running ...",
kelvin8ec71442015-01-15 16:57:00 -08001762 pexpect.TIMEOUT ], timeout=120 )
YPZhangfebf7302016-05-24 16:45:56 -07001763 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001764 self.handle.expect( self.prompt )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001765
You Wangef1e6572016-03-08 12:53:18 -08001766 if i == 0 or i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001767 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001768 return main.TRUE
You Wangef1e6572016-03-08 12:53:18 -08001769 elif i == 2 or i == 3:
kelvin8ec71442015-01-15 16:57:00 -08001770 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001771 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001772 main.cleanup()
1773 main.exit()
1774 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001775 main.log.error( self.name + ": EOF exception found" )
1776 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001777 main.cleanup()
1778 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001779 except Exception:
1780 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001781 main.cleanup()
1782 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001783
Jon Hall63604932015-02-26 17:09:50 -08001784 def setIpTables( self, ip, port='', action='add', packet_type='',
1785 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001786 """
Jon Hall21270ac2015-02-16 17:59:55 -08001787 Description:
1788 add or remove iptables rule to DROP (default) packets from
1789 specific IP and PORT
1790 Usage:
1791 * specify action ('add' or 'remove')
1792 when removing, pass in the same argument as you would add. It will
1793 delete that specific rule.
1794 * specify the ip to block
1795 * specify the destination port to block (defaults to all ports)
1796 * optional packet type to block (default tcp)
1797 * optional iptables rule (default DROP)
1798 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001799 * States boolean toggles adding all supported tcp states to the
1800 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001801 Returns:
1802 main.TRUE on success or
1803 main.FALSE if given invalid input or
1804 main.ERROR if there is an error in response from iptables
1805 WARNING:
1806 * This function uses root privilege iptables command which may result
1807 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001808 """
Jon Hall21270ac2015-02-16 17:59:55 -08001809
1810 # NOTE*********
1811 # The strict checking methods of this driver function is intentional
1812 # to discourage any misuse or error of iptables, which can cause
1813 # severe network errors
1814 # *************
1815
1816 # NOTE: Sleep needed to give some time for rule to be added and
1817 # registered to the instance. If you are calling this function
1818 # multiple times this sleep will prevent any errors.
1819 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001820 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001821 try:
1822 # input validation
1823 action_type = action.lower()
1824 rule = rule.upper()
1825 direction = direction.upper()
1826 if action_type != 'add' and action_type != 'remove':
1827 main.log.error( "Invalid action type. Use 'add' or "
1828 "'remove' table rule" )
1829 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1830 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1831 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1832 "'ACCEPT' or 'LOG' only." )
1833 if direction != 'INPUT' and direction != 'OUTPUT':
1834 # NOTE currently only supports rules INPUT and OUPTUT
1835 main.log.error( "Invalid rule. Valid directions are"
1836 " 'OUTPUT' or 'INPUT'" )
1837 return main.FALSE
1838 return main.FALSE
1839 return main.FALSE
1840 if action_type == 'add':
1841 # -A is the 'append' action of iptables
1842 actionFlag = '-A'
1843 elif action_type == 'remove':
1844 # -D is the 'delete' rule of iptables
1845 actionFlag = '-D'
1846 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001847 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001848 cmd = "sudo iptables " + actionFlag + " " +\
1849 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001850 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001851 # " -p " + str( packet_type ) +\
1852 if packet_type:
1853 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001854 if port:
1855 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001856 if states:
1857 cmd += " -m state --state="
1858 #FIXME- Allow user to configure which states to block
1859 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001860 cmd += " -j " + str( rule )
1861
1862 self.handle.sendline( cmd )
Devin Limc20e79a2017-06-07 10:29:57 -07001863 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001864 main.log.warn( self.handle.before )
1865
1866 info_string = "On " + str( self.name )
1867 info_string += " " + str( action_type )
1868 info_string += " iptable rule [ "
1869 info_string += " IP: " + str( ip )
1870 info_string += " Port: " + str( port )
1871 info_string += " Rule: " + str( rule )
1872 info_string += " Direction: " + str( direction ) + " ]"
1873 main.log.info( info_string )
1874 return main.TRUE
1875 except pexpect.TIMEOUT:
1876 main.log.exception( self.name + ": Timeout exception in "
1877 "setIpTables function" )
1878 return main.ERROR
1879 except pexpect.EOF:
1880 main.log.error( self.name + ": EOF exception found" )
1881 main.log.error( self.name + ": " + self.handle.before )
1882 main.cleanup()
1883 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001884 except Exception:
1885 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001886 main.cleanup()
1887 main.exit()
1888
Jon Hall0468b042015-02-19 19:08:21 -08001889 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001890 """
Jon Hall0468b042015-02-19 19:08:21 -08001891 This method is used by STS to check the status of the controller
1892 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001893 """
Jon Hall0468b042015-02-19 19:08:21 -08001894 import re
1895 try:
1896 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001897 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001898 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -07001899 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001900 self.handle.sendline( "service onos status" )
Devin Limc20e79a2017-06-07 10:29:57 -07001901 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001902 response = self.handle.before
1903 if re.search( "onos start/running", response ):
1904 # onos start/running, process 10457
1905 return 'RUNNING'
1906 # FIXME: Implement this case
1907 # elif re.search( pattern, response ):
1908 # return 'STARTING'
1909 elif re.search( "onos stop/", response ):
1910 # onos stop/waiting
1911 # FIXME handle this differently?: onos stop/pre-stop
1912 return 'STOPPED'
1913 # FIXME: Implement this case
1914 # elif re.search( pattern, response ):
1915 # return 'FROZEN'
1916 else:
1917 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001918 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001919 main.log.warn( response )
1920 return 'ERROR', "Unknown response: %s" % response
1921 except pexpect.TIMEOUT:
1922 main.log.exception( self.name + ": Timeout exception in "
1923 "setIpTables function" )
1924 return 'ERROR', "Pexpect Timeout"
1925 except pexpect.EOF:
1926 main.log.error( self.name + ": EOF exception found" )
1927 main.log.error( self.name + ": " + self.handle.before )
1928 main.cleanup()
1929 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001930 except Exception:
1931 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001932 main.cleanup()
1933 main.exit()
1934
andrew@onlab.us3b087132015-03-11 15:00:08 -07001935 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1936 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001937 Create/formats the LinkGraph.cfg file based on arguments
1938 -only creates a linear topology and connects islands
1939 -evenly distributes devices
andrew@onlab.us3b087132015-03-11 15:00:08 -07001940 -must be called by ONOSbench
1941
Jon Hall4ba53f02015-07-29 13:07:41 -07001942 ONOSIpList - list of all of the node IPs to be used
1943
1944 deviceCount - number of switches to be assigned
andrew@onlab.us3b087132015-03-11 15:00:08 -07001945 '''
Jon Hall6509dbf2016-06-21 17:01:17 -07001946 main.log.info("Creating link graph configuration file." )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001947 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
Jon Hall4ba53f02015-07-29 13:07:41 -07001948 tempFile = "/tmp/linkGraph.cfg"
andrew@onlab.us3b087132015-03-11 15:00:08 -07001949
1950 linkGraph = open(tempFile, 'w+')
1951 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1952 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1953 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001954
andrew@onlab.us3b087132015-03-11 15:00:08 -07001955 clusterCount = len(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07001956
1957 if type(deviceCount) is int or type(deviceCount) is str:
andrew@onlab.us3b087132015-03-11 15:00:08 -07001958 deviceCount = int(deviceCount)
1959 switchList = [0]*(clusterCount+1)
1960 baselineSwitchCount = deviceCount/clusterCount
Jon Hall4ba53f02015-07-29 13:07:41 -07001961
andrew@onlab.us3b087132015-03-11 15:00:08 -07001962 for node in range(1, clusterCount + 1):
1963 switchList[node] = baselineSwitchCount
1964
1965 for node in range(1, (deviceCount%clusterCount)+1):
1966 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001967
andrew@onlab.us3b087132015-03-11 15:00:08 -07001968 if type(deviceCount) is list:
1969 main.log.info("Using provided device distribution")
1970 switchList = [0]
1971 for i in deviceCount:
1972 switchList.append(int(i))
1973
1974 tempList = ['0']
1975 tempList.extend(ONOSIpList)
1976 ONOSIpList = tempList
1977
1978 myPort = 6
1979 lastSwitch = 0
1980 for node in range(1, clusterCount+1):
1981 if switchList[node] == 0:
1982 continue
1983
1984 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001985
andrew@onlab.us3b087132015-03-11 15:00:08 -07001986 if node > 1:
1987 #connect to last device on previous node
Jon Hall4ba53f02015-07-29 13:07:41 -07001988 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
1989 linkGraph.write(line)
1990
1991 lastSwitch = 0
1992 for switch in range (0, switchList[node]-1):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001993 line = ""
1994 line = ("\t" + str(switch) + ":" + str(myPort))
1995 line += " -- "
1996 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
1997 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07001998 lastSwitch = switch+1
andrew@onlab.us3b087132015-03-11 15:00:08 -07001999 lastIp = ONOSIpList[node]
Jon Hall4ba53f02015-07-29 13:07:41 -07002000
andrew@onlab.us3b087132015-03-11 15:00:08 -07002001 #lastSwitch += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07002002 if node < (clusterCount):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002003 #connect to first device on the next node
Jon Hall4ba53f02015-07-29 13:07:41 -07002004 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
andrew@onlab.us3b087132015-03-11 15:00:08 -07002005 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07002006
andrew@onlab.us3b087132015-03-11 15:00:08 -07002007 linkGraph.write("}\n")
2008 linkGraph.close()
2009
2010 #SCP
Jon Hall4ba53f02015-07-29 13:07:41 -07002011 os.system( "scp " + tempFile + " " + self.user_name + "@" + benchIp + ":" + linkGraphPath)
andrew@onlab.us3b087132015-03-11 15:00:08 -07002012 main.log.info("linkGraph.cfg creation complete")
2013
cameron@onlab.us75900962015-03-30 13:22:49 -07002014 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
Jon Hall4ba53f02015-07-29 13:07:41 -07002015
andrew@onlab.us3b087132015-03-11 15:00:08 -07002016 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002017 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
2018 deviceCount = number of switches to distribute, or list of values to use as custom distribution
cameron@onlab.us75900962015-03-30 13:22:49 -07002019 numPorts = number of ports per device. Defaults to 10 both in this function and in ONOS. Optional arg
andrew@onlab.us3b087132015-03-11 15:00:08 -07002020 '''
2021
Jon Hall6509dbf2016-06-21 17:01:17 -07002022 main.log.info("Configuring Null Device Provider" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002023 clusterCount = len(ONOSIpList)
2024
Jon Hall4ba53f02015-07-29 13:07:41 -07002025 try:
2026
cameron@onlab.us75900962015-03-30 13:22:49 -07002027 if type(deviceCount) is int or type(deviceCount) is str:
Jon Hall6509dbf2016-06-21 17:01:17 -07002028 main.log.info("Creating device distribution")
cameron@onlab.us75900962015-03-30 13:22:49 -07002029 deviceCount = int(deviceCount)
2030 switchList = [0]*(clusterCount+1)
2031 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002032
cameron@onlab.us75900962015-03-30 13:22:49 -07002033 for node in range(1, clusterCount + 1):
2034 switchList[node] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002035
cameron@onlab.us75900962015-03-30 13:22:49 -07002036 for node in range(1, (deviceCount%clusterCount)+1):
2037 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07002038
2039 if type(deviceCount) is list:
2040 main.log.info("Using provided device distribution")
2041
2042 if len(deviceCount) == clusterCount:
cameron@onlab.us75900962015-03-30 13:22:49 -07002043 switchList = ['0']
2044 switchList.extend(deviceCount)
Jon Hall4ba53f02015-07-29 13:07:41 -07002045
2046 if len(deviceCount) == (clusterCount + 1):
2047 if deviceCount[0] == '0' or deviceCount[0] == 0:
cameron@onlab.us75900962015-03-30 13:22:49 -07002048 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002049
cameron@onlab.us75900962015-03-30 13:22:49 -07002050 assert len(switchList) == (clusterCount + 1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002051
cameron@onlab.us75900962015-03-30 13:22:49 -07002052 except AssertionError:
Jon Hall4ba53f02015-07-29 13:07:41 -07002053 main.log.error( "Bad device/Ip list match")
cameron@onlab.us75900962015-03-30 13:22:49 -07002054 except TypeError:
2055 main.log.exception( self.name + ": Object not as expected" )
2056 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07002057 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002058 main.log.exception( self.name + ": Uncaught exception!" )
2059 main.cleanup()
2060 main.exit()
2061
andrew@onlab.us3b087132015-03-11 15:00:08 -07002062
2063 ONOSIp = [0]
2064 ONOSIp.extend(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07002065
andrew@onlab.us3b087132015-03-11 15:00:08 -07002066 devicesString = "devConfigs = "
2067 for node in range(1, len(ONOSIp)):
2068 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
2069 if node < clusterCount:
2070 devicesString += (",")
Jon Hall4ba53f02015-07-29 13:07:41 -07002071
2072 try:
cameron@onlab.us75900962015-03-30 13:22:49 -07002073 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
2074 self.handle.expect(":~")
2075 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
2076 self.handle.expect(":~")
andrew@onlab.us3b087132015-03-11 15:00:08 -07002077
cameron@onlab.us75900962015-03-30 13:22:49 -07002078 for i in range(10):
2079 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
2080 self.handle.expect(":~")
2081 verification = self.handle.before
2082 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
2083 break
2084 else:
2085 time.sleep(1)
andrew@onlab.us3b087132015-03-11 15:00:08 -07002086
cameron@onlab.us2cc8bf12015-04-02 14:12:30 -07002087 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002088
cameron@onlab.us75900962015-03-30 13:22:49 -07002089 except AssertionError:
2090 main.log.error("Incorrect Config settings: " + verification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002091 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002092 main.log.exception( self.name + ": Uncaught exception!" )
2093 main.cleanup()
2094 main.exit()
2095
Jon Hall4ba53f02015-07-29 13:07:41 -07002096 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002097 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002098 fileName default is currently the same as the default on ONOS, specify alternate file if
cameron@onlab.us75900962015-03-30 13:22:49 -07002099 you want to use a different topology file than linkGraph.cfg
andrew@onlab.us3b087132015-03-11 15:00:08 -07002100 '''
2101
Jon Hall4ba53f02015-07-29 13:07:41 -07002102
2103 try:
2104 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
2105 self.handle.expect(":~")
cameron@onlab.us75900962015-03-30 13:22:49 -07002106 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2107 self.handle.expect(":~")
Jon Hall4ba53f02015-07-29 13:07:41 -07002108
2109 for i in range(10):
2110 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
cameron@onlab.us75900962015-03-30 13:22:49 -07002111 self.handle.expect(":~")
2112 verification = self.handle.before
Jon Hall4ba53f02015-07-29 13:07:41 -07002113 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002114 break
Jon Hall4ba53f02015-07-29 13:07:41 -07002115 else:
cameron@onlab.us75900962015-03-30 13:22:49 -07002116 time.sleep(1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002117
cameron@onlab.us75900962015-03-30 13:22:49 -07002118 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002119
cameron@onlab.us75900962015-03-30 13:22:49 -07002120 except pexpect.EOF:
2121 main.log.error( self.name + ": EOF exception found" )
2122 main.log.error( self.name + ": " + self.handle.before )
2123 main.cleanup()
2124 main.exit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002125 except AssertionError:
2126 main.log.info("Settings did not post to ONOS")
Jon Hall4ba53f02015-07-29 13:07:41 -07002127 main.log.error(varification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002128 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002129 main.log.exception( self.name + ": Uncaught exception!" )
2130 main.log.error(varification)
2131 main.cleanup()
2132 main.exit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002133
kelvin-onlaba4074292015-07-09 15:19:49 -07002134 def getOnosIps( self ):
2135 """
2136 Get all onos IPs stored in
2137 """
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002138
kelvin-onlaba4074292015-07-09 15:19:49 -07002139 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002140
Chiyu Chengec63bde2016-11-17 18:11:36 -08002141 def listLog( self, nodeIp ):
2142 """
2143 Get a list of all the karaf log names
2144 """
2145 try:
2146 cmd = "onos-ssh " + nodeIp + " ls -tr /opt/onos/log"
2147 self.handle.sendline( cmd )
2148 self.handle.expect( ":~" )
2149 before = self.handle.before.splitlines()
2150 logNames = []
2151 for word in before:
2152 if 'karaf.log' in word:
2153 logNames.append( word )
2154 return logNames
2155 except pexpect.EOF:
2156 main.log.error( self.name + ": EOF exception found" )
2157 main.log.error( self.name + ": " + self.handle.before )
2158 main.cleanup()
2159 main.exit()
2160 except pexpect.TIMEOUT:
2161 main.log.error( self.name + ": TIMEOUT exception found" )
2162 main.log.error( self.name + ": " + self.handle.before )
2163 main.cleanup()
2164 main.exit()
2165 except Exception:
2166 main.log.exception( self.name + ": Uncaught exception!" )
2167 main.cleanup()
2168 main.exit()
2169
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002170 def logReport( self, nodeIp, searchTerms, outputMode="s", startStr=None, endStr=None ):
Jon Hallb4242222016-01-25 17:07:04 -08002171 """
2172 Searches the latest ONOS log file for the given search terms and
2173 prints the total occurances of each term. Returns to combined total of
2174 all occurances.
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002175
Jon Hallb4242222016-01-25 17:07:04 -08002176 Arguments:
2177 * nodeIp - The ip of the ONOS node where the log is located
2178 * searchTerms - A string to grep for or a list of strings to grep
2179 for in the ONOS log. Will print out the number of
2180 occurances for each term.
2181 Optional Arguments:
2182 * outputMode - 's' or 'd'. If 'd' will print the last 5 lines
2183 containing each search term as well as the total
2184 number of occurances of each term. Defaults to 's',
2185 which prints the simple output of just the number
2186 of occurances for each term.
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002187 * startStr - the start string to be given to stream editor command
2188 as the start point for extraction of data
2189 * endStr - the end string to be given to stream editor command as
2190 the end point for extraction of data
Jon Hallb4242222016-01-25 17:07:04 -08002191 """
2192 try:
2193 main.log.info( " Log Report for {} ".format( nodeIp ).center( 70, '=' ) )
2194 if type( searchTerms ) is str:
2195 searchTerms = [searchTerms]
2196 numTerms = len( searchTerms )
2197 outputMode = outputMode.lower()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002198
Jon Hallb4242222016-01-25 17:07:04 -08002199 totalHits = 0
2200 logLines = []
2201 for termIndex in range( numTerms ):
2202 term = searchTerms[termIndex]
2203 logLines.append( [term] )
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002204 if startStr and endStr:
2205 cmd = "onos-ssh {} \"sed -n '/{}/,/{}/p' /opt/onos/log/karaf.log | grep {}\"".format( nodeIp,
2206 startStr,
2207 endStr,
2208 term )
2209 else:
2210 cmd = "onos-ssh {} cat /opt/onos/log/karaf.log | grep {}".format( nodeIp,
2211 term )
Jon Hallb4242222016-01-25 17:07:04 -08002212 self.handle.sendline( cmd )
2213 self.handle.expect( ":~" )
2214 before = self.handle.before.splitlines()
2215 count = 0
2216 for line in before:
2217 if term in line and "grep" not in line:
2218 count += 1
2219 if before.index( line ) > ( len( before ) - 7 ):
2220 logLines[termIndex].append( line )
2221 main.log.info( "{}: {}".format( term, count ) )
2222 totalHits += count
2223 if termIndex == numTerms - 1:
2224 print "\n"
2225 if outputMode != "s":
2226 outputString = ""
2227 for term in logLines:
2228 outputString = term[0] + ": \n"
2229 for line in range( 1, len( term ) ):
2230 outputString += ( "\t" + term[line] + "\n" )
2231 if outputString != ( term[0] + ": \n" ):
2232 main.log.info( outputString )
2233 main.log.info( "=" * 70 )
2234 return totalHits
2235 except pexpect.EOF:
2236 main.log.error( self.name + ": EOF exception found" )
2237 main.log.error( self.name + ": " + self.handle.before )
2238 main.cleanup()
2239 main.exit()
2240 except pexpect.TIMEOUT:
2241 main.log.error( self.name + ": TIMEOUT exception found" )
2242 main.log.error( self.name + ": " + self.handle.before )
2243 main.cleanup()
2244 main.exit()
2245 except Exception:
2246 main.log.exception( self.name + ": Uncaught exception!" )
2247 main.cleanup()
2248 main.exit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002249
2250 def copyMininetFile( self, fileName, localPath, userName, ip,
2251 mnPath='~/mininet/custom/', timeout = 60 ):
2252 """
2253 Description:
2254 Copy mininet topology file from dependency folder in the test folder
2255 and paste it to the mininet machine's mininet/custom folder
2256 Required:
2257 fileName - Name of the topology file to copy
2258 localPath - File path of the mininet topology file
2259 userName - User name of the mininet machine to send the file to
2260 ip - Ip address of the mininet machine
2261 Optional:
2262 mnPath - of the mininet directory to send the file to
2263 Return:
2264 Return main.TRUE if successfully copied the file otherwise
2265 return main.FALSE
2266 """
2267
2268 try:
2269 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2270 str( ip ) + ":" + mnPath + fileName
2271
2272 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07002273 self.handle.expect( self.prompt )
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002274
2275 main.log.info( self.name + ": Execute: " + cmd )
2276
2277 self.handle.sendline( cmd )
2278
2279 i = self.handle.expect( [ 'No such file',
2280 "100%",
2281 pexpect.TIMEOUT ] )
2282
2283 if i == 0:
2284 main.log.error( self.name + ": File " + fileName +
2285 " does not exist!" )
2286 return main.FALSE
2287
2288 if i == 1:
2289 main.log.info( self.name + ": File " + fileName +
2290 " has been copied!" )
2291 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07002292 self.handle.expect( self.prompt )
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002293 return main.TRUE
2294
2295 except pexpect.EOF:
2296 main.log.error( self.name + ": EOF exception found" )
2297 main.log.error( self.name + ": " + self.handle.before )
2298 main.cleanup()
2299 main.exit()
2300 except pexpect.TIMEOUT:
2301 main.log.error( self.name + ": TIMEOUT exception found" )
2302 main.log.error( self.name + ": " + self.handle.before )
2303 main.cleanup()
2304 main.exit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002305
2306 def jvmSet(self, memory=8):
Jon Hall4ba53f02015-07-29 13:07:41 -07002307
cameron@onlab.us78b89652015-07-08 15:21:03 -07002308 import os
2309
2310 homeDir = os.path.expanduser('~')
2311 filename = "/onos/tools/package/bin/onos-service"
2312
2313 serviceConfig = open(homeDir + filename, 'w+')
2314 serviceConfig.write("#!/bin/bash\n ")
2315 serviceConfig.write("#------------------------------------- \n ")
2316 serviceConfig.write("# Starts ONOS Apache Karaf container\n ")
2317 serviceConfig.write("#------------------------------------- \n ")
2318 serviceConfig.write("#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n ")
2319 serviceConfig.write("""export JAVA_OPTS="${JAVA_OPTS:--Xms""" + str(memory) + "G -Xmx" + str(memory) + """G}" \n """)
2320 serviceConfig.write("[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n")
2321 serviceConfig.write("""${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """)
2322 serviceConfig.close()
2323
Jon Hall6c44c0b2016-04-20 15:21:00 -07002324 def createDBFile( self, testData ):
Jon Hall4ba53f02015-07-29 13:07:41 -07002325
cameron@onlab.us78b89652015-07-08 15:21:03 -07002326 filename = main.TEST + "DB"
2327 DBString = ""
Jon Hall4ba53f02015-07-29 13:07:41 -07002328
cameron@onlab.us78b89652015-07-08 15:21:03 -07002329 for item in testData:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002330 if type( item ) is string:
cameron@onlab.us78b89652015-07-08 15:21:03 -07002331 item = "'" + item + "'"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002332 if testData.index( item ) < len( testData - 1 ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002333 item += ","
Jon Hall6c44c0b2016-04-20 15:21:00 -07002334 DBString += str( item )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002335
Jon Hall6c44c0b2016-04-20 15:21:00 -07002336 DBFile = open( filename, "a" )
2337 DBFile.write( DBString )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002338 DBFile.close()
2339
Jon Hall6c44c0b2016-04-20 15:21:00 -07002340 def verifySummary( self, ONOSIp, *deviceCount ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002341
Jon Hall6c44c0b2016-04-20 15:21:00 -07002342 self.handle.sendline( "onos " + ONOSIp + " summary" )
2343 self.handle.expect( ":~" )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002344
2345 summaryStr = self.handle.before
2346 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2347
2348 #passed = "SCC(s)=1" in summaryStr
2349 #if deviceCount:
2350 # passed = "devices=" + str(deviceCount) + "," not in summaryStr
2351
GlennRC772363b2015-08-25 13:05:57 -07002352 passed = False
cameron@onlab.us78b89652015-07-08 15:21:03 -07002353 if "SCC(s)=1," in summaryStr:
2354 passed = True
Jon Hall6c44c0b2016-04-20 15:21:00 -07002355 print "Summary is verifed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002356 else:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002357 print "Summary failed"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002358
2359 if deviceCount:
2360 print" ============================="
Jon Hall6c44c0b2016-04-20 15:21:00 -07002361 checkStr = "devices=" + str( deviceCount[0] ) + ","
cameron@onlab.us78b89652015-07-08 15:21:03 -07002362 print "Checkstr: " + checkStr
2363 if checkStr not in summaryStr:
2364 passed = False
Jon Hall6c44c0b2016-04-20 15:21:00 -07002365 print "Device count failed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002366 else:
2367 print "device count verified"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002368
2369 return passed
Jon Hall6c44c0b2016-04-20 15:21:00 -07002370
Jon Hall8f6d4622016-05-23 15:27:18 -07002371 def getIpAddr( self, iface=None ):
Jon Hall6c44c0b2016-04-20 15:21:00 -07002372 """
2373 Update self.ip_address with numerical ip address. If multiple IP's are
2374 located on the device, will attempt to use self.nicAddr to choose the
2375 right one. Defaults to 127.0.0.1 if no other address is found or cannot
2376 determine the correct address.
2377
2378 ONLY WORKS WITH IPV4 ADDRESSES
2379 """
2380 try:
Jon Hall8f6d4622016-05-23 15:27:18 -07002381 LOCALHOST = "127.0.0.1"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002382 ipPat = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
2383 pattern = re.compile( ipPat )
2384 match = re.search( pattern, self.ip_address )
2385 if self.nicAddr:
2386 nicPat = self.nicAddr.replace( ".", "\." ).replace( "\*", r"\d{1,3}" )
2387 nicPat = re.compile( nicPat )
2388 else:
2389 nicPat = None
2390 # IF self.ip_address is an ip address and matches
2391 # self.nicAddr: return self.ip_address
2392 if match:
2393 curIp = match.group(0)
2394 if nicPat:
2395 nicMatch = re.search( nicPat, curIp )
2396 if nicMatch:
2397 return self.ip_address
Jon Hall8f6d4622016-05-23 15:27:18 -07002398 # ELSE: IF iface, return ip of interface
2399 cmd = "ifconfig"
2400 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2401 if iface:
2402 cmd += " " + str( iface )
2403 raw = subprocess.check_output( cmd.split() )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002404 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2405 ips = re.findall( ifPat, raw )
Jon Hall8f6d4622016-05-23 15:27:18 -07002406 if iface:
2407 if ips:
2408 ip = ips[0]
2409 self.ip_address = ip
2410 return ip
2411 else:
2412 main.log.error( "Error finding ip, ifconfig output:".format( raw ) )
2413 # ELSE: attempt to get address matching nicPat.
Jon Hall6c44c0b2016-04-20 15:21:00 -07002414 if nicPat:
2415 for ip in ips:
2416 curMatch = re.search( nicPat, ip )
2417 if curMatch:
2418 self.ip_address = ip
2419 return ip
Jon Hall8f6d4622016-05-23 15:27:18 -07002420 else: # If only one non-localhost ip, return that
2421 tmpList = [ ip for ip in ips if ip is not LOCALHOST ]
Jon Hall6c44c0b2016-04-20 15:21:00 -07002422 if len(tmpList) == 1:
2423 curIp = tmpList[0]
2424 self.ip_address = curIp
2425 return curIp
Jon Hall8f6d4622016-05-23 15:27:18 -07002426 # Either no non-localhost IPs, or more than 1
Jon Hallebccd352016-05-20 15:17:17 -07002427 main.log.warn( "getIpAddr failed to find a public IP address" )
Jon Hall8f6d4622016-05-23 15:27:18 -07002428 return LOCALHOST
Jon Halle1790952016-05-23 15:51:46 -07002429 except subprocess.CalledProcessError:
Jon Hall8f6d4622016-05-23 15:27:18 -07002430 main.log.exception( "Error executing ifconfig" )
2431 except IndexError:
2432 main.log.exception( "Error getting IP Address" )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002433 except Exception:
2434 main.log.exception( "Uncaught exception" )
suibin zhang116647a2016-05-06 16:30:09 -07002435
Devin Lim461f0872017-06-05 16:49:33 -07002436 def startBasicONOS( self, nodeList, opSleep=60, onosStartupSleep=30, onosUser="sdn" ):
suibin zhang116647a2016-05-06 16:30:09 -07002437 '''
2438 Start onos cluster with defined nodes, but only with drivers app
suibin zhang116647a2016-05-06 16:30:09 -07002439 '''
2440 import time
2441
2442 self.createCellFile( self.ip_address,
2443 "temp",
2444 self.ip_address,
2445 "drivers",
Devin Lim461f0872017-06-05 16:49:33 -07002446 nodeList, onosUser )
suibin zhang116647a2016-05-06 16:30:09 -07002447
2448 main.log.info( self.name + ": Apply cell to environment" )
2449 cellResult = self.setCell( "temp" )
2450 verifyResult = self.verifyCell()
2451
2452 main.log.info( self.name + ": Creating ONOS package" )
You Wangd1bcaca2016-10-24 15:23:26 -07002453 packageResult = self.buckBuild( timeout=opSleep )
suibin zhang116647a2016-05-06 16:30:09 -07002454
You Wangc669d212017-01-25 11:09:48 -08002455 main.log.info( self.name + ": Uninstalling ONOS" )
2456 for nd in nodeList:
2457 self.onosUninstall( nodeIp=nd )
2458
suibin zhang116647a2016-05-06 16:30:09 -07002459 main.log.info( self.name + ": Installing ONOS package" )
2460 for nd in nodeList:
You Wangc669d212017-01-25 11:09:48 -08002461 self.onosInstall( node=nd )
2462
2463 main.log.info( self.name + ": Set up ONOS secure SSH" )
2464 for nd in nodeList:
2465 self.onosSecureSSH( node=nd )
suibin zhang116647a2016-05-06 16:30:09 -07002466
2467 main.log.info( self.name + ": Starting ONOS service" )
2468 time.sleep( onosStartupSleep )
2469
2470 onosStatus = True
2471 for nd in nodeList:
2472 onosStatus = onosStatus & self.isup( node = nd )
2473 #print "onosStatus is: " + str( onosStatus )
2474
2475 return main.TRUE if onosStatus else main.FALSE
2476
Devin Lim02075272017-07-10 15:33:21 -07002477 def onosNetCfg( self, controllerIp, path, fileName ):
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002478 """
2479 Push a specified json file to ONOS through the onos-netcfg service
2480
2481 Required:
Devin Lim02075272017-07-10 15:33:21 -07002482 controllerIp - the Ip of the ONOS node in the cluster
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002483 path - the location of the file to be sent
2484 fileName - name of the json file to be sent
2485
2486 Returns main.TRUE on successfully sending json file, and main.FALSE if
2487 there is an error.
2488 """
2489 try:
Devin Lim02075272017-07-10 15:33:21 -07002490 cmd = "onos-netcfg {0} {1}{2}".format( controllerIp, path, fileName )
2491 main.log.info( "Sending: " + cmd )
2492 main.ONOSbench.handle.sendline( cmd )
Jeremy Ronquillo3008aa32017-07-07 15:38:57 -07002493 main.ONOSbench.handle.expect( self.prompt )
Devin Lim02075272017-07-10 15:33:21 -07002494 handle = self.handle.before
Jeremy Ronquillo3008aa32017-07-07 15:38:57 -07002495 if "Error" in handle or "No such file or directory" in handle or "curl: " in handle:
2496 main.log.error( self.name + ": " + handle + self.handle.after )
Devin Lim02075272017-07-10 15:33:21 -07002497 return main.FALSE
Devin Lim752dd7b2017-06-27 14:40:03 -07002498 return main.TRUE
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002499 except pexpect.EOF:
2500 main.log.error( self.name + ": EOF exception found" )
2501 main.log.error( self.name + ": " + self.handle.before )
2502 main.cleanup()
2503 main.exit()
2504 except Exception:
2505 main.log.exception( self.name + ": Uncaught exception!" )
2506 main.cleanup()
Jeremy Songsterae01bba2016-07-11 15:39:17 -07002507 main.exit()