blob: d0d1b8fbb726b76416db2db03deba3ddfdca664c [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"
kelvin-onlabfb521662015-02-27 09:52:40 -080053
kelvin8ec71442015-01-15 16:57:00 -080054 self.name = self.options[ 'name' ]
55 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080056 user_name=self.user_name,
57 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080058 port=self.port,
59 pwd=self.pwd,
60 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040061
kelvin8ec71442015-01-15 16:57:00 -080062 self.handle.sendline( "cd " + self.home )
63 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040064 if self.handle:
65 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080066 else:
67 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040068 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080069 except TypeError:
70 main.log.exception( self.name + ": Object not as expected" )
71 return None
andrewonlab95ce8322014-10-13 14:12:04 -040072 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080073 main.log.error( self.name + ": EOF exception found" )
74 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040075 main.cleanup()
76 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080077 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -080078 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -040079 main.cleanup()
80 main.exit()
81
kelvin8ec71442015-01-15 16:57:00 -080082 def disconnect( self ):
83 """
andrewonlab95ce8322014-10-13 14:12:04 -040084 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080085 """
Jon Halld61331b2015-02-17 16:35:47 -080086 response = main.TRUE
Jon Hallefbd9792015-03-05 16:11:36 -080087 # noinspection PyBroadException
andrewonlab95ce8322014-10-13 14:12:04 -040088 try:
Jon Hallb7fce3e2015-03-04 10:18:32 -080089 self.logout()
kelvin8ec71442015-01-15 16:57:00 -080090 self.handle.sendline( "" )
91 self.handle.expect( "\$" )
92 self.handle.sendline( "exit" )
93 self.handle.expect( "closed" )
Jon Halld4d4b372015-01-28 16:02:41 -080094 except TypeError:
95 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -080096 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -040097 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080098 main.log.error( self.name + ": EOF exception found" )
99 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800100 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800101 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400102 response = main.FALSE
103 return response
104
kelvin8ec71442015-01-15 16:57:00 -0800105 def logout( self ):
106 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500107 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800108 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500109 try:
kelvin8ec71442015-01-15 16:57:00 -0800110 self.handle.sendline( "" )
Jon Hallb7fce3e2015-03-04 10:18:32 -0800111 i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ],
112 timeout=10 )
113 if i == 0: # In ONOS CLI
kelvin8ec71442015-01-15 16:57:00 -0800114 self.handle.sendline( "logout" )
115 self.handle.expect( "\$" )
Jon Hallb7fce3e2015-03-04 10:18:32 -0800116 elif i == 1: # not in CLI
andrewonlab9627f432014-11-14 12:45:10 -0500117 return main.TRUE
Jon Hallb7fce3e2015-03-04 10:18:32 -0800118 elif i == 3: # Timeout
119 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -0800120 except TypeError:
121 main.log.exception( self.name + ": Object not as expected" )
122 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500123 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800124 main.log.error( self.name + ": eof exception found" )
125 main.log.error( self.name + ": " +
126 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500127 main.cleanup()
128 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800129 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800130 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500131 main.cleanup()
132 main.exit()
133
kelvin-onlabd3b64892015-01-20 13:26:24 -0800134 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800135 """
andrewonlab95ce8322014-10-13 14:12:04 -0400136 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800137
andrewonlab95ce8322014-10-13 14:12:04 -0400138 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800139 """
andrewonlab95ce8322014-10-13 14:12:04 -0400140 try:
141 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800142 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400143 main.cleanup()
144 main.exit()
145 else:
kelvin8ec71442015-01-15 16:57:00 -0800146 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800147 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800148 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400149 # and that this driver will have to change accordingly
Cameron Franke9c94fb02015-01-21 10:20:20 -0800150 self.handle.expect(str(cellname))
kelvin-onlabd3b64892015-01-20 13:26:24 -0800151 handleBefore = self.handle.before
152 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800153 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800154 self.handle.sendline("")
155 self.handle.expect("\$")
kelvin-onlabd3b64892015-01-20 13:26:24 -0800156 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400157
kelvin-onlabd3b64892015-01-20 13:26:24 -0800158 main.log.info( "Cell call returned: " + handleBefore +
159 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400160
161 return main.TRUE
162
Jon Halld4d4b372015-01-28 16:02:41 -0800163 except TypeError:
164 main.log.exception( self.name + ": Object not as expected" )
165 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400166 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800167 main.log.error( self.name + ": eof exception found" )
168 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400169 main.cleanup()
170 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800171 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800172 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400173 main.cleanup()
174 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800175
kelvin-onlabd3b64892015-01-20 13:26:24 -0800176 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800177 """
Jon Hallefbd9792015-03-05 16:11:36 -0800178 karafTimeout is an optional argument. karafTimeout value passed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 by user would be used to set the current karaf shell idle timeout.
180 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800181 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800182 Below is an example to start a session with 60 seconds idle timeout
183 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800184
Hari Krishna25d42f72015-01-05 15:08:28 -0800185 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800186 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800187
kelvin-onlabd3b64892015-01-20 13:26:24 -0800188 Note: karafTimeout is left as str so that this could be read
189 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800190 """
andrewonlab95ce8322014-10-13 14:12:04 -0400191 try:
kelvin8ec71442015-01-15 16:57:00 -0800192 self.handle.sendline( "" )
193 x = self.handle.expect( [
194 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500195
196 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800197 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500198 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400199
kelvin8ec71442015-01-15 16:57:00 -0800200 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800201 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800202 i = self.handle.expect( [
203 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800204 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400205
206 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800207 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800208 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800209 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800210 "config:property-set -p org.apache.karaf.shell\
211 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800212 karafTimeout )
213 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800214 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800215 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400216 return main.TRUE
217 else:
kelvin8ec71442015-01-15 16:57:00 -0800218 # If failed, send ctrl+c to process and try again
219 main.log.info( "Starting CLI failed. Retrying..." )
220 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800221 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800222 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
223 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400224 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800225 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800226 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800227 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800228 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800229 "config:property-set -p org.apache.karaf.shell\
230 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800231 karafTimeout )
232 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800233 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800234 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400235 return main.TRUE
236 else:
kelvin8ec71442015-01-15 16:57:00 -0800237 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800238 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400239 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400240
Jon Halld4d4b372015-01-28 16:02:41 -0800241 except TypeError:
242 main.log.exception( self.name + ": Object not as expected" )
243 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400244 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800245 main.log.error( self.name + ": EOF exception found" )
246 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400247 main.cleanup()
248 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800249 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800250 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400251 main.cleanup()
252 main.exit()
253
Jon Hallefbd9792015-03-05 16:11:36 -0800254 def log( self, cmdStr, level="" ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800255 """
256 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800257 returns main.TRUE on success
Jon Hallefbd9792015-03-05 16:11:36 -0800258 returns main.FALSE if Error occurred
kelvin-onlab338f5512015-02-06 10:53:16 -0800259 Available level: DEBUG, TRACE, INFO, WARN, ERROR
260 Level defaults to INFO
kelvin-onlab9f541032015-02-04 16:19:53 -0800261 """
262 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800263 lvlStr = ""
264 if level:
265 lvlStr = "--level=" + level
266
kelvin-onlab9f541032015-02-04 16:19:53 -0800267 self.handle.sendline( "" )
268 self.handle.expect( "onos>" )
kelvin-onlab338f5512015-02-06 10:53:16 -0800269 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
kelvin-onlab9f541032015-02-04 16:19:53 -0800270 self.handle.expect( "onos>" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800271
kelvin-onlab9f541032015-02-04 16:19:53 -0800272 response = self.handle.before
273 if re.search( "Error", response ):
274 return main.FALSE
275 return main.TRUE
276
277 except pexpect.EOF:
278 main.log.error( self.name + ": EOF exception found" )
279 main.log.error( self.name + ": " + self.handle.before )
280 main.cleanup()
281 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800282 except Exception:
kelvin-onlabfb521662015-02-27 09:52:40 -0800283 main.log.exception( self.name + ": Uncaught exception!" )
kelvin-onlab9f541032015-02-04 16:19:53 -0800284 main.cleanup()
285 main.exit()
286
kelvin-onlabd3b64892015-01-20 13:26:24 -0800287 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800288 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800289 Send a completely user specified string to
290 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400291 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800292
andrewonlaba18f6bf2014-10-13 19:31:54 -0400293 Warning: There are no sanity checking to commands
294 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800295 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400296 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800297 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
298 self.log( logStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800299 self.handle.sendline( cmdStr )
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
Jon Hallaea67aa2015-01-23 13:30:57 -0800312 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
313 + self.name + "." )
Jon Hall7bdfc122015-01-23 11:45:32 -0800314 # Remove control strings from output
315 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" )
shahshreya280223a2015-02-26 12:25:25 -0800497 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
kelvin-onlabd3b64892015-01-20 13:26:24 -0800511 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800512 """
Jon Hall7b02d952014-10-17 20:14:54 -0400513 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400514 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800515 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800516 """
andrewonlab86dc3082014-10-13 18:18:38 -0400517 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800518 if jsonFormat:
519 cmdStr = "devices -j"
520 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800521 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800522 handle variable here contains some ANSI escape color code
523 sequences at the end which are invisible in the print command
524 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800525 function. The repr( handle ) output when printed shows the
526 ANSI escape sequences. In json.loads( somestring ), this
527 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800528 json.loads would fail with the escape sequence. So we take off
529 that escape sequence using:
530
kelvin-onlabd3b64892015-01-20 13:26:24 -0800531 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
532 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800533 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800534 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
535 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400536 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400537 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800538 cmdStr = "devices"
539 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400540 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800541 except TypeError:
542 main.log.exception( self.name + ": Object not as expected" )
543 return None
andrewonlab7c211572014-10-15 16:45:20 -0400544 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800545 main.log.error( self.name + ": EOF exception found" )
546 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400547 main.cleanup()
548 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800549 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800550 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400551 main.cleanup()
552 main.exit()
553
kelvin-onlabd3b64892015-01-20 13:26:24 -0800554 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800555 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800556 This balances the devices across all controllers
557 by issuing command: 'onos> onos:balance-masters'
558 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800559 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800560 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800561 cmdStr = "onos:balance-masters"
562 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800563 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800564 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800565 except TypeError:
566 main.log.exception( self.name + ": Object not as expected" )
567 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800568 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800569 main.log.error( self.name + ": EOF exception found" )
570 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800571 main.cleanup()
572 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800573 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800574 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800575 main.cleanup()
576 main.exit()
577
kelvin-onlabd3b64892015-01-20 13:26:24 -0800578 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800579 """
Jon Halle8217482014-10-17 13:49:14 -0400580 Lists all core links
581 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800582 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800583 """
Jon Halle8217482014-10-17 13:49:14 -0400584 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800585 if jsonFormat:
586 cmdStr = "links -j"
587 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800588 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800589 handle variable here contains some ANSI escape color code
590 sequences at the end which are invisible in the print command
591 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800592 function. The repr( handle ) output when printed shows the ANSI
593 escape sequences. In json.loads( somestring ), this somestring
594 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800595 fail with the escape sequence. So we take off that escape
596 sequence using:
597
kelvin-onlabd3b64892015-01-20 13:26:24 -0800598 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
599 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800600 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800601 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
602 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400603 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400604 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800605 cmdStr = "links"
606 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400607 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800608 except TypeError:
609 main.log.exception( self.name + ": Object not as expected" )
610 return None
Jon Halle8217482014-10-17 13:49:14 -0400611 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800612 main.log.error( self.name + ": EOF exception found" )
613 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400614 main.cleanup()
615 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800616 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800617 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400618 main.cleanup()
619 main.exit()
620
kelvin-onlabd3b64892015-01-20 13:26:24 -0800621 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800622 """
Jon Halle8217482014-10-17 13:49:14 -0400623 Lists all ports
624 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800625 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800626 """
Jon Halle8217482014-10-17 13:49:14 -0400627 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800628 if jsonFormat:
629 cmdStr = "ports -j"
630 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800631 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800632 handle variable here contains some ANSI escape color code
633 sequences at the end which are invisible in the print command
634 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800635 function. The repr( handle ) output when printed shows the ANSI
636 escape sequences. In json.loads( somestring ), this somestring
637 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800638 fail with the escape sequence. So we take off that escape
Jon Hallefbd9792015-03-05 16:11:36 -0800639 sequence using the following commands:
Jon Halle3f39ff2015-01-13 11:50:53 -0800640
kelvin-onlabd3b64892015-01-20 13:26:24 -0800641 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
642 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800643 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800644 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
645 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400646 return handle1
647
Jon Halle8217482014-10-17 13:49:14 -0400648 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800649 cmdStr = "ports"
650 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800651 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800652 except TypeError:
653 main.log.exception( self.name + ": Object not as expected" )
654 return None
Jon Halle8217482014-10-17 13:49:14 -0400655 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800656 main.log.error( self.name + ": EOF exception found" )
657 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400658 main.cleanup()
659 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800660 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800661 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400662 main.cleanup()
663 main.exit()
664
kelvin-onlabd3b64892015-01-20 13:26:24 -0800665 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800666 """
Jon Hall983a1702014-10-28 18:44:22 -0400667 Lists all devices and the controllers with roles assigned to them
668 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800669 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800670 """
andrewonlab7c211572014-10-15 16:45:20 -0400671 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800672 if jsonFormat:
673 cmdStr = "roles -j"
674 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800675 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800676 handle variable here contains some ANSI escape color code
677 sequences at the end which are invisible in the print command
678 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800679 function. The repr( handle ) output when printed shows the ANSI
680 escape sequences. In json.loads( somestring ), this somestring
681 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800682 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500683
Jon Halle3f39ff2015-01-13 11:50:53 -0800684 So we take off that escape sequence using the following
685 commads:
686
kelvin-onlabd3b64892015-01-20 13:26:24 -0800687 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
688 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800689 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800690 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
691 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400692 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400693
andrewonlab7c211572014-10-15 16:45:20 -0400694 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800695 cmdStr = "roles"
696 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800697 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800698 except TypeError:
699 main.log.exception( self.name + ": Object not as expected" )
700 return None
Jon Hall983a1702014-10-28 18:44:22 -0400701 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800702 main.log.error( self.name + ": EOF exception found" )
703 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400704 main.cleanup()
705 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800706 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800707 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400708 main.cleanup()
709 main.exit()
710
kelvin-onlabd3b64892015-01-20 13:26:24 -0800711 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800712 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800713 Given the a string containing the json representation of the "roles"
714 cli command and a partial or whole device id, returns a json object
715 containing the roles output for the first device whose id contains
716 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400717
718 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800719 A dict of the role assignments for the given device or
720 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800721 """
Jon Hall983a1702014-10-28 18:44:22 -0400722 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800723 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400724 return None
725 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800726 rawRoles = self.roles()
727 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800728 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800729 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800730 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800731 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400732 return device
733 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800734 except TypeError:
735 main.log.exception( self.name + ": Object not as expected" )
736 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400737 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800738 main.log.error( self.name + ": EOF exception found" )
739 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400740 main.cleanup()
741 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800742 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800743 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400744 main.cleanup()
745 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800746
kelvin-onlabd3b64892015-01-20 13:26:24 -0800747 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800748 """
Jon Hall94fd0472014-12-08 11:52:42 -0800749 Iterates through each device and checks if there is a master assigned
750 Returns: main.TRUE if each device has a master
751 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800752 """
Jon Hall94fd0472014-12-08 11:52:42 -0800753 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800754 rawRoles = self.roles()
755 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800756 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800757 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800758 # print device
759 if device[ 'master' ] == "none":
760 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800761 return main.FALSE
762 return main.TRUE
763
Jon Halld4d4b372015-01-28 16:02:41 -0800764 except TypeError:
765 main.log.exception( self.name + ": Object not as expected" )
766 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800767 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800768 main.log.error( self.name + ": EOF exception found" )
769 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800770 main.cleanup()
771 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800772 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800773 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800774 main.cleanup()
775 main.exit()
776
kelvin-onlabd3b64892015-01-20 13:26:24 -0800777 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800778 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400779 Returns string of paths, and the cost.
780 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800781 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400782 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800783 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
784 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800785 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800786 main.log.error( "Error in getting paths" )
787 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400788 else:
kelvin8ec71442015-01-15 16:57:00 -0800789 path = handle.split( ";" )[ 0 ]
790 cost = handle.split( ";" )[ 1 ]
791 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800792 except TypeError:
793 main.log.exception( self.name + ": Object not as expected" )
794 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400795 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800796 main.log.error( self.name + ": EOF exception found" )
797 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400798 main.cleanup()
799 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800800 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800801 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400802 main.cleanup()
803 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800804
kelvin-onlabd3b64892015-01-20 13:26:24 -0800805 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800806 """
Jon Hallffb386d2014-11-21 13:43:38 -0800807 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400808 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800809 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800810 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400811 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800812 if jsonFormat:
813 cmdStr = "hosts -j"
814 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800815 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800816 handle variable here contains some ANSI escape color code
817 sequences at the end which are invisible in the print command
818 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800819 function. The repr( handle ) output when printed shows the ANSI
820 escape sequences. In json.loads( somestring ), this somestring
821 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800822 fail with the escape sequence. So we take off that escape
823 sequence using:
824
kelvin-onlabd3b64892015-01-20 13:26:24 -0800825 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
826 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800827 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800828 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
829 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400830 return handle1
831 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800832 cmdStr = "hosts"
833 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400834 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800835 except TypeError:
836 main.log.exception( self.name + ": Object not as expected" )
837 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400838 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800839 main.log.error( self.name + ": EOF exception found" )
840 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400841 main.cleanup()
842 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800843 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800844 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400845 main.cleanup()
846 main.exit()
847
kelvin-onlabd3b64892015-01-20 13:26:24 -0800848 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800849 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400850 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800851
Jon Hallefbd9792015-03-05 16:11:36 -0800852 Note: mac must be a colon separated mac address, but could be a
Jon Halle3f39ff2015-01-13 11:50:53 -0800853 partial mac address
854
Jon Hall42db6dc2014-10-24 19:03:48 -0400855 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800856 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400857 try:
kelvin8ec71442015-01-15 16:57:00 -0800858 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400859 return None
860 else:
861 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800862 rawHosts = self.hosts()
863 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800864 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800865 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800866 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800867 if not host:
868 pass
869 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400870 return host
871 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800872 except TypeError:
873 main.log.exception( self.name + ": Object not as expected" )
874 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400875 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800876 main.log.error( self.name + ": EOF exception found" )
877 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400878 main.cleanup()
879 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800880 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800881 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400882 main.cleanup()
883 main.exit()
884
kelvin-onlabd3b64892015-01-20 13:26:24 -0800885 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800886 """
887 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400888 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800889
andrewonlab3f0a4af2014-10-17 12:25:14 -0400890 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800891 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400892 IMPORTANT:
893 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800894 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400895 Furthermore, it assumes that value of VLAN is '-1'
896 Description:
kelvin8ec71442015-01-15 16:57:00 -0800897 Converts mininet hosts ( h1, h2, h3... ) into
898 ONOS format ( 00:00:00:00:00:01/-1 , ... )
899 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400900 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800901 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400902
kelvin-onlabd3b64892015-01-20 13:26:24 -0800903 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800904 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800905 hostHex = hex( int( host ) ).zfill( 12 )
906 hostHex = str( hostHex ).replace( 'x', '0' )
907 i = iter( str( hostHex ) )
908 hostHex = ":".join( a + b for a, b in zip( i, i ) )
909 hostHex = hostHex + "/-1"
910 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400911
kelvin-onlabd3b64892015-01-20 13:26:24 -0800912 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400913
Jon Halld4d4b372015-01-28 16:02:41 -0800914 except TypeError:
915 main.log.exception( self.name + ": Object not as expected" )
916 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400917 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800918 main.log.error( self.name + ": EOF exception found" )
919 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400920 main.cleanup()
921 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800922 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800923 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400924 main.cleanup()
925 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400926
kelvin-onlabd3b64892015-01-20 13:26:24 -0800927 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800928 """
andrewonlabe6745342014-10-17 14:29:13 -0400929 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800930 * hostIdOne: ONOS host id for host1
931 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400932 Description:
Jon Hallefbd9792015-03-05 16:11:36 -0800933 Adds a host-to-host intent ( bidirectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500934 specifying the two hosts.
kelvin-onlabfb521662015-02-27 09:52:40 -0800935 Returns:
936 A string of the intent id or None on Error
kelvin8ec71442015-01-15 16:57:00 -0800937 """
andrewonlabe6745342014-10-17 14:29:13 -0400938 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800939 cmdStr = "add-host-intent " + str( hostIdOne ) +\
940 " " + str( hostIdTwo )
941 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800942 if re.search( "Error", handle ):
943 main.log.error( "Error in adding Host intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800944 return None
Hari Krishnaac4e1782015-01-26 12:09:12 -0800945 else:
946 main.log.info( "Host intent installed between " +
kelvin-onlabfb521662015-02-27 09:52:40 -0800947 str( hostIdOne ) + " and " + str( hostIdTwo ) )
948 match = re.search('id=0x([\da-f]+),', handle)
949 if match:
950 return match.group()[3:-1]
951 else:
952 main.log.error( "Error, intent ID not found" )
953 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800954 except TypeError:
955 main.log.exception( self.name + ": Object not as expected" )
956 return None
andrewonlabe6745342014-10-17 14:29:13 -0400957 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800958 main.log.error( self.name + ": EOF exception found" )
959 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400960 main.cleanup()
961 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800962 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800963 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400964 main.cleanup()
965 main.exit()
966
kelvin-onlabd3b64892015-01-20 13:26:24 -0800967 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800968 """
andrewonlab7b31d232014-10-24 13:31:47 -0400969 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800970 * ingressDevice: device id of ingress device
971 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400972 Optional:
973 TODO: Still needs to be implemented via dev side
kelvin-onlabfb521662015-02-27 09:52:40 -0800974 Description:
975 Adds an optical intent by specifying an ingress and egress device
976 Returns:
977 A string of the intent id or None on error
kelvin-onlab898a6c62015-01-16 14:13:53 -0800978 """
andrewonlab7b31d232014-10-24 13:31:47 -0400979 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800980 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
981 " " + str( egressDevice )
982 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800983 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800984 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -0800985 main.log.error( "Error in adding Optical intent" )
986 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400987 else:
kelvin-onlabfb521662015-02-27 09:52:40 -0800988 main.log.info( "Optical intent installed between " +
989 str( ingressDevice ) + " and " +
990 str( egressDevice ) )
991 match = re.search('id=0x([\da-f]+),', handle)
992 if match:
993 return match.group()[3:-1]
994 else:
995 main.log.error( "Error, intent ID not found" )
996 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800997 except TypeError:
998 main.log.exception( self.name + ": Object not as expected" )
999 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001000 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001001 main.log.error( self.name + ": EOF exception found" )
1002 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -04001003 main.cleanup()
1004 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001005 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001006 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -04001007 main.cleanup()
1008 main.exit()
1009
kelvin-onlabd3b64892015-01-20 13:26:24 -08001010 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001011 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001012 ingressDevice,
1013 egressDevice,
1014 portIngress="",
1015 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001016 ethType="",
1017 ethSrc="",
1018 ethDst="",
1019 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001020 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001021 ipProto="",
1022 ipSrc="",
1023 ipDst="",
1024 tcpSrc="",
1025 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001026 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001027 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001028 * ingressDevice: device id of ingress device
1029 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001030 Optional:
1031 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001032 * ethSrc: specify ethSrc ( i.e. src mac addr )
1033 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001034 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001035 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001036 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001037 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001038 * ipSrc: specify ip source address
1039 * ipDst: specify ip destination address
1040 * tcpSrc: specify tcp source port
1041 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001042 Description:
kelvin8ec71442015-01-15 16:57:00 -08001043 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001044 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001045 Returns:
1046 A string of the intent id or None on error
andrewonlab289e4b72014-10-21 21:24:18 -04001047
Jon Halle3f39ff2015-01-13 11:50:53 -08001048 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001049 options developers provide for point-to-point
1050 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001051 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001052 try:
kelvin8ec71442015-01-15 16:57:00 -08001053 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001054 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001055 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001056 and not ipProto and not ipSrc and not ipDst \
1057 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001058 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001059
andrewonlab289e4b72014-10-21 21:24:18 -04001060 else:
andrewonlab36af3822014-11-18 17:48:18 -05001061 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001062
andrewonlab0c0a6772014-10-22 12:31:18 -04001063 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001064 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001065 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001066 cmd += " --ethSrc " + str( ethSrc )
1067 if ethDst:
1068 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001069 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001070 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001071 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001072 cmd += " --lambda "
1073 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001074 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001075 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001076 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001077 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001078 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001079 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001080 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001081 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001082 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001083
kelvin8ec71442015-01-15 16:57:00 -08001084 # Check whether the user appended the port
1085 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001086 if "/" in ingressDevice:
1087 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001088 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001089 if not portIngress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001090 main.log.error( "You must specify the ingress port" )
kelvin8ec71442015-01-15 16:57:00 -08001091 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001092 # Would it make sense to throw an exception and exit
1093 # the test?
1094 return None
andrewonlab36af3822014-11-18 17:48:18 -05001095
kelvin8ec71442015-01-15 16:57:00 -08001096 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001097 str( ingressDevice ) + "/" +\
1098 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001099
kelvin-onlabd3b64892015-01-20 13:26:24 -08001100 if "/" in egressDevice:
1101 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001102 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001103 if not portEgress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001104 main.log.error( "You must specify the egress port" )
1105 return None
Jon Halle3f39ff2015-01-13 11:50:53 -08001106
kelvin8ec71442015-01-15 16:57:00 -08001107 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001108 str( egressDevice ) + "/" +\
1109 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001110
kelvin-onlab898a6c62015-01-16 14:13:53 -08001111 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001112 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001113 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001114 main.log.error( "Error in adding point-to-point intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001115 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001116 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001117 # TODO: print out all the options in this message?
1118 main.log.info( "Point-to-point intent installed between " +
1119 str( ingressDevice ) + " and " +
1120 str( egressDevice ) )
1121 match = re.search('id=0x([\da-f]+),', handle)
1122 if match:
1123 return match.group()[3:-1]
1124 else:
1125 main.log.error( "Error, intent ID not found" )
1126 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001127 except TypeError:
1128 main.log.exception( self.name + ": Object not as expected" )
1129 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001130 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001131 main.log.error( self.name + ": EOF exception found" )
1132 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001133 main.cleanup()
1134 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001135 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001136 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001137 main.cleanup()
1138 main.exit()
1139
kelvin-onlabd3b64892015-01-20 13:26:24 -08001140 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001141 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001142 ingressDevice1,
1143 ingressDevice2,
1144 egressDevice,
kelvin-onlabfb521662015-02-27 09:52:40 -08001145 portIngress1="",
1146 portIngress2="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001147 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001148 ethType="",
1149 ethSrc="",
1150 ethDst="",
1151 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001152 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001153 ipProto="",
1154 ipSrc="",
1155 ipDst="",
1156 tcpSrc="",
1157 tcpDst="",
1158 setEthSrc="",
1159 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001160 """
shahshreyad0c80432014-12-04 16:56:05 -08001161 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001162 This function assumes that there would be 2 ingress devices and
1163 one egress device. For more number of ingress devices, this
1164 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001165 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001166 * ingressDevice1: device id of ingress device1
1167 * ingressDevice2: device id of ingress device2
1168 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001169 Optional:
1170 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001171 * ethSrc: specify ethSrc ( i.e. src mac addr )
1172 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001173 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001174 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001175 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001176 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001177 * ipSrc: specify ip source address
1178 * ipDst: specify ip destination address
1179 * tcpSrc: specify tcp source port
1180 * tcpDst: specify tcp destination port
1181 * setEthSrc: action to Rewrite Source MAC Address
1182 * setEthDst: action to Rewrite Destination MAC Address
1183 Description:
kelvin8ec71442015-01-15 16:57:00 -08001184 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001185 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001186 Returns:
1187 A string of the intent id or None on error
shahshreyad0c80432014-12-04 16:56:05 -08001188
Jon Halle3f39ff2015-01-13 11:50:53 -08001189 NOTE: This function may change depending on the
Jon Hallefbd9792015-03-05 16:11:36 -08001190 options developers provide for multipoint-to-singlepoint
shahshreyad0c80432014-12-04 16:56:05 -08001191 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001192 """
shahshreyad0c80432014-12-04 16:56:05 -08001193 try:
kelvin8ec71442015-01-15 16:57:00 -08001194 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001195 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001196 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001197 and not ipProto and not ipSrc and not ipDst\
1198 and not tcpSrc and not tcpDst and not setEthSrc\
1199 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001200 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001201
1202 else:
1203 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001204
shahshreyad0c80432014-12-04 16:56:05 -08001205 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001206 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001207 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001208 cmd += " --ethSrc " + str( ethSrc )
1209 if ethDst:
1210 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001211 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001212 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001213 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001214 cmd += " --lambda "
1215 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001216 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001217 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001218 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001219 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001220 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001221 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001222 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001223 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001224 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001225 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001226 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001227 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001228 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001229
kelvin8ec71442015-01-15 16:57:00 -08001230 # Check whether the user appended the port
1231 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001232 if "/" in ingressDevice1:
1233 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001234 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001235 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001236 main.log.error( "You must specify " +
1237 "the ingress port1" )
1238 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001239 return None
shahshreyad0c80432014-12-04 16:56:05 -08001240
kelvin8ec71442015-01-15 16:57:00 -08001241 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001242 str( ingressDevice1 ) + "/" +\
1243 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001244
kelvin-onlabd3b64892015-01-20 13:26:24 -08001245 if "/" in ingressDevice2:
1246 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001247 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001248 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001249 main.log.error( "You must specify " +
1250 "the ingress port2" )
1251 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001252 return None
shahshreyad0c80432014-12-04 16:56:05 -08001253
kelvin8ec71442015-01-15 16:57:00 -08001254 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001255 str( ingressDevice2 ) + "/" +\
1256 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001257
kelvin-onlabd3b64892015-01-20 13:26:24 -08001258 if "/" in egressDevice:
1259 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001260 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001261 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001262 main.log.error( "You must specify " +
1263 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001264 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001265
kelvin8ec71442015-01-15 16:57:00 -08001266 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001267 str( egressDevice ) + "/" +\
1268 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001269 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001270 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001271 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001272 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001273 main.log.error( "Error in adding multipoint-to-singlepoint " +
1274 "intent" )
1275 return None
shahshreyad0c80432014-12-04 16:56:05 -08001276 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001277 # TODO: print out all the options in this message?
1278 main.log.info( "Multipoint-to-singlepoint intent installed" +
1279 " between " + str( ingressDevice1 ) + ", " +
1280 str( ingressDevice2 ) + " and " +
1281 str( egressDevice ) )
1282 match = re.search('id=0x([\da-f]+),', handle)
1283 if match:
1284 return match.group()[3:-1]
1285 else:
1286 main.log.error( "Error, intent ID not found" )
1287 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001288 except TypeError:
1289 main.log.exception( self.name + ": Object not as expected" )
1290 return None
shahshreyad0c80432014-12-04 16:56:05 -08001291 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001292 main.log.error( self.name + ": EOF exception found" )
1293 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001294 main.cleanup()
1295 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001296 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001297 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001298 main.cleanup()
1299 main.exit()
1300
Jon Hallefbd9792015-03-05 16:11:36 -08001301 def removeIntent( self, intentId, app='org.onosproject.cli',
1302 purge=False, sync=False ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001303 """
shahshreya1c818fc2015-02-26 13:44:08 -08001304 Remove intent for specified application id and intent id
1305 Optional args:-
1306 -s or --sync: Waits for the removal before returning
1307 -p or --purge: Purge the intent from the store after removal
1308
Jon Halle3f39ff2015-01-13 11:50:53 -08001309 Returns:
1310 main.False on error and
1311 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001312 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001313 try:
shahshreya1c818fc2015-02-26 13:44:08 -08001314 cmdStr = "remove-intent "
1315 if purge:
1316 cmdStr += " -p"
1317 if sync:
1318 cmdStr += " -s"
1319
1320 cmdStr += " " + app + " " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001321 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001322 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001323 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001324 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001325 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001326 # TODO: Should this be main.TRUE
1327 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001328 except TypeError:
1329 main.log.exception( self.name + ": Object not as expected" )
1330 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001331 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001332 main.log.error( self.name + ": EOF exception found" )
1333 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001334 main.cleanup()
1335 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001336 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001337 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001338 main.cleanup()
1339 main.exit()
1340
kelvin-onlabd3b64892015-01-20 13:26:24 -08001341 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001342 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001343 NOTE: This method should be used after installing application:
1344 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001345 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001346 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001347 Description:
1348 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001349 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001350 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001351 if jsonFormat:
1352 cmdStr = "routes -j"
1353 handleTmp = self.sendline( cmdStr )
1354 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1355 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001356 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001357 cmdStr = "routes"
1358 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001359 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001360 except TypeError:
1361 main.log.exception( self.name + ": Object not as expected" )
1362 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001363 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001364 main.log.error( self.name + ": EOF exception found" )
1365 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001366 main.cleanup()
1367 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001368 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001369 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001370 main.cleanup()
1371 main.exit()
1372
kelvin-onlabd3b64892015-01-20 13:26:24 -08001373 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001374 """
andrewonlab377693f2014-10-21 16:00:30 -04001375 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001376 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001377 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001378 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001379 """
andrewonlabe6745342014-10-17 14:29:13 -04001380 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001381 if jsonFormat:
1382 cmdStr = "intents -j"
1383 handle = self.sendline( cmdStr )
1384 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1385 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001386 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001387 cmdStr = "intents"
1388 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001389 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001390 except TypeError:
1391 main.log.exception( self.name + ": Object not as expected" )
1392 return None
andrewonlabe6745342014-10-17 14:29:13 -04001393 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001394 main.log.error( self.name + ": EOF exception found" )
1395 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001396 main.cleanup()
1397 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001398 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001399 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001400 main.cleanup()
1401 main.exit()
1402
kelvin-onlab54400a92015-02-26 18:05:51 -08001403 def getIntentState(self, intentsId, intentsJson=None):
1404 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001405 Check intent state.
1406 Accepts a single intent ID (string type) or a list of intent IDs.
1407 Returns the state(string type) of the id if a single intent ID is
1408 accepted.
Jon Hallefbd9792015-03-05 16:11:36 -08001409 Returns a dictionary with intent IDs as the key and its
1410 corresponding states as the values
kelvin-onlabfb521662015-02-27 09:52:40 -08001411 Parameters:
kelvin-onlab54400a92015-02-26 18:05:51 -08001412 intentId: intent ID (string type)
1413 intentsJson: parsed json object from the onos:intents api
1414 Returns:
1415 state = An intent's state- INSTALL,WITHDRAWN etc.
1416 stateDict = Dictionary of intent's state. intent ID as the keys and
1417 state as the values.
1418 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001419 try:
1420 state = "State is Undefined"
1421 if not intentsJson:
Jon Hallefbd9792015-03-05 16:11:36 -08001422 intentsJsonTemp = json.loads( self.intents() )
kelvin-onlab54400a92015-02-26 18:05:51 -08001423 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001424 intentsJsonTemp = json.loads( intentsJson )
1425 if isinstance( intentsId, types.StringType ):
kelvin-onlab54400a92015-02-26 18:05:51 -08001426 for intent in intentsJsonTemp:
1427 if intentsId == intent['id']:
1428 state = intent['state']
1429 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001430 main.log.info( "Cannot find intent ID" + str( intentsId ) +
1431 " on the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001432 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001433 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001434 dictList = []
kelvin-onlab54400a92015-02-26 18:05:51 -08001435 for ID in intentsId:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001436 stateDict = {}
kelvin-onlab54400a92015-02-26 18:05:51 -08001437 for intents in intentsJsonTemp:
1438 if ID == intents['id']:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001439 stateDict['state'] = intents['state']
1440 stateDict['id'] = ID
Jon Hallefbd9792015-03-05 16:11:36 -08001441 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08001442 break
Jon Hallefbd9792015-03-05 16:11:36 -08001443 if len( intentsId ) != len( dictList ):
1444 main.log.info( "Cannot find some of the intent ID state" )
kelvin-onlab07dbd012015-03-04 16:29:39 -08001445 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08001446 else:
1447 main.log.info("Invalid intents ID entry")
1448 return None
kelvin-onlab54400a92015-02-26 18:05:51 -08001449 except TypeError:
1450 main.log.exception( self.name + ": Object not as expected" )
1451 return None
1452 except pexpect.EOF:
1453 main.log.error( self.name + ": EOF exception found" )
1454 main.log.error( self.name + ": " + self.handle.before )
1455 main.cleanup()
1456 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001457 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08001458 main.log.exception( self.name + ": Uncaught exception!" )
1459 main.cleanup()
1460 main.exit()
Jon Hall30b82fa2015-03-04 17:15:43 -08001461
kelvin-onlabd3b64892015-01-20 13:26:24 -08001462 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001463 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001464 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001465 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001466 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001467 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001468 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001469 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001470 if jsonFormat:
1471 cmdStr = "flows -j"
1472 handle = self.sendline( cmdStr )
1473 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1474 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001475 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001476 cmdStr = "flows"
1477 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001478 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001479 main.log.error( self.name + ".flows() response: " +
1480 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001481 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001482 except TypeError:
1483 main.log.exception( self.name + ": Object not as expected" )
1484 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001485 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001486 main.log.error( self.name + ": EOF exception found" )
1487 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001488 main.cleanup()
1489 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001490 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001491 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001492 main.cleanup()
1493 main.exit()
1494
kelvin-onlabd3b64892015-01-20 13:26:24 -08001495 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
Jon Hallefbd9792015-03-05 16:11:36 -08001496 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001497 """
andrewonlab87852b02014-11-19 18:44:19 -05001498 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001499 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001500 a specific point-to-point intent definition
1501 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001502 * dpidSrc: specify source dpid
1503 * dpidDst: specify destination dpid
1504 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001505 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001506 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001507 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001508 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001509 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001510 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001511 """
andrewonlab87852b02014-11-19 18:44:19 -05001512 try:
kelvin8ec71442015-01-15 16:57:00 -08001513 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001514 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1515 str( numIntents )
1516 if numMult:
1517 cmd += " " + str( numMult )
1518 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001519 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001520 if appId:
1521 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001522 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001523 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1524 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001525 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001526 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001527 main.log.info( handle )
1528 # Split result by newline
1529 newline = handle.split( "\r\r\n" )
1530 # Ignore the first object of list, which is empty
1531 newline = newline[ 1: ]
1532 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001533 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001534 result = result.split( ": " )
1535 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001536 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1537 main.log.info( latResult )
1538 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001539 else:
1540 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001541 except TypeError:
1542 main.log.exception( self.name + ": Object not as expected" )
1543 return None
andrewonlab87852b02014-11-19 18:44:19 -05001544 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001545 main.log.error( self.name + ": EOF exception found" )
1546 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001547 main.cleanup()
1548 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001549 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001550 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001551 main.cleanup()
1552 main.exit()
1553
kelvin-onlabd3b64892015-01-20 13:26:24 -08001554 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001555 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001556 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001557 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001558 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001559 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001560 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001561 if jsonFormat:
1562 cmdStr = "intents-events-metrics -j"
1563 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001564 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001565 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1566 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001567 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001568 cmdStr = "intents-events-metrics"
1569 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001570 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001571 except TypeError:
1572 main.log.exception( self.name + ": Object not as expected" )
1573 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001574 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001575 main.log.error( self.name + ": EOF exception found" )
1576 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001577 main.cleanup()
1578 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001579 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001580 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001581 main.cleanup()
1582 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001583
kelvin-onlabd3b64892015-01-20 13:26:24 -08001584 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001585 """
1586 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001587 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001588 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001589 """
andrewonlab867212a2014-10-22 20:13:38 -04001590 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001591 if jsonFormat:
1592 cmdStr = "topology-events-metrics -j"
1593 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001594 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001595 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1596 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001597 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001598 cmdStr = "topology-events-metrics"
1599 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001600 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001601 except TypeError:
1602 main.log.exception( self.name + ": Object not as expected" )
1603 return None
andrewonlab867212a2014-10-22 20:13:38 -04001604 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001605 main.log.error( self.name + ": EOF exception found" )
1606 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001607 main.cleanup()
1608 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001609 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001610 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001611 main.cleanup()
1612 main.exit()
1613
kelvin8ec71442015-01-15 16:57:00 -08001614 # Wrapper functions ****************
1615 # Wrapper functions use existing driver
1616 # functions and extends their use case.
1617 # For example, we may use the output of
1618 # a normal driver function, and parse it
1619 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001620
kelvin-onlabd3b64892015-01-20 13:26:24 -08001621 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001622 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001623 Description:
1624 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001625 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001626 try:
kelvin8ec71442015-01-15 16:57:00 -08001627 # Obtain output of intents function
kelvin-onlabfb521662015-02-27 09:52:40 -08001628 intentsStr = self.intents(jsonFormat=False)
kelvin-onlabd3b64892015-01-20 13:26:24 -08001629 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001630
kelvin8ec71442015-01-15 16:57:00 -08001631 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001632 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1633 for intents in intentsList:
kelvin-onlabfb521662015-02-27 09:52:40 -08001634 match = re.search('id=0x([\da-f]+),', intents)
1635 if match:
1636 tmpId = match.group()[3:-1]
1637 intentIdList.append( tmpId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001638 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001639
Jon Halld4d4b372015-01-28 16:02:41 -08001640 except TypeError:
1641 main.log.exception( self.name + ": Object not as expected" )
1642 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001643 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001644 main.log.error( self.name + ": EOF exception found" )
1645 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001646 main.cleanup()
1647 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001648 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001649 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001650 main.cleanup()
1651 main.exit()
1652
Jon Hall30b82fa2015-03-04 17:15:43 -08001653 def FlowAddedCount( self, deviceId ):
1654 """
1655 Determine the number of flow rules for the given device id that are
1656 in the added state
1657 """
1658 try:
1659 cmdStr = "flows any " + str( deviceId ) + " | " +\
1660 "grep 'state=ADDED' | wc -l"
1661 handle = self.sendline( cmdStr )
1662 return handle
1663 except pexpect.EOF:
1664 main.log.error( self.name + ": EOF exception found" )
1665 main.log.error( self.name + ": " + self.handle.before )
1666 main.cleanup()
1667 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001668 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -08001669 main.log.exception( self.name + ": Uncaught exception!" )
1670 main.cleanup()
1671 main.exit()
1672
kelvin-onlabd3b64892015-01-20 13:26:24 -08001673 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001674 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001675 Use 'devices' function to obtain list of all devices
1676 and parse the result to obtain a list of all device
1677 id's. Returns this list. Returns empty list if no
1678 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001679 List is ordered sequentially
1680
andrewonlab3e15ead2014-10-15 14:21:34 -04001681 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001682 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001683 the ids. By obtaining the list of device ids on the fly,
1684 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001685 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001686 try:
kelvin8ec71442015-01-15 16:57:00 -08001687 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001688 devicesStr = self.devices( jsonFormat=False )
1689 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001690
kelvin-onlabd3b64892015-01-20 13:26:24 -08001691 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001692 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001693 return idList
kelvin8ec71442015-01-15 16:57:00 -08001694
1695 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001696 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001697 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001698 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001699 # Split list further into arguments before and after string
1700 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001701 # append to idList
1702 for arg in tempList:
1703 idList.append( arg.split( "id=" )[ 1 ] )
1704 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001705
Jon Halld4d4b372015-01-28 16:02:41 -08001706 except TypeError:
1707 main.log.exception( self.name + ": Object not as expected" )
1708 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04001709 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001710 main.log.error( self.name + ": EOF exception found" )
1711 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001712 main.cleanup()
1713 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001714 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001715 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001716 main.cleanup()
1717 main.exit()
1718
kelvin-onlabd3b64892015-01-20 13:26:24 -08001719 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001720 """
andrewonlab7c211572014-10-15 16:45:20 -04001721 Uses 'nodes' function to obtain list of all nodes
1722 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001723 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001724 Returns:
1725 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001726 """
andrewonlab7c211572014-10-15 16:45:20 -04001727 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001728 nodesStr = self.nodes()
1729 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001730
kelvin-onlabd3b64892015-01-20 13:26:24 -08001731 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001732 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001733 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001734
kelvin-onlabd3b64892015-01-20 13:26:24 -08001735 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001736 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001737
kelvin8ec71442015-01-15 16:57:00 -08001738 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001739 nodesList = nodesStr.split( "," )
1740 tempList = [ node for node in nodesList if "id=" in node ]
1741 for arg in tempList:
1742 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001743
kelvin-onlabd3b64892015-01-20 13:26:24 -08001744 return idList
kelvin8ec71442015-01-15 16:57:00 -08001745
Jon Halld4d4b372015-01-28 16:02:41 -08001746 except TypeError:
1747 main.log.exception( self.name + ": Object not as expected" )
1748 return None
andrewonlab7c211572014-10-15 16:45:20 -04001749 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001750 main.log.error( self.name + ": EOF exception found" )
1751 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001752 main.cleanup()
1753 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001754 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001755 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001756 main.cleanup()
1757 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001758
kelvin-onlabd3b64892015-01-20 13:26:24 -08001759 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001760 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001761 Return the first device from the devices api whose 'id' contains 'dpid'
1762 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001763 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001764 try:
kelvin8ec71442015-01-15 16:57:00 -08001765 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001766 return None
1767 else:
kelvin8ec71442015-01-15 16:57:00 -08001768 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001769 rawDevices = self.devices()
1770 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001771 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001772 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001773 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1774 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001775 return device
1776 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001777 except TypeError:
1778 main.log.exception( self.name + ": Object not as expected" )
1779 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001780 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001781 main.log.error( self.name + ": EOF exception found" )
1782 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001783 main.cleanup()
1784 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001785 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001786 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001787 main.cleanup()
1788 main.exit()
1789
kelvin-onlabd3b64892015-01-20 13:26:24 -08001790 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001791 """
Jon Hallefbd9792015-03-05 16:11:36 -08001792 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001793 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001794 log level can be specified.
kelvin8ec71442015-01-15 16:57:00 -08001795
Jon Hall42db6dc2014-10-24 19:03:48 -04001796 Params: ip = ip used for the onos cli
1797 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001798 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001799 logLevel = level to log to. Currently accepts
1800 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001801
1802
kelvin-onlabd3b64892015-01-20 13:26:24 -08001803 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001804
Jon Hallefbd9792015-03-05 16:11:36 -08001805 Returns: main.TRUE if the number of switches and links are correct,
1806 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04001807 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001808 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001809 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001810 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001811 if topology == {}:
1812 return main.ERROR
1813 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001814 # Is the number of switches is what we expected
1815 devices = topology.get( 'devices', False )
1816 links = topology.get( 'links', False )
kelvin-onlabfb521662015-02-27 09:52:40 -08001817 if devices is False or links is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04001818 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001819 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001820 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001821 linkCheck = ( int( links ) == int( numolink ) )
1822 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001823 # We expected the correct numbers
Jon Hallefbd9792015-03-05 16:11:36 -08001824 output += "The number of links and switches match " +\
1825 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001826 result = main.TRUE
1827 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001828 output += "The number of links and switches does not match " +\
1829 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001830 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001831 output = output + "\n ONOS sees %i devices (%i expected) \
1832 and %i links (%i expected)" % (
1833 int( devices ), int( numoswitch ), int( links ),
1834 int( numolink ) )
1835 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001836 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001837 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001838 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001839 else:
kelvin8ec71442015-01-15 16:57:00 -08001840 main.log.info( output )
1841 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001842 except TypeError:
1843 main.log.exception( self.name + ": Object not as expected" )
1844 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001845 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001846 main.log.error( self.name + ": EOF exception found" )
1847 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001848 main.cleanup()
1849 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001850 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001851 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001852 main.cleanup()
1853 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001854
kelvin-onlabd3b64892015-01-20 13:26:24 -08001855 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001856 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001857 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001858 deviceId must be the id of a device as seen in the onos devices command
1859 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001860 role must be either master, standby, or none
1861
Jon Halle3f39ff2015-01-13 11:50:53 -08001862 Returns:
1863 main.TRUE or main.FALSE based on argument verification and
1864 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001865 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001866 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001867 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001868 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001869 cmdStr = "device-role " +\
1870 str( deviceId ) + " " +\
1871 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001872 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001873 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001874 if re.search( "Error", handle ):
1875 # end color output to escape any colours
1876 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001877 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001878 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001879 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001880 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001881 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001882 main.log.error( "Invalid 'role' given to device_role(). " +
1883 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001884 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001885 except TypeError:
1886 main.log.exception( self.name + ": Object not as expected" )
1887 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001888 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001889 main.log.error( self.name + ": EOF exception found" )
1890 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001891 main.cleanup()
1892 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001893 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001894 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001895 main.cleanup()
1896 main.exit()
1897
kelvin-onlabd3b64892015-01-20 13:26:24 -08001898 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001899 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001900 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001901 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001902 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001903 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001904 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001905 if jsonFormat:
1906 cmdStr = "clusters -j"
1907 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001908 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001909 handle variable here contains some ANSI escape color code
1910 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001911 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001912 function. The repr( handle ) output when printed shows the ANSI
1913 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001914 variable is actually repr( somestring ) and json.loads would
1915 fail with the escape sequence. So we take off that escape
1916 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001917
kelvin-onlabd3b64892015-01-20 13:26:24 -08001918 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1919 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001920 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001921 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1922 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001923 return handle1
1924 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001925 cmdStr = "clusters"
1926 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001927 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001928 except TypeError:
1929 main.log.exception( self.name + ": Object not as expected" )
1930 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001931 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001932 main.log.error( self.name + ": EOF exception found" )
1933 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001934 main.cleanup()
1935 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001936 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001937 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001938 main.cleanup()
1939 main.exit()
1940
kelvin-onlabd3b64892015-01-20 13:26:24 -08001941 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001942 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001943 CLI command to get the current leader for the Election test application
1944 NOTE: Requires installation of the onos-app-election feature
1945 Returns: Node IP of the leader if one exists
1946 None if none exists
1947 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001948 """
Jon Hall94fd0472014-12-08 11:52:42 -08001949 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001950 cmdStr = "election-test-leader"
1951 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001952 # Leader
1953 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001954 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001955 nodeSearch = re.search( leaderPattern, response )
1956 if nodeSearch:
1957 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001958 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001959 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001960 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001961 # no leader
1962 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001963 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001964 nullSearch = re.search( nullPattern, response )
1965 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001966 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001967 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001968 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001969 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001970 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001971 if re.search( errorPattern, response ):
1972 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001973 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001974 return main.FALSE
1975 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001976 main.log.error( "Error in election_test_leader: " +
1977 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001978 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001979 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001980 except TypeError:
1981 main.log.exception( self.name + ": Object not as expected" )
1982 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001983 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001984 main.log.error( self.name + ": EOF exception found" )
1985 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001986 main.cleanup()
1987 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001988 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001989 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001990 main.cleanup()
1991 main.exit()
1992
kelvin-onlabd3b64892015-01-20 13:26:24 -08001993 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001994 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001995 CLI command to run for leadership of the Election test application.
1996 NOTE: Requires installation of the onos-app-election feature
1997 Returns: Main.TRUE on success
1998 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001999 """
Jon Hall94fd0472014-12-08 11:52:42 -08002000 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002001 cmdStr = "election-test-run"
2002 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002003 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002004 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002005 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002006 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08002007 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08002008 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002009 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002010 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002011 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002012 errorPattern = "Command\snot\sfound"
2013 if re.search( errorPattern, response ):
2014 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002015 return main.FALSE
2016 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002017 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002018 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002019 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002020 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002021 except TypeError:
2022 main.log.exception( self.name + ": Object not as expected" )
2023 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002024 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002025 main.log.error( self.name + ": EOF exception found" )
2026 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002027 main.cleanup()
2028 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002029 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002030 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002031 main.cleanup()
2032 main.exit()
2033
kelvin-onlabd3b64892015-01-20 13:26:24 -08002034 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08002035 """
Jon Hall94fd0472014-12-08 11:52:42 -08002036 * CLI command to withdraw the local node from leadership election for
2037 * the Election test application.
2038 #NOTE: Requires installation of the onos-app-election feature
2039 Returns: Main.TRUE on success
2040 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08002041 """
Jon Hall94fd0472014-12-08 11:52:42 -08002042 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002043 cmdStr = "election-test-withdraw"
2044 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002045 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002046 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002047 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002048 if re.search( successPattern, response ):
2049 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002050 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002051 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002052 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002053 errorPattern = "Command\snot\sfound"
2054 if re.search( errorPattern, response ):
2055 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002056 return main.FALSE
2057 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002058 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002059 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002060 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002061 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002062 except TypeError:
2063 main.log.exception( self.name + ": Object not as expected" )
2064 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002065 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002066 main.log.error( self.name + ": EOF exception found" )
2067 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002068 main.cleanup()
2069 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002070 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002071 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002072 main.cleanup()
2073 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002074
kelvin8ec71442015-01-15 16:57:00 -08002075 def getDevicePortsEnabledCount( self, dpid ):
2076 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002077 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002078 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002079 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08002080 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002081 cmdStr = "onos:ports -e " + dpid + " | wc -l"
2082 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002083 if re.search( "No such device", output ):
2084 main.log.error( "Error in getting ports" )
2085 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002086 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002087 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002088 except TypeError:
2089 main.log.exception( self.name + ": Object not as expected" )
2090 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002091 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002092 main.log.error( self.name + ": EOF exception found" )
2093 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002094 main.cleanup()
2095 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002096 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002097 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002098 main.cleanup()
2099 main.exit()
2100
kelvin8ec71442015-01-15 16:57:00 -08002101 def getDeviceLinksActiveCount( self, dpid ):
2102 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002103 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002104 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002105 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002106 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002107 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
2108 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002109 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002110 main.log.error( "Error in getting ports " )
2111 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002112 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002113 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002114 except TypeError:
2115 main.log.exception( self.name + ": Object not as expected" )
2116 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002117 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002118 main.log.error( self.name + ": EOF exception found" )
2119 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002120 main.cleanup()
2121 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002122 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002123 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002124 main.cleanup()
2125 main.exit()
2126
kelvin8ec71442015-01-15 16:57:00 -08002127 def getAllIntentIds( self ):
2128 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002129 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002130 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002131 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002132 cmdStr = "onos:intents | grep id="
2133 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002134 if re.search( "Error", output ):
2135 main.log.error( "Error in getting ports" )
2136 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002137 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002138 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002139 except TypeError:
2140 main.log.exception( self.name + ": Object not as expected" )
2141 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002142 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002143 main.log.error( self.name + ": EOF exception found" )
2144 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002145 main.cleanup()
2146 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002147 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002148 main.log.exception( self.name + ": Uncaught exception!" )
2149 main.cleanup()
2150 main.exit()
2151
Jon Hall73509952015-02-24 16:42:56 -08002152 def intentSummary( self ):
2153 """
Jon Hallefbd9792015-03-05 16:11:36 -08002154 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08002155 """
2156 try:
2157 intents = self.intents( )
2158 intentStates = []
Jon Hall63604932015-02-26 17:09:50 -08002159 for intent in json.loads( intents ): # Iter through intents of a node
Jon Hall73509952015-02-24 16:42:56 -08002160 intentStates.append( intent.get( 'state', None ) )
Jon Hall63604932015-02-26 17:09:50 -08002161 out = [ (i, intentStates.count( i ) ) for i in set( intentStates ) ]
2162 main.log.info( dict( out ) )
Jon Hall73509952015-02-24 16:42:56 -08002163 return dict( out )
2164 except TypeError:
2165 main.log.exception( self.name + ": Object not as expected" )
2166 return None
2167 except pexpect.EOF:
2168 main.log.error( self.name + ": EOF exception found" )
2169 main.log.error( self.name + ": " + self.handle.before )
2170 main.cleanup()
2171 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002172 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08002173 main.log.exception( self.name + ": Uncaught exception!" )
2174 main.cleanup()
2175 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002176
2177 def leaders( self ):
2178 """
2179 Returns the output of the leaders command.
2180 """
2181 # FIXME: add json output
2182 try:
2183 output = self.sendline( "onos:leaders" )
2184 main.log.warn( output )
2185 return output
2186 except TypeError:
2187 main.log.exception( self.name + ": Object not as expected" )
2188 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002189 except pexpect.EOF:
2190 main.log.error( self.name + ": EOF exception found" )
2191 main.log.error( self.name + ": " + self.handle.before )
2192 main.cleanup()
2193 main.exit()
2194 except:
Jon Hall63604932015-02-26 17:09:50 -08002195 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002196 main.cleanup()
2197 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002198
2199 def pendingMap( self ):
2200 """
2201 Returns the output of the intent Pending map.
2202 """
2203 # FIXME: add json output
2204 try:
2205 output = self.sendline( "onos:intents -p" )
2206 main.log.warn( output )
2207 return output
2208 except TypeError:
2209 main.log.exception( self.name + ": Object not as expected" )
2210 return None
2211 except pexpect.EOF:
2212 main.log.error( self.name + ": EOF exception found" )
2213 main.log.error( self.name + ": " + self.handle.before )
2214 main.cleanup()
2215 main.exit()
2216 except:
2217 main.log.exception( self.name + ": Uncaught exception!" )
2218 main.cleanup()
2219 main.exit()
2220
2221 def partitions( self ):
2222 """
2223 Returns the output of the raft partitions command for ONOS.
2224 """
2225 # FIXME: add json output
2226 try:
2227 output = self.sendline( "partitions" )
2228 main.log.warn( output )
2229 return output
2230 except TypeError:
2231 main.log.exception( self.name + ": Object not as expected" )
2232 return None
2233 except pexpect.EOF:
2234 main.log.error( self.name + ": EOF exception found" )
2235 main.log.error( self.name + ": " + self.handle.before )
2236 main.cleanup()
2237 main.exit()
2238 except:
2239 main.log.exception( self.name + ": Uncaught exception!" )
2240 main.cleanup()
2241 main.exit()
2242