blob: 2d1ab22e770cdfdd9f25553cc81e4d75e750b819 [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
Jon Hall30b82fa2015-03-04 17:15:43 -080022import json
23import types
kelvin8ec71442015-01-15 16:57:00 -080024sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040025from drivers.common.clidriver import CLI
26
andrewonlab95ce8322014-10-13 14:12:04 -040027
kelvin8ec71442015-01-15 16:57:00 -080028class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040029
kelvin8ec71442015-01-15 16:57:00 -080030 def __init__( self ):
31 """
32 Initialize client
33 """
Jon Hallefbd9792015-03-05 16:11:36 -080034 self.name = None
35 self.home = None
36 self.handle = None
kelvin8ec71442015-01-15 16:57:00 -080037 super( CLI, self ).__init__()
38
39 def connect( self, **connectargs ):
40 """
andrewonlab95ce8322014-10-13 14:12:04 -040041 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080042 """
andrewonlab95ce8322014-10-13 14:12:04 -040043 try:
44 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080045 vars( self )[ key ] = connectargs[ key ]
andrewonlab95ce8322014-10-13 14:12:04 -040046 self.home = "~/ONOS"
47 for key in self.options:
48 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080049 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040050 break
kelvin-onlabfb521662015-02-27 09:52:40 -080051 if self.home is None or self.home == "":
kelvin-onlabd6634ac2015-01-29 14:23:10 -080052 self.home = "~/ONOS"
kelvin-onlabfb521662015-02-27 09:52:40 -080053
kelvin8ec71442015-01-15 16:57:00 -080054 self.name = self.options[ 'name' ]
55 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080056 user_name=self.user_name,
57 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080058 port=self.port,
59 pwd=self.pwd,
60 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040061
kelvin8ec71442015-01-15 16:57:00 -080062 self.handle.sendline( "cd " + self.home )
63 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040064 if self.handle:
65 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080066 else:
67 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040068 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080069 except TypeError:
70 main.log.exception( self.name + ": Object not as expected" )
71 return None
andrewonlab95ce8322014-10-13 14:12:04 -040072 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080073 main.log.error( self.name + ": EOF exception found" )
74 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040075 main.cleanup()
76 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080077 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -080078 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -040079 main.cleanup()
80 main.exit()
81
kelvin8ec71442015-01-15 16:57:00 -080082 def disconnect( self ):
83 """
andrewonlab95ce8322014-10-13 14:12:04 -040084 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080085 """
Jon Halld61331b2015-02-17 16:35:47 -080086 response = main.TRUE
Jon Hallefbd9792015-03-05 16:11:36 -080087 # noinspection PyBroadException
andrewonlab95ce8322014-10-13 14:12:04 -040088 try:
Jon Hallb7fce3e2015-03-04 10:18:32 -080089 self.logout()
kelvin8ec71442015-01-15 16:57:00 -080090 self.handle.sendline( "" )
91 self.handle.expect( "\$" )
92 self.handle.sendline( "exit" )
93 self.handle.expect( "closed" )
Jon Halld4d4b372015-01-28 16:02:41 -080094 except TypeError:
95 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -080096 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -040097 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080098 main.log.error( self.name + ": EOF exception found" )
99 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800100 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800101 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400102 response = main.FALSE
103 return response
104
kelvin8ec71442015-01-15 16:57:00 -0800105 def logout( self ):
106 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500107 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800108 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500109 try:
kelvin8ec71442015-01-15 16:57:00 -0800110 self.handle.sendline( "" )
Jon Hallb7fce3e2015-03-04 10:18:32 -0800111 i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ],
112 timeout=10 )
113 if i == 0: # In ONOS CLI
kelvin8ec71442015-01-15 16:57:00 -0800114 self.handle.sendline( "logout" )
115 self.handle.expect( "\$" )
Jon Hallb7fce3e2015-03-04 10:18:32 -0800116 elif i == 1: # not in CLI
andrewonlab9627f432014-11-14 12:45:10 -0500117 return main.TRUE
Jon Hallb7fce3e2015-03-04 10:18:32 -0800118 elif i == 3: # Timeout
119 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -0800120 except TypeError:
121 main.log.exception( self.name + ": Object not as expected" )
122 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500123 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800124 main.log.error( self.name + ": eof exception found" )
125 main.log.error( self.name + ": " +
126 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500127 main.cleanup()
128 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800129 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800130 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500131 main.cleanup()
132 main.exit()
133
kelvin-onlabd3b64892015-01-20 13:26:24 -0800134 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800135 """
andrewonlab95ce8322014-10-13 14:12:04 -0400136 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800137
andrewonlab95ce8322014-10-13 14:12:04 -0400138 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800139 """
andrewonlab95ce8322014-10-13 14:12:04 -0400140 try:
141 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800142 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400143 main.cleanup()
144 main.exit()
145 else:
kelvin8ec71442015-01-15 16:57:00 -0800146 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800147 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800148 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400149 # and that this driver will have to change accordingly
Jon Hall1e03cb62015-02-19 12:07:12 -0800150 self.handle.expect( "ONOS_CELL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800151 handleBefore = self.handle.before
152 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800153 # Get the rest of the handle
154 self.handle.sendline( "" )
155 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800156 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400157
kelvin-onlabd3b64892015-01-20 13:26:24 -0800158 main.log.info( "Cell call returned: " + handleBefore +
159 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400160
161 return main.TRUE
162
Jon Halld4d4b372015-01-28 16:02:41 -0800163 except TypeError:
164 main.log.exception( self.name + ": Object not as expected" )
165 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400166 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800167 main.log.error( self.name + ": eof exception found" )
168 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400169 main.cleanup()
170 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800171 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800172 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400173 main.cleanup()
174 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800175
kelvin-onlabd3b64892015-01-20 13:26:24 -0800176 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800177 """
Jon Hallefbd9792015-03-05 16:11:36 -0800178 karafTimeout is an optional argument. karafTimeout value passed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 by user would be used to set the current karaf shell idle timeout.
180 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800181 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800182 Below is an example to start a session with 60 seconds idle timeout
183 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800184
Hari Krishna25d42f72015-01-05 15:08:28 -0800185 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800186 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800187
kelvin-onlabd3b64892015-01-20 13:26:24 -0800188 Note: karafTimeout is left as str so that this could be read
189 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800190 """
andrewonlab95ce8322014-10-13 14:12:04 -0400191 try:
kelvin8ec71442015-01-15 16:57:00 -0800192 self.handle.sendline( "" )
193 x = self.handle.expect( [
194 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500195
196 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800197 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500198 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400199
kelvin8ec71442015-01-15 16:57:00 -0800200 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800201 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800202 i = self.handle.expect( [
203 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800204 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400205
206 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800207 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800208 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800209 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800210 "config:property-set -p org.apache.karaf.shell\
211 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800212 karafTimeout )
213 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800214 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800215 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400216 return main.TRUE
217 else:
kelvin8ec71442015-01-15 16:57:00 -0800218 # If failed, send ctrl+c to process and try again
219 main.log.info( "Starting CLI failed. Retrying..." )
220 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800221 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800222 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
223 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400224 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800225 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800226 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800227 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800228 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800229 "config:property-set -p org.apache.karaf.shell\
230 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800231 karafTimeout )
232 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800233 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800234 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400235 return main.TRUE
236 else:
kelvin8ec71442015-01-15 16:57:00 -0800237 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800238 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400239 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400240
Jon Halld4d4b372015-01-28 16:02:41 -0800241 except TypeError:
242 main.log.exception( self.name + ": Object not as expected" )
243 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400244 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800245 main.log.error( self.name + ": EOF exception found" )
246 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400247 main.cleanup()
248 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800249 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800250 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400251 main.cleanup()
252 main.exit()
253
Jon Hallefbd9792015-03-05 16:11:36 -0800254 def log( self, cmdStr, level="" ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800255 """
256 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800257 returns main.TRUE on success
Jon Hallefbd9792015-03-05 16:11:36 -0800258 returns main.FALSE if Error occurred
kelvin-onlab338f5512015-02-06 10:53:16 -0800259 Available level: DEBUG, TRACE, INFO, WARN, ERROR
260 Level defaults to INFO
kelvin-onlab9f541032015-02-04 16:19:53 -0800261 """
262 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800263 lvlStr = ""
264 if level:
265 lvlStr = "--level=" + level
266
kelvin-onlab9f541032015-02-04 16:19:53 -0800267 self.handle.sendline( "" )
268 self.handle.expect( "onos>" )
kelvin-onlab338f5512015-02-06 10:53:16 -0800269 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
kelvin-onlab9f541032015-02-04 16:19:53 -0800270 self.handle.expect( "onos>" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800271
kelvin-onlab9f541032015-02-04 16:19:53 -0800272 response = self.handle.before
273 if re.search( "Error", response ):
274 return main.FALSE
275 return main.TRUE
276
277 except pexpect.EOF:
278 main.log.error( self.name + ": EOF exception found" )
279 main.log.error( self.name + ": " + self.handle.before )
280 main.cleanup()
281 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800282 except Exception:
kelvin-onlabfb521662015-02-27 09:52:40 -0800283 main.log.exception( self.name + ": Uncaught exception!" )
kelvin-onlab9f541032015-02-04 16:19:53 -0800284 main.cleanup()
285 main.exit()
286
kelvin-onlabd3b64892015-01-20 13:26:24 -0800287 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800288 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800289 Send a completely user specified string to
290 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400291 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800292
andrewonlaba18f6bf2014-10-13 19:31:54 -0400293 Warning: There are no sanity checking to commands
294 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800295 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400296 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800297 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
298 self.log( logStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800299 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800300 self.handle.expect( "onos>" )
Jon Hallaea67aa2015-01-23 13:30:57 -0800301 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
302 + self.name + "." )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400303 handle = self.handle.before
Jon Hall7bdfc122015-01-23 11:45:32 -0800304 # Remove control strings from output
305 ansiEscape = re.compile( r'\x1b[^m]*m' )
306 handle = ansiEscape.sub( '', handle )
kelvin-onlabfb521662015-02-27 09:52:40 -0800307 # Remove extra return chars that get added
Jon Hallaea67aa2015-01-23 13:30:57 -0800308 handle = re.sub( r"\s\r", "", handle )
309 handle = handle.strip()
Jon Hall7bdfc122015-01-23 11:45:32 -0800310 # parse for just the output, remove the cmd from handle
311 output = handle.split( cmdStr, 1 )[1]
Jon Hall7bdfc122015-01-23 11:45:32 -0800312 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800313 except TypeError:
314 main.log.exception( self.name + ": Object not as expected" )
315 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400316 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800317 main.log.error( self.name + ": EOF exception found" )
318 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400319 main.cleanup()
320 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800321 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800322 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400323 main.cleanup()
324 main.exit()
325
kelvin8ec71442015-01-15 16:57:00 -0800326 # IMPORTANT NOTE:
327 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800328 # the cli command changing 'a:b' with 'aB'.
329 # Ex ) onos:topology > onosTopology
330 # onos:links > onosLinks
331 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800332
kelvin-onlabd3b64892015-01-20 13:26:24 -0800333 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800334 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400335 Adds a new cluster node by ID and address information.
336 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800337 * nodeId
338 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400339 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800340 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800341 """
Jon Hallefbd9792015-03-05 16:11:36 -0800342 # noinspection PyBroadException
andrewonlabc2d05aa2014-10-13 16:51:10 -0400343 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800344 cmdStr = "add-node " + str( nodeId ) + " " +\
345 str( ONOSIp ) + " " + str( tcpPort )
346 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800347 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800348 main.log.error( "Error in adding node" )
349 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800350 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400351 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800352 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400353 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800354 except TypeError:
355 main.log.exception( self.name + ": Object not as expected" )
356 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400357 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800358 main.log.error( self.name + ": EOF exception found" )
359 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400360 main.cleanup()
361 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800362 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800363 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400364 main.cleanup()
365 main.exit()
366
kelvin-onlabd3b64892015-01-20 13:26:24 -0800367 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800368 """
andrewonlab86dc3082014-10-13 18:18:38 -0400369 Removes a cluster by ID
370 Issues command: 'remove-node [<node-id>]'
371 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800372 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800373 """
andrewonlab86dc3082014-10-13 18:18:38 -0400374 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400375
kelvin-onlabd3b64892015-01-20 13:26:24 -0800376 cmdStr = "remove-node " + str( nodeId )
377 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800378 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400379
380 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800381
Jon Halld4d4b372015-01-28 16:02:41 -0800382 except TypeError:
383 main.log.exception( self.name + ": Object not as expected" )
384 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400385 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800386 main.log.error( self.name + ": EOF exception found" )
387 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400388 main.cleanup()
389 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800390 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800391 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400392 main.cleanup()
393 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400394
kelvin8ec71442015-01-15 16:57:00 -0800395 def nodes( self ):
396 """
andrewonlab7c211572014-10-15 16:45:20 -0400397 List the nodes currently visible
398 Issues command: 'nodes'
399 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800400 """
andrewonlab7c211572014-10-15 16:45:20 -0400401 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800402 cmdStr = "nodes"
403 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400404 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800405 except TypeError:
406 main.log.exception( self.name + ": Object not as expected" )
407 return None
andrewonlab7c211572014-10-15 16:45:20 -0400408 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800409 main.log.error( self.name + ": EOF exception found" )
410 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400411 main.cleanup()
412 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800413 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800414 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400415 main.cleanup()
416 main.exit()
417
kelvin8ec71442015-01-15 16:57:00 -0800418 def topology( self ):
419 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400420 Shows the current state of the topology
Jon Hallefbd9792015-03-05 16:11:36 -0800421 by issuing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800422 """
andrewonlab95ce8322014-10-13 14:12:04 -0400423 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800424 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800425 cmdStr = "onos:topology"
426 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800427 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400428 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800429 except TypeError:
430 main.log.exception( self.name + ": Object not as expected" )
431 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400432 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800433 main.log.error( self.name + ": EOF exception found" )
434 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400435 main.cleanup()
436 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800437 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800438 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400439 main.cleanup()
440 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800441
kelvin-onlabd3b64892015-01-20 13:26:24 -0800442 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800443 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800444 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400445 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800446 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400447 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800448 cmdStr = "feature:install " + str( featureStr )
449 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800450 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400451 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800452 except TypeError:
453 main.log.exception( self.name + ": Object not as expected" )
454 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400455 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800456 main.log.error( self.name + ": EOF exception found" )
457 main.log.error( self.name + ": " + self.handle.before )
458 main.log.report( "Failed to install feature" )
459 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400460 main.cleanup()
461 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800462 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800463 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800464 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()
Jon Halle3f39ff2015-01-13 11:50:53 -0800468
kelvin-onlabd3b64892015-01-20 13:26:24 -0800469 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800470 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400471 Uninstalls a specified feature
472 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800473 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400474 try:
Jon Hall30b82fa2015-03-04 17:15:43 -0800475 cmdStr = 'feature:list -i | grep "' + featureStr + '"'
476 handle = self.sendline( cmdStr )
477 if handle != '':
478 cmdStr = "feature:uninstall " + str( featureStr )
479 self.sendline( cmdStr )
480 # TODO: Check for possible error responses from karaf
481 else:
Jon Hallefbd9792015-03-05 16:11:36 -0800482 main.log.info( "Feature needs to be installed before " +
483 "uninstalling it" )
shahshreya280223a2015-02-26 12:25:25 -0800484 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()
Jon Hallfebb1c72015-03-05 13:30:09 -0800493 except Exception:
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()
Jon Hallfebb1c72015-03-05 13:30:09 -0800536 except Exception:
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()
Jon Hallfebb1c72015-03-05 13:30:09 -0800560 except Exception:
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()
Jon Hallfebb1c72015-03-05 13:30:09 -0800603 except Exception:
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
Jon Hallefbd9792015-03-05 16:11:36 -0800626 sequence using the following commands:
Jon Halle3f39ff2015-01-13 11:50:53 -0800627
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()
Jon Hallfebb1c72015-03-05 13:30:09 -0800647 except Exception:
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()
Jon Hallfebb1c72015-03-05 13:30:09 -0800693 except Exception:
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:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800710 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400711 return None
712 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800713 rawRoles = self.roles()
714 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800715 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800716 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800717 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800718 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400719 return device
720 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800721 except TypeError:
722 main.log.exception( self.name + ": Object not as expected" )
723 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400724 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800725 main.log.error( self.name + ": EOF exception found" )
726 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400727 main.cleanup()
728 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800729 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800730 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400731 main.cleanup()
732 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800733
kelvin-onlabd3b64892015-01-20 13:26:24 -0800734 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800735 """
Jon Hall94fd0472014-12-08 11:52:42 -0800736 Iterates through each device and checks if there is a master assigned
737 Returns: main.TRUE if each device has a master
738 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800739 """
Jon Hall94fd0472014-12-08 11:52:42 -0800740 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800741 rawRoles = self.roles()
742 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800743 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800744 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800745 # print device
746 if device[ 'master' ] == "none":
747 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800748 return main.FALSE
749 return main.TRUE
750
Jon Halld4d4b372015-01-28 16:02:41 -0800751 except TypeError:
752 main.log.exception( self.name + ": Object not as expected" )
753 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800754 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800755 main.log.error( self.name + ": EOF exception found" )
756 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800757 main.cleanup()
758 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800759 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800760 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800761 main.cleanup()
762 main.exit()
763
kelvin-onlabd3b64892015-01-20 13:26:24 -0800764 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800765 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400766 Returns string of paths, and the cost.
767 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800768 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400769 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800770 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
771 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800772 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800773 main.log.error( "Error in getting paths" )
774 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400775 else:
kelvin8ec71442015-01-15 16:57:00 -0800776 path = handle.split( ";" )[ 0 ]
777 cost = handle.split( ";" )[ 1 ]
778 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800779 except TypeError:
780 main.log.exception( self.name + ": Object not as expected" )
781 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400782 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800783 main.log.error( self.name + ": EOF exception found" )
784 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400785 main.cleanup()
786 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800787 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800788 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400789 main.cleanup()
790 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800791
kelvin-onlabd3b64892015-01-20 13:26:24 -0800792 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800793 """
Jon Hallffb386d2014-11-21 13:43:38 -0800794 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400795 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800796 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800797 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400798 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800799 if jsonFormat:
800 cmdStr = "hosts -j"
801 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800802 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800803 handle variable here contains some ANSI escape color code
804 sequences at the end which are invisible in the print command
805 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800806 function. The repr( handle ) output when printed shows the ANSI
807 escape sequences. In json.loads( somestring ), this somestring
808 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800809 fail with the escape sequence. So we take off that escape
810 sequence using:
811
kelvin-onlabd3b64892015-01-20 13:26:24 -0800812 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
813 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800814 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800815 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
816 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400817 return handle1
818 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800819 cmdStr = "hosts"
820 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400821 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800822 except TypeError:
823 main.log.exception( self.name + ": Object not as expected" )
824 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400825 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800826 main.log.error( self.name + ": EOF exception found" )
827 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400828 main.cleanup()
829 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800830 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800831 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400832 main.cleanup()
833 main.exit()
834
kelvin-onlabd3b64892015-01-20 13:26:24 -0800835 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800836 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400837 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800838
Jon Hallefbd9792015-03-05 16:11:36 -0800839 Note: mac must be a colon separated mac address, but could be a
Jon Halle3f39ff2015-01-13 11:50:53 -0800840 partial mac address
841
Jon Hall42db6dc2014-10-24 19:03:48 -0400842 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800843 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400844 try:
kelvin8ec71442015-01-15 16:57:00 -0800845 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400846 return None
847 else:
848 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800849 rawHosts = self.hosts()
850 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800851 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800852 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800853 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800854 if not host:
855 pass
856 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400857 return host
858 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800859 except TypeError:
860 main.log.exception( self.name + ": Object not as expected" )
861 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400862 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800863 main.log.error( self.name + ": EOF exception found" )
864 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400865 main.cleanup()
866 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800867 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800868 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400869 main.cleanup()
870 main.exit()
871
kelvin-onlabd3b64892015-01-20 13:26:24 -0800872 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800873 """
874 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400875 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800876
andrewonlab3f0a4af2014-10-17 12:25:14 -0400877 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800878 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400879 IMPORTANT:
880 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800881 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400882 Furthermore, it assumes that value of VLAN is '-1'
883 Description:
kelvin8ec71442015-01-15 16:57:00 -0800884 Converts mininet hosts ( h1, h2, h3... ) into
885 ONOS format ( 00:00:00:00:00:01/-1 , ... )
886 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400887 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800888 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400889
kelvin-onlabd3b64892015-01-20 13:26:24 -0800890 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800891 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800892 hostHex = hex( int( host ) ).zfill( 12 )
893 hostHex = str( hostHex ).replace( 'x', '0' )
894 i = iter( str( hostHex ) )
895 hostHex = ":".join( a + b for a, b in zip( i, i ) )
896 hostHex = hostHex + "/-1"
897 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400898
kelvin-onlabd3b64892015-01-20 13:26:24 -0800899 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400900
Jon Halld4d4b372015-01-28 16:02:41 -0800901 except TypeError:
902 main.log.exception( self.name + ": Object not as expected" )
903 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400904 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800905 main.log.error( self.name + ": EOF exception found" )
906 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400907 main.cleanup()
908 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800909 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800910 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400911 main.cleanup()
912 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400913
kelvin-onlabd3b64892015-01-20 13:26:24 -0800914 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800915 """
andrewonlabe6745342014-10-17 14:29:13 -0400916 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800917 * hostIdOne: ONOS host id for host1
918 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400919 Description:
Jon Hallefbd9792015-03-05 16:11:36 -0800920 Adds a host-to-host intent ( bidirectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500921 specifying the two hosts.
kelvin-onlabfb521662015-02-27 09:52:40 -0800922 Returns:
923 A string of the intent id or None on Error
kelvin8ec71442015-01-15 16:57:00 -0800924 """
andrewonlabe6745342014-10-17 14:29:13 -0400925 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800926 cmdStr = "add-host-intent " + str( hostIdOne ) +\
927 " " + str( hostIdTwo )
928 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800929 if re.search( "Error", handle ):
930 main.log.error( "Error in adding Host intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800931 return None
Hari Krishnaac4e1782015-01-26 12:09:12 -0800932 else:
933 main.log.info( "Host intent installed between " +
kelvin-onlabfb521662015-02-27 09:52:40 -0800934 str( hostIdOne ) + " and " + str( hostIdTwo ) )
935 match = re.search('id=0x([\da-f]+),', handle)
936 if match:
937 return match.group()[3:-1]
938 else:
939 main.log.error( "Error, intent ID not found" )
940 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800941 except TypeError:
942 main.log.exception( self.name + ": Object not as expected" )
943 return None
andrewonlabe6745342014-10-17 14:29:13 -0400944 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800945 main.log.error( self.name + ": EOF exception found" )
946 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400947 main.cleanup()
948 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800949 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800950 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400951 main.cleanup()
952 main.exit()
953
kelvin-onlabd3b64892015-01-20 13:26:24 -0800954 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800955 """
andrewonlab7b31d232014-10-24 13:31:47 -0400956 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800957 * ingressDevice: device id of ingress device
958 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400959 Optional:
960 TODO: Still needs to be implemented via dev side
kelvin-onlabfb521662015-02-27 09:52:40 -0800961 Description:
962 Adds an optical intent by specifying an ingress and egress device
963 Returns:
964 A string of the intent id or None on error
kelvin-onlab898a6c62015-01-16 14:13:53 -0800965 """
andrewonlab7b31d232014-10-24 13:31:47 -0400966 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800967 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
968 " " + str( egressDevice )
969 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800970 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800971 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -0800972 main.log.error( "Error in adding Optical intent" )
973 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400974 else:
kelvin-onlabfb521662015-02-27 09:52:40 -0800975 main.log.info( "Optical intent installed between " +
976 str( ingressDevice ) + " and " +
977 str( egressDevice ) )
978 match = re.search('id=0x([\da-f]+),', handle)
979 if match:
980 return match.group()[3:-1]
981 else:
982 main.log.error( "Error, intent ID not found" )
983 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800984 except TypeError:
985 main.log.exception( self.name + ": Object not as expected" )
986 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400987 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800988 main.log.error( self.name + ": EOF exception found" )
989 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400990 main.cleanup()
991 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800992 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800993 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -0400994 main.cleanup()
995 main.exit()
996
kelvin-onlabd3b64892015-01-20 13:26:24 -0800997 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800998 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800999 ingressDevice,
1000 egressDevice,
1001 portIngress="",
1002 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001003 ethType="",
1004 ethSrc="",
1005 ethDst="",
1006 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001007 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001008 ipProto="",
1009 ipSrc="",
1010 ipDst="",
1011 tcpSrc="",
1012 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001013 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001014 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001015 * ingressDevice: device id of ingress device
1016 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001017 Optional:
1018 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001019 * ethSrc: specify ethSrc ( i.e. src mac addr )
1020 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001021 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001022 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001023 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001024 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001025 * ipSrc: specify ip source address
1026 * ipDst: specify ip destination address
1027 * tcpSrc: specify tcp source port
1028 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001029 Description:
kelvin8ec71442015-01-15 16:57:00 -08001030 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001031 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001032 Returns:
1033 A string of the intent id or None on error
andrewonlab289e4b72014-10-21 21:24:18 -04001034
Jon Halle3f39ff2015-01-13 11:50:53 -08001035 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001036 options developers provide for point-to-point
1037 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001038 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001039 try:
kelvin8ec71442015-01-15 16:57:00 -08001040 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001041 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001042 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001043 and not ipProto and not ipSrc and not ipDst \
1044 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001045 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001046
andrewonlab289e4b72014-10-21 21:24:18 -04001047 else:
andrewonlab36af3822014-11-18 17:48:18 -05001048 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001049
andrewonlab0c0a6772014-10-22 12:31:18 -04001050 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001051 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001052 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001053 cmd += " --ethSrc " + str( ethSrc )
1054 if ethDst:
1055 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001056 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001057 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001058 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001059 cmd += " --lambda "
1060 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001061 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001062 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001063 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001064 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001065 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001066 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001067 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001068 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001069 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001070
kelvin8ec71442015-01-15 16:57:00 -08001071 # Check whether the user appended the port
1072 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001073 if "/" in ingressDevice:
1074 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001075 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001076 if not portIngress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001077 main.log.error( "You must specify the ingress port" )
kelvin8ec71442015-01-15 16:57:00 -08001078 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001079 # Would it make sense to throw an exception and exit
1080 # the test?
1081 return None
andrewonlab36af3822014-11-18 17:48:18 -05001082
kelvin8ec71442015-01-15 16:57:00 -08001083 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001084 str( ingressDevice ) + "/" +\
1085 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001086
kelvin-onlabd3b64892015-01-20 13:26:24 -08001087 if "/" in egressDevice:
1088 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001089 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001090 if not portEgress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001091 main.log.error( "You must specify the egress port" )
1092 return None
Jon Halle3f39ff2015-01-13 11:50:53 -08001093
kelvin8ec71442015-01-15 16:57:00 -08001094 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001095 str( egressDevice ) + "/" +\
1096 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001097
kelvin-onlab898a6c62015-01-16 14:13:53 -08001098 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001099 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001100 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001101 main.log.error( "Error in adding point-to-point intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001102 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001103 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001104 # TODO: print out all the options in this message?
1105 main.log.info( "Point-to-point intent installed between " +
1106 str( ingressDevice ) + " and " +
1107 str( egressDevice ) )
1108 match = re.search('id=0x([\da-f]+),', handle)
1109 if match:
1110 return match.group()[3:-1]
1111 else:
1112 main.log.error( "Error, intent ID not found" )
1113 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001114 except TypeError:
1115 main.log.exception( self.name + ": Object not as expected" )
1116 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001117 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001118 main.log.error( self.name + ": EOF exception found" )
1119 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001120 main.cleanup()
1121 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001122 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001123 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001124 main.cleanup()
1125 main.exit()
1126
kelvin-onlabd3b64892015-01-20 13:26:24 -08001127 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001128 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001129 ingressDevice1,
1130 ingressDevice2,
1131 egressDevice,
kelvin-onlabfb521662015-02-27 09:52:40 -08001132 portIngress1="",
1133 portIngress2="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001134 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001135 ethType="",
1136 ethSrc="",
1137 ethDst="",
1138 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001139 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001140 ipProto="",
1141 ipSrc="",
1142 ipDst="",
1143 tcpSrc="",
1144 tcpDst="",
1145 setEthSrc="",
1146 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001147 """
shahshreyad0c80432014-12-04 16:56:05 -08001148 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001149 This function assumes that there would be 2 ingress devices and
1150 one egress device. For more number of ingress devices, this
1151 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001152 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001153 * ingressDevice1: device id of ingress device1
1154 * ingressDevice2: device id of ingress device2
1155 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001156 Optional:
1157 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001158 * ethSrc: specify ethSrc ( i.e. src mac addr )
1159 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001160 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001161 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001162 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001163 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001164 * ipSrc: specify ip source address
1165 * ipDst: specify ip destination address
1166 * tcpSrc: specify tcp source port
1167 * tcpDst: specify tcp destination port
1168 * setEthSrc: action to Rewrite Source MAC Address
1169 * setEthDst: action to Rewrite Destination MAC Address
1170 Description:
kelvin8ec71442015-01-15 16:57:00 -08001171 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001172 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001173 Returns:
1174 A string of the intent id or None on error
shahshreyad0c80432014-12-04 16:56:05 -08001175
Jon Halle3f39ff2015-01-13 11:50:53 -08001176 NOTE: This function may change depending on the
Jon Hallefbd9792015-03-05 16:11:36 -08001177 options developers provide for multipoint-to-singlepoint
shahshreyad0c80432014-12-04 16:56:05 -08001178 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001179 """
shahshreyad0c80432014-12-04 16:56:05 -08001180 try:
kelvin8ec71442015-01-15 16:57:00 -08001181 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001182 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001183 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001184 and not ipProto and not ipSrc and not ipDst\
1185 and not tcpSrc and not tcpDst and not setEthSrc\
1186 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001187 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001188
1189 else:
1190 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001191
shahshreyad0c80432014-12-04 16:56:05 -08001192 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001193 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001194 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001195 cmd += " --ethSrc " + str( ethSrc )
1196 if ethDst:
1197 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001198 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001199 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001200 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001201 cmd += " --lambda "
1202 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001203 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001204 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001205 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001206 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001207 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001208 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001209 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001210 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001211 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001212 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001213 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001214 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001215 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001216
kelvin8ec71442015-01-15 16:57:00 -08001217 # Check whether the user appended the port
1218 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001219 if "/" in ingressDevice1:
1220 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001221 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001222 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001223 main.log.error( "You must specify " +
1224 "the ingress port1" )
1225 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001226 return None
shahshreyad0c80432014-12-04 16:56:05 -08001227
kelvin8ec71442015-01-15 16:57:00 -08001228 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001229 str( ingressDevice1 ) + "/" +\
1230 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001231
kelvin-onlabd3b64892015-01-20 13:26:24 -08001232 if "/" in ingressDevice2:
1233 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001234 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001235 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001236 main.log.error( "You must specify " +
1237 "the ingress port2" )
1238 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001239 return None
shahshreyad0c80432014-12-04 16:56:05 -08001240
kelvin8ec71442015-01-15 16:57:00 -08001241 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001242 str( ingressDevice2 ) + "/" +\
1243 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001244
kelvin-onlabd3b64892015-01-20 13:26:24 -08001245 if "/" in egressDevice:
1246 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001247 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001248 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001249 main.log.error( "You must specify " +
1250 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001251 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001252
kelvin8ec71442015-01-15 16:57:00 -08001253 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001254 str( egressDevice ) + "/" +\
1255 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001256 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001257 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001258 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001259 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001260 main.log.error( "Error in adding multipoint-to-singlepoint " +
1261 "intent" )
1262 return None
shahshreyad0c80432014-12-04 16:56:05 -08001263 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001264 # TODO: print out all the options in this message?
1265 main.log.info( "Multipoint-to-singlepoint intent installed" +
1266 " between " + str( ingressDevice1 ) + ", " +
1267 str( ingressDevice2 ) + " and " +
1268 str( egressDevice ) )
1269 match = re.search('id=0x([\da-f]+),', handle)
1270 if match:
1271 return match.group()[3:-1]
1272 else:
1273 main.log.error( "Error, intent ID not found" )
1274 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001275 except TypeError:
1276 main.log.exception( self.name + ": Object not as expected" )
1277 return None
shahshreyad0c80432014-12-04 16:56:05 -08001278 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001279 main.log.error( self.name + ": EOF exception found" )
1280 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001281 main.cleanup()
1282 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001283 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001284 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001285 main.cleanup()
1286 main.exit()
1287
Jon Hallefbd9792015-03-05 16:11:36 -08001288 def removeIntent( self, intentId, app='org.onosproject.cli',
1289 purge=False, sync=False ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001290 """
shahshreya1c818fc2015-02-26 13:44:08 -08001291 Remove intent for specified application id and intent id
1292 Optional args:-
1293 -s or --sync: Waits for the removal before returning
1294 -p or --purge: Purge the intent from the store after removal
1295
Jon Halle3f39ff2015-01-13 11:50:53 -08001296 Returns:
1297 main.False on error and
1298 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001299 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001300 try:
shahshreya1c818fc2015-02-26 13:44:08 -08001301 cmdStr = "remove-intent "
1302 if purge:
1303 cmdStr += " -p"
1304 if sync:
1305 cmdStr += " -s"
1306
1307 cmdStr += " " + app + " " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001308 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001309 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001310 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001311 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001312 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001313 # TODO: Should this be main.TRUE
1314 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001315 except TypeError:
1316 main.log.exception( self.name + ": Object not as expected" )
1317 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001318 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001319 main.log.error( self.name + ": EOF exception found" )
1320 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001321 main.cleanup()
1322 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001323 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001324 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001325 main.cleanup()
1326 main.exit()
1327
kelvin-onlabd3b64892015-01-20 13:26:24 -08001328 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001329 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001330 NOTE: This method should be used after installing application:
1331 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001332 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001333 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001334 Description:
1335 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001336 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001337 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001338 if jsonFormat:
1339 cmdStr = "routes -j"
1340 handleTmp = self.sendline( cmdStr )
1341 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1342 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001343 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001344 cmdStr = "routes"
1345 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001346 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001347 except TypeError:
1348 main.log.exception( self.name + ": Object not as expected" )
1349 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001350 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001351 main.log.error( self.name + ": EOF exception found" )
1352 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001353 main.cleanup()
1354 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001355 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001356 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001357 main.cleanup()
1358 main.exit()
1359
kelvin-onlabd3b64892015-01-20 13:26:24 -08001360 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001361 """
andrewonlab377693f2014-10-21 16:00:30 -04001362 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001363 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001364 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001365 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001366 """
andrewonlabe6745342014-10-17 14:29:13 -04001367 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001368 if jsonFormat:
1369 cmdStr = "intents -j"
1370 handle = self.sendline( cmdStr )
1371 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1372 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001373 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001374 cmdStr = "intents"
1375 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001376 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001377 except TypeError:
1378 main.log.exception( self.name + ": Object not as expected" )
1379 return None
andrewonlabe6745342014-10-17 14:29:13 -04001380 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001381 main.log.error( self.name + ": EOF exception found" )
1382 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001383 main.cleanup()
1384 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001385 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001386 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001387 main.cleanup()
1388 main.exit()
1389
kelvin-onlab54400a92015-02-26 18:05:51 -08001390 def getIntentState(self, intentsId, intentsJson=None):
1391 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001392 Check intent state.
1393 Accepts a single intent ID (string type) or a list of intent IDs.
1394 Returns the state(string type) of the id if a single intent ID is
1395 accepted.
Jon Hallefbd9792015-03-05 16:11:36 -08001396 Returns a dictionary with intent IDs as the key and its
1397 corresponding states as the values
kelvin-onlabfb521662015-02-27 09:52:40 -08001398 Parameters:
kelvin-onlab54400a92015-02-26 18:05:51 -08001399 intentId: intent ID (string type)
1400 intentsJson: parsed json object from the onos:intents api
1401 Returns:
1402 state = An intent's state- INSTALL,WITHDRAWN etc.
1403 stateDict = Dictionary of intent's state. intent ID as the keys and
1404 state as the values.
1405 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001406 try:
1407 state = "State is Undefined"
1408 if not intentsJson:
Jon Hallefbd9792015-03-05 16:11:36 -08001409 intentsJsonTemp = json.loads( self.intents() )
kelvin-onlab54400a92015-02-26 18:05:51 -08001410 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001411 intentsJsonTemp = json.loads( intentsJson )
1412 if isinstance( intentsId, types.StringType ):
kelvin-onlab54400a92015-02-26 18:05:51 -08001413 for intent in intentsJsonTemp:
1414 if intentsId == intent['id']:
1415 state = intent['state']
1416 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001417 main.log.info( "Cannot find intent ID" + str( intentsId ) +
1418 " on the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001419 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001420 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001421 dictList = []
kelvin-onlab54400a92015-02-26 18:05:51 -08001422 for ID in intentsId:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001423 stateDict = {}
kelvin-onlab54400a92015-02-26 18:05:51 -08001424 for intents in intentsJsonTemp:
1425 if ID == intents['id']:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001426 stateDict['state'] = intents['state']
1427 stateDict['id'] = ID
Jon Hallefbd9792015-03-05 16:11:36 -08001428 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08001429 break
Jon Hallefbd9792015-03-05 16:11:36 -08001430 if len( intentsId ) != len( dictList ):
1431 main.log.info( "Cannot find some of the intent ID state" )
kelvin-onlab07dbd012015-03-04 16:29:39 -08001432 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08001433 else:
1434 main.log.info("Invalid intents ID entry")
1435 return None
kelvin-onlab54400a92015-02-26 18:05:51 -08001436 except TypeError:
1437 main.log.exception( self.name + ": Object not as expected" )
1438 return None
1439 except pexpect.EOF:
1440 main.log.error( self.name + ": EOF exception found" )
1441 main.log.error( self.name + ": " + self.handle.before )
1442 main.cleanup()
1443 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001444 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08001445 main.log.exception( self.name + ": Uncaught exception!" )
1446 main.cleanup()
1447 main.exit()
Jon Hall30b82fa2015-03-04 17:15:43 -08001448
kelvin-onlabd3b64892015-01-20 13:26:24 -08001449 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001450 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001451 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001452 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001453 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001454 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001455 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001456 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001457 if jsonFormat:
1458 cmdStr = "flows -j"
1459 handle = self.sendline( cmdStr )
1460 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1461 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001462 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001463 cmdStr = "flows"
1464 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001465 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001466 main.log.error( self.name + ".flows() response: " +
1467 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001468 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001469 except TypeError:
1470 main.log.exception( self.name + ": Object not as expected" )
1471 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001472 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001473 main.log.error( self.name + ": EOF exception found" )
1474 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001475 main.cleanup()
1476 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001477 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001478 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001479 main.cleanup()
1480 main.exit()
1481
kelvin-onlabd3b64892015-01-20 13:26:24 -08001482 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
Jon Hallefbd9792015-03-05 16:11:36 -08001483 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001484 """
andrewonlab87852b02014-11-19 18:44:19 -05001485 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001486 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001487 a specific point-to-point intent definition
1488 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001489 * dpidSrc: specify source dpid
1490 * dpidDst: specify destination dpid
1491 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001492 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001493 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001494 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001495 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001496 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001497 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001498 """
andrewonlab87852b02014-11-19 18:44:19 -05001499 try:
kelvin8ec71442015-01-15 16:57:00 -08001500 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001501 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1502 str( numIntents )
1503 if numMult:
1504 cmd += " " + str( numMult )
1505 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001506 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001507 if appId:
1508 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001509 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001510 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1511 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001512 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001513 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001514 main.log.info( handle )
1515 # Split result by newline
1516 newline = handle.split( "\r\r\n" )
1517 # Ignore the first object of list, which is empty
1518 newline = newline[ 1: ]
1519 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001520 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001521 result = result.split( ": " )
1522 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001523 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1524 main.log.info( latResult )
1525 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001526 else:
1527 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001528 except TypeError:
1529 main.log.exception( self.name + ": Object not as expected" )
1530 return None
andrewonlab87852b02014-11-19 18:44:19 -05001531 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001532 main.log.error( self.name + ": EOF exception found" )
1533 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001534 main.cleanup()
1535 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001536 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001537 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001538 main.cleanup()
1539 main.exit()
1540
kelvin-onlabd3b64892015-01-20 13:26:24 -08001541 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001542 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001543 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001544 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001545 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001546 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001547 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001548 if jsonFormat:
1549 cmdStr = "intents-events-metrics -j"
1550 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001551 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001552 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1553 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001554 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001555 cmdStr = "intents-events-metrics"
1556 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001557 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001558 except TypeError:
1559 main.log.exception( self.name + ": Object not as expected" )
1560 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001561 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001562 main.log.error( self.name + ": EOF exception found" )
1563 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001564 main.cleanup()
1565 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001566 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001567 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001568 main.cleanup()
1569 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001570
kelvin-onlabd3b64892015-01-20 13:26:24 -08001571 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001572 """
1573 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001574 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001575 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001576 """
andrewonlab867212a2014-10-22 20:13:38 -04001577 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001578 if jsonFormat:
1579 cmdStr = "topology-events-metrics -j"
1580 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001581 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001582 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1583 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001584 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001585 cmdStr = "topology-events-metrics"
1586 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001587 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001588 except TypeError:
1589 main.log.exception( self.name + ": Object not as expected" )
1590 return None
andrewonlab867212a2014-10-22 20:13:38 -04001591 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001592 main.log.error( self.name + ": EOF exception found" )
1593 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001594 main.cleanup()
1595 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001596 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001597 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001598 main.cleanup()
1599 main.exit()
1600
kelvin8ec71442015-01-15 16:57:00 -08001601 # Wrapper functions ****************
1602 # Wrapper functions use existing driver
1603 # functions and extends their use case.
1604 # For example, we may use the output of
1605 # a normal driver function, and parse it
1606 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001607
kelvin-onlabd3b64892015-01-20 13:26:24 -08001608 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001609 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001610 Description:
1611 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001612 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001613 try:
kelvin8ec71442015-01-15 16:57:00 -08001614 # Obtain output of intents function
kelvin-onlabfb521662015-02-27 09:52:40 -08001615 intentsStr = self.intents(jsonFormat=False)
kelvin-onlabd3b64892015-01-20 13:26:24 -08001616 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001617
kelvin8ec71442015-01-15 16:57:00 -08001618 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001619 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1620 for intents in intentsList:
kelvin-onlabfb521662015-02-27 09:52:40 -08001621 match = re.search('id=0x([\da-f]+),', intents)
1622 if match:
1623 tmpId = match.group()[3:-1]
1624 intentIdList.append( tmpId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001625 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001626
Jon Halld4d4b372015-01-28 16:02:41 -08001627 except TypeError:
1628 main.log.exception( self.name + ": Object not as expected" )
1629 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001630 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001631 main.log.error( self.name + ": EOF exception found" )
1632 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001633 main.cleanup()
1634 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001635 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001636 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001637 main.cleanup()
1638 main.exit()
1639
Jon Hall30b82fa2015-03-04 17:15:43 -08001640 def FlowAddedCount( self, deviceId ):
1641 """
1642 Determine the number of flow rules for the given device id that are
1643 in the added state
1644 """
1645 try:
1646 cmdStr = "flows any " + str( deviceId ) + " | " +\
1647 "grep 'state=ADDED' | wc -l"
1648 handle = self.sendline( cmdStr )
1649 return handle
1650 except pexpect.EOF:
1651 main.log.error( self.name + ": EOF exception found" )
1652 main.log.error( self.name + ": " + self.handle.before )
1653 main.cleanup()
1654 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001655 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -08001656 main.log.exception( self.name + ": Uncaught exception!" )
1657 main.cleanup()
1658 main.exit()
1659
kelvin-onlabd3b64892015-01-20 13:26:24 -08001660 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001661 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001662 Use 'devices' function to obtain list of all devices
1663 and parse the result to obtain a list of all device
1664 id's. Returns this list. Returns empty list if no
1665 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001666 List is ordered sequentially
1667
andrewonlab3e15ead2014-10-15 14:21:34 -04001668 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001669 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001670 the ids. By obtaining the list of device ids on the fly,
1671 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001672 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001673 try:
kelvin8ec71442015-01-15 16:57:00 -08001674 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001675 devicesStr = self.devices( jsonFormat=False )
1676 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001677
kelvin-onlabd3b64892015-01-20 13:26:24 -08001678 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001679 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001680 return idList
kelvin8ec71442015-01-15 16:57:00 -08001681
1682 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001683 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001684 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001685 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001686 # Split list further into arguments before and after string
1687 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001688 # append to idList
1689 for arg in tempList:
1690 idList.append( arg.split( "id=" )[ 1 ] )
1691 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001692
Jon Halld4d4b372015-01-28 16:02:41 -08001693 except TypeError:
1694 main.log.exception( self.name + ": Object not as expected" )
1695 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04001696 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001697 main.log.error( self.name + ": EOF exception found" )
1698 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001699 main.cleanup()
1700 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001701 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001702 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001703 main.cleanup()
1704 main.exit()
1705
kelvin-onlabd3b64892015-01-20 13:26:24 -08001706 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001707 """
andrewonlab7c211572014-10-15 16:45:20 -04001708 Uses 'nodes' function to obtain list of all nodes
1709 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001710 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001711 Returns:
1712 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001713 """
andrewonlab7c211572014-10-15 16:45:20 -04001714 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001715 nodesStr = self.nodes()
1716 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001717
kelvin-onlabd3b64892015-01-20 13:26:24 -08001718 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001719 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001720 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001721
kelvin-onlabd3b64892015-01-20 13:26:24 -08001722 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001723 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001724
kelvin8ec71442015-01-15 16:57:00 -08001725 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001726 nodesList = nodesStr.split( "," )
1727 tempList = [ node for node in nodesList if "id=" in node ]
1728 for arg in tempList:
1729 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001730
kelvin-onlabd3b64892015-01-20 13:26:24 -08001731 return idList
kelvin8ec71442015-01-15 16:57:00 -08001732
Jon Halld4d4b372015-01-28 16:02:41 -08001733 except TypeError:
1734 main.log.exception( self.name + ": Object not as expected" )
1735 return None
andrewonlab7c211572014-10-15 16:45:20 -04001736 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001737 main.log.error( self.name + ": EOF exception found" )
1738 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001739 main.cleanup()
1740 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001741 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001742 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001743 main.cleanup()
1744 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001745
kelvin-onlabd3b64892015-01-20 13:26:24 -08001746 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001747 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001748 Return the first device from the devices api whose 'id' contains 'dpid'
1749 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001750 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001751 try:
kelvin8ec71442015-01-15 16:57:00 -08001752 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001753 return None
1754 else:
kelvin8ec71442015-01-15 16:57:00 -08001755 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001756 rawDevices = self.devices()
1757 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001758 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001759 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001760 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1761 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001762 return device
1763 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001764 except TypeError:
1765 main.log.exception( self.name + ": Object not as expected" )
1766 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001767 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001768 main.log.error( self.name + ": EOF exception found" )
1769 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001770 main.cleanup()
1771 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001772 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001773 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001774 main.cleanup()
1775 main.exit()
1776
kelvin-onlabd3b64892015-01-20 13:26:24 -08001777 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001778 """
Jon Hallefbd9792015-03-05 16:11:36 -08001779 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001780 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001781 log level can be specified.
kelvin8ec71442015-01-15 16:57:00 -08001782
Jon Hall42db6dc2014-10-24 19:03:48 -04001783 Params: ip = ip used for the onos cli
1784 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001785 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001786 logLevel = level to log to. Currently accepts
1787 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001788
1789
kelvin-onlabd3b64892015-01-20 13:26:24 -08001790 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001791
Jon Hallefbd9792015-03-05 16:11:36 -08001792 Returns: main.TRUE if the number of switches and links are correct,
1793 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04001794 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001795 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001796 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001797 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001798 if topology == {}:
1799 return main.ERROR
1800 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001801 # Is the number of switches is what we expected
1802 devices = topology.get( 'devices', False )
1803 links = topology.get( 'links', False )
kelvin-onlabfb521662015-02-27 09:52:40 -08001804 if devices is False or links is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04001805 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001806 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001807 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001808 linkCheck = ( int( links ) == int( numolink ) )
1809 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001810 # We expected the correct numbers
Jon Hallefbd9792015-03-05 16:11:36 -08001811 output += "The number of links and switches match " +\
1812 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001813 result = main.TRUE
1814 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001815 output += "The number of links and switches does not match " +\
1816 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001817 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001818 output = output + "\n ONOS sees %i devices (%i expected) \
1819 and %i links (%i expected)" % (
1820 int( devices ), int( numoswitch ), int( links ),
1821 int( numolink ) )
1822 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001823 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001824 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001825 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001826 else:
kelvin8ec71442015-01-15 16:57:00 -08001827 main.log.info( output )
1828 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001829 except TypeError:
1830 main.log.exception( self.name + ": Object not as expected" )
1831 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001832 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001833 main.log.error( self.name + ": EOF exception found" )
1834 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001835 main.cleanup()
1836 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001837 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001838 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001839 main.cleanup()
1840 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001841
kelvin-onlabd3b64892015-01-20 13:26:24 -08001842 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001843 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001844 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001845 deviceId must be the id of a device as seen in the onos devices command
1846 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001847 role must be either master, standby, or none
1848
Jon Halle3f39ff2015-01-13 11:50:53 -08001849 Returns:
1850 main.TRUE or main.FALSE based on argument verification and
1851 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001852 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001853 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001854 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001855 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001856 cmdStr = "device-role " +\
1857 str( deviceId ) + " " +\
1858 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001859 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001860 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001861 if re.search( "Error", handle ):
1862 # end color output to escape any colours
1863 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001864 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001865 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001866 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001867 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001868 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001869 main.log.error( "Invalid 'role' given to device_role(). " +
1870 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001871 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001872 except TypeError:
1873 main.log.exception( self.name + ": Object not as expected" )
1874 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001875 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001876 main.log.error( self.name + ": EOF exception found" )
1877 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001878 main.cleanup()
1879 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001880 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001881 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001882 main.cleanup()
1883 main.exit()
1884
kelvin-onlabd3b64892015-01-20 13:26:24 -08001885 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001886 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001887 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001888 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001889 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001890 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001891 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001892 if jsonFormat:
1893 cmdStr = "clusters -j"
1894 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001895 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001896 handle variable here contains some ANSI escape color code
1897 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001898 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001899 function. The repr( handle ) output when printed shows the ANSI
1900 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001901 variable is actually repr( somestring ) and json.loads would
1902 fail with the escape sequence. So we take off that escape
1903 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001904
kelvin-onlabd3b64892015-01-20 13:26:24 -08001905 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1906 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001907 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001908 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1909 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001910 return handle1
1911 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001912 cmdStr = "clusters"
1913 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001914 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001915 except TypeError:
1916 main.log.exception( self.name + ": Object not as expected" )
1917 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001918 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001919 main.log.error( self.name + ": EOF exception found" )
1920 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001921 main.cleanup()
1922 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001923 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001924 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001925 main.cleanup()
1926 main.exit()
1927
kelvin-onlabd3b64892015-01-20 13:26:24 -08001928 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001929 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001930 CLI command to get the current leader for the Election test application
1931 NOTE: Requires installation of the onos-app-election feature
1932 Returns: Node IP of the leader if one exists
1933 None if none exists
1934 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001935 """
Jon Hall94fd0472014-12-08 11:52:42 -08001936 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001937 cmdStr = "election-test-leader"
1938 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001939 # Leader
1940 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001941 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001942 nodeSearch = re.search( leaderPattern, response )
1943 if nodeSearch:
1944 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001945 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001946 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001947 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001948 # no leader
1949 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001950 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001951 nullSearch = re.search( nullPattern, response )
1952 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001953 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001954 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001955 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001956 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001957 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001958 if re.search( errorPattern, response ):
1959 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001960 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001961 return main.FALSE
1962 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001963 main.log.error( "Error in election_test_leader: " +
1964 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001965 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001966 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001967 except TypeError:
1968 main.log.exception( self.name + ": Object not as expected" )
1969 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001970 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001971 main.log.error( self.name + ": EOF exception found" )
1972 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001973 main.cleanup()
1974 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001975 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001976 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001977 main.cleanup()
1978 main.exit()
1979
kelvin-onlabd3b64892015-01-20 13:26:24 -08001980 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001981 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001982 CLI command to run for leadership of the Election test application.
1983 NOTE: Requires installation of the onos-app-election feature
1984 Returns: Main.TRUE on success
1985 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001986 """
Jon Hall94fd0472014-12-08 11:52:42 -08001987 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001988 cmdStr = "election-test-run"
1989 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001990 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001991 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001992 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001993 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001994 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001995 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001996 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001997 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001998 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001999 errorPattern = "Command\snot\sfound"
2000 if re.search( errorPattern, response ):
2001 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002002 return main.FALSE
2003 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002004 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002005 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002006 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002007 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002008 except TypeError:
2009 main.log.exception( self.name + ": Object not as expected" )
2010 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002011 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002012 main.log.error( self.name + ": EOF exception found" )
2013 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002014 main.cleanup()
2015 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002016 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002017 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002018 main.cleanup()
2019 main.exit()
2020
kelvin-onlabd3b64892015-01-20 13:26:24 -08002021 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08002022 """
Jon Hall94fd0472014-12-08 11:52:42 -08002023 * CLI command to withdraw the local node from leadership election for
2024 * the Election test application.
2025 #NOTE: Requires installation of the onos-app-election feature
2026 Returns: Main.TRUE on success
2027 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08002028 """
Jon Hall94fd0472014-12-08 11:52:42 -08002029 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002030 cmdStr = "election-test-withdraw"
2031 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002032 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002033 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002034 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002035 if re.search( successPattern, response ):
2036 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002037 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002038 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002039 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002040 errorPattern = "Command\snot\sfound"
2041 if re.search( errorPattern, response ):
2042 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002043 return main.FALSE
2044 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002045 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002046 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002047 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002048 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002049 except TypeError:
2050 main.log.exception( self.name + ": Object not as expected" )
2051 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002052 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002053 main.log.error( self.name + ": EOF exception found" )
2054 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002055 main.cleanup()
2056 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002057 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002058 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002059 main.cleanup()
2060 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002061
kelvin8ec71442015-01-15 16:57:00 -08002062 def getDevicePortsEnabledCount( self, dpid ):
2063 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002064 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002065 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002066 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08002067 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002068 cmdStr = "onos:ports -e " + dpid + " | wc -l"
2069 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002070 if re.search( "No such device", output ):
2071 main.log.error( "Error in getting ports" )
2072 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002073 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002074 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002075 except TypeError:
2076 main.log.exception( self.name + ": Object not as expected" )
2077 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002078 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002079 main.log.error( self.name + ": EOF exception found" )
2080 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002081 main.cleanup()
2082 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002083 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002084 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002085 main.cleanup()
2086 main.exit()
2087
kelvin8ec71442015-01-15 16:57:00 -08002088 def getDeviceLinksActiveCount( self, dpid ):
2089 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002090 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002091 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002092 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002093 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002094 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
2095 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002096 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002097 main.log.error( "Error in getting ports " )
2098 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002099 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002100 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002101 except TypeError:
2102 main.log.exception( self.name + ": Object not as expected" )
2103 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002104 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002105 main.log.error( self.name + ": EOF exception found" )
2106 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002107 main.cleanup()
2108 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002109 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002110 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002111 main.cleanup()
2112 main.exit()
2113
kelvin8ec71442015-01-15 16:57:00 -08002114 def getAllIntentIds( self ):
2115 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002116 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002117 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002118 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002119 cmdStr = "onos:intents | grep id="
2120 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002121 if re.search( "Error", output ):
2122 main.log.error( "Error in getting ports" )
2123 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002124 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002125 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002126 except TypeError:
2127 main.log.exception( self.name + ": Object not as expected" )
2128 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002129 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002130 main.log.error( self.name + ": EOF exception found" )
2131 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002132 main.cleanup()
2133 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002134 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002135 main.log.exception( self.name + ": Uncaught exception!" )
2136 main.cleanup()
2137 main.exit()
2138
Jon Hall73509952015-02-24 16:42:56 -08002139 def intentSummary( self ):
2140 """
Jon Hallefbd9792015-03-05 16:11:36 -08002141 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08002142 """
2143 try:
2144 intents = self.intents( )
2145 intentStates = []
2146 out = []
2147 for intent in json.loads( intents ):
2148 intentStates.append( intent.get( 'state', None ) )
2149 for i in set( intentStates ):
2150 out.append( (i, intentStates.count( i ) ) )
2151 return dict( out )
2152 except TypeError:
2153 main.log.exception( self.name + ": Object not as expected" )
2154 return None
2155 except pexpect.EOF:
2156 main.log.error( self.name + ": EOF exception found" )
2157 main.log.error( self.name + ": " + self.handle.before )
2158 main.cleanup()
2159 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002160 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08002161 main.log.exception( self.name + ": Uncaught exception!" )
2162 main.cleanup()
2163 main.exit()