blob: 419da23223af65d4447442295e913cb48441842c [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
kelvin8ec71442015-01-15 16:57:00 -08003"""
andrewonlab95ce8322014-10-13 14:12:04 -04004This driver enters the onos> prompt to issue commands.
5
kelvin8ec71442015-01-15 16:57:00 -08006Please follow the coding style demonstrated by existing
andrewonlab95ce8322014-10-13 14:12:04 -04007functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040014shreya@onlab.us
andrewonlab95ce8322014-10-13 14:12:04 -040015
16OCT 13 2014
17
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab95ce8322014-10-13 14:12:04 -040019import sys
andrewonlab95ce8322014-10-13 14:12:04 -040020import pexpect
21import re
Jon Hall30b82fa2015-03-04 17:15:43 -080022import json
23import types
kelvin8ec71442015-01-15 16:57:00 -080024sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040025from drivers.common.clidriver import CLI
26
andrewonlab95ce8322014-10-13 14:12:04 -040027
kelvin8ec71442015-01-15 16:57:00 -080028class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040029
kelvin8ec71442015-01-15 16:57:00 -080030 def __init__( self ):
31 """
32 Initialize client
33 """
Jon Hallefbd9792015-03-05 16:11:36 -080034 self.name = None
35 self.home = None
36 self.handle = None
kelvin8ec71442015-01-15 16:57:00 -080037 super( CLI, self ).__init__()
38
39 def connect( self, **connectargs ):
40 """
andrewonlab95ce8322014-10-13 14:12:04 -040041 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080042 """
andrewonlab95ce8322014-10-13 14:12:04 -040043 try:
44 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080045 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us658ec012015-03-11 15:13:09 -070046 self.home = "~/onos"
andrewonlab95ce8322014-10-13 14:12:04 -040047 for key in self.options:
48 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080049 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040050 break
kelvin-onlabfb521662015-02-27 09:52:40 -080051 if self.home is None or self.home == "":
kelvin-onlabd6634ac2015-01-29 14:23:10 -080052 self.home = "~/ONOS"
andrewonlab95ce8322014-10-13 14:12:04 -040053
kelvin8ec71442015-01-15 16:57:00 -080054 self.name = self.options[ 'name' ]
55 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080056 user_name=self.user_name,
57 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080058 port=self.port,
59 pwd=self.pwd,
60 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040061
kelvin8ec71442015-01-15 16:57:00 -080062 self.handle.sendline( "cd " + self.home )
63 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040064 if self.handle:
65 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080066 else:
67 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040068 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080069 except TypeError:
70 main.log.exception( self.name + ": Object not as expected" )
71 return None
andrewonlab95ce8322014-10-13 14:12:04 -040072 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080073 main.log.error( self.name + ": EOF exception found" )
74 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040075 main.cleanup()
76 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080077 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -080078 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -040079 main.cleanup()
80 main.exit()
81
kelvin8ec71442015-01-15 16:57:00 -080082 def disconnect( self ):
83 """
andrewonlab95ce8322014-10-13 14:12:04 -040084 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080085 """
Jon Halld61331b2015-02-17 16:35:47 -080086 response = main.TRUE
Jon Hallefbd9792015-03-05 16:11:36 -080087 # noinspection PyBroadException
andrewonlab95ce8322014-10-13 14:12:04 -040088 try:
Jon Hallb7fce3e2015-03-04 10:18:32 -080089 self.logout()
kelvin8ec71442015-01-15 16:57:00 -080090 self.handle.sendline( "" )
91 self.handle.expect( "\$" )
92 self.handle.sendline( "exit" )
93 self.handle.expect( "closed" )
Jon Halld4d4b372015-01-28 16:02:41 -080094 except TypeError:
95 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -080096 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -040097 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080098 main.log.error( self.name + ": EOF exception found" )
99 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800100 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800101 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400102 response = main.FALSE
103 return response
104
kelvin8ec71442015-01-15 16:57:00 -0800105 def logout( self ):
106 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500107 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800108 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500109 try:
kelvin8ec71442015-01-15 16:57:00 -0800110 self.handle.sendline( "" )
Jon Hallb7fce3e2015-03-04 10:18:32 -0800111 i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ],
112 timeout=10 )
113 if i == 0: # In ONOS CLI
kelvin8ec71442015-01-15 16:57:00 -0800114 self.handle.sendline( "logout" )
115 self.handle.expect( "\$" )
Jon Hallb7fce3e2015-03-04 10:18:32 -0800116 elif i == 1: # not in CLI
andrewonlab9627f432014-11-14 12:45:10 -0500117 return main.TRUE
Jon Hallb7fce3e2015-03-04 10:18:32 -0800118 elif i == 3: # Timeout
119 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -0800120 except TypeError:
121 main.log.exception( self.name + ": Object not as expected" )
122 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500123 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800124 main.log.error( self.name + ": eof exception found" )
125 main.log.error( self.name + ": " +
126 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500127 main.cleanup()
128 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800129 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800130 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500131 main.cleanup()
132 main.exit()
133
kelvin-onlabd3b64892015-01-20 13:26:24 -0800134 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800135 """
andrewonlab95ce8322014-10-13 14:12:04 -0400136 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800137
andrewonlab95ce8322014-10-13 14:12:04 -0400138 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800139 """
andrewonlab95ce8322014-10-13 14:12:04 -0400140 try:
141 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800142 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400143 main.cleanup()
144 main.exit()
145 else:
kelvin8ec71442015-01-15 16:57:00 -0800146 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800147 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800148 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400149 # and that this driver will have to change accordingly
Cameron Franke9c94fb02015-01-21 10:20:20 -0800150 self.handle.expect(str(cellname))
andrew@onlab.usc400b112015-01-21 15:33:19 -0800151 handleBefore = self.handle.before
152 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800153 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800154 self.handle.sendline("")
155 self.handle.expect("\$")
andrew@onlab.usc400b112015-01-21 15:33:19 -0800156 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400157
kelvin-onlabd3b64892015-01-20 13:26:24 -0800158 main.log.info( "Cell call returned: " + handleBefore +
159 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400160
161 return main.TRUE
162
Jon Halld4d4b372015-01-28 16:02:41 -0800163 except TypeError:
164 main.log.exception( self.name + ": Object not as expected" )
165 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400166 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800167 main.log.error( self.name + ": eof exception found" )
168 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400169 main.cleanup()
170 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800171 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800172 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400173 main.cleanup()
174 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800175
kelvin-onlabd3b64892015-01-20 13:26:24 -0800176 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800177 """
Jon Hallefbd9792015-03-05 16:11:36 -0800178 karafTimeout is an optional argument. karafTimeout value passed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 by user would be used to set the current karaf shell idle timeout.
180 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800181 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800182 Below is an example to start a session with 60 seconds idle timeout
183 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800184
Hari Krishna25d42f72015-01-05 15:08:28 -0800185 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800186 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800187
kelvin-onlabd3b64892015-01-20 13:26:24 -0800188 Note: karafTimeout is left as str so that this could be read
189 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800190 """
andrewonlab95ce8322014-10-13 14:12:04 -0400191 try:
kelvin8ec71442015-01-15 16:57:00 -0800192 self.handle.sendline( "" )
193 x = self.handle.expect( [
194 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500195
196 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800197 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500198 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400199
kelvin8ec71442015-01-15 16:57:00 -0800200 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800201 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800202 i = self.handle.expect( [
203 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800204 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400205
206 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800207 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800208 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800209 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800210 "config:property-set -p org.apache.karaf.shell\
211 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800212 karafTimeout )
213 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800214 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800215 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400216 return main.TRUE
217 else:
kelvin8ec71442015-01-15 16:57:00 -0800218 # If failed, send ctrl+c to process and try again
219 main.log.info( "Starting CLI failed. Retrying..." )
220 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800221 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800222 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
223 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400224 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800225 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800226 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800227 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800228 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800229 "config:property-set -p org.apache.karaf.shell\
230 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800231 karafTimeout )
232 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800233 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800234 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400235 return main.TRUE
236 else:
kelvin8ec71442015-01-15 16:57:00 -0800237 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800238 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400239 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400240
Jon Halld4d4b372015-01-28 16:02:41 -0800241 except TypeError:
242 main.log.exception( self.name + ": Object not as expected" )
243 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400244 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800245 main.log.error( self.name + ": EOF exception found" )
246 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400247 main.cleanup()
248 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800249 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800250 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400251 main.cleanup()
252 main.exit()
253
Jon Hallefbd9792015-03-05 16:11:36 -0800254 def log( self, cmdStr, level="" ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800255 """
256 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800257 returns main.TRUE on success
Jon Hallefbd9792015-03-05 16:11:36 -0800258 returns main.FALSE if Error occurred
kelvin-onlab338f5512015-02-06 10:53:16 -0800259 Available level: DEBUG, TRACE, INFO, WARN, ERROR
260 Level defaults to INFO
kelvin-onlab9f541032015-02-04 16:19:53 -0800261 """
262 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800263 lvlStr = ""
264 if level:
265 lvlStr = "--level=" + level
266
kelvin-onlab9f541032015-02-04 16:19:53 -0800267 self.handle.sendline( "" )
268 self.handle.expect( "onos>" )
kelvin-onlab338f5512015-02-06 10:53:16 -0800269 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
kelvin-onlab9f541032015-02-04 16:19:53 -0800270 self.handle.expect( "onos>" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800271
kelvin-onlab9f541032015-02-04 16:19:53 -0800272 response = self.handle.before
273 if re.search( "Error", response ):
274 return main.FALSE
275 return main.TRUE
276
277 except pexpect.EOF:
278 main.log.error( self.name + ": EOF exception found" )
279 main.log.error( self.name + ": " + self.handle.before )
280 main.cleanup()
281 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800282 except Exception:
kelvin-onlabfb521662015-02-27 09:52:40 -0800283 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400284 main.cleanup()
285 main.exit()
286
kelvin-onlabd3b64892015-01-20 13:26:24 -0800287 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800288 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800289 Send a completely user specified string to
290 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400291 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800292
andrewonlaba18f6bf2014-10-13 19:31:54 -0400293 Warning: There are no sanity checking to commands
294 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800295 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400296 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800297 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
298 self.log( logStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800299 self.handle.sendline( cmdStr )
Jon Hall63604932015-02-26 17:09:50 -0800300 i = self.handle.expect( ["onos>", "\$", pexpect.TIMEOUT] )
301 response = self.handle.before
302 if i == 2:
303 self.handle.sendline()
304 self.handle.expect( "\$" )
305 response += self.handle.before
306 print response
307 try:
308 print self.handle.after
309 except:
310 pass
311 # TODO: do something with i
kelvin-onlabd3b64892015-01-20 13:26:24 -0800312 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
kelvin-onlab898a6c62015-01-16 14:13:53 -0800313 + self.name + "." )
Jon Hall7bdfc122015-01-23 11:45:32 -0800314 # Remove control strings from output
kelvin-onlabd3b64892015-01-20 13:26:24 -0800315 ansiEscape = re.compile( r'\x1b[^m]*m' )
Jon Hall63604932015-02-26 17:09:50 -0800316 response = ansiEscape.sub( '', response )
kelvin-onlabfb521662015-02-27 09:52:40 -0800317 # Remove extra return chars that get added
Jon Hall63604932015-02-26 17:09:50 -0800318 response = re.sub( r"\s\r", "", response )
319 response = response.strip()
320 # parse for just the output, remove the cmd from response
321 output = response.split( cmdStr, 1 )[1]
Jon Hall7bdfc122015-01-23 11:45:32 -0800322 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800323 except TypeError:
324 main.log.exception( self.name + ": Object not as expected" )
325 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400326 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800327 main.log.error( self.name + ": EOF exception found" )
328 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400329 main.cleanup()
330 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800331 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800332 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400333 main.cleanup()
334 main.exit()
335
kelvin8ec71442015-01-15 16:57:00 -0800336 # IMPORTANT NOTE:
337 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800338 # the cli command changing 'a:b' with 'aB'.
339 # Ex ) onos:topology > onosTopology
340 # onos:links > onosLinks
341 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800342
kelvin-onlabd3b64892015-01-20 13:26:24 -0800343 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800344 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400345 Adds a new cluster node by ID and address information.
346 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800347 * nodeId
348 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400349 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800350 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800351 """
Jon Hallefbd9792015-03-05 16:11:36 -0800352 # noinspection PyBroadException
andrewonlabc2d05aa2014-10-13 16:51:10 -0400353 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800354 cmdStr = "add-node " + str( nodeId ) + " " +\
355 str( ONOSIp ) + " " + str( tcpPort )
356 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800357 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800358 main.log.error( "Error in adding node" )
359 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800360 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400361 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800362 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400363 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800364 except TypeError:
365 main.log.exception( self.name + ": Object not as expected" )
366 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400367 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800368 main.log.error( self.name + ": EOF exception found" )
369 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400370 main.cleanup()
371 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800372 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800373 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400374 main.cleanup()
375 main.exit()
376
kelvin-onlabd3b64892015-01-20 13:26:24 -0800377 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800378 """
andrewonlab86dc3082014-10-13 18:18:38 -0400379 Removes a cluster by ID
380 Issues command: 'remove-node [<node-id>]'
381 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800382 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800383 """
andrewonlab86dc3082014-10-13 18:18:38 -0400384 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400385
kelvin-onlabd3b64892015-01-20 13:26:24 -0800386 cmdStr = "remove-node " + str( nodeId )
387 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800388 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400389
390 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800391
Jon Halld4d4b372015-01-28 16:02:41 -0800392 except TypeError:
393 main.log.exception( self.name + ": Object not as expected" )
394 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400395 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800396 main.log.error( self.name + ": EOF exception found" )
397 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400398 main.cleanup()
399 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800400 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800401 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400402 main.cleanup()
403 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400404
kelvin8ec71442015-01-15 16:57:00 -0800405 def nodes( self ):
406 """
andrewonlab7c211572014-10-15 16:45:20 -0400407 List the nodes currently visible
408 Issues command: 'nodes'
409 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800410 """
andrewonlab7c211572014-10-15 16:45:20 -0400411 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800412 cmdStr = "nodes"
413 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400414 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800415 except TypeError:
416 main.log.exception( self.name + ": Object not as expected" )
417 return None
andrewonlab7c211572014-10-15 16:45:20 -0400418 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800419 main.log.error( self.name + ": EOF exception found" )
420 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400421 main.cleanup()
422 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800423 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800424 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400425 main.cleanup()
426 main.exit()
427
kelvin8ec71442015-01-15 16:57:00 -0800428 def topology( self ):
429 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700430 Definition:
431 Returns the ouput of topology command.
432 Return:
433 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -0800434 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700435 import json
andrewonlab95ce8322014-10-13 14:12:04 -0400436 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800437 # either onos:topology or 'topology' will work in CLI
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700438 cmdStr = "topology -j"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800439 handle = self.sendline( cmdStr )
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700440 main.log.info( "topology -j returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400441 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800442 except TypeError:
443 main.log.exception( self.name + ": Object not as expected" )
444 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400445 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800446 main.log.error( self.name + ": EOF exception found" )
447 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400448 main.cleanup()
449 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800450 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800451 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400452 main.cleanup()
453 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800454
kelvin-onlabd3b64892015-01-20 13:26:24 -0800455 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800456 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800457 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400458 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800459 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400460 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800461 cmdStr = "feature:install " + str( featureStr )
462 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800463 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400464 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800465 except TypeError:
466 main.log.exception( self.name + ": Object not as expected" )
467 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400468 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800469 main.log.error( self.name + ": EOF exception found" )
470 main.log.error( self.name + ": " + self.handle.before )
471 main.log.report( "Failed to install feature" )
472 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400473 main.cleanup()
474 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800475 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800476 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800477 main.log.report( "Failed to install feature" )
478 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400479 main.cleanup()
480 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800481
kelvin-onlabd3b64892015-01-20 13:26:24 -0800482 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800483 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400484 Uninstalls a specified feature
485 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800486 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400487 try:
Jon Hall30b82fa2015-03-04 17:15:43 -0800488 cmdStr = 'feature:list -i | grep "' + featureStr + '"'
489 handle = self.sendline( cmdStr )
490 if handle != '':
491 cmdStr = "feature:uninstall " + str( featureStr )
492 self.sendline( cmdStr )
493 # TODO: Check for possible error responses from karaf
494 else:
Jon Hallefbd9792015-03-05 16:11:36 -0800495 main.log.info( "Feature needs to be installed before " +
496 "uninstalling it" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400497 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800498 except TypeError:
499 main.log.exception( self.name + ": Object not as expected" )
500 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400501 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800502 main.log.error( self.name + ": EOF exception found" )
503 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400504 main.cleanup()
505 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800506 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800507 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400508 main.cleanup()
509 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800510
jenkins7ead5a82015-03-13 10:28:21 -0700511 def deviceRemove( self, deviceId ):
512 """
513 Removes particular device from storage
514
515 TODO: refactor this function
516 """
517 try:
518 cmdStr = "device-remove "+str(deviceId)
519 handle = self.sendline( cmdStr )
520 return main.TRUE
521 except TypeError:
522 main.log.exception( self.name + ": Object not as expected" )
523 return None
524 except pexpect.EOF:
525 main.log.error( self.name + ": EOF exception found" )
526 main.log.error( self.name + ": " + self.handle.before )
527 main.cleanup()
528 main.exit()
529 except Exception:
530 main.log.exception( self.name + ": Uncaught exception!" )
531 main.cleanup()
532 main.exit()
533
534
535
kelvin-onlabd3b64892015-01-20 13:26:24 -0800536 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800537 """
Jon Hall7b02d952014-10-17 20:14:54 -0400538 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400539 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800540 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800541 """
andrewonlab86dc3082014-10-13 18:18:38 -0400542 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800543 if jsonFormat:
544 cmdStr = "devices -j"
545 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800546 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800547 handle variable here contains some ANSI escape color code
548 sequences at the end which are invisible in the print command
549 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800550 function. The repr( handle ) output when printed shows the
551 ANSI escape sequences. In json.loads( somestring ), this
552 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800553 json.loads would fail with the escape sequence. So we take off
554 that escape sequence using:
555
kelvin-onlabd3b64892015-01-20 13:26:24 -0800556 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
557 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800558 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800559 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
560 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400561 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400562 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800563 cmdStr = "devices"
564 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400565 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800566 except TypeError:
567 main.log.exception( self.name + ": Object not as expected" )
568 return None
andrewonlab7c211572014-10-15 16:45:20 -0400569 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800570 main.log.error( self.name + ": EOF exception found" )
571 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400572 main.cleanup()
573 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800574 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800575 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400576 main.cleanup()
577 main.exit()
578
kelvin-onlabd3b64892015-01-20 13:26:24 -0800579 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800580 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800581 This balances the devices across all controllers
582 by issuing command: 'onos> onos:balance-masters'
583 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800584 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800585 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800586 cmdStr = "onos:balance-masters"
587 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800588 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800589 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800590 except TypeError:
591 main.log.exception( self.name + ": Object not as expected" )
592 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800593 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800594 main.log.error( self.name + ": EOF exception found" )
595 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800596 main.cleanup()
597 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800598 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800599 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800600 main.cleanup()
601 main.exit()
602
kelvin-onlabd3b64892015-01-20 13:26:24 -0800603 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800604 """
Jon Halle8217482014-10-17 13:49:14 -0400605 Lists all core links
606 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800607 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800608 """
Jon Halle8217482014-10-17 13:49:14 -0400609 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800610 if jsonFormat:
611 cmdStr = "links -j"
612 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800613 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800614 handle variable here contains some ANSI escape color code
615 sequences at the end which are invisible in the print command
616 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800617 function. The repr( handle ) output when printed shows the ANSI
618 escape sequences. In json.loads( somestring ), this somestring
619 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800620 fail with the escape sequence. So we take off that escape
621 sequence using:
622
kelvin-onlabd3b64892015-01-20 13:26:24 -0800623 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
624 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800625 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800626 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
627 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400628 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400629 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800630 cmdStr = "links"
631 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400632 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800633 except TypeError:
634 main.log.exception( self.name + ": Object not as expected" )
635 return None
Jon Halle8217482014-10-17 13:49:14 -0400636 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800637 main.log.error( self.name + ": EOF exception found" )
638 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400639 main.cleanup()
640 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800641 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800642 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400643 main.cleanup()
644 main.exit()
645
kelvin-onlabd3b64892015-01-20 13:26:24 -0800646 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800647 """
Jon Halle8217482014-10-17 13:49:14 -0400648 Lists all ports
649 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800650 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800651 """
Jon Halle8217482014-10-17 13:49:14 -0400652 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800653 if jsonFormat:
654 cmdStr = "ports -j"
655 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800656 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800657 handle variable here contains some ANSI escape color code
658 sequences at the end which are invisible in the print command
659 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800660 function. The repr( handle ) output when printed shows the ANSI
661 escape sequences. In json.loads( somestring ), this somestring
662 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800663 fail with the escape sequence. So we take off that escape
Jon Hallefbd9792015-03-05 16:11:36 -0800664 sequence using the following commands:
Jon Halle3f39ff2015-01-13 11:50:53 -0800665
kelvin-onlabd3b64892015-01-20 13:26:24 -0800666 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
667 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800668 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800669 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
670 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400671 return handle1
672
Jon Halle8217482014-10-17 13:49:14 -0400673 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800674 cmdStr = "ports"
675 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800676 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800677 except TypeError:
678 main.log.exception( self.name + ": Object not as expected" )
679 return None
Jon Halle8217482014-10-17 13:49:14 -0400680 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800681 main.log.error( self.name + ": EOF exception found" )
682 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400683 main.cleanup()
684 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800685 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800686 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400687 main.cleanup()
688 main.exit()
689
kelvin-onlabd3b64892015-01-20 13:26:24 -0800690 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800691 """
Jon Hall983a1702014-10-28 18:44:22 -0400692 Lists all devices and the controllers with roles assigned to them
693 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800694 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800695 """
andrewonlab7c211572014-10-15 16:45:20 -0400696 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800697 if jsonFormat:
698 cmdStr = "roles -j"
699 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800700 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800701 handle variable here contains some ANSI escape color code
702 sequences at the end which are invisible in the print command
703 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800704 function. The repr( handle ) output when printed shows the ANSI
705 escape sequences. In json.loads( somestring ), this somestring
706 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800707 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500708
Jon Halle3f39ff2015-01-13 11:50:53 -0800709 So we take off that escape sequence using the following
710 commads:
711
kelvin-onlabd3b64892015-01-20 13:26:24 -0800712 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
713 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800714 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800715 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
716 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400717 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400718
andrewonlab7c211572014-10-15 16:45:20 -0400719 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800720 cmdStr = "roles"
721 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800722 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800723 except TypeError:
724 main.log.exception( self.name + ": Object not as expected" )
725 return None
Jon Hall983a1702014-10-28 18:44:22 -0400726 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800727 main.log.error( self.name + ": EOF exception found" )
728 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400729 main.cleanup()
730 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800731 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800732 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400733 main.cleanup()
734 main.exit()
735
kelvin-onlabd3b64892015-01-20 13:26:24 -0800736 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800737 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800738 Given the a string containing the json representation of the "roles"
739 cli command and a partial or whole device id, returns a json object
740 containing the roles output for the first device whose id contains
741 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400742
743 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800744 A dict of the role assignments for the given device or
745 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800746 """
Jon Hall983a1702014-10-28 18:44:22 -0400747 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800748 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400749 return None
750 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800751 rawRoles = self.roles()
752 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800753 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800754 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800755 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800756 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400757 return device
758 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800759 except TypeError:
760 main.log.exception( self.name + ": Object not as expected" )
761 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400762 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800763 main.log.error( self.name + ": EOF exception found" )
764 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400765 main.cleanup()
766 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800767 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800768 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400769 main.cleanup()
770 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800771
kelvin-onlabd3b64892015-01-20 13:26:24 -0800772 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800773 """
Jon Hall94fd0472014-12-08 11:52:42 -0800774 Iterates through each device and checks if there is a master assigned
775 Returns: main.TRUE if each device has a master
776 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800777 """
Jon Hall94fd0472014-12-08 11:52:42 -0800778 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800779 rawRoles = self.roles()
780 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800781 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800782 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800783 # print device
784 if device[ 'master' ] == "none":
785 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800786 return main.FALSE
787 return main.TRUE
788
Jon Halld4d4b372015-01-28 16:02:41 -0800789 except TypeError:
790 main.log.exception( self.name + ": Object not as expected" )
791 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800792 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800793 main.log.error( self.name + ": EOF exception found" )
794 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800795 main.cleanup()
796 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800797 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800798 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800799 main.cleanup()
800 main.exit()
801
kelvin-onlabd3b64892015-01-20 13:26:24 -0800802 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800803 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400804 Returns string of paths, and the cost.
805 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800806 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400807 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800808 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
809 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800810 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800811 main.log.error( "Error in getting paths" )
812 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400813 else:
kelvin8ec71442015-01-15 16:57:00 -0800814 path = handle.split( ";" )[ 0 ]
815 cost = handle.split( ";" )[ 1 ]
816 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800817 except TypeError:
818 main.log.exception( self.name + ": Object not as expected" )
819 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400820 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800821 main.log.error( self.name + ": EOF exception found" )
822 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400823 main.cleanup()
824 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800825 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800826 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400827 main.cleanup()
828 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800829
kelvin-onlabd3b64892015-01-20 13:26:24 -0800830 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800831 """
Jon Hallffb386d2014-11-21 13:43:38 -0800832 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400833 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800834 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800835 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400836 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800837 if jsonFormat:
838 cmdStr = "hosts -j"
839 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800840 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800841 handle variable here contains some ANSI escape color code
842 sequences at the end which are invisible in the print command
843 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800844 function. The repr( handle ) output when printed shows the ANSI
845 escape sequences. In json.loads( somestring ), this somestring
846 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800847 fail with the escape sequence. So we take off that escape
848 sequence using:
849
kelvin-onlabd3b64892015-01-20 13:26:24 -0800850 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
851 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800852 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800853 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
854 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400855 return handle1
856 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800857 cmdStr = "hosts"
858 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400859 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800860 except TypeError:
861 main.log.exception( self.name + ": Object not as expected" )
862 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400863 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800864 main.log.error( self.name + ": EOF exception found" )
865 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400866 main.cleanup()
867 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800868 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800869 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400870 main.cleanup()
871 main.exit()
872
kelvin-onlabd3b64892015-01-20 13:26:24 -0800873 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800874 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400875 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800876
Jon Hallefbd9792015-03-05 16:11:36 -0800877 Note: mac must be a colon separated mac address, but could be a
Jon Halle3f39ff2015-01-13 11:50:53 -0800878 partial mac address
879
Jon Hall42db6dc2014-10-24 19:03:48 -0400880 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800881 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400882 try:
kelvin8ec71442015-01-15 16:57:00 -0800883 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400884 return None
885 else:
886 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800887 rawHosts = self.hosts()
888 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800889 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800890 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800891 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800892 if not host:
893 pass
894 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400895 return host
896 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800897 except TypeError:
898 main.log.exception( self.name + ": Object not as expected" )
899 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400900 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800901 main.log.error( self.name + ": EOF exception found" )
902 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400903 main.cleanup()
904 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800905 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800906 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400907 main.cleanup()
908 main.exit()
909
kelvin-onlabd3b64892015-01-20 13:26:24 -0800910 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800911 """
912 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400913 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800914
andrewonlab3f0a4af2014-10-17 12:25:14 -0400915 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800916 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400917 IMPORTANT:
918 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800919 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400920 Furthermore, it assumes that value of VLAN is '-1'
921 Description:
kelvin8ec71442015-01-15 16:57:00 -0800922 Converts mininet hosts ( h1, h2, h3... ) into
923 ONOS format ( 00:00:00:00:00:01/-1 , ... )
924 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400925 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800926 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400927
kelvin-onlabd3b64892015-01-20 13:26:24 -0800928 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800929 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800930 hostHex = hex( int( host ) ).zfill( 12 )
931 hostHex = str( hostHex ).replace( 'x', '0' )
932 i = iter( str( hostHex ) )
933 hostHex = ":".join( a + b for a, b in zip( i, i ) )
934 hostHex = hostHex + "/-1"
935 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400936
kelvin-onlabd3b64892015-01-20 13:26:24 -0800937 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400938
Jon Halld4d4b372015-01-28 16:02:41 -0800939 except TypeError:
940 main.log.exception( self.name + ": Object not as expected" )
941 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400942 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800943 main.log.error( self.name + ": EOF exception found" )
944 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400945 main.cleanup()
946 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800947 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800948 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400949 main.cleanup()
950 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400951
kelvin-onlabd3b64892015-01-20 13:26:24 -0800952 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800953 """
andrewonlabe6745342014-10-17 14:29:13 -0400954 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800955 * hostIdOne: ONOS host id for host1
956 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400957 Description:
Jon Hallefbd9792015-03-05 16:11:36 -0800958 Adds a host-to-host intent ( bidirectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500959 specifying the two hosts.
kelvin-onlabfb521662015-02-27 09:52:40 -0800960 Returns:
961 A string of the intent id or None on Error
kelvin8ec71442015-01-15 16:57:00 -0800962 """
andrewonlabe6745342014-10-17 14:29:13 -0400963 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800964 cmdStr = "add-host-intent " + str( hostIdOne ) +\
965 " " + str( hostIdTwo )
966 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800967 if re.search( "Error", handle ):
968 main.log.error( "Error in adding Host intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800969 return None
Hari Krishnaac4e1782015-01-26 12:09:12 -0800970 else:
971 main.log.info( "Host intent installed between " +
kelvin-onlabfb521662015-02-27 09:52:40 -0800972 str( hostIdOne ) + " and " + str( hostIdTwo ) )
973 match = re.search('id=0x([\da-f]+),', handle)
974 if match:
975 return match.group()[3:-1]
976 else:
977 main.log.error( "Error, intent ID not found" )
978 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800979 except TypeError:
980 main.log.exception( self.name + ": Object not as expected" )
981 return None
andrewonlabe6745342014-10-17 14:29:13 -0400982 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800983 main.log.error( self.name + ": EOF exception found" )
984 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400985 main.cleanup()
986 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800987 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800988 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400989 main.cleanup()
990 main.exit()
991
kelvin-onlabd3b64892015-01-20 13:26:24 -0800992 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800993 """
andrewonlab7b31d232014-10-24 13:31:47 -0400994 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800995 * ingressDevice: device id of ingress device
996 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400997 Optional:
998 TODO: Still needs to be implemented via dev side
kelvin-onlabfb521662015-02-27 09:52:40 -0800999 Description:
1000 Adds an optical intent by specifying an ingress and egress device
1001 Returns:
1002 A string of the intent id or None on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001003 """
andrewonlab7b31d232014-10-24 13:31:47 -04001004 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001005 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
1006 " " + str( egressDevice )
1007 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001008 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -08001009 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001010 main.log.error( "Error in adding Optical intent" )
1011 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001012 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001013 main.log.info( "Optical intent installed between " +
1014 str( ingressDevice ) + " and " +
1015 str( egressDevice ) )
1016 match = re.search('id=0x([\da-f]+),', handle)
1017 if match:
1018 return match.group()[3:-1]
1019 else:
1020 main.log.error( "Error, intent ID not found" )
1021 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001022 except TypeError:
1023 main.log.exception( self.name + ": Object not as expected" )
1024 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001025 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001026 main.log.error( self.name + ": EOF exception found" )
1027 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -04001028 main.cleanup()
1029 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001030 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001031 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -04001032 main.cleanup()
1033 main.exit()
1034
kelvin-onlabd3b64892015-01-20 13:26:24 -08001035 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001036 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001037 ingressDevice,
1038 egressDevice,
1039 portIngress="",
1040 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001041 ethType="",
1042 ethSrc="",
1043 ethDst="",
1044 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001045 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001046 ipProto="",
1047 ipSrc="",
1048 ipDst="",
1049 tcpSrc="",
1050 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001051 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001052 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001053 * ingressDevice: device id of ingress device
1054 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001055 Optional:
1056 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001057 * ethSrc: specify ethSrc ( i.e. src mac addr )
1058 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001059 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001060 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001061 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001062 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001063 * ipSrc: specify ip source address
1064 * ipDst: specify ip destination address
1065 * tcpSrc: specify tcp source port
1066 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001067 Description:
kelvin8ec71442015-01-15 16:57:00 -08001068 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001069 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001070 Returns:
1071 A string of the intent id or None on error
andrewonlab289e4b72014-10-21 21:24:18 -04001072
Jon Halle3f39ff2015-01-13 11:50:53 -08001073 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001074 options developers provide for point-to-point
1075 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001076 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001077 try:
kelvin8ec71442015-01-15 16:57:00 -08001078 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001079 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001080 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001081 and not ipProto and not ipSrc and not ipDst \
1082 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001083 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001084
andrewonlab289e4b72014-10-21 21:24:18 -04001085 else:
andrewonlab36af3822014-11-18 17:48:18 -05001086 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001087
andrewonlab0c0a6772014-10-22 12:31:18 -04001088 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001089 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001090 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001091 cmd += " --ethSrc " + str( ethSrc )
1092 if ethDst:
1093 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001094 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001095 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001096 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001097 cmd += " --lambda "
1098 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001099 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001100 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001101 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001102 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001103 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001104 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001105 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001106 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001107 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001108
kelvin8ec71442015-01-15 16:57:00 -08001109 # Check whether the user appended the port
1110 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001111 if "/" in ingressDevice:
1112 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001113 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001114 if not portIngress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001115 main.log.error( "You must specify the ingress port" )
kelvin8ec71442015-01-15 16:57:00 -08001116 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001117 # Would it make sense to throw an exception and exit
1118 # the test?
1119 return None
andrewonlab36af3822014-11-18 17:48:18 -05001120
kelvin8ec71442015-01-15 16:57:00 -08001121 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001122 str( ingressDevice ) + "/" +\
1123 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001124
kelvin-onlabd3b64892015-01-20 13:26:24 -08001125 if "/" in egressDevice:
1126 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001127 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001128 if not portEgress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001129 main.log.error( "You must specify the egress port" )
1130 return None
Jon Halle3f39ff2015-01-13 11:50:53 -08001131
kelvin8ec71442015-01-15 16:57:00 -08001132 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001133 str( egressDevice ) + "/" +\
1134 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001135
kelvin-onlab898a6c62015-01-16 14:13:53 -08001136 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001137 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001138 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001139 main.log.error( "Error in adding point-to-point intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001140 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001141 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001142 # TODO: print out all the options in this message?
1143 main.log.info( "Point-to-point intent installed between " +
1144 str( ingressDevice ) + " and " +
1145 str( egressDevice ) )
1146 match = re.search('id=0x([\da-f]+),', handle)
1147 if match:
1148 return match.group()[3:-1]
1149 else:
1150 main.log.error( "Error, intent ID not found" )
1151 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001152 except TypeError:
1153 main.log.exception( self.name + ": Object not as expected" )
1154 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001155 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001156 main.log.error( self.name + ": EOF exception found" )
1157 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001158 main.cleanup()
1159 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001160 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001161 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001162 main.cleanup()
1163 main.exit()
1164
kelvin-onlabd3b64892015-01-20 13:26:24 -08001165 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001166 self,
shahshreyac2f97072015-03-19 17:04:29 -07001167 ingressDeviceList,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001168 egressDevice,
shahshreyac2f97072015-03-19 17:04:29 -07001169 portIngressList=None,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001170 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001171 ethType="",
1172 ethSrc="",
1173 ethDst="",
1174 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001175 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001176 ipProto="",
1177 ipSrc="",
1178 ipDst="",
1179 tcpSrc="",
1180 tcpDst="",
1181 setEthSrc="",
1182 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001183 """
shahshreyad0c80432014-12-04 16:56:05 -08001184 Note:
shahshreya70622b12015-03-19 17:19:00 -07001185 This function assumes the format of all ingress devices
1186 is same. That is, all ingress devices include port nos
1187 with a "/" or all ingress devices could specify device
1188 ids and port nos seperately.
shahshreyad0c80432014-12-04 16:56:05 -08001189 Required:
shahshreyac2f97072015-03-19 17:04:29 -07001190 * ingressDeviceList: List of device ids of ingress device
1191 ( Atleast 2 ingress devices required in the list )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001192 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001193 Optional:
1194 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001195 * ethSrc: specify ethSrc ( i.e. src mac addr )
1196 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001197 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001198 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001199 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001200 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001201 * ipSrc: specify ip source address
1202 * ipDst: specify ip destination address
1203 * tcpSrc: specify tcp source port
1204 * tcpDst: specify tcp destination port
1205 * setEthSrc: action to Rewrite Source MAC Address
1206 * setEthDst: action to Rewrite Destination MAC Address
1207 Description:
kelvin8ec71442015-01-15 16:57:00 -08001208 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001209 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001210 Returns:
1211 A string of the intent id or None on error
shahshreyad0c80432014-12-04 16:56:05 -08001212
Jon Halle3f39ff2015-01-13 11:50:53 -08001213 NOTE: This function may change depending on the
Jon Hallefbd9792015-03-05 16:11:36 -08001214 options developers provide for multipoint-to-singlepoint
shahshreyad0c80432014-12-04 16:56:05 -08001215 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001216 """
shahshreyad0c80432014-12-04 16:56:05 -08001217 try:
kelvin8ec71442015-01-15 16:57:00 -08001218 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001219 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001220 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001221 and not ipProto and not ipSrc and not ipDst\
1222 and not tcpSrc and not tcpDst and not setEthSrc\
1223 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001224 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001225
1226 else:
1227 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001228
shahshreyad0c80432014-12-04 16:56:05 -08001229 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001230 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001231 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001232 cmd += " --ethSrc " + str( ethSrc )
1233 if ethDst:
1234 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001235 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001236 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001237 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001238 cmd += " --lambda "
1239 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001240 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001241 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001242 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001243 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001244 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001245 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001246 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001247 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001248 cmd += " --tcpDst " + str( tcpDst )
shahshreya70622b12015-03-19 17:19:00 -07001249 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001250 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001251 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001252 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001253 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001254
kelvin8ec71442015-01-15 16:57:00 -08001255 # Check whether the user appended the port
1256 # or provided it as an input
shahshreyac2f97072015-03-19 17:04:29 -07001257
1258 if portIngressList is None:
1259 for ingressDevice in ingressDeviceList:
1260 if "/" in ingressDevice:
1261 cmd += " " + str( ingressDevice )
1262 else:
1263 main.log.error( "You must specify " +
1264 "the ingress port" )
1265 # TODO: perhaps more meaningful return
1266 return main.FALSE
shahshreyad0c80432014-12-04 16:56:05 -08001267 else:
shahshreya70622b12015-03-19 17:19:00 -07001268 if len( ingressDeviceList ) == len( portIngressList )
1269 for ingressDevice,portIngress in zip( ingressDeviceList,portIngressList ):
1270 cmd += " " + \
1271 str( ingressDevice ) + "/" +\
1272 str( portIngress ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001273
kelvin-onlabd3b64892015-01-20 13:26:24 -08001274 if "/" in egressDevice:
1275 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001276 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001277 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001278 main.log.error( "You must specify " +
1279 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001280 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001281
kelvin8ec71442015-01-15 16:57:00 -08001282 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001283 str( egressDevice ) + "/" +\
1284 str( portEgress )
shahshreyac2f97072015-03-19 17:04:29 -07001285
kelvin8ec71442015-01-15 16:57:00 -08001286 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001287 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001288 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001289 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001290 main.log.error( "Error in adding multipoint-to-singlepoint " +
1291 "intent" )
1292 return None
shahshreyad0c80432014-12-04 16:56:05 -08001293 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001294 # TODO: print out all the options in this message?
1295 main.log.info( "Multipoint-to-singlepoint intent installed" +
shahshreyac2f97072015-03-19 17:04:29 -07001296 " failed " )
1297 return None
1298 #match = re.search('id=0x([\da-f]+),', handle)
1299 #if match:
1300 #return match.group()[3:-1]
1301 #else:
1302 #main.log.error( "Error, intent ID not found" )
1303 #return None
Jon Halld4d4b372015-01-28 16:02:41 -08001304 except TypeError:
1305 main.log.exception( self.name + ": Object not as expected" )
1306 return None
shahshreyad0c80432014-12-04 16:56:05 -08001307 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001308 main.log.error( self.name + ": EOF exception found" )
1309 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001310 main.cleanup()
1311 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001312 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001313 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001314 main.cleanup()
1315 main.exit()
1316
Jon Hallefbd9792015-03-05 16:11:36 -08001317 def removeIntent( self, intentId, app='org.onosproject.cli',
1318 purge=False, sync=False ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001319 """
shahshreya1c818fc2015-02-26 13:44:08 -08001320 Remove intent for specified application id and intent id
1321 Optional args:-
1322 -s or --sync: Waits for the removal before returning
1323 -p or --purge: Purge the intent from the store after removal
1324
Jon Halle3f39ff2015-01-13 11:50:53 -08001325 Returns:
1326 main.False on error and
1327 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001328 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001329 try:
shahshreya1c818fc2015-02-26 13:44:08 -08001330 cmdStr = "remove-intent "
1331 if purge:
1332 cmdStr += " -p"
1333 if sync:
1334 cmdStr += " -s"
1335
1336 cmdStr += " " + app + " " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001337 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001338 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001339 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001340 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001341 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001342 # TODO: Should this be main.TRUE
1343 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001344 except TypeError:
1345 main.log.exception( self.name + ": Object not as expected" )
1346 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001347 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001348 main.log.error( self.name + ": EOF exception found" )
1349 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001350 main.cleanup()
1351 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001352 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001353 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001354 main.cleanup()
1355 main.exit()
1356
kelvin-onlabd3b64892015-01-20 13:26:24 -08001357 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001358 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001359 NOTE: This method should be used after installing application:
1360 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001361 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001362 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001363 Description:
1364 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001365 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001366 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001367 if jsonFormat:
1368 cmdStr = "routes -j"
1369 handleTmp = self.sendline( cmdStr )
1370 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1371 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001372 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001373 cmdStr = "routes"
1374 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001375 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001376 except TypeError:
1377 main.log.exception( self.name + ": Object not as expected" )
1378 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001379 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001380 main.log.error( self.name + ": EOF exception found" )
1381 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001382 main.cleanup()
1383 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001384 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001385 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001386 main.cleanup()
1387 main.exit()
1388
kelvin-onlabd3b64892015-01-20 13:26:24 -08001389 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001390 """
andrewonlab377693f2014-10-21 16:00:30 -04001391 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001392 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001393 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001394 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001395 """
andrewonlabe6745342014-10-17 14:29:13 -04001396 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001397 if jsonFormat:
1398 cmdStr = "intents -j"
1399 handle = self.sendline( cmdStr )
1400 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1401 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001402 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001403 cmdStr = "intents"
1404 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001405 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001406 except TypeError:
1407 main.log.exception( self.name + ": Object not as expected" )
1408 return None
andrewonlabe6745342014-10-17 14:29:13 -04001409 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001410 main.log.error( self.name + ": EOF exception found" )
1411 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001412 main.cleanup()
1413 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001414 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001415 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001416 main.cleanup()
1417 main.exit()
1418
kelvin-onlab54400a92015-02-26 18:05:51 -08001419 def getIntentState(self, intentsId, intentsJson=None):
1420 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001421 Check intent state.
1422 Accepts a single intent ID (string type) or a list of intent IDs.
1423 Returns the state(string type) of the id if a single intent ID is
1424 accepted.
Jon Hallefbd9792015-03-05 16:11:36 -08001425 Returns a dictionary with intent IDs as the key and its
1426 corresponding states as the values
kelvin-onlabfb521662015-02-27 09:52:40 -08001427 Parameters:
kelvin-onlab54400a92015-02-26 18:05:51 -08001428 intentId: intent ID (string type)
1429 intentsJson: parsed json object from the onos:intents api
1430 Returns:
1431 state = An intent's state- INSTALL,WITHDRAWN etc.
1432 stateDict = Dictionary of intent's state. intent ID as the keys and
1433 state as the values.
1434 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001435 try:
1436 state = "State is Undefined"
1437 if not intentsJson:
Jon Hallefbd9792015-03-05 16:11:36 -08001438 intentsJsonTemp = json.loads( self.intents() )
kelvin-onlab54400a92015-02-26 18:05:51 -08001439 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001440 intentsJsonTemp = json.loads( intentsJson )
1441 if isinstance( intentsId, types.StringType ):
kelvin-onlab54400a92015-02-26 18:05:51 -08001442 for intent in intentsJsonTemp:
1443 if intentsId == intent['id']:
1444 state = intent['state']
1445 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001446 main.log.info( "Cannot find intent ID" + str( intentsId ) +
1447 " on the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001448 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001449 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001450 dictList = []
kelvin-onlab54400a92015-02-26 18:05:51 -08001451 for ID in intentsId:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001452 stateDict = {}
kelvin-onlab54400a92015-02-26 18:05:51 -08001453 for intents in intentsJsonTemp:
1454 if ID == intents['id']:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001455 stateDict['state'] = intents['state']
1456 stateDict['id'] = ID
Jon Hallefbd9792015-03-05 16:11:36 -08001457 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08001458 break
Jon Hallefbd9792015-03-05 16:11:36 -08001459 if len( intentsId ) != len( dictList ):
1460 main.log.info( "Cannot find some of the intent ID state" )
kelvin-onlab07dbd012015-03-04 16:29:39 -08001461 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08001462 else:
1463 main.log.info("Invalid intents ID entry")
1464 return None
kelvin-onlab54400a92015-02-26 18:05:51 -08001465 except TypeError:
1466 main.log.exception( self.name + ": Object not as expected" )
1467 return None
1468 except pexpect.EOF:
1469 main.log.error( self.name + ": EOF exception found" )
1470 main.log.error( self.name + ": " + self.handle.before )
1471 main.cleanup()
1472 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001473 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08001474 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001475 main.cleanup()
1476 main.exit()
1477
kelvin-onlabd3b64892015-01-20 13:26:24 -08001478 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001479 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001480 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001481 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001482 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001483 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001484 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001485 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001486 if jsonFormat:
1487 cmdStr = "flows -j"
1488 handle = self.sendline( cmdStr )
1489 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1490 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001491 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001492 cmdStr = "flows"
1493 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001494 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001495 main.log.error( self.name + ".flows() response: " +
1496 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001497 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001498 except TypeError:
1499 main.log.exception( self.name + ": Object not as expected" )
1500 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001501 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001502 main.log.error( self.name + ": EOF exception found" )
1503 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001504 main.cleanup()
1505 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001506 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001507 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001508 main.cleanup()
1509 main.exit()
1510
kelvin-onlabd3b64892015-01-20 13:26:24 -08001511 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
Jon Hallefbd9792015-03-05 16:11:36 -08001512 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001513 """
andrewonlab87852b02014-11-19 18:44:19 -05001514 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001515 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001516 a specific point-to-point intent definition
1517 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001518 * dpidSrc: specify source dpid
1519 * dpidDst: specify destination dpid
1520 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001521 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001522 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001523 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001524 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001525 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001526 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001527 """
andrewonlab87852b02014-11-19 18:44:19 -05001528 try:
kelvin8ec71442015-01-15 16:57:00 -08001529 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001530 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1531 str( numIntents )
1532 if numMult:
1533 cmd += " " + str( numMult )
1534 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001535 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001536 if appId:
1537 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001538 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001539 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1540 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001541 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001542 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001543 main.log.info( handle )
1544 # Split result by newline
1545 newline = handle.split( "\r\r\n" )
1546 # Ignore the first object of list, which is empty
1547 newline = newline[ 1: ]
1548 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001549 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001550 result = result.split( ": " )
1551 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001552 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1553 main.log.info( latResult )
1554 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001555 else:
1556 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001557 except TypeError:
1558 main.log.exception( self.name + ": Object not as expected" )
1559 return None
andrewonlab87852b02014-11-19 18:44:19 -05001560 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001561 main.log.error( self.name + ": EOF exception found" )
1562 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001563 main.cleanup()
1564 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001565 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001566 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001567 main.cleanup()
1568 main.exit()
1569
kelvin-onlabd3b64892015-01-20 13:26:24 -08001570 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001571 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001572 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001573 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001574 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001575 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001576 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001577 if jsonFormat:
1578 cmdStr = "intents-events-metrics -j"
1579 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001580 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001581 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1582 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001583 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001584 cmdStr = "intents-events-metrics"
1585 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001586 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001587 except TypeError:
1588 main.log.exception( self.name + ": Object not as expected" )
1589 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001590 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001591 main.log.error( self.name + ": EOF exception found" )
1592 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001593 main.cleanup()
1594 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001595 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001596 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001597 main.cleanup()
1598 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001599
kelvin-onlabd3b64892015-01-20 13:26:24 -08001600 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001601 """
1602 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001603 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001604 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001605 """
andrewonlab867212a2014-10-22 20:13:38 -04001606 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001607 if jsonFormat:
1608 cmdStr = "topology-events-metrics -j"
1609 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001610 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001611 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1612 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001613 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001614 cmdStr = "topology-events-metrics"
1615 handle = self.sendline( cmdStr )
jenkins7ead5a82015-03-13 10:28:21 -07001616 if handle:
1617 return handle
1618 else:
1619 # Return empty json
1620 return '{}'
Jon Halld4d4b372015-01-28 16:02:41 -08001621 except TypeError:
1622 main.log.exception( self.name + ": Object not as expected" )
1623 return None
andrewonlab867212a2014-10-22 20:13:38 -04001624 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001625 main.log.error( self.name + ": EOF exception found" )
1626 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001627 main.cleanup()
1628 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001629 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001630 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001631 main.cleanup()
1632 main.exit()
1633
kelvin8ec71442015-01-15 16:57:00 -08001634 # Wrapper functions ****************
1635 # Wrapper functions use existing driver
1636 # functions and extends their use case.
1637 # For example, we may use the output of
1638 # a normal driver function, and parse it
1639 # using a wrapper function
andrewonlab7e4d2d32014-10-15 13:23:21 -04001640
kelvin-onlabd3b64892015-01-20 13:26:24 -08001641 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001642 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001643 Description:
1644 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001645 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001646 try:
kelvin8ec71442015-01-15 16:57:00 -08001647 # Obtain output of intents function
kelvin-onlabfb521662015-02-27 09:52:40 -08001648 intentsStr = self.intents(jsonFormat=False)
kelvin-onlabd3b64892015-01-20 13:26:24 -08001649 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001650
kelvin8ec71442015-01-15 16:57:00 -08001651 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001652 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1653 for intents in intentsList:
kelvin-onlabfb521662015-02-27 09:52:40 -08001654 match = re.search('id=0x([\da-f]+),', intents)
1655 if match:
1656 tmpId = match.group()[3:-1]
1657 intentIdList.append( tmpId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001658 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001659
Jon Halld4d4b372015-01-28 16:02:41 -08001660 except TypeError:
1661 main.log.exception( self.name + ": Object not as expected" )
1662 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001663 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001664 main.log.error( self.name + ": EOF exception found" )
1665 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001666 main.cleanup()
1667 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001668 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001669 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001670 main.cleanup()
1671 main.exit()
1672
Jon Hall30b82fa2015-03-04 17:15:43 -08001673 def FlowAddedCount( self, deviceId ):
1674 """
1675 Determine the number of flow rules for the given device id that are
1676 in the added state
1677 """
1678 try:
1679 cmdStr = "flows any " + str( deviceId ) + " | " +\
1680 "grep 'state=ADDED' | wc -l"
1681 handle = self.sendline( cmdStr )
1682 return handle
1683 except pexpect.EOF:
1684 main.log.error( self.name + ": EOF exception found" )
1685 main.log.error( self.name + ": " + self.handle.before )
1686 main.cleanup()
1687 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001688 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -08001689 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001690 main.cleanup()
1691 main.exit()
1692
kelvin-onlabd3b64892015-01-20 13:26:24 -08001693 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001694 """
andrewonlab95ce8322014-10-13 14:12:04 -04001695 Use 'devices' function to obtain list of all devices
1696 and parse the result to obtain a list of all device
1697 id's. Returns this list. Returns empty list if no
1698 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001699 List is ordered sequentially
1700
andrewonlab95ce8322014-10-13 14:12:04 -04001701 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001702 device id, and wish to execute other commands using
andrewonlab95ce8322014-10-13 14:12:04 -04001703 the ids. By obtaining the list of device ids on the fly,
1704 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001705 """
andrewonlab95ce8322014-10-13 14:12:04 -04001706 try:
kelvin8ec71442015-01-15 16:57:00 -08001707 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001708 devicesStr = self.devices( jsonFormat=False )
1709 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001710
kelvin-onlabd3b64892015-01-20 13:26:24 -08001711 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001712 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001713 return idList
kelvin8ec71442015-01-15 16:57:00 -08001714
1715 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001716 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001717 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001718 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001719 # Split list further into arguments before and after string
1720 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001721 # append to idList
1722 for arg in tempList:
1723 idList.append( arg.split( "id=" )[ 1 ] )
1724 return idList
andrewonlab95ce8322014-10-13 14:12:04 -04001725
Jon Halld4d4b372015-01-28 16:02:41 -08001726 except TypeError:
1727 main.log.exception( self.name + ": Object not as expected" )
1728 return None
andrewonlab95ce8322014-10-13 14:12:04 -04001729 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001730 main.log.error( self.name + ": EOF exception found" )
1731 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -04001732 main.cleanup()
1733 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001734 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001735 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -04001736 main.cleanup()
1737 main.exit()
1738
kelvin-onlabd3b64892015-01-20 13:26:24 -08001739 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001740 """
andrewonlab7c211572014-10-15 16:45:20 -04001741 Uses 'nodes' function to obtain list of all nodes
1742 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001743 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001744 Returns:
1745 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001746 """
andrewonlab7c211572014-10-15 16:45:20 -04001747 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001748 nodesStr = self.nodes()
1749 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001750
kelvin-onlabd3b64892015-01-20 13:26:24 -08001751 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001752 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001753 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001754
kelvin-onlabd3b64892015-01-20 13:26:24 -08001755 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001756 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001757
kelvin8ec71442015-01-15 16:57:00 -08001758 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001759 nodesList = nodesStr.split( "," )
1760 tempList = [ node for node in nodesList if "id=" in node ]
1761 for arg in tempList:
1762 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001763
kelvin-onlabd3b64892015-01-20 13:26:24 -08001764 return idList
kelvin8ec71442015-01-15 16:57:00 -08001765
Jon Halld4d4b372015-01-28 16:02:41 -08001766 except TypeError:
1767 main.log.exception( self.name + ": Object not as expected" )
1768 return None
andrewonlab7c211572014-10-15 16:45:20 -04001769 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001770 main.log.error( self.name + ": EOF exception found" )
1771 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001772 main.cleanup()
1773 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001774 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001775 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001776 main.cleanup()
1777 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -04001778
kelvin-onlabd3b64892015-01-20 13:26:24 -08001779 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001780 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001781 Return the first device from the devices api whose 'id' contains 'dpid'
1782 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001783 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001784 try:
kelvin8ec71442015-01-15 16:57:00 -08001785 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001786 return None
1787 else:
kelvin8ec71442015-01-15 16:57:00 -08001788 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001789 rawDevices = self.devices()
1790 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001791 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001792 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001793 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1794 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001795 return device
1796 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001797 except TypeError:
1798 main.log.exception( self.name + ": Object not as expected" )
1799 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001800 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001801 main.log.error( self.name + ": EOF exception found" )
1802 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001803 main.cleanup()
1804 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001805 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001806 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001807 main.cleanup()
1808 main.exit()
1809
kelvin-onlabd3b64892015-01-20 13:26:24 -08001810 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001811 """
Jon Hallefbd9792015-03-05 16:11:36 -08001812 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001813 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001814 log level can be specified.
kelvin8ec71442015-01-15 16:57:00 -08001815
Jon Hall42db6dc2014-10-24 19:03:48 -04001816 Params: ip = ip used for the onos cli
1817 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001818 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001819 logLevel = level to log to. Currently accepts
1820 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001821
1822
kelvin-onlabd3b64892015-01-20 13:26:24 -08001823 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001824
Jon Hallefbd9792015-03-05 16:11:36 -08001825 Returns: main.TRUE if the number of switches and links are correct,
1826 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04001827 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001828 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001829 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001830 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001831 if topology == {}:
1832 return main.ERROR
1833 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001834 # Is the number of switches is what we expected
1835 devices = topology.get( 'devices', False )
1836 links = topology.get( 'links', False )
kelvin-onlabfb521662015-02-27 09:52:40 -08001837 if devices is False or links is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04001838 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001839 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001840 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001841 linkCheck = ( int( links ) == int( numolink ) )
1842 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001843 # We expected the correct numbers
Jon Hallefbd9792015-03-05 16:11:36 -08001844 output += "The number of links and switches match " +\
1845 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001846 result = main.TRUE
1847 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001848 output += "The number of links and switches does not match " +\
1849 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001850 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001851 output = output + "\n ONOS sees %i devices (%i expected) \
1852 and %i links (%i expected)" % (
1853 int( devices ), int( numoswitch ), int( links ),
1854 int( numolink ) )
1855 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001856 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001857 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001858 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001859 else:
kelvin8ec71442015-01-15 16:57:00 -08001860 main.log.info( output )
1861 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001862 except TypeError:
1863 main.log.exception( self.name + ": Object not as expected" )
1864 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001865 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001866 main.log.error( self.name + ": EOF exception found" )
1867 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001868 main.cleanup()
1869 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001870 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001871 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001872 main.cleanup()
1873 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001874
kelvin-onlabd3b64892015-01-20 13:26:24 -08001875 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001876 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001877 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001878 deviceId must be the id of a device as seen in the onos devices command
1879 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001880 role must be either master, standby, or none
1881
Jon Halle3f39ff2015-01-13 11:50:53 -08001882 Returns:
1883 main.TRUE or main.FALSE based on argument verification and
1884 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001885 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001886 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001887 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001888 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001889 cmdStr = "device-role " +\
1890 str( deviceId ) + " " +\
1891 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001892 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001893 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001894 if re.search( "Error", handle ):
1895 # end color output to escape any colours
1896 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001897 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001898 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001899 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001900 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001901 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001902 main.log.error( "Invalid 'role' given to device_role(). " +
1903 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001904 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001905 except TypeError:
1906 main.log.exception( self.name + ": Object not as expected" )
1907 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001908 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001909 main.log.error( self.name + ": EOF exception found" )
1910 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001911 main.cleanup()
1912 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001913 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001914 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001915 main.cleanup()
1916 main.exit()
1917
kelvin-onlabd3b64892015-01-20 13:26:24 -08001918 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001919 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001920 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001921 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001922 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001923 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001924 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001925 if jsonFormat:
1926 cmdStr = "clusters -j"
1927 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001928 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001929 handle variable here contains some ANSI escape color code
1930 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001931 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001932 function. The repr( handle ) output when printed shows the ANSI
1933 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001934 variable is actually repr( somestring ) and json.loads would
1935 fail with the escape sequence. So we take off that escape
1936 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001937
kelvin-onlabd3b64892015-01-20 13:26:24 -08001938 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1939 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001940 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001941 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1942 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001943 return handle1
1944 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001945 cmdStr = "clusters"
1946 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001947 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001948 except TypeError:
1949 main.log.exception( self.name + ": Object not as expected" )
1950 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001951 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001952 main.log.error( self.name + ": EOF exception found" )
1953 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001954 main.cleanup()
1955 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001956 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001957 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001958 main.cleanup()
1959 main.exit()
1960
kelvin-onlabd3b64892015-01-20 13:26:24 -08001961 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001962 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001963 CLI command to get the current leader for the Election test application
1964 NOTE: Requires installation of the onos-app-election feature
1965 Returns: Node IP of the leader if one exists
1966 None if none exists
1967 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001968 """
Jon Hall94fd0472014-12-08 11:52:42 -08001969 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001970 cmdStr = "election-test-leader"
1971 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001972 # Leader
1973 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001974 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001975 nodeSearch = re.search( leaderPattern, response )
1976 if nodeSearch:
1977 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001978 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001979 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001980 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001981 # no leader
1982 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001983 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001984 nullSearch = re.search( nullPattern, response )
1985 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001986 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001987 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001988 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001989 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001990 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001991 if re.search( errorPattern, response ):
1992 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001993 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001994 return main.FALSE
1995 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001996 main.log.error( "Error in election_test_leader: " +
1997 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001998 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001999 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002000 except TypeError:
2001 main.log.exception( self.name + ": Object not as expected" )
2002 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002003 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002004 main.log.error( self.name + ": EOF exception found" )
2005 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002006 main.cleanup()
2007 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002008 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002009 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002010 main.cleanup()
2011 main.exit()
2012
kelvin-onlabd3b64892015-01-20 13:26:24 -08002013 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002014 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002015 CLI command to run for leadership of the Election test application.
2016 NOTE: Requires installation of the onos-app-election feature
2017 Returns: Main.TRUE on success
2018 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08002019 """
Jon Hall94fd0472014-12-08 11:52:42 -08002020 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002021 cmdStr = "election-test-run"
2022 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002023 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002024 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002025 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002026 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08002027 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08002028 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002029 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002030 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002031 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002032 errorPattern = "Command\snot\sfound"
2033 if re.search( errorPattern, response ):
2034 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002035 return main.FALSE
2036 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002037 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002038 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002039 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002040 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002041 except TypeError:
2042 main.log.exception( self.name + ": Object not as expected" )
2043 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002044 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002045 main.log.error( self.name + ": EOF exception found" )
2046 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002047 main.cleanup()
2048 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002049 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002050 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002051 main.cleanup()
2052 main.exit()
2053
kelvin-onlabd3b64892015-01-20 13:26:24 -08002054 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08002055 """
Jon Hall94fd0472014-12-08 11:52:42 -08002056 * CLI command to withdraw the local node from leadership election for
2057 * the Election test application.
2058 #NOTE: Requires installation of the onos-app-election feature
2059 Returns: Main.TRUE on success
2060 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08002061 """
Jon Hall94fd0472014-12-08 11:52:42 -08002062 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002063 cmdStr = "election-test-withdraw"
2064 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002065 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002066 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002067 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002068 if re.search( successPattern, response ):
2069 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002070 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002071 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002072 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002073 errorPattern = "Command\snot\sfound"
2074 if re.search( errorPattern, response ):
2075 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002076 return main.FALSE
2077 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002078 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002079 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002080 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002081 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002082 except TypeError:
2083 main.log.exception( self.name + ": Object not as expected" )
2084 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002085 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002086 main.log.error( self.name + ": EOF exception found" )
2087 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002088 main.cleanup()
2089 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002090 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002091 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002092 main.cleanup()
2093 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002094
kelvin8ec71442015-01-15 16:57:00 -08002095 def getDevicePortsEnabledCount( self, dpid ):
2096 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002097 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002098 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002099 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08002100 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002101 cmdStr = "onos:ports -e " + dpid + " | wc -l"
2102 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002103 if re.search( "No such device", output ):
2104 main.log.error( "Error in getting ports" )
2105 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002106 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002107 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002108 except TypeError:
2109 main.log.exception( self.name + ": Object not as expected" )
2110 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002111 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002112 main.log.error( self.name + ": EOF exception found" )
2113 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002114 main.cleanup()
2115 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002116 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002117 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002118 main.cleanup()
2119 main.exit()
2120
kelvin8ec71442015-01-15 16:57:00 -08002121 def getDeviceLinksActiveCount( self, dpid ):
2122 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002123 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002124 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002125 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002126 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002127 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
2128 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002129 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002130 main.log.error( "Error in getting ports " )
2131 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002132 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002133 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002134 except TypeError:
2135 main.log.exception( self.name + ": Object not as expected" )
2136 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002137 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002138 main.log.error( self.name + ": EOF exception found" )
2139 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002140 main.cleanup()
2141 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002142 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002143 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002144 main.cleanup()
2145 main.exit()
2146
kelvin8ec71442015-01-15 16:57:00 -08002147 def getAllIntentIds( self ):
2148 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002149 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002150 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002151 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002152 cmdStr = "onos:intents | grep id="
2153 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002154 if re.search( "Error", output ):
2155 main.log.error( "Error in getting ports" )
2156 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002157 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002158 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002159 except TypeError:
2160 main.log.exception( self.name + ": Object not as expected" )
2161 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002162 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002163 main.log.error( self.name + ": EOF exception found" )
2164 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002165 main.cleanup()
2166 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002167 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002168 main.log.exception( self.name + ": Uncaught exception!" )
2169 main.cleanup()
2170 main.exit()
2171
Jon Hall73509952015-02-24 16:42:56 -08002172 def intentSummary( self ):
2173 """
Jon Hallefbd9792015-03-05 16:11:36 -08002174 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08002175 """
2176 try:
2177 intents = self.intents( )
2178 intentStates = []
Jon Hall63604932015-02-26 17:09:50 -08002179 for intent in json.loads( intents ): # Iter through intents of a node
Jon Hall73509952015-02-24 16:42:56 -08002180 intentStates.append( intent.get( 'state', None ) )
Jon Hall63604932015-02-26 17:09:50 -08002181 out = [ (i, intentStates.count( i ) ) for i in set( intentStates ) ]
2182 main.log.info( dict( out ) )
Jon Hall73509952015-02-24 16:42:56 -08002183 return dict( out )
2184 except TypeError:
2185 main.log.exception( self.name + ": Object not as expected" )
2186 return None
2187 except pexpect.EOF:
2188 main.log.error( self.name + ": EOF exception found" )
2189 main.log.error( self.name + ": " + self.handle.before )
2190 main.cleanup()
2191 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002192 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08002193 main.log.exception( self.name + ": Uncaught exception!" )
2194 main.cleanup()
2195 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002196
2197 def leaders( self ):
2198 """
2199 Returns the output of the leaders command.
2200 """
2201 # FIXME: add json output
2202 try:
2203 output = self.sendline( "onos:leaders" )
2204 main.log.warn( output )
2205 return output
2206 except TypeError:
2207 main.log.exception( self.name + ": Object not as expected" )
2208 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002209 except pexpect.EOF:
2210 main.log.error( self.name + ": EOF exception found" )
2211 main.log.error( self.name + ": " + self.handle.before )
2212 main.cleanup()
2213 main.exit()
2214 except:
Jon Hall63604932015-02-26 17:09:50 -08002215 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002216 main.cleanup()
2217 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002218
2219 def pendingMap( self ):
2220 """
2221 Returns the output of the intent Pending map.
2222 """
2223 # FIXME: add json output
2224 try:
2225 output = self.sendline( "onos:intents -p" )
2226 main.log.warn( output )
2227 return output
2228 except TypeError:
2229 main.log.exception( self.name + ": Object not as expected" )
2230 return None
2231 except pexpect.EOF:
2232 main.log.error( self.name + ": EOF exception found" )
2233 main.log.error( self.name + ": " + self.handle.before )
2234 main.cleanup()
2235 main.exit()
2236 except:
2237 main.log.exception( self.name + ": Uncaught exception!" )
2238 main.cleanup()
2239 main.exit()
2240
2241 def partitions( self ):
2242 """
2243 Returns the output of the raft partitions command for ONOS.
2244 """
2245 # FIXME: add json output
2246 try:
2247 output = self.sendline( "partitions" )
2248 main.log.warn( output )
2249 return output
2250 except TypeError:
2251 main.log.exception( self.name + ": Object not as expected" )
2252 return None
2253 except pexpect.EOF:
2254 main.log.error( self.name + ": EOF exception found" )
2255 main.log.error( self.name + ": " + self.handle.before )
2256 main.cleanup()
2257 main.exit()
2258 except:
2259 main.log.exception( self.name + ": Uncaught exception!" )
2260 main.cleanup()
2261 main.exit()
2262