blob: f0ce0ce610557b3139d4be2de74d775c8ddb028e [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
Devin Lim142b5342017-07-20 15:22:39 -07001409 def preventAutoRespawn( self ):
1410 """
1411 Description:
1412 This will prevent ONOSservice to automatically
1413 respawn.
1414 """
1415 try:
1416 self.handle.sendline( "sed -i -e 's/^respawn$/#respawn/g' tools/package/init/onos.conf" )
1417 self.handle.expect( "\$" ) # $ from the command
1418 self.handle.sendline( "sed -i -e 's/^Restart=always/Restart=no/g' tools/package/init/onos.service" )
1419 self.handle.expect( "\$" ) # $ from the command
1420 self.handle.expect( "\$" ) # $ from the prompt
1421 except pexpect.EOF:
1422 main.log.error( self.name + ": EOF exception found" )
1423 main.log.error( self.name + ": " + self.handle.before )
1424 main.cleanup()
1425 main.exit()
1426 except Exception:
1427 main.log.exception( self.name + ": Uncaught exception!" )
1428 main.cleanup()
1429 main.exit()
1430
1431
kelvin-onlabd3b64892015-01-20 13:26:24 -08001432 def pushTestIntentsShell(
1433 self,
1434 dpidSrc,
1435 dpidDst,
1436 numIntents,
1437 dirFile,
1438 onosIp,
1439 numMult="",
1440 appId="",
1441 report=True,
1442 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001443 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001444 Description:
kelvin8ec71442015-01-15 16:57:00 -08001445 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001446 better parallelize the results than the CLI
1447 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001448 * dpidSrc: specify source dpid
1449 * dpidDst: specify destination dpid
1450 * numIntents: specify number of intents to push
1451 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001452 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001453 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001454 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001455 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001456 """
1457 try:
1458 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001459 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001460 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001461 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001462 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001463 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001464
kelvin-onlabd3b64892015-01-20 13:26:24 -08001465 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1466 if not numMult:
1467 addIntents = addDpid + " " + str( numIntents )
1468 elif numMult:
1469 addIntents = addDpid + " " + str( numIntents ) + " " +\
1470 str( numMult )
1471 if appId:
1472 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001473 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001474 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001475
andrewonlabaedc8332014-12-04 12:43:03 -05001476 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001477 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001478 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001479 sendCmd = addApp + " &"
1480 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001481
kelvin-onlabd3b64892015-01-20 13:26:24 -08001482 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001483
1484 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001485 main.log.error( self.name + ": EOF exception found" )
1486 main.log.error( self.name + ": " + self.handle.before )
andrewonlabb66dfa12014-12-02 15:51:10 -05001487 main.cleanup()
1488 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001489 except Exception:
1490 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabb66dfa12014-12-02 15:51:10 -05001491 main.cleanup()
kelvin8ec71442015-01-15 16:57:00 -08001492 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -04001493
kelvin-onlabd3b64892015-01-20 13:26:24 -08001494 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001495 """
andrewonlab970399c2014-11-07 13:09:32 -05001496 Capture all packet activity and store in specified
1497 directory/file
1498
1499 Required:
1500 * interface: interface to capture
1501 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001502 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001503 try:
1504 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001505 self.handle.expect( self.prompt )
andrewonlab970399c2014-11-07 13:09:32 -05001506
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001507 self.handle.sendline( "tshark -i " + str( interface ) + " -t e -w " + str( dirFile ) + " &" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001508 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001509 self.handle.expect( "Capturing on" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001510 self.handle.sendline( "\n" )
Devin Limc20e79a2017-06-07 10:29:57 -07001511 self.handle.expect( self.prompt )
andrewonlab970399c2014-11-07 13:09:32 -05001512
Jon Hallfebb1c72015-03-05 13:30:09 -08001513 main.log.info( "Tshark started capturing files on " +
1514 str( interface ) + " and saving to directory: " +
1515 str( dirFile ) )
1516 except pexpect.EOF:
1517 main.log.error( self.name + ": EOF exception found" )
1518 main.log.error( self.name + ": " + self.handle.before )
1519 main.cleanup()
1520 main.exit()
1521 except Exception:
1522 main.log.exception( self.name + ": Uncaught exception!" )
1523 main.cleanup()
1524 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001525
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001526 def onosTopoCfg( self, onosIp, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001527 """
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001528 Description:
1529 Execute onos-topo-cfg command
1530 Required:
1531 onosIp - IP of the onos node you want to send the json to
1532 jsonFile - File path of the json file
1533 Return:
1534 Returns main.TRUE if the command is successfull; Returns
1535 main.FALSE if there was an error
kelvin8ec71442015-01-15 16:57:00 -08001536 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001537 try:
kelvin8ec71442015-01-15 16:57:00 -08001538 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001539 self.handle.expect( self.prompt )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001540 cmd = "onos-topo-cfg "
1541 self.handle.sendline( cmd + str( onosIp ) + " " + jsonFile )
1542 handle = self.handle.before
1543 print handle
1544 if "Error" in handle:
1545 main.log.error( self.name + ": " + self.handle.before )
1546 return main.FALSE
1547 else:
Devin Limc20e79a2017-06-07 10:29:57 -07001548 self.handle.expect( self.prompt )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001549 return main.TRUE
1550
Jon Hallfebb1c72015-03-05 13:30:09 -08001551 except pexpect.EOF:
1552 main.log.error( self.name + ": EOF exception found" )
1553 main.log.error( self.name + ": " + self.handle.before )
1554 main.cleanup()
1555 main.exit()
1556 except Exception:
1557 main.log.exception( self.name + ": Uncaught exception!" )
1558 main.cleanup()
1559 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08001560
jenkins1e99e7b2015-04-02 18:15:39 -07001561 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001562 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001563 Required:
kelvin8ec71442015-01-15 16:57:00 -08001564 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001565 * directory to store results
1566 Optional:
1567 * interface - default: eth0
Jon Hall4ba53f02015-07-29 13:07:41 -07001568 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001569 Description:
1570 Uses tshark command to grep specific group of packets
1571 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001572 The timestamp is hardcoded to be in epoch
1573 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001574 try:
1575 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001576 self.handle.expect( self.prompt )
Jon Hallfebb1c72015-03-05 13:30:09 -08001577 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001578 if grepOptions:
1579 grepStr = "grep "+str(grepOptions)
1580 else:
1581 grepStr = "grep"
Jon Hall4ba53f02015-07-29 13:07:41 -07001582
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001583 cmd = (
1584 "sudo tshark -i " +
Jon Hallfebb1c72015-03-05 13:30:09 -08001585 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001586 " -t e | " +
1587 grepStr + " --line-buffered \"" +
Jon Hallfebb1c72015-03-05 13:30:09 -08001588 str(grep) +
1589 "\" >" +
1590 directory +
1591 " &" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001592 self.handle.sendline(cmd)
1593 main.log.info(cmd)
Jon Hallfebb1c72015-03-05 13:30:09 -08001594 self.handle.expect( "Capturing on" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001595 self.handle.sendline( "\n" )
Devin Limc20e79a2017-06-07 10:29:57 -07001596 self.handle.expect( self.prompt )
Jon Hallfebb1c72015-03-05 13:30:09 -08001597 except pexpect.EOF:
1598 main.log.error( self.name + ": EOF exception found" )
1599 main.log.error( self.name + ": " + self.handle.before )
1600 main.cleanup()
1601 main.exit()
1602 except Exception:
1603 main.log.exception( self.name + ": Uncaught exception!" )
1604 main.cleanup()
1605 main.exit()
1606
kelvin-onlabd3b64892015-01-20 13:26:24 -08001607 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001608 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001609 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001610 """
1611 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001612 try:
1613 self.execute( cmd="sudo rm /tmp/wireshark*" )
1614 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001615 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1616 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001617 self.handle.sendline( "" )
1618 main.log.info( "Tshark stopped" )
1619 except pexpect.EOF:
1620 main.log.error( self.name + ": EOF exception found" )
1621 main.log.error( self.name + ": " + self.handle.before )
1622 main.cleanup()
1623 main.exit()
1624 except Exception:
1625 main.log.exception( self.name + ": Uncaught exception!" )
1626 main.cleanup()
1627 main.exit()
1628
kelvin8ec71442015-01-15 16:57:00 -08001629 def ptpd( self, args ):
1630 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001631 Initiate ptp with user-specified args.
1632 Required:
1633 * args: specify string of args after command
1634 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001635 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001636 try:
kelvin8ec71442015-01-15 16:57:00 -08001637 self.handle.sendline( "sudo ptpd " + str( args ) )
1638 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001639 "Multiple",
1640 "Error",
Devin Limc20e79a2017-06-07 10:29:57 -07001641 self.prompt ] )
1642 self.handle.expect( self.prompt )
andrewonlabba44bcf2014-10-16 16:54:41 -04001643
andrewonlab0c38a4a2014-10-28 18:35:35 -04001644 if i == 0:
1645 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001646 main.log.info( "ptpd returned an error: " +
1647 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001648 return handle
1649 elif i == 1:
1650 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001651 main.log.error( "ptpd returned an error: " +
1652 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001653 return handle
1654 else:
1655 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001656
andrewonlab0c38a4a2014-10-28 18:35:35 -04001657 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001658 main.log.error( self.name + ": EOF exception found" )
1659 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001660 main.cleanup()
1661 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001662 except Exception:
1663 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001664 main.cleanup()
1665 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001666
Pier50f0bc62016-09-07 17:53:40 -07001667 def dumpONOSCmd(self, ONOSIp, CMD, destDir, filename, options=""):
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001668 """
Pier50f0bc62016-09-07 17:53:40 -07001669 Dump Cmd to a desired directory.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001670 For debugging purposes, you may want to use
Pier50f0bc62016-09-07 17:53:40 -07001671 this function to capture Cmd at a given point in time.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001672 Localtime will be attached to the filename
1673
1674 Required:
1675 * ONOSIp: the IP of the target ONOS instance
Pier50f0bc62016-09-07 17:53:40 -07001676 * CMD: the command to dump;
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001677 * destDir: specify directory to copy to.
1678 ex ) /tmp/
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001679 * fileName: Name of the file
Pier50f0bc62016-09-07 17:53:40 -07001680 * options: Options for ONOS command
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001681 """
1682
1683 localtime = time.strftime( '%x %X' )
1684 localtime = localtime.replace( "/", "" )
1685 localtime = localtime.replace( " ", "_" )
1686 localtime = localtime.replace( ":", "" )
1687 if destDir[ -1: ] != "/":
1688 destDir += "/"
Pier50f0bc62016-09-07 17:53:40 -07001689 cmd=CMD + " " + options + " > " + str( destDir ) + str( filename ) + localtime
1690 return self.onosCli(ONOSIp, cmd)
Flavio Castrob7718952016-05-18 08:53:41 -07001691
kelvin-onlabd3b64892015-01-20 13:26:24 -08001692 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001693 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001694 """
1695 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001696 Current implementation of ONOS deletes its karaf
1697 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001698 you may want to use this function to capture
1699 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001700 Localtime will be attached to the filename
1701
1702 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001703 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001704 copy.
kelvin8ec71442015-01-15 16:57:00 -08001705 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001706 For copying multiple files, leave copyFileName
1707 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001708 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001709 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001710 ex ) /tmp/
1711 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001712 * copyFileName: If you want to rename the log
1713 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001714 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001715 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001716 try:
Flavio Castro09ab59d2016-05-25 17:01:35 -07001717 localtime = time.strftime( '%H %M' )
kelvin8ec71442015-01-15 16:57:00 -08001718 localtime = localtime.replace( "/", "" )
1719 localtime = localtime.replace( " ", "_" )
1720 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001721 if destDir[ -1: ] != "/":
1722 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001723
kelvin-onlabd3b64892015-01-20 13:26:24 -08001724 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001725 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1726 str( destDir ) + str( copyFileName ) +
1727 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001728 self.handle.expect( "cp" )
Devin Limc20e79a2017-06-07 10:29:57 -07001729 self.handle.expect( self.prompt )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001730 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001731 self.handle.sendline( "cp " + str( logToCopy ) +
1732 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001733 self.handle.expect( "cp" )
Devin Limc20e79a2017-06-07 10:29:57 -07001734 self.handle.expect( self.prompt )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001735
kelvin8ec71442015-01-15 16:57:00 -08001736 return self.handle.before
1737
1738 except pexpect.EOF:
1739 main.log.error( "Copying files failed" )
1740 main.log.error( self.name + ": EOF exception found" )
1741 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001742 except Exception:
1743 main.log.exception( "Copying files failed" )
1744
Jon Hall16b72c42015-05-20 10:23:36 -07001745 def checkLogs( self, onosIp, restart=False):
kelvin8ec71442015-01-15 16:57:00 -08001746 """
Jon Hall94fd0472014-12-08 11:52:42 -08001747 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001748 If restart is True, use the old version of onos-check-logs which
1749 does not print the full stacktrace, but shows the entire log file,
1750 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001751 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001752 """
Jon Hall94fd0472014-12-08 11:52:42 -08001753 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001754 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001755 if restart:
1756 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001757 self.handle.sendline( cmd )
1758 self.handle.expect( cmd )
Devin Limdc78e202017-06-09 18:30:07 -07001759 self.handle.expect( self.prompt + " " )
Jon Hall94fd0472014-12-08 11:52:42 -08001760 response = self.handle.before
1761 return response
1762 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001763 main.log.error( "Lost ssh connection" )
1764 main.log.error( self.name + ": EOF exception found" )
1765 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001766 except Exception:
1767 main.log.exception( self.name + ": Uncaught exception!" )
1768 main.cleanup()
1769 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -08001770
kelvin-onlabd3b64892015-01-20 13:26:24 -08001771 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001772 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001773 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001774 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001775 try:
kelvin8ec71442015-01-15 16:57:00 -08001776 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001777 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001778 self.handle.sendline( "onos-service " + str( node ) +
1779 " status" )
1780 i = self.handle.expect( [
You Wangef1e6572016-03-08 12:53:18 -08001781 "start/running",
You Wang7bd83062016-03-01 11:50:00 -08001782 "Running ...",
You Wangef1e6572016-03-08 12:53:18 -08001783 "stop/waiting",
You Wang7bd83062016-03-01 11:50:00 -08001784 "Not Running ...",
kelvin8ec71442015-01-15 16:57:00 -08001785 pexpect.TIMEOUT ], timeout=120 )
YPZhangfebf7302016-05-24 16:45:56 -07001786 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001787 self.handle.expect( self.prompt )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001788
You Wangef1e6572016-03-08 12:53:18 -08001789 if i == 0 or i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001790 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001791 return main.TRUE
You Wangef1e6572016-03-08 12:53:18 -08001792 elif i == 2 or i == 3:
kelvin8ec71442015-01-15 16:57:00 -08001793 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001794 main.log.error( "ONOS service failed to check the status" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001795 main.cleanup()
1796 main.exit()
1797 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001798 main.log.error( self.name + ": EOF exception found" )
1799 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001800 main.cleanup()
1801 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001802 except Exception:
1803 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001804 main.cleanup()
1805 main.exit()
Jon Hall21270ac2015-02-16 17:59:55 -08001806
Jon Hall63604932015-02-26 17:09:50 -08001807 def setIpTables( self, ip, port='', action='add', packet_type='',
1808 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001809 """
Jon Hall21270ac2015-02-16 17:59:55 -08001810 Description:
1811 add or remove iptables rule to DROP (default) packets from
1812 specific IP and PORT
1813 Usage:
1814 * specify action ('add' or 'remove')
1815 when removing, pass in the same argument as you would add. It will
1816 delete that specific rule.
1817 * specify the ip to block
1818 * specify the destination port to block (defaults to all ports)
1819 * optional packet type to block (default tcp)
1820 * optional iptables rule (default DROP)
1821 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001822 * States boolean toggles adding all supported tcp states to the
1823 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001824 Returns:
1825 main.TRUE on success or
1826 main.FALSE if given invalid input or
1827 main.ERROR if there is an error in response from iptables
1828 WARNING:
1829 * This function uses root privilege iptables command which may result
1830 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001831 """
Jon Hall21270ac2015-02-16 17:59:55 -08001832
1833 # NOTE*********
1834 # The strict checking methods of this driver function is intentional
1835 # to discourage any misuse or error of iptables, which can cause
1836 # severe network errors
1837 # *************
1838
1839 # NOTE: Sleep needed to give some time for rule to be added and
1840 # registered to the instance. If you are calling this function
1841 # multiple times this sleep will prevent any errors.
1842 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001843 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001844 try:
1845 # input validation
1846 action_type = action.lower()
1847 rule = rule.upper()
1848 direction = direction.upper()
1849 if action_type != 'add' and action_type != 'remove':
1850 main.log.error( "Invalid action type. Use 'add' or "
1851 "'remove' table rule" )
1852 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1853 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1854 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1855 "'ACCEPT' or 'LOG' only." )
1856 if direction != 'INPUT' and direction != 'OUTPUT':
1857 # NOTE currently only supports rules INPUT and OUPTUT
1858 main.log.error( "Invalid rule. Valid directions are"
1859 " 'OUTPUT' or 'INPUT'" )
1860 return main.FALSE
1861 return main.FALSE
1862 return main.FALSE
1863 if action_type == 'add':
1864 # -A is the 'append' action of iptables
1865 actionFlag = '-A'
1866 elif action_type == 'remove':
1867 # -D is the 'delete' rule of iptables
1868 actionFlag = '-D'
1869 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001870 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001871 cmd = "sudo iptables " + actionFlag + " " +\
1872 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001873 " -s " + str( ip )
Jon Hall63604932015-02-26 17:09:50 -08001874 # " -p " + str( packet_type ) +\
1875 if packet_type:
1876 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001877 if port:
1878 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001879 if states:
1880 cmd += " -m state --state="
1881 #FIXME- Allow user to configure which states to block
1882 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001883 cmd += " -j " + str( rule )
1884
1885 self.handle.sendline( cmd )
Devin Limc20e79a2017-06-07 10:29:57 -07001886 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001887 main.log.warn( self.handle.before )
1888
1889 info_string = "On " + str( self.name )
1890 info_string += " " + str( action_type )
1891 info_string += " iptable rule [ "
1892 info_string += " IP: " + str( ip )
1893 info_string += " Port: " + str( port )
1894 info_string += " Rule: " + str( rule )
1895 info_string += " Direction: " + str( direction ) + " ]"
1896 main.log.info( info_string )
1897 return main.TRUE
1898 except pexpect.TIMEOUT:
1899 main.log.exception( self.name + ": Timeout exception in "
1900 "setIpTables function" )
1901 return main.ERROR
1902 except pexpect.EOF:
1903 main.log.error( self.name + ": EOF exception found" )
1904 main.log.error( self.name + ": " + self.handle.before )
1905 main.cleanup()
1906 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001907 except Exception:
1908 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall21270ac2015-02-16 17:59:55 -08001909 main.cleanup()
1910 main.exit()
1911
Jon Hall0468b042015-02-19 19:08:21 -08001912 def detailed_status(self, log_filename):
Jon Hallefbd9792015-03-05 16:11:36 -08001913 """
Jon Hall0468b042015-02-19 19:08:21 -08001914 This method is used by STS to check the status of the controller
1915 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001916 """
Jon Hall0468b042015-02-19 19:08:21 -08001917 import re
1918 try:
1919 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001920 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001921 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -07001922 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001923 self.handle.sendline( "service onos status" )
Devin Limc20e79a2017-06-07 10:29:57 -07001924 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001925 response = self.handle.before
1926 if re.search( "onos start/running", response ):
1927 # onos start/running, process 10457
1928 return 'RUNNING'
1929 # FIXME: Implement this case
1930 # elif re.search( pattern, response ):
1931 # return 'STARTING'
1932 elif re.search( "onos stop/", response ):
1933 # onos stop/waiting
1934 # FIXME handle this differently?: onos stop/pre-stop
1935 return 'STOPPED'
1936 # FIXME: Implement this case
1937 # elif re.search( pattern, response ):
1938 # return 'FROZEN'
1939 else:
1940 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001941 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001942 main.log.warn( response )
1943 return 'ERROR', "Unknown response: %s" % response
1944 except pexpect.TIMEOUT:
1945 main.log.exception( self.name + ": Timeout exception in "
1946 "setIpTables function" )
1947 return 'ERROR', "Pexpect Timeout"
1948 except pexpect.EOF:
1949 main.log.error( self.name + ": EOF exception found" )
1950 main.log.error( self.name + ": " + self.handle.before )
1951 main.cleanup()
1952 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001953 except Exception:
1954 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall0468b042015-02-19 19:08:21 -08001955 main.cleanup()
1956 main.exit()
1957
andrew@onlab.us3b087132015-03-11 15:00:08 -07001958 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount):
1959 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001960 Create/formats the LinkGraph.cfg file based on arguments
1961 -only creates a linear topology and connects islands
1962 -evenly distributes devices
andrew@onlab.us3b087132015-03-11 15:00:08 -07001963 -must be called by ONOSbench
1964
Jon Hall4ba53f02015-07-29 13:07:41 -07001965 ONOSIpList - list of all of the node IPs to be used
1966
1967 deviceCount - number of switches to be assigned
andrew@onlab.us3b087132015-03-11 15:00:08 -07001968 '''
Jon Hall6509dbf2016-06-21 17:01:17 -07001969 main.log.info("Creating link graph configuration file." )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001970 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
Jon Hall4ba53f02015-07-29 13:07:41 -07001971 tempFile = "/tmp/linkGraph.cfg"
andrew@onlab.us3b087132015-03-11 15:00:08 -07001972
1973 linkGraph = open(tempFile, 'w+')
1974 linkGraph.write("# NullLinkProvider topology description (config file).\n")
1975 linkGraph.write("# The NodeId is only added if the destination is another node's device.\n")
1976 linkGraph.write("# Bugs: Comments cannot be appended to a line to be read.\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07001977
andrew@onlab.us3b087132015-03-11 15:00:08 -07001978 clusterCount = len(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07001979
1980 if type(deviceCount) is int or type(deviceCount) is str:
andrew@onlab.us3b087132015-03-11 15:00:08 -07001981 deviceCount = int(deviceCount)
1982 switchList = [0]*(clusterCount+1)
1983 baselineSwitchCount = deviceCount/clusterCount
Jon Hall4ba53f02015-07-29 13:07:41 -07001984
andrew@onlab.us3b087132015-03-11 15:00:08 -07001985 for node in range(1, clusterCount + 1):
1986 switchList[node] = baselineSwitchCount
1987
1988 for node in range(1, (deviceCount%clusterCount)+1):
1989 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001990
andrew@onlab.us3b087132015-03-11 15:00:08 -07001991 if type(deviceCount) is list:
1992 main.log.info("Using provided device distribution")
1993 switchList = [0]
1994 for i in deviceCount:
1995 switchList.append(int(i))
1996
1997 tempList = ['0']
1998 tempList.extend(ONOSIpList)
1999 ONOSIpList = tempList
2000
2001 myPort = 6
2002 lastSwitch = 0
2003 for node in range(1, clusterCount+1):
2004 if switchList[node] == 0:
2005 continue
2006
2007 linkGraph.write("graph " + ONOSIpList[node] + " {\n")
Jon Hall4ba53f02015-07-29 13:07:41 -07002008
andrew@onlab.us3b087132015-03-11 15:00:08 -07002009 if node > 1:
2010 #connect to last device on previous node
Jon Hall4ba53f02015-07-29 13:07:41 -07002011 line = ("\t0:5 -> " + str(lastSwitch) + ":6:" + lastIp + "\n") #ONOSIpList[node-1]
2012 linkGraph.write(line)
2013
2014 lastSwitch = 0
2015 for switch in range (0, switchList[node]-1):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002016 line = ""
2017 line = ("\t" + str(switch) + ":" + str(myPort))
2018 line += " -- "
2019 line += (str(switch+1) + ":" + str(myPort-1) + "\n")
2020 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07002021 lastSwitch = switch+1
andrew@onlab.us3b087132015-03-11 15:00:08 -07002022 lastIp = ONOSIpList[node]
Jon Hall4ba53f02015-07-29 13:07:41 -07002023
andrew@onlab.us3b087132015-03-11 15:00:08 -07002024 #lastSwitch += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07002025 if node < (clusterCount):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002026 #connect to first device on the next node
Jon Hall4ba53f02015-07-29 13:07:41 -07002027 line = ("\t" + str(lastSwitch) + ":6 -> 0:5:" + ONOSIpList[node+1] + "\n")
andrew@onlab.us3b087132015-03-11 15:00:08 -07002028 linkGraph.write(line)
Jon Hall4ba53f02015-07-29 13:07:41 -07002029
andrew@onlab.us3b087132015-03-11 15:00:08 -07002030 linkGraph.write("}\n")
2031 linkGraph.close()
2032
2033 #SCP
Jon Hall4ba53f02015-07-29 13:07:41 -07002034 os.system( "scp " + tempFile + " " + self.user_name + "@" + benchIp + ":" + linkGraphPath)
andrew@onlab.us3b087132015-03-11 15:00:08 -07002035 main.log.info("linkGraph.cfg creation complete")
2036
cameron@onlab.us75900962015-03-30 13:22:49 -07002037 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10):
Jon Hall4ba53f02015-07-29 13:07:41 -07002038
andrew@onlab.us3b087132015-03-11 15:00:08 -07002039 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002040 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
2041 deviceCount = number of switches to distribute, or list of values to use as custom distribution
cameron@onlab.us75900962015-03-30 13:22:49 -07002042 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 -07002043 '''
2044
Jon Hall6509dbf2016-06-21 17:01:17 -07002045 main.log.info("Configuring Null Device Provider" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002046 clusterCount = len(ONOSIpList)
2047
Jon Hall4ba53f02015-07-29 13:07:41 -07002048 try:
2049
cameron@onlab.us75900962015-03-30 13:22:49 -07002050 if type(deviceCount) is int or type(deviceCount) is str:
Jon Hall6509dbf2016-06-21 17:01:17 -07002051 main.log.info("Creating device distribution")
cameron@onlab.us75900962015-03-30 13:22:49 -07002052 deviceCount = int(deviceCount)
2053 switchList = [0]*(clusterCount+1)
2054 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002055
cameron@onlab.us75900962015-03-30 13:22:49 -07002056 for node in range(1, clusterCount + 1):
2057 switchList[node] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002058
cameron@onlab.us75900962015-03-30 13:22:49 -07002059 for node in range(1, (deviceCount%clusterCount)+1):
2060 switchList[node] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07002061
2062 if type(deviceCount) is list:
2063 main.log.info("Using provided device distribution")
2064
2065 if len(deviceCount) == clusterCount:
cameron@onlab.us75900962015-03-30 13:22:49 -07002066 switchList = ['0']
2067 switchList.extend(deviceCount)
Jon Hall4ba53f02015-07-29 13:07:41 -07002068
2069 if len(deviceCount) == (clusterCount + 1):
2070 if deviceCount[0] == '0' or deviceCount[0] == 0:
cameron@onlab.us75900962015-03-30 13:22:49 -07002071 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002072
cameron@onlab.us75900962015-03-30 13:22:49 -07002073 assert len(switchList) == (clusterCount + 1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002074
cameron@onlab.us75900962015-03-30 13:22:49 -07002075 except AssertionError:
Jon Hall4ba53f02015-07-29 13:07:41 -07002076 main.log.error( "Bad device/Ip list match")
cameron@onlab.us75900962015-03-30 13:22:49 -07002077 except TypeError:
2078 main.log.exception( self.name + ": Object not as expected" )
2079 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07002080 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002081 main.log.exception( self.name + ": Uncaught exception!" )
2082 main.cleanup()
2083 main.exit()
2084
andrew@onlab.us3b087132015-03-11 15:00:08 -07002085
2086 ONOSIp = [0]
2087 ONOSIp.extend(ONOSIpList)
Jon Hall4ba53f02015-07-29 13:07:41 -07002088
andrew@onlab.us3b087132015-03-11 15:00:08 -07002089 devicesString = "devConfigs = "
2090 for node in range(1, len(ONOSIp)):
2091 devicesString += (ONOSIp[node] + ":" + str(switchList[node] ))
2092 if node < clusterCount:
2093 devicesString += (",")
Jon Hall4ba53f02015-07-29 13:07:41 -07002094
2095 try:
cameron@onlab.us75900962015-03-30 13:22:49 -07002096 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
2097 self.handle.expect(":~")
2098 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str(numPorts) )
2099 self.handle.expect(":~")
andrew@onlab.us3b087132015-03-11 15:00:08 -07002100
cameron@onlab.us75900962015-03-30 13:22:49 -07002101 for i in range(10):
2102 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider")
2103 self.handle.expect(":~")
2104 verification = self.handle.before
2105 if (" value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification:
2106 break
2107 else:
2108 time.sleep(1)
andrew@onlab.us3b087132015-03-11 15:00:08 -07002109
cameron@onlab.us2cc8bf12015-04-02 14:12:30 -07002110 assert ("value=" + str(numPorts)) in verification and (" value=" + devicesString) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002111
cameron@onlab.us75900962015-03-30 13:22:49 -07002112 except AssertionError:
2113 main.log.error("Incorrect Config settings: " + verification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002114 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002115 main.log.exception( self.name + ": Uncaught exception!" )
2116 main.cleanup()
2117 main.exit()
2118
Jon Hall4ba53f02015-07-29 13:07:41 -07002119 def configNullLink( self,fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002120 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002121 fileName default is currently the same as the default on ONOS, specify alternate file if
cameron@onlab.us75900962015-03-30 13:22:49 -07002122 you want to use a different topology file than linkGraph.cfg
andrew@onlab.us3b087132015-03-11 15:00:08 -07002123 '''
2124
Jon Hall4ba53f02015-07-29 13:07:41 -07002125
2126 try:
2127 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str(eventRate))
2128 self.handle.expect(":~")
cameron@onlab.us75900962015-03-30 13:22:49 -07002129 self.handle.sendline("onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2130 self.handle.expect(":~")
Jon Hall4ba53f02015-07-29 13:07:41 -07002131
2132 for i in range(10):
2133 self.handle.sendline("onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider")
cameron@onlab.us75900962015-03-30 13:22:49 -07002134 self.handle.expect(":~")
2135 verification = self.handle.before
Jon Hall4ba53f02015-07-29 13:07:41 -07002136 if (" value=" + str(eventRate)) in verification and (" value=" + fileName) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002137 break
Jon Hall4ba53f02015-07-29 13:07:41 -07002138 else:
cameron@onlab.us75900962015-03-30 13:22:49 -07002139 time.sleep(1)
Jon Hall4ba53f02015-07-29 13:07:41 -07002140
cameron@onlab.us75900962015-03-30 13:22:49 -07002141 assert ("value=" + str(eventRate)) in verification and (" value=" + fileName) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002142
cameron@onlab.us75900962015-03-30 13:22:49 -07002143 except pexpect.EOF:
2144 main.log.error( self.name + ": EOF exception found" )
2145 main.log.error( self.name + ": " + self.handle.before )
2146 main.cleanup()
2147 main.exit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002148 except AssertionError:
2149 main.log.info("Settings did not post to ONOS")
Jon Hall4ba53f02015-07-29 13:07:41 -07002150 main.log.error(varification)
Jon Hall77ba41c2015-04-06 10:25:40 -07002151 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002152 main.log.exception( self.name + ": Uncaught exception!" )
2153 main.log.error(varification)
2154 main.cleanup()
2155 main.exit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002156
kelvin-onlaba4074292015-07-09 15:19:49 -07002157 def getOnosIps( self ):
2158 """
2159 Get all onos IPs stored in
2160 """
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002161
kelvin-onlaba4074292015-07-09 15:19:49 -07002162 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002163
Chiyu Chengec63bde2016-11-17 18:11:36 -08002164 def listLog( self, nodeIp ):
2165 """
2166 Get a list of all the karaf log names
2167 """
2168 try:
2169 cmd = "onos-ssh " + nodeIp + " ls -tr /opt/onos/log"
2170 self.handle.sendline( cmd )
2171 self.handle.expect( ":~" )
2172 before = self.handle.before.splitlines()
2173 logNames = []
2174 for word in before:
2175 if 'karaf.log' in word:
2176 logNames.append( word )
2177 return logNames
2178 except pexpect.EOF:
2179 main.log.error( self.name + ": EOF exception found" )
2180 main.log.error( self.name + ": " + self.handle.before )
2181 main.cleanup()
2182 main.exit()
2183 except pexpect.TIMEOUT:
2184 main.log.error( self.name + ": TIMEOUT exception found" )
2185 main.log.error( self.name + ": " + self.handle.before )
2186 main.cleanup()
2187 main.exit()
2188 except Exception:
2189 main.log.exception( self.name + ": Uncaught exception!" )
2190 main.cleanup()
2191 main.exit()
2192
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002193 def logReport( self, nodeIp, searchTerms, outputMode="s", startStr=None, endStr=None ):
Jon Hallb4242222016-01-25 17:07:04 -08002194 """
2195 Searches the latest ONOS log file for the given search terms and
2196 prints the total occurances of each term. Returns to combined total of
2197 all occurances.
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002198
Jon Hallb4242222016-01-25 17:07:04 -08002199 Arguments:
2200 * nodeIp - The ip of the ONOS node where the log is located
2201 * searchTerms - A string to grep for or a list of strings to grep
2202 for in the ONOS log. Will print out the number of
2203 occurances for each term.
2204 Optional Arguments:
2205 * outputMode - 's' or 'd'. If 'd' will print the last 5 lines
2206 containing each search term as well as the total
2207 number of occurances of each term. Defaults to 's',
2208 which prints the simple output of just the number
2209 of occurances for each term.
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002210 * startStr - the start string to be given to stream editor command
2211 as the start point for extraction of data
2212 * endStr - the end string to be given to stream editor command as
2213 the end point for extraction of data
Jon Hallb4242222016-01-25 17:07:04 -08002214 """
2215 try:
2216 main.log.info( " Log Report for {} ".format( nodeIp ).center( 70, '=' ) )
2217 if type( searchTerms ) is str:
2218 searchTerms = [searchTerms]
2219 numTerms = len( searchTerms )
2220 outputMode = outputMode.lower()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002221
Jon Hallb4242222016-01-25 17:07:04 -08002222 totalHits = 0
2223 logLines = []
2224 for termIndex in range( numTerms ):
2225 term = searchTerms[termIndex]
2226 logLines.append( [term] )
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002227 if startStr and endStr:
2228 cmd = "onos-ssh {} \"sed -n '/{}/,/{}/p' /opt/onos/log/karaf.log | grep {}\"".format( nodeIp,
2229 startStr,
2230 endStr,
2231 term )
2232 else:
2233 cmd = "onos-ssh {} cat /opt/onos/log/karaf.log | grep {}".format( nodeIp,
2234 term )
Jon Hallb4242222016-01-25 17:07:04 -08002235 self.handle.sendline( cmd )
2236 self.handle.expect( ":~" )
2237 before = self.handle.before.splitlines()
2238 count = 0
2239 for line in before:
2240 if term in line and "grep" not in line:
2241 count += 1
2242 if before.index( line ) > ( len( before ) - 7 ):
2243 logLines[termIndex].append( line )
2244 main.log.info( "{}: {}".format( term, count ) )
2245 totalHits += count
2246 if termIndex == numTerms - 1:
2247 print "\n"
2248 if outputMode != "s":
2249 outputString = ""
2250 for term in logLines:
2251 outputString = term[0] + ": \n"
2252 for line in range( 1, len( term ) ):
2253 outputString += ( "\t" + term[line] + "\n" )
2254 if outputString != ( term[0] + ": \n" ):
2255 main.log.info( outputString )
2256 main.log.info( "=" * 70 )
2257 return totalHits
2258 except pexpect.EOF:
2259 main.log.error( self.name + ": EOF exception found" )
2260 main.log.error( self.name + ": " + self.handle.before )
2261 main.cleanup()
2262 main.exit()
2263 except pexpect.TIMEOUT:
2264 main.log.error( self.name + ": TIMEOUT exception found" )
2265 main.log.error( self.name + ": " + self.handle.before )
2266 main.cleanup()
2267 main.exit()
2268 except Exception:
2269 main.log.exception( self.name + ": Uncaught exception!" )
2270 main.cleanup()
2271 main.exit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002272
2273 def copyMininetFile( self, fileName, localPath, userName, ip,
2274 mnPath='~/mininet/custom/', timeout = 60 ):
2275 """
2276 Description:
2277 Copy mininet topology file from dependency folder in the test folder
2278 and paste it to the mininet machine's mininet/custom folder
2279 Required:
2280 fileName - Name of the topology file to copy
2281 localPath - File path of the mininet topology file
2282 userName - User name of the mininet machine to send the file to
2283 ip - Ip address of the mininet machine
2284 Optional:
2285 mnPath - of the mininet directory to send the file to
2286 Return:
2287 Return main.TRUE if successfully copied the file otherwise
2288 return main.FALSE
2289 """
2290
2291 try:
2292 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2293 str( ip ) + ":" + mnPath + fileName
2294
2295 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07002296 self.handle.expect( self.prompt )
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002297
2298 main.log.info( self.name + ": Execute: " + cmd )
2299
2300 self.handle.sendline( cmd )
2301
2302 i = self.handle.expect( [ 'No such file',
2303 "100%",
2304 pexpect.TIMEOUT ] )
2305
2306 if i == 0:
2307 main.log.error( self.name + ": File " + fileName +
2308 " does not exist!" )
2309 return main.FALSE
2310
2311 if i == 1:
2312 main.log.info( self.name + ": File " + fileName +
2313 " has been copied!" )
2314 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07002315 self.handle.expect( self.prompt )
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002316 return main.TRUE
2317
2318 except pexpect.EOF:
2319 main.log.error( self.name + ": EOF exception found" )
2320 main.log.error( self.name + ": " + self.handle.before )
2321 main.cleanup()
2322 main.exit()
2323 except pexpect.TIMEOUT:
2324 main.log.error( self.name + ": TIMEOUT exception found" )
2325 main.log.error( self.name + ": " + self.handle.before )
2326 main.cleanup()
2327 main.exit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002328
2329 def jvmSet(self, memory=8):
Jon Hall4ba53f02015-07-29 13:07:41 -07002330
cameron@onlab.us78b89652015-07-08 15:21:03 -07002331 import os
2332
2333 homeDir = os.path.expanduser('~')
2334 filename = "/onos/tools/package/bin/onos-service"
2335
2336 serviceConfig = open(homeDir + filename, 'w+')
2337 serviceConfig.write("#!/bin/bash\n ")
2338 serviceConfig.write("#------------------------------------- \n ")
2339 serviceConfig.write("# Starts ONOS Apache Karaf container\n ")
2340 serviceConfig.write("#------------------------------------- \n ")
2341 serviceConfig.write("#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n ")
2342 serviceConfig.write("""export JAVA_OPTS="${JAVA_OPTS:--Xms""" + str(memory) + "G -Xmx" + str(memory) + """G}" \n """)
2343 serviceConfig.write("[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n")
2344 serviceConfig.write("""${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """)
2345 serviceConfig.close()
2346
Jon Hall6c44c0b2016-04-20 15:21:00 -07002347 def createDBFile( self, testData ):
Jon Hall4ba53f02015-07-29 13:07:41 -07002348
cameron@onlab.us78b89652015-07-08 15:21:03 -07002349 filename = main.TEST + "DB"
2350 DBString = ""
Jon Hall4ba53f02015-07-29 13:07:41 -07002351
cameron@onlab.us78b89652015-07-08 15:21:03 -07002352 for item in testData:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002353 if type( item ) is string:
cameron@onlab.us78b89652015-07-08 15:21:03 -07002354 item = "'" + item + "'"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002355 if testData.index( item ) < len( testData - 1 ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002356 item += ","
Jon Hall6c44c0b2016-04-20 15:21:00 -07002357 DBString += str( item )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002358
Jon Hall6c44c0b2016-04-20 15:21:00 -07002359 DBFile = open( filename, "a" )
2360 DBFile.write( DBString )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002361 DBFile.close()
2362
Jon Hall6c44c0b2016-04-20 15:21:00 -07002363 def verifySummary( self, ONOSIp, *deviceCount ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002364
Jon Hall6c44c0b2016-04-20 15:21:00 -07002365 self.handle.sendline( "onos " + ONOSIp + " summary" )
2366 self.handle.expect( ":~" )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002367
2368 summaryStr = self.handle.before
2369 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2370
2371 #passed = "SCC(s)=1" in summaryStr
2372 #if deviceCount:
2373 # passed = "devices=" + str(deviceCount) + "," not in summaryStr
2374
GlennRC772363b2015-08-25 13:05:57 -07002375 passed = False
cameron@onlab.us78b89652015-07-08 15:21:03 -07002376 if "SCC(s)=1," in summaryStr:
2377 passed = True
Jon Hall6c44c0b2016-04-20 15:21:00 -07002378 print "Summary is verifed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002379 else:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002380 print "Summary failed"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002381
2382 if deviceCount:
2383 print" ============================="
Jon Hall6c44c0b2016-04-20 15:21:00 -07002384 checkStr = "devices=" + str( deviceCount[0] ) + ","
cameron@onlab.us78b89652015-07-08 15:21:03 -07002385 print "Checkstr: " + checkStr
2386 if checkStr not in summaryStr:
2387 passed = False
Jon Hall6c44c0b2016-04-20 15:21:00 -07002388 print "Device count failed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002389 else:
2390 print "device count verified"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002391
2392 return passed
Jon Hall6c44c0b2016-04-20 15:21:00 -07002393
Jon Hall8f6d4622016-05-23 15:27:18 -07002394 def getIpAddr( self, iface=None ):
Jon Hall6c44c0b2016-04-20 15:21:00 -07002395 """
2396 Update self.ip_address with numerical ip address. If multiple IP's are
2397 located on the device, will attempt to use self.nicAddr to choose the
2398 right one. Defaults to 127.0.0.1 if no other address is found or cannot
2399 determine the correct address.
2400
2401 ONLY WORKS WITH IPV4 ADDRESSES
2402 """
2403 try:
Jon Hall8f6d4622016-05-23 15:27:18 -07002404 LOCALHOST = "127.0.0.1"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002405 ipPat = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
2406 pattern = re.compile( ipPat )
2407 match = re.search( pattern, self.ip_address )
2408 if self.nicAddr:
2409 nicPat = self.nicAddr.replace( ".", "\." ).replace( "\*", r"\d{1,3}" )
2410 nicPat = re.compile( nicPat )
2411 else:
2412 nicPat = None
2413 # IF self.ip_address is an ip address and matches
2414 # self.nicAddr: return self.ip_address
2415 if match:
2416 curIp = match.group(0)
2417 if nicPat:
2418 nicMatch = re.search( nicPat, curIp )
2419 if nicMatch:
2420 return self.ip_address
Jon Hall8f6d4622016-05-23 15:27:18 -07002421 # ELSE: IF iface, return ip of interface
2422 cmd = "ifconfig"
2423 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2424 if iface:
2425 cmd += " " + str( iface )
2426 raw = subprocess.check_output( cmd.split() )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002427 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2428 ips = re.findall( ifPat, raw )
Jon Hall8f6d4622016-05-23 15:27:18 -07002429 if iface:
2430 if ips:
2431 ip = ips[0]
2432 self.ip_address = ip
2433 return ip
2434 else:
2435 main.log.error( "Error finding ip, ifconfig output:".format( raw ) )
2436 # ELSE: attempt to get address matching nicPat.
Jon Hall6c44c0b2016-04-20 15:21:00 -07002437 if nicPat:
2438 for ip in ips:
2439 curMatch = re.search( nicPat, ip )
2440 if curMatch:
2441 self.ip_address = ip
2442 return ip
Jon Hall8f6d4622016-05-23 15:27:18 -07002443 else: # If only one non-localhost ip, return that
2444 tmpList = [ ip for ip in ips if ip is not LOCALHOST ]
Jon Hall6c44c0b2016-04-20 15:21:00 -07002445 if len(tmpList) == 1:
2446 curIp = tmpList[0]
2447 self.ip_address = curIp
2448 return curIp
Jon Hall8f6d4622016-05-23 15:27:18 -07002449 # Either no non-localhost IPs, or more than 1
Jon Hallebccd352016-05-20 15:17:17 -07002450 main.log.warn( "getIpAddr failed to find a public IP address" )
Jon Hall8f6d4622016-05-23 15:27:18 -07002451 return LOCALHOST
Jon Halle1790952016-05-23 15:51:46 -07002452 except subprocess.CalledProcessError:
Jon Hall8f6d4622016-05-23 15:27:18 -07002453 main.log.exception( "Error executing ifconfig" )
2454 except IndexError:
2455 main.log.exception( "Error getting IP Address" )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002456 except Exception:
2457 main.log.exception( "Uncaught exception" )
suibin zhang116647a2016-05-06 16:30:09 -07002458
Devin Lim461f0872017-06-05 16:49:33 -07002459 def startBasicONOS( self, nodeList, opSleep=60, onosStartupSleep=30, onosUser="sdn" ):
suibin zhang116647a2016-05-06 16:30:09 -07002460 '''
2461 Start onos cluster with defined nodes, but only with drivers app
suibin zhang116647a2016-05-06 16:30:09 -07002462 '''
2463 import time
2464
2465 self.createCellFile( self.ip_address,
2466 "temp",
2467 self.ip_address,
2468 "drivers",
Devin Lim461f0872017-06-05 16:49:33 -07002469 nodeList, onosUser )
suibin zhang116647a2016-05-06 16:30:09 -07002470
2471 main.log.info( self.name + ": Apply cell to environment" )
2472 cellResult = self.setCell( "temp" )
2473 verifyResult = self.verifyCell()
2474
2475 main.log.info( self.name + ": Creating ONOS package" )
You Wangd1bcaca2016-10-24 15:23:26 -07002476 packageResult = self.buckBuild( timeout=opSleep )
suibin zhang116647a2016-05-06 16:30:09 -07002477
You Wangc669d212017-01-25 11:09:48 -08002478 main.log.info( self.name + ": Uninstalling ONOS" )
2479 for nd in nodeList:
2480 self.onosUninstall( nodeIp=nd )
2481
suibin zhang116647a2016-05-06 16:30:09 -07002482 main.log.info( self.name + ": Installing ONOS package" )
2483 for nd in nodeList:
You Wangc669d212017-01-25 11:09:48 -08002484 self.onosInstall( node=nd )
2485
2486 main.log.info( self.name + ": Set up ONOS secure SSH" )
2487 for nd in nodeList:
2488 self.onosSecureSSH( node=nd )
suibin zhang116647a2016-05-06 16:30:09 -07002489
2490 main.log.info( self.name + ": Starting ONOS service" )
2491 time.sleep( onosStartupSleep )
2492
2493 onosStatus = True
2494 for nd in nodeList:
2495 onosStatus = onosStatus & self.isup( node = nd )
2496 #print "onosStatus is: " + str( onosStatus )
2497
2498 return main.TRUE if onosStatus else main.FALSE
2499
Devin Lim02075272017-07-10 15:33:21 -07002500 def onosNetCfg( self, controllerIp, path, fileName ):
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002501 """
2502 Push a specified json file to ONOS through the onos-netcfg service
2503
2504 Required:
Devin Lim02075272017-07-10 15:33:21 -07002505 controllerIp - the Ip of the ONOS node in the cluster
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002506 path - the location of the file to be sent
2507 fileName - name of the json file to be sent
2508
2509 Returns main.TRUE on successfully sending json file, and main.FALSE if
2510 there is an error.
2511 """
2512 try:
Devin Lim02075272017-07-10 15:33:21 -07002513 cmd = "onos-netcfg {0} {1}{2}".format( controllerIp, path, fileName )
2514 main.log.info( "Sending: " + cmd )
2515 main.ONOSbench.handle.sendline( cmd )
Jeremy Ronquillo3008aa32017-07-07 15:38:57 -07002516 main.ONOSbench.handle.expect( self.prompt )
Devin Lim02075272017-07-10 15:33:21 -07002517 handle = self.handle.before
Jeremy Ronquillo3008aa32017-07-07 15:38:57 -07002518 if "Error" in handle or "No such file or directory" in handle or "curl: " in handle:
2519 main.log.error( self.name + ": " + handle + self.handle.after )
Devin Lim02075272017-07-10 15:33:21 -07002520 return main.FALSE
Devin Lim752dd7b2017-06-27 14:40:03 -07002521 return main.TRUE
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002522 except pexpect.EOF:
2523 main.log.error( self.name + ": EOF exception found" )
2524 main.log.error( self.name + ": " + self.handle.before )
2525 main.cleanup()
2526 main.exit()
2527 except Exception:
2528 main.log.exception( self.name + ": Uncaught exception!" )
2529 main.cleanup()
Jeremy Songsterae01bba2016-07-11 15:39:17 -07002530 main.exit()