blob: 7146211dd73ce2e899c4868663af97edc1dcd4f6 [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
kelvin8ec71442015-01-15 16:57:00 -08003"""
andrewonlab95ce8322014-10-13 14:12:04 -04004This driver enters the onos> prompt to issue commands.
5
kelvin8ec71442015-01-15 16:57:00 -08006Please follow the coding style demonstrated by existing
andrewonlab95ce8322014-10-13 14:12:04 -04007functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040014shreya@onlab.us
andrewonlab95ce8322014-10-13 14:12:04 -040015
16OCT 13 2014
17
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab95ce8322014-10-13 14:12:04 -040019import sys
andrewonlab95ce8322014-10-13 14:12:04 -040020import pexpect
21import re
Jon Hall30b82fa2015-03-04 17:15:43 -080022import json
23import types
kelvin8ec71442015-01-15 16:57:00 -080024sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040025from drivers.common.clidriver import CLI
26
andrewonlab95ce8322014-10-13 14:12:04 -040027
kelvin8ec71442015-01-15 16:57:00 -080028class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040029
kelvin8ec71442015-01-15 16:57:00 -080030 def __init__( self ):
31 """
32 Initialize client
33 """
Jon Hallefbd9792015-03-05 16:11:36 -080034 self.name = None
35 self.home = None
36 self.handle = None
kelvin8ec71442015-01-15 16:57:00 -080037 super( CLI, self ).__init__()
38
39 def connect( self, **connectargs ):
40 """
andrewonlab95ce8322014-10-13 14:12:04 -040041 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080042 """
andrewonlab95ce8322014-10-13 14:12:04 -040043 try:
44 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080045 vars( self )[ key ] = connectargs[ key ]
andrewonlab95ce8322014-10-13 14:12:04 -040046 self.home = "~/ONOS"
47 for key in self.options:
48 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080049 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040050 break
kelvin-onlabfb521662015-02-27 09:52:40 -080051 if self.home is None or self.home == "":
kelvin-onlabd6634ac2015-01-29 14:23:10 -080052 self.home = "~/ONOS"
kelvin-onlabfb521662015-02-27 09:52:40 -080053
kelvin8ec71442015-01-15 16:57:00 -080054 self.name = self.options[ 'name' ]
55 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080056 user_name=self.user_name,
57 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080058 port=self.port,
59 pwd=self.pwd,
60 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040061
kelvin8ec71442015-01-15 16:57:00 -080062 self.handle.sendline( "cd " + self.home )
63 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040064 if self.handle:
65 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080066 else:
67 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040068 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080069 except TypeError:
70 main.log.exception( self.name + ": Object not as expected" )
71 return None
andrewonlab95ce8322014-10-13 14:12:04 -040072 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080073 main.log.error( self.name + ": EOF exception found" )
74 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040075 main.cleanup()
76 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080077 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -080078 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -040079 main.cleanup()
80 main.exit()
81
kelvin8ec71442015-01-15 16:57:00 -080082 def disconnect( self ):
83 """
andrewonlab95ce8322014-10-13 14:12:04 -040084 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080085 """
Jon Halld61331b2015-02-17 16:35:47 -080086 response = main.TRUE
Jon Hallefbd9792015-03-05 16:11:36 -080087 # noinspection PyBroadException
andrewonlab95ce8322014-10-13 14:12:04 -040088 try:
Jon Hallb7fce3e2015-03-04 10:18:32 -080089 self.logout()
kelvin8ec71442015-01-15 16:57:00 -080090 self.handle.sendline( "" )
91 self.handle.expect( "\$" )
92 self.handle.sendline( "exit" )
93 self.handle.expect( "closed" )
Jon Halld4d4b372015-01-28 16:02:41 -080094 except TypeError:
95 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -080096 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -040097 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080098 main.log.error( self.name + ": EOF exception found" )
99 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800100 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800101 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400102 response = main.FALSE
103 return response
104
kelvin8ec71442015-01-15 16:57:00 -0800105 def logout( self ):
106 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500107 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800108 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500109 try:
kelvin8ec71442015-01-15 16:57:00 -0800110 self.handle.sendline( "" )
Jon Hallb7fce3e2015-03-04 10:18:32 -0800111 i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ],
112 timeout=10 )
113 if i == 0: # In ONOS CLI
kelvin8ec71442015-01-15 16:57:00 -0800114 self.handle.sendline( "logout" )
115 self.handle.expect( "\$" )
Jon Hallb7fce3e2015-03-04 10:18:32 -0800116 elif i == 1: # not in CLI
andrewonlab9627f432014-11-14 12:45:10 -0500117 return main.TRUE
Jon Hallb7fce3e2015-03-04 10:18:32 -0800118 elif i == 3: # Timeout
119 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -0800120 except TypeError:
121 main.log.exception( self.name + ": Object not as expected" )
122 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500123 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800124 main.log.error( self.name + ": eof exception found" )
125 main.log.error( self.name + ": " +
126 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500127 main.cleanup()
128 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800129 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800130 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500131 main.cleanup()
132 main.exit()
133
kelvin-onlabd3b64892015-01-20 13:26:24 -0800134 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800135 """
andrewonlab95ce8322014-10-13 14:12:04 -0400136 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800137
andrewonlab95ce8322014-10-13 14:12:04 -0400138 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800139 """
andrewonlab95ce8322014-10-13 14:12:04 -0400140 try:
141 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800142 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400143 main.cleanup()
144 main.exit()
145 else:
kelvin8ec71442015-01-15 16:57:00 -0800146 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800147 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800148 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400149 # and that this driver will have to change accordingly
Jon Hall1e03cb62015-02-19 12:07:12 -0800150 self.handle.expect( "ONOS_CELL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800151 handleBefore = self.handle.before
152 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800153 # Get the rest of the handle
154 self.handle.sendline( "" )
155 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800156 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400157
kelvin-onlabd3b64892015-01-20 13:26:24 -0800158 main.log.info( "Cell call returned: " + handleBefore +
159 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400160
161 return main.TRUE
162
Jon Halld4d4b372015-01-28 16:02:41 -0800163 except TypeError:
164 main.log.exception( self.name + ": Object not as expected" )
165 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400166 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800167 main.log.error( self.name + ": eof exception found" )
168 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400169 main.cleanup()
170 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800171 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800172 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400173 main.cleanup()
174 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800175
kelvin-onlabd3b64892015-01-20 13:26:24 -0800176 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800177 """
Jon Hallefbd9792015-03-05 16:11:36 -0800178 karafTimeout is an optional argument. karafTimeout value passed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 by user would be used to set the current karaf shell idle timeout.
180 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800181 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800182 Below is an example to start a session with 60 seconds idle timeout
183 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800184
Hari Krishna25d42f72015-01-05 15:08:28 -0800185 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800186 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800187
kelvin-onlabd3b64892015-01-20 13:26:24 -0800188 Note: karafTimeout is left as str so that this could be read
189 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800190 """
andrewonlab95ce8322014-10-13 14:12:04 -0400191 try:
kelvin8ec71442015-01-15 16:57:00 -0800192 self.handle.sendline( "" )
193 x = self.handle.expect( [
194 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500195
196 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800197 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500198 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400199
kelvin8ec71442015-01-15 16:57:00 -0800200 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800201 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800202 i = self.handle.expect( [
203 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800204 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400205
206 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800207 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800208 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800209 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800210 "config:property-set -p org.apache.karaf.shell\
211 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800212 karafTimeout )
213 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800214 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800215 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400216 return main.TRUE
217 else:
kelvin8ec71442015-01-15 16:57:00 -0800218 # If failed, send ctrl+c to process and try again
219 main.log.info( "Starting CLI failed. Retrying..." )
220 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800221 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800222 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
223 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400224 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800225 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800226 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800227 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800228 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800229 "config:property-set -p org.apache.karaf.shell\
230 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800231 karafTimeout )
232 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800233 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800234 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400235 return main.TRUE
236 else:
kelvin8ec71442015-01-15 16:57:00 -0800237 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800238 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400239 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400240
Jon Halld4d4b372015-01-28 16:02:41 -0800241 except TypeError:
242 main.log.exception( self.name + ": Object not as expected" )
243 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400244 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800245 main.log.error( self.name + ": EOF exception found" )
246 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400247 main.cleanup()
248 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800249 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800250 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400251 main.cleanup()
252 main.exit()
253
Jon Hallefbd9792015-03-05 16:11:36 -0800254 def log( self, cmdStr, level="" ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800255 """
256 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800257 returns main.TRUE on success
Jon Hallefbd9792015-03-05 16:11:36 -0800258 returns main.FALSE if Error occurred
kelvin-onlab338f5512015-02-06 10:53:16 -0800259 Available level: DEBUG, TRACE, INFO, WARN, ERROR
260 Level defaults to INFO
kelvin-onlab9f541032015-02-04 16:19:53 -0800261 """
262 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800263 lvlStr = ""
264 if level:
265 lvlStr = "--level=" + level
266
kelvin-onlab9f541032015-02-04 16:19:53 -0800267 self.handle.sendline( "" )
268 self.handle.expect( "onos>" )
kelvin-onlab338f5512015-02-06 10:53:16 -0800269 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
kelvin-onlab9f541032015-02-04 16:19:53 -0800270 self.handle.expect( "onos>" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800271
kelvin-onlab9f541032015-02-04 16:19:53 -0800272 response = self.handle.before
273 if re.search( "Error", response ):
274 return main.FALSE
275 return main.TRUE
276
277 except pexpect.EOF:
278 main.log.error( self.name + ": EOF exception found" )
279 main.log.error( self.name + ": " + self.handle.before )
280 main.cleanup()
281 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800282 except Exception:
kelvin-onlabfb521662015-02-27 09:52:40 -0800283 main.log.exception( self.name + ": Uncaught exception!" )
kelvin-onlab9f541032015-02-04 16:19:53 -0800284 main.cleanup()
285 main.exit()
286
kelvin-onlabd3b64892015-01-20 13:26:24 -0800287 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800288 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800289 Send a completely user specified string to
290 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400291 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800292
andrewonlaba18f6bf2014-10-13 19:31:54 -0400293 Warning: There are no sanity checking to commands
294 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800295 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400296 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800297 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
298 self.log( logStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800299 self.handle.sendline( cmdStr )
Jon Hall63604932015-02-26 17:09:50 -0800300 i = self.handle.expect( ["onos>", "\$", pexpect.TIMEOUT] )
301 response = self.handle.before
302 if i == 2:
303 self.handle.sendline()
304 self.handle.expect( "\$" )
305 response += self.handle.before
306 print response
307 try:
308 print self.handle.after
309 except:
310 pass
311 # TODO: do something with i
Jon Hallaea67aa2015-01-23 13:30:57 -0800312 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
313 + self.name + "." )
Jon Hall7bdfc122015-01-23 11:45:32 -0800314 # Remove control strings from output
315 ansiEscape = re.compile( r'\x1b[^m]*m' )
Jon Hall63604932015-02-26 17:09:50 -0800316 response = ansiEscape.sub( '', response )
kelvin-onlabfb521662015-02-27 09:52:40 -0800317 # Remove extra return chars that get added
Jon Hall63604932015-02-26 17:09:50 -0800318 response = re.sub( r"\s\r", "", response )
319 response = response.strip()
320 # parse for just the output, remove the cmd from response
321 output = response.split( cmdStr, 1 )[1]
Jon Hall7bdfc122015-01-23 11:45:32 -0800322 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800323 except TypeError:
324 main.log.exception( self.name + ": Object not as expected" )
325 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400326 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800327 main.log.error( self.name + ": EOF exception found" )
328 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400329 main.cleanup()
330 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800331 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800332 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400333 main.cleanup()
334 main.exit()
335
kelvin8ec71442015-01-15 16:57:00 -0800336 # IMPORTANT NOTE:
337 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800338 # the cli command changing 'a:b' with 'aB'.
339 # Ex ) onos:topology > onosTopology
340 # onos:links > onosLinks
341 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800342
kelvin-onlabd3b64892015-01-20 13:26:24 -0800343 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800344 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400345 Adds a new cluster node by ID and address information.
346 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800347 * nodeId
348 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400349 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800350 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800351 """
Jon Hallefbd9792015-03-05 16:11:36 -0800352 # noinspection PyBroadException
andrewonlabc2d05aa2014-10-13 16:51:10 -0400353 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800354 cmdStr = "add-node " + str( nodeId ) + " " +\
355 str( ONOSIp ) + " " + str( tcpPort )
356 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800357 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800358 main.log.error( "Error in adding node" )
359 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800360 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400361 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800362 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400363 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800364 except TypeError:
365 main.log.exception( self.name + ": Object not as expected" )
366 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400367 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800368 main.log.error( self.name + ": EOF exception found" )
369 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400370 main.cleanup()
371 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800372 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800373 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400374 main.cleanup()
375 main.exit()
376
kelvin-onlabd3b64892015-01-20 13:26:24 -0800377 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800378 """
andrewonlab86dc3082014-10-13 18:18:38 -0400379 Removes a cluster by ID
380 Issues command: 'remove-node [<node-id>]'
381 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800382 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800383 """
andrewonlab86dc3082014-10-13 18:18:38 -0400384 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400385
kelvin-onlabd3b64892015-01-20 13:26:24 -0800386 cmdStr = "remove-node " + str( nodeId )
387 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800388 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400389
390 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800391
Jon Halld4d4b372015-01-28 16:02:41 -0800392 except TypeError:
393 main.log.exception( self.name + ": Object not as expected" )
394 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400395 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800396 main.log.error( self.name + ": EOF exception found" )
397 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400398 main.cleanup()
399 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800400 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800401 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400402 main.cleanup()
403 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400404
kelvin8ec71442015-01-15 16:57:00 -0800405 def nodes( self ):
406 """
andrewonlab7c211572014-10-15 16:45:20 -0400407 List the nodes currently visible
408 Issues command: 'nodes'
409 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800410 """
andrewonlab7c211572014-10-15 16:45:20 -0400411 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800412 cmdStr = "nodes"
413 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400414 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800415 except TypeError:
416 main.log.exception( self.name + ": Object not as expected" )
417 return None
andrewonlab7c211572014-10-15 16:45:20 -0400418 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800419 main.log.error( self.name + ": EOF exception found" )
420 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400421 main.cleanup()
422 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800423 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800424 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400425 main.cleanup()
426 main.exit()
427
kelvin8ec71442015-01-15 16:57:00 -0800428 def topology( self ):
429 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400430 Shows the current state of the topology
Jon Hallefbd9792015-03-05 16:11:36 -0800431 by issuing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800432 """
andrewonlab95ce8322014-10-13 14:12:04 -0400433 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800434 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800435 cmdStr = "onos:topology"
436 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800437 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400438 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800439 except TypeError:
440 main.log.exception( self.name + ": Object not as expected" )
441 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400442 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800443 main.log.error( self.name + ": EOF exception found" )
444 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400445 main.cleanup()
446 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800447 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800448 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400449 main.cleanup()
450 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800451
kelvin-onlabd3b64892015-01-20 13:26:24 -0800452 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800453 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800454 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400455 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800456 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400457 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800458 cmdStr = "feature:install " + str( featureStr )
459 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800460 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400461 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800462 except TypeError:
463 main.log.exception( self.name + ": Object not as expected" )
464 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400465 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800466 main.log.error( self.name + ": EOF exception found" )
467 main.log.error( self.name + ": " + self.handle.before )
468 main.log.report( "Failed to install feature" )
469 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400470 main.cleanup()
471 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800472 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800473 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800474 main.log.report( "Failed to install feature" )
475 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400476 main.cleanup()
477 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800478
kelvin-onlabd3b64892015-01-20 13:26:24 -0800479 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800480 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400481 Uninstalls a specified feature
482 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800483 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400484 try:
Jon Hall30b82fa2015-03-04 17:15:43 -0800485 cmdStr = 'feature:list -i | grep "' + featureStr + '"'
486 handle = self.sendline( cmdStr )
487 if handle != '':
488 cmdStr = "feature:uninstall " + str( featureStr )
489 self.sendline( cmdStr )
490 # TODO: Check for possible error responses from karaf
491 else:
Jon Hallefbd9792015-03-05 16:11:36 -0800492 main.log.info( "Feature needs to be installed before " +
493 "uninstalling it" )
shahshreya280223a2015-02-26 12:25:25 -0800494 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800495 except TypeError:
496 main.log.exception( self.name + ": Object not as expected" )
497 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400498 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800499 main.log.error( self.name + ": EOF exception found" )
500 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400501 main.cleanup()
502 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800503 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800504 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400505 main.cleanup()
506 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800507
kelvin-onlabd3b64892015-01-20 13:26:24 -0800508 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800509 """
Jon Hall7b02d952014-10-17 20:14:54 -0400510 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400511 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800512 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800513 """
andrewonlab86dc3082014-10-13 18:18:38 -0400514 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800515 if jsonFormat:
516 cmdStr = "devices -j"
517 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800518 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800519 handle variable here contains some ANSI escape color code
520 sequences at the end which are invisible in the print command
521 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800522 function. The repr( handle ) output when printed shows the
523 ANSI escape sequences. In json.loads( somestring ), this
524 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800525 json.loads would fail with the escape sequence. So we take off
526 that escape sequence using:
527
kelvin-onlabd3b64892015-01-20 13:26:24 -0800528 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
529 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800530 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800531 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
532 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400533 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400534 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800535 cmdStr = "devices"
536 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400537 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800538 except TypeError:
539 main.log.exception( self.name + ": Object not as expected" )
540 return None
andrewonlab7c211572014-10-15 16:45:20 -0400541 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800542 main.log.error( self.name + ": EOF exception found" )
543 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400544 main.cleanup()
545 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800546 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800547 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400548 main.cleanup()
549 main.exit()
550
kelvin-onlabd3b64892015-01-20 13:26:24 -0800551 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800552 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800553 This balances the devices across all controllers
554 by issuing command: 'onos> onos:balance-masters'
555 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800556 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800557 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800558 cmdStr = "onos:balance-masters"
559 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800560 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800561 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800562 except TypeError:
563 main.log.exception( self.name + ": Object not as expected" )
564 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800565 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800566 main.log.error( self.name + ": EOF exception found" )
567 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800568 main.cleanup()
569 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800570 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800571 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800572 main.cleanup()
573 main.exit()
574
kelvin-onlabd3b64892015-01-20 13:26:24 -0800575 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800576 """
Jon Halle8217482014-10-17 13:49:14 -0400577 Lists all core links
578 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800579 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800580 """
Jon Halle8217482014-10-17 13:49:14 -0400581 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800582 if jsonFormat:
583 cmdStr = "links -j"
584 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800585 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800586 handle variable here contains some ANSI escape color code
587 sequences at the end which are invisible in the print command
588 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800589 function. The repr( handle ) output when printed shows the ANSI
590 escape sequences. In json.loads( somestring ), this somestring
591 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800592 fail with the escape sequence. So we take off that escape
593 sequence using:
594
kelvin-onlabd3b64892015-01-20 13:26:24 -0800595 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
596 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800597 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800598 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
599 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400600 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400601 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800602 cmdStr = "links"
603 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400604 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800605 except TypeError:
606 main.log.exception( self.name + ": Object not as expected" )
607 return None
Jon Halle8217482014-10-17 13:49:14 -0400608 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800609 main.log.error( self.name + ": EOF exception found" )
610 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400611 main.cleanup()
612 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800613 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800614 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400615 main.cleanup()
616 main.exit()
617
kelvin-onlabd3b64892015-01-20 13:26:24 -0800618 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800619 """
Jon Halle8217482014-10-17 13:49:14 -0400620 Lists all ports
621 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800622 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800623 """
Jon Halle8217482014-10-17 13:49:14 -0400624 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800625 if jsonFormat:
626 cmdStr = "ports -j"
627 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800628 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800629 handle variable here contains some ANSI escape color code
630 sequences at the end which are invisible in the print command
631 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800632 function. The repr( handle ) output when printed shows the ANSI
633 escape sequences. In json.loads( somestring ), this somestring
634 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800635 fail with the escape sequence. So we take off that escape
Jon Hallefbd9792015-03-05 16:11:36 -0800636 sequence using the following commands:
Jon Halle3f39ff2015-01-13 11:50:53 -0800637
kelvin-onlabd3b64892015-01-20 13:26:24 -0800638 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
639 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800640 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800641 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
642 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400643 return handle1
644
Jon Halle8217482014-10-17 13:49:14 -0400645 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800646 cmdStr = "ports"
647 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800648 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800649 except TypeError:
650 main.log.exception( self.name + ": Object not as expected" )
651 return None
Jon Halle8217482014-10-17 13:49:14 -0400652 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800653 main.log.error( self.name + ": EOF exception found" )
654 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400655 main.cleanup()
656 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800657 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800658 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400659 main.cleanup()
660 main.exit()
661
kelvin-onlabd3b64892015-01-20 13:26:24 -0800662 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800663 """
Jon Hall983a1702014-10-28 18:44:22 -0400664 Lists all devices and the controllers with roles assigned to them
665 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800666 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800667 """
andrewonlab7c211572014-10-15 16:45:20 -0400668 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800669 if jsonFormat:
670 cmdStr = "roles -j"
671 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800672 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800673 handle variable here contains some ANSI escape color code
674 sequences at the end which are invisible in the print command
675 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800676 function. The repr( handle ) output when printed shows the ANSI
677 escape sequences. In json.loads( somestring ), this somestring
678 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800679 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500680
Jon Halle3f39ff2015-01-13 11:50:53 -0800681 So we take off that escape sequence using the following
682 commads:
683
kelvin-onlabd3b64892015-01-20 13:26:24 -0800684 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
685 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800686 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800687 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
688 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400689 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400690
andrewonlab7c211572014-10-15 16:45:20 -0400691 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800692 cmdStr = "roles"
693 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800694 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800695 except TypeError:
696 main.log.exception( self.name + ": Object not as expected" )
697 return None
Jon Hall983a1702014-10-28 18:44:22 -0400698 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800699 main.log.error( self.name + ": EOF exception found" )
700 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400701 main.cleanup()
702 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800703 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800704 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400705 main.cleanup()
706 main.exit()
707
kelvin-onlabd3b64892015-01-20 13:26:24 -0800708 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800709 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800710 Given the a string containing the json representation of the "roles"
711 cli command and a partial or whole device id, returns a json object
712 containing the roles output for the first device whose id contains
713 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400714
715 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800716 A dict of the role assignments for the given device or
717 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800718 """
Jon Hall983a1702014-10-28 18:44:22 -0400719 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800720 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400721 return None
722 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800723 rawRoles = self.roles()
724 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800725 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800726 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800727 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800728 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400729 return device
730 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800731 except TypeError:
732 main.log.exception( self.name + ": Object not as expected" )
733 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400734 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800735 main.log.error( self.name + ": EOF exception found" )
736 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400737 main.cleanup()
738 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800739 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800740 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400741 main.cleanup()
742 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800743
kelvin-onlabd3b64892015-01-20 13:26:24 -0800744 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800745 """
Jon Hall94fd0472014-12-08 11:52:42 -0800746 Iterates through each device and checks if there is a master assigned
747 Returns: main.TRUE if each device has a master
748 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800749 """
Jon Hall94fd0472014-12-08 11:52:42 -0800750 try:
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
756 if device[ 'master' ] == "none":
757 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800758 return main.FALSE
759 return main.TRUE
760
Jon Halld4d4b372015-01-28 16:02:41 -0800761 except TypeError:
762 main.log.exception( self.name + ": Object not as expected" )
763 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800764 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800765 main.log.error( self.name + ": EOF exception found" )
766 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800767 main.cleanup()
768 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800769 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800770 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800771 main.cleanup()
772 main.exit()
773
kelvin-onlabd3b64892015-01-20 13:26:24 -0800774 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800775 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400776 Returns string of paths, and the cost.
777 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800778 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400779 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800780 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
781 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800782 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800783 main.log.error( "Error in getting paths" )
784 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400785 else:
kelvin8ec71442015-01-15 16:57:00 -0800786 path = handle.split( ";" )[ 0 ]
787 cost = handle.split( ";" )[ 1 ]
788 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800789 except TypeError:
790 main.log.exception( self.name + ": Object not as expected" )
791 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400792 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 )
andrewonlab3e15ead2014-10-15 14:21:34 -0400795 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!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400799 main.cleanup()
800 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800801
kelvin-onlabd3b64892015-01-20 13:26:24 -0800802 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800803 """
Jon Hallffb386d2014-11-21 13:43:38 -0800804 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400805 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800806 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800807 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400808 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800809 if jsonFormat:
810 cmdStr = "hosts -j"
811 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800812 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800813 handle variable here contains some ANSI escape color code
814 sequences at the end which are invisible in the print command
815 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800816 function. The repr( handle ) output when printed shows the ANSI
817 escape sequences. In json.loads( somestring ), this somestring
818 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800819 fail with the escape sequence. So we take off that escape
820 sequence using:
821
kelvin-onlabd3b64892015-01-20 13:26:24 -0800822 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
823 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800824 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800825 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
826 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400827 return handle1
828 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800829 cmdStr = "hosts"
830 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400831 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800832 except TypeError:
833 main.log.exception( self.name + ": Object not as expected" )
834 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400835 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800836 main.log.error( self.name + ": EOF exception found" )
837 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400838 main.cleanup()
839 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800840 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800841 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400842 main.cleanup()
843 main.exit()
844
kelvin-onlabd3b64892015-01-20 13:26:24 -0800845 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800846 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400847 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800848
Jon Hallefbd9792015-03-05 16:11:36 -0800849 Note: mac must be a colon separated mac address, but could be a
Jon Halle3f39ff2015-01-13 11:50:53 -0800850 partial mac address
851
Jon Hall42db6dc2014-10-24 19:03:48 -0400852 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800853 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400854 try:
kelvin8ec71442015-01-15 16:57:00 -0800855 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400856 return None
857 else:
858 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800859 rawHosts = self.hosts()
860 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800861 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800862 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800863 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800864 if not host:
865 pass
866 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400867 return host
868 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800869 except TypeError:
870 main.log.exception( self.name + ": Object not as expected" )
871 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400872 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800873 main.log.error( self.name + ": EOF exception found" )
874 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400875 main.cleanup()
876 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800877 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800878 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400879 main.cleanup()
880 main.exit()
881
kelvin-onlabd3b64892015-01-20 13:26:24 -0800882 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800883 """
884 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400885 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800886
andrewonlab3f0a4af2014-10-17 12:25:14 -0400887 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800888 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400889 IMPORTANT:
890 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800891 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400892 Furthermore, it assumes that value of VLAN is '-1'
893 Description:
kelvin8ec71442015-01-15 16:57:00 -0800894 Converts mininet hosts ( h1, h2, h3... ) into
895 ONOS format ( 00:00:00:00:00:01/-1 , ... )
896 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400897 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800898 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400899
kelvin-onlabd3b64892015-01-20 13:26:24 -0800900 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800901 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800902 hostHex = hex( int( host ) ).zfill( 12 )
903 hostHex = str( hostHex ).replace( 'x', '0' )
904 i = iter( str( hostHex ) )
905 hostHex = ":".join( a + b for a, b in zip( i, i ) )
906 hostHex = hostHex + "/-1"
907 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400908
kelvin-onlabd3b64892015-01-20 13:26:24 -0800909 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400910
Jon Halld4d4b372015-01-28 16:02:41 -0800911 except TypeError:
912 main.log.exception( self.name + ": Object not as expected" )
913 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400914 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800915 main.log.error( self.name + ": EOF exception found" )
916 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400917 main.cleanup()
918 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800919 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800920 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400921 main.cleanup()
922 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400923
kelvin-onlabd3b64892015-01-20 13:26:24 -0800924 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800925 """
andrewonlabe6745342014-10-17 14:29:13 -0400926 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800927 * hostIdOne: ONOS host id for host1
928 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400929 Description:
Jon Hallefbd9792015-03-05 16:11:36 -0800930 Adds a host-to-host intent ( bidirectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500931 specifying the two hosts.
kelvin-onlabfb521662015-02-27 09:52:40 -0800932 Returns:
933 A string of the intent id or None on Error
kelvin8ec71442015-01-15 16:57:00 -0800934 """
andrewonlabe6745342014-10-17 14:29:13 -0400935 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800936 cmdStr = "add-host-intent " + str( hostIdOne ) +\
937 " " + str( hostIdTwo )
938 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800939 if re.search( "Error", handle ):
940 main.log.error( "Error in adding Host intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800941 return None
Hari Krishnaac4e1782015-01-26 12:09:12 -0800942 else:
943 main.log.info( "Host intent installed between " +
kelvin-onlabfb521662015-02-27 09:52:40 -0800944 str( hostIdOne ) + " and " + str( hostIdTwo ) )
945 match = re.search('id=0x([\da-f]+),', handle)
946 if match:
947 return match.group()[3:-1]
948 else:
949 main.log.error( "Error, intent ID not found" )
950 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800951 except TypeError:
952 main.log.exception( self.name + ": Object not as expected" )
953 return None
andrewonlabe6745342014-10-17 14:29:13 -0400954 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800955 main.log.error( self.name + ": EOF exception found" )
956 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400957 main.cleanup()
958 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800959 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800960 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400961 main.cleanup()
962 main.exit()
963
kelvin-onlabd3b64892015-01-20 13:26:24 -0800964 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800965 """
andrewonlab7b31d232014-10-24 13:31:47 -0400966 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800967 * ingressDevice: device id of ingress device
968 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400969 Optional:
970 TODO: Still needs to be implemented via dev side
kelvin-onlabfb521662015-02-27 09:52:40 -0800971 Description:
972 Adds an optical intent by specifying an ingress and egress device
973 Returns:
974 A string of the intent id or None on error
kelvin-onlab898a6c62015-01-16 14:13:53 -0800975 """
andrewonlab7b31d232014-10-24 13:31:47 -0400976 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800977 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
978 " " + str( egressDevice )
979 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800980 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800981 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -0800982 main.log.error( "Error in adding Optical intent" )
983 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400984 else:
kelvin-onlabfb521662015-02-27 09:52:40 -0800985 main.log.info( "Optical intent installed between " +
986 str( ingressDevice ) + " and " +
987 str( egressDevice ) )
988 match = re.search('id=0x([\da-f]+),', handle)
989 if match:
990 return match.group()[3:-1]
991 else:
992 main.log.error( "Error, intent ID not found" )
993 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800994 except TypeError:
995 main.log.exception( self.name + ": Object not as expected" )
996 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400997 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800998 main.log.error( self.name + ": EOF exception found" )
999 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -04001000 main.cleanup()
1001 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001002 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001003 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -04001004 main.cleanup()
1005 main.exit()
1006
kelvin-onlabd3b64892015-01-20 13:26:24 -08001007 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001008 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001009 ingressDevice,
1010 egressDevice,
1011 portIngress="",
1012 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001013 ethType="",
1014 ethSrc="",
1015 ethDst="",
1016 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001017 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001018 ipProto="",
1019 ipSrc="",
1020 ipDst="",
1021 tcpSrc="",
1022 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001023 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001024 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001025 * ingressDevice: device id of ingress device
1026 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001027 Optional:
1028 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001029 * ethSrc: specify ethSrc ( i.e. src mac addr )
1030 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001031 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001032 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001033 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001034 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001035 * ipSrc: specify ip source address
1036 * ipDst: specify ip destination address
1037 * tcpSrc: specify tcp source port
1038 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001039 Description:
kelvin8ec71442015-01-15 16:57:00 -08001040 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001041 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001042 Returns:
1043 A string of the intent id or None on error
andrewonlab289e4b72014-10-21 21:24:18 -04001044
Jon Halle3f39ff2015-01-13 11:50:53 -08001045 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001046 options developers provide for point-to-point
1047 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001048 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001049 try:
kelvin8ec71442015-01-15 16:57:00 -08001050 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001051 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001052 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001053 and not ipProto and not ipSrc and not ipDst \
1054 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001055 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001056
andrewonlab289e4b72014-10-21 21:24:18 -04001057 else:
andrewonlab36af3822014-11-18 17:48:18 -05001058 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001059
andrewonlab0c0a6772014-10-22 12:31:18 -04001060 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001061 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001062 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001063 cmd += " --ethSrc " + str( ethSrc )
1064 if ethDst:
1065 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001066 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001067 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001068 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001069 cmd += " --lambda "
1070 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001071 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001072 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001073 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001074 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001075 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001076 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001077 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001078 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001079 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001080
kelvin8ec71442015-01-15 16:57:00 -08001081 # Check whether the user appended the port
1082 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001083 if "/" in ingressDevice:
1084 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001085 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001086 if not portIngress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001087 main.log.error( "You must specify the ingress port" )
kelvin8ec71442015-01-15 16:57:00 -08001088 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001089 # Would it make sense to throw an exception and exit
1090 # the test?
1091 return None
andrewonlab36af3822014-11-18 17:48:18 -05001092
kelvin8ec71442015-01-15 16:57:00 -08001093 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001094 str( ingressDevice ) + "/" +\
1095 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001096
kelvin-onlabd3b64892015-01-20 13:26:24 -08001097 if "/" in egressDevice:
1098 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001099 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001100 if not portEgress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001101 main.log.error( "You must specify the egress port" )
1102 return None
Jon Halle3f39ff2015-01-13 11:50:53 -08001103
kelvin8ec71442015-01-15 16:57:00 -08001104 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001105 str( egressDevice ) + "/" +\
1106 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001107
kelvin-onlab898a6c62015-01-16 14:13:53 -08001108 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001109 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001110 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001111 main.log.error( "Error in adding point-to-point intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001112 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001113 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001114 # TODO: print out all the options in this message?
1115 main.log.info( "Point-to-point intent installed between " +
1116 str( ingressDevice ) + " and " +
1117 str( egressDevice ) )
1118 match = re.search('id=0x([\da-f]+),', handle)
1119 if match:
1120 return match.group()[3:-1]
1121 else:
1122 main.log.error( "Error, intent ID not found" )
1123 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001124 except TypeError:
1125 main.log.exception( self.name + ": Object not as expected" )
1126 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001127 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001128 main.log.error( self.name + ": EOF exception found" )
1129 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001130 main.cleanup()
1131 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001132 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001133 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001134 main.cleanup()
1135 main.exit()
1136
kelvin-onlabd3b64892015-01-20 13:26:24 -08001137 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001138 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001139 ingressDevice1,
1140 ingressDevice2,
1141 egressDevice,
kelvin-onlabfb521662015-02-27 09:52:40 -08001142 portIngress1="",
1143 portIngress2="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001144 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001145 ethType="",
1146 ethSrc="",
1147 ethDst="",
1148 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001149 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001150 ipProto="",
1151 ipSrc="",
1152 ipDst="",
1153 tcpSrc="",
1154 tcpDst="",
1155 setEthSrc="",
1156 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001157 """
shahshreyad0c80432014-12-04 16:56:05 -08001158 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001159 This function assumes that there would be 2 ingress devices and
1160 one egress device. For more number of ingress devices, this
1161 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001162 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001163 * ingressDevice1: device id of ingress device1
1164 * ingressDevice2: device id of ingress device2
1165 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001166 Optional:
1167 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001168 * ethSrc: specify ethSrc ( i.e. src mac addr )
1169 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001170 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001171 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001172 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001173 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001174 * ipSrc: specify ip source address
1175 * ipDst: specify ip destination address
1176 * tcpSrc: specify tcp source port
1177 * tcpDst: specify tcp destination port
1178 * setEthSrc: action to Rewrite Source MAC Address
1179 * setEthDst: action to Rewrite Destination MAC Address
1180 Description:
kelvin8ec71442015-01-15 16:57:00 -08001181 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001182 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001183 Returns:
1184 A string of the intent id or None on error
shahshreyad0c80432014-12-04 16:56:05 -08001185
Jon Halle3f39ff2015-01-13 11:50:53 -08001186 NOTE: This function may change depending on the
Jon Hallefbd9792015-03-05 16:11:36 -08001187 options developers provide for multipoint-to-singlepoint
shahshreyad0c80432014-12-04 16:56:05 -08001188 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001189 """
shahshreyad0c80432014-12-04 16:56:05 -08001190 try:
kelvin8ec71442015-01-15 16:57:00 -08001191 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001192 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001193 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001194 and not ipProto and not ipSrc and not ipDst\
1195 and not tcpSrc and not tcpDst and not setEthSrc\
1196 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001197 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001198
1199 else:
1200 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001201
shahshreyad0c80432014-12-04 16:56:05 -08001202 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001203 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001204 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001205 cmd += " --ethSrc " + str( ethSrc )
1206 if ethDst:
1207 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001208 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001209 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001210 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001211 cmd += " --lambda "
1212 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001213 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001214 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001215 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001216 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001217 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001218 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001219 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001220 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001221 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001222 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001223 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001224 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001225 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001226
kelvin8ec71442015-01-15 16:57:00 -08001227 # Check whether the user appended the port
1228 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001229 if "/" in ingressDevice1:
1230 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001231 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001232 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001233 main.log.error( "You must specify " +
1234 "the ingress port1" )
1235 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001236 return None
shahshreyad0c80432014-12-04 16:56:05 -08001237
kelvin8ec71442015-01-15 16:57:00 -08001238 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001239 str( ingressDevice1 ) + "/" +\
1240 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001241
kelvin-onlabd3b64892015-01-20 13:26:24 -08001242 if "/" in ingressDevice2:
1243 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001244 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001245 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001246 main.log.error( "You must specify " +
1247 "the ingress port2" )
1248 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001249 return None
shahshreyad0c80432014-12-04 16:56:05 -08001250
kelvin8ec71442015-01-15 16:57:00 -08001251 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001252 str( ingressDevice2 ) + "/" +\
1253 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001254
kelvin-onlabd3b64892015-01-20 13:26:24 -08001255 if "/" in egressDevice:
1256 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001257 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001258 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001259 main.log.error( "You must specify " +
1260 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001261 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001262
kelvin8ec71442015-01-15 16:57:00 -08001263 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001264 str( egressDevice ) + "/" +\
1265 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001266 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001267 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001268 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001269 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001270 main.log.error( "Error in adding multipoint-to-singlepoint " +
1271 "intent" )
1272 return None
shahshreyad0c80432014-12-04 16:56:05 -08001273 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001274 # TODO: print out all the options in this message?
1275 main.log.info( "Multipoint-to-singlepoint intent installed" +
1276 " between " + str( ingressDevice1 ) + ", " +
1277 str( ingressDevice2 ) + " and " +
1278 str( egressDevice ) )
1279 match = re.search('id=0x([\da-f]+),', handle)
1280 if match:
1281 return match.group()[3:-1]
1282 else:
1283 main.log.error( "Error, intent ID not found" )
1284 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001285 except TypeError:
1286 main.log.exception( self.name + ": Object not as expected" )
1287 return None
shahshreyad0c80432014-12-04 16:56:05 -08001288 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001289 main.log.error( self.name + ": EOF exception found" )
1290 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001291 main.cleanup()
1292 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001293 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001294 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001295 main.cleanup()
1296 main.exit()
1297
Jon Hallefbd9792015-03-05 16:11:36 -08001298 def removeIntent( self, intentId, app='org.onosproject.cli',
1299 purge=False, sync=False ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001300 """
shahshreya1c818fc2015-02-26 13:44:08 -08001301 Remove intent for specified application id and intent id
1302 Optional args:-
1303 -s or --sync: Waits for the removal before returning
1304 -p or --purge: Purge the intent from the store after removal
1305
Jon Halle3f39ff2015-01-13 11:50:53 -08001306 Returns:
1307 main.False on error and
1308 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001309 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001310 try:
shahshreya1c818fc2015-02-26 13:44:08 -08001311 cmdStr = "remove-intent "
1312 if purge:
1313 cmdStr += " -p"
1314 if sync:
1315 cmdStr += " -s"
1316
1317 cmdStr += " " + app + " " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001318 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001319 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001320 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001321 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001322 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001323 # TODO: Should this be main.TRUE
1324 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001325 except TypeError:
1326 main.log.exception( self.name + ": Object not as expected" )
1327 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001328 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001329 main.log.error( self.name + ": EOF exception found" )
1330 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001331 main.cleanup()
1332 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001333 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001334 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001335 main.cleanup()
1336 main.exit()
1337
kelvin-onlabd3b64892015-01-20 13:26:24 -08001338 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001339 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001340 NOTE: This method should be used after installing application:
1341 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001342 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001343 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001344 Description:
1345 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001346 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001347 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001348 if jsonFormat:
1349 cmdStr = "routes -j"
1350 handleTmp = self.sendline( cmdStr )
1351 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1352 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001353 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001354 cmdStr = "routes"
1355 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001356 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001357 except TypeError:
1358 main.log.exception( self.name + ": Object not as expected" )
1359 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001360 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001361 main.log.error( self.name + ": EOF exception found" )
1362 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001363 main.cleanup()
1364 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001365 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001366 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001367 main.cleanup()
1368 main.exit()
1369
kelvin-onlabd3b64892015-01-20 13:26:24 -08001370 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001371 """
andrewonlab377693f2014-10-21 16:00:30 -04001372 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001373 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001374 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001375 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001376 """
andrewonlabe6745342014-10-17 14:29:13 -04001377 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001378 if jsonFormat:
1379 cmdStr = "intents -j"
1380 handle = self.sendline( cmdStr )
1381 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1382 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001383 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001384 cmdStr = "intents"
1385 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001386 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001387 except TypeError:
1388 main.log.exception( self.name + ": Object not as expected" )
1389 return None
andrewonlabe6745342014-10-17 14:29:13 -04001390 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001391 main.log.error( self.name + ": EOF exception found" )
1392 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001393 main.cleanup()
1394 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001395 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001396 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001397 main.cleanup()
1398 main.exit()
1399
kelvin-onlab54400a92015-02-26 18:05:51 -08001400 def getIntentState(self, intentsId, intentsJson=None):
1401 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001402 Check intent state.
1403 Accepts a single intent ID (string type) or a list of intent IDs.
1404 Returns the state(string type) of the id if a single intent ID is
1405 accepted.
Jon Hallefbd9792015-03-05 16:11:36 -08001406 Returns a dictionary with intent IDs as the key and its
1407 corresponding states as the values
kelvin-onlabfb521662015-02-27 09:52:40 -08001408 Parameters:
kelvin-onlab54400a92015-02-26 18:05:51 -08001409 intentId: intent ID (string type)
1410 intentsJson: parsed json object from the onos:intents api
1411 Returns:
1412 state = An intent's state- INSTALL,WITHDRAWN etc.
1413 stateDict = Dictionary of intent's state. intent ID as the keys and
1414 state as the values.
1415 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001416 try:
1417 state = "State is Undefined"
1418 if not intentsJson:
Jon Hallefbd9792015-03-05 16:11:36 -08001419 intentsJsonTemp = json.loads( self.intents() )
kelvin-onlab54400a92015-02-26 18:05:51 -08001420 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001421 intentsJsonTemp = json.loads( intentsJson )
1422 if isinstance( intentsId, types.StringType ):
kelvin-onlab54400a92015-02-26 18:05:51 -08001423 for intent in intentsJsonTemp:
1424 if intentsId == intent['id']:
1425 state = intent['state']
1426 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001427 main.log.info( "Cannot find intent ID" + str( intentsId ) +
1428 " on the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001429 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001430 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001431 dictList = []
kelvin-onlab54400a92015-02-26 18:05:51 -08001432 for ID in intentsId:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001433 stateDict = {}
kelvin-onlab54400a92015-02-26 18:05:51 -08001434 for intents in intentsJsonTemp:
1435 if ID == intents['id']:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001436 stateDict['state'] = intents['state']
1437 stateDict['id'] = ID
Jon Hallefbd9792015-03-05 16:11:36 -08001438 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08001439 break
Jon Hallefbd9792015-03-05 16:11:36 -08001440 if len( intentsId ) != len( dictList ):
1441 main.log.info( "Cannot find some of the intent ID state" )
kelvin-onlab07dbd012015-03-04 16:29:39 -08001442 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08001443 else:
1444 main.log.info("Invalid intents ID entry")
1445 return None
kelvin-onlab54400a92015-02-26 18:05:51 -08001446 except TypeError:
1447 main.log.exception( self.name + ": Object not as expected" )
1448 return None
1449 except pexpect.EOF:
1450 main.log.error( self.name + ": EOF exception found" )
1451 main.log.error( self.name + ": " + self.handle.before )
1452 main.cleanup()
1453 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001454 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08001455 main.log.exception( self.name + ": Uncaught exception!" )
1456 main.cleanup()
1457 main.exit()
Jon Hall30b82fa2015-03-04 17:15:43 -08001458
kelvin-onlabd3b64892015-01-20 13:26:24 -08001459 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001460 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001461 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001462 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001463 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001464 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001465 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001466 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001467 if jsonFormat:
1468 cmdStr = "flows -j"
1469 handle = self.sendline( cmdStr )
1470 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1471 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001472 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001473 cmdStr = "flows"
1474 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001475 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001476 main.log.error( self.name + ".flows() response: " +
1477 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001478 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001479 except TypeError:
1480 main.log.exception( self.name + ": Object not as expected" )
1481 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001482 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001483 main.log.error( self.name + ": EOF exception found" )
1484 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001485 main.cleanup()
1486 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001487 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001488 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001489 main.cleanup()
1490 main.exit()
1491
kelvin-onlabd3b64892015-01-20 13:26:24 -08001492 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
Jon Hallefbd9792015-03-05 16:11:36 -08001493 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001494 """
andrewonlab87852b02014-11-19 18:44:19 -05001495 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001496 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001497 a specific point-to-point intent definition
1498 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001499 * dpidSrc: specify source dpid
1500 * dpidDst: specify destination dpid
1501 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001502 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001503 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001504 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001505 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001506 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001507 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001508 """
andrewonlab87852b02014-11-19 18:44:19 -05001509 try:
kelvin8ec71442015-01-15 16:57:00 -08001510 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001511 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1512 str( numIntents )
1513 if numMult:
1514 cmd += " " + str( numMult )
1515 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001516 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001517 if appId:
1518 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001519 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001520 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1521 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001522 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001523 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001524 main.log.info( handle )
1525 # Split result by newline
1526 newline = handle.split( "\r\r\n" )
1527 # Ignore the first object of list, which is empty
1528 newline = newline[ 1: ]
1529 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001530 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001531 result = result.split( ": " )
1532 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001533 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1534 main.log.info( latResult )
1535 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001536 else:
1537 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001538 except TypeError:
1539 main.log.exception( self.name + ": Object not as expected" )
1540 return None
andrewonlab87852b02014-11-19 18:44:19 -05001541 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001542 main.log.error( self.name + ": EOF exception found" )
1543 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001544 main.cleanup()
1545 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001546 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001547 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001548 main.cleanup()
1549 main.exit()
1550
kelvin-onlabd3b64892015-01-20 13:26:24 -08001551 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001552 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001553 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001554 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001555 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001556 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001557 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001558 if jsonFormat:
1559 cmdStr = "intents-events-metrics -j"
1560 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001561 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001562 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1563 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001564 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001565 cmdStr = "intents-events-metrics"
1566 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001567 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001568 except TypeError:
1569 main.log.exception( self.name + ": Object not as expected" )
1570 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001571 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001572 main.log.error( self.name + ": EOF exception found" )
1573 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001574 main.cleanup()
1575 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001576 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001577 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001578 main.cleanup()
1579 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001580
kelvin-onlabd3b64892015-01-20 13:26:24 -08001581 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001582 """
1583 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001584 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001585 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001586 """
andrewonlab867212a2014-10-22 20:13:38 -04001587 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001588 if jsonFormat:
1589 cmdStr = "topology-events-metrics -j"
1590 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001591 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001592 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1593 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001594 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001595 cmdStr = "topology-events-metrics"
1596 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001597 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001598 except TypeError:
1599 main.log.exception( self.name + ": Object not as expected" )
1600 return None
andrewonlab867212a2014-10-22 20:13:38 -04001601 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001602 main.log.error( self.name + ": EOF exception found" )
1603 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001604 main.cleanup()
1605 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001606 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001607 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001608 main.cleanup()
1609 main.exit()
1610
kelvin8ec71442015-01-15 16:57:00 -08001611 # Wrapper functions ****************
1612 # Wrapper functions use existing driver
1613 # functions and extends their use case.
1614 # For example, we may use the output of
1615 # a normal driver function, and parse it
1616 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001617
kelvin-onlabd3b64892015-01-20 13:26:24 -08001618 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001619 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001620 Description:
1621 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001622 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001623 try:
kelvin8ec71442015-01-15 16:57:00 -08001624 # Obtain output of intents function
kelvin-onlabfb521662015-02-27 09:52:40 -08001625 intentsStr = self.intents(jsonFormat=False)
kelvin-onlabd3b64892015-01-20 13:26:24 -08001626 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001627
kelvin8ec71442015-01-15 16:57:00 -08001628 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001629 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1630 for intents in intentsList:
kelvin-onlabfb521662015-02-27 09:52:40 -08001631 match = re.search('id=0x([\da-f]+),', intents)
1632 if match:
1633 tmpId = match.group()[3:-1]
1634 intentIdList.append( tmpId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001635 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001636
Jon Halld4d4b372015-01-28 16:02:41 -08001637 except TypeError:
1638 main.log.exception( self.name + ": Object not as expected" )
1639 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001640 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001641 main.log.error( self.name + ": EOF exception found" )
1642 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001643 main.cleanup()
1644 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001645 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001646 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001647 main.cleanup()
1648 main.exit()
1649
Jon Hall30b82fa2015-03-04 17:15:43 -08001650 def FlowAddedCount( self, deviceId ):
1651 """
1652 Determine the number of flow rules for the given device id that are
1653 in the added state
1654 """
1655 try:
1656 cmdStr = "flows any " + str( deviceId ) + " | " +\
1657 "grep 'state=ADDED' | wc -l"
1658 handle = self.sendline( cmdStr )
1659 return handle
1660 except pexpect.EOF:
1661 main.log.error( self.name + ": EOF exception found" )
1662 main.log.error( self.name + ": " + self.handle.before )
1663 main.cleanup()
1664 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001665 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -08001666 main.log.exception( self.name + ": Uncaught exception!" )
1667 main.cleanup()
1668 main.exit()
1669
kelvin-onlabd3b64892015-01-20 13:26:24 -08001670 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001671 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001672 Use 'devices' function to obtain list of all devices
1673 and parse the result to obtain a list of all device
1674 id's. Returns this list. Returns empty list if no
1675 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001676 List is ordered sequentially
1677
andrewonlab3e15ead2014-10-15 14:21:34 -04001678 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001679 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001680 the ids. By obtaining the list of device ids on the fly,
1681 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001682 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001683 try:
kelvin8ec71442015-01-15 16:57:00 -08001684 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001685 devicesStr = self.devices( jsonFormat=False )
1686 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001687
kelvin-onlabd3b64892015-01-20 13:26:24 -08001688 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001689 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001690 return idList
kelvin8ec71442015-01-15 16:57:00 -08001691
1692 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001693 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001694 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001695 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001696 # Split list further into arguments before and after string
1697 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001698 # append to idList
1699 for arg in tempList:
1700 idList.append( arg.split( "id=" )[ 1 ] )
1701 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001702
Jon Halld4d4b372015-01-28 16:02:41 -08001703 except TypeError:
1704 main.log.exception( self.name + ": Object not as expected" )
1705 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04001706 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001707 main.log.error( self.name + ": EOF exception found" )
1708 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001709 main.cleanup()
1710 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001711 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001712 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001713 main.cleanup()
1714 main.exit()
1715
kelvin-onlabd3b64892015-01-20 13:26:24 -08001716 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001717 """
andrewonlab7c211572014-10-15 16:45:20 -04001718 Uses 'nodes' function to obtain list of all nodes
1719 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001720 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001721 Returns:
1722 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001723 """
andrewonlab7c211572014-10-15 16:45:20 -04001724 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001725 nodesStr = self.nodes()
1726 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001727
kelvin-onlabd3b64892015-01-20 13:26:24 -08001728 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001729 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001730 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001731
kelvin-onlabd3b64892015-01-20 13:26:24 -08001732 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001733 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001734
kelvin8ec71442015-01-15 16:57:00 -08001735 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001736 nodesList = nodesStr.split( "," )
1737 tempList = [ node for node in nodesList if "id=" in node ]
1738 for arg in tempList:
1739 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001740
kelvin-onlabd3b64892015-01-20 13:26:24 -08001741 return idList
kelvin8ec71442015-01-15 16:57:00 -08001742
Jon Halld4d4b372015-01-28 16:02:41 -08001743 except TypeError:
1744 main.log.exception( self.name + ": Object not as expected" )
1745 return None
andrewonlab7c211572014-10-15 16:45:20 -04001746 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001747 main.log.error( self.name + ": EOF exception found" )
1748 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001749 main.cleanup()
1750 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001751 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001752 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001753 main.cleanup()
1754 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001755
kelvin-onlabd3b64892015-01-20 13:26:24 -08001756 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001757 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001758 Return the first device from the devices api whose 'id' contains 'dpid'
1759 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001760 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001761 try:
kelvin8ec71442015-01-15 16:57:00 -08001762 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001763 return None
1764 else:
kelvin8ec71442015-01-15 16:57:00 -08001765 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001766 rawDevices = self.devices()
1767 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001768 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001769 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001770 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1771 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001772 return device
1773 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001774 except TypeError:
1775 main.log.exception( self.name + ": Object not as expected" )
1776 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001777 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001778 main.log.error( self.name + ": EOF exception found" )
1779 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001780 main.cleanup()
1781 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001782 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001783 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001784 main.cleanup()
1785 main.exit()
1786
kelvin-onlabd3b64892015-01-20 13:26:24 -08001787 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001788 """
Jon Hallefbd9792015-03-05 16:11:36 -08001789 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001790 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001791 log level can be specified.
kelvin8ec71442015-01-15 16:57:00 -08001792
Jon Hall42db6dc2014-10-24 19:03:48 -04001793 Params: ip = ip used for the onos cli
1794 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001795 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001796 logLevel = level to log to. Currently accepts
1797 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001798
1799
kelvin-onlabd3b64892015-01-20 13:26:24 -08001800 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001801
Jon Hallefbd9792015-03-05 16:11:36 -08001802 Returns: main.TRUE if the number of switches and links are correct,
1803 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04001804 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001805 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001806 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001807 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001808 if topology == {}:
1809 return main.ERROR
1810 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001811 # Is the number of switches is what we expected
1812 devices = topology.get( 'devices', False )
1813 links = topology.get( 'links', False )
kelvin-onlabfb521662015-02-27 09:52:40 -08001814 if devices is False or links is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04001815 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001816 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001817 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001818 linkCheck = ( int( links ) == int( numolink ) )
1819 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001820 # We expected the correct numbers
Jon Hallefbd9792015-03-05 16:11:36 -08001821 output += "The number of links and switches match " +\
1822 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001823 result = main.TRUE
1824 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001825 output += "The number of links and switches does not match " +\
1826 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001827 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001828 output = output + "\n ONOS sees %i devices (%i expected) \
1829 and %i links (%i expected)" % (
1830 int( devices ), int( numoswitch ), int( links ),
1831 int( numolink ) )
1832 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001833 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001834 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001835 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001836 else:
kelvin8ec71442015-01-15 16:57:00 -08001837 main.log.info( output )
1838 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001839 except TypeError:
1840 main.log.exception( self.name + ": Object not as expected" )
1841 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001842 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001843 main.log.error( self.name + ": EOF exception found" )
1844 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001845 main.cleanup()
1846 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001847 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001848 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001849 main.cleanup()
1850 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001851
kelvin-onlabd3b64892015-01-20 13:26:24 -08001852 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001853 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001854 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001855 deviceId must be the id of a device as seen in the onos devices command
1856 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001857 role must be either master, standby, or none
1858
Jon Halle3f39ff2015-01-13 11:50:53 -08001859 Returns:
1860 main.TRUE or main.FALSE based on argument verification and
1861 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001862 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001863 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001864 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001865 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001866 cmdStr = "device-role " +\
1867 str( deviceId ) + " " +\
1868 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001869 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001870 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001871 if re.search( "Error", handle ):
1872 # end color output to escape any colours
1873 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001874 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001875 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001876 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001877 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001878 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001879 main.log.error( "Invalid 'role' given to device_role(). " +
1880 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001881 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001882 except TypeError:
1883 main.log.exception( self.name + ": Object not as expected" )
1884 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001885 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001886 main.log.error( self.name + ": EOF exception found" )
1887 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001888 main.cleanup()
1889 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001890 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001891 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001892 main.cleanup()
1893 main.exit()
1894
kelvin-onlabd3b64892015-01-20 13:26:24 -08001895 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001896 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001897 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001898 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001899 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001900 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001901 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001902 if jsonFormat:
1903 cmdStr = "clusters -j"
1904 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001905 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001906 handle variable here contains some ANSI escape color code
1907 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001908 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001909 function. The repr( handle ) output when printed shows the ANSI
1910 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001911 variable is actually repr( somestring ) and json.loads would
1912 fail with the escape sequence. So we take off that escape
1913 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001914
kelvin-onlabd3b64892015-01-20 13:26:24 -08001915 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1916 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001917 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001918 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1919 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001920 return handle1
1921 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001922 cmdStr = "clusters"
1923 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001924 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001925 except TypeError:
1926 main.log.exception( self.name + ": Object not as expected" )
1927 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001928 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001929 main.log.error( self.name + ": EOF exception found" )
1930 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001931 main.cleanup()
1932 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001933 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001934 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001935 main.cleanup()
1936 main.exit()
1937
kelvin-onlabd3b64892015-01-20 13:26:24 -08001938 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001939 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001940 CLI command to get the current leader for the Election test application
1941 NOTE: Requires installation of the onos-app-election feature
1942 Returns: Node IP of the leader if one exists
1943 None if none exists
1944 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001945 """
Jon Hall94fd0472014-12-08 11:52:42 -08001946 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001947 cmdStr = "election-test-leader"
1948 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001949 # Leader
1950 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001951 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001952 nodeSearch = re.search( leaderPattern, response )
1953 if nodeSearch:
1954 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001955 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001956 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001957 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001958 # no leader
1959 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001960 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001961 nullSearch = re.search( nullPattern, response )
1962 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001963 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001964 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001965 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001966 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001967 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001968 if re.search( errorPattern, response ):
1969 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001970 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001971 return main.FALSE
1972 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001973 main.log.error( "Error in election_test_leader: " +
1974 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001975 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001976 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001977 except TypeError:
1978 main.log.exception( self.name + ": Object not as expected" )
1979 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001980 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001981 main.log.error( self.name + ": EOF exception found" )
1982 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001983 main.cleanup()
1984 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001985 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001986 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001987 main.cleanup()
1988 main.exit()
1989
kelvin-onlabd3b64892015-01-20 13:26:24 -08001990 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001991 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001992 CLI command to run for leadership of the Election test application.
1993 NOTE: Requires installation of the onos-app-election feature
1994 Returns: Main.TRUE on success
1995 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001996 """
Jon Hall94fd0472014-12-08 11:52:42 -08001997 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001998 cmdStr = "election-test-run"
1999 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002000 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002001 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002002 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002003 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08002004 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08002005 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002006 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002007 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002008 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002009 errorPattern = "Command\snot\sfound"
2010 if re.search( errorPattern, response ):
2011 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002012 return main.FALSE
2013 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002014 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002015 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002016 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002017 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002018 except TypeError:
2019 main.log.exception( self.name + ": Object not as expected" )
2020 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002021 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002022 main.log.error( self.name + ": EOF exception found" )
2023 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002024 main.cleanup()
2025 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002026 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002027 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002028 main.cleanup()
2029 main.exit()
2030
kelvin-onlabd3b64892015-01-20 13:26:24 -08002031 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08002032 """
Jon Hall94fd0472014-12-08 11:52:42 -08002033 * CLI command to withdraw the local node from leadership election for
2034 * the Election test application.
2035 #NOTE: Requires installation of the onos-app-election feature
2036 Returns: Main.TRUE on success
2037 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08002038 """
Jon Hall94fd0472014-12-08 11:52:42 -08002039 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002040 cmdStr = "election-test-withdraw"
2041 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002042 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002043 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002044 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002045 if re.search( successPattern, response ):
2046 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002047 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002048 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002049 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002050 errorPattern = "Command\snot\sfound"
2051 if re.search( errorPattern, response ):
2052 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002053 return main.FALSE
2054 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002055 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002056 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002057 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002058 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002059 except TypeError:
2060 main.log.exception( self.name + ": Object not as expected" )
2061 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002062 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002063 main.log.error( self.name + ": EOF exception found" )
2064 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002065 main.cleanup()
2066 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002067 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002068 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002069 main.cleanup()
2070 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002071
kelvin8ec71442015-01-15 16:57:00 -08002072 def getDevicePortsEnabledCount( self, dpid ):
2073 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002074 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002075 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002076 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08002077 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002078 cmdStr = "onos:ports -e " + dpid + " | wc -l"
2079 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002080 if re.search( "No such device", output ):
2081 main.log.error( "Error in getting ports" )
2082 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002083 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002084 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002085 except TypeError:
2086 main.log.exception( self.name + ": Object not as expected" )
2087 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002088 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002089 main.log.error( self.name + ": EOF exception found" )
2090 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002091 main.cleanup()
2092 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002093 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002094 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002095 main.cleanup()
2096 main.exit()
2097
kelvin8ec71442015-01-15 16:57:00 -08002098 def getDeviceLinksActiveCount( self, dpid ):
2099 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002100 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002101 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002102 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002103 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002104 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
2105 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002106 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002107 main.log.error( "Error in getting ports " )
2108 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002109 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002110 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002111 except TypeError:
2112 main.log.exception( self.name + ": Object not as expected" )
2113 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002114 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002115 main.log.error( self.name + ": EOF exception found" )
2116 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002117 main.cleanup()
2118 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002119 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002120 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002121 main.cleanup()
2122 main.exit()
2123
kelvin8ec71442015-01-15 16:57:00 -08002124 def getAllIntentIds( self ):
2125 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002126 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002127 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002128 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002129 cmdStr = "onos:intents | grep id="
2130 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002131 if re.search( "Error", output ):
2132 main.log.error( "Error in getting ports" )
2133 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002134 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002135 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002136 except TypeError:
2137 main.log.exception( self.name + ": Object not as expected" )
2138 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002139 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002140 main.log.error( self.name + ": EOF exception found" )
2141 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002142 main.cleanup()
2143 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002144 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002145 main.log.exception( self.name + ": Uncaught exception!" )
2146 main.cleanup()
2147 main.exit()
2148
Jon Hall73509952015-02-24 16:42:56 -08002149 def intentSummary( self ):
2150 """
Jon Hallefbd9792015-03-05 16:11:36 -08002151 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08002152 """
2153 try:
2154 intents = self.intents( )
2155 intentStates = []
Jon Hall63604932015-02-26 17:09:50 -08002156 for intent in json.loads( intents ): # Iter through intents of a node
Jon Hall73509952015-02-24 16:42:56 -08002157 intentStates.append( intent.get( 'state', None ) )
Jon Hall63604932015-02-26 17:09:50 -08002158 out = [ (i, intentStates.count( i ) ) for i in set( intentStates ) ]
2159 main.log.info( dict( out ) )
Jon Hall73509952015-02-24 16:42:56 -08002160 return dict( out )
2161 except TypeError:
2162 main.log.exception( self.name + ": Object not as expected" )
2163 return None
2164 except pexpect.EOF:
2165 main.log.error( self.name + ": EOF exception found" )
2166 main.log.error( self.name + ": " + self.handle.before )
2167 main.cleanup()
2168 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002169 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08002170 main.log.exception( self.name + ": Uncaught exception!" )
2171 main.cleanup()
2172 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002173
2174 def leaders( self ):
2175 """
2176 Returns the output of the leaders command.
2177 """
2178 # FIXME: add json output
2179 try:
2180 output = self.sendline( "onos:leaders" )
2181 main.log.warn( output )
2182 return output
2183 except TypeError:
2184 main.log.exception( self.name + ": Object not as expected" )
2185 return None
2186 except pexpect.EOF:
2187 main.log.error( self.name + ": EOF exception found" )
2188 main.log.error( self.name + ": " + self.handle.before )
2189 main.cleanup()
2190 main.exit()
2191 except:
2192 main.log.exception( self.name + ": Uncaught exception!" )
2193 main.cleanup()
2194 main.exit()
2195
2196 def pendingMap( self ):
2197 """
2198 Returns the output of the intent Pending map.
2199 """
2200 # FIXME: add json output
2201 try:
2202 output = self.sendline( "onos:intents -p" )
2203 main.log.warn( output )
2204 return output
2205 except TypeError:
2206 main.log.exception( self.name + ": Object not as expected" )
2207 return None
2208 except pexpect.EOF:
2209 main.log.error( self.name + ": EOF exception found" )
2210 main.log.error( self.name + ": " + self.handle.before )
2211 main.cleanup()
2212 main.exit()
2213 except:
2214 main.log.exception( self.name + ": Uncaught exception!" )
2215 main.cleanup()
2216 main.exit()
2217
2218 def partitions( self ):
2219 """
2220 Returns the output of the raft partitions command for ONOS.
2221 """
2222 # FIXME: add json output
2223 try:
2224 output = self.sendline( "partitions" )
2225 main.log.warn( output )
2226 return output
2227 except TypeError:
2228 main.log.exception( self.name + ": Object not as expected" )
2229 return None
2230 except pexpect.EOF:
2231 main.log.error( self.name + ": EOF exception found" )
2232 main.log.error( self.name + ": " + self.handle.before )
2233 main.cleanup()
2234 main.exit()
2235 except:
2236 main.log.exception( self.name + ": Uncaught exception!" )
2237 main.cleanup()
2238 main.exit()
2239