blob: e88aab2ebd2932170db35f4dd492c390dfa89a49 [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
kelvin8ec71442015-01-15 16:57:00 -08003"""
andrewonlab95ce8322014-10-13 14:12:04 -04004This driver enters the onos> prompt to issue commands.
5
kelvin8ec71442015-01-15 16:57:00 -08006Please follow the coding style demonstrated by existing
andrewonlab95ce8322014-10-13 14:12:04 -04007functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040014shreya@onlab.us
andrewonlab95ce8322014-10-13 14:12:04 -040015
16OCT 13 2014
17
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab95ce8322014-10-13 14:12:04 -040019import sys
andrewonlab95ce8322014-10-13 14:12:04 -040020import pexpect
21import re
kelvin8ec71442015-01-15 16:57:00 -080022sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040023from drivers.common.clidriver import CLI
24
andrewonlab95ce8322014-10-13 14:12:04 -040025
kelvin8ec71442015-01-15 16:57:00 -080026class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040027
kelvin8ec71442015-01-15 16:57:00 -080028 def __init__( self ):
29 """
30 Initialize client
31 """
32 super( CLI, self ).__init__()
33
34 def connect( self, **connectargs ):
35 """
andrewonlab95ce8322014-10-13 14:12:04 -040036 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080037 """
andrewonlab95ce8322014-10-13 14:12:04 -040038 try:
39 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080040 vars( self )[ key ] = connectargs[ key ]
andrewonlab95ce8322014-10-13 14:12:04 -040041 self.home = "~/ONOS"
42 for key in self.options:
43 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080044 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040045 break
kelvin-onlabd6634ac2015-01-29 14:23:10 -080046 if self.home == None or self.home == "":
47 self.home = "~/ONOS"
48
kelvin8ec71442015-01-15 16:57:00 -080049 self.name = self.options[ 'name' ]
50 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080051 user_name=self.user_name,
52 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080053 port=self.port,
54 pwd=self.pwd,
55 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040056
kelvin8ec71442015-01-15 16:57:00 -080057 self.handle.sendline( "cd " + self.home )
58 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040059 if self.handle:
60 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080061 else:
62 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040063 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080064 except TypeError:
65 main.log.exception( self.name + ": Object not as expected" )
66 return None
andrewonlab95ce8322014-10-13 14:12:04 -040067 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080068 main.log.error( self.name + ": EOF exception found" )
69 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040070 main.cleanup()
71 main.exit()
72 except:
Jon Halld4d4b372015-01-28 16:02:41 -080073 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -040074 main.cleanup()
75 main.exit()
76
kelvin8ec71442015-01-15 16:57:00 -080077 def disconnect( self ):
78 """
andrewonlab95ce8322014-10-13 14:12:04 -040079 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080080 """
Jon Halld61331b2015-02-17 16:35:47 -080081 response = main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -040082 try:
kelvin8ec71442015-01-15 16:57:00 -080083 self.handle.sendline( "" )
84 i = self.handle.expect( [ "onos>", "\$" ] )
Jon Hall7e5b9172014-10-22 12:32:47 -040085 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -080086 self.handle.sendline( "system:shutdown" )
87 self.handle.expect( "Confirm" )
88 self.handle.sendline( "yes" )
89 self.handle.expect( "\$" )
90 self.handle.sendline( "" )
91 self.handle.expect( "\$" )
92 self.handle.sendline( "exit" )
93 self.handle.expect( "closed" )
andrewonlabc2d05aa2014-10-13 16:51:10 -040094
Jon Halld4d4b372015-01-28 16:02:41 -080095 except TypeError:
96 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -080097 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -040098 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080099 main.log.error( self.name + ": EOF exception found" )
100 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400101 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800102 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400103 response = main.FALSE
104 return response
105
kelvin8ec71442015-01-15 16:57:00 -0800106 def logout( self ):
107 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500108 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800109 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500110 try:
kelvin8ec71442015-01-15 16:57:00 -0800111 self.handle.sendline( "" )
112 i = self.handle.expect( [
andrewonlab9627f432014-11-14 12:45:10 -0500113 "onos>",
kelvin8ec71442015-01-15 16:57:00 -0800114 "\$" ], timeout=10 )
andrewonlab9627f432014-11-14 12:45:10 -0500115 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800116 self.handle.sendline( "logout" )
117 self.handle.expect( "\$" )
andrewonlab9627f432014-11-14 12:45:10 -0500118 elif i == 1:
119 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800120
Jon Halld4d4b372015-01-28 16:02:41 -0800121 except TypeError:
122 main.log.exception( self.name + ": Object not as expected" )
123 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500124 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800125 main.log.error( self.name + ": eof exception found" )
126 main.log.error( self.name + ": " +
127 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500128 main.cleanup()
129 main.exit()
130 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800131 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500132 main.cleanup()
133 main.exit()
134
kelvin-onlabd3b64892015-01-20 13:26:24 -0800135 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800136 """
andrewonlab95ce8322014-10-13 14:12:04 -0400137 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800138
andrewonlab95ce8322014-10-13 14:12:04 -0400139 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800140 """
andrewonlab95ce8322014-10-13 14:12:04 -0400141 try:
142 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800143 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400144 main.cleanup()
145 main.exit()
146 else:
kelvin8ec71442015-01-15 16:57:00 -0800147 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800148 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800149 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400150 # and that this driver will have to change accordingly
Jon Hall1e03cb62015-02-19 12:07:12 -0800151 self.handle.expect( "ONOS_CELL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800152 handleBefore = self.handle.before
153 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800154 # Get the rest of the handle
155 self.handle.sendline( "" )
156 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800157 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400158
kelvin-onlabd3b64892015-01-20 13:26:24 -0800159 main.log.info( "Cell call returned: " + handleBefore +
160 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400161
162 return main.TRUE
163
Jon Halld4d4b372015-01-28 16:02:41 -0800164 except TypeError:
165 main.log.exception( self.name + ": Object not as expected" )
166 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400167 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800168 main.log.error( self.name + ": eof exception found" )
169 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400170 main.cleanup()
171 main.exit()
172 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800173 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400174 main.cleanup()
175 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800176
kelvin-onlabd3b64892015-01-20 13:26:24 -0800177 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800178 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 karafTimeout is an optional arugument. karafTimeout value passed
180 by user would be used to set the current karaf shell idle timeout.
181 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800182 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800183 Below is an example to start a session with 60 seconds idle timeout
184 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800185
Hari Krishna25d42f72015-01-05 15:08:28 -0800186 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800187 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800188
kelvin-onlabd3b64892015-01-20 13:26:24 -0800189 Note: karafTimeout is left as str so that this could be read
190 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800191 """
andrewonlab95ce8322014-10-13 14:12:04 -0400192 try:
kelvin8ec71442015-01-15 16:57:00 -0800193 self.handle.sendline( "" )
194 x = self.handle.expect( [
195 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500196
197 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800198 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500199 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400200
kelvin8ec71442015-01-15 16:57:00 -0800201 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800202 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800203 i = self.handle.expect( [
204 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800205 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400206
207 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800208 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800209 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800210 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800211 "config:property-set -p org.apache.karaf.shell\
212 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800213 karafTimeout )
214 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800216 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400217 return main.TRUE
218 else:
kelvin8ec71442015-01-15 16:57:00 -0800219 # If failed, send ctrl+c to process and try again
220 main.log.info( "Starting CLI failed. Retrying..." )
221 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800222 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800223 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
224 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400225 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800226 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800227 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800228 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800229 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800230 "config:property-set -p org.apache.karaf.shell\
231 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800232 karafTimeout )
233 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800234 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800235 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400236 return main.TRUE
237 else:
kelvin8ec71442015-01-15 16:57:00 -0800238 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800239 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400240 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400241
Jon Halld4d4b372015-01-28 16:02:41 -0800242 except TypeError:
243 main.log.exception( self.name + ": Object not as expected" )
244 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400245 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800246 main.log.error( self.name + ": EOF exception found" )
247 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400248 main.cleanup()
249 main.exit()
250 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800251 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400252 main.cleanup()
253 main.exit()
254
kelvin-onlab338f5512015-02-06 10:53:16 -0800255 def log( self, cmdStr , level = "" ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800256 """
257 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800258 returns main.TRUE on success
259 returns main.FALSE if Error occured
260 Available level: DEBUG, TRACE, INFO, WARN, ERROR
261 Level defaults to INFO
kelvin-onlab9f541032015-02-04 16:19:53 -0800262 """
263 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800264 lvlStr = ""
265 if level:
266 lvlStr = "--level=" + level
267
kelvin-onlab9f541032015-02-04 16:19:53 -0800268 self.handle.sendline( "" )
269 self.handle.expect( "onos>" )
kelvin-onlab338f5512015-02-06 10:53:16 -0800270 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
kelvin-onlab9f541032015-02-04 16:19:53 -0800271 self.handle.expect( "onos>" )
272
273 response = self.handle.before
274 if re.search( "Error", response ):
275 return main.FALSE
276 return main.TRUE
277
278 except pexpect.EOF:
279 main.log.error( self.name + ": EOF exception found" )
280 main.log.error( self.name + ": " + self.handle.before )
281 main.cleanup()
282 main.exit()
283 except:
284 main.log.info( self.name + " ::::::" )
285 main.log.error( traceback.print_exc() )
286 main.log.info( self.name + " ::::::" )
287 main.cleanup()
288 main.exit()
289
kelvin-onlabd3b64892015-01-20 13:26:24 -0800290 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800291 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800292 Send a completely user specified string to
293 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400294 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800295
andrewonlaba18f6bf2014-10-13 19:31:54 -0400296 Warning: There are no sanity checking to commands
297 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800298 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400299 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800300
301 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
302 self.log( logStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800303 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800304 self.handle.expect( "onos>" )
Jon Hallaea67aa2015-01-23 13:30:57 -0800305 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
306 + self.name + "." )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400307
308 handle = self.handle.before
Jon Hall7bdfc122015-01-23 11:45:32 -0800309 # Remove control strings from output
310 ansiEscape = re.compile( r'\x1b[^m]*m' )
311 handle = ansiEscape.sub( '', handle )
Jon Hall44225f82015-01-23 13:45:14 -0800312 #Remove extra return chars that get added
Jon Hallaea67aa2015-01-23 13:30:57 -0800313 handle = re.sub( r"\s\r", "", handle )
314 handle = handle.strip()
Jon Hall7bdfc122015-01-23 11:45:32 -0800315 # parse for just the output, remove the cmd from handle
316 output = handle.split( cmdStr, 1 )[1]
kelvin8ec71442015-01-15 16:57:00 -0800317
andrewonlaba18f6bf2014-10-13 19:31:54 -0400318
Jon Hall7bdfc122015-01-23 11:45:32 -0800319 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800320 except TypeError:
321 main.log.exception( self.name + ": Object not as expected" )
322 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400323 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800324 main.log.error( self.name + ": EOF exception found" )
325 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400326 main.cleanup()
327 main.exit()
328 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800329 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400330 main.cleanup()
331 main.exit()
332
kelvin8ec71442015-01-15 16:57:00 -0800333 # IMPORTANT NOTE:
334 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800335 # the cli command changing 'a:b' with 'aB'.
336 # Ex ) onos:topology > onosTopology
337 # onos:links > onosLinks
338 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800339
kelvin-onlabd3b64892015-01-20 13:26:24 -0800340 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800341 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400342 Adds a new cluster node by ID and address information.
343 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800344 * nodeId
345 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400346 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800347 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800348 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400349 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800350 cmdStr = "add-node " + str( nodeId ) + " " +\
351 str( ONOSIp ) + " " + str( tcpPort )
352 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800353 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800354 main.log.error( "Error in adding node" )
355 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800356 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400357 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800358 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400359 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800360 except TypeError:
361 main.log.exception( self.name + ": Object not as expected" )
362 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400363 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800364 main.log.error( self.name + ": EOF exception found" )
365 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400366 main.cleanup()
367 main.exit()
368 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800369 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400370 main.cleanup()
371 main.exit()
372
kelvin-onlabd3b64892015-01-20 13:26:24 -0800373 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800374 """
andrewonlab86dc3082014-10-13 18:18:38 -0400375 Removes a cluster by ID
376 Issues command: 'remove-node [<node-id>]'
377 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800378 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800379 """
andrewonlab86dc3082014-10-13 18:18:38 -0400380 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400381
kelvin-onlabd3b64892015-01-20 13:26:24 -0800382 cmdStr = "remove-node " + str( nodeId )
383 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800384 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400385
386 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800387
Jon Halld4d4b372015-01-28 16:02:41 -0800388 except TypeError:
389 main.log.exception( self.name + ": Object not as expected" )
390 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400391 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800392 main.log.error( self.name + ": EOF exception found" )
393 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400394 main.cleanup()
395 main.exit()
396 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800397 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400398 main.cleanup()
399 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400400
kelvin8ec71442015-01-15 16:57:00 -0800401 def nodes( self ):
402 """
andrewonlab7c211572014-10-15 16:45:20 -0400403 List the nodes currently visible
404 Issues command: 'nodes'
405 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800406 """
andrewonlab7c211572014-10-15 16:45:20 -0400407 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800408 cmdStr = "nodes"
409 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400410 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800411 except TypeError:
412 main.log.exception( self.name + ": Object not as expected" )
413 return None
andrewonlab7c211572014-10-15 16:45:20 -0400414 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800415 main.log.error( self.name + ": EOF exception found" )
416 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400417 main.cleanup()
418 main.exit()
419 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800420 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400421 main.cleanup()
422 main.exit()
423
kelvin8ec71442015-01-15 16:57:00 -0800424 def topology( self ):
425 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400426 Shows the current state of the topology
427 by issusing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800428 """
andrewonlab95ce8322014-10-13 14:12:04 -0400429 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800430 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800431 cmdStr = "onos:topology"
432 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800433 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400434 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800435 except TypeError:
436 main.log.exception( self.name + ": Object not as expected" )
437 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400438 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800439 main.log.error( self.name + ": EOF exception found" )
440 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400441 main.cleanup()
442 main.exit()
443 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800444 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400445 main.cleanup()
446 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800447
kelvin-onlabd3b64892015-01-20 13:26:24 -0800448 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800449 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800450 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400451 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800452 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400453 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800454 cmdStr = "feature:install " + str( featureStr )
455 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800456 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400457 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800458 except TypeError:
459 main.log.exception( self.name + ": Object not as expected" )
460 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400461 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800462 main.log.error( self.name + ": EOF exception found" )
463 main.log.error( self.name + ": " + self.handle.before )
464 main.log.report( "Failed to install feature" )
465 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400466 main.cleanup()
467 main.exit()
468 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800469 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800470 main.log.report( "Failed to install feature" )
471 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400472 main.cleanup()
473 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800474
kelvin-onlabd3b64892015-01-20 13:26:24 -0800475 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800476 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400477 Uninstalls a specified feature
478 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800479 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400480 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800481 cmdStr = "feature:uninstall " + str( featureStr )
482 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800483 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400484 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800485 except TypeError:
486 main.log.exception( self.name + ": Object not as expected" )
487 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400488 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800489 main.log.error( self.name + ": EOF exception found" )
490 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400491 main.cleanup()
492 main.exit()
493 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800494 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400495 main.cleanup()
496 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800497
kelvin-onlabd3b64892015-01-20 13:26:24 -0800498 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800499 """
Jon Hall7b02d952014-10-17 20:14:54 -0400500 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400501 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800502 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800503 """
andrewonlab86dc3082014-10-13 18:18:38 -0400504 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800505 if jsonFormat:
506 cmdStr = "devices -j"
507 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800508 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800509 handle variable here contains some ANSI escape color code
510 sequences at the end which are invisible in the print command
511 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800512 function. The repr( handle ) output when printed shows the
513 ANSI escape sequences. In json.loads( somestring ), this
514 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800515 json.loads would fail with the escape sequence. So we take off
516 that escape sequence using:
517
kelvin-onlabd3b64892015-01-20 13:26:24 -0800518 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
519 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800520 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800521 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
522 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400523 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400524 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800525 cmdStr = "devices"
526 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400527 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800528 except TypeError:
529 main.log.exception( self.name + ": Object not as expected" )
530 return None
andrewonlab7c211572014-10-15 16:45:20 -0400531 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800532 main.log.error( self.name + ": EOF exception found" )
533 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400534 main.cleanup()
535 main.exit()
536 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800537 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400538 main.cleanup()
539 main.exit()
540
kelvin-onlabd3b64892015-01-20 13:26:24 -0800541 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800542 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800543 This balances the devices across all controllers
544 by issuing command: 'onos> onos:balance-masters'
545 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800546 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800547 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800548 cmdStr = "onos:balance-masters"
549 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800550 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800551 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800552 except TypeError:
553 main.log.exception( self.name + ": Object not as expected" )
554 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800555 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800556 main.log.error( self.name + ": EOF exception found" )
557 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800558 main.cleanup()
559 main.exit()
560 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800561 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800562 main.cleanup()
563 main.exit()
564
kelvin-onlabd3b64892015-01-20 13:26:24 -0800565 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800566 """
Jon Halle8217482014-10-17 13:49:14 -0400567 Lists all core links
568 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800569 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800570 """
Jon Halle8217482014-10-17 13:49:14 -0400571 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800572 if jsonFormat:
573 cmdStr = "links -j"
574 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800575 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800576 handle variable here contains some ANSI escape color code
577 sequences at the end which are invisible in the print command
578 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800579 function. The repr( handle ) output when printed shows the ANSI
580 escape sequences. In json.loads( somestring ), this somestring
581 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800582 fail with the escape sequence. So we take off that escape
583 sequence using:
584
kelvin-onlabd3b64892015-01-20 13:26:24 -0800585 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
586 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800587 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800588 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
589 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400590 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400591 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800592 cmdStr = "links"
593 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400594 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800595 except TypeError:
596 main.log.exception( self.name + ": Object not as expected" )
597 return None
Jon Halle8217482014-10-17 13:49:14 -0400598 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800599 main.log.error( self.name + ": EOF exception found" )
600 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400601 main.cleanup()
602 main.exit()
603 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800604 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400605 main.cleanup()
606 main.exit()
607
kelvin-onlabd3b64892015-01-20 13:26:24 -0800608 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800609 """
Jon Halle8217482014-10-17 13:49:14 -0400610 Lists all ports
611 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800612 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800613 """
Jon Halle8217482014-10-17 13:49:14 -0400614 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800615 if jsonFormat:
616 cmdStr = "ports -j"
617 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800618 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800619 handle variable here contains some ANSI escape color code
620 sequences at the end which are invisible in the print command
621 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800622 function. The repr( handle ) output when printed shows the ANSI
623 escape sequences. In json.loads( somestring ), this somestring
624 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800625 fail with the escape sequence. So we take off that escape
626 sequence using the following commads:
627
kelvin-onlabd3b64892015-01-20 13:26:24 -0800628 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
629 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800630 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800631 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
632 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400633 return handle1
634
Jon Halle8217482014-10-17 13:49:14 -0400635 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800636 cmdStr = "ports"
637 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800638 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800639 except TypeError:
640 main.log.exception( self.name + ": Object not as expected" )
641 return None
Jon Halle8217482014-10-17 13:49:14 -0400642 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800643 main.log.error( self.name + ": EOF exception found" )
644 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400645 main.cleanup()
646 main.exit()
647 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800648 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400649 main.cleanup()
650 main.exit()
651
kelvin-onlabd3b64892015-01-20 13:26:24 -0800652 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800653 """
Jon Hall983a1702014-10-28 18:44:22 -0400654 Lists all devices and the controllers with roles assigned to them
655 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800656 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800657 """
andrewonlab7c211572014-10-15 16:45:20 -0400658 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800659 if jsonFormat:
660 cmdStr = "roles -j"
661 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800662 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800663 handle variable here contains some ANSI escape color code
664 sequences at the end which are invisible in the print command
665 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800666 function. The repr( handle ) output when printed shows the ANSI
667 escape sequences. In json.loads( somestring ), this somestring
668 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800669 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500670
Jon Halle3f39ff2015-01-13 11:50:53 -0800671 So we take off that escape sequence using the following
672 commads:
673
kelvin-onlabd3b64892015-01-20 13:26:24 -0800674 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
675 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800676 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800677 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
678 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400679 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400680
andrewonlab7c211572014-10-15 16:45:20 -0400681 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800682 cmdStr = "roles"
683 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800684 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800685 except TypeError:
686 main.log.exception( self.name + ": Object not as expected" )
687 return None
Jon Hall983a1702014-10-28 18:44:22 -0400688 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800689 main.log.error( self.name + ": EOF exception found" )
690 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400691 main.cleanup()
692 main.exit()
693 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800694 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400695 main.cleanup()
696 main.exit()
697
kelvin-onlabd3b64892015-01-20 13:26:24 -0800698 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800699 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800700 Given the a string containing the json representation of the "roles"
701 cli command and a partial or whole device id, returns a json object
702 containing the roles output for the first device whose id contains
703 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400704
705 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800706 A dict of the role assignments for the given device or
707 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800708 """
Jon Hall983a1702014-10-28 18:44:22 -0400709 try:
710 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800711 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400712 return None
713 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800714 rawRoles = self.roles()
715 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800716 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800717 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800718 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800719 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400720 return device
721 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800722 except TypeError:
723 main.log.exception( self.name + ": Object not as expected" )
724 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400725 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800726 main.log.error( self.name + ": EOF exception found" )
727 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400728 main.cleanup()
729 main.exit()
730 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800731 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400732 main.cleanup()
733 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800734
kelvin-onlabd3b64892015-01-20 13:26:24 -0800735 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800736 """
Jon Hall94fd0472014-12-08 11:52:42 -0800737 Iterates through each device and checks if there is a master assigned
738 Returns: main.TRUE if each device has a master
739 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800740 """
Jon Hall94fd0472014-12-08 11:52:42 -0800741 try:
742 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800743 rawRoles = self.roles()
744 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800745 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800746 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800747 # print device
748 if device[ 'master' ] == "none":
749 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800750 return main.FALSE
751 return main.TRUE
752
Jon Halld4d4b372015-01-28 16:02:41 -0800753 except TypeError:
754 main.log.exception( self.name + ": Object not as expected" )
755 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800756 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800757 main.log.error( self.name + ": EOF exception found" )
758 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800759 main.cleanup()
760 main.exit()
761 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800762 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800763 main.cleanup()
764 main.exit()
765
kelvin-onlabd3b64892015-01-20 13:26:24 -0800766 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800767 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400768 Returns string of paths, and the cost.
769 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800770 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400771 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800772 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
773 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800774 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800775 main.log.error( "Error in getting paths" )
776 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400777 else:
kelvin8ec71442015-01-15 16:57:00 -0800778 path = handle.split( ";" )[ 0 ]
779 cost = handle.split( ";" )[ 1 ]
780 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800781 except TypeError:
782 main.log.exception( self.name + ": Object not as expected" )
783 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400784 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800785 main.log.error( self.name + ": EOF exception found" )
786 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400787 main.cleanup()
788 main.exit()
789 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800790 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400791 main.cleanup()
792 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800793
kelvin-onlabd3b64892015-01-20 13:26:24 -0800794 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800795 """
Jon Hallffb386d2014-11-21 13:43:38 -0800796 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400797 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800798 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800799 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400800 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800801 if jsonFormat:
802 cmdStr = "hosts -j"
803 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800804 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800805 handle variable here contains some ANSI escape color code
806 sequences at the end which are invisible in the print command
807 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800808 function. The repr( handle ) output when printed shows the ANSI
809 escape sequences. In json.loads( somestring ), this somestring
810 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800811 fail with the escape sequence. So we take off that escape
812 sequence using:
813
kelvin-onlabd3b64892015-01-20 13:26:24 -0800814 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
815 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800816 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800817 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
818 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400819 return handle1
820 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800821 cmdStr = "hosts"
822 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400823 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800824 except TypeError:
825 main.log.exception( self.name + ": Object not as expected" )
826 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400827 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800828 main.log.error( self.name + ": EOF exception found" )
829 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400830 main.cleanup()
831 main.exit()
832 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800833 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400834 main.cleanup()
835 main.exit()
836
kelvin-onlabd3b64892015-01-20 13:26:24 -0800837 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800838 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400839 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800840
841 Note: mac must be a colon seperated mac address, but could be a
842 partial mac address
843
Jon Hall42db6dc2014-10-24 19:03:48 -0400844 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800845 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400846 import json
847 try:
kelvin8ec71442015-01-15 16:57:00 -0800848 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400849 return None
850 else:
851 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800852 rawHosts = self.hosts()
853 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800854 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800855 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800856 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800857 if not host:
858 pass
859 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400860 return host
861 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800862 except TypeError:
863 main.log.exception( self.name + ": Object not as expected" )
864 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400865 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800866 main.log.error( self.name + ": EOF exception found" )
867 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400868 main.cleanup()
869 main.exit()
870 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800871 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400872 main.cleanup()
873 main.exit()
874
kelvin-onlabd3b64892015-01-20 13:26:24 -0800875 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800876 """
877 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400878 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800879
andrewonlab3f0a4af2014-10-17 12:25:14 -0400880 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800881 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400882 IMPORTANT:
883 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800884 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400885 Furthermore, it assumes that value of VLAN is '-1'
886 Description:
kelvin8ec71442015-01-15 16:57:00 -0800887 Converts mininet hosts ( h1, h2, h3... ) into
888 ONOS format ( 00:00:00:00:00:01/-1 , ... )
889 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400890 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800891 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400892
kelvin-onlabd3b64892015-01-20 13:26:24 -0800893 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800894 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800895 hostHex = hex( int( host ) ).zfill( 12 )
896 hostHex = str( hostHex ).replace( 'x', '0' )
897 i = iter( str( hostHex ) )
898 hostHex = ":".join( a + b for a, b in zip( i, i ) )
899 hostHex = hostHex + "/-1"
900 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400901
kelvin-onlabd3b64892015-01-20 13:26:24 -0800902 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400903
Jon Halld4d4b372015-01-28 16:02:41 -0800904 except TypeError:
905 main.log.exception( self.name + ": Object not as expected" )
906 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400907 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800908 main.log.error( self.name + ": EOF exception found" )
909 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400910 main.cleanup()
911 main.exit()
912 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800913 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400914 main.cleanup()
915 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400916
kelvin-onlabd3b64892015-01-20 13:26:24 -0800917 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800918 """
andrewonlabe6745342014-10-17 14:29:13 -0400919 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800920 * hostIdOne: ONOS host id for host1
921 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400922 Description:
kelvin8ec71442015-01-15 16:57:00 -0800923 Adds a host-to-host intent ( bidrectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500924 specifying the two hosts.
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
Hari Krishnaccfb0d52015-02-19 09:38:29 -0800941
andrewonlabe6745342014-10-17 14:29:13 -0400942 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800943 main.log.error( self.name + ": EOF exception found" )
944 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400945 main.cleanup()
946 main.exit()
947 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800948 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400949 main.cleanup()
950 main.exit()
951
kelvin-onlabd3b64892015-01-20 13:26:24 -0800952 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800953 """
andrewonlab7b31d232014-10-24 13:31:47 -0400954 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800955 * ingressDevice: device id of ingress device
956 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400957 Optional:
958 TODO: Still needs to be implemented via dev side
kelvin-onlab898a6c62015-01-16 14:13:53 -0800959 """
andrewonlab7b31d232014-10-24 13:31:47 -0400960 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800961 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
962 " " + str( egressDevice )
963 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800964 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800965 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400966 return handle
967 else:
968 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800969 except TypeError:
970 main.log.exception( self.name + ": Object not as expected" )
971 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400972 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800973 main.log.error( self.name + ": EOF exception found" )
974 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400975 main.cleanup()
976 main.exit()
977 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800978 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -0400979 main.cleanup()
980 main.exit()
981
kelvin-onlabd3b64892015-01-20 13:26:24 -0800982 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800983 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800984 ingressDevice,
985 egressDevice,
986 portIngress="",
987 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800988 ethType="",
989 ethSrc="",
990 ethDst="",
991 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800992 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800993 ipProto="",
994 ipSrc="",
995 ipDst="",
996 tcpSrc="",
997 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -0800998 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400999 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001000 * ingressDevice: device id of ingress device
1001 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001002 Optional:
1003 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001004 * ethSrc: specify ethSrc ( i.e. src mac addr )
1005 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001006 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001007 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001008 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001009 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001010 * ipSrc: specify ip source address
1011 * ipDst: specify ip destination address
1012 * tcpSrc: specify tcp source port
1013 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001014 Description:
kelvin8ec71442015-01-15 16:57:00 -08001015 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001016 specifying device id's and optional fields
1017
Jon Halle3f39ff2015-01-13 11:50:53 -08001018 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001019 options developers provide for point-to-point
1020 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001021 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001022 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001023 cmd = ""
1024
kelvin8ec71442015-01-15 16:57:00 -08001025 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001026 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001027 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001028 and not ipProto and not ipSrc and not ipDst \
1029 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001030 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001031
andrewonlab289e4b72014-10-21 21:24:18 -04001032 else:
andrewonlab36af3822014-11-18 17:48:18 -05001033 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001034
andrewonlab0c0a6772014-10-22 12:31:18 -04001035 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001036 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001037 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001038 cmd += " --ethSrc " + str( ethSrc )
1039 if ethDst:
1040 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001041 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001042 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001043 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001044 cmd += " --lambda "
1045 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001046 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001047 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001048 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001049 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001050 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001051 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001052 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001053 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001054 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001055
kelvin8ec71442015-01-15 16:57:00 -08001056 # Check whether the user appended the port
1057 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001058 if "/" in ingressDevice:
1059 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001060 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001061 if not portIngress:
kelvin8ec71442015-01-15 16:57:00 -08001062 main.log.error( "You must specify " +
1063 "the ingress port" )
1064 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -05001065 return main.FALSE
1066
kelvin8ec71442015-01-15 16:57:00 -08001067 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001068 str( ingressDevice ) + "/" +\
1069 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001070
kelvin-onlabd3b64892015-01-20 13:26:24 -08001071 if "/" in egressDevice:
1072 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001073 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001074 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001075 main.log.error( "You must specify " +
1076 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001077 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001078
kelvin8ec71442015-01-15 16:57:00 -08001079 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001080 str( egressDevice ) + "/" +\
1081 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001082
kelvin-onlab898a6c62015-01-16 14:13:53 -08001083 handle = self.sendline( cmd )
1084 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001085 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001086 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001087 else:
1088 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001089 except TypeError:
1090 main.log.exception( self.name + ": Object not as expected" )
1091 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001092 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001093 main.log.error( self.name + ": EOF exception found" )
1094 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001095 main.cleanup()
1096 main.exit()
1097 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001098 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001099 main.cleanup()
1100 main.exit()
1101
kelvin-onlabd3b64892015-01-20 13:26:24 -08001102 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001103 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001104 ingressDevice1,
1105 ingressDevice2,
1106 egressDevice,
1107 portIngress="",
1108 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001109 ethType="",
1110 ethSrc="",
1111 ethDst="",
1112 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001113 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001114 ipProto="",
1115 ipSrc="",
1116 ipDst="",
1117 tcpSrc="",
1118 tcpDst="",
1119 setEthSrc="",
1120 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001121 """
shahshreyad0c80432014-12-04 16:56:05 -08001122 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001123 This function assumes that there would be 2 ingress devices and
1124 one egress device. For more number of ingress devices, this
1125 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001126 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001127 * ingressDevice1: device id of ingress device1
1128 * ingressDevice2: device id of ingress device2
1129 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001130 Optional:
1131 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001132 * ethSrc: specify ethSrc ( i.e. src mac addr )
1133 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001134 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001135 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001136 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001137 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001138 * ipSrc: specify ip source address
1139 * ipDst: specify ip destination address
1140 * tcpSrc: specify tcp source port
1141 * tcpDst: specify tcp destination port
1142 * setEthSrc: action to Rewrite Source MAC Address
1143 * setEthDst: action to Rewrite Destination MAC Address
1144 Description:
kelvin8ec71442015-01-15 16:57:00 -08001145 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001146 specifying device id's and optional fields
1147
Jon Halle3f39ff2015-01-13 11:50:53 -08001148 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001149 options developers provide for multipointpoint-to-singlepoint
1150 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001151 """
shahshreyad0c80432014-12-04 16:56:05 -08001152 try:
1153 cmd = ""
1154
kelvin8ec71442015-01-15 16:57:00 -08001155 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001156 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001157 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001158 and not ipProto and not ipSrc and not ipDst\
1159 and not tcpSrc and not tcpDst and not setEthSrc\
1160 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001161 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001162
1163 else:
1164 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001165
shahshreyad0c80432014-12-04 16:56:05 -08001166 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001167 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001168 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001169 cmd += " --ethSrc " + str( ethSrc )
1170 if ethDst:
1171 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001172 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001173 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001174 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001175 cmd += " --lambda "
1176 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001177 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001178 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001179 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001180 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001181 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001182 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001183 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001184 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001185 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001186 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001187 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001188 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001189 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001190
kelvin8ec71442015-01-15 16:57:00 -08001191 # Check whether the user appended the port
1192 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001193 if "/" in ingressDevice1:
1194 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001195 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001196 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001197 main.log.error( "You must specify " +
1198 "the ingress port1" )
1199 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001200 return main.FALSE
1201
kelvin8ec71442015-01-15 16:57:00 -08001202 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001203 str( ingressDevice1 ) + "/" +\
1204 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001205
kelvin-onlabd3b64892015-01-20 13:26:24 -08001206 if "/" in ingressDevice2:
1207 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001208 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001209 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001210 main.log.error( "You must specify " +
1211 "the ingress port2" )
1212 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001213 return main.FALSE
1214
kelvin8ec71442015-01-15 16:57:00 -08001215 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001216 str( ingressDevice2 ) + "/" +\
1217 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001218
kelvin-onlabd3b64892015-01-20 13:26:24 -08001219 if "/" in egressDevice:
1220 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001221 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001222 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001223 main.log.error( "You must specify " +
1224 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001225 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001226
kelvin8ec71442015-01-15 16:57:00 -08001227 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001228 str( egressDevice ) + "/" +\
1229 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001230 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001231 handle = self.sendline( cmd )
1232 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001233 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001234 return self.handle
1235 else:
1236 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001237 except TypeError:
1238 main.log.exception( self.name + ": Object not as expected" )
1239 return None
shahshreyad0c80432014-12-04 16:56:05 -08001240 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001241 main.log.error( self.name + ": EOF exception found" )
1242 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001243 main.cleanup()
1244 main.exit()
1245 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001246 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001247 main.cleanup()
1248 main.exit()
1249
kelvin-onlabd3b64892015-01-20 13:26:24 -08001250 def removeIntent( self, intentId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001251 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001252 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001253
1254 Returns:
1255 main.False on error and
1256 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001257 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001258 try:
shahshreyac033d022015-02-20 16:10:19 -08001259 cmdStr = "remove-intent org.onosproject.cli " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001260 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001261 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001262 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001263 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001264 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001265 # TODO: Should this be main.TRUE
1266 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001267 except TypeError:
1268 main.log.exception( self.name + ": Object not as expected" )
1269 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001270 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001271 main.log.error( self.name + ": EOF exception found" )
1272 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001273 main.cleanup()
1274 main.exit()
1275 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001276 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001277 main.cleanup()
1278 main.exit()
1279
kelvin-onlabd3b64892015-01-20 13:26:24 -08001280 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001281 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001282 NOTE: This method should be used after installing application:
1283 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001284 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001285 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001286 Description:
1287 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001288 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001289 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001290 if jsonFormat:
1291 cmdStr = "routes -j"
1292 handleTmp = self.sendline( cmdStr )
1293 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1294 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001295 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001296 cmdStr = "routes"
1297 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001298 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001299 except TypeError:
1300 main.log.exception( self.name + ": Object not as expected" )
1301 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001302 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001303 main.log.error( self.name + ": EOF exception found" )
1304 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001305 main.cleanup()
1306 main.exit()
1307 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001308 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001309 main.cleanup()
1310 main.exit()
1311
kelvin-onlabd3b64892015-01-20 13:26:24 -08001312 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001313 """
andrewonlab377693f2014-10-21 16:00:30 -04001314 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001315 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001316 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001317 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001318 """
andrewonlabe6745342014-10-17 14:29:13 -04001319 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001320 if jsonFormat:
1321 cmdStr = "intents -j"
1322 handle = self.sendline( cmdStr )
1323 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1324 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001325 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001326 cmdStr = "intents"
1327 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001328 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001329 except TypeError:
1330 main.log.exception( self.name + ": Object not as expected" )
1331 return None
andrewonlabe6745342014-10-17 14:29:13 -04001332 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001333 main.log.error( self.name + ": EOF exception found" )
1334 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001335 main.cleanup()
1336 main.exit()
1337 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001338 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001339 main.cleanup()
1340 main.exit()
1341
kelvin-onlabd3b64892015-01-20 13:26:24 -08001342 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001343 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001344 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001345 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001346 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001347 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001348 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001349 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001350 if jsonFormat:
1351 cmdStr = "flows -j"
1352 handle = self.sendline( cmdStr )
1353 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1354 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001355 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001356 cmdStr = "flows"
1357 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001358 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001359 main.log.error( self.name + ".flows() response: " +
1360 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001361 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001362 except TypeError:
1363 main.log.exception( self.name + ": Object not as expected" )
1364 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001365 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001366 main.log.error( self.name + ": EOF exception found" )
1367 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001368 main.cleanup()
1369 main.exit()
1370 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001371 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001372 main.cleanup()
1373 main.exit()
1374
kelvin-onlabd3b64892015-01-20 13:26:24 -08001375 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
1376 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001377 """
andrewonlab87852b02014-11-19 18:44:19 -05001378 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001379 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001380 a specific point-to-point intent definition
1381 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001382 * dpidSrc: specify source dpid
1383 * dpidDst: specify destination dpid
1384 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001385 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001386 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001387 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001388 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001389 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001390 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001391 """
andrewonlab87852b02014-11-19 18:44:19 -05001392 try:
kelvin8ec71442015-01-15 16:57:00 -08001393 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001394 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1395 str( numIntents )
1396 if numMult:
1397 cmd += " " + str( numMult )
1398 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001399 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001400 if appId:
1401 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001402 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001403 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1404 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001405 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001406 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001407 main.log.info( handle )
1408 # Split result by newline
1409 newline = handle.split( "\r\r\n" )
1410 # Ignore the first object of list, which is empty
1411 newline = newline[ 1: ]
1412 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001413 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001414 result = result.split( ": " )
1415 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001416 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1417 main.log.info( latResult )
1418 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001419 else:
1420 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001421 except TypeError:
1422 main.log.exception( self.name + ": Object not as expected" )
1423 return None
andrewonlab87852b02014-11-19 18:44:19 -05001424 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001425 main.log.error( self.name + ": EOF exception found" )
1426 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001427 main.cleanup()
1428 main.exit()
1429 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001430 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001431 main.cleanup()
1432 main.exit()
1433
kelvin-onlabd3b64892015-01-20 13:26:24 -08001434 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001435 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001436 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001437 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001438 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001439 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001440 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001441 if jsonFormat:
1442 cmdStr = "intents-events-metrics -j"
1443 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001444 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001445 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1446 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001447 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001448 cmdStr = "intents-events-metrics"
1449 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001450 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001451 except TypeError:
1452 main.log.exception( self.name + ": Object not as expected" )
1453 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001454 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001455 main.log.error( self.name + ": EOF exception found" )
1456 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001457 main.cleanup()
1458 main.exit()
1459 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001460 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001461 main.cleanup()
1462 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001463
kelvin-onlabd3b64892015-01-20 13:26:24 -08001464 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001465 """
1466 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001467 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001468 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001469 """
andrewonlab867212a2014-10-22 20:13:38 -04001470 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001471 if jsonFormat:
1472 cmdStr = "topology-events-metrics -j"
1473 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001474 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001475 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1476 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001477 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001478 cmdStr = "topology-events-metrics"
1479 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001480 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001481 except TypeError:
1482 main.log.exception( self.name + ": Object not as expected" )
1483 return None
andrewonlab867212a2014-10-22 20:13:38 -04001484 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001485 main.log.error( self.name + ": EOF exception found" )
1486 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001487 main.cleanup()
1488 main.exit()
1489 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001490 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001491 main.cleanup()
1492 main.exit()
1493
kelvin8ec71442015-01-15 16:57:00 -08001494 # Wrapper functions ****************
1495 # Wrapper functions use existing driver
1496 # functions and extends their use case.
1497 # For example, we may use the output of
1498 # a normal driver function, and parse it
1499 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001500
kelvin-onlabd3b64892015-01-20 13:26:24 -08001501 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001502 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001503 Description:
1504 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001505 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001506 try:
kelvin8ec71442015-01-15 16:57:00 -08001507 # Obtain output of intents function
kelvin-onlabd3b64892015-01-20 13:26:24 -08001508 intentsStr = self.intents()
1509 allIntentList = []
1510 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001511
kelvin8ec71442015-01-15 16:57:00 -08001512 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001513 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1514 for intents in intentsList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001515 if "onos>" in intents:
1516 continue
1517 elif "intents" in intents:
1518 continue
1519 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001520 lineList = intents.split( " " )
1521 allIntentList.append( lineList[ 0 ] )
kelvin8ec71442015-01-15 16:57:00 -08001522
kelvin-onlabd3b64892015-01-20 13:26:24 -08001523 allIntentList = allIntentList[ 1:-2 ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001524
kelvin-onlabd3b64892015-01-20 13:26:24 -08001525 for intents in allIntentList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001526 if not intents:
1527 continue
1528 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001529 intentIdList.append( intents )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001530
kelvin-onlabd3b64892015-01-20 13:26:24 -08001531 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001532
Jon Halld4d4b372015-01-28 16:02:41 -08001533 except TypeError:
1534 main.log.exception( self.name + ": Object not as expected" )
1535 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001536 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001537 main.log.error( self.name + ": EOF exception found" )
1538 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001539 main.cleanup()
1540 main.exit()
1541 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001542 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001543 main.cleanup()
1544 main.exit()
1545
kelvin-onlabd3b64892015-01-20 13:26:24 -08001546 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001547 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001548 Use 'devices' function to obtain list of all devices
1549 and parse the result to obtain a list of all device
1550 id's. Returns this list. Returns empty list if no
1551 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001552 List is ordered sequentially
1553
andrewonlab3e15ead2014-10-15 14:21:34 -04001554 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001555 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001556 the ids. By obtaining the list of device ids on the fly,
1557 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001558 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001559 try:
kelvin8ec71442015-01-15 16:57:00 -08001560 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001561 devicesStr = self.devices( jsonFormat=False )
1562 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001563
kelvin-onlabd3b64892015-01-20 13:26:24 -08001564 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001565 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001566 return idList
kelvin8ec71442015-01-15 16:57:00 -08001567
1568 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001569 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001570 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001571 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001572 # Split list further into arguments before and after string
1573 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001574 # append to idList
1575 for arg in tempList:
1576 idList.append( arg.split( "id=" )[ 1 ] )
1577 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001578
Jon Halld4d4b372015-01-28 16:02:41 -08001579 except TypeError:
1580 main.log.exception( self.name + ": Object not as expected" )
1581 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04001582 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001583 main.log.error( self.name + ": EOF exception found" )
1584 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001585 main.cleanup()
1586 main.exit()
1587 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001588 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001589 main.cleanup()
1590 main.exit()
1591
kelvin-onlabd3b64892015-01-20 13:26:24 -08001592 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001593 """
andrewonlab7c211572014-10-15 16:45:20 -04001594 Uses 'nodes' function to obtain list of all nodes
1595 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001596 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001597 Returns:
1598 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001599 """
andrewonlab7c211572014-10-15 16:45:20 -04001600 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001601 nodesStr = self.nodes()
1602 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001603
kelvin-onlabd3b64892015-01-20 13:26:24 -08001604 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001605 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001606 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001607
kelvin-onlabd3b64892015-01-20 13:26:24 -08001608 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001609 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001610
kelvin8ec71442015-01-15 16:57:00 -08001611 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001612 nodesList = nodesStr.split( "," )
1613 tempList = [ node for node in nodesList if "id=" in node ]
1614 for arg in tempList:
1615 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001616
kelvin-onlabd3b64892015-01-20 13:26:24 -08001617 return idList
kelvin8ec71442015-01-15 16:57:00 -08001618
Jon Halld4d4b372015-01-28 16:02:41 -08001619 except TypeError:
1620 main.log.exception( self.name + ": Object not as expected" )
1621 return None
andrewonlab7c211572014-10-15 16:45:20 -04001622 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001623 main.log.error( self.name + ": EOF exception found" )
1624 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001625 main.cleanup()
1626 main.exit()
1627 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001628 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001629 main.cleanup()
1630 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001631
kelvin-onlabd3b64892015-01-20 13:26:24 -08001632 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001633 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001634 Return the first device from the devices api whose 'id' contains 'dpid'
1635 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001636 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001637 import json
1638 try:
kelvin8ec71442015-01-15 16:57:00 -08001639 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001640 return None
1641 else:
kelvin8ec71442015-01-15 16:57:00 -08001642 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001643 rawDevices = self.devices()
1644 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001645 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001646 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001647 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1648 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001649 return device
1650 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001651 except TypeError:
1652 main.log.exception( self.name + ": Object not as expected" )
1653 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001654 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001655 main.log.error( self.name + ": EOF exception found" )
1656 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001657 main.cleanup()
1658 main.exit()
1659 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001660 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001661 main.cleanup()
1662 main.exit()
1663
kelvin-onlabd3b64892015-01-20 13:26:24 -08001664 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001665 """
1666 Checks the number of swithes & links that ONOS sees against the
1667 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001668 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001669
Jon Hall42db6dc2014-10-24 19:03:48 -04001670 Params: ip = ip used for the onos cli
1671 numoswitch = expected number of switches
1672 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001673 logLevel = level to log to. Currently accepts
1674 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001675
1676
kelvin-onlabd3b64892015-01-20 13:26:24 -08001677 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001678
kelvin8ec71442015-01-15 16:57:00 -08001679 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001680 main.FALSE if the numer of switches and links is incorrect,
1681 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001682 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001683 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001684 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001685 if topology == {}:
1686 return main.ERROR
1687 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001688 # Is the number of switches is what we expected
1689 devices = topology.get( 'devices', False )
1690 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001691 if devices == False or links == False:
1692 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001693 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001694 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001695 linkCheck = ( int( links ) == int( numolink ) )
1696 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001697 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001698 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001699 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001700 result = main.TRUE
1701 else:
1702 output = output + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001703 "The number of links and switches does not matc\
1704 h what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001705 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001706 output = output + "\n ONOS sees %i devices (%i expected) \
1707 and %i links (%i expected)" % (
1708 int( devices ), int( numoswitch ), int( links ),
1709 int( numolink ) )
1710 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001711 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001712 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001713 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001714 else:
kelvin8ec71442015-01-15 16:57:00 -08001715 main.log.info( output )
1716 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001717 except TypeError:
1718 main.log.exception( self.name + ": Object not as expected" )
1719 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001720 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001721 main.log.error( self.name + ": EOF exception found" )
1722 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001723 main.cleanup()
1724 main.exit()
1725 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001726 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001727 main.cleanup()
1728 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001729
kelvin-onlabd3b64892015-01-20 13:26:24 -08001730 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001731 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001732 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001733 deviceId must be the id of a device as seen in the onos devices command
1734 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001735 role must be either master, standby, or none
1736
Jon Halle3f39ff2015-01-13 11:50:53 -08001737 Returns:
1738 main.TRUE or main.FALSE based on argument verification and
1739 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001740 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001741 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001742 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001743 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001744 cmdStr = "device-role " +\
1745 str( deviceId ) + " " +\
1746 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001747 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001748 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001749 if re.search( "Error", handle ):
1750 # end color output to escape any colours
1751 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001752 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001753 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001754 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001755 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001756 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001757 main.log.error( "Invalid 'role' given to device_role(). " +
1758 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001759 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001760 except TypeError:
1761 main.log.exception( self.name + ": Object not as expected" )
1762 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001763 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001764 main.log.error( self.name + ": EOF exception found" )
1765 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001766 main.cleanup()
1767 main.exit()
1768 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001769 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001770 main.cleanup()
1771 main.exit()
1772
kelvin-onlabd3b64892015-01-20 13:26:24 -08001773 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001774 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001775 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001776 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001777 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001778 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001779 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001780 if jsonFormat:
1781 cmdStr = "clusters -j"
1782 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001783 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001784 handle variable here contains some ANSI escape color code
1785 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001786 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001787 function. The repr( handle ) output when printed shows the ANSI
1788 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001789 variable is actually repr( somestring ) and json.loads would
1790 fail with the escape sequence. So we take off that escape
1791 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001792
kelvin-onlabd3b64892015-01-20 13:26:24 -08001793 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1794 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001795 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001796 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1797 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001798 return handle1
1799 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001800 cmdStr = "clusters"
1801 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001802 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001803 except TypeError:
1804 main.log.exception( self.name + ": Object not as expected" )
1805 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001806 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001807 main.log.error( self.name + ": EOF exception found" )
1808 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001809 main.cleanup()
1810 main.exit()
1811 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001812 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001813 main.cleanup()
1814 main.exit()
1815
kelvin-onlabd3b64892015-01-20 13:26:24 -08001816 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001817 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001818 CLI command to get the current leader for the Election test application
1819 NOTE: Requires installation of the onos-app-election feature
1820 Returns: Node IP of the leader if one exists
1821 None if none exists
1822 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001823 """
Jon Hall94fd0472014-12-08 11:52:42 -08001824 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001825 cmdStr = "election-test-leader"
1826 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001827 # Leader
1828 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001829 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001830 nodeSearch = re.search( leaderPattern, response )
1831 if nodeSearch:
1832 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001833 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001834 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001835 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001836 # no leader
1837 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001838 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001839 nullSearch = re.search( nullPattern, response )
1840 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001841 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001842 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001843 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001844 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001845 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001846 if re.search( errorPattern, response ):
1847 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001848 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001849 return main.FALSE
1850 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001851 main.log.error( "Error in election_test_leader: " +
1852 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001853 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001854 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001855 except TypeError:
1856 main.log.exception( self.name + ": Object not as expected" )
1857 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001858 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001859 main.log.error( self.name + ": EOF exception found" )
1860 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001861 main.cleanup()
1862 main.exit()
1863 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001864 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001865 main.cleanup()
1866 main.exit()
1867
kelvin-onlabd3b64892015-01-20 13:26:24 -08001868 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001869 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001870 CLI command to run for leadership of the Election test application.
1871 NOTE: Requires installation of the onos-app-election feature
1872 Returns: Main.TRUE on success
1873 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001874 """
Jon Hall94fd0472014-12-08 11:52:42 -08001875 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001876 cmdStr = "election-test-run"
1877 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001878 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001879 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001880 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001881 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001882 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001883 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001884 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001885 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001886 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001887 errorPattern = "Command\snot\sfound"
1888 if re.search( errorPattern, response ):
1889 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001890 return main.FALSE
1891 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001892 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001893 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001894 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001895 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001896 except TypeError:
1897 main.log.exception( self.name + ": Object not as expected" )
1898 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001899 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001900 main.log.error( self.name + ": EOF exception found" )
1901 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001902 main.cleanup()
1903 main.exit()
1904 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001905 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001906 main.cleanup()
1907 main.exit()
1908
kelvin-onlabd3b64892015-01-20 13:26:24 -08001909 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08001910 """
Jon Hall94fd0472014-12-08 11:52:42 -08001911 * CLI command to withdraw the local node from leadership election for
1912 * the Election test application.
1913 #NOTE: Requires installation of the onos-app-election feature
1914 Returns: Main.TRUE on success
1915 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001916 """
Jon Hall94fd0472014-12-08 11:52:42 -08001917 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001918 cmdStr = "election-test-withdraw"
1919 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001920 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001921 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001922 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001923 if re.search( successPattern, response ):
1924 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001925 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001926 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001927 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001928 errorPattern = "Command\snot\sfound"
1929 if re.search( errorPattern, response ):
1930 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001931 return main.FALSE
1932 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001933 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001934 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001935 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001936 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001937 except TypeError:
1938 main.log.exception( self.name + ": Object not as expected" )
1939 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001940 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001941 main.log.error( self.name + ": EOF exception found" )
1942 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001943 main.cleanup()
1944 main.exit()
1945 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001946 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001947 main.cleanup()
1948 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001949
kelvin8ec71442015-01-15 16:57:00 -08001950 def getDevicePortsEnabledCount( self, dpid ):
1951 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001952 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001953 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001954 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001955 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001956 cmdStr = "onos:ports -e " + dpid + " | wc -l"
1957 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001958 if re.search( "No such device", output ):
1959 main.log.error( "Error in getting ports" )
1960 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001961 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001962 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001963 except TypeError:
1964 main.log.exception( self.name + ": Object not as expected" )
1965 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001966 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001967 main.log.error( self.name + ": EOF exception found" )
1968 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001969 main.cleanup()
1970 main.exit()
1971 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001972 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001973 main.cleanup()
1974 main.exit()
1975
kelvin8ec71442015-01-15 16:57:00 -08001976 def getDeviceLinksActiveCount( self, dpid ):
1977 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001978 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001979 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001980 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001981 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001982 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
1983 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001984 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001985 main.log.error( "Error in getting ports " )
1986 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001987 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001988 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001989 except TypeError:
1990 main.log.exception( self.name + ": Object not as expected" )
1991 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001992 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001993 main.log.error( self.name + ": EOF exception found" )
1994 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001995 main.cleanup()
1996 main.exit()
1997 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001998 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001999 main.cleanup()
2000 main.exit()
2001
kelvin8ec71442015-01-15 16:57:00 -08002002 def getAllIntentIds( self ):
2003 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002004 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002005 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002006 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002007 cmdStr = "onos:intents | grep id="
2008 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002009 if re.search( "Error", output ):
2010 main.log.error( "Error in getting ports" )
2011 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002012 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002013 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002014 except TypeError:
2015 main.log.exception( self.name + ": Object not as expected" )
2016 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002017 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002018 main.log.error( self.name + ": EOF exception found" )
2019 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002020 main.cleanup()
2021 main.exit()
2022 except:
Jon Halld4d4b372015-01-28 16:02:41 -08002023 main.log.exception( self.name + ": Uncaught exception!" )
2024 main.cleanup()
2025 main.exit()
2026
2027 def testExceptions( self, obj ):
2028 """
2029 Test exception logging
2030 """
2031 # FIXME: Remove this before you commit
2032
2033 try:
2034 return obj[ 'dedf' ]
2035 except TypeError:
2036 main.log.exception( self.name + ": Object not as expected" )
2037 return None
2038 except pexpect.EOF:
2039 main.log.error( self.name + ": EOF exception found" )
2040 main.log.error( self.name + ": " + self.handle.before )
2041 main.cleanup()
2042 main.exit()
2043 except:
2044 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002045 main.cleanup()
2046 main.exit()