blob: a666d77572ca15570ff74cbb2e96eefbf6fa5fef [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"""
Jeremy Ronquilloec916a42018-02-02 13:05:57 -08004Copyright 2014 Open Networking Foundation (ONF)
5
kelvin8ec71442015-01-15 16:57:00 -08006This driver interacts with ONOS bench, the OSGi platform
7that configures the ONOS nodes. ( aka ONOS-next )
andrewonlabe8e56fd2014-10-09 17:12:44 -04008
kelvin8ec71442015-01-15 16:57:00 -08009Please follow the coding style demonstrated by existing
andrewonlabe8e56fd2014-10-09 17:12:44 -040010functions and document properly.
11
12If you are a contributor to the driver, please
13list your email here for future contact:
14
15jhall@onlab.us
16andrew@onlab.us
17
18OCT 9 2014
Jeremy Songsterae01bba2016-07-11 15:39:17 -070019Modified 2016 by ON.Lab
20
21Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
22the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
23or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
andrewonlabe8e56fd2014-10-09 17:12:44 -040024
kelvin8ec71442015-01-15 16:57:00 -080025"""
Jon Hall05b2b432014-10-08 19:53:25 -040026import time
Jon Hall6801cda2015-07-15 14:13:45 -070027import types
Jon Hall05b2b432014-10-08 19:53:25 -040028import pexpect
kelvin-onlaba4074292015-07-09 15:19:49 -070029import os
Jon Hall6c44c0b2016-04-20 15:21:00 -070030import re
31import subprocess
pingping-lin6d23d9e2015-02-02 16:54:24 -080032from requests.models import Response
Jon Hall05b2b432014-10-08 19:53:25 -040033from drivers.common.clidriver import CLI
34
kelvin8ec71442015-01-15 16:57:00 -080035class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040036
kelvin8ec71442015-01-15 16:57:00 -080037 def __init__( self ):
38 """
39 Initialize client
40 """
Jon Hallefbd9792015-03-05 16:11:36 -080041 self.name = None
42 self.home = None
43 self.handle = None
Jon Hall6c44c0b2016-04-20 15:21:00 -070044 self.nicAddr = None
Devin Limdc78e202017-06-09 18:30:07 -070045 super( OnosDriver, self ).__init__()
kelvin8ec71442015-01-15 16:57:00 -080046
47 def connect( self, **connectargs ):
48 """
Jon Hall05b2b432014-10-08 19:53:25 -040049 Creates ssh handle for ONOS "bench".
kelvin-onlaba4074292015-07-09 15:19:49 -070050 NOTE:
51 The ip_address would come from the topo file using the host tag, the
52 value can be an environment variable as well as a "localhost" to get
53 the ip address needed to ssh to the "bench"
kelvin8ec71442015-01-15 16:57:00 -080054 """
Jon Hall05b2b432014-10-08 19:53:25 -040055 try:
Devin Limdc78e202017-06-09 18:30:07 -070056
Jon Hall05b2b432014-10-08 19:53:25 -040057 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080058 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us3b087132015-03-11 15:00:08 -070059 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040060 for key in self.options:
61 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080062 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040063 break
Jon Hall274b6642015-02-17 11:57:17 -080064 if self.home is None or self.home == "":
Jon Halle94919c2015-03-23 11:42:57 -070065 self.home = "~/onos"
Jon Hall21270ac2015-02-16 17:59:55 -080066
kelvin8ec71442015-01-15 16:57:00 -080067 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070068
kelvin-onlabc2b79102015-07-14 11:41:20 -070069 # The 'nodes' tag is optional and it is not required in .topo file
kelvin-onlaba4074292015-07-09 15:19:49 -070070 for key in self.options:
71 if key == "nodes":
kelvin-onlabc2b79102015-07-14 11:41:20 -070072 # Maximum number of ONOS nodes to run, if there is any
kelvin-onlaba4074292015-07-09 15:19:49 -070073 self.maxNodes = int( self.options[ 'nodes' ] )
74 break
75 self.maxNodes = None
76
Jeremy Ronquillo82705492017-10-18 14:19:55 -070077 if self.maxNodes is None or self.maxNodes == "":
kelvin-onlabc2b79102015-07-14 11:41:20 -070078 self.maxNodes = 100
kelvin-onlaba4074292015-07-09 15:19:49 -070079
kelvin-onlabc2b79102015-07-14 11:41:20 -070080 # Grabs all OC environment variables based on max number of nodes
kelvin-onlaba4074292015-07-09 15:19:49 -070081 self.onosIps = {} # Dictionary of all possible ONOS ip
82
83 try:
84 if self.maxNodes:
kelvin-onlaba4074292015-07-09 15:19:49 -070085 for i in range( self.maxNodes ):
86 envString = "OC" + str( i + 1 )
kelvin-onlabc2b79102015-07-14 11:41:20 -070087 # If there is no more OC# then break the loop
88 if os.getenv( envString ):
89 self.onosIps[ envString ] = os.getenv( envString )
90 else:
91 self.maxNodes = len( self.onosIps )
92 main.log.info( self.name +
93 ": Created cluster data with " +
94 str( self.maxNodes ) +
95 " maximum number" +
96 " of nodes" )
97 break
kelvin-onlaba4074292015-07-09 15:19:49 -070098
99 if not self.onosIps:
100 main.log.info( "Could not read any environment variable"
101 + " please load a cell file with all" +
102 " onos IP" )
Jon Hall5cf14d52015-07-16 12:15:19 -0700103 self.maxNodes = None
kelvin-onlaba4074292015-07-09 15:19:49 -0700104 else:
105 main.log.info( self.name + ": Found " +
106 str( self.onosIps.values() ) +
107 " ONOS IPs" )
kelvin-onlaba4074292015-07-09 15:19:49 -0700108 except KeyError:
109 main.log.info( "Invalid environment variable" )
110 except Exception as inst:
111 main.log.error( "Uncaught exception: " + str( inst ) )
112
113 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700114 if os.getenv( str( self.ip_address ) ) is not None:
kelvin-onlaba4074292015-07-09 15:19:49 -0700115 self.ip_address = os.getenv( str( self.ip_address ) )
116 else:
117 main.log.info( self.name +
118 ": Trying to connect to " +
119 self.ip_address )
kelvin-onlaba4074292015-07-09 15:19:49 -0700120 except KeyError:
121 main.log.info( "Invalid host name," +
122 " connecting to local host instead" )
123 self.ip_address = 'localhost'
124 except Exception as inst:
125 main.log.error( "Uncaught exception: " + str( inst ) )
126
kelvin8ec71442015-01-15 16:57:00 -0800127 self.handle = super( OnosDriver, self ).connect(
128 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -0800129 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800130 port=self.port,
131 pwd=self.pwd,
132 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -0400133
Jon Hall05b2b432014-10-08 19:53:25 -0400134 if self.handle:
Jon Hall0fc0d452015-07-14 09:49:58 -0700135 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700136 self.handle.expect( self.prompt )
Jon Hall05b2b432014-10-08 19:53:25 -0400137 return self.handle
kelvin8ec71442015-01-15 16:57:00 -0800138 else:
Jon Hall0fc0d452015-07-14 09:49:58 -0700139 main.log.info( "Failed to create ONOS handle" )
Jon Hall05b2b432014-10-08 19:53:25 -0400140 return main.FALSE
141 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800142 main.log.error( self.name + ": EOF exception found" )
143 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700144 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800145 except Exception:
146 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700147 main.cleanAndExit()
Jon Hall05b2b432014-10-08 19:53:25 -0400148
kelvin8ec71442015-01-15 16:57:00 -0800149 def disconnect( self ):
150 """
Jon Hall05b2b432014-10-08 19:53:25 -0400151 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800152 """
Jon Halld61331b2015-02-17 16:35:47 -0800153 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -0400154 try:
Jon Hall61282e32015-03-19 11:34:11 -0700155 if self.handle:
156 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700157 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700158 self.handle.sendline( "exit" )
159 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -0400160 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800161 main.log.error( self.name + ": EOF exception found" )
162 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700163 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700164 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700165 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800166 except Exception:
167 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -0400168 response = main.FALSE
169 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400170
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400171 def getEpochMs( self ):
172 """
173 Returns milliseconds since epoch
Jon Hall4ba53f02015-07-29 13:07:41 -0700174
175 When checking multiple nodes in a for loop,
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000176 around a hundred milliseconds of difference (ascending) is
Jon Hall4ba53f02015-07-29 13:07:41 -0700177 generally acceptable due to calltime of the function.
178 Few seconds, however, is not and it means clocks
179 are off sync.
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400180 """
181 try:
182 self.handle.sendline( 'date +%s.%N' )
183 self.handle.expect( 'date \+\%s\.\%N' )
Devin Limdc78e202017-06-09 18:30:07 -0700184 self.handle.expect( self.prompt )
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400185 epochMs = self.handle.before
186 return epochMs
187 except Exception:
188 main.log.exception( 'Uncaught exception getting epoch time' )
Devin Lim44075962017-08-11 10:56:37 -0700189 main.cleanAndExit()
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400190
Jon Hall6c44c0b2016-04-20 15:21:00 -0700191 def onosPackage( self, opTimeout=180 ):
kelvin8ec71442015-01-15 16:57:00 -0800192 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400193 Produce a self-contained tar.gz file that can be deployed
Jon Hall64af8502015-12-15 10:09:33 -0800194 and executed on any platform with Java 8 JRE.
kelvin8ec71442015-01-15 16:57:00 -0800195 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400196 try:
Jon Hall64af8502015-12-15 10:09:33 -0800197 ret = main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800198 self.handle.sendline( "onos-package" )
199 self.handle.expect( "onos-package" )
Jon Hall96451092016-05-04 09:42:30 -0700200 while True:
201 i = self.handle.expect( [ "Downloading",
202 "Unknown options",
203 "No such file or directory",
204 "tar.gz",
Devin Limc20e79a2017-06-07 10:29:57 -0700205 self.prompt ],
Jon Hall96451092016-05-04 09:42:30 -0700206 opTimeout )
207 handle = str( self.handle.before + self.handle.after )
208 if i == 0:
209 # Give more time to download the file
210 continue # expect again
211 elif i == 1:
212 # Incorrect usage
213 main.log.error( "onos-package does not recognize the given options" )
214 ret = main.FALSE
215 continue # expect again
216 elif i == 2:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000217 # File(s) not found
Jon Hall96451092016-05-04 09:42:30 -0700218 main.log.error( "onos-package could not find a file or directory" )
219 ret = main.FALSE
220 continue # expect again
221 elif i == 3:
222 # tar.gz
223 continue # expect again
224 elif i == 4:
225 # Prompt returned
226 break
Jon Hallc6793552016-01-19 14:18:37 -0800227 main.log.info( "onos-package command returned: " + handle )
kelvin8ec71442015-01-15 16:57:00 -0800228 # As long as the sendline does not time out,
229 # return true. However, be careful to interpret
230 # the results of the onos-package command return
Jon Hall64af8502015-12-15 10:09:33 -0800231 return ret
232 except pexpect.TIMEOUT:
233 main.log.exception( self.name + ": TIMEOUT exception found in onosPackage" )
234 main.log.error( self.name + ": " + self.handle.before )
235 return main.FALSE
andrewonlab7735d852014-10-09 13:02:47 -0400236 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800237 main.log.error( self.name + ": EOF exception found" )
238 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700239 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800240 except Exception:
241 main.log.exception( "Failed to package ONOS" )
Devin Lim44075962017-08-11 10:56:37 -0700242 main.cleanAndExit()
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400243
kelvin-onlabd3b64892015-01-20 13:26:24 -0800244 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800245 """
andrewonlab8790abb2014-11-06 13:51:54 -0500246 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800247 """
andrewonlab8790abb2014-11-06 13:51:54 -0500248 try:
kelvin8ec71442015-01-15 16:57:00 -0800249 self.handle.sendline( "onos-build" )
250 self.handle.expect( "onos-build" )
251 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800252 "BUILD SUCCESS",
253 "ERROR",
254 "BUILD FAILED" ],
255 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800256 handle = str( self.handle.before )
Devin Limc20e79a2017-06-07 10:29:57 -0700257 self.handle.expect( self.prompt )
andrewonlab8790abb2014-11-06 13:51:54 -0500258
kelvin8ec71442015-01-15 16:57:00 -0800259 main.log.info( "onos-build command returned: " +
260 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500261
262 if i == 0:
263 return main.TRUE
264 else:
265 return handle
266
267 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800268 main.log.error( self.name + ": EOF exception found" )
269 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800270 except Exception:
271 main.log.exception( "Failed to build ONOS" )
Devin Lim44075962017-08-11 10:56:37 -0700272 main.cleanAndExit()
andrewonlab8790abb2014-11-06 13:51:54 -0500273
shahshreya9f531fe2015-06-10 12:03:51 -0700274 def cleanInstall( self, skipTest=False, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800275 """
276 Runs mvn clean install in the root of the ONOS directory.
277 This will clean all ONOS artifacts then compile each module
shahshreya9f531fe2015-06-10 12:03:51 -0700278 Optional:
279 skipTest - Does "-DskipTests -Dcheckstyle.skip -U -T 1C" which
280 skip the test. This will make the building faster.
281 Disregarding the credibility of the build
kelvin8ec71442015-01-15 16:57:00 -0800282 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400283 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800284 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400285 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800286 main.log.info( "Running 'mvn clean install' on " +
287 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800288 ". This may take some time." )
289 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700290 self.handle.expect( self.prompt )
Jon Hallea7818b2014-10-09 14:30:59 -0400291
kelvin8ec71442015-01-15 16:57:00 -0800292 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700293 self.handle.expect( self.prompt )
shahshreya9f531fe2015-06-10 12:03:51 -0700294
295 if not skipTest:
296 self.handle.sendline( "mvn clean install" )
297 self.handle.expect( "mvn clean install" )
298 else:
299 self.handle.sendline( "mvn clean install -DskipTests" +
300 " -Dcheckstyle.skip -U -T 1C" )
301 self.handle.expect( "mvn clean install -DskipTests" +
302 " -Dcheckstyle.skip -U -T 1C" )
kelvin8ec71442015-01-15 16:57:00 -0800303 while True:
304 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800305 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800306 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400307 'BUILD\sFAILURE',
308 'BUILD\sSUCCESS',
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700309 'onos' + self.prompt, # TODO: fix this to be more generic?
Devin Limdc78e202017-06-09 18:30:07 -0700310 'ONOS' + self.prompt,
pingping-lin57a56ce2015-05-20 16:43:48 -0700311 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400312 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800313 main.log.error( self.name + ":There is insufficient memory \
314 for the Java Runtime Environment to continue." )
315 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700316
317 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400318 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800319 main.log.error( self.name + ": Build failure!" )
320 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700321
322 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400323 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800324 main.log.info( self.name + ": Build success!" )
Jon Halle94919c2015-03-23 11:42:57 -0700325 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800326 main.log.info( self.name + ": Build complete" )
327 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400328 for line in self.handle.before.splitlines():
329 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800330 main.log.info( line )
331 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700332 self.handle.expect( self.prompt, timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400333 return main.TRUE
Jon Halle94919c2015-03-23 11:42:57 -0700334 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800335 main.log.error(
336 self.name +
337 ": mvn clean install TIMEOUT!" )
338 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700339
340 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400341 else:
Jon Hall274b6642015-02-17 11:57:17 -0800342 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800343 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800344 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700345
346 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400347 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800348 main.log.error( self.name + ": EOF exception found" )
349 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700350 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800351 except Exception:
352 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700353 main.cleanAndExit()
Jon Hallacabffd2014-10-09 12:36:53 -0400354
Jon Hall3576f572016-08-23 10:01:07 -0700355 def buckBuild( self, timeout=180 ):
356 """
357 Build onos using buck.
358 """
359 try:
360 ret = main.TRUE
361 self.handle.sendline( "buck build onos" )
362 self.handle.expect( "buck build onos" )
363 output = ""
364 while True:
365 i = self.handle.expect( [ "This does not appear to be the root of a Buck project.",
366 "\n",
367 "BUILD FAILED",
Jon Halld9066132018-03-01 14:52:53 -0800368 "no buck in",
Devin Limc20e79a2017-06-07 10:29:57 -0700369 self.prompt ],
Jon Hall3576f572016-08-23 10:01:07 -0700370 timeout=timeout )
371 output += str( self.handle.before + self.handle.after )
372 if i == 0:
373 main.log.error( "Wrong location" )
374 ret = main.FALSE
375 elif i == 1:
376 # end of a line, buck is still printing output
377 pass
378 elif i == 2:
379 # Build failed
380 main.log.error( "Build failed" )
381 ret = main.FALSE
382 elif i == 3:
Jon Halld9066132018-03-01 14:52:53 -0800383 main.log.error( "Could not find buck in your PATH." )
384 ret = main.FALSE
385 elif i == 4:
Jon Hall3576f572016-08-23 10:01:07 -0700386 # Prompt returned
387 break
388 main.log.debug( output )
389 return ret
390 except pexpect.TIMEOUT:
391 main.log.exception( self.name + ": TIMEOUT exception found" )
392 main.log.error( self.name + ": " + self.handle.before )
393 return main.FALSE
394 except pexpect.EOF:
395 main.log.error( self.name + ": EOF exception found" )
396 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700397 main.cleanAndExit()
Jon Hall3576f572016-08-23 10:01:07 -0700398 except Exception:
399 main.log.exception( "Failed to build and package ONOS" )
Devin Lim44075962017-08-11 10:56:37 -0700400 main.cleanAndExit()
Jon Hall3576f572016-08-23 10:01:07 -0700401
Jon Hall61282e32015-03-19 11:34:11 -0700402 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800403 """
Jon Hallacabffd2014-10-09 12:36:53 -0400404 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800405
Jon Hall61282e32015-03-19 11:34:11 -0700406 If the fastForward boolean is set to true, only git pulls that can
407 be fast forwarded will be performed. IE if you have not local commits
408 in your branch.
409
Jon Hallacabffd2014-10-09 12:36:53 -0400410 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800411 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400412 for the purpose of pulling from other nodes if necessary.
413
Jon Hall47a93fb2015-01-06 16:46:06 -0800414 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400415 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800416 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400417 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400418
kelvin8ec71442015-01-15 16:57:00 -0800419 """
Jon Hallacabffd2014-10-09 12:36:53 -0400420 try:
kelvin8ec71442015-01-15 16:57:00 -0800421 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700422 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700423 cmd = "git pull"
424 if comp1 != "":
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700425 cmd += ' ' + comp1
Jon Hall61282e32015-03-19 11:34:11 -0700426 if fastForward:
427 cmd += ' ' + " --ff-only"
428 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800429 i = self.handle.expect(
430 [
431 'fatal',
432 'Username\sfor\s(.*):\s',
433 '\sfile(s*) changed,\s',
434 'Already up-to-date',
435 'Aborting',
436 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800437 'You asked me to pull without telling me which branch you',
438 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700439 'Please enter a commit message to explain why this merge',
440 'Found a swap file by the name',
441 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800442 pexpect.TIMEOUT ],
443 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800444 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700445 main.log.error( self.name + ": Git pull had some issue" )
446 output = self.handle.after
Devin Limdc78e202017-06-09 18:30:07 -0700447 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700448 output += self.handle.before
449 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400450 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800451 elif i == 1:
452 main.log.error(
453 self.name +
454 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400455 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800456 elif i == 2:
457 main.log.info(
458 self.name +
459 ": Git Pull - pulling repository now" )
Devin Limc20e79a2017-06-07 10:29:57 -0700460 self.handle.expect( self.prompt, 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800461 # So that only when git pull is done, we do mvn clean compile
462 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800463 elif i == 3:
464 main.log.info( self.name + ": Git Pull - Already up to date" )
Devin Limc20e79a2017-06-07 10:29:57 -0700465 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800466 elif i == 4:
467 main.log.info(
468 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800469 ": Git Pull - Aborting..." +
470 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400471 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800472 elif i == 5:
473 main.log.info(
474 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800475 ": Git Pull - You are not currently " +
476 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400477 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800478 elif i == 6:
479 main.log.info(
480 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800481 ": Git Pull - You have not configured an upstream " +
482 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400483 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800484 elif i == 7:
485 main.log.info(
486 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800487 ": Git Pull - Pull is not possible because " +
488 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400489 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800490 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700491 # NOTE: abandoning test since we can't reliably handle this
492 # there could be different default text editors and we
493 # also don't know if we actually want to make the commit
494 main.log.error( "Git pull resulted in a merge commit message" +
495 ". Exiting test!" )
Devin Lim44075962017-08-11 10:56:37 -0700496
497 main.cleanAndExit()
Jon Hall61282e32015-03-19 11:34:11 -0700498 elif i == 9: # Merge commit message but swap file exists
499 main.log.error( "Git pull resulted in a merge commit message" +
500 " but a swap file exists." )
501 try:
502 self.handle.send( 'A' ) # Abort
Devin Limc20e79a2017-06-07 10:29:57 -0700503 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700504 return main.ERROR
505 except Exception:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700506 main.log.exception( "Couldn't exit editor prompt!" )
Devin Lim44075962017-08-11 10:56:37 -0700507
508 main.cleanAndExit()
Jon Hall61282e32015-03-19 11:34:11 -0700509 elif i == 10: # In the middle of a merge commit
510 main.log.error( "Git branch is in the middle of a merge. " )
511 main.log.warn( self.handle.before + self.handle.after )
512 return main.ERROR
513 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800514 main.log.error( self.name + ": Git Pull - TIMEOUT" )
515 main.log.error(
516 self.name + " Response was: " + str(
517 self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400518 return main.ERROR
519 else:
kelvin8ec71442015-01-15 16:57:00 -0800520 main.log.error(
521 self.name +
522 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400523 return main.ERROR
524 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800525 main.log.error( self.name + ": EOF exception found" )
526 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700527 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800528 except Exception:
529 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700530 main.cleanAndExit()
Jon Hallacabffd2014-10-09 12:36:53 -0400531
kelvin-onlabd3b64892015-01-20 13:26:24 -0800532 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800533 """
Jon Hallacabffd2014-10-09 12:36:53 -0400534 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800535
Jon Hallacabffd2014-10-09 12:36:53 -0400536 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800537 If used as gitCheckout( "branch" ) it will do git checkout
538 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400539
540 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800541 branch of the ONOS repository. If it has any problems, it will return
542 main.ERROR.
543 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400544 successful then the function will return main.TRUE.
545
kelvin8ec71442015-01-15 16:57:00 -0800546 """
Jon Hallacabffd2014-10-09 12:36:53 -0400547 try:
kelvin8ec71442015-01-15 16:57:00 -0800548 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700549 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800550 main.log.info( self.name +
551 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800552 cmd = "git checkout " + branch
553 self.handle.sendline( cmd )
554 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800555 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800556 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700557 'Username for (.*): ',
558 'Already on \'',
Jon Hall7a8354f2015-06-10 15:37:00 -0700559 'Switched to (a new )?branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800560 pexpect.TIMEOUT,
561 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800562 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800563 'error: you need to resolve your current index first',
564 "You are in 'detached HEAD' state.",
565 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800566 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800567 if i == 0:
568 main.log.error(
569 self.name +
570 ": Git checkout had some issue..." )
571 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400572 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800573 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800574 main.log.error(
575 self.name +
576 ": Git checkout asking for username." +
577 " Please configure your local git repository to be able " +
578 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800579 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400580 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800581 elif i == 2:
582 main.log.info(
583 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800584 ": Git Checkout %s : Already on this branch" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700585 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800586 # main.log.info( "DEBUG: after checkout cmd = "+
587 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400588 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800589 elif i == 3:
590 main.log.info(
591 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800592 ": Git checkout %s - Switched to this branch" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700593 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800594 # main.log.info( "DEBUG: after checkout cmd = "+
595 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400596 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800597 elif i == 4:
598 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
599 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800600 self.name + " Response was: " + str( self.handle.before ) )
Jon Hallacabffd2014-10-09 12:36:53 -0400601 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800602 elif i == 5:
603 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800604 main.log.error(
605 self.name +
606 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800607 "Your local changes to the following files would" +
608 " be overwritten by checkout:" +
609 str( self.handle.before ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700610 self.handle.expect( self.prompt )
Jon Hall81e29af2014-11-04 20:41:23 -0500611 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800612 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800613 main.log.error(
614 self.name +
615 ": Git checkout error: \n" +
616 "You need to resolve your current index first:" +
617 str( self.handle.before ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700618 self.handle.expect( self.prompt )
Jon Hall81e29af2014-11-04 20:41:23 -0500619 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800620 elif i == 7:
621 main.log.info(
622 self.name +
623 ": Git checkout " + str( branch ) +
624 " - You are in 'detached HEAD' state. HEAD is now at " +
625 str( branch ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700626 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800627 return main.TRUE
628 elif i == 8: # Already in detached HEAD on the specified commit
629 main.log.info(
630 self.name +
631 ": Git Checkout %s : Already on commit" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700632 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800633 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400634 else:
kelvin8ec71442015-01-15 16:57:00 -0800635 main.log.error(
636 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800637 ": Git Checkout - Unexpected response, " +
638 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800639 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400640 return main.ERROR
641
642 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800643 main.log.error( self.name + ": EOF exception found" )
644 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700645 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800646 except Exception:
647 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700648 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400649
pingping-lin6d23d9e2015-02-02 16:54:24 -0800650 def getBranchName( self ):
You Wang9cdf9a22017-05-01 13:44:18 -0700651 import re
652 try:
653 main.log.info( "self.home = " )
654 main.log.info( self.home )
655 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700656 self.handle.expect( self.prompt )
You Wang9cdf9a22017-05-01 13:44:18 -0700657 self.handle.sendline( "git name-rev --name-only HEAD" )
658 self.handle.expect( "git name-rev --name-only HEAD" )
Devin Limc20e79a2017-06-07 10:29:57 -0700659 self.handle.expect( self.prompt )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700660 lines = self.handle.before.splitlines()
661 if lines[ 1 ] == "master" or re.search( "^onos-\d+(\.\d+)+$", lines[ 1 ] ):
662 return lines[ 1 ]
You Wang9cdf9a22017-05-01 13:44:18 -0700663 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700664 main.log.info( lines[ 1 ] )
You Wang9cdf9a22017-05-01 13:44:18 -0700665 return "unexpected ONOS branch"
666 except pexpect.EOF:
667 main.log.error( self.name + ": EOF exception found" )
668 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700669 main.cleanAndExit()
You Wang9cdf9a22017-05-01 13:44:18 -0700670 except pexpect.TIMEOUT:
671 main.log.error( self.name + ": TIMEOUT exception found" )
672 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700673 main.cleanAndExit()
You Wang9cdf9a22017-05-01 13:44:18 -0700674 except Exception:
675 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700676 main.cleanAndExit()
pingping-lin6d23d9e2015-02-02 16:54:24 -0800677
kelvin-onlabd3b64892015-01-20 13:26:24 -0800678 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800679 """
Jon Hall274b6642015-02-17 11:57:17 -0800680 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800681 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800682 """
Jon Hall45ec0922014-10-10 19:33:49 -0400683 try:
kelvin8ec71442015-01-15 16:57:00 -0800684 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700685 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800686 self.handle.sendline(
687 "cd " +
688 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800689 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
690 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800691 # NOTE: for some reason there are backspaces inserted in this
692 # phrase when run from Jenkins on some tests
693 self.handle.expect( "never" )
Devin Limc20e79a2017-06-07 10:29:57 -0700694 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800695 response = ( self.name + ": \n" + str(
696 self.handle.before + self.handle.after ) )
697 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700698 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800699 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400700 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500701 print line
702 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700703 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800704 for line in lines[ 2:-1 ]:
705 # Bracket replacement is for Wiki-compliant
706 # formatting. '<' or '>' are interpreted
707 # as xml specific tags that cause errors
708 line = line.replace( "<", "[" )
709 line = line.replace( ">", "]" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700710 # main.log.wiki( "\t" + line )
pingping-lin763ee042015-05-20 17:45:30 -0700711 main.log.wiki( line + "<br /> " )
712 main.log.summary( line )
713 main.log.wiki( "</blockquote>" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700714 main.log.summary( "\n" )
kelvin8ec71442015-01-15 16:57:00 -0800715 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400716 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800717 main.log.error( self.name + ": EOF exception found" )
718 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700719 main.cleanAndExit()
Jon Hall368769f2014-11-19 15:43:35 -0800720 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800721 main.log.error( self.name + ": TIMEOUT exception found" )
722 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700723 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800724 except Exception:
725 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700726 main.cleanAndExit()
Jon Hall45ec0922014-10-10 19:33:49 -0400727
kelvin-onlabd3b64892015-01-20 13:26:24 -0800728 def createCellFile( self, benchIp, fileName, mnIpAddrs,
Jon Hall810b67d2016-12-05 10:19:01 -0800729 appString, onosIpAddrs, onosUser="sdn", useSSH=True ):
kelvin8ec71442015-01-15 16:57:00 -0800730 """
andrewonlab94282092014-10-10 13:00:11 -0400731 Creates a cell file based on arguments
732 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800733 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400734 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800735 * File name of the cell file ( fileName )
736 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800737 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400738 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800739 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400740 - Must be passed in as last arguments
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000741 * ONOS USER (onosUser)
Flavio Castrocc38a542016-03-03 13:15:46 -0800742 - optional argument to set ONOS_USER environment variable
kelvin8ec71442015-01-15 16:57:00 -0800743
andrewonlab94282092014-10-10 13:00:11 -0400744 NOTE: Assumes cells are located at:
745 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800746 """
andrewonlab94282092014-10-10 13:00:11 -0400747 try:
Devin Lim461f0872017-06-05 16:49:33 -0700748
Jon Hall2c8959e2016-12-16 12:17:34 -0800749 # Variable initialization
750 cellDirectory = self.home + "/tools/test/cells/"
751 # We want to create the cell file in the dependencies directory
752 # of TestON first, then copy over to ONOS bench
753 tempDirectory = "/tmp/"
754 # Create the cell file in the directory for writing ( w+ )
755 cellFile = open( tempDirectory + fileName, 'w+' )
756 if isinstance( onosIpAddrs, types.StringType ):
757 onosIpAddrs = [ onosIpAddrs ]
758
759 # App string is hardcoded environment variables
760 # That you may wish to use by default on startup.
761 # Note that you may not want certain apps listed
762 # on here.
763 appString = "export ONOS_APPS=" + appString
764 onosGroup = "export ONOS_GROUP=" + onosUser
765 onosUser = "export ONOS_USER=" + onosUser
766 if useSSH:
767 onosUseSSH = "export ONOS_USE_SSH=true"
768 mnString = "export OCN="
769 if mnIpAddrs == "":
770 mnString = ""
771 onosString = "export OC"
772 tempCount = 1
773
774 # Create ONOSNIC ip address prefix
775 tempOnosIp = str( onosIpAddrs[ 0 ] )
776 tempList = []
777 tempList = tempOnosIp.split( "." )
778 # Omit last element of list to format for NIC
779 tempList = tempList[ :-1 ]
780 # Structure the nic string ip
781 nicAddr = ".".join( tempList ) + ".*"
782 self.nicAddr = nicAddr
783 onosNicString = "export ONOS_NIC=" + nicAddr
784
kelvin8ec71442015-01-15 16:57:00 -0800785 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800786 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400787
kelvin-onlabd3b64892015-01-20 13:26:24 -0800788 for arg in onosIpAddrs:
789 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800790 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400791 # export OC1="10.128.20.11"
792 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800793 cellFile.write( onosString + str( tempCount ) +
Jon Hall6f665652015-09-18 10:08:07 -0700794 "=\"" + arg + "\"\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800795 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800796
Jon Hall6f665652015-09-18 10:08:07 -0700797 cellFile.write( "export OCI=$OC1\n" )
Jon Hallab611372018-02-21 15:26:05 -0800798 if mnString:
799 cellFile.write( mnString + "\"" + str( mnIpAddrs ) + "\"\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700800 cellFile.write( appString + "\n" )
Flavio Castrocc38a542016-03-03 13:15:46 -0800801 cellFile.write( onosGroup + "\n" )
802 cellFile.write( onosUser + "\n" )
Pier88189b62016-09-07 17:01:53 -0700803 if useSSH:
804 cellFile.write( onosUseSSH + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800805 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400806
kelvin8ec71442015-01-15 16:57:00 -0800807 # We use os.system to send the command to TestON cluster
808 # to account for the case in which TestON is not located
809 # on the same cluster as the ONOS bench
810 # Note that even if TestON is located on the same cluster
811 # as ONOS bench, you must setup passwordless ssh
812 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabc2b79102015-07-14 11:41:20 -0700813 os.system( "scp " + tempDirectory + fileName + " " +
814 self.user_name + "@" + self.ip_address + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400815
andrewonlab2a6c9342014-10-16 13:40:15 -0400816 return main.TRUE
817
andrewonlab94282092014-10-10 13:00:11 -0400818 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800819 main.log.error( self.name + ": EOF exception found" )
820 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700821 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800822 except Exception:
823 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700824 main.cleanAndExit()
andrewonlab94282092014-10-10 13:00:11 -0400825
kelvin-onlabd3b64892015-01-20 13:26:24 -0800826 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800827 """
andrewonlab95ca1462014-10-09 14:04:24 -0400828 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800829 """
andrewonlab95ca1462014-10-09 14:04:24 -0400830 try:
831 if not cellname:
You Wang1cdc5f52017-12-19 16:47:51 -0800832 main.log.error( self.name + ": Must define cellname" )
Devin Lim44075962017-08-11 10:56:37 -0700833 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400834 else:
kelvin8ec71442015-01-15 16:57:00 -0800835 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800836 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800837 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400838 # and that this driver will have to change accordingly
Jon Hall3b489db2015-10-05 14:38:37 -0700839 self.handle.expect( str( cellname ) )
Jon Hallab611372018-02-21 15:26:05 -0800840 response = self.handle.before + self.handle.after
You Wang1cdc5f52017-12-19 16:47:51 -0800841 i = self.handle.expect( [ "No such cell",
Jon Hallab611372018-02-21 15:26:05 -0800842 "command not found",
843 self.prompt ], timeout=10 )
844 response += self.handle.before + self.handle.after
You Wang1cdc5f52017-12-19 16:47:51 -0800845 if i == 0:
Jon Hallab611372018-02-21 15:26:05 -0800846 main.log.error( self.name + ": No such cell. Response: " + str( response ) )
You Wang1cdc5f52017-12-19 16:47:51 -0800847 main.cleanAndExit()
848 elif i == 1:
Jon Hallab611372018-02-21 15:26:05 -0800849 main.log.error( self.name + ": Error setting cell. Response: " + str( response ) )
850 main.cleanAndExit()
You Wang1cdc5f52017-12-19 16:47:51 -0800851 elif i == 2:
Jon Hallab611372018-02-21 15:26:05 -0800852 main.log.info( self.name + ": Successfully set cell: " + str( response ) )
andrewonlab95ca1462014-10-09 14:04:24 -0400853 return main.TRUE
You Wang1cdc5f52017-12-19 16:47:51 -0800854 except pexpect.TIMEOUT:
855 main.log.error( self.name + ": TIMEOUT exception found" )
856 main.log.error( self.name + ": " + self.handle.before )
857 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400858 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800859 main.log.error( self.name + ": EOF exception found" )
860 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700861 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800862 except Exception:
863 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700864 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400865
kelvin-onlabd3b64892015-01-20 13:26:24 -0800866 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800867 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400868 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800869 """
870 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400871
andrewonlabc03bf6c2014-10-09 14:56:18 -0400872 try:
kelvin8ec71442015-01-15 16:57:00 -0800873 # Clean handle by sending empty and expecting $
874 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700875 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800876 self.handle.sendline( "onos-verify-cell" )
Devin Limc20e79a2017-06-07 10:29:57 -0700877 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800878 handleBefore = self.handle.before
879 handleAfter = self.handle.after
kelvin-onlabd3b64892015-01-20 13:26:24 -0800880 main.log.info( "Verify cell returned: " + handleBefore +
Jon Hall3b489db2015-10-05 14:38:37 -0700881 handleAfter )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400882 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800883 except pexpect.ExceptionPexpect as e:
Jon Hall3b489db2015-10-05 14:38:37 -0700884 main.log.exception( self.name + ": Pexpect exception found: " )
kelvin8ec71442015-01-15 16:57:00 -0800885 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700886 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800887 except Exception:
888 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700889 main.cleanAndExit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400890
jenkins1e99e7b2015-04-02 18:15:39 -0700891 def onosCfgSet( self, ONOSIp, configName, configParam ):
892 """
893 Uses 'onos <node-ip> cfg set' to change a parameter value of an
Jon Hall4ba53f02015-07-29 13:07:41 -0700894 application.
895
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000896 ex)
jenkins1e99e7b2015-04-02 18:15:39 -0700897 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
jenkins1e99e7b2015-04-02 18:15:39 -0700898 ONOSIp = '10.0.0.1'
899 configName = 'org.onosproject.myapp'
900 configParam = 'appSetting 1'
jenkins1e99e7b2015-04-02 18:15:39 -0700901 """
Jon Hall72280bc2016-01-25 14:29:05 -0800902 try:
903 cfgStr = "onos {} cfg set {} {}".format( ONOSIp,
904 configName,
905 configParam )
906 self.handle.sendline( "" )
907 self.handle.expect( ":~" )
908 self.handle.sendline( cfgStr )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700909 self.handle.expect( "cfg set" )
Jon Hall72280bc2016-01-25 14:29:05 -0800910 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700911
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700912 paramValue = configParam.split( " " )[ 1 ]
913 paramName = configParam.split( " " )[ 0 ]
Jon Hall4ba53f02015-07-29 13:07:41 -0700914
Jon Hall72280bc2016-01-25 14:29:05 -0800915 checkStr = 'onos {} cfg get " {} {} " '.format( ONOSIp, configName, paramName )
Jon Hall4ba53f02015-07-29 13:07:41 -0700916
Jon Hall72280bc2016-01-25 14:29:05 -0800917 self.handle.sendline( checkStr )
918 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700919
Jon Hall72280bc2016-01-25 14:29:05 -0800920 if "value=" + paramValue + "," in self.handle.before:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700921 main.log.info( "cfg " + configName + " successfully set to " + configParam )
Jon Hall72280bc2016-01-25 14:29:05 -0800922 return main.TRUE
923 except pexpect.ExceptionPexpect as e:
924 main.log.exception( self.name + ": Pexpect exception found: " )
925 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700926 main.cleanAndExit()
Jon Hall72280bc2016-01-25 14:29:05 -0800927 except Exception:
928 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700929 main.cleanAndExit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700930
You Wang54b1d672018-06-11 16:44:13 -0700931 def onosCli( self, ONOSIp, cmdstr, timeout=60 ):
kelvin8ec71442015-01-15 16:57:00 -0800932 """
andrewonlab05e362f2014-10-10 00:40:57 -0400933 Uses 'onos' command to send various ONOS CLI arguments.
934 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800935 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400936 * cmdstr: specify the command string to send
You Wang54b1d672018-06-11 16:44:13 -0700937 Optional:
938 * timeout: pexpect timeout for running the command
kelvin8ec71442015-01-15 16:57:00 -0800939
940 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -0400941 CLI commands for ONOS. Try to use this function first
942 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -0800943 function.
944 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -0400945 by starting onos, and typing in 'onos' to enter the
946 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -0800947 available commands.
948 """
andrewonlab05e362f2014-10-10 00:40:57 -0400949 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800950 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -0800951 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -0400952 return main.FALSE
953 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -0800954 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -0400955 return main.FALSE
956
kelvin8ec71442015-01-15 16:57:00 -0800957 cmdstr = str( cmdstr )
958 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700959 self.handle.expect( self.prompt )
andrewonlab05e362f2014-10-10 00:40:57 -0400960
You Wangdd3dae52018-02-15 13:31:25 -0800961 self.handle.sendline( "onos-wait-for-start " + ONOSIp )
962 self.handle.expect( self.prompt )
963
964 self.handle.sendline( "onos " + ONOSIp + " " + cmdstr )
You Wang54b1d672018-06-11 16:44:13 -0700965 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], timeout=timeout )
966 if i == 0:
967 handleBefore = self.handle.before
968 main.log.info( "Command sent successfully" )
969 # Obtain return handle that consists of result from
970 # the onos command. The string may need to be
971 # configured further.
972 returnString = handleBefore
973 return returnString
974 elif i == 1:
975 main.log.error( self.name + ": Timeout when sending " + cmdstr )
976 self.handle.sendline( "\x03" ) # Control-C
977 self.handle.expect( self.prompt )
978 return main.FALSE
You Wangd66de192018-04-30 17:30:12 -0700979 except pexpect.TIMEOUT:
980 main.log.exception( self.name + ": Timeout when sending " + cmdstr )
981 return main.FALSE
andrewonlab05e362f2014-10-10 00:40:57 -0400982 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800983 main.log.error( self.name + ": EOF exception found" )
984 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700985 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800986 except Exception:
987 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700988 main.cleanAndExit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400989
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700990 def onosSecureSSH( self, userName="onos", userPWD="rocks", node="" ):
Pier88189b62016-09-07 17:01:53 -0700991 """
992 Enables secure access to ONOS console
993 by removing default users & keys.
994
995 onos-secure-ssh -u onos -p rocks node
996
997 Returns: main.TRUE on success and main.FALSE on failure
998 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000999
Pier88189b62016-09-07 17:01:53 -07001000 try:
Chiyu Chengef109502016-11-21 15:51:38 -08001001 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001002 self.handle.expect( self.prompt )
Pier88189b62016-09-07 17:01:53 -07001003 self.handle.sendline( " onos-secure-ssh -u " + userName + " -p " + userPWD + " " + node )
1004
1005 # NOTE: this timeout may need to change depending on the network
1006 # and size of ONOS
1007 # TODO: Handle the other possible error
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001008 i = self.handle.expect( [ "Network\sis\sunreachable",
1009 self.prompt,
1010 pexpect.TIMEOUT ], timeout=180 )
Pier88189b62016-09-07 17:01:53 -07001011 if i == 0:
1012 # can't reach ONOS node
1013 main.log.warn( "Network is unreachable" )
Devin Limc20e79a2017-06-07 10:29:57 -07001014 self.handle.expect( self.prompt )
Pier88189b62016-09-07 17:01:53 -07001015 return main.FALSE
1016 elif i == 1:
1017 # Process started
1018 main.log.info(
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001019 "Secure SSH performed on " +
1020 node )
Pier88189b62016-09-07 17:01:53 -07001021 return main.TRUE
1022 except pexpect.EOF:
1023 main.log.error( self.name + ": EOF exception found" )
1024 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001025 main.cleanAndExit()
Pier88189b62016-09-07 17:01:53 -07001026 except Exception:
1027 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001028 main.cleanAndExit()
Pier88189b62016-09-07 17:01:53 -07001029
kelvin-onlabd3b64892015-01-20 13:26:24 -08001030 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001031 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001032 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -08001033 If -f option is provided, it also forces an uninstall.
1034 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -04001035 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -08001036 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -04001037 files to certain onos nodes
1038
1039 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -08001040 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001041 try:
andrewonlab114768a2014-11-14 12:44:44 -05001042 if options:
kelvin8ec71442015-01-15 16:57:00 -08001043 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -05001044 else:
kelvin8ec71442015-01-15 16:57:00 -08001045 self.handle.sendline( "onos-install " + node )
1046 self.handle.expect( "onos-install " )
1047 # NOTE: this timeout may need to change depending on the network
1048 # and size of ONOS
1049 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001050 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -08001051 "ONOS\sis\salready\sinstalled",
Jeremyc72b2582016-02-26 18:27:38 -08001052 "already\sup-to-date",
Jon Hall3576f572016-08-23 10:01:07 -07001053 "does not exist",
Devin Limc20e79a2017-06-07 10:29:57 -07001054 self.prompt,
Jon Hall6c44c0b2016-04-20 15:21:00 -07001055 pexpect.TIMEOUT ], timeout=180 )
Jon Hall7993bfc2014-10-09 16:30:14 -04001056 if i == 0:
Jon Hall3576f572016-08-23 10:01:07 -07001057 # can't reach ONOS node
kelvin8ec71442015-01-15 16:57:00 -08001058 main.log.warn( "Network is unreachable" )
Devin Limc20e79a2017-06-07 10:29:57 -07001059 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001060 return main.FALSE
1061 elif i == 1:
Jon Hall3576f572016-08-23 10:01:07 -07001062 # Process started
kelvin8ec71442015-01-15 16:57:00 -08001063 main.log.info(
1064 "ONOS was installed on " +
1065 node +
1066 " and started" )
Devin Limc20e79a2017-06-07 10:29:57 -07001067 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001068 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001069 elif i == 2 or i == 3:
1070 # same bits are already on ONOS node
Jeremyc72b2582016-02-26 18:27:38 -08001071 main.log.info( "ONOS is already installed on " + node )
Devin Limc20e79a2017-06-07 10:29:57 -07001072 self.handle.expect( self.prompt )
Jeremyc72b2582016-02-26 18:27:38 -08001073 return main.TRUE
1074 elif i == 4:
Jon Hall3576f572016-08-23 10:01:07 -07001075 # onos not packaged
1076 main.log.error( "ONOS package not found." )
Devin Limc20e79a2017-06-07 10:29:57 -07001077 self.handle.expect( self.prompt )
Jon Hall3576f572016-08-23 10:01:07 -07001078 return main.FALSE
1079 elif i == 5:
1080 # prompt
Jeremyc72b2582016-02-26 18:27:38 -08001081 main.log.info( "ONOS was installed on " + node )
1082 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001083 elif i == 6:
1084 # timeout
kelvin8ec71442015-01-15 16:57:00 -08001085 main.log.info(
1086 "Installation of ONOS on " +
1087 node +
1088 " timed out" )
Devin Limc20e79a2017-06-07 10:29:57 -07001089 self.handle.expect( self.prompt )
Jon Hall53c5e662016-04-13 16:06:56 -07001090 main.log.warn( self.handle.before )
Jon Hall7993bfc2014-10-09 16:30:14 -04001091 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -04001092 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001093 main.log.error( self.name + ": EOF exception found" )
1094 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001095 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001096 except Exception:
1097 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001098 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -04001099
kelvin-onlabd3b64892015-01-20 13:26:24 -08001100 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001101 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001102 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001103 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001104 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001105 try:
kelvin8ec71442015-01-15 16:57:00 -08001106 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001107 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001108 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001109 " start" )
1110 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -04001111 "Job\sis\salready\srunning",
1112 "start/running",
Devin Limc20e79a2017-06-07 10:29:57 -07001113 self.prompt,
andrewonlab8d0d7d72014-10-09 16:33:15 -04001114 "Unknown\sinstance",
Jeremy Songster14c13572016-04-21 17:34:03 -07001115 pexpect.TIMEOUT ], timeout=180 )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001116 if i == 0:
Devin Limc20e79a2017-06-07 10:29:57 -07001117 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001118 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001119 return main.TRUE
1120 elif i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001121 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001122 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001123 return main.TRUE
Jeremyd0e9a6d2016-03-02 11:28:52 -08001124 elif i == 2:
1125 main.log.info( "ONOS service started" )
1126 return main.TRUE
andrewonlab8d0d7d72014-10-09 16:33:15 -04001127 else:
Devin Limc20e79a2017-06-07 10:29:57 -07001128 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001129 main.log.error( "ONOS service failed to start" )
Devin Lim44075962017-08-11 10:56:37 -07001130
1131 main.cleanAndExit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001132 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001133 main.log.error( self.name + ": EOF exception found" )
1134 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001135 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001136 except Exception:
1137 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001138 main.cleanAndExit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001139
kelvin-onlabd3b64892015-01-20 13:26:24 -08001140 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001141 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001142 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001143 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001144 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001145 try:
kelvin8ec71442015-01-15 16:57:00 -08001146 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001147 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001148 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001149 " stop" )
1150 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -04001151 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -07001152 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -04001153 "Unknown\sinstance",
Devin Limc20e79a2017-06-07 10:29:57 -07001154 self.prompt,
Jeremy Songster14c13572016-04-21 17:34:03 -07001155 pexpect.TIMEOUT ], timeout=180 )
andrewonlab2b30bd32014-10-09 16:48:55 -04001156 if i == 0:
Devin Limc20e79a2017-06-07 10:29:57 -07001157 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001158 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001159 return main.TRUE
1160 elif i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001161 self.handle.expect( self.prompt )
Jon Hall65844a32015-03-09 19:09:37 -07001162 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001163 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -04001164 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -07001165 elif i == 2:
Devin Limc20e79a2017-06-07 10:29:57 -07001166 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -07001167 main.log.warn( "ONOS wasn't running" )
1168 return main.TRUE
YPZhang77badfc2016-03-09 10:28:59 -08001169 elif i == 3:
1170 main.log.info( "ONOS service stopped" )
1171 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -04001172 else:
kelvin8ec71442015-01-15 16:57:00 -08001173 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001174 return main.FALSE
andrewonlab2b30bd32014-10-09 16:48:55 -04001175 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001176 main.log.error( self.name + ": EOF exception found" )
1177 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001178 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001179 except Exception:
1180 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001181 main.cleanAndExit()
kelvin8ec71442015-01-15 16:57:00 -08001182
kelvin-onlabd3b64892015-01-20 13:26:24 -08001183 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -08001184 """
andrewonlabc8d47972014-10-09 16:52:36 -04001185 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -08001186 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -04001187 if needed
kelvin8ec71442015-01-15 16:57:00 -08001188 """
andrewonlabc8d47972014-10-09 16:52:36 -04001189 try:
kelvin8ec71442015-01-15 16:57:00 -08001190 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001191 self.handle.expect( self.prompt, timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001192 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
Devin Limc20e79a2017-06-07 10:29:57 -07001193 self.handle.expect( self.prompt, timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001194 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
kelvin8ec71442015-01-15 16:57:00 -08001195 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -04001196 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -07001197 except pexpect.TIMEOUT:
1198 main.log.exception( self.name + ": Timeout in onosUninstall" )
1199 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -04001200 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001201 main.log.error( self.name + ": EOF exception found" )
1202 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001203 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001204 except Exception:
1205 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001206 main.cleanAndExit()
andrewonlab2b30bd32014-10-09 16:48:55 -04001207
kelvin-onlabd3b64892015-01-20 13:26:24 -08001208 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001209 """
andrewonlabaedc8332014-12-04 12:43:03 -05001210 Issues the command 'onos-die <node-ip>'
1211 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -08001212 """
andrewonlabaedc8332014-12-04 12:43:03 -05001213 try:
kelvin8ec71442015-01-15 16:57:00 -08001214 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001215 self.handle.expect( self.prompt )
Jeremyf0aecdb2016-03-30 13:19:57 -07001216 cmdStr = "onos-die " + str( nodeIp )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001217 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001218 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001219 "Killing\sONOS",
1220 "ONOS\sprocess\sis\snot\srunning",
Jeremy Songster14c13572016-04-21 17:34:03 -07001221 pexpect.TIMEOUT ], timeout=60 )
andrewonlabaedc8332014-12-04 12:43:03 -05001222 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001223 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001224 " was killed and stopped" )
Jon Hall53c5e662016-04-13 16:06:56 -07001225 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001226 self.handle.expect( self.prompt )
andrewonlabaedc8332014-12-04 12:43:03 -05001227 return main.TRUE
1228 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001229 main.log.info( "ONOS process was not running" )
Jon Hall53c5e662016-04-13 16:06:56 -07001230 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001231 self.handle.expect( self.prompt )
andrewonlabaedc8332014-12-04 12:43:03 -05001232 return main.FALSE
1233 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001234 main.log.error( self.name + ": EOF exception found" )
1235 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001236 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001237 except Exception:
1238 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001239 main.cleanAndExit()
andrewonlabaedc8332014-12-04 12:43:03 -05001240
kelvin-onlabd3b64892015-01-20 13:26:24 -08001241 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001242 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001243 Calls the command: 'onos-kill [<node-ip>]'
1244 "Remotely, and unceremoniously kills the ONOS instance running on
1245 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001246 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001247 try:
kelvin8ec71442015-01-15 16:57:00 -08001248 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001249 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001250 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001251 i = self.handle.expect( [
Devin Limc20e79a2017-06-07 10:29:57 -07001252 self.prompt,
andrewonlabe8e56fd2014-10-09 17:12:44 -04001253 "No\sroute\sto\shost",
1254 "password:",
Jeremy Songster14c13572016-04-21 17:34:03 -07001255 pexpect.TIMEOUT ], timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -08001256
andrewonlabe8e56fd2014-10-09 17:12:44 -04001257 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001258 main.log.info(
1259 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001260 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001261 return main.TRUE
1262 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001263 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001264 return main.FALSE
1265 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001266 main.log.info(
1267 "Passwordless login for host: " +
1268 str( nodeIp ) +
1269 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001270 return main.FALSE
1271 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001272 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001273 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001274
andrewonlabe8e56fd2014-10-09 17:12:44 -04001275 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001276 main.log.error( self.name + ": EOF exception found" )
1277 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001278 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001279 except Exception:
1280 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001281 main.cleanAndExit()
andrewonlabe8e56fd2014-10-09 17:12:44 -04001282
kelvin-onlabd3b64892015-01-20 13:26:24 -08001283 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001284 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001285 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001286 a cleaner environment.
1287
andrewonlab19fbdca2014-11-14 12:55:59 -05001288 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001289 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001290 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001291 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001292 try:
kelvin8ec71442015-01-15 16:57:00 -08001293 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001294 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001295 self.handle.sendline( "onos-remove-raft-logs" )
1296 # Sometimes this command hangs
Devin Limc20e79a2017-06-07 10:29:57 -07001297 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
kelvin8ec71442015-01-15 16:57:00 -08001298 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001299 if i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001300 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
kelvin8ec71442015-01-15 16:57:00 -08001301 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001302 if i == 1:
1303 return main.FALSE
andrewonlab19fbdca2014-11-14 12:55:59 -05001304 return main.TRUE
andrewonlab19fbdca2014-11-14 12:55:59 -05001305 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001306 main.log.error( self.name + ": EOF exception found" )
1307 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001308 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001309 except Exception:
1310 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001311 main.cleanAndExit()
Jon Hallfcc88622014-11-25 13:09:54 -05001312
kelvin-onlabd3b64892015-01-20 13:26:24 -08001313 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001314 """
1315 Calls the command 'onos-start-network [ <mininet-topo> ]
1316 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001317 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001318 cell."
andrewonlab94282092014-10-10 13:00:11 -04001319 * Specify mininet topology file name for mntopo
1320 * Topo files should be placed at:
1321 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001322
andrewonlab94282092014-10-10 13:00:11 -04001323 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001324 """
andrewonlab94282092014-10-10 13:00:11 -04001325 try:
1326 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001327 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001328 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001329
kelvin8ec71442015-01-15 16:57:00 -08001330 mntopo = str( mntopo )
1331 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001332 self.handle.expect( self.prompt )
andrewonlab94282092014-10-10 13:00:11 -04001333
kelvin8ec71442015-01-15 16:57:00 -08001334 self.handle.sendline( "onos-start-network " + mntopo )
1335 self.handle.expect( "mininet>" )
1336 main.log.info( "Network started, entered mininet prompt" )
1337
1338 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001339
1340 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001341 main.log.error( self.name + ": EOF exception found" )
1342 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001343 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001344 except Exception:
1345 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001346 main.cleanAndExit()
andrewonlab94282092014-10-10 13:00:11 -04001347
Jeremy Songster14c13572016-04-21 17:34:03 -07001348 def isup( self, node="", timeout=240 ):
kelvin8ec71442015-01-15 16:57:00 -08001349 """
1350 Run's onos-wait-for-start which only returns once ONOS is at run
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001351 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001352
Jon Hall7993bfc2014-10-09 16:30:14 -04001353 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001354 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001355 try:
Jon Hall3b489db2015-10-05 14:38:37 -07001356 self.handle.sendline( "onos-wait-for-start " + node )
1357 self.handle.expect( "onos-wait-for-start" )
kelvin8ec71442015-01-15 16:57:00 -08001358 # NOTE: this timeout is arbitrary"
Jon Hallcababf72018-02-05 12:05:19 -08001359 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT, "Password:" ], timeout )
Jon Hall7993bfc2014-10-09 16:30:14 -04001360 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001361 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001362 return main.TRUE
Jon Hallcababf72018-02-05 12:05:19 -08001363 elif i == 1 or i == 2:
kelvin8ec71442015-01-15 16:57:00 -08001364 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001365 # we will kill it on timeout
Jon Hallcababf72018-02-05 12:05:19 -08001366 if i == 1:
1367 main.log.error( "ONOS has not started yet" )
1368 elif i == 2:
1369 main.log.error( "Cannot login to ONOS CLI, try using onos-secure-ssh" )
kelvin8ec71442015-01-15 16:57:00 -08001370 self.handle.send( "\x03" ) # Control-C
Devin Limc20e79a2017-06-07 10:29:57 -07001371 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001372 return main.FALSE
1373 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001374 main.log.error( self.name + ": EOF exception found" )
1375 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001376 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001377 except Exception:
1378 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001379 main.cleanAndExit()
andrewonlab05e362f2014-10-10 00:40:57 -04001380
Devin Lim142b5342017-07-20 15:22:39 -07001381 def preventAutoRespawn( self ):
1382 """
1383 Description:
1384 This will prevent ONOSservice to automatically
1385 respawn.
1386 """
1387 try:
1388 self.handle.sendline( "sed -i -e 's/^respawn$/#respawn/g' tools/package/init/onos.conf" )
1389 self.handle.expect( "\$" ) # $ from the command
1390 self.handle.sendline( "sed -i -e 's/^Restart=always/Restart=no/g' tools/package/init/onos.service" )
1391 self.handle.expect( "\$" ) # $ from the command
1392 self.handle.expect( "\$" ) # $ from the prompt
1393 except pexpect.EOF:
1394 main.log.error( self.name + ": EOF exception found" )
1395 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001396 main.cleanAndExit()
Devin Lim142b5342017-07-20 15:22:39 -07001397 except Exception:
1398 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001399 main.cleanAndExit()
Devin Lim142b5342017-07-20 15:22:39 -07001400
kelvin-onlabd3b64892015-01-20 13:26:24 -08001401 def pushTestIntentsShell(
1402 self,
1403 dpidSrc,
1404 dpidDst,
1405 numIntents,
1406 dirFile,
1407 onosIp,
1408 numMult="",
1409 appId="",
1410 report=True,
1411 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001412 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001413 Description:
kelvin8ec71442015-01-15 16:57:00 -08001414 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001415 better parallelize the results than the CLI
1416 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001417 * dpidSrc: specify source dpid
1418 * dpidDst: specify destination dpid
1419 * numIntents: specify number of intents to push
1420 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001421 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001422 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001423 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001424 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001425 """
1426 try:
1427 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001428 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001429 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001430 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001431 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001432 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001433
kelvin-onlabd3b64892015-01-20 13:26:24 -08001434 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1435 if not numMult:
1436 addIntents = addDpid + " " + str( numIntents )
1437 elif numMult:
1438 addIntents = addDpid + " " + str( numIntents ) + " " +\
1439 str( numMult )
1440 if appId:
1441 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001442 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001443 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001444
andrewonlabaedc8332014-12-04 12:43:03 -05001445 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001446 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001447 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001448 sendCmd = addApp + " &"
1449 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001450
kelvin-onlabd3b64892015-01-20 13:26:24 -08001451 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001452
1453 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001454 main.log.error( self.name + ": EOF exception found" )
1455 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001456 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001457 except Exception:
1458 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001459 main.cleanAndExit()
andrewonlab05e362f2014-10-10 00:40:57 -04001460
kelvin-onlabd3b64892015-01-20 13:26:24 -08001461 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001462 """
andrewonlab970399c2014-11-07 13:09:32 -05001463 Capture all packet activity and store in specified
1464 directory/file
1465
1466 Required:
1467 * interface: interface to capture
1468 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001469 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001470 try:
1471 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001472 self.handle.expect( self.prompt )
andrewonlab970399c2014-11-07 13:09:32 -05001473
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001474 self.handle.sendline( "tshark -i " + str( interface ) + " -t e -w " + str( dirFile ) + " &" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001475 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001476 self.handle.expect( "Capturing on" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001477 self.handle.sendline( "\n" )
Devin Limc20e79a2017-06-07 10:29:57 -07001478 self.handle.expect( self.prompt )
andrewonlab970399c2014-11-07 13:09:32 -05001479
Jon Hallfebb1c72015-03-05 13:30:09 -08001480 main.log.info( "Tshark started capturing files on " +
1481 str( interface ) + " and saving to directory: " +
1482 str( dirFile ) )
1483 except pexpect.EOF:
1484 main.log.error( self.name + ": EOF exception found" )
1485 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001486 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001487 except Exception:
1488 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001489 main.cleanAndExit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001490
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001491 def onosTopoCfg( self, onosIp, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001492 """
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001493 Description:
1494 Execute onos-topo-cfg command
1495 Required:
1496 onosIp - IP of the onos node you want to send the json to
1497 jsonFile - File path of the json file
1498 Return:
1499 Returns main.TRUE if the command is successfull; Returns
1500 main.FALSE if there was an error
kelvin8ec71442015-01-15 16:57:00 -08001501 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001502 try:
kelvin8ec71442015-01-15 16:57:00 -08001503 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001504 self.handle.expect( self.prompt )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001505 cmd = "onos-topo-cfg "
1506 self.handle.sendline( cmd + str( onosIp ) + " " + jsonFile )
1507 handle = self.handle.before
1508 print handle
1509 if "Error" in handle:
1510 main.log.error( self.name + ": " + self.handle.before )
1511 return main.FALSE
1512 else:
Devin Limc20e79a2017-06-07 10:29:57 -07001513 self.handle.expect( self.prompt )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001514 return main.TRUE
1515
Jon Hallfebb1c72015-03-05 13:30:09 -08001516 except pexpect.EOF:
1517 main.log.error( self.name + ": EOF exception found" )
1518 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001519 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001520 except Exception:
1521 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001522 main.cleanAndExit()
kelvin8ec71442015-01-15 16:57:00 -08001523
jenkins1e99e7b2015-04-02 18:15:39 -07001524 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001525 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001526 Required:
kelvin8ec71442015-01-15 16:57:00 -08001527 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001528 * directory to store results
1529 Optional:
1530 * interface - default: eth0
Jon Hall4ba53f02015-07-29 13:07:41 -07001531 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001532 Description:
1533 Uses tshark command to grep specific group of packets
1534 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001535 The timestamp is hardcoded to be in epoch
1536 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001537 try:
1538 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001539 self.handle.expect( self.prompt )
Jon Hallfebb1c72015-03-05 13:30:09 -08001540 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001541 if grepOptions:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001542 grepStr = "grep " + str( grepOptions )
jenkins1e99e7b2015-04-02 18:15:39 -07001543 else:
1544 grepStr = "grep"
Jon Hall4ba53f02015-07-29 13:07:41 -07001545
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001546 cmd = (
1547 "sudo tshark -i " +
Jon Hallfebb1c72015-03-05 13:30:09 -08001548 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001549 " -t e | " +
1550 grepStr + " --line-buffered \"" +
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001551 str( grep ) +
Jon Hallfebb1c72015-03-05 13:30:09 -08001552 "\" >" +
1553 directory +
1554 " &" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001555 self.handle.sendline( cmd )
1556 main.log.info( cmd )
Jon Hallfebb1c72015-03-05 13:30:09 -08001557 self.handle.expect( "Capturing on" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001558 self.handle.sendline( "\n" )
Devin Limc20e79a2017-06-07 10:29:57 -07001559 self.handle.expect( self.prompt )
Jon Hallfebb1c72015-03-05 13:30:09 -08001560 except pexpect.EOF:
1561 main.log.error( self.name + ": EOF exception found" )
1562 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001563 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001564 except Exception:
1565 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001566 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001567
kelvin-onlabd3b64892015-01-20 13:26:24 -08001568 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001569 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001570 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001571 """
1572 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001573 try:
1574 self.execute( cmd="sudo rm /tmp/wireshark*" )
1575 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001576 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1577 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001578 self.handle.sendline( "" )
1579 main.log.info( "Tshark stopped" )
1580 except pexpect.EOF:
1581 main.log.error( self.name + ": EOF exception found" )
1582 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001583 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001584 except Exception:
1585 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001586 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001587
kelvin8ec71442015-01-15 16:57:00 -08001588 def ptpd( self, args ):
1589 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001590 Initiate ptp with user-specified args.
1591 Required:
1592 * args: specify string of args after command
1593 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001594 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001595 try:
kelvin8ec71442015-01-15 16:57:00 -08001596 self.handle.sendline( "sudo ptpd " + str( args ) )
1597 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001598 "Multiple",
1599 "Error",
Devin Limc20e79a2017-06-07 10:29:57 -07001600 self.prompt ] )
1601 self.handle.expect( self.prompt )
andrewonlabba44bcf2014-10-16 16:54:41 -04001602
andrewonlab0c38a4a2014-10-28 18:35:35 -04001603 if i == 0:
1604 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001605 main.log.info( "ptpd returned an error: " +
1606 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001607 return handle
1608 elif i == 1:
1609 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001610 main.log.error( "ptpd returned an error: " +
1611 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001612 return handle
1613 else:
1614 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001615
andrewonlab0c38a4a2014-10-28 18:35:35 -04001616 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001617 main.log.error( self.name + ": EOF exception found" )
1618 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001619 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001620 except Exception:
1621 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001622 main.cleanAndExit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001623
You Wang54b1d672018-06-11 16:44:13 -07001624 def dumpONOSCmd( self, ONOSIp, CMD, destDir, filename, options="", timeout=60 ):
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001625 """
Pier50f0bc62016-09-07 17:53:40 -07001626 Dump Cmd to a desired directory.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001627 For debugging purposes, you may want to use
Pier50f0bc62016-09-07 17:53:40 -07001628 this function to capture Cmd at a given point in time.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001629 Localtime will be attached to the filename
1630
1631 Required:
1632 * ONOSIp: the IP of the target ONOS instance
Pier50f0bc62016-09-07 17:53:40 -07001633 * CMD: the command to dump;
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001634 * destDir: specify directory to copy to.
1635 ex ) /tmp/
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001636 * fileName: Name of the file
You Wang54b1d672018-06-11 16:44:13 -07001637 Optional:
Pier50f0bc62016-09-07 17:53:40 -07001638 * options: Options for ONOS command
You Wang54b1d672018-06-11 16:44:13 -07001639 * timeout: pexpect timeout for running the ONOS command
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001640 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001641
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001642 localtime = time.strftime( '%x %X' )
1643 localtime = localtime.replace( "/", "" )
1644 localtime = localtime.replace( " ", "_" )
1645 localtime = localtime.replace( ":", "" )
1646 if destDir[ -1: ] != "/":
1647 destDir += "/"
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001648 cmd = CMD + " " + options + " > " + str( destDir ) + str( filename ) + localtime
You Wang54b1d672018-06-11 16:44:13 -07001649 return self.onosCli( ONOSIp, cmd, timeout=timeout )
Flavio Castrob7718952016-05-18 08:53:41 -07001650
kelvin-onlabd3b64892015-01-20 13:26:24 -08001651 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001652 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001653 """
1654 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001655 Current implementation of ONOS deletes its karaf
1656 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001657 you may want to use this function to capture
1658 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001659 Localtime will be attached to the filename
1660
1661 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001662 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001663 copy.
kelvin8ec71442015-01-15 16:57:00 -08001664 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001665 For copying multiple files, leave copyFileName
1666 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001667 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001668 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001669 ex ) /tmp/
1670 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001671 * copyFileName: If you want to rename the log
1672 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001673 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001674 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001675 try:
Flavio Castro09ab59d2016-05-25 17:01:35 -07001676 localtime = time.strftime( '%H %M' )
kelvin8ec71442015-01-15 16:57:00 -08001677 localtime = localtime.replace( "/", "" )
1678 localtime = localtime.replace( " ", "_" )
1679 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001680 if destDir[ -1: ] != "/":
1681 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001682
kelvin-onlabd3b64892015-01-20 13:26:24 -08001683 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001684 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1685 str( destDir ) + str( copyFileName ) +
1686 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001687 self.handle.expect( "cp" )
Devin Limc20e79a2017-06-07 10:29:57 -07001688 self.handle.expect( self.prompt )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001689 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001690 self.handle.sendline( "cp " + str( logToCopy ) +
1691 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001692 self.handle.expect( "cp" )
Devin Limc20e79a2017-06-07 10:29:57 -07001693 self.handle.expect( self.prompt )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001694
kelvin8ec71442015-01-15 16:57:00 -08001695 return self.handle.before
1696
1697 except pexpect.EOF:
1698 main.log.error( "Copying files failed" )
1699 main.log.error( self.name + ": EOF exception found" )
1700 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001701 except Exception:
1702 main.log.exception( "Copying files failed" )
1703
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001704 def checkLogs( self, onosIp, restart=False ):
kelvin8ec71442015-01-15 16:57:00 -08001705 """
Jon Hall94fd0472014-12-08 11:52:42 -08001706 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001707 If restart is True, use the old version of onos-check-logs which
1708 does not print the full stacktrace, but shows the entire log file,
1709 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001710 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001711 """
Jon Hall94fd0472014-12-08 11:52:42 -08001712 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001713 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001714 if restart:
1715 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001716 self.handle.sendline( cmd )
1717 self.handle.expect( cmd )
Devin Limdc78e202017-06-09 18:30:07 -07001718 self.handle.expect( self.prompt + " " )
Jon Hall94fd0472014-12-08 11:52:42 -08001719 response = self.handle.before
1720 return response
1721 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001722 main.log.error( "Lost ssh connection" )
1723 main.log.error( self.name + ": EOF exception found" )
1724 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001725 except Exception:
1726 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001727 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08001728
kelvin-onlabd3b64892015-01-20 13:26:24 -08001729 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001730 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001731 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001732 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001733 try:
kelvin8ec71442015-01-15 16:57:00 -08001734 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001735 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001736 self.handle.sendline( "onos-service " + str( node ) +
1737 " status" )
1738 i = self.handle.expect( [
You Wangef1e6572016-03-08 12:53:18 -08001739 "start/running",
You Wang7bd83062016-03-01 11:50:00 -08001740 "Running ...",
You Wangef1e6572016-03-08 12:53:18 -08001741 "stop/waiting",
You Wang7bd83062016-03-01 11:50:00 -08001742 "Not Running ...",
kelvin8ec71442015-01-15 16:57:00 -08001743 pexpect.TIMEOUT ], timeout=120 )
YPZhangfebf7302016-05-24 16:45:56 -07001744 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001745 self.handle.expect( self.prompt )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001746
You Wangef1e6572016-03-08 12:53:18 -08001747 if i == 0 or i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001748 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001749 return main.TRUE
You Wangef1e6572016-03-08 12:53:18 -08001750 elif i == 2 or i == 3:
kelvin8ec71442015-01-15 16:57:00 -08001751 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001752 main.log.error( "ONOS service failed to check the status" )
Devin Lim44075962017-08-11 10:56:37 -07001753
1754 main.cleanAndExit()
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001755 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001756 main.log.error( self.name + ": EOF exception found" )
1757 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001758 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001759 except Exception:
1760 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001761 main.cleanAndExit()
Jon Hall21270ac2015-02-16 17:59:55 -08001762
Jon Hall63604932015-02-26 17:09:50 -08001763 def setIpTables( self, ip, port='', action='add', packet_type='',
1764 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001765 """
Jon Hall21270ac2015-02-16 17:59:55 -08001766 Description:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001767 add or remove iptables rule to DROP (default) packets from
Jon Hall21270ac2015-02-16 17:59:55 -08001768 specific IP and PORT
1769 Usage:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001770 * specify action ('add' or 'remove')
Jon Hall21270ac2015-02-16 17:59:55 -08001771 when removing, pass in the same argument as you would add. It will
1772 delete that specific rule.
1773 * specify the ip to block
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001774 * specify the destination port to block (defaults to all ports)
1775 * optional packet type to block (default tcp)
1776 * optional iptables rule (default DROP)
1777 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001778 * States boolean toggles adding all supported tcp states to the
1779 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001780 Returns:
1781 main.TRUE on success or
1782 main.FALSE if given invalid input or
1783 main.ERROR if there is an error in response from iptables
1784 WARNING:
1785 * This function uses root privilege iptables command which may result
1786 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001787 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001788
Jon Hall21270ac2015-02-16 17:59:55 -08001789 # NOTE*********
1790 # The strict checking methods of this driver function is intentional
1791 # to discourage any misuse or error of iptables, which can cause
1792 # severe network errors
1793 # *************
1794
1795 # NOTE: Sleep needed to give some time for rule to be added and
1796 # registered to the instance. If you are calling this function
1797 # multiple times this sleep will prevent any errors.
1798 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001799 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001800 try:
1801 # input validation
1802 action_type = action.lower()
1803 rule = rule.upper()
1804 direction = direction.upper()
1805 if action_type != 'add' and action_type != 'remove':
1806 main.log.error( "Invalid action type. Use 'add' or "
1807 "'remove' table rule" )
1808 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1809 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1810 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1811 "'ACCEPT' or 'LOG' only." )
1812 if direction != 'INPUT' and direction != 'OUTPUT':
1813 # NOTE currently only supports rules INPUT and OUPTUT
1814 main.log.error( "Invalid rule. Valid directions are"
1815 " 'OUTPUT' or 'INPUT'" )
1816 return main.FALSE
1817 return main.FALSE
1818 return main.FALSE
1819 if action_type == 'add':
1820 # -A is the 'append' action of iptables
1821 actionFlag = '-A'
1822 elif action_type == 'remove':
1823 # -D is the 'delete' rule of iptables
1824 actionFlag = '-D'
1825 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001826 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001827 cmd = "sudo iptables " + actionFlag + " " +\
1828 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001829 " -s " + str( ip )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001830 # " -p " + str( packet_type ) +\
Jon Hall63604932015-02-26 17:09:50 -08001831 if packet_type:
1832 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001833 if port:
1834 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001835 if states:
1836 cmd += " -m state --state="
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001837 # FIXME- Allow user to configure which states to block
Jon Hall63604932015-02-26 17:09:50 -08001838 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001839 cmd += " -j " + str( rule )
1840
1841 self.handle.sendline( cmd )
Devin Limc20e79a2017-06-07 10:29:57 -07001842 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001843 main.log.warn( self.handle.before )
1844
1845 info_string = "On " + str( self.name )
1846 info_string += " " + str( action_type )
1847 info_string += " iptable rule [ "
1848 info_string += " IP: " + str( ip )
1849 info_string += " Port: " + str( port )
1850 info_string += " Rule: " + str( rule )
1851 info_string += " Direction: " + str( direction ) + " ]"
1852 main.log.info( info_string )
1853 return main.TRUE
1854 except pexpect.TIMEOUT:
1855 main.log.exception( self.name + ": Timeout exception in "
1856 "setIpTables function" )
1857 return main.ERROR
1858 except pexpect.EOF:
1859 main.log.error( self.name + ": EOF exception found" )
1860 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001861 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001862 except Exception:
1863 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001864 main.cleanAndExit()
Jon Hall21270ac2015-02-16 17:59:55 -08001865
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001866 def detailed_status( self, log_filename ):
Jon Hallefbd9792015-03-05 16:11:36 -08001867 """
Jon Hall0468b042015-02-19 19:08:21 -08001868 This method is used by STS to check the status of the controller
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001869 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001870 """
Jon Hall0468b042015-02-19 19:08:21 -08001871 import re
1872 try:
1873 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001874 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001875 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -07001876 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001877 self.handle.sendline( "service onos status" )
Devin Limc20e79a2017-06-07 10:29:57 -07001878 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001879 response = self.handle.before
1880 if re.search( "onos start/running", response ):
1881 # onos start/running, process 10457
1882 return 'RUNNING'
1883 # FIXME: Implement this case
1884 # elif re.search( pattern, response ):
1885 # return 'STARTING'
1886 elif re.search( "onos stop/", response ):
1887 # onos stop/waiting
1888 # FIXME handle this differently?: onos stop/pre-stop
1889 return 'STOPPED'
1890 # FIXME: Implement this case
1891 # elif re.search( pattern, response ):
1892 # return 'FROZEN'
1893 else:
1894 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001895 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001896 main.log.warn( response )
1897 return 'ERROR', "Unknown response: %s" % response
1898 except pexpect.TIMEOUT:
1899 main.log.exception( self.name + ": Timeout exception in "
1900 "setIpTables function" )
1901 return 'ERROR', "Pexpect Timeout"
1902 except pexpect.EOF:
1903 main.log.error( self.name + ": EOF exception found" )
1904 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001905 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001906 except Exception:
1907 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001908 main.cleanAndExit()
Jon Hall0468b042015-02-19 19:08:21 -08001909
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001910 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001911 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001912 Create/formats the LinkGraph.cfg file based on arguments
1913 -only creates a linear topology and connects islands
1914 -evenly distributes devices
andrew@onlab.us3b087132015-03-11 15:00:08 -07001915 -must be called by ONOSbench
1916
Jon Hall4ba53f02015-07-29 13:07:41 -07001917 ONOSIpList - list of all of the node IPs to be used
1918
1919 deviceCount - number of switches to be assigned
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001920 '''
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001921 main.log.info( "Creating link graph configuration file." )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001922 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
Jon Hall4ba53f02015-07-29 13:07:41 -07001923 tempFile = "/tmp/linkGraph.cfg"
andrew@onlab.us3b087132015-03-11 15:00:08 -07001924
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001925 linkGraph = open( tempFile, 'w+' )
1926 linkGraph.write( "# NullLinkProvider topology description (config file).\n" )
1927 linkGraph.write( "# The NodeId is only added if the destination is another node's device.\n" )
1928 linkGraph.write( "# Bugs: Comments cannot be appended to a line to be read.\n" )
Jon Hall4ba53f02015-07-29 13:07:41 -07001929
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001930 clusterCount = len( ONOSIpList )
Jon Hall4ba53f02015-07-29 13:07:41 -07001931
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001932 if isinstance( deviceCount, int ) or isinstance( deviceCount, str ):
1933 deviceCount = int( deviceCount )
1934 switchList = [ 0 ]*( clusterCount+1 )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001935 baselineSwitchCount = deviceCount/clusterCount
Jon Hall4ba53f02015-07-29 13:07:41 -07001936
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001937 for node in range( 1, clusterCount + 1 ):
1938 switchList[ node ] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07001939
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001940 for node in range( 1, ( deviceCount % clusterCount )+1 ):
1941 switchList[ node ] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07001942
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001943 if isinstance( deviceCount, list ):
1944 main.log.info( "Using provided device distribution" )
1945 switchList = [ 0 ]
andrew@onlab.us3b087132015-03-11 15:00:08 -07001946 for i in deviceCount:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001947 switchList.append( int( i ) )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001948
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001949 tempList = [ '0' ]
1950 tempList.extend( ONOSIpList )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001951 ONOSIpList = tempList
1952
1953 myPort = 6
1954 lastSwitch = 0
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001955 for node in range( 1, clusterCount+1 ):
1956 if switchList[ node ] == 0:
andrew@onlab.us3b087132015-03-11 15:00:08 -07001957 continue
1958
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001959 linkGraph.write( "graph " + ONOSIpList[ node ] + " {\n" )
Jon Hall4ba53f02015-07-29 13:07:41 -07001960
andrew@onlab.us3b087132015-03-11 15:00:08 -07001961 if node > 1:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001962 # connect to last device on previous node
1963 line = ( "\t0:5 -> " + str( lastSwitch ) + ":6:" + lastIp + "\n" ) # ONOSIpList[node-1]
1964 linkGraph.write( line )
Jon Hall4ba53f02015-07-29 13:07:41 -07001965
1966 lastSwitch = 0
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001967 for switch in range( 0, switchList[ node ]-1 ):
andrew@onlab.us3b087132015-03-11 15:00:08 -07001968 line = ""
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001969 line = ( "\t" + str( switch ) + ":" + str( myPort ) )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001970 line += " -- "
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001971 line += ( str( switch+1 ) + ":" + str( myPort-1 ) + "\n" )
1972 linkGraph.write( line )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001973 lastSwitch = switch+1
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001974 lastIp = ONOSIpList[ node ]
Jon Hall4ba53f02015-07-29 13:07:41 -07001975
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001976 # lastSwitch += 1
1977 if node < ( clusterCount ):
1978 # connect to first device on the next node
1979 line = ( "\t" + str( lastSwitch ) + ":6 -> 0:5:" + ONOSIpList[ node+1 ] + "\n" )
1980 linkGraph.write( line )
Jon Hall4ba53f02015-07-29 13:07:41 -07001981
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001982 linkGraph.write( "}\n" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001983 linkGraph.close()
1984
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001985 # SCP
1986 os.system( "scp " + tempFile + " " + self.user_name + "@" + benchIp + ":" + linkGraphPath )
1987 main.log.info( "linkGraph.cfg creation complete" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001988
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001989 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10 ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001990 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001991 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
1992 deviceCount = number of switches to distribute, or list of values to use as custom distribution
cameron@onlab.us75900962015-03-30 13:22:49 -07001993 numPorts = number of ports per device. Defaults to 10 both in this function and in ONOS. Optional arg
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001994 '''
1995
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001996 main.log.info( "Configuring Null Device Provider" )
1997 clusterCount = len( ONOSIpList )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001998
Jon Hall4ba53f02015-07-29 13:07:41 -07001999 try:
2000
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002001 if isinstance( deviceCount, int ) or isinstance( deviceCount, str ):
2002 main.log.info( "Creating device distribution" )
2003 deviceCount = int( deviceCount )
2004 switchList = [ 0 ]*( clusterCount+1 )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002005 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002006
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002007 for node in range( 1, clusterCount + 1 ):
2008 switchList[ node ] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002009
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002010 for node in range( 1, ( deviceCount % clusterCount )+1 ):
2011 switchList[ node ] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07002012
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002013 if isinstance( deviceCount, list ):
2014 main.log.info( "Using provided device distribution" )
Jon Hall4ba53f02015-07-29 13:07:41 -07002015
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002016 if len( deviceCount ) == clusterCount:
2017 switchList = [ '0' ]
2018 switchList.extend( deviceCount )
Jon Hall4ba53f02015-07-29 13:07:41 -07002019
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002020 if len( deviceCount ) == ( clusterCount + 1 ):
2021 if deviceCount[ 0 ] == '0' or deviceCount[ 0 ] == 0:
cameron@onlab.us75900962015-03-30 13:22:49 -07002022 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002023
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002024 assert len( switchList ) == ( clusterCount + 1 )
Jon Hall4ba53f02015-07-29 13:07:41 -07002025
cameron@onlab.us75900962015-03-30 13:22:49 -07002026 except AssertionError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002027 main.log.error( "Bad device/Ip list match" )
cameron@onlab.us75900962015-03-30 13:22:49 -07002028 except TypeError:
2029 main.log.exception( self.name + ": Object not as expected" )
2030 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07002031 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002032 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002033 main.cleanAndExit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002034
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002035 ONOSIp = [ 0 ]
2036 ONOSIp.extend( ONOSIpList )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002037
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002038 devicesString = "devConfigs = "
2039 for node in range( 1, len( ONOSIp ) ):
2040 devicesString += ( ONOSIp[ node ] + ":" + str( switchList[ node ] ) )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002041 if node < clusterCount:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002042 devicesString += ( "," )
Jon Hall4ba53f02015-07-29 13:07:41 -07002043
2044 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002045 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
2046 self.handle.expect( ":~" )
2047 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str( numPorts ) )
2048 self.handle.expect( ":~" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002049
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002050 for i in range( 10 ):
2051 self.handle.sendline( "onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider" )
2052 self.handle.expect( ":~" )
cameron@onlab.us75900962015-03-30 13:22:49 -07002053 verification = self.handle.before
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002054 if ( " value=" + str( numPorts ) ) in verification and ( " value=" + devicesString ) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002055 break
2056 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002057 time.sleep( 1 )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002058
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002059 assert ( "value=" + str( numPorts ) ) in verification and ( " value=" + devicesString ) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002060
cameron@onlab.us75900962015-03-30 13:22:49 -07002061 except AssertionError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002062 main.log.error( "Incorrect Config settings: " + verification )
Jon Hall77ba41c2015-04-06 10:25:40 -07002063 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002064 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002065 main.cleanAndExit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002066
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002067 def configNullLink( self, fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0 ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002068 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002069 fileName default is currently the same as the default on ONOS, specify alternate file if
cameron@onlab.us75900962015-03-30 13:22:49 -07002070 you want to use a different topology file than linkGraph.cfg
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002071 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002072
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002073 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002074 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str( eventRate ) )
2075 self.handle.expect( ":~" )
2076 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2077 self.handle.expect( ":~" )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002078
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002079 for i in range( 10 ):
2080 self.handle.sendline( "onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider" )
2081 self.handle.expect( ":~" )
cameron@onlab.us75900962015-03-30 13:22:49 -07002082 verification = self.handle.before
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002083 if ( " value=" + str( eventRate ) ) in verification and ( " value=" + fileName ) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002084 break
Jon Hall4ba53f02015-07-29 13:07:41 -07002085 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002086 time.sleep( 1 )
Jon Hall4ba53f02015-07-29 13:07:41 -07002087
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002088 assert ( "value=" + str( eventRate ) ) in verification and ( " value=" + fileName ) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002089
cameron@onlab.us75900962015-03-30 13:22:49 -07002090 except pexpect.EOF:
2091 main.log.error( self.name + ": EOF exception found" )
2092 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002093 main.cleanAndExit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002094 except AssertionError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002095 main.log.info( "Settings did not post to ONOS" )
2096 main.log.error( varification )
Jon Hall77ba41c2015-04-06 10:25:40 -07002097 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002098 main.log.exception( self.name + ": Uncaught exception!" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002099 main.log.error( varification )
Devin Lim44075962017-08-11 10:56:37 -07002100 main.cleanAndExit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002101
kelvin-onlaba4074292015-07-09 15:19:49 -07002102 def getOnosIps( self ):
2103 """
2104 Get all onos IPs stored in
2105 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002106
kelvin-onlaba4074292015-07-09 15:19:49 -07002107 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002108
Chiyu Chengec63bde2016-11-17 18:11:36 -08002109 def listLog( self, nodeIp ):
2110 """
2111 Get a list of all the karaf log names
2112 """
2113 try:
2114 cmd = "onos-ssh " + nodeIp + " ls -tr /opt/onos/log"
2115 self.handle.sendline( cmd )
2116 self.handle.expect( ":~" )
2117 before = self.handle.before.splitlines()
2118 logNames = []
2119 for word in before:
2120 if 'karaf.log' in word:
2121 logNames.append( word )
2122 return logNames
2123 except pexpect.EOF:
2124 main.log.error( self.name + ": EOF exception found" )
2125 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002126 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08002127 except pexpect.TIMEOUT:
2128 main.log.error( self.name + ": TIMEOUT exception found" )
2129 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002130 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08002131 except Exception:
2132 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002133 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08002134
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002135 def logReport( self, nodeIp, searchTerms, outputMode="s", startStr=None, endStr=None ):
Jon Hallb4242222016-01-25 17:07:04 -08002136 """
2137 Searches the latest ONOS log file for the given search terms and
2138 prints the total occurances of each term. Returns to combined total of
2139 all occurances.
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002140
Jon Hallb4242222016-01-25 17:07:04 -08002141 Arguments:
2142 * nodeIp - The ip of the ONOS node where the log is located
2143 * searchTerms - A string to grep for or a list of strings to grep
2144 for in the ONOS log. Will print out the number of
2145 occurances for each term.
2146 Optional Arguments:
2147 * outputMode - 's' or 'd'. If 'd' will print the last 5 lines
2148 containing each search term as well as the total
2149 number of occurances of each term. Defaults to 's',
2150 which prints the simple output of just the number
2151 of occurances for each term.
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002152 * startStr - the start string to be given to stream editor command
2153 as the start point for extraction of data
2154 * endStr - the end string to be given to stream editor command as
2155 the end point for extraction of data
Jon Hallb4242222016-01-25 17:07:04 -08002156 """
2157 try:
2158 main.log.info( " Log Report for {} ".format( nodeIp ).center( 70, '=' ) )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002159 if isinstance( searchTerms, str ):
2160 searchTerms = [ searchTerms ]
Jon Hallb4242222016-01-25 17:07:04 -08002161 numTerms = len( searchTerms )
2162 outputMode = outputMode.lower()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002163
Jon Hallb4242222016-01-25 17:07:04 -08002164 totalHits = 0
2165 logLines = []
2166 for termIndex in range( numTerms ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002167 term = searchTerms[ termIndex ]
2168 logLines.append( [ term ] )
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002169 if startStr and endStr:
2170 cmd = "onos-ssh {} \"sed -n '/{}/,/{}/p' /opt/onos/log/karaf.log | grep {}\"".format( nodeIp,
2171 startStr,
2172 endStr,
2173 term )
2174 else:
2175 cmd = "onos-ssh {} cat /opt/onos/log/karaf.log | grep {}".format( nodeIp,
2176 term )
Jon Hallb4242222016-01-25 17:07:04 -08002177 self.handle.sendline( cmd )
2178 self.handle.expect( ":~" )
2179 before = self.handle.before.splitlines()
2180 count = 0
2181 for line in before:
2182 if term in line and "grep" not in line:
2183 count += 1
2184 if before.index( line ) > ( len( before ) - 7 ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002185 logLines[ termIndex ].append( line )
Jon Hallb4242222016-01-25 17:07:04 -08002186 main.log.info( "{}: {}".format( term, count ) )
2187 totalHits += count
2188 if termIndex == numTerms - 1:
2189 print "\n"
2190 if outputMode != "s":
2191 outputString = ""
2192 for term in logLines:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002193 outputString = term[ 0 ] + ": \n"
Jon Hallb4242222016-01-25 17:07:04 -08002194 for line in range( 1, len( term ) ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002195 outputString += ( "\t" + term[ line ] + "\n" )
2196 if outputString != ( term[ 0 ] + ": \n" ):
Jon Hallb4242222016-01-25 17:07:04 -08002197 main.log.info( outputString )
2198 main.log.info( "=" * 70 )
2199 return totalHits
2200 except pexpect.EOF:
2201 main.log.error( self.name + ": EOF exception found" )
2202 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002203 main.cleanAndExit()
Jon Hallb4242222016-01-25 17:07:04 -08002204 except pexpect.TIMEOUT:
2205 main.log.error( self.name + ": TIMEOUT exception found" )
2206 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002207 main.cleanAndExit()
Jon Hallb4242222016-01-25 17:07:04 -08002208 except Exception:
2209 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002210 main.cleanAndExit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002211
2212 def copyMininetFile( self, fileName, localPath, userName, ip,
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002213 mnPath='~/mininet/custom/', timeout = 60 ):
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002214 """
2215 Description:
2216 Copy mininet topology file from dependency folder in the test folder
2217 and paste it to the mininet machine's mininet/custom folder
2218 Required:
2219 fileName - Name of the topology file to copy
2220 localPath - File path of the mininet topology file
2221 userName - User name of the mininet machine to send the file to
2222 ip - Ip address of the mininet machine
2223 Optional:
2224 mnPath - of the mininet directory to send the file to
2225 Return:
2226 Return main.TRUE if successfully copied the file otherwise
2227 return main.FALSE
2228 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002229
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002230 try:
2231 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2232 str( ip ) + ":" + mnPath + fileName
2233
2234 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07002235 self.handle.expect( self.prompt )
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002236
2237 main.log.info( self.name + ": Execute: " + cmd )
2238
2239 self.handle.sendline( cmd )
2240
2241 i = self.handle.expect( [ 'No such file',
2242 "100%",
2243 pexpect.TIMEOUT ] )
2244
2245 if i == 0:
2246 main.log.error( self.name + ": File " + fileName +
2247 " does not exist!" )
2248 return main.FALSE
2249
2250 if i == 1:
2251 main.log.info( self.name + ": File " + fileName +
2252 " has been copied!" )
2253 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07002254 self.handle.expect( self.prompt )
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002255 return main.TRUE
2256
2257 except pexpect.EOF:
2258 main.log.error( self.name + ": EOF exception found" )
2259 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002260 main.cleanAndExit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002261 except pexpect.TIMEOUT:
2262 main.log.error( self.name + ": TIMEOUT exception found" )
2263 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002264 main.cleanAndExit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002265
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002266 def jvmSet( self, memory=8 ):
Jon Hall4ba53f02015-07-29 13:07:41 -07002267
cameron@onlab.us78b89652015-07-08 15:21:03 -07002268 import os
2269
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002270 homeDir = os.path.expanduser( '~' )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002271 filename = "/onos/tools/package/bin/onos-service"
2272
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002273 serviceConfig = open( homeDir + filename, 'w+' )
2274 serviceConfig.write( "#!/bin/bash\n " )
2275 serviceConfig.write( "#------------------------------------- \n " )
2276 serviceConfig.write( "# Starts ONOS Apache Karaf container\n " )
2277 serviceConfig.write( "#------------------------------------- \n " )
2278 serviceConfig.write( "#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n " )
2279 serviceConfig.write( """export JAVA_OPTS="${JAVA_OPTS:--Xms""" + str( memory ) + "G -Xmx" + str( memory ) + """G}" \n """ )
2280 serviceConfig.write( "[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n" )
2281 serviceConfig.write( """${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """ )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002282 serviceConfig.close()
2283
Jon Hall6c44c0b2016-04-20 15:21:00 -07002284 def createDBFile( self, testData ):
Jon Hall4ba53f02015-07-29 13:07:41 -07002285
cameron@onlab.us78b89652015-07-08 15:21:03 -07002286 filename = main.TEST + "DB"
2287 DBString = ""
Jon Hall4ba53f02015-07-29 13:07:41 -07002288
cameron@onlab.us78b89652015-07-08 15:21:03 -07002289 for item in testData:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002290 if isinstance( item, string ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002291 item = "'" + item + "'"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002292 if testData.index( item ) < len( testData - 1 ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002293 item += ","
Jon Hall6c44c0b2016-04-20 15:21:00 -07002294 DBString += str( item )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002295
Jon Hall6c44c0b2016-04-20 15:21:00 -07002296 DBFile = open( filename, "a" )
2297 DBFile.write( DBString )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002298 DBFile.close()
2299
Jon Hall6c44c0b2016-04-20 15:21:00 -07002300 def verifySummary( self, ONOSIp, *deviceCount ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002301
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002302 self.handle.sendline( "onos " + ONOSIp + " summary" )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002303 self.handle.expect( ":~" )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002304
2305 summaryStr = self.handle.before
2306 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2307
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002308 # passed = "SCC(s)=1" in summaryStr
2309 # if deviceCount:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002310 # passed = "devices=" + str(deviceCount) + "," not in summaryStr
cameron@onlab.us78b89652015-07-08 15:21:03 -07002311
GlennRC772363b2015-08-25 13:05:57 -07002312 passed = False
cameron@onlab.us78b89652015-07-08 15:21:03 -07002313 if "SCC(s)=1," in summaryStr:
2314 passed = True
Jon Hall6c44c0b2016-04-20 15:21:00 -07002315 print "Summary is verifed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002316 else:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002317 print "Summary failed"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002318
2319 if deviceCount:
2320 print" ============================="
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002321 checkStr = "devices=" + str( deviceCount[ 0 ] ) + ","
cameron@onlab.us78b89652015-07-08 15:21:03 -07002322 print "Checkstr: " + checkStr
2323 if checkStr not in summaryStr:
2324 passed = False
Jon Hall6c44c0b2016-04-20 15:21:00 -07002325 print "Device count failed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002326 else:
2327 print "device count verified"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002328
2329 return passed
Jon Hall6c44c0b2016-04-20 15:21:00 -07002330
Jon Hall8f6d4622016-05-23 15:27:18 -07002331 def getIpAddr( self, iface=None ):
Jon Hall6c44c0b2016-04-20 15:21:00 -07002332 """
2333 Update self.ip_address with numerical ip address. If multiple IP's are
2334 located on the device, will attempt to use self.nicAddr to choose the
2335 right one. Defaults to 127.0.0.1 if no other address is found or cannot
2336 determine the correct address.
2337
2338 ONLY WORKS WITH IPV4 ADDRESSES
2339 """
2340 try:
Jon Hall8f6d4622016-05-23 15:27:18 -07002341 LOCALHOST = "127.0.0.1"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002342 ipPat = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
2343 pattern = re.compile( ipPat )
2344 match = re.search( pattern, self.ip_address )
2345 if self.nicAddr:
2346 nicPat = self.nicAddr.replace( ".", "\." ).replace( "\*", r"\d{1,3}" )
2347 nicPat = re.compile( nicPat )
2348 else:
2349 nicPat = None
2350 # IF self.ip_address is an ip address and matches
2351 # self.nicAddr: return self.ip_address
2352 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002353 curIp = match.group( 0 )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002354 if nicPat:
2355 nicMatch = re.search( nicPat, curIp )
2356 if nicMatch:
2357 return self.ip_address
Jon Hall8f6d4622016-05-23 15:27:18 -07002358 # ELSE: IF iface, return ip of interface
2359 cmd = "ifconfig"
2360 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2361 if iface:
2362 cmd += " " + str( iface )
2363 raw = subprocess.check_output( cmd.split() )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002364 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2365 ips = re.findall( ifPat, raw )
Jon Hall8f6d4622016-05-23 15:27:18 -07002366 if iface:
2367 if ips:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002368 ip = ips[ 0 ]
Jon Hall8f6d4622016-05-23 15:27:18 -07002369 self.ip_address = ip
2370 return ip
2371 else:
2372 main.log.error( "Error finding ip, ifconfig output:".format( raw ) )
2373 # ELSE: attempt to get address matching nicPat.
Jon Hall6c44c0b2016-04-20 15:21:00 -07002374 if nicPat:
2375 for ip in ips:
2376 curMatch = re.search( nicPat, ip )
2377 if curMatch:
2378 self.ip_address = ip
2379 return ip
Jon Hall8f6d4622016-05-23 15:27:18 -07002380 else: # If only one non-localhost ip, return that
2381 tmpList = [ ip for ip in ips if ip is not LOCALHOST ]
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002382 if len( tmpList ) == 1:
2383 curIp = tmpList[ 0 ]
Jon Hall6c44c0b2016-04-20 15:21:00 -07002384 self.ip_address = curIp
2385 return curIp
Jon Hall8f6d4622016-05-23 15:27:18 -07002386 # Either no non-localhost IPs, or more than 1
Jon Hallebccd352016-05-20 15:17:17 -07002387 main.log.warn( "getIpAddr failed to find a public IP address" )
Jon Hall8f6d4622016-05-23 15:27:18 -07002388 return LOCALHOST
Jon Halle1790952016-05-23 15:51:46 -07002389 except subprocess.CalledProcessError:
Jon Hall8f6d4622016-05-23 15:27:18 -07002390 main.log.exception( "Error executing ifconfig" )
2391 except IndexError:
2392 main.log.exception( "Error getting IP Address" )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002393 except Exception:
2394 main.log.exception( "Uncaught exception" )
suibin zhang116647a2016-05-06 16:30:09 -07002395
Devin Lim461f0872017-06-05 16:49:33 -07002396 def startBasicONOS( self, nodeList, opSleep=60, onosStartupSleep=30, onosUser="sdn" ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002397 '''
suibin zhang116647a2016-05-06 16:30:09 -07002398 Start onos cluster with defined nodes, but only with drivers app
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002399 '''
suibin zhang116647a2016-05-06 16:30:09 -07002400 import time
2401
2402 self.createCellFile( self.ip_address,
2403 "temp",
2404 self.ip_address,
2405 "drivers",
Devin Lim461f0872017-06-05 16:49:33 -07002406 nodeList, onosUser )
suibin zhang116647a2016-05-06 16:30:09 -07002407
2408 main.log.info( self.name + ": Apply cell to environment" )
2409 cellResult = self.setCell( "temp" )
2410 verifyResult = self.verifyCell()
2411
2412 main.log.info( self.name + ": Creating ONOS package" )
You Wangd1bcaca2016-10-24 15:23:26 -07002413 packageResult = self.buckBuild( timeout=opSleep )
suibin zhang116647a2016-05-06 16:30:09 -07002414
You Wangc669d212017-01-25 11:09:48 -08002415 main.log.info( self.name + ": Uninstalling ONOS" )
2416 for nd in nodeList:
2417 self.onosUninstall( nodeIp=nd )
2418
suibin zhang116647a2016-05-06 16:30:09 -07002419 main.log.info( self.name + ": Installing ONOS package" )
2420 for nd in nodeList:
You Wangc669d212017-01-25 11:09:48 -08002421 self.onosInstall( node=nd )
2422
2423 main.log.info( self.name + ": Set up ONOS secure SSH" )
2424 for nd in nodeList:
2425 self.onosSecureSSH( node=nd )
suibin zhang116647a2016-05-06 16:30:09 -07002426
2427 main.log.info( self.name + ": Starting ONOS service" )
2428 time.sleep( onosStartupSleep )
2429
2430 onosStatus = True
2431 for nd in nodeList:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002432 onosStatus = onosStatus & self.isup( node = nd )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002433 # print "onosStatus is: " + str( onosStatus )
suibin zhang116647a2016-05-06 16:30:09 -07002434
2435 return main.TRUE if onosStatus else main.FALSE
2436
Devin Lim02075272017-07-10 15:33:21 -07002437 def onosNetCfg( self, controllerIp, path, fileName ):
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002438 """
2439 Push a specified json file to ONOS through the onos-netcfg service
2440
2441 Required:
Devin Lim02075272017-07-10 15:33:21 -07002442 controllerIp - the Ip of the ONOS node in the cluster
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002443 path - the location of the file to be sent
2444 fileName - name of the json file to be sent
2445
2446 Returns main.TRUE on successfully sending json file, and main.FALSE if
2447 there is an error.
2448 """
2449 try:
Devin Lim02075272017-07-10 15:33:21 -07002450 cmd = "onos-netcfg {0} {1}{2}".format( controllerIp, path, fileName )
2451 main.log.info( "Sending: " + cmd )
2452 main.ONOSbench.handle.sendline( cmd )
Jeremy Ronquillo3008aa32017-07-07 15:38:57 -07002453 main.ONOSbench.handle.expect( self.prompt )
Devin Lim02075272017-07-10 15:33:21 -07002454 handle = self.handle.before
Jeremy Ronquillo3008aa32017-07-07 15:38:57 -07002455 if "Error" in handle or "No such file or directory" in handle or "curl: " in handle:
2456 main.log.error( self.name + ": " + handle + self.handle.after )
Devin Lim02075272017-07-10 15:33:21 -07002457 return main.FALSE
Devin Lim752dd7b2017-06-27 14:40:03 -07002458 return main.TRUE
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002459 except pexpect.EOF:
2460 main.log.error( self.name + ": EOF exception found" )
2461 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002462 main.cleanAndExit()
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002463 except Exception:
2464 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002465 main.cleanAndExit()
Devin Lim3ebd5e72017-11-14 10:38:00 -08002466
2467 def formCluster( self, onosIPs ):
2468 """
2469 From ONOS cluster for IP addresses in onosIPs list
2470 """
2471 try:
2472 onosIPs = " ".join( onosIPs )
2473 command = "onos-form-cluster {}".format( onosIPs )
2474 main.log.info( "Sending: " + command )
2475 self.handle.sendline( "" )
2476 self.handle.expect( self.prompt )
2477 self.handle.sendline( command )
2478 self.handle.expect( self.prompt )
2479 handle = self.handle.before
2480 main.log.debug( handle )
2481 assert handle is not None, "Error in sendline"
2482 assert "Command not found:" not in handle, handle
2483 assert "Error" not in handle, handle
2484 assert "Exception:" not in handle, handle
2485 assert "curl:" not in handle, handle
2486 return main.TRUE
2487 except AssertionError:
2488 main.log.exception( "{} Error in onos-form-cluster output:".format( self.name ) )
2489 return main.FALSE
2490 except TypeError:
2491 main.log.exception( self.name + ": Object not as expected" )
2492 return main.FALSE
2493 except pexpect.EOF:
2494 main.log.error( self.name + ": EOF exception found" )
2495 main.log.error( self.name + ": " + self.handle.before )
2496 main.cleanAndExit()
2497 except Exception:
2498 main.log.exception( self.name + ": Uncaught exception!" )
2499 main.cleanAndExit()
Jon Hall7ce46ea2018-02-05 12:20:59 -08002500
2501 def backupData( self, location ):
2502 """
2503 Backs up ONOS data and logs to a given location. Returns main.FALSE
2504 if there is an error executing the command, and main.TRUE otherwise.
2505 required arguments:
2506 loaction - The file path to save the backup to
2507 """
2508 try:
2509 cmd = "/opt/onos/bin/onos-backup " + str( location )
2510 self.handle.sendline( cmd )
2511 self.handle.expect( self.prompt )
2512 handle = self.handle.before
2513 main.log.debug( handle )
2514 assert handle is not None, "Error in sendline"
2515 assert "Command not found:" not in handle, handle
2516 assert "Error" not in handle, handle
2517 assert "Exception:" not in handle, handle
2518 return main.TRUE
2519 except AssertionError:
2520 main.log.exception( "{} Error in onos-backup output:".format( self.name ) )
2521 return main.FALSE
2522 except TypeError:
2523 main.log.exception( self.name + ": Object not as expected" )
2524 return main.FALSE
2525 except pexpect.EOF:
2526 main.log.error( self.name + ": EOF exception found" )
2527 main.log.error( self.name + ": " + self.handle.before )
2528 main.cleanAndExit()
2529 except Exception:
2530 main.log.exception( self.name + ": Uncaught exception!" )
2531 main.cleanAndExit()
2532
2533 def restoreData( self, location ):
2534 """
2535 Restores ONOS data and logs from a given location. Returns main.FALSE
2536 if there is an error executing the command, and main.TRUE otherwise.
2537 required arguments:
2538 loaction - The file path of a backup file
2539 """
2540 try:
2541 cmd = "/opt/onos/bin/onos-restore " + str( location )
2542 self.handle.sendline( cmd )
2543 self.handle.expect( self.prompt )
2544 handle = self.handle.before
2545 main.log.debug( handle )
2546 assert handle is not None, "Error in sendline"
2547 assert "Command not found:" not in handle, handle
2548 assert "Error" not in handle, handle
2549 assert "Exception:" not in handle, handle
2550 return main.TRUE
2551 except AssertionError:
2552 main.log.exception( "{} Error in onos-restore output:".format( self.name ) )
2553 return main.FALSE
2554 except TypeError:
2555 main.log.exception( self.name + ": Object not as expected" )
2556 return main.FALSE
2557 except pexpect.EOF:
2558 main.log.error( self.name + ": EOF exception found" )
2559 main.log.error( self.name + ": " + self.handle.before )
2560 main.cleanAndExit()
2561 except Exception:
2562 main.log.exception( self.name + ": Uncaught exception!" )
2563 main.cleanAndExit()
You Wang5df1c6d2018-04-06 18:02:02 -07002564
2565 def onosDiagnostics( self, onosIPs, dstDir, suffix ):
2566 """
2567 Run onos-diagnostics with given ONOS instance IPs and save output to dstDir
2568 with suffix specified E.g. onos-diags-suffix.tar.gz
Jon Hall0e240372018-05-02 11:21:57 -07002569 required arguments:
You Wang5df1c6d2018-04-06 18:02:02 -07002570 onosIPs - list of ONOS IPs for collecting diags
2571 dstDir - diags file will be saved under the directory specified
2572 suffix - diags file will be named with the suffix specified
2573 returns:
2574 main.FALSE if there's an error executing the command, and main.TRUE otherwise
2575 """
2576 try:
2577 cmd = "onos-diagnostics"
2578 assert isinstance( onosIPs, list )
2579 for ip in onosIPs:
2580 cmd += " " + str( ip )
2581 self.handle.sendline( cmd )
2582 self.handle.expect( self.prompt )
2583 handle = self.handle.before
2584 main.log.debug( handle )
2585 assert handle is not None, "Error in sendline"
2586 assert "Command not found:" not in handle, handle
2587 assert "Exception:" not in handle, handle
2588 # Rename and move diags file to dstDir from /tmp
2589 if dstDir[ -1: ] != "/":
2590 dstDir += "/"
2591 self.handle.sendline( "mv /tmp/onos-diags.tar.gz " + str( dstDir ) + "onos-diags" + str( suffix ) + ".tar.gz" )
2592 self.handle.expect( self.prompt )
2593 handle = self.handle.before
2594 main.log.debug( handle )
2595 assert handle is not None, "Error in sendline"
2596 assert "No such file or directory" not in handle, handle
2597 return main.TRUE
2598 except AssertionError:
2599 main.log.exception( "{} Error in onos-diagnostics output:".format( self.name ) )
2600 return main.FALSE
2601 except TypeError:
2602 main.log.exception( self.name + ": Object not as expected" )
2603 return main.FALSE
2604 except pexpect.EOF:
2605 main.log.error( self.name + ": EOF exception found" )
2606 main.log.error( self.name + ": " + self.handle.before )
2607 main.cleanAndExit()
2608 except Exception:
2609 main.log.exception( self.name + ": Uncaught exception!" )
2610 main.cleanAndExit()
Jon Hall0e240372018-05-02 11:21:57 -07002611
2612 def onosPower( self, onosIP, toggle, userName=None ):
2613 """
2614 Run onos-power script to tell the cell warden to simulate a power faulure
2615 for the given container.
2616 required :
2617 onosIP - ONOS node IP
2618 toggle - either "off" or "on", used to indicate whether
2619 the node should be powered off or on
2620 returns:
2621 main.FALSE if there's an error executing the command, and main.TRUE otherwise
2622 """
2623 try:
2624 cmd = "onos-power {} {}".format( onosIP, toggle )
2625 if userName:
2626 cmd += " {}".format( userName )
2627 self.handle.sendline( cmd )
2628 self.handle.expect( self.prompt )
2629 handle = self.handle.before
2630 main.log.debug( handle )
2631 assert handle is not None, "Error in sendline"
2632 assert "Command not found:" not in handle, handle
2633 assert "Exception:" not in handle, handle
2634 assert "usage:" not in handle, handle
2635 return main.TRUE
2636 except AssertionError:
2637 main.log.exception( "{} Error in onos-power output:".format( self.name ) )
2638 return main.FALSE
2639 except TypeError:
2640 main.log.exception( self.name + ": Object not as expected" )
2641 return main.FALSE
2642 except pexpect.EOF:
2643 main.log.error( self.name + ": EOF exception found" )
2644 main.log.error( self.name + ": " + self.handle.before )
2645 main.cleanAndExit()
2646 except Exception:
2647 main.log.exception( self.name + ": Uncaught exception!" )
2648 main.cleanAndExit()