blob: f8ec9e53f8d8e0e561879fed81a1e16d130d2fbd [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
kelvin8ec71442015-01-15 16:57:00 -08003"""
andrewonlab95ce8322014-10-13 14:12:04 -04004This driver enters the onos> prompt to issue commands.
5
kelvin8ec71442015-01-15 16:57:00 -08006Please follow the coding style demonstrated by existing
andrewonlab95ce8322014-10-13 14:12:04 -04007functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040014shreya@onlab.us
andrewonlab95ce8322014-10-13 14:12:04 -040015
16OCT 13 2014
17
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab95ce8322014-10-13 14:12:04 -040019import sys
andrewonlab95ce8322014-10-13 14:12:04 -040020import pexpect
21import re
Jon Hall30b82fa2015-03-04 17:15:43 -080022import json
23import types
kelvin8ec71442015-01-15 16:57:00 -080024sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040025from drivers.common.clidriver import CLI
26
andrewonlab95ce8322014-10-13 14:12:04 -040027
kelvin8ec71442015-01-15 16:57:00 -080028class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040029
kelvin8ec71442015-01-15 16:57:00 -080030 def __init__( self ):
31 """
32 Initialize client
33 """
Jon Hallefbd9792015-03-05 16:11:36 -080034 self.name = None
35 self.home = None
36 self.handle = None
kelvin8ec71442015-01-15 16:57:00 -080037 super( CLI, self ).__init__()
38
39 def connect( self, **connectargs ):
40 """
andrewonlab95ce8322014-10-13 14:12:04 -040041 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080042 """
andrewonlab95ce8322014-10-13 14:12:04 -040043 try:
44 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080045 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us658ec012015-03-11 15:13:09 -070046 self.home = "~/onos"
andrewonlab95ce8322014-10-13 14:12:04 -040047 for key in self.options:
48 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080049 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040050 break
kelvin-onlabfb521662015-02-27 09:52:40 -080051 if self.home is None or self.home == "":
kelvin-onlabd6634ac2015-01-29 14:23:10 -080052 self.home = "~/ONOS"
andrewonlab95ce8322014-10-13 14:12:04 -040053
kelvin8ec71442015-01-15 16:57:00 -080054 self.name = self.options[ 'name' ]
55 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080056 user_name=self.user_name,
57 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080058 port=self.port,
59 pwd=self.pwd,
60 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040061
kelvin8ec71442015-01-15 16:57:00 -080062 self.handle.sendline( "cd " + self.home )
63 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040064 if self.handle:
65 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080066 else:
67 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040068 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080069 except TypeError:
70 main.log.exception( self.name + ": Object not as expected" )
71 return None
andrewonlab95ce8322014-10-13 14:12:04 -040072 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080073 main.log.error( self.name + ": EOF exception found" )
74 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040075 main.cleanup()
76 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080077 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -080078 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -040079 main.cleanup()
80 main.exit()
81
kelvin8ec71442015-01-15 16:57:00 -080082 def disconnect( self ):
83 """
andrewonlab95ce8322014-10-13 14:12:04 -040084 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080085 """
Jon Halld61331b2015-02-17 16:35:47 -080086 response = main.TRUE
Jon Hallefbd9792015-03-05 16:11:36 -080087 # noinspection PyBroadException
andrewonlab95ce8322014-10-13 14:12:04 -040088 try:
Jon Hallb7fce3e2015-03-04 10:18:32 -080089 self.logout()
kelvin8ec71442015-01-15 16:57:00 -080090 self.handle.sendline( "" )
91 self.handle.expect( "\$" )
92 self.handle.sendline( "exit" )
93 self.handle.expect( "closed" )
Jon Halld4d4b372015-01-28 16:02:41 -080094 except TypeError:
95 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -080096 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -040097 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080098 main.log.error( self.name + ": EOF exception found" )
99 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800100 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800101 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400102 response = main.FALSE
103 return response
104
kelvin8ec71442015-01-15 16:57:00 -0800105 def logout( self ):
106 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500107 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800108 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500109 try:
kelvin8ec71442015-01-15 16:57:00 -0800110 self.handle.sendline( "" )
Jon Hallb7fce3e2015-03-04 10:18:32 -0800111 i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ],
112 timeout=10 )
113 if i == 0: # In ONOS CLI
kelvin8ec71442015-01-15 16:57:00 -0800114 self.handle.sendline( "logout" )
115 self.handle.expect( "\$" )
Jon Hallb7fce3e2015-03-04 10:18:32 -0800116 elif i == 1: # not in CLI
andrewonlab9627f432014-11-14 12:45:10 -0500117 return main.TRUE
Jon Hallb7fce3e2015-03-04 10:18:32 -0800118 elif i == 3: # Timeout
119 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -0800120 except TypeError:
121 main.log.exception( self.name + ": Object not as expected" )
122 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500123 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800124 main.log.error( self.name + ": eof exception found" )
125 main.log.error( self.name + ": " +
126 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500127 main.cleanup()
128 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800129 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800130 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500131 main.cleanup()
132 main.exit()
133
kelvin-onlabd3b64892015-01-20 13:26:24 -0800134 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800135 """
andrewonlab95ce8322014-10-13 14:12:04 -0400136 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800137
andrewonlab95ce8322014-10-13 14:12:04 -0400138 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800139 """
andrewonlab95ce8322014-10-13 14:12:04 -0400140 try:
141 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800142 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400143 main.cleanup()
144 main.exit()
145 else:
kelvin8ec71442015-01-15 16:57:00 -0800146 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800147 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800148 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400149 # and that this driver will have to change accordingly
Cameron Franke9c94fb02015-01-21 10:20:20 -0800150 self.handle.expect(str(cellname))
andrew@onlab.usc400b112015-01-21 15:33:19 -0800151 handleBefore = self.handle.before
152 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800153 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800154 self.handle.sendline("")
155 self.handle.expect("\$")
andrew@onlab.usc400b112015-01-21 15:33:19 -0800156 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400157
kelvin-onlabd3b64892015-01-20 13:26:24 -0800158 main.log.info( "Cell call returned: " + handleBefore +
159 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400160
161 return main.TRUE
162
Jon Halld4d4b372015-01-28 16:02:41 -0800163 except TypeError:
164 main.log.exception( self.name + ": Object not as expected" )
165 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400166 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800167 main.log.error( self.name + ": eof exception found" )
168 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400169 main.cleanup()
170 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800171 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800172 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400173 main.cleanup()
174 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800175
kelvin-onlabd3b64892015-01-20 13:26:24 -0800176 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800177 """
Jon Hallefbd9792015-03-05 16:11:36 -0800178 karafTimeout is an optional argument. karafTimeout value passed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 by user would be used to set the current karaf shell idle timeout.
180 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800181 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800182 Below is an example to start a session with 60 seconds idle timeout
183 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800184
Hari Krishna25d42f72015-01-05 15:08:28 -0800185 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800186 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800187
kelvin-onlabd3b64892015-01-20 13:26:24 -0800188 Note: karafTimeout is left as str so that this could be read
189 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800190 """
andrewonlab95ce8322014-10-13 14:12:04 -0400191 try:
kelvin8ec71442015-01-15 16:57:00 -0800192 self.handle.sendline( "" )
193 x = self.handle.expect( [
194 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500195
196 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800197 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500198 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400199
kelvin8ec71442015-01-15 16:57:00 -0800200 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800201 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800202 i = self.handle.expect( [
203 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800204 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400205
206 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800207 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800208 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800209 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800210 "config:property-set -p org.apache.karaf.shell\
211 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800212 karafTimeout )
213 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800214 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800215 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400216 return main.TRUE
217 else:
kelvin8ec71442015-01-15 16:57:00 -0800218 # If failed, send ctrl+c to process and try again
219 main.log.info( "Starting CLI failed. Retrying..." )
220 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800221 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800222 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
223 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400224 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800225 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800226 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800227 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800228 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800229 "config:property-set -p org.apache.karaf.shell\
230 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800231 karafTimeout )
232 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800233 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800234 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400235 return main.TRUE
236 else:
kelvin8ec71442015-01-15 16:57:00 -0800237 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800238 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400239 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400240
Jon Halld4d4b372015-01-28 16:02:41 -0800241 except TypeError:
242 main.log.exception( self.name + ": Object not as expected" )
243 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400244 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800245 main.log.error( self.name + ": EOF exception found" )
246 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400247 main.cleanup()
248 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800249 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800250 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400251 main.cleanup()
252 main.exit()
253
Jon Hallefbd9792015-03-05 16:11:36 -0800254 def log( self, cmdStr, level="" ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800255 """
256 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800257 returns main.TRUE on success
Jon Hallefbd9792015-03-05 16:11:36 -0800258 returns main.FALSE if Error occurred
kelvin-onlab338f5512015-02-06 10:53:16 -0800259 Available level: DEBUG, TRACE, INFO, WARN, ERROR
260 Level defaults to INFO
kelvin-onlab9f541032015-02-04 16:19:53 -0800261 """
262 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800263 lvlStr = ""
264 if level:
265 lvlStr = "--level=" + level
266
kelvin-onlab9f541032015-02-04 16:19:53 -0800267 self.handle.sendline( "" )
268 self.handle.expect( "onos>" )
kelvin-onlab338f5512015-02-06 10:53:16 -0800269 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
kelvin-onlab9f541032015-02-04 16:19:53 -0800270 self.handle.expect( "onos>" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800271
kelvin-onlab9f541032015-02-04 16:19:53 -0800272 response = self.handle.before
273 if re.search( "Error", response ):
274 return main.FALSE
275 return main.TRUE
276
277 except pexpect.EOF:
278 main.log.error( self.name + ": EOF exception found" )
279 main.log.error( self.name + ": " + self.handle.before )
280 main.cleanup()
281 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800282 except Exception:
kelvin-onlabfb521662015-02-27 09:52:40 -0800283 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400284 main.cleanup()
285 main.exit()
286
kelvin-onlabd3b64892015-01-20 13:26:24 -0800287 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800288 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800289 Send a completely user specified string to
290 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400291 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800292
andrewonlaba18f6bf2014-10-13 19:31:54 -0400293 Warning: There are no sanity checking to commands
294 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800295 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400296 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800297 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
298 self.log( logStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800299 self.handle.sendline( cmdStr )
Jon Hall63604932015-02-26 17:09:50 -0800300 i = self.handle.expect( ["onos>", "\$", pexpect.TIMEOUT] )
301 response = self.handle.before
302 if i == 2:
303 self.handle.sendline()
304 self.handle.expect( "\$" )
305 response += self.handle.before
306 print response
307 try:
308 print self.handle.after
309 except:
310 pass
311 # TODO: do something with i
kelvin-onlabd3b64892015-01-20 13:26:24 -0800312 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
kelvin-onlab898a6c62015-01-16 14:13:53 -0800313 + self.name + "." )
Jon Hall7bdfc122015-01-23 11:45:32 -0800314 # Remove control strings from output
kelvin-onlabd3b64892015-01-20 13:26:24 -0800315 ansiEscape = re.compile( r'\x1b[^m]*m' )
Jon Hall63604932015-02-26 17:09:50 -0800316 response = ansiEscape.sub( '', response )
kelvin-onlabfb521662015-02-27 09:52:40 -0800317 # Remove extra return chars that get added
Jon Hall63604932015-02-26 17:09:50 -0800318 response = re.sub( r"\s\r", "", response )
319 response = response.strip()
320 # parse for just the output, remove the cmd from response
321 output = response.split( cmdStr, 1 )[1]
Jon Hall7bdfc122015-01-23 11:45:32 -0800322 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800323 except TypeError:
324 main.log.exception( self.name + ": Object not as expected" )
325 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400326 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800327 main.log.error( self.name + ": EOF exception found" )
328 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400329 main.cleanup()
330 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800331 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800332 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400333 main.cleanup()
334 main.exit()
335
kelvin8ec71442015-01-15 16:57:00 -0800336 # IMPORTANT NOTE:
337 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800338 # the cli command changing 'a:b' with 'aB'.
339 # Ex ) onos:topology > onosTopology
340 # onos:links > onosLinks
341 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800342
kelvin-onlabd3b64892015-01-20 13:26:24 -0800343 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800344 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400345 Adds a new cluster node by ID and address information.
346 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800347 * nodeId
348 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400349 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800350 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800351 """
Jon Hallefbd9792015-03-05 16:11:36 -0800352 # noinspection PyBroadException
andrewonlabc2d05aa2014-10-13 16:51:10 -0400353 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800354 cmdStr = "add-node " + str( nodeId ) + " " +\
355 str( ONOSIp ) + " " + str( tcpPort )
356 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800357 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800358 main.log.error( "Error in adding node" )
359 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800360 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400361 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800362 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400363 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800364 except TypeError:
365 main.log.exception( self.name + ": Object not as expected" )
366 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400367 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800368 main.log.error( self.name + ": EOF exception found" )
369 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400370 main.cleanup()
371 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800372 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800373 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400374 main.cleanup()
375 main.exit()
376
kelvin-onlabd3b64892015-01-20 13:26:24 -0800377 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800378 """
andrewonlab86dc3082014-10-13 18:18:38 -0400379 Removes a cluster by ID
380 Issues command: 'remove-node [<node-id>]'
381 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800382 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800383 """
andrewonlab86dc3082014-10-13 18:18:38 -0400384 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400385
kelvin-onlabd3b64892015-01-20 13:26:24 -0800386 cmdStr = "remove-node " + str( nodeId )
387 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800388 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400389
390 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800391
Jon Halld4d4b372015-01-28 16:02:41 -0800392 except TypeError:
393 main.log.exception( self.name + ": Object not as expected" )
394 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400395 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800396 main.log.error( self.name + ": EOF exception found" )
397 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400398 main.cleanup()
399 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800400 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800401 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400402 main.cleanup()
403 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400404
kelvin8ec71442015-01-15 16:57:00 -0800405 def nodes( self ):
406 """
andrewonlab7c211572014-10-15 16:45:20 -0400407 List the nodes currently visible
408 Issues command: 'nodes'
409 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800410 """
andrewonlab7c211572014-10-15 16:45:20 -0400411 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800412 cmdStr = "nodes"
413 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400414 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800415 except TypeError:
416 main.log.exception( self.name + ": Object not as expected" )
417 return None
andrewonlab7c211572014-10-15 16:45:20 -0400418 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800419 main.log.error( self.name + ": EOF exception found" )
420 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400421 main.cleanup()
422 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800423 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800424 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400425 main.cleanup()
426 main.exit()
427
kelvin8ec71442015-01-15 16:57:00 -0800428 def topology( self ):
429 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400430 Shows the current state of the topology
Jon Hallefbd9792015-03-05 16:11:36 -0800431 by issuing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800432 """
andrewonlab95ce8322014-10-13 14:12:04 -0400433 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800434 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800435 cmdStr = "onos:topology"
436 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800437 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400438 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800439 except TypeError:
440 main.log.exception( self.name + ": Object not as expected" )
441 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400442 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800443 main.log.error( self.name + ": EOF exception found" )
444 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400445 main.cleanup()
446 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800447 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800448 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400449 main.cleanup()
450 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800451
kelvin-onlabd3b64892015-01-20 13:26:24 -0800452 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800453 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800454 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400455 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800456 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400457 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800458 cmdStr = "feature:install " + str( featureStr )
459 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800460 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400461 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800462 except TypeError:
463 main.log.exception( self.name + ": Object not as expected" )
464 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400465 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800466 main.log.error( self.name + ": EOF exception found" )
467 main.log.error( self.name + ": " + self.handle.before )
468 main.log.report( "Failed to install feature" )
469 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400470 main.cleanup()
471 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800472 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800473 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800474 main.log.report( "Failed to install feature" )
475 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400476 main.cleanup()
477 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800478
kelvin-onlabd3b64892015-01-20 13:26:24 -0800479 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800480 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400481 Uninstalls a specified feature
482 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800483 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400484 try:
Jon Hall30b82fa2015-03-04 17:15:43 -0800485 cmdStr = 'feature:list -i | grep "' + featureStr + '"'
486 handle = self.sendline( cmdStr )
487 if handle != '':
488 cmdStr = "feature:uninstall " + str( featureStr )
489 self.sendline( cmdStr )
490 # TODO: Check for possible error responses from karaf
491 else:
Jon Hallefbd9792015-03-05 16:11:36 -0800492 main.log.info( "Feature needs to be installed before " +
493 "uninstalling it" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400494 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800495 except TypeError:
496 main.log.exception( self.name + ": Object not as expected" )
497 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400498 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800499 main.log.error( self.name + ": EOF exception found" )
500 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400501 main.cleanup()
502 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800503 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800504 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400505 main.cleanup()
506 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800507
jenkins7ead5a82015-03-13 10:28:21 -0700508 def deviceRemove( self, deviceId ):
509 """
510 Removes particular device from storage
511
512 TODO: refactor this function
513 """
514 try:
515 cmdStr = "device-remove "+str(deviceId)
516 handle = self.sendline( cmdStr )
517 return main.TRUE
518 except TypeError:
519 main.log.exception( self.name + ": Object not as expected" )
520 return None
521 except pexpect.EOF:
522 main.log.error( self.name + ": EOF exception found" )
523 main.log.error( self.name + ": " + self.handle.before )
524 main.cleanup()
525 main.exit()
526 except Exception:
527 main.log.exception( self.name + ": Uncaught exception!" )
528 main.cleanup()
529 main.exit()
530
531
532
kelvin-onlabd3b64892015-01-20 13:26:24 -0800533 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800534 """
Jon Hall7b02d952014-10-17 20:14:54 -0400535 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400536 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800537 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800538 """
andrewonlab86dc3082014-10-13 18:18:38 -0400539 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800540 if jsonFormat:
541 cmdStr = "devices -j"
542 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800543 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800544 handle variable here contains some ANSI escape color code
545 sequences at the end which are invisible in the print command
546 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800547 function. The repr( handle ) output when printed shows the
548 ANSI escape sequences. In json.loads( somestring ), this
549 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800550 json.loads would fail with the escape sequence. So we take off
551 that escape sequence using:
552
kelvin-onlabd3b64892015-01-20 13:26:24 -0800553 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
554 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800555 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800556 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
557 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400558 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400559 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800560 cmdStr = "devices"
561 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400562 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800563 except TypeError:
564 main.log.exception( self.name + ": Object not as expected" )
565 return None
andrewonlab7c211572014-10-15 16:45:20 -0400566 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800567 main.log.error( self.name + ": EOF exception found" )
568 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400569 main.cleanup()
570 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800571 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800572 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400573 main.cleanup()
574 main.exit()
575
kelvin-onlabd3b64892015-01-20 13:26:24 -0800576 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800577 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800578 This balances the devices across all controllers
579 by issuing command: 'onos> onos:balance-masters'
580 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800581 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800582 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800583 cmdStr = "onos:balance-masters"
584 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800585 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800586 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800587 except TypeError:
588 main.log.exception( self.name + ": Object not as expected" )
589 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800590 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800591 main.log.error( self.name + ": EOF exception found" )
592 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800593 main.cleanup()
594 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800595 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800596 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800597 main.cleanup()
598 main.exit()
599
kelvin-onlabd3b64892015-01-20 13:26:24 -0800600 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800601 """
Jon Halle8217482014-10-17 13:49:14 -0400602 Lists all core links
603 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800604 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800605 """
Jon Halle8217482014-10-17 13:49:14 -0400606 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800607 if jsonFormat:
608 cmdStr = "links -j"
609 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800610 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800611 handle variable here contains some ANSI escape color code
612 sequences at the end which are invisible in the print command
613 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800614 function. The repr( handle ) output when printed shows the ANSI
615 escape sequences. In json.loads( somestring ), this somestring
616 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800617 fail with the escape sequence. So we take off that escape
618 sequence using:
619
kelvin-onlabd3b64892015-01-20 13:26:24 -0800620 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
621 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800622 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800623 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
624 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400625 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400626 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800627 cmdStr = "links"
628 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400629 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800630 except TypeError:
631 main.log.exception( self.name + ": Object not as expected" )
632 return None
Jon Halle8217482014-10-17 13:49:14 -0400633 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800634 main.log.error( self.name + ": EOF exception found" )
635 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400636 main.cleanup()
637 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800638 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800639 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400640 main.cleanup()
641 main.exit()
642
kelvin-onlabd3b64892015-01-20 13:26:24 -0800643 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800644 """
Jon Halle8217482014-10-17 13:49:14 -0400645 Lists all ports
646 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800647 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800648 """
Jon Halle8217482014-10-17 13:49:14 -0400649 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800650 if jsonFormat:
651 cmdStr = "ports -j"
652 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800653 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800654 handle variable here contains some ANSI escape color code
655 sequences at the end which are invisible in the print command
656 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800657 function. The repr( handle ) output when printed shows the ANSI
658 escape sequences. In json.loads( somestring ), this somestring
659 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800660 fail with the escape sequence. So we take off that escape
Jon Hallefbd9792015-03-05 16:11:36 -0800661 sequence using the following commands:
Jon Halle3f39ff2015-01-13 11:50:53 -0800662
kelvin-onlabd3b64892015-01-20 13:26:24 -0800663 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
664 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800665 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800666 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
667 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400668 return handle1
669
Jon Halle8217482014-10-17 13:49:14 -0400670 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800671 cmdStr = "ports"
672 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800673 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800674 except TypeError:
675 main.log.exception( self.name + ": Object not as expected" )
676 return None
Jon Halle8217482014-10-17 13:49:14 -0400677 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800678 main.log.error( self.name + ": EOF exception found" )
679 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400680 main.cleanup()
681 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800682 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800683 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400684 main.cleanup()
685 main.exit()
686
kelvin-onlabd3b64892015-01-20 13:26:24 -0800687 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800688 """
Jon Hall983a1702014-10-28 18:44:22 -0400689 Lists all devices and the controllers with roles assigned to them
690 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800691 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800692 """
andrewonlab7c211572014-10-15 16:45:20 -0400693 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800694 if jsonFormat:
695 cmdStr = "roles -j"
696 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800697 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800698 handle variable here contains some ANSI escape color code
699 sequences at the end which are invisible in the print command
700 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800701 function. The repr( handle ) output when printed shows the ANSI
702 escape sequences. In json.loads( somestring ), this somestring
703 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800704 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500705
Jon Halle3f39ff2015-01-13 11:50:53 -0800706 So we take off that escape sequence using the following
707 commads:
708
kelvin-onlabd3b64892015-01-20 13:26:24 -0800709 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
710 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800711 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800712 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
713 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400714 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400715
andrewonlab7c211572014-10-15 16:45:20 -0400716 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800717 cmdStr = "roles"
718 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800719 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800720 except TypeError:
721 main.log.exception( self.name + ": Object not as expected" )
722 return None
Jon Hall983a1702014-10-28 18:44:22 -0400723 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800724 main.log.error( self.name + ": EOF exception found" )
725 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400726 main.cleanup()
727 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800728 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800729 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400730 main.cleanup()
731 main.exit()
732
kelvin-onlabd3b64892015-01-20 13:26:24 -0800733 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800734 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800735 Given the a string containing the json representation of the "roles"
736 cli command and a partial or whole device id, returns a json object
737 containing the roles output for the first device whose id contains
738 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400739
740 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800741 A dict of the role assignments for the given device or
742 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800743 """
Jon Hall983a1702014-10-28 18:44:22 -0400744 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800745 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400746 return None
747 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800748 rawRoles = self.roles()
749 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800750 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800751 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800752 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800753 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400754 return device
755 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800756 except TypeError:
757 main.log.exception( self.name + ": Object not as expected" )
758 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400759 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800760 main.log.error( self.name + ": EOF exception found" )
761 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400762 main.cleanup()
763 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800764 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800765 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400766 main.cleanup()
767 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800768
kelvin-onlabd3b64892015-01-20 13:26:24 -0800769 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800770 """
Jon Hall94fd0472014-12-08 11:52:42 -0800771 Iterates through each device and checks if there is a master assigned
772 Returns: main.TRUE if each device has a master
773 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800774 """
Jon Hall94fd0472014-12-08 11:52:42 -0800775 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800776 rawRoles = self.roles()
777 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800778 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800779 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800780 # print device
781 if device[ 'master' ] == "none":
782 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800783 return main.FALSE
784 return main.TRUE
785
Jon Halld4d4b372015-01-28 16:02:41 -0800786 except TypeError:
787 main.log.exception( self.name + ": Object not as expected" )
788 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800789 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800790 main.log.error( self.name + ": EOF exception found" )
791 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800792 main.cleanup()
793 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800794 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800795 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800796 main.cleanup()
797 main.exit()
798
kelvin-onlabd3b64892015-01-20 13:26:24 -0800799 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800800 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400801 Returns string of paths, and the cost.
802 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800803 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400804 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800805 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
806 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800807 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800808 main.log.error( "Error in getting paths" )
809 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400810 else:
kelvin8ec71442015-01-15 16:57:00 -0800811 path = handle.split( ";" )[ 0 ]
812 cost = handle.split( ";" )[ 1 ]
813 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800814 except TypeError:
815 main.log.exception( self.name + ": Object not as expected" )
816 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400817 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800818 main.log.error( self.name + ": EOF exception found" )
819 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400820 main.cleanup()
821 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800822 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800823 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400824 main.cleanup()
825 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800826
kelvin-onlabd3b64892015-01-20 13:26:24 -0800827 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800828 """
Jon Hallffb386d2014-11-21 13:43:38 -0800829 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400830 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800831 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800832 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400833 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800834 if jsonFormat:
835 cmdStr = "hosts -j"
836 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800837 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800838 handle variable here contains some ANSI escape color code
839 sequences at the end which are invisible in the print command
840 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800841 function. The repr( handle ) output when printed shows the ANSI
842 escape sequences. In json.loads( somestring ), this somestring
843 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800844 fail with the escape sequence. So we take off that escape
845 sequence using:
846
kelvin-onlabd3b64892015-01-20 13:26:24 -0800847 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
848 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800849 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800850 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
851 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400852 return handle1
853 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800854 cmdStr = "hosts"
855 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400856 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800857 except TypeError:
858 main.log.exception( self.name + ": Object not as expected" )
859 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400860 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800861 main.log.error( self.name + ": EOF exception found" )
862 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400863 main.cleanup()
864 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800865 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800866 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400867 main.cleanup()
868 main.exit()
869
kelvin-onlabd3b64892015-01-20 13:26:24 -0800870 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800871 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400872 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800873
Jon Hallefbd9792015-03-05 16:11:36 -0800874 Note: mac must be a colon separated mac address, but could be a
Jon Halle3f39ff2015-01-13 11:50:53 -0800875 partial mac address
876
Jon Hall42db6dc2014-10-24 19:03:48 -0400877 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800878 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400879 try:
kelvin8ec71442015-01-15 16:57:00 -0800880 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400881 return None
882 else:
883 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800884 rawHosts = self.hosts()
885 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800886 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800887 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800888 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800889 if not host:
890 pass
891 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400892 return host
893 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800894 except TypeError:
895 main.log.exception( self.name + ": Object not as expected" )
896 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400897 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800898 main.log.error( self.name + ": EOF exception found" )
899 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400900 main.cleanup()
901 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800902 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800903 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400904 main.cleanup()
905 main.exit()
906
kelvin-onlabd3b64892015-01-20 13:26:24 -0800907 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800908 """
909 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400910 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800911
andrewonlab3f0a4af2014-10-17 12:25:14 -0400912 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800913 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400914 IMPORTANT:
915 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800916 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400917 Furthermore, it assumes that value of VLAN is '-1'
918 Description:
kelvin8ec71442015-01-15 16:57:00 -0800919 Converts mininet hosts ( h1, h2, h3... ) into
920 ONOS format ( 00:00:00:00:00:01/-1 , ... )
921 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400922 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800923 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400924
kelvin-onlabd3b64892015-01-20 13:26:24 -0800925 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800926 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800927 hostHex = hex( int( host ) ).zfill( 12 )
928 hostHex = str( hostHex ).replace( 'x', '0' )
929 i = iter( str( hostHex ) )
930 hostHex = ":".join( a + b for a, b in zip( i, i ) )
931 hostHex = hostHex + "/-1"
932 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400933
kelvin-onlabd3b64892015-01-20 13:26:24 -0800934 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400935
Jon Halld4d4b372015-01-28 16:02:41 -0800936 except TypeError:
937 main.log.exception( self.name + ": Object not as expected" )
938 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400939 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800940 main.log.error( self.name + ": EOF exception found" )
941 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400942 main.cleanup()
943 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800944 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800945 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400946 main.cleanup()
947 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400948
kelvin-onlabd3b64892015-01-20 13:26:24 -0800949 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800950 """
andrewonlabe6745342014-10-17 14:29:13 -0400951 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800952 * hostIdOne: ONOS host id for host1
953 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400954 Description:
Jon Hallefbd9792015-03-05 16:11:36 -0800955 Adds a host-to-host intent ( bidirectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500956 specifying the two hosts.
kelvin-onlabfb521662015-02-27 09:52:40 -0800957 Returns:
958 A string of the intent id or None on Error
kelvin8ec71442015-01-15 16:57:00 -0800959 """
andrewonlabe6745342014-10-17 14:29:13 -0400960 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800961 cmdStr = "add-host-intent " + str( hostIdOne ) +\
962 " " + str( hostIdTwo )
963 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800964 if re.search( "Error", handle ):
965 main.log.error( "Error in adding Host intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800966 return None
Hari Krishnaac4e1782015-01-26 12:09:12 -0800967 else:
968 main.log.info( "Host intent installed between " +
kelvin-onlabfb521662015-02-27 09:52:40 -0800969 str( hostIdOne ) + " and " + str( hostIdTwo ) )
970 match = re.search('id=0x([\da-f]+),', handle)
971 if match:
972 return match.group()[3:-1]
973 else:
974 main.log.error( "Error, intent ID not found" )
975 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800976 except TypeError:
977 main.log.exception( self.name + ": Object not as expected" )
978 return None
andrewonlabe6745342014-10-17 14:29:13 -0400979 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800980 main.log.error( self.name + ": EOF exception found" )
981 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400982 main.cleanup()
983 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800984 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800985 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400986 main.cleanup()
987 main.exit()
988
kelvin-onlabd3b64892015-01-20 13:26:24 -0800989 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800990 """
andrewonlab7b31d232014-10-24 13:31:47 -0400991 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800992 * ingressDevice: device id of ingress device
993 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400994 Optional:
995 TODO: Still needs to be implemented via dev side
kelvin-onlabfb521662015-02-27 09:52:40 -0800996 Description:
997 Adds an optical intent by specifying an ingress and egress device
998 Returns:
999 A string of the intent id or None on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001000 """
andrewonlab7b31d232014-10-24 13:31:47 -04001001 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001002 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
1003 " " + str( egressDevice )
1004 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001005 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -08001006 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001007 main.log.error( "Error in adding Optical intent" )
1008 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001009 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001010 main.log.info( "Optical intent installed between " +
1011 str( ingressDevice ) + " and " +
1012 str( egressDevice ) )
1013 match = re.search('id=0x([\da-f]+),', handle)
1014 if match:
1015 return match.group()[3:-1]
1016 else:
1017 main.log.error( "Error, intent ID not found" )
1018 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001019 except TypeError:
1020 main.log.exception( self.name + ": Object not as expected" )
1021 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001022 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001023 main.log.error( self.name + ": EOF exception found" )
1024 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -04001025 main.cleanup()
1026 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001027 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001028 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -04001029 main.cleanup()
1030 main.exit()
1031
kelvin-onlabd3b64892015-01-20 13:26:24 -08001032 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001033 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001034 ingressDevice,
1035 egressDevice,
1036 portIngress="",
1037 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001038 ethType="",
1039 ethSrc="",
1040 ethDst="",
1041 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001042 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001043 ipProto="",
1044 ipSrc="",
1045 ipDst="",
1046 tcpSrc="",
1047 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001048 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001049 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001050 * ingressDevice: device id of ingress device
1051 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001052 Optional:
1053 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001054 * ethSrc: specify ethSrc ( i.e. src mac addr )
1055 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001056 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001057 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001058 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001059 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001060 * ipSrc: specify ip source address
1061 * ipDst: specify ip destination address
1062 * tcpSrc: specify tcp source port
1063 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001064 Description:
kelvin8ec71442015-01-15 16:57:00 -08001065 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001066 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001067 Returns:
1068 A string of the intent id or None on error
andrewonlab289e4b72014-10-21 21:24:18 -04001069
Jon Halle3f39ff2015-01-13 11:50:53 -08001070 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001071 options developers provide for point-to-point
1072 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001073 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001074 try:
kelvin8ec71442015-01-15 16:57:00 -08001075 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001076 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001077 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001078 and not ipProto and not ipSrc and not ipDst \
1079 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001080 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001081
andrewonlab289e4b72014-10-21 21:24:18 -04001082 else:
andrewonlab36af3822014-11-18 17:48:18 -05001083 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001084
andrewonlab0c0a6772014-10-22 12:31:18 -04001085 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001086 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001087 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001088 cmd += " --ethSrc " + str( ethSrc )
1089 if ethDst:
1090 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001091 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001092 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001093 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001094 cmd += " --lambda "
1095 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001096 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001097 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001098 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001099 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001100 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001101 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001102 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001103 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001104 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001105
kelvin8ec71442015-01-15 16:57:00 -08001106 # Check whether the user appended the port
1107 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001108 if "/" in ingressDevice:
1109 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001110 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001111 if not portIngress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001112 main.log.error( "You must specify the ingress port" )
kelvin8ec71442015-01-15 16:57:00 -08001113 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001114 # Would it make sense to throw an exception and exit
1115 # the test?
1116 return None
andrewonlab36af3822014-11-18 17:48:18 -05001117
kelvin8ec71442015-01-15 16:57:00 -08001118 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001119 str( ingressDevice ) + "/" +\
1120 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001121
kelvin-onlabd3b64892015-01-20 13:26:24 -08001122 if "/" in egressDevice:
1123 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001124 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001125 if not portEgress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001126 main.log.error( "You must specify the egress port" )
1127 return None
Jon Halle3f39ff2015-01-13 11:50:53 -08001128
kelvin8ec71442015-01-15 16:57:00 -08001129 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001130 str( egressDevice ) + "/" +\
1131 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001132
kelvin-onlab898a6c62015-01-16 14:13:53 -08001133 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001134 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001135 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001136 main.log.error( "Error in adding point-to-point intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001137 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001138 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001139 # TODO: print out all the options in this message?
1140 main.log.info( "Point-to-point intent installed between " +
1141 str( ingressDevice ) + " and " +
1142 str( egressDevice ) )
1143 match = re.search('id=0x([\da-f]+),', handle)
1144 if match:
1145 return match.group()[3:-1]
1146 else:
1147 main.log.error( "Error, intent ID not found" )
1148 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001149 except TypeError:
1150 main.log.exception( self.name + ": Object not as expected" )
1151 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001152 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001153 main.log.error( self.name + ": EOF exception found" )
1154 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001155 main.cleanup()
1156 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001157 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001158 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001159 main.cleanup()
1160 main.exit()
1161
kelvin-onlabd3b64892015-01-20 13:26:24 -08001162 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001163 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001164 ingressDevice1,
1165 ingressDevice2,
1166 egressDevice,
kelvin-onlabfb521662015-02-27 09:52:40 -08001167 portIngress1="",
1168 portIngress2="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001169 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001170 ethType="",
1171 ethSrc="",
1172 ethDst="",
1173 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001174 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001175 ipProto="",
1176 ipSrc="",
1177 ipDst="",
1178 tcpSrc="",
1179 tcpDst="",
1180 setEthSrc="",
1181 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001182 """
shahshreyad0c80432014-12-04 16:56:05 -08001183 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001184 This function assumes that there would be 2 ingress devices and
1185 one egress device. For more number of ingress devices, this
1186 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001187 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001188 * ingressDevice1: device id of ingress device1
1189 * ingressDevice2: device id of ingress device2
1190 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001191 Optional:
1192 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001193 * ethSrc: specify ethSrc ( i.e. src mac addr )
1194 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001195 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001196 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001197 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001198 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001199 * ipSrc: specify ip source address
1200 * ipDst: specify ip destination address
1201 * tcpSrc: specify tcp source port
1202 * tcpDst: specify tcp destination port
1203 * setEthSrc: action to Rewrite Source MAC Address
1204 * setEthDst: action to Rewrite Destination MAC Address
1205 Description:
kelvin8ec71442015-01-15 16:57:00 -08001206 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001207 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001208 Returns:
1209 A string of the intent id or None on error
shahshreyad0c80432014-12-04 16:56:05 -08001210
Jon Halle3f39ff2015-01-13 11:50:53 -08001211 NOTE: This function may change depending on the
Jon Hallefbd9792015-03-05 16:11:36 -08001212 options developers provide for multipoint-to-singlepoint
shahshreyad0c80432014-12-04 16:56:05 -08001213 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001214 """
shahshreyad0c80432014-12-04 16:56:05 -08001215 try:
kelvin8ec71442015-01-15 16:57:00 -08001216 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001217 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001218 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001219 and not ipProto and not ipSrc and not ipDst\
1220 and not tcpSrc and not tcpDst and not setEthSrc\
1221 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001222 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001223
1224 else:
1225 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001226
shahshreyad0c80432014-12-04 16:56:05 -08001227 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001228 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001229 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001230 cmd += " --ethSrc " + str( ethSrc )
1231 if ethDst:
1232 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001233 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001234 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001235 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001236 cmd += " --lambda "
1237 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001238 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001239 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001240 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001241 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001242 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001243 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001244 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001245 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001246 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001247 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001248 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001249 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001250 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001251
kelvin8ec71442015-01-15 16:57:00 -08001252 # Check whether the user appended the port
1253 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001254 if "/" in ingressDevice1:
1255 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001256 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001257 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001258 main.log.error( "You must specify " +
1259 "the ingress port1" )
1260 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001261 return None
shahshreyad0c80432014-12-04 16:56:05 -08001262
kelvin8ec71442015-01-15 16:57:00 -08001263 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001264 str( ingressDevice1 ) + "/" +\
1265 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001266
kelvin-onlabd3b64892015-01-20 13:26:24 -08001267 if "/" in ingressDevice2:
1268 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001269 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001270 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001271 main.log.error( "You must specify " +
1272 "the ingress port2" )
1273 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001274 return None
shahshreyad0c80432014-12-04 16:56:05 -08001275
kelvin8ec71442015-01-15 16:57:00 -08001276 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001277 str( ingressDevice2 ) + "/" +\
1278 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001279
kelvin-onlabd3b64892015-01-20 13:26:24 -08001280 if "/" in egressDevice:
1281 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001282 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001283 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001284 main.log.error( "You must specify " +
1285 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001286 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001287
kelvin8ec71442015-01-15 16:57:00 -08001288 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001289 str( egressDevice ) + "/" +\
1290 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001291 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001292 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001293 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001294 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001295 main.log.error( "Error in adding multipoint-to-singlepoint " +
1296 "intent" )
1297 return None
shahshreyad0c80432014-12-04 16:56:05 -08001298 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001299 # TODO: print out all the options in this message?
1300 main.log.info( "Multipoint-to-singlepoint intent installed" +
1301 " between " + str( ingressDevice1 ) + ", " +
1302 str( ingressDevice2 ) + " and " +
1303 str( egressDevice ) )
1304 match = re.search('id=0x([\da-f]+),', handle)
1305 if match:
1306 return match.group()[3:-1]
1307 else:
1308 main.log.error( "Error, intent ID not found" )
1309 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001310 except TypeError:
1311 main.log.exception( self.name + ": Object not as expected" )
1312 return None
shahshreyad0c80432014-12-04 16:56:05 -08001313 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001314 main.log.error( self.name + ": EOF exception found" )
1315 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001316 main.cleanup()
1317 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001318 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001319 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001320 main.cleanup()
1321 main.exit()
1322
Jon Hallefbd9792015-03-05 16:11:36 -08001323 def removeIntent( self, intentId, app='org.onosproject.cli',
1324 purge=False, sync=False ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001325 """
shahshreya1c818fc2015-02-26 13:44:08 -08001326 Remove intent for specified application id and intent id
1327 Optional args:-
1328 -s or --sync: Waits for the removal before returning
1329 -p or --purge: Purge the intent from the store after removal
1330
Jon Halle3f39ff2015-01-13 11:50:53 -08001331 Returns:
1332 main.False on error and
1333 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001334 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001335 try:
shahshreya1c818fc2015-02-26 13:44:08 -08001336 cmdStr = "remove-intent "
1337 if purge:
1338 cmdStr += " -p"
1339 if sync:
1340 cmdStr += " -s"
1341
1342 cmdStr += " " + app + " " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001343 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001344 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001345 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001346 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001347 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001348 # TODO: Should this be main.TRUE
1349 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001350 except TypeError:
1351 main.log.exception( self.name + ": Object not as expected" )
1352 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001353 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001354 main.log.error( self.name + ": EOF exception found" )
1355 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001356 main.cleanup()
1357 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001358 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001359 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001360 main.cleanup()
1361 main.exit()
1362
kelvin-onlabd3b64892015-01-20 13:26:24 -08001363 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001364 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001365 NOTE: This method should be used after installing application:
1366 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001367 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001368 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001369 Description:
1370 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001371 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001372 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001373 if jsonFormat:
1374 cmdStr = "routes -j"
1375 handleTmp = self.sendline( cmdStr )
1376 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1377 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001378 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001379 cmdStr = "routes"
1380 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001381 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001382 except TypeError:
1383 main.log.exception( self.name + ": Object not as expected" )
1384 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001385 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001386 main.log.error( self.name + ": EOF exception found" )
1387 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001388 main.cleanup()
1389 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001390 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001391 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001392 main.cleanup()
1393 main.exit()
1394
kelvin-onlabd3b64892015-01-20 13:26:24 -08001395 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001396 """
andrewonlab377693f2014-10-21 16:00:30 -04001397 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001398 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001399 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001400 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001401 """
andrewonlabe6745342014-10-17 14:29:13 -04001402 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001403 if jsonFormat:
1404 cmdStr = "intents -j"
1405 handle = self.sendline( cmdStr )
1406 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1407 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001408 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001409 cmdStr = "intents"
1410 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001411 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001412 except TypeError:
1413 main.log.exception( self.name + ": Object not as expected" )
1414 return None
andrewonlabe6745342014-10-17 14:29:13 -04001415 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001416 main.log.error( self.name + ": EOF exception found" )
1417 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001418 main.cleanup()
1419 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001420 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001421 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001422 main.cleanup()
1423 main.exit()
1424
kelvin-onlab54400a92015-02-26 18:05:51 -08001425 def getIntentState(self, intentsId, intentsJson=None):
1426 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001427 Check intent state.
1428 Accepts a single intent ID (string type) or a list of intent IDs.
1429 Returns the state(string type) of the id if a single intent ID is
1430 accepted.
Jon Hallefbd9792015-03-05 16:11:36 -08001431 Returns a dictionary with intent IDs as the key and its
1432 corresponding states as the values
kelvin-onlabfb521662015-02-27 09:52:40 -08001433 Parameters:
kelvin-onlab54400a92015-02-26 18:05:51 -08001434 intentId: intent ID (string type)
1435 intentsJson: parsed json object from the onos:intents api
1436 Returns:
1437 state = An intent's state- INSTALL,WITHDRAWN etc.
1438 stateDict = Dictionary of intent's state. intent ID as the keys and
1439 state as the values.
1440 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001441 try:
1442 state = "State is Undefined"
1443 if not intentsJson:
Jon Hallefbd9792015-03-05 16:11:36 -08001444 intentsJsonTemp = json.loads( self.intents() )
kelvin-onlab54400a92015-02-26 18:05:51 -08001445 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001446 intentsJsonTemp = json.loads( intentsJson )
1447 if isinstance( intentsId, types.StringType ):
kelvin-onlab54400a92015-02-26 18:05:51 -08001448 for intent in intentsJsonTemp:
1449 if intentsId == intent['id']:
1450 state = intent['state']
1451 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001452 main.log.info( "Cannot find intent ID" + str( intentsId ) +
1453 " on the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001454 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001455 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001456 dictList = []
kelvin-onlab54400a92015-02-26 18:05:51 -08001457 for ID in intentsId:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001458 stateDict = {}
kelvin-onlab54400a92015-02-26 18:05:51 -08001459 for intents in intentsJsonTemp:
1460 if ID == intents['id']:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001461 stateDict['state'] = intents['state']
1462 stateDict['id'] = ID
Jon Hallefbd9792015-03-05 16:11:36 -08001463 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08001464 break
Jon Hallefbd9792015-03-05 16:11:36 -08001465 if len( intentsId ) != len( dictList ):
1466 main.log.info( "Cannot find some of the intent ID state" )
kelvin-onlab07dbd012015-03-04 16:29:39 -08001467 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08001468 else:
1469 main.log.info("Invalid intents ID entry")
1470 return None
kelvin-onlab54400a92015-02-26 18:05:51 -08001471 except TypeError:
1472 main.log.exception( self.name + ": Object not as expected" )
1473 return None
1474 except pexpect.EOF:
1475 main.log.error( self.name + ": EOF exception found" )
1476 main.log.error( self.name + ": " + self.handle.before )
1477 main.cleanup()
1478 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001479 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08001480 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001481 main.cleanup()
1482 main.exit()
1483
kelvin-onlabd3b64892015-01-20 13:26:24 -08001484 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001485 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001486 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001487 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001488 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001489 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001490 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001491 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001492 if jsonFormat:
1493 cmdStr = "flows -j"
1494 handle = self.sendline( cmdStr )
1495 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1496 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001497 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001498 cmdStr = "flows"
1499 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001500 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001501 main.log.error( self.name + ".flows() response: " +
1502 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001503 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001504 except TypeError:
1505 main.log.exception( self.name + ": Object not as expected" )
1506 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001507 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001508 main.log.error( self.name + ": EOF exception found" )
1509 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001510 main.cleanup()
1511 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001512 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001513 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001514 main.cleanup()
1515 main.exit()
1516
kelvin-onlabd3b64892015-01-20 13:26:24 -08001517 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
Jon Hallefbd9792015-03-05 16:11:36 -08001518 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001519 """
andrewonlab87852b02014-11-19 18:44:19 -05001520 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001521 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001522 a specific point-to-point intent definition
1523 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001524 * dpidSrc: specify source dpid
1525 * dpidDst: specify destination dpid
1526 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001527 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001528 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001529 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001530 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001531 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001532 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001533 """
andrewonlab87852b02014-11-19 18:44:19 -05001534 try:
kelvin8ec71442015-01-15 16:57:00 -08001535 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001536 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1537 str( numIntents )
1538 if numMult:
1539 cmd += " " + str( numMult )
1540 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001541 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001542 if appId:
1543 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001544 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001545 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1546 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001547 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001548 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001549 main.log.info( handle )
1550 # Split result by newline
1551 newline = handle.split( "\r\r\n" )
1552 # Ignore the first object of list, which is empty
1553 newline = newline[ 1: ]
1554 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001555 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001556 result = result.split( ": " )
1557 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001558 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1559 main.log.info( latResult )
1560 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001561 else:
1562 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001563 except TypeError:
1564 main.log.exception( self.name + ": Object not as expected" )
1565 return None
andrewonlab87852b02014-11-19 18:44:19 -05001566 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001567 main.log.error( self.name + ": EOF exception found" )
1568 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001569 main.cleanup()
1570 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001571 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001572 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001573 main.cleanup()
1574 main.exit()
1575
kelvin-onlabd3b64892015-01-20 13:26:24 -08001576 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001577 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001578 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001579 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001580 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001581 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001582 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001583 if jsonFormat:
1584 cmdStr = "intents-events-metrics -j"
1585 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001586 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001587 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1588 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001589 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001590 cmdStr = "intents-events-metrics"
1591 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001592 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001593 except TypeError:
1594 main.log.exception( self.name + ": Object not as expected" )
1595 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001596 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001597 main.log.error( self.name + ": EOF exception found" )
1598 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001599 main.cleanup()
1600 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001601 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001602 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001603 main.cleanup()
1604 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001605
kelvin-onlabd3b64892015-01-20 13:26:24 -08001606 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001607 """
1608 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001609 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001610 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001611 """
andrewonlab867212a2014-10-22 20:13:38 -04001612 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001613 if jsonFormat:
1614 cmdStr = "topology-events-metrics -j"
1615 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001616 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001617 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1618 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001619 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001620 cmdStr = "topology-events-metrics"
1621 handle = self.sendline( cmdStr )
jenkins7ead5a82015-03-13 10:28:21 -07001622 if handle:
1623 return handle
1624 else:
1625 # Return empty json
1626 return '{}'
Jon Halld4d4b372015-01-28 16:02:41 -08001627 except TypeError:
1628 main.log.exception( self.name + ": Object not as expected" )
1629 return None
andrewonlab867212a2014-10-22 20:13:38 -04001630 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001631 main.log.error( self.name + ": EOF exception found" )
1632 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001633 main.cleanup()
1634 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001635 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001636 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001637 main.cleanup()
1638 main.exit()
1639
kelvin8ec71442015-01-15 16:57:00 -08001640 # Wrapper functions ****************
1641 # Wrapper functions use existing driver
1642 # functions and extends their use case.
1643 # For example, we may use the output of
1644 # a normal driver function, and parse it
1645 # using a wrapper function
andrewonlab7e4d2d32014-10-15 13:23:21 -04001646
kelvin-onlabd3b64892015-01-20 13:26:24 -08001647 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001648 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001649 Description:
1650 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001651 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001652 try:
kelvin8ec71442015-01-15 16:57:00 -08001653 # Obtain output of intents function
kelvin-onlabfb521662015-02-27 09:52:40 -08001654 intentsStr = self.intents(jsonFormat=False)
kelvin-onlabd3b64892015-01-20 13:26:24 -08001655 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001656
kelvin8ec71442015-01-15 16:57:00 -08001657 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001658 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1659 for intents in intentsList:
kelvin-onlabfb521662015-02-27 09:52:40 -08001660 match = re.search('id=0x([\da-f]+),', intents)
1661 if match:
1662 tmpId = match.group()[3:-1]
1663 intentIdList.append( tmpId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001664 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001665
Jon Halld4d4b372015-01-28 16:02:41 -08001666 except TypeError:
1667 main.log.exception( self.name + ": Object not as expected" )
1668 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001669 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001670 main.log.error( self.name + ": EOF exception found" )
1671 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001672 main.cleanup()
1673 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001674 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001675 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001676 main.cleanup()
1677 main.exit()
1678
Jon Hall30b82fa2015-03-04 17:15:43 -08001679 def FlowAddedCount( self, deviceId ):
1680 """
1681 Determine the number of flow rules for the given device id that are
1682 in the added state
1683 """
1684 try:
1685 cmdStr = "flows any " + str( deviceId ) + " | " +\
1686 "grep 'state=ADDED' | wc -l"
1687 handle = self.sendline( cmdStr )
1688 return handle
1689 except pexpect.EOF:
1690 main.log.error( self.name + ": EOF exception found" )
1691 main.log.error( self.name + ": " + self.handle.before )
1692 main.cleanup()
1693 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001694 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -08001695 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001696 main.cleanup()
1697 main.exit()
1698
kelvin-onlabd3b64892015-01-20 13:26:24 -08001699 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001700 """
andrewonlab95ce8322014-10-13 14:12:04 -04001701 Use 'devices' function to obtain list of all devices
1702 and parse the result to obtain a list of all device
1703 id's. Returns this list. Returns empty list if no
1704 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001705 List is ordered sequentially
1706
andrewonlab95ce8322014-10-13 14:12:04 -04001707 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001708 device id, and wish to execute other commands using
andrewonlab95ce8322014-10-13 14:12:04 -04001709 the ids. By obtaining the list of device ids on the fly,
1710 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001711 """
andrewonlab95ce8322014-10-13 14:12:04 -04001712 try:
kelvin8ec71442015-01-15 16:57:00 -08001713 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001714 devicesStr = self.devices( jsonFormat=False )
1715 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001716
kelvin-onlabd3b64892015-01-20 13:26:24 -08001717 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001718 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001719 return idList
kelvin8ec71442015-01-15 16:57:00 -08001720
1721 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001722 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001723 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001724 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001725 # Split list further into arguments before and after string
1726 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001727 # append to idList
1728 for arg in tempList:
1729 idList.append( arg.split( "id=" )[ 1 ] )
1730 return idList
andrewonlab95ce8322014-10-13 14:12:04 -04001731
Jon Halld4d4b372015-01-28 16:02:41 -08001732 except TypeError:
1733 main.log.exception( self.name + ": Object not as expected" )
1734 return None
andrewonlab95ce8322014-10-13 14:12:04 -04001735 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001736 main.log.error( self.name + ": EOF exception found" )
1737 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -04001738 main.cleanup()
1739 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001740 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001741 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -04001742 main.cleanup()
1743 main.exit()
1744
kelvin-onlabd3b64892015-01-20 13:26:24 -08001745 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001746 """
andrewonlab7c211572014-10-15 16:45:20 -04001747 Uses 'nodes' function to obtain list of all nodes
1748 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001749 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001750 Returns:
1751 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001752 """
andrewonlab7c211572014-10-15 16:45:20 -04001753 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001754 nodesStr = self.nodes()
1755 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001756
kelvin-onlabd3b64892015-01-20 13:26:24 -08001757 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001758 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001759 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001760
kelvin-onlabd3b64892015-01-20 13:26:24 -08001761 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001762 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001763
kelvin8ec71442015-01-15 16:57:00 -08001764 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001765 nodesList = nodesStr.split( "," )
1766 tempList = [ node for node in nodesList if "id=" in node ]
1767 for arg in tempList:
1768 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001769
kelvin-onlabd3b64892015-01-20 13:26:24 -08001770 return idList
kelvin8ec71442015-01-15 16:57:00 -08001771
Jon Halld4d4b372015-01-28 16:02:41 -08001772 except TypeError:
1773 main.log.exception( self.name + ": Object not as expected" )
1774 return None
andrewonlab7c211572014-10-15 16:45:20 -04001775 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001776 main.log.error( self.name + ": EOF exception found" )
1777 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001778 main.cleanup()
1779 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001780 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001781 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001782 main.cleanup()
1783 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -04001784
kelvin-onlabd3b64892015-01-20 13:26:24 -08001785 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001786 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001787 Return the first device from the devices api whose 'id' contains 'dpid'
1788 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001789 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001790 try:
kelvin8ec71442015-01-15 16:57:00 -08001791 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001792 return None
1793 else:
kelvin8ec71442015-01-15 16:57:00 -08001794 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001795 rawDevices = self.devices()
1796 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001797 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001798 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001799 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1800 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001801 return device
1802 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001803 except TypeError:
1804 main.log.exception( self.name + ": Object not as expected" )
1805 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001806 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001807 main.log.error( self.name + ": EOF exception found" )
1808 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001809 main.cleanup()
1810 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001811 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001812 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001813 main.cleanup()
1814 main.exit()
1815
kelvin-onlabd3b64892015-01-20 13:26:24 -08001816 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001817 """
Jon Hallefbd9792015-03-05 16:11:36 -08001818 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001819 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001820 log level can be specified.
kelvin8ec71442015-01-15 16:57:00 -08001821
Jon Hall42db6dc2014-10-24 19:03:48 -04001822 Params: ip = ip used for the onos cli
1823 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001824 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001825 logLevel = level to log to. Currently accepts
1826 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001827
1828
kelvin-onlabd3b64892015-01-20 13:26:24 -08001829 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001830
Jon Hallefbd9792015-03-05 16:11:36 -08001831 Returns: main.TRUE if the number of switches and links are correct,
1832 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04001833 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001834 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001835 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001836 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001837 if topology == {}:
1838 return main.ERROR
1839 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001840 # Is the number of switches is what we expected
1841 devices = topology.get( 'devices', False )
1842 links = topology.get( 'links', False )
kelvin-onlabfb521662015-02-27 09:52:40 -08001843 if devices is False or links is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04001844 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001845 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001846 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001847 linkCheck = ( int( links ) == int( numolink ) )
1848 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001849 # We expected the correct numbers
Jon Hallefbd9792015-03-05 16:11:36 -08001850 output += "The number of links and switches match " +\
1851 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001852 result = main.TRUE
1853 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001854 output += "The number of links and switches does not match " +\
1855 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001856 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001857 output = output + "\n ONOS sees %i devices (%i expected) \
1858 and %i links (%i expected)" % (
1859 int( devices ), int( numoswitch ), int( links ),
1860 int( numolink ) )
1861 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001862 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001863 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001864 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001865 else:
kelvin8ec71442015-01-15 16:57:00 -08001866 main.log.info( output )
1867 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001868 except TypeError:
1869 main.log.exception( self.name + ": Object not as expected" )
1870 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001871 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001872 main.log.error( self.name + ": EOF exception found" )
1873 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001874 main.cleanup()
1875 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001876 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001877 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001878 main.cleanup()
1879 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001880
kelvin-onlabd3b64892015-01-20 13:26:24 -08001881 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001882 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001883 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001884 deviceId must be the id of a device as seen in the onos devices command
1885 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001886 role must be either master, standby, or none
1887
Jon Halle3f39ff2015-01-13 11:50:53 -08001888 Returns:
1889 main.TRUE or main.FALSE based on argument verification and
1890 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001891 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001892 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001893 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001894 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001895 cmdStr = "device-role " +\
1896 str( deviceId ) + " " +\
1897 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001898 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001899 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001900 if re.search( "Error", handle ):
1901 # end color output to escape any colours
1902 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001903 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001904 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001905 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001906 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001907 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001908 main.log.error( "Invalid 'role' given to device_role(). " +
1909 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001910 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001911 except TypeError:
1912 main.log.exception( self.name + ": Object not as expected" )
1913 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001914 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001915 main.log.error( self.name + ": EOF exception found" )
1916 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001917 main.cleanup()
1918 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001919 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001920 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001921 main.cleanup()
1922 main.exit()
1923
kelvin-onlabd3b64892015-01-20 13:26:24 -08001924 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001925 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001926 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001927 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001928 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001929 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001930 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001931 if jsonFormat:
1932 cmdStr = "clusters -j"
1933 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001934 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001935 handle variable here contains some ANSI escape color code
1936 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001937 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001938 function. The repr( handle ) output when printed shows the ANSI
1939 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001940 variable is actually repr( somestring ) and json.loads would
1941 fail with the escape sequence. So we take off that escape
1942 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001943
kelvin-onlabd3b64892015-01-20 13:26:24 -08001944 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1945 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001946 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001947 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1948 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001949 return handle1
1950 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001951 cmdStr = "clusters"
1952 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001953 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001954 except TypeError:
1955 main.log.exception( self.name + ": Object not as expected" )
1956 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001957 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001958 main.log.error( self.name + ": EOF exception found" )
1959 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001960 main.cleanup()
1961 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001962 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001963 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001964 main.cleanup()
1965 main.exit()
1966
kelvin-onlabd3b64892015-01-20 13:26:24 -08001967 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001968 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001969 CLI command to get the current leader for the Election test application
1970 NOTE: Requires installation of the onos-app-election feature
1971 Returns: Node IP of the leader if one exists
1972 None if none exists
1973 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001974 """
Jon Hall94fd0472014-12-08 11:52:42 -08001975 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001976 cmdStr = "election-test-leader"
1977 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001978 # Leader
1979 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001980 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001981 nodeSearch = re.search( leaderPattern, response )
1982 if nodeSearch:
1983 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001984 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001985 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001986 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001987 # no leader
1988 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001989 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001990 nullSearch = re.search( nullPattern, response )
1991 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001992 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001993 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001994 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001995 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001996 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001997 if re.search( errorPattern, response ):
1998 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001999 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08002000 return main.FALSE
2001 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002002 main.log.error( "Error in election_test_leader: " +
2003 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08002004 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002005 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002006 except TypeError:
2007 main.log.exception( self.name + ": Object not as expected" )
2008 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002009 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002010 main.log.error( self.name + ": EOF exception found" )
2011 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002012 main.cleanup()
2013 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002014 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002015 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002016 main.cleanup()
2017 main.exit()
2018
kelvin-onlabd3b64892015-01-20 13:26:24 -08002019 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002020 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002021 CLI command to run for leadership of the Election test application.
2022 NOTE: Requires installation of the onos-app-election feature
2023 Returns: Main.TRUE on success
2024 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08002025 """
Jon Hall94fd0472014-12-08 11:52:42 -08002026 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002027 cmdStr = "election-test-run"
2028 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002029 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002030 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002031 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002032 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08002033 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08002034 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002035 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002036 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002037 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002038 errorPattern = "Command\snot\sfound"
2039 if re.search( errorPattern, response ):
2040 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002041 return main.FALSE
2042 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002043 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002044 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002045 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002046 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002047 except TypeError:
2048 main.log.exception( self.name + ": Object not as expected" )
2049 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002050 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002051 main.log.error( self.name + ": EOF exception found" )
2052 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002053 main.cleanup()
2054 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002055 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002056 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002057 main.cleanup()
2058 main.exit()
2059
kelvin-onlabd3b64892015-01-20 13:26:24 -08002060 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08002061 """
Jon Hall94fd0472014-12-08 11:52:42 -08002062 * CLI command to withdraw the local node from leadership election for
2063 * the Election test application.
2064 #NOTE: Requires installation of the onos-app-election feature
2065 Returns: Main.TRUE on success
2066 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08002067 """
Jon Hall94fd0472014-12-08 11:52:42 -08002068 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002069 cmdStr = "election-test-withdraw"
2070 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002071 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002072 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002073 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002074 if re.search( successPattern, response ):
2075 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002076 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002077 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002078 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002079 errorPattern = "Command\snot\sfound"
2080 if re.search( errorPattern, response ):
2081 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002082 return main.FALSE
2083 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002084 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002085 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002086 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002087 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002088 except TypeError:
2089 main.log.exception( self.name + ": Object not as expected" )
2090 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -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 )
Jon Hall94fd0472014-12-08 11:52:42 -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!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002098 main.cleanup()
2099 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002100
kelvin8ec71442015-01-15 16:57:00 -08002101 def getDevicePortsEnabledCount( 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:
Jon Halle3f39ff2015-01-13 11:50:53 -08002106 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002107 cmdStr = "onos:ports -e " + dpid + " | wc -l"
2108 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002109 if re.search( "No such device", output ):
2110 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 getDeviceLinksActiveCount( self, dpid ):
2128 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002129 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002130 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002131 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002132 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002133 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
2134 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002135 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002136 main.log.error( "Error in getting ports " )
2137 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002138 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002139 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002140 except TypeError:
2141 main.log.exception( self.name + ": Object not as expected" )
2142 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002143 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002144 main.log.error( self.name + ": EOF exception found" )
2145 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002146 main.cleanup()
2147 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002148 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002149 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002150 main.cleanup()
2151 main.exit()
2152
kelvin8ec71442015-01-15 16:57:00 -08002153 def getAllIntentIds( self ):
2154 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002155 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002156 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002157 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002158 cmdStr = "onos:intents | grep id="
2159 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002160 if re.search( "Error", output ):
2161 main.log.error( "Error in getting ports" )
2162 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002163 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002164 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002165 except TypeError:
2166 main.log.exception( self.name + ": Object not as expected" )
2167 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002168 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002169 main.log.error( self.name + ": EOF exception found" )
2170 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002171 main.cleanup()
2172 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002173 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002174 main.log.exception( self.name + ": Uncaught exception!" )
2175 main.cleanup()
2176 main.exit()
2177
Jon Hall73509952015-02-24 16:42:56 -08002178 def intentSummary( self ):
2179 """
Jon Hallefbd9792015-03-05 16:11:36 -08002180 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08002181 """
2182 try:
2183 intents = self.intents( )
2184 intentStates = []
Jon Hall63604932015-02-26 17:09:50 -08002185 for intent in json.loads( intents ): # Iter through intents of a node
Jon Hall73509952015-02-24 16:42:56 -08002186 intentStates.append( intent.get( 'state', None ) )
Jon Hall63604932015-02-26 17:09:50 -08002187 out = [ (i, intentStates.count( i ) ) for i in set( intentStates ) ]
2188 main.log.info( dict( out ) )
Jon Hall73509952015-02-24 16:42:56 -08002189 return dict( out )
2190 except TypeError:
2191 main.log.exception( self.name + ": Object not as expected" )
2192 return None
2193 except pexpect.EOF:
2194 main.log.error( self.name + ": EOF exception found" )
2195 main.log.error( self.name + ": " + self.handle.before )
2196 main.cleanup()
2197 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002198 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08002199 main.log.exception( self.name + ": Uncaught exception!" )
2200 main.cleanup()
2201 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002202
2203 def leaders( self ):
2204 """
2205 Returns the output of the leaders command.
2206 """
2207 # FIXME: add json output
2208 try:
2209 output = self.sendline( "onos:leaders" )
2210 main.log.warn( output )
2211 return output
2212 except TypeError:
2213 main.log.exception( self.name + ": Object not as expected" )
2214 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002215 except pexpect.EOF:
2216 main.log.error( self.name + ": EOF exception found" )
2217 main.log.error( self.name + ": " + self.handle.before )
2218 main.cleanup()
2219 main.exit()
2220 except:
Jon Hall63604932015-02-26 17:09:50 -08002221 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002222 main.cleanup()
2223 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002224
2225 def pendingMap( self ):
2226 """
2227 Returns the output of the intent Pending map.
2228 """
2229 # FIXME: add json output
2230 try:
2231 output = self.sendline( "onos:intents -p" )
2232 main.log.warn( output )
2233 return output
2234 except TypeError:
2235 main.log.exception( self.name + ": Object not as expected" )
2236 return None
2237 except pexpect.EOF:
2238 main.log.error( self.name + ": EOF exception found" )
2239 main.log.error( self.name + ": " + self.handle.before )
2240 main.cleanup()
2241 main.exit()
2242 except:
2243 main.log.exception( self.name + ": Uncaught exception!" )
2244 main.cleanup()
2245 main.exit()
2246
2247 def partitions( self ):
2248 """
2249 Returns the output of the raft partitions command for ONOS.
2250 """
2251 # FIXME: add json output
2252 try:
2253 output = self.sendline( "partitions" )
2254 main.log.warn( output )
2255 return output
2256 except TypeError:
2257 main.log.exception( self.name + ": Object not as expected" )
2258 return None
2259 except pexpect.EOF:
2260 main.log.error( self.name + ": EOF exception found" )
2261 main.log.error( self.name + ": " + self.handle.before )
2262 main.cleanup()
2263 main.exit()
2264 except:
2265 main.log.exception( self.name + ": Uncaught exception!" )
2266 main.cleanup()
2267 main.exit()
2268