blob: e85d4bbfd7db84791334bf25f32c36a7526871b1 [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
kelvin8ec71442015-01-15 16:57:00 -08003"""
andrewonlab95ce8322014-10-13 14:12:04 -04004This driver enters the onos> prompt to issue commands.
5
kelvin8ec71442015-01-15 16:57:00 -08006Please follow the coding style demonstrated by existing
andrewonlab95ce8322014-10-13 14:12:04 -04007functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040014shreya@onlab.us
andrewonlab95ce8322014-10-13 14:12:04 -040015
16OCT 13 2014
17
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab95ce8322014-10-13 14:12:04 -040019import sys
andrewonlab95ce8322014-10-13 14:12:04 -040020import pexpect
21import re
kelvin8ec71442015-01-15 16:57:00 -080022sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040023from drivers.common.clidriver import CLI
24
andrewonlab95ce8322014-10-13 14:12:04 -040025
kelvin8ec71442015-01-15 16:57:00 -080026class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040027
kelvin8ec71442015-01-15 16:57:00 -080028 def __init__( self ):
29 """
30 Initialize client
31 """
32 super( CLI, self ).__init__()
33
34 def connect( self, **connectargs ):
35 """
andrewonlab95ce8322014-10-13 14:12:04 -040036 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080037 """
andrewonlab95ce8322014-10-13 14:12:04 -040038 try:
39 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080040 vars( self )[ key ] = connectargs[ key ]
andrewonlab95ce8322014-10-13 14:12:04 -040041 self.home = "~/ONOS"
42 for key in self.options:
43 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080044 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040045 break
kelvin-onlabd6634ac2015-01-29 14:23:10 -080046 if self.home == None or self.home == "":
47 self.home = "~/ONOS"
48
kelvin8ec71442015-01-15 16:57:00 -080049 self.name = self.options[ 'name' ]
50 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080051 user_name=self.user_name,
52 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080053 port=self.port,
54 pwd=self.pwd,
55 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040056
kelvin8ec71442015-01-15 16:57:00 -080057 self.handle.sendline( "cd " + self.home )
58 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040059 if self.handle:
60 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080061 else:
62 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040063 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080064 except TypeError:
65 main.log.exception( self.name + ": Object not as expected" )
66 return None
andrewonlab95ce8322014-10-13 14:12:04 -040067 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080068 main.log.error( self.name + ": EOF exception found" )
69 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040070 main.cleanup()
71 main.exit()
72 except:
Jon Halld4d4b372015-01-28 16:02:41 -080073 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -040074 main.cleanup()
75 main.exit()
76
kelvin8ec71442015-01-15 16:57:00 -080077 def disconnect( self ):
78 """
andrewonlab95ce8322014-10-13 14:12:04 -040079 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080080 """
Jon Halld61331b2015-02-17 16:35:47 -080081 response = main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -040082 try:
kelvin8ec71442015-01-15 16:57:00 -080083 self.handle.sendline( "" )
84 i = self.handle.expect( [ "onos>", "\$" ] )
Jon Hall7e5b9172014-10-22 12:32:47 -040085 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -080086 self.handle.sendline( "system:shutdown" )
87 self.handle.expect( "Confirm" )
88 self.handle.sendline( "yes" )
89 self.handle.expect( "\$" )
90 self.handle.sendline( "" )
91 self.handle.expect( "\$" )
92 self.handle.sendline( "exit" )
93 self.handle.expect( "closed" )
andrewonlabc2d05aa2014-10-13 16:51:10 -040094
Jon Halld4d4b372015-01-28 16:02:41 -080095 except TypeError:
96 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -080097 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -040098 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080099 main.log.error( self.name + ": EOF exception found" )
100 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400101 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800102 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400103 response = main.FALSE
104 return response
105
kelvin8ec71442015-01-15 16:57:00 -0800106 def logout( self ):
107 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500108 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800109 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500110 try:
kelvin8ec71442015-01-15 16:57:00 -0800111 self.handle.sendline( "" )
112 i = self.handle.expect( [
andrewonlab9627f432014-11-14 12:45:10 -0500113 "onos>",
kelvin8ec71442015-01-15 16:57:00 -0800114 "\$" ], timeout=10 )
andrewonlab9627f432014-11-14 12:45:10 -0500115 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800116 self.handle.sendline( "logout" )
117 self.handle.expect( "\$" )
andrewonlab9627f432014-11-14 12:45:10 -0500118 elif i == 1:
119 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800120
Jon Halld4d4b372015-01-28 16:02:41 -0800121 except TypeError:
122 main.log.exception( self.name + ": Object not as expected" )
123 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500124 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800125 main.log.error( self.name + ": eof exception found" )
126 main.log.error( self.name + ": " +
127 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500128 main.cleanup()
129 main.exit()
130 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800131 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500132 main.cleanup()
133 main.exit()
134
kelvin-onlabd3b64892015-01-20 13:26:24 -0800135 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800136 """
andrewonlab95ce8322014-10-13 14:12:04 -0400137 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800138
andrewonlab95ce8322014-10-13 14:12:04 -0400139 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800140 """
andrewonlab95ce8322014-10-13 14:12:04 -0400141 try:
142 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800143 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400144 main.cleanup()
145 main.exit()
146 else:
kelvin8ec71442015-01-15 16:57:00 -0800147 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800148 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800149 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400150 # and that this driver will have to change accordingly
Jon Hall1e03cb62015-02-19 12:07:12 -0800151 self.handle.expect( "ONOS_CELL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800152 handleBefore = self.handle.before
153 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800154 # Get the rest of the handle
155 self.handle.sendline( "" )
156 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800157 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400158
kelvin-onlabd3b64892015-01-20 13:26:24 -0800159 main.log.info( "Cell call returned: " + handleBefore +
160 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400161
162 return main.TRUE
163
Jon Halld4d4b372015-01-28 16:02:41 -0800164 except TypeError:
165 main.log.exception( self.name + ": Object not as expected" )
166 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400167 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800168 main.log.error( self.name + ": eof exception found" )
169 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400170 main.cleanup()
171 main.exit()
172 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800173 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400174 main.cleanup()
175 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800176
kelvin-onlabd3b64892015-01-20 13:26:24 -0800177 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800178 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 karafTimeout is an optional arugument. karafTimeout value passed
180 by user would be used to set the current karaf shell idle timeout.
181 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800182 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800183 Below is an example to start a session with 60 seconds idle timeout
184 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800185
Hari Krishna25d42f72015-01-05 15:08:28 -0800186 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800187 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800188
kelvin-onlabd3b64892015-01-20 13:26:24 -0800189 Note: karafTimeout is left as str so that this could be read
190 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800191 """
andrewonlab95ce8322014-10-13 14:12:04 -0400192 try:
kelvin8ec71442015-01-15 16:57:00 -0800193 self.handle.sendline( "" )
194 x = self.handle.expect( [
195 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500196
197 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800198 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500199 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400200
kelvin8ec71442015-01-15 16:57:00 -0800201 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800202 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800203 i = self.handle.expect( [
204 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800205 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400206
207 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800208 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800209 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800210 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800211 "config:property-set -p org.apache.karaf.shell\
212 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800213 karafTimeout )
214 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800216 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400217 return main.TRUE
218 else:
kelvin8ec71442015-01-15 16:57:00 -0800219 # If failed, send ctrl+c to process and try again
220 main.log.info( "Starting CLI failed. Retrying..." )
221 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800222 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800223 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
224 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400225 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800226 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800227 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800228 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800229 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800230 "config:property-set -p org.apache.karaf.shell\
231 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800232 karafTimeout )
233 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800234 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800235 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400236 return main.TRUE
237 else:
kelvin8ec71442015-01-15 16:57:00 -0800238 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800239 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400240 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400241
Jon Halld4d4b372015-01-28 16:02:41 -0800242 except TypeError:
243 main.log.exception( self.name + ": Object not as expected" )
244 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400245 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800246 main.log.error( self.name + ": EOF exception found" )
247 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400248 main.cleanup()
249 main.exit()
250 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800251 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400252 main.cleanup()
253 main.exit()
254
kelvin-onlab338f5512015-02-06 10:53:16 -0800255 def log( self, cmdStr , level = "" ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800256 """
257 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800258 returns main.TRUE on success
259 returns main.FALSE if Error occured
260 Available level: DEBUG, TRACE, INFO, WARN, ERROR
261 Level defaults to INFO
kelvin-onlab9f541032015-02-04 16:19:53 -0800262 """
263 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800264 lvlStr = ""
265 if level:
266 lvlStr = "--level=" + level
267
kelvin-onlab9f541032015-02-04 16:19:53 -0800268 self.handle.sendline( "" )
269 self.handle.expect( "onos>" )
kelvin-onlab338f5512015-02-06 10:53:16 -0800270 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
kelvin-onlab9f541032015-02-04 16:19:53 -0800271 self.handle.expect( "onos>" )
272
273 response = self.handle.before
274 if re.search( "Error", response ):
275 return main.FALSE
276 return main.TRUE
277
278 except pexpect.EOF:
279 main.log.error( self.name + ": EOF exception found" )
280 main.log.error( self.name + ": " + self.handle.before )
281 main.cleanup()
282 main.exit()
283 except:
284 main.log.info( self.name + " ::::::" )
285 main.log.error( traceback.print_exc() )
286 main.log.info( self.name + " ::::::" )
287 main.cleanup()
288 main.exit()
289
kelvin-onlabd3b64892015-01-20 13:26:24 -0800290 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800291 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800292 Send a completely user specified string to
293 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400294 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800295
andrewonlaba18f6bf2014-10-13 19:31:54 -0400296 Warning: There are no sanity checking to commands
297 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800298 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400299 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800300
301 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
302 self.log( logStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800303 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800304 self.handle.expect( "onos>" )
Jon Hallaea67aa2015-01-23 13:30:57 -0800305 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
306 + self.name + "." )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400307
308 handle = self.handle.before
Jon Hall7bdfc122015-01-23 11:45:32 -0800309 # Remove control strings from output
310 ansiEscape = re.compile( r'\x1b[^m]*m' )
311 handle = ansiEscape.sub( '', handle )
Jon Hall44225f82015-01-23 13:45:14 -0800312 #Remove extra return chars that get added
Jon Hallaea67aa2015-01-23 13:30:57 -0800313 handle = re.sub( r"\s\r", "", handle )
314 handle = handle.strip()
Jon Hall7bdfc122015-01-23 11:45:32 -0800315 # parse for just the output, remove the cmd from handle
316 output = handle.split( cmdStr, 1 )[1]
kelvin8ec71442015-01-15 16:57:00 -0800317
andrewonlaba18f6bf2014-10-13 19:31:54 -0400318
Jon Hall7bdfc122015-01-23 11:45:32 -0800319 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800320 except TypeError:
321 main.log.exception( self.name + ": Object not as expected" )
322 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400323 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800324 main.log.error( self.name + ": EOF exception found" )
325 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400326 main.cleanup()
327 main.exit()
328 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800329 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400330 main.cleanup()
331 main.exit()
332
kelvin8ec71442015-01-15 16:57:00 -0800333 # IMPORTANT NOTE:
334 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800335 # the cli command changing 'a:b' with 'aB'.
336 # Ex ) onos:topology > onosTopology
337 # onos:links > onosLinks
338 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800339
kelvin-onlabd3b64892015-01-20 13:26:24 -0800340 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800341 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400342 Adds a new cluster node by ID and address information.
343 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800344 * nodeId
345 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400346 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800347 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800348 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400349 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800350 cmdStr = "add-node " + str( nodeId ) + " " +\
351 str( ONOSIp ) + " " + str( tcpPort )
352 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800353 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800354 main.log.error( "Error in adding node" )
355 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800356 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400357 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800358 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400359 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800360 except TypeError:
361 main.log.exception( self.name + ": Object not as expected" )
362 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400363 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800364 main.log.error( self.name + ": EOF exception found" )
365 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400366 main.cleanup()
367 main.exit()
368 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800369 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400370 main.cleanup()
371 main.exit()
372
kelvin-onlabd3b64892015-01-20 13:26:24 -0800373 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800374 """
andrewonlab86dc3082014-10-13 18:18:38 -0400375 Removes a cluster by ID
376 Issues command: 'remove-node [<node-id>]'
377 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800378 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800379 """
andrewonlab86dc3082014-10-13 18:18:38 -0400380 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400381
kelvin-onlabd3b64892015-01-20 13:26:24 -0800382 cmdStr = "remove-node " + str( nodeId )
383 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800384 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400385
386 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800387
Jon Halld4d4b372015-01-28 16:02:41 -0800388 except TypeError:
389 main.log.exception( self.name + ": Object not as expected" )
390 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400391 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800392 main.log.error( self.name + ": EOF exception found" )
393 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400394 main.cleanup()
395 main.exit()
396 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800397 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400398 main.cleanup()
399 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400400
kelvin8ec71442015-01-15 16:57:00 -0800401 def nodes( self ):
402 """
andrewonlab7c211572014-10-15 16:45:20 -0400403 List the nodes currently visible
404 Issues command: 'nodes'
405 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800406 """
andrewonlab7c211572014-10-15 16:45:20 -0400407 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800408 cmdStr = "nodes"
409 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400410 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800411 except TypeError:
412 main.log.exception( self.name + ": Object not as expected" )
413 return None
andrewonlab7c211572014-10-15 16:45:20 -0400414 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800415 main.log.error( self.name + ": EOF exception found" )
416 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400417 main.cleanup()
418 main.exit()
419 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800420 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400421 main.cleanup()
422 main.exit()
423
kelvin8ec71442015-01-15 16:57:00 -0800424 def topology( self ):
425 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400426 Shows the current state of the topology
427 by issusing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800428 """
andrewonlab95ce8322014-10-13 14:12:04 -0400429 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800430 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800431 cmdStr = "onos:topology"
432 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800433 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400434 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800435 except TypeError:
436 main.log.exception( self.name + ": Object not as expected" )
437 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400438 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800439 main.log.error( self.name + ": EOF exception found" )
440 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400441 main.cleanup()
442 main.exit()
443 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800444 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400445 main.cleanup()
446 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800447
kelvin-onlabd3b64892015-01-20 13:26:24 -0800448 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800449 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800450 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400451 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800452 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400453 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800454 cmdStr = "feature:install " + str( featureStr )
455 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800456 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400457 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800458 except TypeError:
459 main.log.exception( self.name + ": Object not as expected" )
460 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400461 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800462 main.log.error( self.name + ": EOF exception found" )
463 main.log.error( self.name + ": " + self.handle.before )
464 main.log.report( "Failed to install feature" )
465 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400466 main.cleanup()
467 main.exit()
468 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800469 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800470 main.log.report( "Failed to install feature" )
471 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400472 main.cleanup()
473 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800474
kelvin-onlabd3b64892015-01-20 13:26:24 -0800475 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800476 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400477 Uninstalls a specified feature
478 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800479 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400480 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800481 cmdStr = "feature:uninstall " + str( featureStr )
482 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800483 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400484 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800485 except TypeError:
486 main.log.exception( self.name + ": Object not as expected" )
487 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400488 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800489 main.log.error( self.name + ": EOF exception found" )
490 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400491 main.cleanup()
492 main.exit()
493 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800494 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400495 main.cleanup()
496 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800497
kelvin-onlabd3b64892015-01-20 13:26:24 -0800498 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800499 """
Jon Hall7b02d952014-10-17 20:14:54 -0400500 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400501 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800502 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800503 """
andrewonlab86dc3082014-10-13 18:18:38 -0400504 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800505 if jsonFormat:
506 cmdStr = "devices -j"
507 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800508 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800509 handle variable here contains some ANSI escape color code
510 sequences at the end which are invisible in the print command
511 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800512 function. The repr( handle ) output when printed shows the
513 ANSI escape sequences. In json.loads( somestring ), this
514 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800515 json.loads would fail with the escape sequence. So we take off
516 that escape sequence using:
517
kelvin-onlabd3b64892015-01-20 13:26:24 -0800518 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
519 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800520 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800521 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
522 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400523 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400524 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800525 cmdStr = "devices"
526 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400527 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800528 except TypeError:
529 main.log.exception( self.name + ": Object not as expected" )
530 return None
andrewonlab7c211572014-10-15 16:45:20 -0400531 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800532 main.log.error( self.name + ": EOF exception found" )
533 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400534 main.cleanup()
535 main.exit()
536 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800537 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400538 main.cleanup()
539 main.exit()
540
kelvin-onlabd3b64892015-01-20 13:26:24 -0800541 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800542 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800543 This balances the devices across all controllers
544 by issuing command: 'onos> onos:balance-masters'
545 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800546 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800547 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800548 cmdStr = "onos:balance-masters"
549 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800550 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800551 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800552 except TypeError:
553 main.log.exception( self.name + ": Object not as expected" )
554 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800555 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800556 main.log.error( self.name + ": EOF exception found" )
557 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800558 main.cleanup()
559 main.exit()
560 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800561 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800562 main.cleanup()
563 main.exit()
564
kelvin-onlabd3b64892015-01-20 13:26:24 -0800565 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800566 """
Jon Halle8217482014-10-17 13:49:14 -0400567 Lists all core links
568 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800569 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800570 """
Jon Halle8217482014-10-17 13:49:14 -0400571 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800572 if jsonFormat:
573 cmdStr = "links -j"
574 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800575 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800576 handle variable here contains some ANSI escape color code
577 sequences at the end which are invisible in the print command
578 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800579 function. The repr( handle ) output when printed shows the ANSI
580 escape sequences. In json.loads( somestring ), this somestring
581 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800582 fail with the escape sequence. So we take off that escape
583 sequence using:
584
kelvin-onlabd3b64892015-01-20 13:26:24 -0800585 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
586 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800587 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800588 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
589 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400590 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400591 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800592 cmdStr = "links"
593 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400594 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800595 except TypeError:
596 main.log.exception( self.name + ": Object not as expected" )
597 return None
Jon Halle8217482014-10-17 13:49:14 -0400598 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800599 main.log.error( self.name + ": EOF exception found" )
600 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400601 main.cleanup()
602 main.exit()
603 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800604 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400605 main.cleanup()
606 main.exit()
607
kelvin-onlabd3b64892015-01-20 13:26:24 -0800608 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800609 """
Jon Halle8217482014-10-17 13:49:14 -0400610 Lists all ports
611 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800612 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800613 """
Jon Halle8217482014-10-17 13:49:14 -0400614 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800615 if jsonFormat:
616 cmdStr = "ports -j"
617 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800618 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800619 handle variable here contains some ANSI escape color code
620 sequences at the end which are invisible in the print command
621 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800622 function. The repr( handle ) output when printed shows the ANSI
623 escape sequences. In json.loads( somestring ), this somestring
624 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800625 fail with the escape sequence. So we take off that escape
626 sequence using the following commads:
627
kelvin-onlabd3b64892015-01-20 13:26:24 -0800628 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
629 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800630 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800631 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
632 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400633 return handle1
634
Jon Halle8217482014-10-17 13:49:14 -0400635 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800636 cmdStr = "ports"
637 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800638 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800639 except TypeError:
640 main.log.exception( self.name + ": Object not as expected" )
641 return None
Jon Halle8217482014-10-17 13:49:14 -0400642 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 )
Jon Halle8217482014-10-17 13:49:14 -0400645 main.cleanup()
646 main.exit()
647 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800648 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400649 main.cleanup()
650 main.exit()
651
kelvin-onlabd3b64892015-01-20 13:26:24 -0800652 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800653 """
Jon Hall983a1702014-10-28 18:44:22 -0400654 Lists all devices and the controllers with roles assigned to them
655 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800656 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800657 """
andrewonlab7c211572014-10-15 16:45:20 -0400658 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800659 if jsonFormat:
660 cmdStr = "roles -j"
661 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800662 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800663 handle variable here contains some ANSI escape color code
664 sequences at the end which are invisible in the print command
665 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800666 function. The repr( handle ) output when printed shows the ANSI
667 escape sequences. In json.loads( somestring ), this somestring
668 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800669 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500670
Jon Halle3f39ff2015-01-13 11:50:53 -0800671 So we take off that escape sequence using the following
672 commads:
673
kelvin-onlabd3b64892015-01-20 13:26:24 -0800674 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
675 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800676 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800677 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
678 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400679 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400680
andrewonlab7c211572014-10-15 16:45:20 -0400681 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800682 cmdStr = "roles"
683 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800684 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800685 except TypeError:
686 main.log.exception( self.name + ": Object not as expected" )
687 return None
Jon Hall983a1702014-10-28 18:44:22 -0400688 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800689 main.log.error( self.name + ": EOF exception found" )
690 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400691 main.cleanup()
692 main.exit()
693 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800694 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400695 main.cleanup()
696 main.exit()
697
kelvin-onlabd3b64892015-01-20 13:26:24 -0800698 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800699 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800700 Given the a string containing the json representation of the "roles"
701 cli command and a partial or whole device id, returns a json object
702 containing the roles output for the first device whose id contains
703 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400704
705 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800706 A dict of the role assignments for the given device or
707 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800708 """
Jon Hall983a1702014-10-28 18:44:22 -0400709 try:
710 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800711 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400712 return None
713 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800714 rawRoles = self.roles()
715 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800716 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800717 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800718 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800719 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400720 return device
721 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800722 except TypeError:
723 main.log.exception( self.name + ": Object not as expected" )
724 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400725 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800726 main.log.error( self.name + ": EOF exception found" )
727 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400728 main.cleanup()
729 main.exit()
730 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800731 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400732 main.cleanup()
733 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800734
kelvin-onlabd3b64892015-01-20 13:26:24 -0800735 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800736 """
Jon Hall94fd0472014-12-08 11:52:42 -0800737 Iterates through each device and checks if there is a master assigned
738 Returns: main.TRUE if each device has a master
739 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800740 """
Jon Hall94fd0472014-12-08 11:52:42 -0800741 try:
742 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800743 rawRoles = self.roles()
744 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800745 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800746 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800747 # print device
748 if device[ 'master' ] == "none":
749 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800750 return main.FALSE
751 return main.TRUE
752
Jon Halld4d4b372015-01-28 16:02:41 -0800753 except TypeError:
754 main.log.exception( self.name + ": Object not as expected" )
755 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800756 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800757 main.log.error( self.name + ": EOF exception found" )
758 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800759 main.cleanup()
760 main.exit()
761 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800762 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800763 main.cleanup()
764 main.exit()
765
kelvin-onlabd3b64892015-01-20 13:26:24 -0800766 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800767 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400768 Returns string of paths, and the cost.
769 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800770 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400771 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800772 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
773 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800774 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800775 main.log.error( "Error in getting paths" )
776 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400777 else:
kelvin8ec71442015-01-15 16:57:00 -0800778 path = handle.split( ";" )[ 0 ]
779 cost = handle.split( ";" )[ 1 ]
780 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800781 except TypeError:
782 main.log.exception( self.name + ": Object not as expected" )
783 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400784 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800785 main.log.error( self.name + ": EOF exception found" )
786 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400787 main.cleanup()
788 main.exit()
789 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800790 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400791 main.cleanup()
792 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800793
kelvin-onlabd3b64892015-01-20 13:26:24 -0800794 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800795 """
Jon Hallffb386d2014-11-21 13:43:38 -0800796 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400797 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800798 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800799 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400800 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800801 if jsonFormat:
802 cmdStr = "hosts -j"
803 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800804 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800805 handle variable here contains some ANSI escape color code
806 sequences at the end which are invisible in the print command
807 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800808 function. The repr( handle ) output when printed shows the ANSI
809 escape sequences. In json.loads( somestring ), this somestring
810 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800811 fail with the escape sequence. So we take off that escape
812 sequence using:
813
kelvin-onlabd3b64892015-01-20 13:26:24 -0800814 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
815 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800816 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800817 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
818 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400819 return handle1
820 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800821 cmdStr = "hosts"
822 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400823 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800824 except TypeError:
825 main.log.exception( self.name + ": Object not as expected" )
826 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400827 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800828 main.log.error( self.name + ": EOF exception found" )
829 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400830 main.cleanup()
831 main.exit()
832 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800833 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400834 main.cleanup()
835 main.exit()
836
kelvin-onlabd3b64892015-01-20 13:26:24 -0800837 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800838 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400839 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800840
841 Note: mac must be a colon seperated mac address, but could be a
842 partial mac address
843
Jon Hall42db6dc2014-10-24 19:03:48 -0400844 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800845 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400846 import json
847 try:
kelvin8ec71442015-01-15 16:57:00 -0800848 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400849 return None
850 else:
851 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800852 rawHosts = self.hosts()
853 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800854 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800855 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800856 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800857 if not host:
858 pass
859 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400860 return host
861 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800862 except TypeError:
863 main.log.exception( self.name + ": Object not as expected" )
864 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400865 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800866 main.log.error( self.name + ": EOF exception found" )
867 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400868 main.cleanup()
869 main.exit()
870 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800871 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400872 main.cleanup()
873 main.exit()
874
kelvin-onlabd3b64892015-01-20 13:26:24 -0800875 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800876 """
877 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400878 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800879
andrewonlab3f0a4af2014-10-17 12:25:14 -0400880 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800881 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400882 IMPORTANT:
883 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800884 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400885 Furthermore, it assumes that value of VLAN is '-1'
886 Description:
kelvin8ec71442015-01-15 16:57:00 -0800887 Converts mininet hosts ( h1, h2, h3... ) into
888 ONOS format ( 00:00:00:00:00:01/-1 , ... )
889 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400890 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800891 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400892
kelvin-onlabd3b64892015-01-20 13:26:24 -0800893 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800894 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800895 hostHex = hex( int( host ) ).zfill( 12 )
896 hostHex = str( hostHex ).replace( 'x', '0' )
897 i = iter( str( hostHex ) )
898 hostHex = ":".join( a + b for a, b in zip( i, i ) )
899 hostHex = hostHex + "/-1"
900 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400901
kelvin-onlabd3b64892015-01-20 13:26:24 -0800902 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400903
Jon Halld4d4b372015-01-28 16:02:41 -0800904 except TypeError:
905 main.log.exception( self.name + ": Object not as expected" )
906 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400907 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800908 main.log.error( self.name + ": EOF exception found" )
909 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400910 main.cleanup()
911 main.exit()
912 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800913 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400914 main.cleanup()
915 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400916
kelvin-onlabd3b64892015-01-20 13:26:24 -0800917 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800918 """
andrewonlabe6745342014-10-17 14:29:13 -0400919 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800920 * hostIdOne: ONOS host id for host1
921 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400922 Description:
kelvin8ec71442015-01-15 16:57:00 -0800923 Adds a host-to-host intent ( bidrectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500924 specifying the two hosts.
Jon Halla5cb6172015-02-23 09:28:28 -0800925 returns a string of the intent id or an Error message
kelvin8ec71442015-01-15 16:57:00 -0800926 """
andrewonlabe6745342014-10-17 14:29:13 -0400927 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800928 cmdStr = "add-host-intent " + str( hostIdOne ) +\
929 " " + str( hostIdTwo )
930 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800931 if re.search( "Error", handle ):
932 main.log.error( "Error in adding Host intent" )
933 return handle
934 else:
935 main.log.info( "Host intent installed between " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800936 str( hostIdOne ) + " and " + str( hostIdTwo ) )
Jon Halla5cb6172015-02-23 09:28:28 -0800937 match = re.search('id=0x([\da-f]+),', handle)
938 if match:
939 return match.group()[3:-1]
940 else:
941 return handle
Hari Krishnaac4e1782015-01-26 12:09:12 -0800942
Jon Halld4d4b372015-01-28 16:02:41 -0800943 except TypeError:
944 main.log.exception( self.name + ": Object not as expected" )
945 return None
Hari Krishnaccfb0d52015-02-19 09:38:29 -0800946
andrewonlabe6745342014-10-17 14:29:13 -0400947 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800948 main.log.error( self.name + ": EOF exception found" )
949 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400950 main.cleanup()
951 main.exit()
952 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800953 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400954 main.cleanup()
955 main.exit()
956
kelvin-onlabd3b64892015-01-20 13:26:24 -0800957 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800958 """
andrewonlab7b31d232014-10-24 13:31:47 -0400959 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800960 * ingressDevice: device id of ingress device
961 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400962 Optional:
963 TODO: Still needs to be implemented via dev side
kelvin-onlab898a6c62015-01-16 14:13:53 -0800964 """
andrewonlab7b31d232014-10-24 13:31:47 -0400965 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800966 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
967 " " + str( egressDevice )
968 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800969 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800970 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400971 return handle
972 else:
973 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800974 except TypeError:
975 main.log.exception( self.name + ": Object not as expected" )
976 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400977 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800978 main.log.error( self.name + ": EOF exception found" )
979 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400980 main.cleanup()
981 main.exit()
982 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800983 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -0400984 main.cleanup()
985 main.exit()
986
kelvin-onlabd3b64892015-01-20 13:26:24 -0800987 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800988 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800989 ingressDevice,
990 egressDevice,
991 portIngress="",
992 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800993 ethType="",
994 ethSrc="",
995 ethDst="",
996 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800997 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800998 ipProto="",
999 ipSrc="",
1000 ipDst="",
1001 tcpSrc="",
1002 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001003 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001004 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001005 * ingressDevice: device id of ingress device
1006 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001007 Optional:
1008 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001009 * ethSrc: specify ethSrc ( i.e. src mac addr )
1010 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001011 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001012 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001013 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001014 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001015 * ipSrc: specify ip source address
1016 * ipDst: specify ip destination address
1017 * tcpSrc: specify tcp source port
1018 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001019 Description:
kelvin8ec71442015-01-15 16:57:00 -08001020 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001021 specifying device id's and optional fields
1022
Jon Halle3f39ff2015-01-13 11:50:53 -08001023 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001024 options developers provide for point-to-point
1025 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001026 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001027 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001028 cmd = ""
1029
kelvin8ec71442015-01-15 16:57:00 -08001030 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001031 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001032 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001033 and not ipProto and not ipSrc and not ipDst \
1034 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001035 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001036
andrewonlab289e4b72014-10-21 21:24:18 -04001037 else:
andrewonlab36af3822014-11-18 17:48:18 -05001038 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001039
andrewonlab0c0a6772014-10-22 12:31:18 -04001040 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001041 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001042 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001043 cmd += " --ethSrc " + str( ethSrc )
1044 if ethDst:
1045 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001046 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001047 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001048 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001049 cmd += " --lambda "
1050 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001051 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001052 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001053 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001054 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001055 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001056 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001057 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001058 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001059 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001060
kelvin8ec71442015-01-15 16:57:00 -08001061 # Check whether the user appended the port
1062 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001063 if "/" in ingressDevice:
1064 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001065 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001066 if not portIngress:
kelvin8ec71442015-01-15 16:57:00 -08001067 main.log.error( "You must specify " +
1068 "the ingress port" )
1069 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -05001070 return main.FALSE
1071
kelvin8ec71442015-01-15 16:57:00 -08001072 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001073 str( ingressDevice ) + "/" +\
1074 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001075
kelvin-onlabd3b64892015-01-20 13:26:24 -08001076 if "/" in egressDevice:
1077 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001078 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001079 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001080 main.log.error( "You must specify " +
1081 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001082 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001083
kelvin8ec71442015-01-15 16:57:00 -08001084 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001085 str( egressDevice ) + "/" +\
1086 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001087
kelvin-onlab898a6c62015-01-16 14:13:53 -08001088 handle = self.sendline( cmd )
1089 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001090 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001091 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001092 else:
1093 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001094 except TypeError:
1095 main.log.exception( self.name + ": Object not as expected" )
1096 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001097 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001098 main.log.error( self.name + ": EOF exception found" )
1099 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001100 main.cleanup()
1101 main.exit()
1102 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001103 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001104 main.cleanup()
1105 main.exit()
1106
kelvin-onlabd3b64892015-01-20 13:26:24 -08001107 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001108 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001109 ingressDevice1,
1110 ingressDevice2,
1111 egressDevice,
1112 portIngress="",
1113 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001114 ethType="",
1115 ethSrc="",
1116 ethDst="",
1117 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001118 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001119 ipProto="",
1120 ipSrc="",
1121 ipDst="",
1122 tcpSrc="",
1123 tcpDst="",
1124 setEthSrc="",
1125 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001126 """
shahshreyad0c80432014-12-04 16:56:05 -08001127 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001128 This function assumes that there would be 2 ingress devices and
1129 one egress device. For more number of ingress devices, this
1130 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001131 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001132 * ingressDevice1: device id of ingress device1
1133 * ingressDevice2: device id of ingress device2
1134 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001135 Optional:
1136 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001137 * ethSrc: specify ethSrc ( i.e. src mac addr )
1138 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001139 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001140 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001141 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001142 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001143 * ipSrc: specify ip source address
1144 * ipDst: specify ip destination address
1145 * tcpSrc: specify tcp source port
1146 * tcpDst: specify tcp destination port
1147 * setEthSrc: action to Rewrite Source MAC Address
1148 * setEthDst: action to Rewrite Destination MAC Address
1149 Description:
kelvin8ec71442015-01-15 16:57:00 -08001150 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001151 specifying device id's and optional fields
1152
Jon Halle3f39ff2015-01-13 11:50:53 -08001153 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001154 options developers provide for multipointpoint-to-singlepoint
1155 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001156 """
shahshreyad0c80432014-12-04 16:56:05 -08001157 try:
1158 cmd = ""
1159
kelvin8ec71442015-01-15 16:57:00 -08001160 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001161 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001162 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001163 and not ipProto and not ipSrc and not ipDst\
1164 and not tcpSrc and not tcpDst and not setEthSrc\
1165 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001166 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001167
1168 else:
1169 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001170
shahshreyad0c80432014-12-04 16:56:05 -08001171 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001172 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001173 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001174 cmd += " --ethSrc " + str( ethSrc )
1175 if ethDst:
1176 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001177 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001178 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001179 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001180 cmd += " --lambda "
1181 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001182 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001183 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001184 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001185 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001186 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001187 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001188 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001189 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001190 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001191 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001192 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001193 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001194 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001195
kelvin8ec71442015-01-15 16:57:00 -08001196 # Check whether the user appended the port
1197 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001198 if "/" in ingressDevice1:
1199 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001200 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001201 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001202 main.log.error( "You must specify " +
1203 "the ingress port1" )
1204 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001205 return main.FALSE
1206
kelvin8ec71442015-01-15 16:57:00 -08001207 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001208 str( ingressDevice1 ) + "/" +\
1209 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001210
kelvin-onlabd3b64892015-01-20 13:26:24 -08001211 if "/" in ingressDevice2:
1212 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001213 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001214 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001215 main.log.error( "You must specify " +
1216 "the ingress port2" )
1217 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001218 return main.FALSE
1219
kelvin8ec71442015-01-15 16:57:00 -08001220 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001221 str( ingressDevice2 ) + "/" +\
1222 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001223
kelvin-onlabd3b64892015-01-20 13:26:24 -08001224 if "/" in egressDevice:
1225 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001226 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001227 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001228 main.log.error( "You must specify " +
1229 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001230 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001231
kelvin8ec71442015-01-15 16:57:00 -08001232 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001233 str( egressDevice ) + "/" +\
1234 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001235 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001236 handle = self.sendline( cmd )
1237 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001238 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001239 return self.handle
1240 else:
1241 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001242 except TypeError:
1243 main.log.exception( self.name + ": Object not as expected" )
1244 return None
shahshreyad0c80432014-12-04 16:56:05 -08001245 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001246 main.log.error( self.name + ": EOF exception found" )
1247 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001248 main.cleanup()
1249 main.exit()
1250 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001251 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001252 main.cleanup()
1253 main.exit()
1254
kelvin-onlabd3b64892015-01-20 13:26:24 -08001255 def removeIntent( self, intentId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001256 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001257 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001258
1259 Returns:
1260 main.False on error and
1261 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001262 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001263 try:
shahshreyac033d022015-02-20 16:10:19 -08001264 cmdStr = "remove-intent org.onosproject.cli " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001265 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001266 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001267 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001268 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001269 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001270 # TODO: Should this be main.TRUE
1271 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001272 except TypeError:
1273 main.log.exception( self.name + ": Object not as expected" )
1274 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -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 )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001278 main.cleanup()
1279 main.exit()
1280 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001281 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001282 main.cleanup()
1283 main.exit()
1284
kelvin-onlabd3b64892015-01-20 13:26:24 -08001285 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001286 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001287 NOTE: This method should be used after installing application:
1288 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001289 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001290 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001291 Description:
1292 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001293 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001294 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001295 if jsonFormat:
1296 cmdStr = "routes -j"
1297 handleTmp = self.sendline( cmdStr )
1298 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1299 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001300 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001301 cmdStr = "routes"
1302 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001303 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001304 except TypeError:
1305 main.log.exception( self.name + ": Object not as expected" )
1306 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001307 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001308 main.log.error( self.name + ": EOF exception found" )
1309 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001310 main.cleanup()
1311 main.exit()
1312 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001313 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001314 main.cleanup()
1315 main.exit()
1316
kelvin-onlabd3b64892015-01-20 13:26:24 -08001317 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001318 """
andrewonlab377693f2014-10-21 16:00:30 -04001319 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001320 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001321 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001322 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001323 """
andrewonlabe6745342014-10-17 14:29:13 -04001324 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001325 if jsonFormat:
1326 cmdStr = "intents -j"
1327 handle = self.sendline( cmdStr )
1328 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1329 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001330 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001331 cmdStr = "intents"
1332 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001333 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001334 except TypeError:
1335 main.log.exception( self.name + ": Object not as expected" )
1336 return None
andrewonlabe6745342014-10-17 14:29:13 -04001337 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001338 main.log.error( self.name + ": EOF exception found" )
1339 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001340 main.cleanup()
1341 main.exit()
1342 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001343 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001344 main.cleanup()
1345 main.exit()
1346
kelvin-onlabd3b64892015-01-20 13:26:24 -08001347 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001348 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001349 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001350 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001351 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001352 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001353 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001354 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001355 if jsonFormat:
1356 cmdStr = "flows -j"
1357 handle = self.sendline( cmdStr )
1358 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1359 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001360 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001361 cmdStr = "flows"
1362 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001363 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001364 main.log.error( self.name + ".flows() response: " +
1365 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001366 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001367 except TypeError:
1368 main.log.exception( self.name + ": Object not as expected" )
1369 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001370 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001371 main.log.error( self.name + ": EOF exception found" )
1372 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001373 main.cleanup()
1374 main.exit()
1375 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001376 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001377 main.cleanup()
1378 main.exit()
1379
kelvin-onlabd3b64892015-01-20 13:26:24 -08001380 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
1381 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001382 """
andrewonlab87852b02014-11-19 18:44:19 -05001383 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001384 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001385 a specific point-to-point intent definition
1386 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001387 * dpidSrc: specify source dpid
1388 * dpidDst: specify destination dpid
1389 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001390 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001391 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001392 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001393 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001394 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001395 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001396 """
andrewonlab87852b02014-11-19 18:44:19 -05001397 try:
kelvin8ec71442015-01-15 16:57:00 -08001398 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001399 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1400 str( numIntents )
1401 if numMult:
1402 cmd += " " + str( numMult )
1403 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001404 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001405 if appId:
1406 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001407 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001408 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1409 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001410 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001411 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001412 main.log.info( handle )
1413 # Split result by newline
1414 newline = handle.split( "\r\r\n" )
1415 # Ignore the first object of list, which is empty
1416 newline = newline[ 1: ]
1417 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001418 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001419 result = result.split( ": " )
1420 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001421 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1422 main.log.info( latResult )
1423 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001424 else:
1425 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001426 except TypeError:
1427 main.log.exception( self.name + ": Object not as expected" )
1428 return None
andrewonlab87852b02014-11-19 18:44:19 -05001429 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001430 main.log.error( self.name + ": EOF exception found" )
1431 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001432 main.cleanup()
1433 main.exit()
1434 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001435 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001436 main.cleanup()
1437 main.exit()
1438
kelvin-onlabd3b64892015-01-20 13:26:24 -08001439 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001440 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001441 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001442 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001443 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001444 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001445 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001446 if jsonFormat:
1447 cmdStr = "intents-events-metrics -j"
1448 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001449 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001450 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1451 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001452 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001453 cmdStr = "intents-events-metrics"
1454 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001455 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001456 except TypeError:
1457 main.log.exception( self.name + ": Object not as expected" )
1458 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001459 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001460 main.log.error( self.name + ": EOF exception found" )
1461 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001462 main.cleanup()
1463 main.exit()
1464 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001465 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001466 main.cleanup()
1467 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001468
kelvin-onlabd3b64892015-01-20 13:26:24 -08001469 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001470 """
1471 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001472 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001473 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001474 """
andrewonlab867212a2014-10-22 20:13:38 -04001475 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001476 if jsonFormat:
1477 cmdStr = "topology-events-metrics -j"
1478 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001479 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001480 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1481 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001482 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001483 cmdStr = "topology-events-metrics"
1484 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001485 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001486 except TypeError:
1487 main.log.exception( self.name + ": Object not as expected" )
1488 return None
andrewonlab867212a2014-10-22 20:13:38 -04001489 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001490 main.log.error( self.name + ": EOF exception found" )
1491 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001492 main.cleanup()
1493 main.exit()
1494 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001495 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001496 main.cleanup()
1497 main.exit()
1498
kelvin8ec71442015-01-15 16:57:00 -08001499 # Wrapper functions ****************
1500 # Wrapper functions use existing driver
1501 # functions and extends their use case.
1502 # For example, we may use the output of
1503 # a normal driver function, and parse it
1504 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001505
kelvin-onlabd3b64892015-01-20 13:26:24 -08001506 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001507 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001508 Description:
1509 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001510 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001511 try:
kelvin8ec71442015-01-15 16:57:00 -08001512 # Obtain output of intents function
Jon Halla5cb6172015-02-23 09:28:28 -08001513 intentsStr = self.intents(jsonFormat=False)
kelvin-onlabd3b64892015-01-20 13:26:24 -08001514 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001515
kelvin8ec71442015-01-15 16:57:00 -08001516 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001517 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1518 for intents in intentsList:
Jon Halla5cb6172015-02-23 09:28:28 -08001519 match = re.search('id=0x([\da-f]+),', intents)
1520 if match:
1521 tmpId = match.group()[3:-1]
1522 intentIdList.append( tmpId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001523 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001524
Jon Halld4d4b372015-01-28 16:02:41 -08001525 except TypeError:
1526 main.log.exception( self.name + ": Object not as expected" )
1527 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001528 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001529 main.log.error( self.name + ": EOF exception found" )
1530 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001531 main.cleanup()
1532 main.exit()
1533 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001534 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001535 main.cleanup()
1536 main.exit()
1537
kelvin-onlabd3b64892015-01-20 13:26:24 -08001538 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001539 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001540 Use 'devices' function to obtain list of all devices
1541 and parse the result to obtain a list of all device
1542 id's. Returns this list. Returns empty list if no
1543 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001544 List is ordered sequentially
1545
andrewonlab3e15ead2014-10-15 14:21:34 -04001546 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001547 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001548 the ids. By obtaining the list of device ids on the fly,
1549 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001550 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001551 try:
kelvin8ec71442015-01-15 16:57:00 -08001552 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001553 devicesStr = self.devices( jsonFormat=False )
1554 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001555
kelvin-onlabd3b64892015-01-20 13:26:24 -08001556 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001557 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001558 return idList
kelvin8ec71442015-01-15 16:57:00 -08001559
1560 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001561 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001562 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001563 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001564 # Split list further into arguments before and after string
1565 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001566 # append to idList
1567 for arg in tempList:
1568 idList.append( arg.split( "id=" )[ 1 ] )
1569 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001570
Jon Halld4d4b372015-01-28 16:02:41 -08001571 except TypeError:
1572 main.log.exception( self.name + ": Object not as expected" )
1573 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04001574 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001575 main.log.error( self.name + ": EOF exception found" )
1576 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001577 main.cleanup()
1578 main.exit()
1579 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001580 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001581 main.cleanup()
1582 main.exit()
1583
kelvin-onlabd3b64892015-01-20 13:26:24 -08001584 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001585 """
andrewonlab7c211572014-10-15 16:45:20 -04001586 Uses 'nodes' function to obtain list of all nodes
1587 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001588 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001589 Returns:
1590 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001591 """
andrewonlab7c211572014-10-15 16:45:20 -04001592 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001593 nodesStr = self.nodes()
1594 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001595
kelvin-onlabd3b64892015-01-20 13:26:24 -08001596 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001597 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001598 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001599
kelvin-onlabd3b64892015-01-20 13:26:24 -08001600 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001601 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001602
kelvin8ec71442015-01-15 16:57:00 -08001603 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001604 nodesList = nodesStr.split( "," )
1605 tempList = [ node for node in nodesList if "id=" in node ]
1606 for arg in tempList:
1607 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001608
kelvin-onlabd3b64892015-01-20 13:26:24 -08001609 return idList
kelvin8ec71442015-01-15 16:57:00 -08001610
Jon Halld4d4b372015-01-28 16:02:41 -08001611 except TypeError:
1612 main.log.exception( self.name + ": Object not as expected" )
1613 return None
andrewonlab7c211572014-10-15 16:45:20 -04001614 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001615 main.log.error( self.name + ": EOF exception found" )
1616 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001617 main.cleanup()
1618 main.exit()
1619 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001620 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001621 main.cleanup()
1622 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001623
kelvin-onlabd3b64892015-01-20 13:26:24 -08001624 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001625 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001626 Return the first device from the devices api whose 'id' contains 'dpid'
1627 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001628 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001629 import json
1630 try:
kelvin8ec71442015-01-15 16:57:00 -08001631 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001632 return None
1633 else:
kelvin8ec71442015-01-15 16:57:00 -08001634 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001635 rawDevices = self.devices()
1636 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001637 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001638 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001639 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1640 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001641 return device
1642 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001643 except TypeError:
1644 main.log.exception( self.name + ": Object not as expected" )
1645 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001646 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001647 main.log.error( self.name + ": EOF exception found" )
1648 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001649 main.cleanup()
1650 main.exit()
1651 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001652 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001653 main.cleanup()
1654 main.exit()
1655
kelvin-onlabd3b64892015-01-20 13:26:24 -08001656 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001657 """
1658 Checks the number of swithes & links that ONOS sees against the
1659 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001660 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001661
Jon Hall42db6dc2014-10-24 19:03:48 -04001662 Params: ip = ip used for the onos cli
1663 numoswitch = expected number of switches
1664 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001665 logLevel = level to log to. Currently accepts
1666 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001667
1668
kelvin-onlabd3b64892015-01-20 13:26:24 -08001669 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001670
kelvin8ec71442015-01-15 16:57:00 -08001671 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001672 main.FALSE if the numer of switches and links is incorrect,
1673 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001674 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001675 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001676 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001677 if topology == {}:
1678 return main.ERROR
1679 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001680 # Is the number of switches is what we expected
1681 devices = topology.get( 'devices', False )
1682 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001683 if devices == False or links == False:
1684 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001685 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001686 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001687 linkCheck = ( int( links ) == int( numolink ) )
1688 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001689 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001690 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001691 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001692 result = main.TRUE
1693 else:
1694 output = output + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001695 "The number of links and switches does not matc\
1696 h what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001697 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001698 output = output + "\n ONOS sees %i devices (%i expected) \
1699 and %i links (%i expected)" % (
1700 int( devices ), int( numoswitch ), int( links ),
1701 int( numolink ) )
1702 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001703 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001704 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001705 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001706 else:
kelvin8ec71442015-01-15 16:57:00 -08001707 main.log.info( output )
1708 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001709 except TypeError:
1710 main.log.exception( self.name + ": Object not as expected" )
1711 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001712 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001713 main.log.error( self.name + ": EOF exception found" )
1714 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001715 main.cleanup()
1716 main.exit()
1717 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001718 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001719 main.cleanup()
1720 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001721
kelvin-onlabd3b64892015-01-20 13:26:24 -08001722 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001723 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001724 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001725 deviceId must be the id of a device as seen in the onos devices command
1726 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001727 role must be either master, standby, or none
1728
Jon Halle3f39ff2015-01-13 11:50:53 -08001729 Returns:
1730 main.TRUE or main.FALSE based on argument verification and
1731 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001732 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001733 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001734 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001735 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001736 cmdStr = "device-role " +\
1737 str( deviceId ) + " " +\
1738 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001739 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001740 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001741 if re.search( "Error", handle ):
1742 # end color output to escape any colours
1743 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001744 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001745 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001746 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001747 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001748 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001749 main.log.error( "Invalid 'role' given to device_role(). " +
1750 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001751 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001752 except TypeError:
1753 main.log.exception( self.name + ": Object not as expected" )
1754 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001755 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 )
Jon Hall1c9e8732014-10-27 19:29:27 -04001758 main.cleanup()
1759 main.exit()
1760 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001761 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001762 main.cleanup()
1763 main.exit()
1764
kelvin-onlabd3b64892015-01-20 13:26:24 -08001765 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001766 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001767 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001768 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001769 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001770 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001771 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001772 if jsonFormat:
1773 cmdStr = "clusters -j"
1774 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001775 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001776 handle variable here contains some ANSI escape color code
1777 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001778 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001779 function. The repr( handle ) output when printed shows the ANSI
1780 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001781 variable is actually repr( somestring ) and json.loads would
1782 fail with the escape sequence. So we take off that escape
1783 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001784
kelvin-onlabd3b64892015-01-20 13:26:24 -08001785 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1786 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001787 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001788 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1789 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001790 return handle1
1791 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001792 cmdStr = "clusters"
1793 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001794 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001795 except TypeError:
1796 main.log.exception( self.name + ": Object not as expected" )
1797 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001798 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001799 main.log.error( self.name + ": EOF exception found" )
1800 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001801 main.cleanup()
1802 main.exit()
1803 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001804 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001805 main.cleanup()
1806 main.exit()
1807
kelvin-onlabd3b64892015-01-20 13:26:24 -08001808 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001809 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001810 CLI command to get the current leader for the Election test application
1811 NOTE: Requires installation of the onos-app-election feature
1812 Returns: Node IP of the leader if one exists
1813 None if none exists
1814 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001815 """
Jon Hall94fd0472014-12-08 11:52:42 -08001816 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001817 cmdStr = "election-test-leader"
1818 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001819 # Leader
1820 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001821 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001822 nodeSearch = re.search( leaderPattern, response )
1823 if nodeSearch:
1824 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001825 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001826 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001827 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001828 # no leader
1829 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001830 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001831 nullSearch = re.search( nullPattern, response )
1832 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001833 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001834 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001835 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001836 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001837 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001838 if re.search( errorPattern, response ):
1839 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001840 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001841 return main.FALSE
1842 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001843 main.log.error( "Error in election_test_leader: " +
1844 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001845 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001846 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001847 except TypeError:
1848 main.log.exception( self.name + ": Object not as expected" )
1849 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001850 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001851 main.log.error( self.name + ": EOF exception found" )
1852 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001853 main.cleanup()
1854 main.exit()
1855 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001856 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001857 main.cleanup()
1858 main.exit()
1859
kelvin-onlabd3b64892015-01-20 13:26:24 -08001860 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001861 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001862 CLI command to run for leadership of the Election test application.
1863 NOTE: Requires installation of the onos-app-election feature
1864 Returns: Main.TRUE on success
1865 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001866 """
Jon Hall94fd0472014-12-08 11:52:42 -08001867 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001868 cmdStr = "election-test-run"
1869 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001870 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001871 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001872 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001873 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001874 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001875 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001876 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001877 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001878 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001879 errorPattern = "Command\snot\sfound"
1880 if re.search( errorPattern, response ):
1881 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001882 return main.FALSE
1883 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001884 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001885 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001886 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001887 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001888 except TypeError:
1889 main.log.exception( self.name + ": Object not as expected" )
1890 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001891 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001892 main.log.error( self.name + ": EOF exception found" )
1893 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001894 main.cleanup()
1895 main.exit()
1896 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001897 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001898 main.cleanup()
1899 main.exit()
1900
kelvin-onlabd3b64892015-01-20 13:26:24 -08001901 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08001902 """
Jon Hall94fd0472014-12-08 11:52:42 -08001903 * CLI command to withdraw the local node from leadership election for
1904 * the Election test application.
1905 #NOTE: Requires installation of the onos-app-election feature
1906 Returns: Main.TRUE on success
1907 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001908 """
Jon Hall94fd0472014-12-08 11:52:42 -08001909 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001910 cmdStr = "election-test-withdraw"
1911 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001912 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001913 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001914 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001915 if re.search( successPattern, response ):
1916 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001917 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001918 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001919 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001920 errorPattern = "Command\snot\sfound"
1921 if re.search( errorPattern, response ):
1922 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001923 return main.FALSE
1924 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001925 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001926 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001927 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001928 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001929 except TypeError:
1930 main.log.exception( self.name + ": Object not as expected" )
1931 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001932 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001933 main.log.error( self.name + ": EOF exception found" )
1934 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001935 main.cleanup()
1936 main.exit()
1937 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001938 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001939 main.cleanup()
1940 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001941
kelvin8ec71442015-01-15 16:57:00 -08001942 def getDevicePortsEnabledCount( self, dpid ):
1943 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001944 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001945 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001946 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001947 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001948 cmdStr = "onos:ports -e " + dpid + " | wc -l"
1949 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001950 if re.search( "No such device", output ):
1951 main.log.error( "Error in getting ports" )
1952 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001953 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001954 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001955 except TypeError:
1956 main.log.exception( self.name + ": Object not as expected" )
1957 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001958 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001959 main.log.error( self.name + ": EOF exception found" )
1960 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001961 main.cleanup()
1962 main.exit()
1963 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001964 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001965 main.cleanup()
1966 main.exit()
1967
kelvin8ec71442015-01-15 16:57:00 -08001968 def getDeviceLinksActiveCount( self, dpid ):
1969 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001970 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001971 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001972 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001973 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001974 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
1975 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001976 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001977 main.log.error( "Error in getting ports " )
1978 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001979 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001980 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001981 except TypeError:
1982 main.log.exception( self.name + ": Object not as expected" )
1983 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001984 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001985 main.log.error( self.name + ": EOF exception found" )
1986 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001987 main.cleanup()
1988 main.exit()
1989 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001990 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001991 main.cleanup()
1992 main.exit()
1993
kelvin8ec71442015-01-15 16:57:00 -08001994 def getAllIntentIds( self ):
1995 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001996 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08001997 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001998 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001999 cmdStr = "onos:intents | grep id="
2000 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002001 if re.search( "Error", output ):
2002 main.log.error( "Error in getting ports" )
2003 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002004 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002005 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002006 except TypeError:
2007 main.log.exception( self.name + ": Object not as expected" )
2008 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002009 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002010 main.log.error( self.name + ": EOF exception found" )
2011 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002012 main.cleanup()
2013 main.exit()
2014 except:
Jon Halld4d4b372015-01-28 16:02:41 -08002015 main.log.exception( self.name + ": Uncaught exception!" )
2016 main.cleanup()
2017 main.exit()
2018
2019 def testExceptions( self, obj ):
2020 """
2021 Test exception logging
2022 """
2023 # FIXME: Remove this before you commit
2024
2025 try:
2026 return obj[ 'dedf' ]
2027 except TypeError:
2028 main.log.exception( self.name + ": Object not as expected" )
2029 return None
2030 except pexpect.EOF:
2031 main.log.error( self.name + ": EOF exception found" )
2032 main.log.error( self.name + ": " + self.handle.before )
2033 main.cleanup()
2034 main.exit()
2035 except:
2036 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002037 main.cleanup()
2038 main.exit()