blob: e79cf1b153e02e4b011e2416856c4cf1fd93673d [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 """
andrewonlab95ce8322014-10-13 14:12:04 -040081 response = ''
82 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" )
97 return None
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
kelvin8ec71442015-01-15 16:57:00 -0800151 self.handle.expect( "ONOS_CELL=" + str( cellname ) )
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.
kelvin8ec71442015-01-15 16:57:00 -0800925 """
andrewonlabe6745342014-10-17 14:29:13 -0400926 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800927 cmdStr = "add-host-intent " + str( hostIdOne ) +\
928 " " + str( hostIdTwo )
929 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800930 if re.search( "Error", handle ):
931 main.log.error( "Error in adding Host intent" )
932 return handle
933 else:
934 main.log.info( "Host intent installed between " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800935 str( hostIdOne ) + " and " + str( hostIdTwo ) )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800936 return main.TRUE
937
Jon Halld4d4b372015-01-28 16:02:41 -0800938 except TypeError:
939 main.log.exception( self.name + ": Object not as expected" )
940 return None
andrewonlabe6745342014-10-17 14:29:13 -0400941 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800942 main.log.error( self.name + ": EOF exception found" )
943 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400944 main.cleanup()
945 main.exit()
946 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800947 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400948 main.cleanup()
949 main.exit()
950
kelvin-onlabd3b64892015-01-20 13:26:24 -0800951 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800952 """
andrewonlab7b31d232014-10-24 13:31:47 -0400953 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800954 * ingressDevice: device id of ingress device
955 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400956 Optional:
957 TODO: Still needs to be implemented via dev side
kelvin-onlab898a6c62015-01-16 14:13:53 -0800958 """
andrewonlab7b31d232014-10-24 13:31:47 -0400959 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800960 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
961 " " + str( egressDevice )
962 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800963 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800964 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400965 return handle
966 else:
967 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800968 except TypeError:
969 main.log.exception( self.name + ": Object not as expected" )
970 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400971 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800972 main.log.error( self.name + ": EOF exception found" )
973 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400974 main.cleanup()
975 main.exit()
976 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800977 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -0400978 main.cleanup()
979 main.exit()
980
kelvin-onlabd3b64892015-01-20 13:26:24 -0800981 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800982 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800983 ingressDevice,
984 egressDevice,
985 portIngress="",
986 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800987 ethType="",
988 ethSrc="",
989 ethDst="",
990 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800991 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800992 ipProto="",
993 ipSrc="",
994 ipDst="",
995 tcpSrc="",
996 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -0800997 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400998 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800999 * ingressDevice: device id of ingress device
1000 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001001 Optional:
1002 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001003 * ethSrc: specify ethSrc ( i.e. src mac addr )
1004 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001005 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001006 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001007 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001008 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001009 * ipSrc: specify ip source address
1010 * ipDst: specify ip destination address
1011 * tcpSrc: specify tcp source port
1012 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001013 Description:
kelvin8ec71442015-01-15 16:57:00 -08001014 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001015 specifying device id's and optional fields
1016
Jon Halle3f39ff2015-01-13 11:50:53 -08001017 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001018 options developers provide for point-to-point
1019 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001020 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001021 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001022 cmd = ""
1023
kelvin8ec71442015-01-15 16:57:00 -08001024 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001025 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001026 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001027 and not ipProto and not ipSrc and not ipDst \
1028 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001029 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001030
andrewonlab289e4b72014-10-21 21:24:18 -04001031 else:
andrewonlab36af3822014-11-18 17:48:18 -05001032 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001033
andrewonlab0c0a6772014-10-22 12:31:18 -04001034 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001035 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001036 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001037 cmd += " --ethSrc " + str( ethSrc )
1038 if ethDst:
1039 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001040 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001041 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001042 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001043 cmd += " --lambda "
1044 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001045 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001046 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001047 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001048 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001049 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001050 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001051 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001052 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001053 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001054
kelvin8ec71442015-01-15 16:57:00 -08001055 # Check whether the user appended the port
1056 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001057 if "/" in ingressDevice:
1058 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001059 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001060 if not portIngress:
kelvin8ec71442015-01-15 16:57:00 -08001061 main.log.error( "You must specify " +
1062 "the ingress port" )
1063 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -05001064 return main.FALSE
1065
kelvin8ec71442015-01-15 16:57:00 -08001066 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001067 str( ingressDevice ) + "/" +\
1068 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001069
kelvin-onlabd3b64892015-01-20 13:26:24 -08001070 if "/" in egressDevice:
1071 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001072 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001073 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001074 main.log.error( "You must specify " +
1075 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001076 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001077
kelvin8ec71442015-01-15 16:57:00 -08001078 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001079 str( egressDevice ) + "/" +\
1080 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001081
kelvin-onlab898a6c62015-01-16 14:13:53 -08001082 handle = self.sendline( cmd )
1083 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001084 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001085 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001086 else:
1087 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001088 except TypeError:
1089 main.log.exception( self.name + ": Object not as expected" )
1090 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001091 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001092 main.log.error( self.name + ": EOF exception found" )
1093 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001094 main.cleanup()
1095 main.exit()
1096 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001097 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001098 main.cleanup()
1099 main.exit()
1100
kelvin-onlabd3b64892015-01-20 13:26:24 -08001101 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001102 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001103 ingressDevice1,
1104 ingressDevice2,
1105 egressDevice,
1106 portIngress="",
1107 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001108 ethType="",
1109 ethSrc="",
1110 ethDst="",
1111 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001112 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001113 ipProto="",
1114 ipSrc="",
1115 ipDst="",
1116 tcpSrc="",
1117 tcpDst="",
1118 setEthSrc="",
1119 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001120 """
shahshreyad0c80432014-12-04 16:56:05 -08001121 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001122 This function assumes that there would be 2 ingress devices and
1123 one egress device. For more number of ingress devices, this
1124 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001125 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001126 * ingressDevice1: device id of ingress device1
1127 * ingressDevice2: device id of ingress device2
1128 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001129 Optional:
1130 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001131 * ethSrc: specify ethSrc ( i.e. src mac addr )
1132 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001133 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001134 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001135 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001136 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001137 * ipSrc: specify ip source address
1138 * ipDst: specify ip destination address
1139 * tcpSrc: specify tcp source port
1140 * tcpDst: specify tcp destination port
1141 * setEthSrc: action to Rewrite Source MAC Address
1142 * setEthDst: action to Rewrite Destination MAC Address
1143 Description:
kelvin8ec71442015-01-15 16:57:00 -08001144 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001145 specifying device id's and optional fields
1146
Jon Halle3f39ff2015-01-13 11:50:53 -08001147 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001148 options developers provide for multipointpoint-to-singlepoint
1149 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001150 """
shahshreyad0c80432014-12-04 16:56:05 -08001151 try:
1152 cmd = ""
1153
kelvin8ec71442015-01-15 16:57:00 -08001154 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001155 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001156 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001157 and not ipProto and not ipSrc and not ipDst\
1158 and not tcpSrc and not tcpDst and not setEthSrc\
1159 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001160 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001161
1162 else:
1163 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001164
shahshreyad0c80432014-12-04 16:56:05 -08001165 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001166 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001167 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001168 cmd += " --ethSrc " + str( ethSrc )
1169 if ethDst:
1170 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001171 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001172 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001173 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001174 cmd += " --lambda "
1175 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001176 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001177 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001178 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001179 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001180 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001181 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001182 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001183 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001184 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001185 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001186 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001187 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001188 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001189
kelvin8ec71442015-01-15 16:57:00 -08001190 # Check whether the user appended the port
1191 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001192 if "/" in ingressDevice1:
1193 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001194 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001195 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001196 main.log.error( "You must specify " +
1197 "the ingress port1" )
1198 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001199 return main.FALSE
1200
kelvin8ec71442015-01-15 16:57:00 -08001201 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001202 str( ingressDevice1 ) + "/" +\
1203 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001204
kelvin-onlabd3b64892015-01-20 13:26:24 -08001205 if "/" in ingressDevice2:
1206 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001207 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001208 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001209 main.log.error( "You must specify " +
1210 "the ingress port2" )
1211 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001212 return main.FALSE
1213
kelvin8ec71442015-01-15 16:57:00 -08001214 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001215 str( ingressDevice2 ) + "/" +\
1216 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001217
kelvin-onlabd3b64892015-01-20 13:26:24 -08001218 if "/" in egressDevice:
1219 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001220 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001221 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001222 main.log.error( "You must specify " +
1223 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001224 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001225
kelvin8ec71442015-01-15 16:57:00 -08001226 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001227 str( egressDevice ) + "/" +\
1228 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001229 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001230 handle = self.sendline( cmd )
1231 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001232 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001233 return self.handle
1234 else:
1235 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001236 except TypeError:
1237 main.log.exception( self.name + ": Object not as expected" )
1238 return None
shahshreyad0c80432014-12-04 16:56:05 -08001239 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001240 main.log.error( self.name + ": EOF exception found" )
1241 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001242 main.cleanup()
1243 main.exit()
1244 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001245 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001246 main.cleanup()
1247 main.exit()
1248
kelvin-onlabd3b64892015-01-20 13:26:24 -08001249 def removeIntent( self, intentId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001250 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001251 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001252
1253 Returns:
1254 main.False on error and
1255 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001256 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001257 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001258 cmdStr = "remove-intent " + str( intentId )
1259 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001260 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001261 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001262 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001263 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001264 # TODO: Should this be main.TRUE
1265 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001266 except TypeError:
1267 main.log.exception( self.name + ": Object not as expected" )
1268 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001269 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001270 main.log.error( self.name + ": EOF exception found" )
1271 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001272 main.cleanup()
1273 main.exit()
1274 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001275 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001276 main.cleanup()
1277 main.exit()
1278
kelvin-onlabd3b64892015-01-20 13:26:24 -08001279 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001280 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001281 NOTE: This method should be used after installing application:
1282 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001283 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001284 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001285 Description:
1286 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001287 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001288 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001289 if jsonFormat:
1290 cmdStr = "routes -j"
1291 handleTmp = self.sendline( cmdStr )
1292 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1293 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001294 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001295 cmdStr = "routes"
1296 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001297 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001298 except TypeError:
1299 main.log.exception( self.name + ": Object not as expected" )
1300 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001301 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001302 main.log.error( self.name + ": EOF exception found" )
1303 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001304 main.cleanup()
1305 main.exit()
1306 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001307 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001308 main.cleanup()
1309 main.exit()
1310
kelvin-onlabd3b64892015-01-20 13:26:24 -08001311 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001312 """
andrewonlab377693f2014-10-21 16:00:30 -04001313 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001314 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001315 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001316 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001317 """
andrewonlabe6745342014-10-17 14:29:13 -04001318 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001319 if jsonFormat:
1320 cmdStr = "intents -j"
1321 handle = self.sendline( cmdStr )
1322 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1323 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001324 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001325 cmdStr = "intents"
1326 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001327 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001328 except TypeError:
1329 main.log.exception( self.name + ": Object not as expected" )
1330 return None
andrewonlabe6745342014-10-17 14:29:13 -04001331 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001332 main.log.error( self.name + ": EOF exception found" )
1333 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001334 main.cleanup()
1335 main.exit()
1336 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001337 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001338 main.cleanup()
1339 main.exit()
1340
kelvin-onlabd3b64892015-01-20 13:26:24 -08001341 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001342 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001343 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001344 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001345 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001346 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001347 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001348 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001349 if jsonFormat:
1350 cmdStr = "flows -j"
1351 handle = self.sendline( cmdStr )
1352 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1353 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001354 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001355 cmdStr = "flows"
1356 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001357 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001358 main.log.error( self.name + ".flows() response: " +
1359 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001360 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001361 except TypeError:
1362 main.log.exception( self.name + ": Object not as expected" )
1363 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001364 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001365 main.log.error( self.name + ": EOF exception found" )
1366 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001367 main.cleanup()
1368 main.exit()
1369 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001370 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001371 main.cleanup()
1372 main.exit()
1373
kelvin-onlabd3b64892015-01-20 13:26:24 -08001374 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
1375 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001376 """
andrewonlab87852b02014-11-19 18:44:19 -05001377 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001378 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001379 a specific point-to-point intent definition
1380 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001381 * dpidSrc: specify source dpid
1382 * dpidDst: specify destination dpid
1383 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001384 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001385 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001386 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001387 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001388 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001389 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001390 """
andrewonlab87852b02014-11-19 18:44:19 -05001391 try:
kelvin8ec71442015-01-15 16:57:00 -08001392 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001393 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1394 str( numIntents )
1395 if numMult:
1396 cmd += " " + str( numMult )
1397 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001398 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001399 if appId:
1400 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001401 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001402 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1403 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001404 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001405 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001406 main.log.info( handle )
1407 # Split result by newline
1408 newline = handle.split( "\r\r\n" )
1409 # Ignore the first object of list, which is empty
1410 newline = newline[ 1: ]
1411 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001412 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001413 result = result.split( ": " )
1414 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001415 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1416 main.log.info( latResult )
1417 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001418 else:
1419 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001420 except TypeError:
1421 main.log.exception( self.name + ": Object not as expected" )
1422 return None
andrewonlab87852b02014-11-19 18:44:19 -05001423 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001424 main.log.error( self.name + ": EOF exception found" )
1425 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001426 main.cleanup()
1427 main.exit()
1428 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001429 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001430 main.cleanup()
1431 main.exit()
1432
kelvin-onlabd3b64892015-01-20 13:26:24 -08001433 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001434 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001435 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001436 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001437 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001438 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001439 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001440 if jsonFormat:
1441 cmdStr = "intents-events-metrics -j"
1442 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001443 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001444 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1445 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001446 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001447 cmdStr = "intents-events-metrics"
1448 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001449 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001450 except TypeError:
1451 main.log.exception( self.name + ": Object not as expected" )
1452 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001453 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001454 main.log.error( self.name + ": EOF exception found" )
1455 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001456 main.cleanup()
1457 main.exit()
1458 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001459 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001460 main.cleanup()
1461 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001462
kelvin-onlabd3b64892015-01-20 13:26:24 -08001463 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001464 """
1465 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001466 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001467 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001468 """
andrewonlab867212a2014-10-22 20:13:38 -04001469 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001470 if jsonFormat:
1471 cmdStr = "topology-events-metrics -j"
1472 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001473 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001474 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1475 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001476 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001477 cmdStr = "topology-events-metrics"
1478 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001479 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001480 except TypeError:
1481 main.log.exception( self.name + ": Object not as expected" )
1482 return None
andrewonlab867212a2014-10-22 20:13:38 -04001483 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001484 main.log.error( self.name + ": EOF exception found" )
1485 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001486 main.cleanup()
1487 main.exit()
1488 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001489 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001490 main.cleanup()
1491 main.exit()
1492
kelvin8ec71442015-01-15 16:57:00 -08001493 # Wrapper functions ****************
1494 # Wrapper functions use existing driver
1495 # functions and extends their use case.
1496 # For example, we may use the output of
1497 # a normal driver function, and parse it
1498 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001499
kelvin-onlabd3b64892015-01-20 13:26:24 -08001500 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001501 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001502 Description:
1503 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001504 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001505 try:
kelvin8ec71442015-01-15 16:57:00 -08001506 # Obtain output of intents function
kelvin-onlabd3b64892015-01-20 13:26:24 -08001507 intentsStr = self.intents()
1508 allIntentList = []
1509 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001510
kelvin8ec71442015-01-15 16:57:00 -08001511 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001512 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1513 for intents in intentsList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001514 if "onos>" in intents:
1515 continue
1516 elif "intents" in intents:
1517 continue
1518 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001519 lineList = intents.split( " " )
1520 allIntentList.append( lineList[ 0 ] )
kelvin8ec71442015-01-15 16:57:00 -08001521
kelvin-onlabd3b64892015-01-20 13:26:24 -08001522 allIntentList = allIntentList[ 1:-2 ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001523
kelvin-onlabd3b64892015-01-20 13:26:24 -08001524 for intents in allIntentList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001525 if not intents:
1526 continue
1527 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001528 intentIdList.append( intents )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001529
kelvin-onlabd3b64892015-01-20 13:26:24 -08001530 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001531
Jon Halld4d4b372015-01-28 16:02:41 -08001532 except TypeError:
1533 main.log.exception( self.name + ": Object not as expected" )
1534 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001535 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001536 main.log.error( self.name + ": EOF exception found" )
1537 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001538 main.cleanup()
1539 main.exit()
1540 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001541 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001542 main.cleanup()
1543 main.exit()
1544
kelvin-onlabd3b64892015-01-20 13:26:24 -08001545 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001546 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001547 Use 'devices' function to obtain list of all devices
1548 and parse the result to obtain a list of all device
1549 id's. Returns this list. Returns empty list if no
1550 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001551 List is ordered sequentially
1552
andrewonlab3e15ead2014-10-15 14:21:34 -04001553 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001554 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001555 the ids. By obtaining the list of device ids on the fly,
1556 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001557 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001558 try:
kelvin8ec71442015-01-15 16:57:00 -08001559 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001560 devicesStr = self.devices( jsonFormat=False )
1561 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001562
kelvin-onlabd3b64892015-01-20 13:26:24 -08001563 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001564 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001565 return idList
kelvin8ec71442015-01-15 16:57:00 -08001566
1567 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001568 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001569 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001570 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001571 # Split list further into arguments before and after string
1572 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001573 # append to idList
1574 for arg in tempList:
1575 idList.append( arg.split( "id=" )[ 1 ] )
1576 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001577
Jon Halld4d4b372015-01-28 16:02:41 -08001578 except TypeError:
1579 main.log.exception( self.name + ": Object not as expected" )
1580 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04001581 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001582 main.log.error( self.name + ": EOF exception found" )
1583 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001584 main.cleanup()
1585 main.exit()
1586 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001587 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001588 main.cleanup()
1589 main.exit()
1590
kelvin-onlabd3b64892015-01-20 13:26:24 -08001591 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001592 """
andrewonlab7c211572014-10-15 16:45:20 -04001593 Uses 'nodes' function to obtain list of all nodes
1594 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001595 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001596 Returns:
1597 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001598 """
andrewonlab7c211572014-10-15 16:45:20 -04001599 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001600 nodesStr = self.nodes()
1601 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001602
kelvin-onlabd3b64892015-01-20 13:26:24 -08001603 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001604 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001605 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001606
kelvin-onlabd3b64892015-01-20 13:26:24 -08001607 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001608 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001609
kelvin8ec71442015-01-15 16:57:00 -08001610 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001611 nodesList = nodesStr.split( "," )
1612 tempList = [ node for node in nodesList if "id=" in node ]
1613 for arg in tempList:
1614 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001615
kelvin-onlabd3b64892015-01-20 13:26:24 -08001616 return idList
kelvin8ec71442015-01-15 16:57:00 -08001617
Jon Halld4d4b372015-01-28 16:02:41 -08001618 except TypeError:
1619 main.log.exception( self.name + ": Object not as expected" )
1620 return None
andrewonlab7c211572014-10-15 16:45:20 -04001621 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001622 main.log.error( self.name + ": EOF exception found" )
1623 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001624 main.cleanup()
1625 main.exit()
1626 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001627 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001628 main.cleanup()
1629 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001630
kelvin-onlabd3b64892015-01-20 13:26:24 -08001631 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001632 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001633 Return the first device from the devices api whose 'id' contains 'dpid'
1634 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001635 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001636 import json
1637 try:
kelvin8ec71442015-01-15 16:57:00 -08001638 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001639 return None
1640 else:
kelvin8ec71442015-01-15 16:57:00 -08001641 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001642 rawDevices = self.devices()
1643 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001644 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001645 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001646 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1647 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001648 return device
1649 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001650 except TypeError:
1651 main.log.exception( self.name + ": Object not as expected" )
1652 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001653 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001654 main.log.error( self.name + ": EOF exception found" )
1655 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001656 main.cleanup()
1657 main.exit()
1658 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001659 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001660 main.cleanup()
1661 main.exit()
1662
kelvin-onlabd3b64892015-01-20 13:26:24 -08001663 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001664 """
1665 Checks the number of swithes & links that ONOS sees against the
1666 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001667 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001668
Jon Hall42db6dc2014-10-24 19:03:48 -04001669 Params: ip = ip used for the onos cli
1670 numoswitch = expected number of switches
1671 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001672 logLevel = level to log to. Currently accepts
1673 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001674
1675
kelvin-onlabd3b64892015-01-20 13:26:24 -08001676 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001677
kelvin8ec71442015-01-15 16:57:00 -08001678 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001679 main.FALSE if the numer of switches and links is incorrect,
1680 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001681 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001682 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001683 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001684 if topology == {}:
1685 return main.ERROR
1686 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001687 # Is the number of switches is what we expected
1688 devices = topology.get( 'devices', False )
1689 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001690 if devices == False or links == False:
1691 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001692 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001693 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001694 linkCheck = ( int( links ) == int( numolink ) )
1695 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001696 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001697 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001698 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001699 result = main.TRUE
1700 else:
1701 output = output + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001702 "The number of links and switches does not matc\
1703 h what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001704 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001705 output = output + "\n ONOS sees %i devices (%i expected) \
1706 and %i links (%i expected)" % (
1707 int( devices ), int( numoswitch ), int( links ),
1708 int( numolink ) )
1709 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001710 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001711 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001712 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001713 else:
kelvin8ec71442015-01-15 16:57:00 -08001714 main.log.info( output )
1715 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001716 except TypeError:
1717 main.log.exception( self.name + ": Object not as expected" )
1718 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001719 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001720 main.log.error( self.name + ": EOF exception found" )
1721 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001722 main.cleanup()
1723 main.exit()
1724 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001725 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001726 main.cleanup()
1727 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001728
kelvin-onlabd3b64892015-01-20 13:26:24 -08001729 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001730 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001731 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001732 deviceId must be the id of a device as seen in the onos devices command
1733 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001734 role must be either master, standby, or none
1735
Jon Halle3f39ff2015-01-13 11:50:53 -08001736 Returns:
1737 main.TRUE or main.FALSE based on argument verification and
1738 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001739 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001740 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001741 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001742 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001743 cmdStr = "device-role " +\
1744 str( deviceId ) + " " +\
1745 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001746 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001747 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001748 if re.search( "Error", handle ):
1749 # end color output to escape any colours
1750 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001751 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001752 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001753 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001754 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001755 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001756 main.log.error( "Invalid 'role' given to device_role(). " +
1757 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001758 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001759 except TypeError:
1760 main.log.exception( self.name + ": Object not as expected" )
1761 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001762 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001763 main.log.error( self.name + ": EOF exception found" )
1764 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001765 main.cleanup()
1766 main.exit()
1767 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001768 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001769 main.cleanup()
1770 main.exit()
1771
kelvin-onlabd3b64892015-01-20 13:26:24 -08001772 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001773 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001774 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001775 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001776 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001777 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001778 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001779 if jsonFormat:
1780 cmdStr = "clusters -j"
1781 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001782 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001783 handle variable here contains some ANSI escape color code
1784 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001785 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001786 function. The repr( handle ) output when printed shows the ANSI
1787 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001788 variable is actually repr( somestring ) and json.loads would
1789 fail with the escape sequence. So we take off that escape
1790 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001791
kelvin-onlabd3b64892015-01-20 13:26:24 -08001792 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1793 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001794 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001795 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1796 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001797 return handle1
1798 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001799 cmdStr = "clusters"
1800 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001801 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001802 except TypeError:
1803 main.log.exception( self.name + ": Object not as expected" )
1804 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001805 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001806 main.log.error( self.name + ": EOF exception found" )
1807 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001808 main.cleanup()
1809 main.exit()
1810 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001811 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001812 main.cleanup()
1813 main.exit()
1814
kelvin-onlabd3b64892015-01-20 13:26:24 -08001815 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001816 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001817 CLI command to get the current leader for the Election test application
1818 NOTE: Requires installation of the onos-app-election feature
1819 Returns: Node IP of the leader if one exists
1820 None if none exists
1821 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001822 """
Jon Hall94fd0472014-12-08 11:52:42 -08001823 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001824 cmdStr = "election-test-leader"
1825 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001826 # Leader
1827 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001828 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001829 nodeSearch = re.search( leaderPattern, response )
1830 if nodeSearch:
1831 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001832 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001833 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001834 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001835 # no leader
1836 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001837 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001838 nullSearch = re.search( nullPattern, response )
1839 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001840 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001841 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001842 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001843 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001844 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001845 if re.search( errorPattern, response ):
1846 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001847 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001848 return main.FALSE
1849 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001850 main.log.error( "Error in election_test_leader: " +
1851 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001852 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001853 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001854 except TypeError:
1855 main.log.exception( self.name + ": Object not as expected" )
1856 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001857 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001858 main.log.error( self.name + ": EOF exception found" )
1859 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001860 main.cleanup()
1861 main.exit()
1862 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001863 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001864 main.cleanup()
1865 main.exit()
1866
kelvin-onlabd3b64892015-01-20 13:26:24 -08001867 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001868 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001869 CLI command to run for leadership of the Election test application.
1870 NOTE: Requires installation of the onos-app-election feature
1871 Returns: Main.TRUE on success
1872 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001873 """
Jon Hall94fd0472014-12-08 11:52:42 -08001874 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001875 cmdStr = "election-test-run"
1876 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001877 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001878 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001879 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001880 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001881 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001882 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001883 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001884 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001885 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001886 errorPattern = "Command\snot\sfound"
1887 if re.search( errorPattern, response ):
1888 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001889 return main.FALSE
1890 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001891 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001892 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001893 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001894 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001895 except TypeError:
1896 main.log.exception( self.name + ": Object not as expected" )
1897 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001898 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001899 main.log.error( self.name + ": EOF exception found" )
1900 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001901 main.cleanup()
1902 main.exit()
1903 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001904 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001905 main.cleanup()
1906 main.exit()
1907
kelvin-onlabd3b64892015-01-20 13:26:24 -08001908 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08001909 """
Jon Hall94fd0472014-12-08 11:52:42 -08001910 * CLI command to withdraw the local node from leadership election for
1911 * the Election test application.
1912 #NOTE: Requires installation of the onos-app-election feature
1913 Returns: Main.TRUE on success
1914 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001915 """
Jon Hall94fd0472014-12-08 11:52:42 -08001916 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001917 cmdStr = "election-test-withdraw"
1918 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001919 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001920 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001921 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001922 if re.search( successPattern, response ):
1923 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001924 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001925 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001926 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001927 errorPattern = "Command\snot\sfound"
1928 if re.search( errorPattern, response ):
1929 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001930 return main.FALSE
1931 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001932 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001933 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001934 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001935 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001936 except TypeError:
1937 main.log.exception( self.name + ": Object not as expected" )
1938 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001939 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001940 main.log.error( self.name + ": EOF exception found" )
1941 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001942 main.cleanup()
1943 main.exit()
1944 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001945 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001946 main.cleanup()
1947 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001948
kelvin8ec71442015-01-15 16:57:00 -08001949 def getDevicePortsEnabledCount( self, dpid ):
1950 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001951 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001952 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001953 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001954 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001955 cmdStr = "onos:ports -e " + dpid + " | wc -l"
1956 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001957 if re.search( "No such device", output ):
1958 main.log.error( "Error in getting ports" )
1959 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001960 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001961 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001962 except TypeError:
1963 main.log.exception( self.name + ": Object not as expected" )
1964 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001965 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001966 main.log.error( self.name + ": EOF exception found" )
1967 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001968 main.cleanup()
1969 main.exit()
1970 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001971 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001972 main.cleanup()
1973 main.exit()
1974
kelvin8ec71442015-01-15 16:57:00 -08001975 def getDeviceLinksActiveCount( self, dpid ):
1976 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001977 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001978 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001979 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001980 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001981 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
1982 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001983 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001984 main.log.error( "Error in getting ports " )
1985 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001986 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001987 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001988 except TypeError:
1989 main.log.exception( self.name + ": Object not as expected" )
1990 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001991 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001992 main.log.error( self.name + ": EOF exception found" )
1993 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001994 main.cleanup()
1995 main.exit()
1996 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001997 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001998 main.cleanup()
1999 main.exit()
2000
kelvin8ec71442015-01-15 16:57:00 -08002001 def getAllIntentIds( self ):
2002 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002003 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002004 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002005 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002006 cmdStr = "onos:intents | grep id="
2007 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002008 if re.search( "Error", output ):
2009 main.log.error( "Error in getting ports" )
2010 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002011 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002012 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002013 except TypeError:
2014 main.log.exception( self.name + ": Object not as expected" )
2015 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002016 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002017 main.log.error( self.name + ": EOF exception found" )
2018 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002019 main.cleanup()
2020 main.exit()
2021 except:
Jon Halld4d4b372015-01-28 16:02:41 -08002022 main.log.exception( self.name + ": Uncaught exception!" )
2023 main.cleanup()
2024 main.exit()
2025
2026 def testExceptions( self, obj ):
2027 """
2028 Test exception logging
2029 """
2030 # FIXME: Remove this before you commit
2031
2032 try:
2033 return obj[ 'dedf' ]
2034 except TypeError:
2035 main.log.exception( self.name + ": Object not as expected" )
2036 return None
2037 except pexpect.EOF:
2038 main.log.error( self.name + ": EOF exception found" )
2039 main.log.error( self.name + ": " + self.handle.before )
2040 main.cleanup()
2041 main.exit()
2042 except:
2043 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002044 main.cleanup()
2045 main.exit()