blob: 353903e69cf167fefab075303bc3c76d3ef033a7 [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 ]
andrew@onlab.us658ec012015-03-11 15:13:09 -070046 self.home = "~/onos"
andrewonlab95ce8322014-10-13 14:12:04 -040047 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"
andrewonlab95ce8322014-10-13 14:12:04 -040053
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
Cameron Franke9c94fb02015-01-21 10:20:20 -0800150 self.handle.expect(str(cellname))
andrew@onlab.usc400b112015-01-21 15:33:19 -0800151 handleBefore = self.handle.before
152 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800153 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800154 self.handle.sendline("")
155 self.handle.expect("\$")
andrew@onlab.usc400b112015-01-21 15:33:19 -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!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400284 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 )
Jon Hall63604932015-02-26 17:09:50 -0800300 i = self.handle.expect( ["onos>", "\$", pexpect.TIMEOUT] )
301 response = self.handle.before
302 if i == 2:
303 self.handle.sendline()
304 self.handle.expect( "\$" )
305 response += self.handle.before
306 print response
307 try:
308 print self.handle.after
309 except:
310 pass
311 # TODO: do something with i
kelvin-onlabd3b64892015-01-20 13:26:24 -0800312 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
kelvin-onlab898a6c62015-01-16 14:13:53 -0800313 + self.name + "." )
Jon Hall7bdfc122015-01-23 11:45:32 -0800314 # Remove control strings from output
kelvin-onlabd3b64892015-01-20 13:26:24 -0800315 ansiEscape = re.compile( r'\x1b[^m]*m' )
Jon Hall63604932015-02-26 17:09:50 -0800316 response = ansiEscape.sub( '', response )
kelvin-onlabfb521662015-02-27 09:52:40 -0800317 # Remove extra return chars that get added
Jon Hall63604932015-02-26 17:09:50 -0800318 response = re.sub( r"\s\r", "", response )
319 response = response.strip()
320 # parse for just the output, remove the cmd from response
321 output = response.split( cmdStr, 1 )[1]
Jon Hall7bdfc122015-01-23 11:45:32 -0800322 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800323 except TypeError:
324 main.log.exception( self.name + ": Object not as expected" )
325 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400326 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800327 main.log.error( self.name + ": EOF exception found" )
328 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400329 main.cleanup()
330 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800331 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800332 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400333 main.cleanup()
334 main.exit()
335
kelvin8ec71442015-01-15 16:57:00 -0800336 # IMPORTANT NOTE:
337 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800338 # the cli command changing 'a:b' with 'aB'.
339 # Ex ) onos:topology > onosTopology
340 # onos:links > onosLinks
341 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800342
kelvin-onlabd3b64892015-01-20 13:26:24 -0800343 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800344 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400345 Adds a new cluster node by ID and address information.
346 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800347 * nodeId
348 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400349 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800350 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800351 """
Jon Hallefbd9792015-03-05 16:11:36 -0800352 # noinspection PyBroadException
andrewonlabc2d05aa2014-10-13 16:51:10 -0400353 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800354 cmdStr = "add-node " + str( nodeId ) + " " +\
355 str( ONOSIp ) + " " + str( tcpPort )
356 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800357 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800358 main.log.error( "Error in adding node" )
359 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800360 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400361 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800362 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400363 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800364 except TypeError:
365 main.log.exception( self.name + ": Object not as expected" )
366 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400367 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800368 main.log.error( self.name + ": EOF exception found" )
369 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400370 main.cleanup()
371 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800372 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800373 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400374 main.cleanup()
375 main.exit()
376
kelvin-onlabd3b64892015-01-20 13:26:24 -0800377 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800378 """
andrewonlab86dc3082014-10-13 18:18:38 -0400379 Removes a cluster by ID
380 Issues command: 'remove-node [<node-id>]'
381 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800382 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800383 """
andrewonlab86dc3082014-10-13 18:18:38 -0400384 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400385
kelvin-onlabd3b64892015-01-20 13:26:24 -0800386 cmdStr = "remove-node " + str( nodeId )
387 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800388 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400389
390 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800391
Jon Halld4d4b372015-01-28 16:02:41 -0800392 except TypeError:
393 main.log.exception( self.name + ": Object not as expected" )
394 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400395 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800396 main.log.error( self.name + ": EOF exception found" )
397 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400398 main.cleanup()
399 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800400 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800401 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400402 main.cleanup()
403 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400404
kelvin8ec71442015-01-15 16:57:00 -0800405 def nodes( self ):
406 """
andrewonlab7c211572014-10-15 16:45:20 -0400407 List the nodes currently visible
408 Issues command: 'nodes'
409 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800410 """
andrewonlab7c211572014-10-15 16:45:20 -0400411 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800412 cmdStr = "nodes"
413 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400414 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800415 except TypeError:
416 main.log.exception( self.name + ": Object not as expected" )
417 return None
andrewonlab7c211572014-10-15 16:45:20 -0400418 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800419 main.log.error( self.name + ": EOF exception found" )
420 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400421 main.cleanup()
422 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800423 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800424 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400425 main.cleanup()
426 main.exit()
427
kelvin8ec71442015-01-15 16:57:00 -0800428 def topology( self ):
429 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700430 Definition:
431 Returns the ouput of topology command.
432 Return:
433 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -0800434 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700435 import json
andrewonlab95ce8322014-10-13 14:12:04 -0400436 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800437 # either onos:topology or 'topology' will work in CLI
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700438 cmdStr = "topology -j"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800439 handle = self.sendline( cmdStr )
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700440 main.log.info( "topology -j returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400441 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800442 except TypeError:
443 main.log.exception( self.name + ": Object not as expected" )
444 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400445 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800446 main.log.error( self.name + ": EOF exception found" )
447 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400448 main.cleanup()
449 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800450 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800451 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400452 main.cleanup()
453 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800454
kelvin-onlabd3b64892015-01-20 13:26:24 -0800455 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800456 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800457 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400458 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800459 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400460 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800461 cmdStr = "feature:install " + str( featureStr )
462 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800463 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400464 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800465 except TypeError:
466 main.log.exception( self.name + ": Object not as expected" )
467 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400468 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800469 main.log.error( self.name + ": EOF exception found" )
470 main.log.error( self.name + ": " + self.handle.before )
471 main.log.report( "Failed to install feature" )
472 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400473 main.cleanup()
474 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800475 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800476 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800477 main.log.report( "Failed to install feature" )
478 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400479 main.cleanup()
480 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800481
kelvin-onlabd3b64892015-01-20 13:26:24 -0800482 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800483 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400484 Uninstalls a specified feature
485 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800486 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400487 try:
Jon Hall30b82fa2015-03-04 17:15:43 -0800488 cmdStr = 'feature:list -i | grep "' + featureStr + '"'
489 handle = self.sendline( cmdStr )
490 if handle != '':
491 cmdStr = "feature:uninstall " + str( featureStr )
492 self.sendline( cmdStr )
493 # TODO: Check for possible error responses from karaf
494 else:
Jon Hallefbd9792015-03-05 16:11:36 -0800495 main.log.info( "Feature needs to be installed before " +
496 "uninstalling it" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400497 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800498 except TypeError:
499 main.log.exception( self.name + ": Object not as expected" )
500 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400501 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800502 main.log.error( self.name + ": EOF exception found" )
503 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400504 main.cleanup()
505 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800506 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800507 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400508 main.cleanup()
509 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800510
jenkins7ead5a82015-03-13 10:28:21 -0700511 def deviceRemove( self, deviceId ):
512 """
513 Removes particular device from storage
514
515 TODO: refactor this function
516 """
517 try:
518 cmdStr = "device-remove "+str(deviceId)
519 handle = self.sendline( cmdStr )
520 return main.TRUE
521 except TypeError:
522 main.log.exception( self.name + ": Object not as expected" )
523 return None
524 except pexpect.EOF:
525 main.log.error( self.name + ": EOF exception found" )
526 main.log.error( self.name + ": " + self.handle.before )
527 main.cleanup()
528 main.exit()
529 except Exception:
530 main.log.exception( self.name + ": Uncaught exception!" )
531 main.cleanup()
532 main.exit()
533
534
535
kelvin-onlabd3b64892015-01-20 13:26:24 -0800536 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800537 """
Jon Hall7b02d952014-10-17 20:14:54 -0400538 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400539 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800540 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800541 """
andrewonlab86dc3082014-10-13 18:18:38 -0400542 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800543 if jsonFormat:
544 cmdStr = "devices -j"
545 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800546 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800547 handle variable here contains some ANSI escape color code
548 sequences at the end which are invisible in the print command
549 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800550 function. The repr( handle ) output when printed shows the
551 ANSI escape sequences. In json.loads( somestring ), this
552 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800553 json.loads would fail with the escape sequence. So we take off
554 that escape sequence using:
555
kelvin-onlabd3b64892015-01-20 13:26:24 -0800556 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
557 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800558 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800559 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
560 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400561 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400562 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800563 cmdStr = "devices"
564 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400565 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800566 except TypeError:
567 main.log.exception( self.name + ": Object not as expected" )
568 return None
andrewonlab7c211572014-10-15 16:45:20 -0400569 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800570 main.log.error( self.name + ": EOF exception found" )
571 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400572 main.cleanup()
573 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800574 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800575 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400576 main.cleanup()
577 main.exit()
578
kelvin-onlabd3b64892015-01-20 13:26:24 -0800579 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800580 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800581 This balances the devices across all controllers
582 by issuing command: 'onos> onos:balance-masters'
583 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800584 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800585 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800586 cmdStr = "onos:balance-masters"
587 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800588 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800589 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800590 except TypeError:
591 main.log.exception( self.name + ": Object not as expected" )
592 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800593 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800594 main.log.error( self.name + ": EOF exception found" )
595 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800596 main.cleanup()
597 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800598 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800599 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800600 main.cleanup()
601 main.exit()
602
kelvin-onlabd3b64892015-01-20 13:26:24 -0800603 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800604 """
Jon Halle8217482014-10-17 13:49:14 -0400605 Lists all core links
606 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800607 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800608 """
Jon Halle8217482014-10-17 13:49:14 -0400609 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800610 if jsonFormat:
611 cmdStr = "links -j"
612 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800613 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800614 handle variable here contains some ANSI escape color code
615 sequences at the end which are invisible in the print command
616 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800617 function. The repr( handle ) output when printed shows the ANSI
618 escape sequences. In json.loads( somestring ), this somestring
619 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800620 fail with the escape sequence. So we take off that escape
621 sequence using:
622
kelvin-onlabd3b64892015-01-20 13:26:24 -0800623 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
624 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800625 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800626 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
627 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400628 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400629 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800630 cmdStr = "links"
631 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400632 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800633 except TypeError:
634 main.log.exception( self.name + ": Object not as expected" )
635 return None
Jon Halle8217482014-10-17 13:49:14 -0400636 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800637 main.log.error( self.name + ": EOF exception found" )
638 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400639 main.cleanup()
640 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800641 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800642 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400643 main.cleanup()
644 main.exit()
645
kelvin-onlabd3b64892015-01-20 13:26:24 -0800646 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800647 """
Jon Halle8217482014-10-17 13:49:14 -0400648 Lists all ports
649 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800650 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800651 """
Jon Halle8217482014-10-17 13:49:14 -0400652 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800653 if jsonFormat:
654 cmdStr = "ports -j"
655 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800656 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800657 handle variable here contains some ANSI escape color code
658 sequences at the end which are invisible in the print command
659 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800660 function. The repr( handle ) output when printed shows the ANSI
661 escape sequences. In json.loads( somestring ), this somestring
662 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800663 fail with the escape sequence. So we take off that escape
Jon Hallefbd9792015-03-05 16:11:36 -0800664 sequence using the following commands:
Jon Halle3f39ff2015-01-13 11:50:53 -0800665
kelvin-onlabd3b64892015-01-20 13:26:24 -0800666 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
667 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800668 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800669 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
670 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400671 return handle1
672
Jon Halle8217482014-10-17 13:49:14 -0400673 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800674 cmdStr = "ports"
675 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800676 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800677 except TypeError:
678 main.log.exception( self.name + ": Object not as expected" )
679 return None
Jon Halle8217482014-10-17 13:49:14 -0400680 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800681 main.log.error( self.name + ": EOF exception found" )
682 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400683 main.cleanup()
684 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800685 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800686 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400687 main.cleanup()
688 main.exit()
689
kelvin-onlabd3b64892015-01-20 13:26:24 -0800690 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800691 """
Jon Hall983a1702014-10-28 18:44:22 -0400692 Lists all devices and the controllers with roles assigned to them
693 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800694 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800695 """
andrewonlab7c211572014-10-15 16:45:20 -0400696 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800697 if jsonFormat:
698 cmdStr = "roles -j"
699 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800700 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800701 handle variable here contains some ANSI escape color code
702 sequences at the end which are invisible in the print command
703 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800704 function. The repr( handle ) output when printed shows the ANSI
705 escape sequences. In json.loads( somestring ), this somestring
706 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800707 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500708
Jon Halle3f39ff2015-01-13 11:50:53 -0800709 So we take off that escape sequence using the following
710 commads:
711
kelvin-onlabd3b64892015-01-20 13:26:24 -0800712 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
713 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800714 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800715 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
716 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400717 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400718
andrewonlab7c211572014-10-15 16:45:20 -0400719 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800720 cmdStr = "roles"
721 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800722 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800723 except TypeError:
724 main.log.exception( self.name + ": Object not as expected" )
725 return None
Jon Hall983a1702014-10-28 18:44:22 -0400726 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800727 main.log.error( self.name + ": EOF exception found" )
728 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400729 main.cleanup()
730 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800731 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800732 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400733 main.cleanup()
734 main.exit()
735
kelvin-onlabd3b64892015-01-20 13:26:24 -0800736 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800737 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800738 Given the a string containing the json representation of the "roles"
739 cli command and a partial or whole device id, returns a json object
740 containing the roles output for the first device whose id contains
741 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400742
743 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800744 A dict of the role assignments for the given device or
745 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800746 """
Jon Hall983a1702014-10-28 18:44:22 -0400747 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800748 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400749 return None
750 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800751 rawRoles = self.roles()
752 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800753 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800754 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800755 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800756 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400757 return device
758 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800759 except TypeError:
760 main.log.exception( self.name + ": Object not as expected" )
761 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400762 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800763 main.log.error( self.name + ": EOF exception found" )
764 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400765 main.cleanup()
766 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800767 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800768 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400769 main.cleanup()
770 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800771
kelvin-onlabd3b64892015-01-20 13:26:24 -0800772 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800773 """
Jon Hall94fd0472014-12-08 11:52:42 -0800774 Iterates through each device and checks if there is a master assigned
775 Returns: main.TRUE if each device has a master
776 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800777 """
Jon Hall94fd0472014-12-08 11:52:42 -0800778 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800779 rawRoles = self.roles()
780 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800781 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800782 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800783 # print device
784 if device[ 'master' ] == "none":
785 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800786 return main.FALSE
787 return main.TRUE
788
Jon Halld4d4b372015-01-28 16:02:41 -0800789 except TypeError:
790 main.log.exception( self.name + ": Object not as expected" )
791 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800792 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800793 main.log.error( self.name + ": EOF exception found" )
794 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800795 main.cleanup()
796 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800797 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800798 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800799 main.cleanup()
800 main.exit()
801
kelvin-onlabd3b64892015-01-20 13:26:24 -0800802 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800803 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400804 Returns string of paths, and the cost.
805 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800806 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400807 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800808 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
809 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800810 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800811 main.log.error( "Error in getting paths" )
812 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400813 else:
kelvin8ec71442015-01-15 16:57:00 -0800814 path = handle.split( ";" )[ 0 ]
815 cost = handle.split( ";" )[ 1 ]
816 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800817 except TypeError:
818 main.log.exception( self.name + ": Object not as expected" )
819 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400820 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800821 main.log.error( self.name + ": EOF exception found" )
822 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400823 main.cleanup()
824 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800825 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800826 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400827 main.cleanup()
828 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800829
kelvin-onlabd3b64892015-01-20 13:26:24 -0800830 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800831 """
Jon Hallffb386d2014-11-21 13:43:38 -0800832 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400833 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800834 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800835 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400836 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800837 if jsonFormat:
838 cmdStr = "hosts -j"
839 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800840 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800841 handle variable here contains some ANSI escape color code
842 sequences at the end which are invisible in the print command
843 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800844 function. The repr( handle ) output when printed shows the ANSI
845 escape sequences. In json.loads( somestring ), this somestring
846 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800847 fail with the escape sequence. So we take off that escape
848 sequence using:
849
kelvin-onlabd3b64892015-01-20 13:26:24 -0800850 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
851 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800852 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800853 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
854 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400855 return handle1
856 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800857 cmdStr = "hosts"
858 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400859 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800860 except TypeError:
861 main.log.exception( self.name + ": Object not as expected" )
862 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400863 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800864 main.log.error( self.name + ": EOF exception found" )
865 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400866 main.cleanup()
867 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800868 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800869 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400870 main.cleanup()
871 main.exit()
872
kelvin-onlabd3b64892015-01-20 13:26:24 -0800873 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800874 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400875 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800876
Jon Hallefbd9792015-03-05 16:11:36 -0800877 Note: mac must be a colon separated mac address, but could be a
Jon Halle3f39ff2015-01-13 11:50:53 -0800878 partial mac address
879
Jon Hall42db6dc2014-10-24 19:03:48 -0400880 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800881 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400882 try:
kelvin8ec71442015-01-15 16:57:00 -0800883 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400884 return None
885 else:
886 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800887 rawHosts = self.hosts()
888 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800889 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800890 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800891 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800892 if not host:
893 pass
894 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400895 return host
896 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800897 except TypeError:
898 main.log.exception( self.name + ": Object not as expected" )
899 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400900 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800901 main.log.error( self.name + ": EOF exception found" )
902 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400903 main.cleanup()
904 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800905 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800906 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400907 main.cleanup()
908 main.exit()
909
kelvin-onlabd3b64892015-01-20 13:26:24 -0800910 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800911 """
912 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400913 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800914
andrewonlab3f0a4af2014-10-17 12:25:14 -0400915 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800916 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400917 IMPORTANT:
918 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800919 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400920 Furthermore, it assumes that value of VLAN is '-1'
921 Description:
kelvin8ec71442015-01-15 16:57:00 -0800922 Converts mininet hosts ( h1, h2, h3... ) into
923 ONOS format ( 00:00:00:00:00:01/-1 , ... )
924 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400925 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800926 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400927
kelvin-onlabd3b64892015-01-20 13:26:24 -0800928 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800929 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800930 hostHex = hex( int( host ) ).zfill( 12 )
931 hostHex = str( hostHex ).replace( 'x', '0' )
932 i = iter( str( hostHex ) )
933 hostHex = ":".join( a + b for a, b in zip( i, i ) )
934 hostHex = hostHex + "/-1"
935 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400936
kelvin-onlabd3b64892015-01-20 13:26:24 -0800937 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400938
Jon Halld4d4b372015-01-28 16:02:41 -0800939 except TypeError:
940 main.log.exception( self.name + ": Object not as expected" )
941 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400942 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800943 main.log.error( self.name + ": EOF exception found" )
944 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400945 main.cleanup()
946 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800947 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800948 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400949 main.cleanup()
950 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400951
kelvin-onlabd3b64892015-01-20 13:26:24 -0800952 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800953 """
andrewonlabe6745342014-10-17 14:29:13 -0400954 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800955 * hostIdOne: ONOS host id for host1
956 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400957 Description:
Jon Hallefbd9792015-03-05 16:11:36 -0800958 Adds a host-to-host intent ( bidirectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500959 specifying the two hosts.
kelvin-onlabfb521662015-02-27 09:52:40 -0800960 Returns:
961 A string of the intent id or None on Error
kelvin8ec71442015-01-15 16:57:00 -0800962 """
andrewonlabe6745342014-10-17 14:29:13 -0400963 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800964 cmdStr = "add-host-intent " + str( hostIdOne ) +\
965 " " + str( hostIdTwo )
966 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800967 if re.search( "Error", handle ):
968 main.log.error( "Error in adding Host intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800969 return None
Hari Krishnaac4e1782015-01-26 12:09:12 -0800970 else:
971 main.log.info( "Host intent installed between " +
kelvin-onlabfb521662015-02-27 09:52:40 -0800972 str( hostIdOne ) + " and " + str( hostIdTwo ) )
973 match = re.search('id=0x([\da-f]+),', handle)
974 if match:
975 return match.group()[3:-1]
976 else:
977 main.log.error( "Error, intent ID not found" )
978 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800979 except TypeError:
980 main.log.exception( self.name + ": Object not as expected" )
981 return None
andrewonlabe6745342014-10-17 14:29:13 -0400982 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800983 main.log.error( self.name + ": EOF exception found" )
984 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400985 main.cleanup()
986 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800987 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800988 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400989 main.cleanup()
990 main.exit()
991
kelvin-onlabd3b64892015-01-20 13:26:24 -0800992 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800993 """
andrewonlab7b31d232014-10-24 13:31:47 -0400994 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800995 * ingressDevice: device id of ingress device
996 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400997 Optional:
998 TODO: Still needs to be implemented via dev side
kelvin-onlabfb521662015-02-27 09:52:40 -0800999 Description:
1000 Adds an optical intent by specifying an ingress and egress device
1001 Returns:
1002 A string of the intent id or None on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001003 """
andrewonlab7b31d232014-10-24 13:31:47 -04001004 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001005 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
1006 " " + str( egressDevice )
1007 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001008 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -08001009 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001010 main.log.error( "Error in adding Optical intent" )
1011 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001012 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001013 main.log.info( "Optical intent installed between " +
1014 str( ingressDevice ) + " and " +
1015 str( egressDevice ) )
1016 match = re.search('id=0x([\da-f]+),', handle)
1017 if match:
1018 return match.group()[3:-1]
1019 else:
1020 main.log.error( "Error, intent ID not found" )
1021 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001022 except TypeError:
1023 main.log.exception( self.name + ": Object not as expected" )
1024 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001025 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001026 main.log.error( self.name + ": EOF exception found" )
1027 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -04001028 main.cleanup()
1029 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001030 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001031 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -04001032 main.cleanup()
1033 main.exit()
1034
kelvin-onlabd3b64892015-01-20 13:26:24 -08001035 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001036 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001037 ingressDevice,
1038 egressDevice,
1039 portIngress="",
1040 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001041 ethType="",
1042 ethSrc="",
1043 ethDst="",
1044 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001045 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001046 ipProto="",
1047 ipSrc="",
1048 ipDst="",
1049 tcpSrc="",
1050 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001051 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001052 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001053 * ingressDevice: device id of ingress device
1054 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001055 Optional:
1056 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001057 * ethSrc: specify ethSrc ( i.e. src mac addr )
1058 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001059 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001060 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001061 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001062 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001063 * ipSrc: specify ip source address
1064 * ipDst: specify ip destination address
1065 * tcpSrc: specify tcp source port
1066 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001067 Description:
kelvin8ec71442015-01-15 16:57:00 -08001068 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001069 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001070 Returns:
1071 A string of the intent id or None on error
andrewonlab289e4b72014-10-21 21:24:18 -04001072
Jon Halle3f39ff2015-01-13 11:50:53 -08001073 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001074 options developers provide for point-to-point
1075 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001076 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001077 try:
kelvin8ec71442015-01-15 16:57:00 -08001078 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001079 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001080 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001081 and not ipProto and not ipSrc and not ipDst \
1082 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001083 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001084
andrewonlab289e4b72014-10-21 21:24:18 -04001085 else:
andrewonlab36af3822014-11-18 17:48:18 -05001086 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001087
andrewonlab0c0a6772014-10-22 12:31:18 -04001088 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001089 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001090 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001091 cmd += " --ethSrc " + str( ethSrc )
1092 if ethDst:
1093 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001094 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001095 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001096 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001097 cmd += " --lambda "
1098 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001099 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001100 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001101 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001102 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001103 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001104 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001105 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001106 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001107 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001108
kelvin8ec71442015-01-15 16:57:00 -08001109 # Check whether the user appended the port
1110 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001111 if "/" in ingressDevice:
1112 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001113 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001114 if not portIngress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001115 main.log.error( "You must specify the ingress port" )
kelvin8ec71442015-01-15 16:57:00 -08001116 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001117 # Would it make sense to throw an exception and exit
1118 # the test?
1119 return None
andrewonlab36af3822014-11-18 17:48:18 -05001120
kelvin8ec71442015-01-15 16:57:00 -08001121 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001122 str( ingressDevice ) + "/" +\
1123 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001124
kelvin-onlabd3b64892015-01-20 13:26:24 -08001125 if "/" in egressDevice:
1126 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001127 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001128 if not portEgress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001129 main.log.error( "You must specify the egress port" )
1130 return None
Jon Halle3f39ff2015-01-13 11:50:53 -08001131
kelvin8ec71442015-01-15 16:57:00 -08001132 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001133 str( egressDevice ) + "/" +\
1134 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001135
kelvin-onlab898a6c62015-01-16 14:13:53 -08001136 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001137 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001138 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001139 main.log.error( "Error in adding point-to-point intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001140 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001141 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001142 # TODO: print out all the options in this message?
1143 main.log.info( "Point-to-point intent installed between " +
1144 str( ingressDevice ) + " and " +
1145 str( egressDevice ) )
1146 match = re.search('id=0x([\da-f]+),', handle)
1147 if match:
1148 return match.group()[3:-1]
1149 else:
1150 main.log.error( "Error, intent ID not found" )
1151 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001152 except TypeError:
1153 main.log.exception( self.name + ": Object not as expected" )
1154 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001155 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001156 main.log.error( self.name + ": EOF exception found" )
1157 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001158 main.cleanup()
1159 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001160 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001161 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001162 main.cleanup()
1163 main.exit()
1164
kelvin-onlabd3b64892015-01-20 13:26:24 -08001165 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001166 self,
shahshreyac2f97072015-03-19 17:04:29 -07001167 ingressDeviceList,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001168 egressDevice,
shahshreyac2f97072015-03-19 17:04:29 -07001169 portIngressList=None,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001170 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001171 ethType="",
1172 ethSrc="",
1173 ethDst="",
1174 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001175 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001176 ipProto="",
1177 ipSrc="",
1178 ipDst="",
1179 tcpSrc="",
1180 tcpDst="",
1181 setEthSrc="",
1182 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001183 """
shahshreyad0c80432014-12-04 16:56:05 -08001184 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001185 This function assumes that there would be 2 ingress devices and
1186 one egress device. For more number of ingress devices, this
1187 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001188 Required:
shahshreyac2f97072015-03-19 17:04:29 -07001189 * ingressDeviceList: List of device ids of ingress device
1190 ( Atleast 2 ingress devices required in the list )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001191 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001192 Optional:
1193 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001194 * ethSrc: specify ethSrc ( i.e. src mac addr )
1195 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001196 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001197 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001198 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001199 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001200 * ipSrc: specify ip source address
1201 * ipDst: specify ip destination address
1202 * tcpSrc: specify tcp source port
1203 * tcpDst: specify tcp destination port
1204 * setEthSrc: action to Rewrite Source MAC Address
1205 * setEthDst: action to Rewrite Destination MAC Address
1206 Description:
kelvin8ec71442015-01-15 16:57:00 -08001207 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001208 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001209 Returns:
1210 A string of the intent id or None on error
shahshreyad0c80432014-12-04 16:56:05 -08001211
Jon Halle3f39ff2015-01-13 11:50:53 -08001212 NOTE: This function may change depending on the
Jon Hallefbd9792015-03-05 16:11:36 -08001213 options developers provide for multipoint-to-singlepoint
shahshreyad0c80432014-12-04 16:56:05 -08001214 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001215 """
shahshreyad0c80432014-12-04 16:56:05 -08001216 try:
kelvin8ec71442015-01-15 16:57:00 -08001217 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001218 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001219 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001220 and not ipProto and not ipSrc and not ipDst\
1221 and not tcpSrc and not tcpDst and not setEthSrc\
1222 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001223 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001224
1225 else:
1226 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001227
shahshreyad0c80432014-12-04 16:56:05 -08001228 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001229 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001230 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001231 cmd += " --ethSrc " + str( ethSrc )
1232 if ethDst:
1233 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001234 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001235 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001236 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001237 cmd += " --lambda "
1238 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001239 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001240 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001241 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001242 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001243 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001244 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001245 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001246 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001247 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001248 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001249 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001250 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001251 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001252
kelvin8ec71442015-01-15 16:57:00 -08001253 # Check whether the user appended the port
1254 # or provided it as an input
shahshreyac2f97072015-03-19 17:04:29 -07001255
1256 if portIngressList is None:
1257 for ingressDevice in ingressDeviceList:
1258 if "/" in ingressDevice:
1259 cmd += " " + str( ingressDevice )
1260 else:
1261 main.log.error( "You must specify " +
1262 "the ingress port" )
1263 # TODO: perhaps more meaningful return
1264 return main.FALSE
shahshreyad0c80432014-12-04 16:56:05 -08001265 else:
shahshreyac2f97072015-03-19 17:04:29 -07001266 for ingressDevice,portIngress in zip(ingressDeviceList,portIngressList):
1267 cmd += " " + \
1268 str( ingressDevice ) + "/" +\
1269 str( portIngress ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001270
kelvin-onlabd3b64892015-01-20 13:26:24 -08001271 if "/" in egressDevice:
1272 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001273 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001274 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001275 main.log.error( "You must specify " +
1276 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001277 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001278
kelvin8ec71442015-01-15 16:57:00 -08001279 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001280 str( egressDevice ) + "/" +\
1281 str( portEgress )
shahshreyac2f97072015-03-19 17:04:29 -07001282
kelvin8ec71442015-01-15 16:57:00 -08001283 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001284 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001285 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001286 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001287 main.log.error( "Error in adding multipoint-to-singlepoint " +
1288 "intent" )
1289 return None
shahshreyad0c80432014-12-04 16:56:05 -08001290 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001291 # TODO: print out all the options in this message?
1292 main.log.info( "Multipoint-to-singlepoint intent installed" +
shahshreyac2f97072015-03-19 17:04:29 -07001293 " failed " )
1294 return None
1295 #match = re.search('id=0x([\da-f]+),', handle)
1296 #if match:
1297 #return match.group()[3:-1]
1298 #else:
1299 #main.log.error( "Error, intent ID not found" )
1300 #return None
Jon Halld4d4b372015-01-28 16:02:41 -08001301 except TypeError:
1302 main.log.exception( self.name + ": Object not as expected" )
1303 return None
shahshreyad0c80432014-12-04 16:56:05 -08001304 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001305 main.log.error( self.name + ": EOF exception found" )
1306 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001307 main.cleanup()
1308 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001309 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001310 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001311 main.cleanup()
1312 main.exit()
1313
Jon Hallefbd9792015-03-05 16:11:36 -08001314 def removeIntent( self, intentId, app='org.onosproject.cli',
1315 purge=False, sync=False ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001316 """
shahshreya1c818fc2015-02-26 13:44:08 -08001317 Remove intent for specified application id and intent id
1318 Optional args:-
1319 -s or --sync: Waits for the removal before returning
1320 -p or --purge: Purge the intent from the store after removal
1321
Jon Halle3f39ff2015-01-13 11:50:53 -08001322 Returns:
1323 main.False on error and
1324 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001325 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001326 try:
shahshreya1c818fc2015-02-26 13:44:08 -08001327 cmdStr = "remove-intent "
1328 if purge:
1329 cmdStr += " -p"
1330 if sync:
1331 cmdStr += " -s"
1332
1333 cmdStr += " " + app + " " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001334 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001335 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001336 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001337 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001338 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001339 # TODO: Should this be main.TRUE
1340 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001341 except TypeError:
1342 main.log.exception( self.name + ": Object not as expected" )
1343 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001344 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001345 main.log.error( self.name + ": EOF exception found" )
1346 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001347 main.cleanup()
1348 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001349 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001350 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001351 main.cleanup()
1352 main.exit()
1353
kelvin-onlabd3b64892015-01-20 13:26:24 -08001354 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001355 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001356 NOTE: This method should be used after installing application:
1357 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001358 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001359 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001360 Description:
1361 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001362 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001363 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001364 if jsonFormat:
1365 cmdStr = "routes -j"
1366 handleTmp = self.sendline( cmdStr )
1367 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1368 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001369 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001370 cmdStr = "routes"
1371 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001372 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001373 except TypeError:
1374 main.log.exception( self.name + ": Object not as expected" )
1375 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001376 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001377 main.log.error( self.name + ": EOF exception found" )
1378 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001379 main.cleanup()
1380 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001381 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001382 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001383 main.cleanup()
1384 main.exit()
1385
kelvin-onlabd3b64892015-01-20 13:26:24 -08001386 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001387 """
andrewonlab377693f2014-10-21 16:00:30 -04001388 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001389 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001390 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001391 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001392 """
andrewonlabe6745342014-10-17 14:29:13 -04001393 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001394 if jsonFormat:
1395 cmdStr = "intents -j"
1396 handle = self.sendline( cmdStr )
1397 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1398 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001399 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001400 cmdStr = "intents"
1401 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001402 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001403 except TypeError:
1404 main.log.exception( self.name + ": Object not as expected" )
1405 return None
andrewonlabe6745342014-10-17 14:29:13 -04001406 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001407 main.log.error( self.name + ": EOF exception found" )
1408 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001409 main.cleanup()
1410 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001411 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001412 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001413 main.cleanup()
1414 main.exit()
1415
kelvin-onlab54400a92015-02-26 18:05:51 -08001416 def getIntentState(self, intentsId, intentsJson=None):
1417 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001418 Check intent state.
1419 Accepts a single intent ID (string type) or a list of intent IDs.
1420 Returns the state(string type) of the id if a single intent ID is
1421 accepted.
Jon Hallefbd9792015-03-05 16:11:36 -08001422 Returns a dictionary with intent IDs as the key and its
1423 corresponding states as the values
kelvin-onlabfb521662015-02-27 09:52:40 -08001424 Parameters:
kelvin-onlab54400a92015-02-26 18:05:51 -08001425 intentId: intent ID (string type)
1426 intentsJson: parsed json object from the onos:intents api
1427 Returns:
1428 state = An intent's state- INSTALL,WITHDRAWN etc.
1429 stateDict = Dictionary of intent's state. intent ID as the keys and
1430 state as the values.
1431 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001432 try:
1433 state = "State is Undefined"
1434 if not intentsJson:
Jon Hallefbd9792015-03-05 16:11:36 -08001435 intentsJsonTemp = json.loads( self.intents() )
kelvin-onlab54400a92015-02-26 18:05:51 -08001436 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001437 intentsJsonTemp = json.loads( intentsJson )
1438 if isinstance( intentsId, types.StringType ):
kelvin-onlab54400a92015-02-26 18:05:51 -08001439 for intent in intentsJsonTemp:
1440 if intentsId == intent['id']:
1441 state = intent['state']
1442 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001443 main.log.info( "Cannot find intent ID" + str( intentsId ) +
1444 " on the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001445 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001446 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001447 dictList = []
kelvin-onlab54400a92015-02-26 18:05:51 -08001448 for ID in intentsId:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001449 stateDict = {}
kelvin-onlab54400a92015-02-26 18:05:51 -08001450 for intents in intentsJsonTemp:
1451 if ID == intents['id']:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001452 stateDict['state'] = intents['state']
1453 stateDict['id'] = ID
Jon Hallefbd9792015-03-05 16:11:36 -08001454 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08001455 break
Jon Hallefbd9792015-03-05 16:11:36 -08001456 if len( intentsId ) != len( dictList ):
1457 main.log.info( "Cannot find some of the intent ID state" )
kelvin-onlab07dbd012015-03-04 16:29:39 -08001458 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08001459 else:
1460 main.log.info("Invalid intents ID entry")
1461 return None
kelvin-onlab54400a92015-02-26 18:05:51 -08001462 except TypeError:
1463 main.log.exception( self.name + ": Object not as expected" )
1464 return None
1465 except pexpect.EOF:
1466 main.log.error( self.name + ": EOF exception found" )
1467 main.log.error( self.name + ": " + self.handle.before )
1468 main.cleanup()
1469 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001470 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08001471 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001472 main.cleanup()
1473 main.exit()
1474
kelvin-onlabd3b64892015-01-20 13:26:24 -08001475 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001476 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001477 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001478 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001479 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001480 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001481 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001482 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001483 if jsonFormat:
1484 cmdStr = "flows -j"
1485 handle = self.sendline( cmdStr )
1486 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1487 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001488 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001489 cmdStr = "flows"
1490 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001491 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001492 main.log.error( self.name + ".flows() response: " +
1493 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001494 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001495 except TypeError:
1496 main.log.exception( self.name + ": Object not as expected" )
1497 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001498 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001499 main.log.error( self.name + ": EOF exception found" )
1500 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001501 main.cleanup()
1502 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001503 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001504 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001505 main.cleanup()
1506 main.exit()
1507
kelvin-onlabd3b64892015-01-20 13:26:24 -08001508 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
Jon Hallefbd9792015-03-05 16:11:36 -08001509 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001510 """
andrewonlab87852b02014-11-19 18:44:19 -05001511 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001512 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001513 a specific point-to-point intent definition
1514 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001515 * dpidSrc: specify source dpid
1516 * dpidDst: specify destination dpid
1517 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001518 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001519 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001520 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001521 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001522 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001523 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001524 """
andrewonlab87852b02014-11-19 18:44:19 -05001525 try:
kelvin8ec71442015-01-15 16:57:00 -08001526 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001527 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1528 str( numIntents )
1529 if numMult:
1530 cmd += " " + str( numMult )
1531 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001532 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001533 if appId:
1534 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001535 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001536 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1537 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001538 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001539 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001540 main.log.info( handle )
1541 # Split result by newline
1542 newline = handle.split( "\r\r\n" )
1543 # Ignore the first object of list, which is empty
1544 newline = newline[ 1: ]
1545 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001546 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001547 result = result.split( ": " )
1548 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001549 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1550 main.log.info( latResult )
1551 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001552 else:
1553 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001554 except TypeError:
1555 main.log.exception( self.name + ": Object not as expected" )
1556 return None
andrewonlab87852b02014-11-19 18:44:19 -05001557 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001558 main.log.error( self.name + ": EOF exception found" )
1559 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001560 main.cleanup()
1561 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001562 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001563 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001564 main.cleanup()
1565 main.exit()
1566
kelvin-onlabd3b64892015-01-20 13:26:24 -08001567 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001568 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001569 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001570 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001571 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001572 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001573 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001574 if jsonFormat:
1575 cmdStr = "intents-events-metrics -j"
1576 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001577 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001578 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1579 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001580 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001581 cmdStr = "intents-events-metrics"
1582 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001583 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001584 except TypeError:
1585 main.log.exception( self.name + ": Object not as expected" )
1586 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001587 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001588 main.log.error( self.name + ": EOF exception found" )
1589 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001590 main.cleanup()
1591 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001592 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001593 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001594 main.cleanup()
1595 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001596
kelvin-onlabd3b64892015-01-20 13:26:24 -08001597 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001598 """
1599 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001600 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001601 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001602 """
andrewonlab867212a2014-10-22 20:13:38 -04001603 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001604 if jsonFormat:
1605 cmdStr = "topology-events-metrics -j"
1606 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001607 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001608 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1609 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001610 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001611 cmdStr = "topology-events-metrics"
1612 handle = self.sendline( cmdStr )
jenkins7ead5a82015-03-13 10:28:21 -07001613 if handle:
1614 return handle
1615 else:
1616 # Return empty json
1617 return '{}'
Jon Halld4d4b372015-01-28 16:02:41 -08001618 except TypeError:
1619 main.log.exception( self.name + ": Object not as expected" )
1620 return None
andrewonlab867212a2014-10-22 20:13:38 -04001621 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001622 main.log.error( self.name + ": EOF exception found" )
1623 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001624 main.cleanup()
1625 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001626 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001627 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001628 main.cleanup()
1629 main.exit()
1630
kelvin8ec71442015-01-15 16:57:00 -08001631 # Wrapper functions ****************
1632 # Wrapper functions use existing driver
1633 # functions and extends their use case.
1634 # For example, we may use the output of
1635 # a normal driver function, and parse it
1636 # using a wrapper function
andrewonlab7e4d2d32014-10-15 13:23:21 -04001637
kelvin-onlabd3b64892015-01-20 13:26:24 -08001638 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001639 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001640 Description:
1641 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001642 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001643 try:
kelvin8ec71442015-01-15 16:57:00 -08001644 # Obtain output of intents function
kelvin-onlabfb521662015-02-27 09:52:40 -08001645 intentsStr = self.intents(jsonFormat=False)
kelvin-onlabd3b64892015-01-20 13:26:24 -08001646 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001647
kelvin8ec71442015-01-15 16:57:00 -08001648 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001649 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1650 for intents in intentsList:
kelvin-onlabfb521662015-02-27 09:52:40 -08001651 match = re.search('id=0x([\da-f]+),', intents)
1652 if match:
1653 tmpId = match.group()[3:-1]
1654 intentIdList.append( tmpId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001655 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001656
Jon Halld4d4b372015-01-28 16:02:41 -08001657 except TypeError:
1658 main.log.exception( self.name + ": Object not as expected" )
1659 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001660 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001661 main.log.error( self.name + ": EOF exception found" )
1662 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001663 main.cleanup()
1664 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001665 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001666 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001667 main.cleanup()
1668 main.exit()
1669
Jon Hall30b82fa2015-03-04 17:15:43 -08001670 def FlowAddedCount( self, deviceId ):
1671 """
1672 Determine the number of flow rules for the given device id that are
1673 in the added state
1674 """
1675 try:
1676 cmdStr = "flows any " + str( deviceId ) + " | " +\
1677 "grep 'state=ADDED' | wc -l"
1678 handle = self.sendline( cmdStr )
1679 return handle
1680 except pexpect.EOF:
1681 main.log.error( self.name + ": EOF exception found" )
1682 main.log.error( self.name + ": " + self.handle.before )
1683 main.cleanup()
1684 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001685 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -08001686 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001687 main.cleanup()
1688 main.exit()
1689
kelvin-onlabd3b64892015-01-20 13:26:24 -08001690 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001691 """
andrewonlab95ce8322014-10-13 14:12:04 -04001692 Use 'devices' function to obtain list of all devices
1693 and parse the result to obtain a list of all device
1694 id's. Returns this list. Returns empty list if no
1695 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001696 List is ordered sequentially
1697
andrewonlab95ce8322014-10-13 14:12:04 -04001698 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001699 device id, and wish to execute other commands using
andrewonlab95ce8322014-10-13 14:12:04 -04001700 the ids. By obtaining the list of device ids on the fly,
1701 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001702 """
andrewonlab95ce8322014-10-13 14:12:04 -04001703 try:
kelvin8ec71442015-01-15 16:57:00 -08001704 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001705 devicesStr = self.devices( jsonFormat=False )
1706 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001707
kelvin-onlabd3b64892015-01-20 13:26:24 -08001708 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001709 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001710 return idList
kelvin8ec71442015-01-15 16:57:00 -08001711
1712 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001713 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001714 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001715 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001716 # Split list further into arguments before and after string
1717 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001718 # append to idList
1719 for arg in tempList:
1720 idList.append( arg.split( "id=" )[ 1 ] )
1721 return idList
andrewonlab95ce8322014-10-13 14:12:04 -04001722
Jon Halld4d4b372015-01-28 16:02:41 -08001723 except TypeError:
1724 main.log.exception( self.name + ": Object not as expected" )
1725 return None
andrewonlab95ce8322014-10-13 14:12:04 -04001726 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001727 main.log.error( self.name + ": EOF exception found" )
1728 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -04001729 main.cleanup()
1730 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001731 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001732 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -04001733 main.cleanup()
1734 main.exit()
1735
kelvin-onlabd3b64892015-01-20 13:26:24 -08001736 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001737 """
andrewonlab7c211572014-10-15 16:45:20 -04001738 Uses 'nodes' function to obtain list of all nodes
1739 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001740 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001741 Returns:
1742 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001743 """
andrewonlab7c211572014-10-15 16:45:20 -04001744 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001745 nodesStr = self.nodes()
1746 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001747
kelvin-onlabd3b64892015-01-20 13:26:24 -08001748 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001749 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001750 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001751
kelvin-onlabd3b64892015-01-20 13:26:24 -08001752 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001753 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001754
kelvin8ec71442015-01-15 16:57:00 -08001755 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001756 nodesList = nodesStr.split( "," )
1757 tempList = [ node for node in nodesList if "id=" in node ]
1758 for arg in tempList:
1759 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001760
kelvin-onlabd3b64892015-01-20 13:26:24 -08001761 return idList
kelvin8ec71442015-01-15 16:57:00 -08001762
Jon Halld4d4b372015-01-28 16:02:41 -08001763 except TypeError:
1764 main.log.exception( self.name + ": Object not as expected" )
1765 return None
andrewonlab7c211572014-10-15 16:45:20 -04001766 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001767 main.log.error( self.name + ": EOF exception found" )
1768 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001769 main.cleanup()
1770 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001771 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001772 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001773 main.cleanup()
1774 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -04001775
kelvin-onlabd3b64892015-01-20 13:26:24 -08001776 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001777 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001778 Return the first device from the devices api whose 'id' contains 'dpid'
1779 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001780 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001781 try:
kelvin8ec71442015-01-15 16:57:00 -08001782 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001783 return None
1784 else:
kelvin8ec71442015-01-15 16:57:00 -08001785 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001786 rawDevices = self.devices()
1787 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001788 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001789 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001790 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1791 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001792 return device
1793 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001794 except TypeError:
1795 main.log.exception( self.name + ": Object not as expected" )
1796 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001797 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001798 main.log.error( self.name + ": EOF exception found" )
1799 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001800 main.cleanup()
1801 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001802 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001803 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001804 main.cleanup()
1805 main.exit()
1806
kelvin-onlabd3b64892015-01-20 13:26:24 -08001807 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001808 """
Jon Hallefbd9792015-03-05 16:11:36 -08001809 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001810 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001811 log level can be specified.
kelvin8ec71442015-01-15 16:57:00 -08001812
Jon Hall42db6dc2014-10-24 19:03:48 -04001813 Params: ip = ip used for the onos cli
1814 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001815 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001816 logLevel = level to log to. Currently accepts
1817 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001818
1819
kelvin-onlabd3b64892015-01-20 13:26:24 -08001820 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001821
Jon Hallefbd9792015-03-05 16:11:36 -08001822 Returns: main.TRUE if the number of switches and links are correct,
1823 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04001824 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001825 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001826 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001827 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001828 if topology == {}:
1829 return main.ERROR
1830 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001831 # Is the number of switches is what we expected
1832 devices = topology.get( 'devices', False )
1833 links = topology.get( 'links', False )
kelvin-onlabfb521662015-02-27 09:52:40 -08001834 if devices is False or links is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04001835 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001836 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001837 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001838 linkCheck = ( int( links ) == int( numolink ) )
1839 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001840 # We expected the correct numbers
Jon Hallefbd9792015-03-05 16:11:36 -08001841 output += "The number of links and switches match " +\
1842 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001843 result = main.TRUE
1844 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001845 output += "The number of links and switches does not match " +\
1846 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001847 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001848 output = output + "\n ONOS sees %i devices (%i expected) \
1849 and %i links (%i expected)" % (
1850 int( devices ), int( numoswitch ), int( links ),
1851 int( numolink ) )
1852 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001853 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001854 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001855 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001856 else:
kelvin8ec71442015-01-15 16:57:00 -08001857 main.log.info( output )
1858 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001859 except TypeError:
1860 main.log.exception( self.name + ": Object not as expected" )
1861 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001862 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001863 main.log.error( self.name + ": EOF exception found" )
1864 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001865 main.cleanup()
1866 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001867 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001868 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001869 main.cleanup()
1870 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001871
kelvin-onlabd3b64892015-01-20 13:26:24 -08001872 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001873 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001874 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001875 deviceId must be the id of a device as seen in the onos devices command
1876 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001877 role must be either master, standby, or none
1878
Jon Halle3f39ff2015-01-13 11:50:53 -08001879 Returns:
1880 main.TRUE or main.FALSE based on argument verification and
1881 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001882 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001883 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001884 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001885 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001886 cmdStr = "device-role " +\
1887 str( deviceId ) + " " +\
1888 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001889 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001890 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001891 if re.search( "Error", handle ):
1892 # end color output to escape any colours
1893 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001894 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001895 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001896 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001897 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001898 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001899 main.log.error( "Invalid 'role' given to device_role(). " +
1900 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001901 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001902 except TypeError:
1903 main.log.exception( self.name + ": Object not as expected" )
1904 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001905 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001906 main.log.error( self.name + ": EOF exception found" )
1907 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001908 main.cleanup()
1909 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001910 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001911 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001912 main.cleanup()
1913 main.exit()
1914
kelvin-onlabd3b64892015-01-20 13:26:24 -08001915 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001916 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001917 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001918 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001919 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001920 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001921 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001922 if jsonFormat:
1923 cmdStr = "clusters -j"
1924 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001925 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001926 handle variable here contains some ANSI escape color code
1927 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001928 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001929 function. The repr( handle ) output when printed shows the ANSI
1930 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001931 variable is actually repr( somestring ) and json.loads would
1932 fail with the escape sequence. So we take off that escape
1933 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001934
kelvin-onlabd3b64892015-01-20 13:26:24 -08001935 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1936 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001937 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001938 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1939 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001940 return handle1
1941 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001942 cmdStr = "clusters"
1943 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001944 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001945 except TypeError:
1946 main.log.exception( self.name + ": Object not as expected" )
1947 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001948 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001949 main.log.error( self.name + ": EOF exception found" )
1950 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001951 main.cleanup()
1952 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001953 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001954 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001955 main.cleanup()
1956 main.exit()
1957
kelvin-onlabd3b64892015-01-20 13:26:24 -08001958 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001959 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001960 CLI command to get the current leader for the Election test application
1961 NOTE: Requires installation of the onos-app-election feature
1962 Returns: Node IP of the leader if one exists
1963 None if none exists
1964 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001965 """
Jon Hall94fd0472014-12-08 11:52:42 -08001966 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001967 cmdStr = "election-test-leader"
1968 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001969 # Leader
1970 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001971 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001972 nodeSearch = re.search( leaderPattern, response )
1973 if nodeSearch:
1974 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001975 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001976 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001977 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001978 # no leader
1979 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001980 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001981 nullSearch = re.search( nullPattern, response )
1982 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001983 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001984 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001985 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001986 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001987 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001988 if re.search( errorPattern, response ):
1989 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001990 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001991 return main.FALSE
1992 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001993 main.log.error( "Error in election_test_leader: " +
1994 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001995 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001996 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001997 except TypeError:
1998 main.log.exception( self.name + ": Object not as expected" )
1999 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002000 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002001 main.log.error( self.name + ": EOF exception found" )
2002 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002003 main.cleanup()
2004 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002005 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002006 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002007 main.cleanup()
2008 main.exit()
2009
kelvin-onlabd3b64892015-01-20 13:26:24 -08002010 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002011 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002012 CLI command to run for leadership of the Election test application.
2013 NOTE: Requires installation of the onos-app-election feature
2014 Returns: Main.TRUE on success
2015 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08002016 """
Jon Hall94fd0472014-12-08 11:52:42 -08002017 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002018 cmdStr = "election-test-run"
2019 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002020 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002021 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002022 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002023 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08002024 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08002025 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002026 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002027 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002028 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002029 errorPattern = "Command\snot\sfound"
2030 if re.search( errorPattern, response ):
2031 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002032 return main.FALSE
2033 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002034 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002035 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002036 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002037 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002038 except TypeError:
2039 main.log.exception( self.name + ": Object not as expected" )
2040 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002041 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002042 main.log.error( self.name + ": EOF exception found" )
2043 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002044 main.cleanup()
2045 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002046 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002047 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002048 main.cleanup()
2049 main.exit()
2050
kelvin-onlabd3b64892015-01-20 13:26:24 -08002051 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08002052 """
Jon Hall94fd0472014-12-08 11:52:42 -08002053 * CLI command to withdraw the local node from leadership election for
2054 * the Election test application.
2055 #NOTE: Requires installation of the onos-app-election feature
2056 Returns: Main.TRUE on success
2057 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08002058 """
Jon Hall94fd0472014-12-08 11:52:42 -08002059 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002060 cmdStr = "election-test-withdraw"
2061 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002062 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002063 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002064 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002065 if re.search( successPattern, response ):
2066 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002067 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002068 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002069 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002070 errorPattern = "Command\snot\sfound"
2071 if re.search( errorPattern, response ):
2072 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002073 return main.FALSE
2074 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002075 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002076 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002077 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002078 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002079 except TypeError:
2080 main.log.exception( self.name + ": Object not as expected" )
2081 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002082 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002083 main.log.error( self.name + ": EOF exception found" )
2084 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002085 main.cleanup()
2086 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002087 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002088 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002089 main.cleanup()
2090 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002091
kelvin8ec71442015-01-15 16:57:00 -08002092 def getDevicePortsEnabledCount( self, dpid ):
2093 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002094 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002095 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002096 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08002097 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002098 cmdStr = "onos:ports -e " + dpid + " | wc -l"
2099 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002100 if re.search( "No such device", output ):
2101 main.log.error( "Error in getting ports" )
2102 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002103 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002104 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002105 except TypeError:
2106 main.log.exception( self.name + ": Object not as expected" )
2107 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002108 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002109 main.log.error( self.name + ": EOF exception found" )
2110 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002111 main.cleanup()
2112 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002113 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002114 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002115 main.cleanup()
2116 main.exit()
2117
kelvin8ec71442015-01-15 16:57:00 -08002118 def getDeviceLinksActiveCount( self, dpid ):
2119 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002120 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002121 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002122 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002123 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002124 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
2125 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002126 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002127 main.log.error( "Error in getting ports " )
2128 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002129 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002130 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002131 except TypeError:
2132 main.log.exception( self.name + ": Object not as expected" )
2133 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002134 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002135 main.log.error( self.name + ": EOF exception found" )
2136 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002137 main.cleanup()
2138 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002139 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002140 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002141 main.cleanup()
2142 main.exit()
2143
kelvin8ec71442015-01-15 16:57:00 -08002144 def getAllIntentIds( self ):
2145 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002146 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002147 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002148 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002149 cmdStr = "onos:intents | grep id="
2150 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002151 if re.search( "Error", output ):
2152 main.log.error( "Error in getting ports" )
2153 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002154 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002155 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002156 except TypeError:
2157 main.log.exception( self.name + ": Object not as expected" )
2158 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002159 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002160 main.log.error( self.name + ": EOF exception found" )
2161 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002162 main.cleanup()
2163 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002164 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002165 main.log.exception( self.name + ": Uncaught exception!" )
2166 main.cleanup()
2167 main.exit()
2168
Jon Hall73509952015-02-24 16:42:56 -08002169 def intentSummary( self ):
2170 """
Jon Hallefbd9792015-03-05 16:11:36 -08002171 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08002172 """
2173 try:
2174 intents = self.intents( )
2175 intentStates = []
Jon Hall63604932015-02-26 17:09:50 -08002176 for intent in json.loads( intents ): # Iter through intents of a node
Jon Hall73509952015-02-24 16:42:56 -08002177 intentStates.append( intent.get( 'state', None ) )
Jon Hall63604932015-02-26 17:09:50 -08002178 out = [ (i, intentStates.count( i ) ) for i in set( intentStates ) ]
2179 main.log.info( dict( out ) )
Jon Hall73509952015-02-24 16:42:56 -08002180 return dict( out )
2181 except TypeError:
2182 main.log.exception( self.name + ": Object not as expected" )
2183 return None
2184 except pexpect.EOF:
2185 main.log.error( self.name + ": EOF exception found" )
2186 main.log.error( self.name + ": " + self.handle.before )
2187 main.cleanup()
2188 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002189 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08002190 main.log.exception( self.name + ": Uncaught exception!" )
2191 main.cleanup()
2192 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002193
2194 def leaders( self ):
2195 """
2196 Returns the output of the leaders command.
2197 """
2198 # FIXME: add json output
2199 try:
2200 output = self.sendline( "onos:leaders" )
2201 main.log.warn( output )
2202 return output
2203 except TypeError:
2204 main.log.exception( self.name + ": Object not as expected" )
2205 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002206 except pexpect.EOF:
2207 main.log.error( self.name + ": EOF exception found" )
2208 main.log.error( self.name + ": " + self.handle.before )
2209 main.cleanup()
2210 main.exit()
2211 except:
Jon Hall63604932015-02-26 17:09:50 -08002212 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002213 main.cleanup()
2214 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002215
2216 def pendingMap( self ):
2217 """
2218 Returns the output of the intent Pending map.
2219 """
2220 # FIXME: add json output
2221 try:
2222 output = self.sendline( "onos:intents -p" )
2223 main.log.warn( output )
2224 return output
2225 except TypeError:
2226 main.log.exception( self.name + ": Object not as expected" )
2227 return None
2228 except pexpect.EOF:
2229 main.log.error( self.name + ": EOF exception found" )
2230 main.log.error( self.name + ": " + self.handle.before )
2231 main.cleanup()
2232 main.exit()
2233 except:
2234 main.log.exception( self.name + ": Uncaught exception!" )
2235 main.cleanup()
2236 main.exit()
2237
2238 def partitions( self ):
2239 """
2240 Returns the output of the raft partitions command for ONOS.
2241 """
2242 # FIXME: add json output
2243 try:
2244 output = self.sendline( "partitions" )
2245 main.log.warn( output )
2246 return output
2247 except TypeError:
2248 main.log.exception( self.name + ": Object not as expected" )
2249 return None
2250 except pexpect.EOF:
2251 main.log.error( self.name + ": EOF exception found" )
2252 main.log.error( self.name + ": " + self.handle.before )
2253 main.cleanup()
2254 main.exit()
2255 except:
2256 main.log.exception( self.name + ": Uncaught exception!" )
2257 main.cleanup()
2258 main.exit()
2259