blob: baed7191869c5e5e44e5c6e30f419301f397dc11 [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
kelvin8ec71442015-01-15 16:57:00 -08003"""
andrewonlab95ce8322014-10-13 14:12:04 -04004This driver enters the onos> prompt to issue commands.
5
kelvin8ec71442015-01-15 16:57:00 -08006Please follow the coding style demonstrated by existing
andrewonlab95ce8322014-10-13 14:12:04 -04007functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040014shreya@onlab.us
andrewonlab95ce8322014-10-13 14:12:04 -040015
16OCT 13 2014
17
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab95ce8322014-10-13 14:12:04 -040019import sys
andrewonlab95ce8322014-10-13 14:12:04 -040020import pexpect
21import re
Jon Hall30b82fa2015-03-04 17:15:43 -080022import json
23import types
kelvin8ec71442015-01-15 16:57:00 -080024sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040025from drivers.common.clidriver import CLI
26
andrewonlab95ce8322014-10-13 14:12:04 -040027
kelvin8ec71442015-01-15 16:57:00 -080028class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040029
kelvin8ec71442015-01-15 16:57:00 -080030 def __init__( self ):
31 """
32 Initialize client
33 """
Jon Hallefbd9792015-03-05 16:11:36 -080034 self.name = None
35 self.home = None
36 self.handle = None
kelvin8ec71442015-01-15 16:57:00 -080037 super( CLI, self ).__init__()
38
39 def connect( self, **connectargs ):
40 """
andrewonlab95ce8322014-10-13 14:12:04 -040041 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080042 """
andrewonlab95ce8322014-10-13 14:12:04 -040043 try:
44 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080045 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us658ec012015-03-11 15:13:09 -070046 self.home = "~/onos"
andrewonlab95ce8322014-10-13 14:12:04 -040047 for key in self.options:
48 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080049 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040050 break
kelvin-onlabfb521662015-02-27 09:52:40 -080051 if self.home is None or self.home == "":
Jon Halle94919c2015-03-23 11:42:57 -070052 self.home = "~/onos"
andrewonlab95ce8322014-10-13 14:12:04 -040053
kelvin8ec71442015-01-15 16:57:00 -080054 self.name = self.options[ 'name' ]
55 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080056 user_name=self.user_name,
57 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080058 port=self.port,
59 pwd=self.pwd,
60 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040061
kelvin8ec71442015-01-15 16:57:00 -080062 self.handle.sendline( "cd " + self.home )
63 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040064 if self.handle:
65 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080066 else:
67 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040068 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080069 except TypeError:
70 main.log.exception( self.name + ": Object not as expected" )
71 return None
andrewonlab95ce8322014-10-13 14:12:04 -040072 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080073 main.log.error( self.name + ": EOF exception found" )
74 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040075 main.cleanup()
76 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080077 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -080078 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -040079 main.cleanup()
80 main.exit()
81
kelvin8ec71442015-01-15 16:57:00 -080082 def disconnect( self ):
83 """
andrewonlab95ce8322014-10-13 14:12:04 -040084 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080085 """
Jon Halld61331b2015-02-17 16:35:47 -080086 response = main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -040087 try:
Jon Hall61282e32015-03-19 11:34:11 -070088 if self.handle:
89 i = self.logout()
90 if i == main.TRUE:
91 self.handle.sendline( "" )
92 self.handle.expect( "\$" )
93 self.handle.sendline( "exit" )
94 self.handle.expect( "closed" )
Jon Halld4d4b372015-01-28 16:02:41 -080095 except TypeError:
96 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -080097 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -040098 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080099 main.log.error( self.name + ": EOF exception found" )
100 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700101 except ValueError:
102 main.log.exception( "Exception in discconect of " + self.name )
103 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800104 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800105 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400106 response = main.FALSE
107 return response
108
kelvin8ec71442015-01-15 16:57:00 -0800109 def logout( self ):
110 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500111 Sends 'logout' command to ONOS cli
Jon Hall61282e32015-03-19 11:34:11 -0700112 Returns main.TRUE if exited CLI and
113 main.FALSE on timeout (not guranteed you are disconnected)
114 None on TypeError
115 Exits test on unknown error or pexpect exits unexpectedly
kelvin8ec71442015-01-15 16:57:00 -0800116 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500117 try:
Jon Hall61282e32015-03-19 11:34:11 -0700118 if self.handle:
119 self.handle.sendline( "" )
120 i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ],
121 timeout=10 )
122 if i == 0: # In ONOS CLI
123 self.handle.sendline( "logout" )
124 self.handle.expect( "\$" )
125 return main.TRUE
126 elif i == 1: # not in CLI
127 return main.TRUE
128 elif i == 3: # Timeout
129 return main.FALSE
130 else:
andrewonlab9627f432014-11-14 12:45:10 -0500131 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800132 except TypeError:
133 main.log.exception( self.name + ": Object not as expected" )
134 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500135 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800136 main.log.error( self.name + ": eof exception found" )
Jon Hall61282e32015-03-19 11:34:11 -0700137 main.log.error( self.name + ": " + self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500138 main.cleanup()
139 main.exit()
Jon Hall61282e32015-03-19 11:34:11 -0700140 except ValueError:
Jon Hall5aa168b2015-03-23 14:23:09 -0700141 main.log.error( self.name +
142 "ValueError exception in logout method" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800143 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800144 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500145 main.cleanup()
146 main.exit()
147
kelvin-onlabd3b64892015-01-20 13:26:24 -0800148 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800149 """
andrewonlab95ce8322014-10-13 14:12:04 -0400150 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800151
andrewonlab95ce8322014-10-13 14:12:04 -0400152 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800153 """
andrewonlab95ce8322014-10-13 14:12:04 -0400154 try:
155 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800156 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400157 main.cleanup()
158 main.exit()
159 else:
kelvin8ec71442015-01-15 16:57:00 -0800160 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800161 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800162 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400163 # and that this driver will have to change accordingly
Cameron Franke9c94fb02015-01-21 10:20:20 -0800164 self.handle.expect(str(cellname))
andrew@onlab.usc400b112015-01-21 15:33:19 -0800165 handleBefore = self.handle.before
166 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800167 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800168 self.handle.sendline("")
169 self.handle.expect("\$")
andrew@onlab.usc400b112015-01-21 15:33:19 -0800170 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400171
kelvin-onlabd3b64892015-01-20 13:26:24 -0800172 main.log.info( "Cell call returned: " + handleBefore +
173 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400174
175 return main.TRUE
176
Jon Halld4d4b372015-01-28 16:02:41 -0800177 except TypeError:
178 main.log.exception( self.name + ": Object not as expected" )
179 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400180 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800181 main.log.error( self.name + ": eof exception found" )
182 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400183 main.cleanup()
184 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800185 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800186 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400187 main.cleanup()
188 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800189
kelvin-onlabd3b64892015-01-20 13:26:24 -0800190 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800191 """
Jon Hallefbd9792015-03-05 16:11:36 -0800192 karafTimeout is an optional argument. karafTimeout value passed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800193 by user would be used to set the current karaf shell idle timeout.
194 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800195 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800196 Below is an example to start a session with 60 seconds idle timeout
197 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800198
Hari Krishna25d42f72015-01-05 15:08:28 -0800199 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800200 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800201
kelvin-onlabd3b64892015-01-20 13:26:24 -0800202 Note: karafTimeout is left as str so that this could be read
203 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800204 """
andrewonlab95ce8322014-10-13 14:12:04 -0400205 try:
kelvin8ec71442015-01-15 16:57:00 -0800206 self.handle.sendline( "" )
207 x = self.handle.expect( [
208 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500209
210 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800211 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500212 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400213
kelvin8ec71442015-01-15 16:57:00 -0800214 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800216 i = self.handle.expect( [
217 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800218 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400219
220 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800221 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800222 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800223 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800224 "config:property-set -p org.apache.karaf.shell\
225 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800226 karafTimeout )
227 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800228 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800229 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400230 return main.TRUE
231 else:
kelvin8ec71442015-01-15 16:57:00 -0800232 # If failed, send ctrl+c to process and try again
233 main.log.info( "Starting CLI failed. Retrying..." )
234 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800235 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800236 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
237 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400238 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800239 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800240 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800241 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800242 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800243 "config:property-set -p org.apache.karaf.shell\
244 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800245 karafTimeout )
246 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800247 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800248 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400249 return main.TRUE
250 else:
kelvin8ec71442015-01-15 16:57:00 -0800251 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800252 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400253 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400254
Jon Halld4d4b372015-01-28 16:02:41 -0800255 except TypeError:
256 main.log.exception( self.name + ": Object not as expected" )
257 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400258 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800259 main.log.error( self.name + ": EOF exception found" )
260 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400261 main.cleanup()
262 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800263 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800264 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400265 main.cleanup()
266 main.exit()
267
Jon Hallefbd9792015-03-05 16:11:36 -0800268 def log( self, cmdStr, level="" ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800269 """
270 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800271 returns main.TRUE on success
Jon Hallefbd9792015-03-05 16:11:36 -0800272 returns main.FALSE if Error occurred
kelvin-onlab338f5512015-02-06 10:53:16 -0800273 Available level: DEBUG, TRACE, INFO, WARN, ERROR
274 Level defaults to INFO
kelvin-onlab9f541032015-02-04 16:19:53 -0800275 """
276 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800277 lvlStr = ""
278 if level:
279 lvlStr = "--level=" + level
280
kelvin-onlab9f541032015-02-04 16:19:53 -0800281 self.handle.sendline( "" )
282 self.handle.expect( "onos>" )
kelvin-onlab338f5512015-02-06 10:53:16 -0800283 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
kelvin-onlab9f541032015-02-04 16:19:53 -0800284 self.handle.expect( "onos>" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800285
kelvin-onlab9f541032015-02-04 16:19:53 -0800286 response = self.handle.before
287 if re.search( "Error", response ):
288 return main.FALSE
289 return main.TRUE
290
291 except pexpect.EOF:
292 main.log.error( self.name + ": EOF exception found" )
293 main.log.error( self.name + ": " + self.handle.before )
294 main.cleanup()
295 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800296 except Exception:
kelvin-onlabfb521662015-02-27 09:52:40 -0800297 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400298 main.cleanup()
299 main.exit()
300
kelvin-onlabd3b64892015-01-20 13:26:24 -0800301 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800302 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800303 Send a completely user specified string to
304 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400305 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800306
andrewonlaba18f6bf2014-10-13 19:31:54 -0400307 Warning: There are no sanity checking to commands
308 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800309 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400310 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800311 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
312 self.log( logStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800313 self.handle.sendline( cmdStr )
Jon Hall63604932015-02-26 17:09:50 -0800314 i = self.handle.expect( ["onos>", "\$", pexpect.TIMEOUT] )
315 response = self.handle.before
316 if i == 2:
317 self.handle.sendline()
318 self.handle.expect( "\$" )
319 response += self.handle.before
320 print response
321 try:
322 print self.handle.after
323 except:
324 pass
325 # TODO: do something with i
kelvin-onlabd3b64892015-01-20 13:26:24 -0800326 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
kelvin-onlab898a6c62015-01-16 14:13:53 -0800327 + self.name + "." )
Jon Hall7bdfc122015-01-23 11:45:32 -0800328 # Remove control strings from output
kelvin-onlabd3b64892015-01-20 13:26:24 -0800329 ansiEscape = re.compile( r'\x1b[^m]*m' )
Jon Hall63604932015-02-26 17:09:50 -0800330 response = ansiEscape.sub( '', response )
kelvin-onlabfb521662015-02-27 09:52:40 -0800331 # Remove extra return chars that get added
Jon Hall63604932015-02-26 17:09:50 -0800332 response = re.sub( r"\s\r", "", response )
333 response = response.strip()
334 # parse for just the output, remove the cmd from response
335 output = response.split( cmdStr, 1 )[1]
Jon Hall7bdfc122015-01-23 11:45:32 -0800336 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800337 except TypeError:
338 main.log.exception( self.name + ": Object not as expected" )
339 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400340 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800341 main.log.error( self.name + ": EOF exception found" )
342 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400343 main.cleanup()
344 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800345 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800346 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400347 main.cleanup()
348 main.exit()
349
kelvin8ec71442015-01-15 16:57:00 -0800350 # IMPORTANT NOTE:
351 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800352 # the cli command changing 'a:b' with 'aB'.
353 # Ex ) onos:topology > onosTopology
354 # onos:links > onosLinks
355 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800356
kelvin-onlabd3b64892015-01-20 13:26:24 -0800357 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800358 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400359 Adds a new cluster node by ID and address information.
360 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800361 * nodeId
362 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400363 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800364 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800365 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400366 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800367 cmdStr = "add-node " + str( nodeId ) + " " +\
368 str( ONOSIp ) + " " + str( tcpPort )
369 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800370 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800371 main.log.error( "Error in adding node" )
372 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800373 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400374 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800375 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400376 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800377 except TypeError:
378 main.log.exception( self.name + ": Object not as expected" )
379 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400380 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800381 main.log.error( self.name + ": EOF exception found" )
382 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400383 main.cleanup()
384 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800385 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800386 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400387 main.cleanup()
388 main.exit()
389
kelvin-onlabd3b64892015-01-20 13:26:24 -0800390 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800391 """
andrewonlab86dc3082014-10-13 18:18:38 -0400392 Removes a cluster by ID
393 Issues command: 'remove-node [<node-id>]'
394 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800395 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800396 """
andrewonlab86dc3082014-10-13 18:18:38 -0400397 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400398
kelvin-onlabd3b64892015-01-20 13:26:24 -0800399 cmdStr = "remove-node " + str( nodeId )
400 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800401 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400402
403 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800404
Jon Halld4d4b372015-01-28 16:02:41 -0800405 except TypeError:
406 main.log.exception( self.name + ": Object not as expected" )
407 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400408 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800409 main.log.error( self.name + ": EOF exception found" )
410 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400411 main.cleanup()
412 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800413 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800414 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400415 main.cleanup()
416 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400417
Jon Hall61282e32015-03-19 11:34:11 -0700418 def nodes( self, jsonFormat=True):
kelvin8ec71442015-01-15 16:57:00 -0800419 """
andrewonlab7c211572014-10-15 16:45:20 -0400420 List the nodes currently visible
421 Issues command: 'nodes'
Jon Hall61282e32015-03-19 11:34:11 -0700422 Optional argument:
423 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800424 """
andrewonlab7c211572014-10-15 16:45:20 -0400425 try:
Jon Hall61282e32015-03-19 11:34:11 -0700426 if jsonFormat:
427 cmdStr = "nodes -j"
428 output = self.sendline( cmdStr )
429 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
430 parsedOutput = ansiEscape.sub( '', output )
431 return parsedOutput
432 else:
433 cmdStr = "nodes"
434 output = self.sendline( cmdStr )
435 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800436 except TypeError:
437 main.log.exception( self.name + ": Object not as expected" )
438 return None
andrewonlab7c211572014-10-15 16:45:20 -0400439 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800440 main.log.error( self.name + ": EOF exception found" )
441 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400442 main.cleanup()
443 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800444 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800445 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400446 main.cleanup()
447 main.exit()
448
kelvin8ec71442015-01-15 16:57:00 -0800449 def topology( self ):
450 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700451 Definition:
452 Returns the ouput of topology command.
453 Return:
454 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -0800455 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700456 import json
andrewonlab95ce8322014-10-13 14:12:04 -0400457 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800458 # either onos:topology or 'topology' will work in CLI
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700459 cmdStr = "topology -j"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800460 handle = self.sendline( cmdStr )
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700461 main.log.info( "topology -j returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400462 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800463 except TypeError:
464 main.log.exception( self.name + ": Object not as expected" )
465 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400466 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800467 main.log.error( self.name + ": EOF exception found" )
468 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400469 main.cleanup()
470 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800471 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800472 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400473 main.cleanup()
474 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800475
kelvin-onlabd3b64892015-01-20 13:26:24 -0800476 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800477 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800478 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400479 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800480 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400481 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800482 cmdStr = "feature:install " + str( featureStr )
483 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800484 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400485 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800486 except TypeError:
487 main.log.exception( self.name + ": Object not as expected" )
488 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400489 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800490 main.log.error( self.name + ": EOF exception found" )
491 main.log.error( self.name + ": " + self.handle.before )
492 main.log.report( "Failed to install feature" )
493 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400494 main.cleanup()
495 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800496 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800497 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800498 main.log.report( "Failed to install feature" )
499 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400500 main.cleanup()
501 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800502
kelvin-onlabd3b64892015-01-20 13:26:24 -0800503 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800504 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400505 Uninstalls a specified feature
506 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800507 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400508 try:
Jon Hall30b82fa2015-03-04 17:15:43 -0800509 cmdStr = 'feature:list -i | grep "' + featureStr + '"'
510 handle = self.sendline( cmdStr )
511 if handle != '':
512 cmdStr = "feature:uninstall " + str( featureStr )
513 self.sendline( cmdStr )
514 # TODO: Check for possible error responses from karaf
515 else:
Jon Hallefbd9792015-03-05 16:11:36 -0800516 main.log.info( "Feature needs to be installed before " +
517 "uninstalling it" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400518 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800519 except TypeError:
520 main.log.exception( self.name + ": Object not as expected" )
521 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400522 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800523 main.log.error( self.name + ": EOF exception found" )
524 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400525 main.cleanup()
526 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800527 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800528 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400529 main.cleanup()
530 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800531
jenkins7ead5a82015-03-13 10:28:21 -0700532 def deviceRemove( self, deviceId ):
533 """
534 Removes particular device from storage
535
536 TODO: refactor this function
537 """
538 try:
539 cmdStr = "device-remove "+str(deviceId)
540 handle = self.sendline( cmdStr )
541 return main.TRUE
542 except TypeError:
543 main.log.exception( self.name + ": Object not as expected" )
544 return None
545 except pexpect.EOF:
546 main.log.error( self.name + ": EOF exception found" )
547 main.log.error( self.name + ": " + self.handle.before )
548 main.cleanup()
549 main.exit()
550 except Exception:
551 main.log.exception( self.name + ": Uncaught exception!" )
552 main.cleanup()
553 main.exit()
554
555
556
kelvin-onlabd3b64892015-01-20 13:26:24 -0800557 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800558 """
Jon Hall7b02d952014-10-17 20:14:54 -0400559 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400560 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800561 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800562 """
andrewonlab86dc3082014-10-13 18:18:38 -0400563 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800564 if jsonFormat:
565 cmdStr = "devices -j"
566 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800567 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800568 handle variable here contains some ANSI escape color code
569 sequences at the end which are invisible in the print command
570 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800571 function. The repr( handle ) output when printed shows the
572 ANSI escape sequences. In json.loads( somestring ), this
573 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800574 json.loads would fail with the escape sequence. So we take off
575 that escape sequence using:
576
kelvin-onlabd3b64892015-01-20 13:26:24 -0800577 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
578 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800579 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800580 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
581 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400582 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400583 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800584 cmdStr = "devices"
585 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400586 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800587 except TypeError:
588 main.log.exception( self.name + ": Object not as expected" )
589 return None
andrewonlab7c211572014-10-15 16:45:20 -0400590 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800591 main.log.error( self.name + ": EOF exception found" )
592 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400593 main.cleanup()
594 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800595 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800596 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400597 main.cleanup()
598 main.exit()
599
kelvin-onlabd3b64892015-01-20 13:26:24 -0800600 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800601 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800602 This balances the devices across all controllers
603 by issuing command: 'onos> onos:balance-masters'
604 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800605 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800606 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800607 cmdStr = "onos:balance-masters"
608 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800609 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800610 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800611 except TypeError:
612 main.log.exception( self.name + ": Object not as expected" )
613 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800614 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800615 main.log.error( self.name + ": EOF exception found" )
616 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800617 main.cleanup()
618 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800619 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800620 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800621 main.cleanup()
622 main.exit()
623
kelvin-onlabd3b64892015-01-20 13:26:24 -0800624 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800625 """
Jon Halle8217482014-10-17 13:49:14 -0400626 Lists all core links
627 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800628 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800629 """
Jon Halle8217482014-10-17 13:49:14 -0400630 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800631 if jsonFormat:
632 cmdStr = "links -j"
633 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800634 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800635 handle variable here contains some ANSI escape color code
636 sequences at the end which are invisible in the print command
637 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800638 function. The repr( handle ) output when printed shows the ANSI
639 escape sequences. In json.loads( somestring ), this somestring
640 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800641 fail with the escape sequence. So we take off that escape
642 sequence using:
643
kelvin-onlabd3b64892015-01-20 13:26:24 -0800644 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
645 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800646 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800647 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
648 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400649 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400650 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800651 cmdStr = "links"
652 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400653 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800654 except TypeError:
655 main.log.exception( self.name + ": Object not as expected" )
656 return None
Jon Halle8217482014-10-17 13:49:14 -0400657 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800658 main.log.error( self.name + ": EOF exception found" )
659 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400660 main.cleanup()
661 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800662 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800663 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400664 main.cleanup()
665 main.exit()
666
kelvin-onlabd3b64892015-01-20 13:26:24 -0800667 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800668 """
Jon Halle8217482014-10-17 13:49:14 -0400669 Lists all ports
670 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800671 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800672 """
Jon Halle8217482014-10-17 13:49:14 -0400673 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800674 if jsonFormat:
675 cmdStr = "ports -j"
676 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800677 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800678 handle variable here contains some ANSI escape color code
679 sequences at the end which are invisible in the print command
680 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800681 function. The repr( handle ) output when printed shows the ANSI
682 escape sequences. In json.loads( somestring ), this somestring
683 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800684 fail with the escape sequence. So we take off that escape
Jon Hallefbd9792015-03-05 16:11:36 -0800685 sequence using the following commands:
Jon Halle3f39ff2015-01-13 11:50: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 )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800689 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800690 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
691 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400692 return handle1
693
Jon Halle8217482014-10-17 13:49:14 -0400694 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800695 cmdStr = "ports"
696 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800697 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800698 except TypeError:
699 main.log.exception( self.name + ": Object not as expected" )
700 return None
Jon Halle8217482014-10-17 13:49:14 -0400701 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800702 main.log.error( self.name + ": EOF exception found" )
703 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400704 main.cleanup()
705 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800706 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800707 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400708 main.cleanup()
709 main.exit()
710
kelvin-onlabd3b64892015-01-20 13:26:24 -0800711 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800712 """
Jon Hall983a1702014-10-28 18:44:22 -0400713 Lists all devices and the controllers with roles assigned to them
714 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800715 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800716 """
andrewonlab7c211572014-10-15 16:45:20 -0400717 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800718 if jsonFormat:
719 cmdStr = "roles -j"
720 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800721 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800722 handle variable here contains some ANSI escape color code
723 sequences at the end which are invisible in the print command
724 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800725 function. The repr( handle ) output when printed shows the ANSI
726 escape sequences. In json.loads( somestring ), this somestring
727 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800728 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500729
Jon Halle3f39ff2015-01-13 11:50:53 -0800730 So we take off that escape sequence using the following
731 commads:
732
kelvin-onlabd3b64892015-01-20 13:26:24 -0800733 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
734 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800735 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800736 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
737 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400738 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400739
andrewonlab7c211572014-10-15 16:45:20 -0400740 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800741 cmdStr = "roles"
742 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800743 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800744 except TypeError:
745 main.log.exception( self.name + ": Object not as expected" )
746 return None
Jon Hall983a1702014-10-28 18:44:22 -0400747 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800748 main.log.error( self.name + ": EOF exception found" )
749 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400750 main.cleanup()
751 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800752 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800753 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400754 main.cleanup()
755 main.exit()
756
kelvin-onlabd3b64892015-01-20 13:26:24 -0800757 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800758 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800759 Given the a string containing the json representation of the "roles"
760 cli command and a partial or whole device id, returns a json object
761 containing the roles output for the first device whose id contains
762 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400763
764 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800765 A dict of the role assignments for the given device or
766 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800767 """
Jon Hall983a1702014-10-28 18:44:22 -0400768 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800769 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400770 return None
771 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800772 rawRoles = self.roles()
773 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800774 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800775 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800776 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800777 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400778 return device
779 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800780 except TypeError:
781 main.log.exception( self.name + ": Object not as expected" )
782 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400783 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800784 main.log.error( self.name + ": EOF exception found" )
785 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400786 main.cleanup()
787 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800788 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800789 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400790 main.cleanup()
791 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800792
kelvin-onlabd3b64892015-01-20 13:26:24 -0800793 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800794 """
Jon Hall94fd0472014-12-08 11:52:42 -0800795 Iterates through each device and checks if there is a master assigned
796 Returns: main.TRUE if each device has a master
797 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800798 """
Jon Hall94fd0472014-12-08 11:52:42 -0800799 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800800 rawRoles = self.roles()
801 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800802 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800803 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800804 # print device
805 if device[ 'master' ] == "none":
806 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800807 return main.FALSE
808 return main.TRUE
809
Jon Halld4d4b372015-01-28 16:02:41 -0800810 except TypeError:
811 main.log.exception( self.name + ": Object not as expected" )
812 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800813 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800814 main.log.error( self.name + ": EOF exception found" )
815 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800816 main.cleanup()
817 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800818 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800819 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800820 main.cleanup()
821 main.exit()
822
kelvin-onlabd3b64892015-01-20 13:26:24 -0800823 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800824 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400825 Returns string of paths, and the cost.
826 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800827 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400828 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800829 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
830 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800831 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800832 main.log.error( "Error in getting paths" )
833 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400834 else:
kelvin8ec71442015-01-15 16:57:00 -0800835 path = handle.split( ";" )[ 0 ]
836 cost = handle.split( ";" )[ 1 ]
837 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800838 except TypeError:
839 main.log.exception( self.name + ": Object not as expected" )
840 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400841 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800842 main.log.error( self.name + ": EOF exception found" )
843 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400844 main.cleanup()
845 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800846 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800847 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400848 main.cleanup()
849 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800850
kelvin-onlabd3b64892015-01-20 13:26:24 -0800851 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800852 """
Jon Hallffb386d2014-11-21 13:43:38 -0800853 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400854 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800855 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800856 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400857 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800858 if jsonFormat:
859 cmdStr = "hosts -j"
860 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800861 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800862 handle variable here contains some ANSI escape color code
863 sequences at the end which are invisible in the print command
864 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800865 function. The repr( handle ) output when printed shows the ANSI
866 escape sequences. In json.loads( somestring ), this somestring
867 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800868 fail with the escape sequence. So we take off that escape
869 sequence using:
870
kelvin-onlabd3b64892015-01-20 13:26:24 -0800871 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
872 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800873 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800874 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
875 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400876 return handle1
877 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800878 cmdStr = "hosts"
879 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400880 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800881 except TypeError:
882 main.log.exception( self.name + ": Object not as expected" )
883 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400884 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800885 main.log.error( self.name + ": EOF exception found" )
886 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400887 main.cleanup()
888 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800889 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800890 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400891 main.cleanup()
892 main.exit()
893
kelvin-onlabd3b64892015-01-20 13:26:24 -0800894 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800895 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400896 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800897
Jon Hallefbd9792015-03-05 16:11:36 -0800898 Note: mac must be a colon separated mac address, but could be a
Jon Halle3f39ff2015-01-13 11:50:53 -0800899 partial mac address
900
Jon Hall42db6dc2014-10-24 19:03:48 -0400901 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800902 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400903 try:
kelvin8ec71442015-01-15 16:57:00 -0800904 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400905 return None
906 else:
907 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800908 rawHosts = self.hosts()
909 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800910 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800911 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800912 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800913 if not host:
914 pass
915 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400916 return host
917 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800918 except TypeError:
919 main.log.exception( self.name + ": Object not as expected" )
920 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400921 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800922 main.log.error( self.name + ": EOF exception found" )
923 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400924 main.cleanup()
925 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800926 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800927 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400928 main.cleanup()
929 main.exit()
930
kelvin-onlabd3b64892015-01-20 13:26:24 -0800931 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800932 """
933 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400934 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800935
andrewonlab3f0a4af2014-10-17 12:25:14 -0400936 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800937 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400938 IMPORTANT:
939 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800940 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400941 Furthermore, it assumes that value of VLAN is '-1'
942 Description:
kelvin8ec71442015-01-15 16:57:00 -0800943 Converts mininet hosts ( h1, h2, h3... ) into
944 ONOS format ( 00:00:00:00:00:01/-1 , ... )
945 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400946 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800947 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400948
kelvin-onlabd3b64892015-01-20 13:26:24 -0800949 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800950 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800951 hostHex = hex( int( host ) ).zfill( 12 )
952 hostHex = str( hostHex ).replace( 'x', '0' )
953 i = iter( str( hostHex ) )
954 hostHex = ":".join( a + b for a, b in zip( i, i ) )
955 hostHex = hostHex + "/-1"
956 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400957
kelvin-onlabd3b64892015-01-20 13:26:24 -0800958 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400959
Jon Halld4d4b372015-01-28 16:02:41 -0800960 except TypeError:
961 main.log.exception( self.name + ": Object not as expected" )
962 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400963 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800964 main.log.error( self.name + ": EOF exception found" )
965 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400966 main.cleanup()
967 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800968 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800969 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400970 main.cleanup()
971 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400972
kelvin-onlabd3b64892015-01-20 13:26:24 -0800973 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800974 """
andrewonlabe6745342014-10-17 14:29:13 -0400975 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800976 * hostIdOne: ONOS host id for host1
977 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400978 Description:
Jon Hallefbd9792015-03-05 16:11:36 -0800979 Adds a host-to-host intent ( bidirectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500980 specifying the two hosts.
kelvin-onlabfb521662015-02-27 09:52:40 -0800981 Returns:
982 A string of the intent id or None on Error
kelvin8ec71442015-01-15 16:57:00 -0800983 """
andrewonlabe6745342014-10-17 14:29:13 -0400984 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800985 cmdStr = "add-host-intent " + str( hostIdOne ) +\
986 " " + str( hostIdTwo )
987 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800988 if re.search( "Error", handle ):
989 main.log.error( "Error in adding Host intent" )
Jon Hall61282e32015-03-19 11:34:11 -0700990 main.log.debug( "Response from ONOS was: " + repr( handle ) )
kelvin-onlabfb521662015-02-27 09:52:40 -0800991 return None
Hari Krishnaac4e1782015-01-26 12:09:12 -0800992 else:
993 main.log.info( "Host intent installed between " +
kelvin-onlabfb521662015-02-27 09:52:40 -0800994 str( hostIdOne ) + " and " + str( hostIdTwo ) )
995 match = re.search('id=0x([\da-f]+),', handle)
996 if match:
997 return match.group()[3:-1]
998 else:
999 main.log.error( "Error, intent ID not found" )
Jon Hall61282e32015-03-19 11:34:11 -07001000 main.log.debug( "Response from ONOS was: " +
1001 repr( handle ) )
kelvin-onlabfb521662015-02-27 09:52:40 -08001002 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001003 except TypeError:
1004 main.log.exception( self.name + ": Object not as expected" )
1005 return None
andrewonlabe6745342014-10-17 14:29:13 -04001006 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001007 main.log.error( self.name + ": EOF exception found" )
1008 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001009 main.cleanup()
1010 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001011 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001012 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001013 main.cleanup()
1014 main.exit()
1015
kelvin-onlabd3b64892015-01-20 13:26:24 -08001016 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -08001017 """
andrewonlab7b31d232014-10-24 13:31:47 -04001018 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001019 * ingressDevice: device id of ingress device
1020 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -04001021 Optional:
1022 TODO: Still needs to be implemented via dev side
kelvin-onlabfb521662015-02-27 09:52:40 -08001023 Description:
1024 Adds an optical intent by specifying an ingress and egress device
1025 Returns:
1026 A string of the intent id or None on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001027 """
andrewonlab7b31d232014-10-24 13:31:47 -04001028 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001029 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
1030 " " + str( egressDevice )
1031 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001032 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -08001033 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001034 main.log.error( "Error in adding Optical intent" )
1035 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001036 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001037 main.log.info( "Optical intent installed between " +
1038 str( ingressDevice ) + " and " +
1039 str( egressDevice ) )
1040 match = re.search('id=0x([\da-f]+),', handle)
1041 if match:
1042 return match.group()[3:-1]
1043 else:
1044 main.log.error( "Error, intent ID not found" )
1045 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001046 except TypeError:
1047 main.log.exception( self.name + ": Object not as expected" )
1048 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001049 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001050 main.log.error( self.name + ": EOF exception found" )
1051 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -04001052 main.cleanup()
1053 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001054 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001055 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -04001056 main.cleanup()
1057 main.exit()
1058
kelvin-onlabd3b64892015-01-20 13:26:24 -08001059 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001060 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001061 ingressDevice,
1062 egressDevice,
1063 portIngress="",
1064 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001065 ethType="",
1066 ethSrc="",
1067 ethDst="",
1068 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001069 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001070 ipProto="",
1071 ipSrc="",
1072 ipDst="",
1073 tcpSrc="",
1074 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001075 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001076 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001077 * ingressDevice: device id of ingress device
1078 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001079 Optional:
1080 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001081 * ethSrc: specify ethSrc ( i.e. src mac addr )
1082 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001083 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001084 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001085 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001086 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001087 * ipSrc: specify ip source address
1088 * ipDst: specify ip destination address
1089 * tcpSrc: specify tcp source port
1090 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001091 Description:
kelvin8ec71442015-01-15 16:57:00 -08001092 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001093 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001094 Returns:
1095 A string of the intent id or None on error
andrewonlab289e4b72014-10-21 21:24:18 -04001096
Jon Halle3f39ff2015-01-13 11:50:53 -08001097 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001098 options developers provide for point-to-point
1099 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001100 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001101 try:
kelvin8ec71442015-01-15 16:57:00 -08001102 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001103 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001104 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001105 and not ipProto and not ipSrc and not ipDst \
1106 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001107 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001108
andrewonlab289e4b72014-10-21 21:24:18 -04001109 else:
andrewonlab36af3822014-11-18 17:48:18 -05001110 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001111
andrewonlab0c0a6772014-10-22 12:31:18 -04001112 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001113 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001114 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001115 cmd += " --ethSrc " + str( ethSrc )
1116 if ethDst:
1117 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001118 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001119 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001120 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001121 cmd += " --lambda "
1122 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001123 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001124 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001125 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001126 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001127 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001128 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001129 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001130 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001131 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001132
kelvin8ec71442015-01-15 16:57:00 -08001133 # Check whether the user appended the port
1134 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001135 if "/" in ingressDevice:
1136 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001137 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001138 if not portIngress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001139 main.log.error( "You must specify the ingress port" )
kelvin8ec71442015-01-15 16:57:00 -08001140 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001141 # Would it make sense to throw an exception and exit
1142 # the test?
1143 return None
andrewonlab36af3822014-11-18 17:48:18 -05001144
kelvin8ec71442015-01-15 16:57:00 -08001145 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001146 str( ingressDevice ) + "/" +\
1147 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001148
kelvin-onlabd3b64892015-01-20 13:26:24 -08001149 if "/" in egressDevice:
1150 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001151 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001152 if not portEgress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001153 main.log.error( "You must specify the egress port" )
1154 return None
Jon Halle3f39ff2015-01-13 11:50:53 -08001155
kelvin8ec71442015-01-15 16:57:00 -08001156 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001157 str( egressDevice ) + "/" +\
1158 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001159
kelvin-onlab898a6c62015-01-16 14:13:53 -08001160 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001161 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001162 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001163 main.log.error( "Error in adding point-to-point intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001164 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001165 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001166 # TODO: print out all the options in this message?
1167 main.log.info( "Point-to-point intent installed between " +
1168 str( ingressDevice ) + " and " +
1169 str( egressDevice ) )
1170 match = re.search('id=0x([\da-f]+),', handle)
1171 if match:
1172 return match.group()[3:-1]
1173 else:
1174 main.log.error( "Error, intent ID not found" )
1175 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001176 except TypeError:
1177 main.log.exception( self.name + ": Object not as expected" )
1178 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001179 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001180 main.log.error( self.name + ": EOF exception found" )
1181 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001182 main.cleanup()
1183 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001184 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001185 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001186 main.cleanup()
1187 main.exit()
1188
kelvin-onlabd3b64892015-01-20 13:26:24 -08001189 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001190 self,
shahshreyac2f97072015-03-19 17:04:29 -07001191 ingressDeviceList,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001192 egressDevice,
shahshreyac2f97072015-03-19 17:04:29 -07001193 portIngressList=None,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001194 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001195 ethType="",
1196 ethSrc="",
1197 ethDst="",
1198 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001199 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001200 ipProto="",
1201 ipSrc="",
1202 ipDst="",
1203 tcpSrc="",
1204 tcpDst="",
1205 setEthSrc="",
1206 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001207 """
shahshreyad0c80432014-12-04 16:56:05 -08001208 Note:
shahshreya70622b12015-03-19 17:19:00 -07001209 This function assumes the format of all ingress devices
1210 is same. That is, all ingress devices include port nos
1211 with a "/" or all ingress devices could specify device
1212 ids and port nos seperately.
shahshreyad0c80432014-12-04 16:56:05 -08001213 Required:
shahshreyac2f97072015-03-19 17:04:29 -07001214 * ingressDeviceList: List of device ids of ingress device
1215 ( Atleast 2 ingress devices required in the list )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001216 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001217 Optional:
1218 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001219 * ethSrc: specify ethSrc ( i.e. src mac addr )
1220 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001221 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001222 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001223 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001224 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001225 * ipSrc: specify ip source address
1226 * ipDst: specify ip destination address
1227 * tcpSrc: specify tcp source port
1228 * tcpDst: specify tcp destination port
1229 * setEthSrc: action to Rewrite Source MAC Address
1230 * setEthDst: action to Rewrite Destination MAC Address
1231 Description:
kelvin8ec71442015-01-15 16:57:00 -08001232 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001233 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001234 Returns:
1235 A string of the intent id or None on error
shahshreyad0c80432014-12-04 16:56:05 -08001236
Jon Halle3f39ff2015-01-13 11:50:53 -08001237 NOTE: This function may change depending on the
Jon Hallefbd9792015-03-05 16:11:36 -08001238 options developers provide for multipoint-to-singlepoint
shahshreyad0c80432014-12-04 16:56:05 -08001239 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001240 """
shahshreyad0c80432014-12-04 16:56:05 -08001241 try:
kelvin8ec71442015-01-15 16:57:00 -08001242 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001243 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001244 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001245 and not ipProto and not ipSrc and not ipDst\
1246 and not tcpSrc and not tcpDst and not setEthSrc\
1247 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001248 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001249
1250 else:
1251 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001252
shahshreyad0c80432014-12-04 16:56:05 -08001253 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001254 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001255 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001256 cmd += " --ethSrc " + str( ethSrc )
1257 if ethDst:
1258 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001259 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001260 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001261 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001262 cmd += " --lambda "
1263 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001264 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001265 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001266 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001267 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001268 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001269 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001270 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001271 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001272 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001273 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001274 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001275 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001276 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001277
kelvin8ec71442015-01-15 16:57:00 -08001278 # Check whether the user appended the port
1279 # or provided it as an input
shahshreyac2f97072015-03-19 17:04:29 -07001280
1281 if portIngressList is None:
1282 for ingressDevice in ingressDeviceList:
1283 if "/" in ingressDevice:
1284 cmd += " " + str( ingressDevice )
1285 else:
1286 main.log.error( "You must specify " +
1287 "the ingress port" )
1288 # TODO: perhaps more meaningful return
1289 return main.FALSE
shahshreyad0c80432014-12-04 16:56:05 -08001290 else:
Jon Hall71ce4e72015-03-23 14:05:58 -07001291 if len( ingressDeviceList ) == len( portIngressList ):
shahshreya70622b12015-03-19 17:19:00 -07001292 for ingressDevice,portIngress in zip( ingressDeviceList,portIngressList ):
1293 cmd += " " + \
1294 str( ingressDevice ) + "/" +\
1295 str( portIngress ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001296
kelvin-onlabd3b64892015-01-20 13:26:24 -08001297 if "/" in egressDevice:
1298 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001299 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001300 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001301 main.log.error( "You must specify " +
1302 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001303 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001304
kelvin8ec71442015-01-15 16:57:00 -08001305 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001306 str( egressDevice ) + "/" +\
1307 str( portEgress )
shahshreyac2f97072015-03-19 17:04:29 -07001308
kelvin8ec71442015-01-15 16:57:00 -08001309 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001310 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001311 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001312 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001313 main.log.error( "Error in adding multipoint-to-singlepoint " +
1314 "intent" )
1315 return None
shahshreyad0c80432014-12-04 16:56:05 -08001316 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001317 # TODO: print out all the options in this message?
1318 main.log.info( "Multipoint-to-singlepoint intent installed" +
shahshreyac2f97072015-03-19 17:04:29 -07001319 " failed " )
1320 return None
1321 #match = re.search('id=0x([\da-f]+),', handle)
1322 #if match:
1323 #return match.group()[3:-1]
1324 #else:
1325 #main.log.error( "Error, intent ID not found" )
1326 #return None
Jon Halld4d4b372015-01-28 16:02:41 -08001327 except TypeError:
1328 main.log.exception( self.name + ": Object not as expected" )
1329 return None
shahshreyad0c80432014-12-04 16:56:05 -08001330 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001331 main.log.error( self.name + ": EOF exception found" )
1332 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001333 main.cleanup()
1334 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001335 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001336 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001337 main.cleanup()
1338 main.exit()
1339
Jon Hallefbd9792015-03-05 16:11:36 -08001340 def removeIntent( self, intentId, app='org.onosproject.cli',
1341 purge=False, sync=False ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001342 """
shahshreya1c818fc2015-02-26 13:44:08 -08001343 Remove intent for specified application id and intent id
Jon Hall61282e32015-03-19 11:34:11 -07001344 Optional args:-
shahshreya1c818fc2015-02-26 13:44:08 -08001345 -s or --sync: Waits for the removal before returning
Jon Hall61282e32015-03-19 11:34:11 -07001346 -p or --purge: Purge the intent from the store after removal
1347
Jon Halle3f39ff2015-01-13 11:50:53 -08001348 Returns:
1349 main.False on error and
1350 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001351 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001352 try:
shahshreya1c818fc2015-02-26 13:44:08 -08001353 cmdStr = "remove-intent "
1354 if purge:
1355 cmdStr += " -p"
1356 if sync:
1357 cmdStr += " -s"
1358
1359 cmdStr += " " + app + " " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001360 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001361 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001362 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001363 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001364 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001365 # TODO: Should this be main.TRUE
1366 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001367 except TypeError:
1368 main.log.exception( self.name + ": Object not as expected" )
1369 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001370 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001371 main.log.error( self.name + ": EOF exception found" )
1372 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001373 main.cleanup()
1374 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001375 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001376 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001377 main.cleanup()
1378 main.exit()
1379
kelvin-onlabd3b64892015-01-20 13:26:24 -08001380 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001381 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001382 NOTE: This method should be used after installing application:
1383 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001384 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001385 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001386 Description:
1387 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001388 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001389 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001390 if jsonFormat:
1391 cmdStr = "routes -j"
1392 handleTmp = self.sendline( cmdStr )
1393 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1394 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001395 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001396 cmdStr = "routes"
1397 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001398 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001399 except TypeError:
1400 main.log.exception( self.name + ": Object not as expected" )
1401 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001402 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001403 main.log.error( self.name + ": EOF exception found" )
1404 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001405 main.cleanup()
1406 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001407 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001408 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001409 main.cleanup()
1410 main.exit()
1411
kelvin-onlabd3b64892015-01-20 13:26:24 -08001412 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001413 """
andrewonlab377693f2014-10-21 16:00:30 -04001414 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001415 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001416 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001417 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001418 """
andrewonlabe6745342014-10-17 14:29:13 -04001419 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001420 if jsonFormat:
1421 cmdStr = "intents -j"
1422 handle = self.sendline( cmdStr )
1423 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1424 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001425 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001426 cmdStr = "intents"
1427 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001428 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001429 except TypeError:
1430 main.log.exception( self.name + ": Object not as expected" )
1431 return None
andrewonlabe6745342014-10-17 14:29:13 -04001432 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001433 main.log.error( self.name + ": EOF exception found" )
1434 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001435 main.cleanup()
1436 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001437 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001438 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001439 main.cleanup()
1440 main.exit()
1441
kelvin-onlab54400a92015-02-26 18:05:51 -08001442 def getIntentState(self, intentsId, intentsJson=None):
1443 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001444 Check intent state.
1445 Accepts a single intent ID (string type) or a list of intent IDs.
1446 Returns the state(string type) of the id if a single intent ID is
1447 accepted.
Jon Hallefbd9792015-03-05 16:11:36 -08001448 Returns a dictionary with intent IDs as the key and its
1449 corresponding states as the values
kelvin-onlabfb521662015-02-27 09:52:40 -08001450 Parameters:
kelvin-onlab54400a92015-02-26 18:05:51 -08001451 intentId: intent ID (string type)
1452 intentsJson: parsed json object from the onos:intents api
1453 Returns:
1454 state = An intent's state- INSTALL,WITHDRAWN etc.
1455 stateDict = Dictionary of intent's state. intent ID as the keys and
1456 state as the values.
1457 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001458 try:
1459 state = "State is Undefined"
1460 if not intentsJson:
Jon Hallefbd9792015-03-05 16:11:36 -08001461 intentsJsonTemp = json.loads( self.intents() )
kelvin-onlab54400a92015-02-26 18:05:51 -08001462 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001463 intentsJsonTemp = json.loads( intentsJson )
1464 if isinstance( intentsId, types.StringType ):
kelvin-onlab54400a92015-02-26 18:05:51 -08001465 for intent in intentsJsonTemp:
1466 if intentsId == intent['id']:
1467 state = intent['state']
1468 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001469 main.log.info( "Cannot find intent ID" + str( intentsId ) +
1470 " on the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001471 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001472 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001473 dictList = []
kelvin-onlab54400a92015-02-26 18:05:51 -08001474 for ID in intentsId:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001475 stateDict = {}
kelvin-onlab54400a92015-02-26 18:05:51 -08001476 for intents in intentsJsonTemp:
1477 if ID == intents['id']:
kelvin-onlab07dbd012015-03-04 16:29:39 -08001478 stateDict['state'] = intents['state']
1479 stateDict['id'] = ID
Jon Hallefbd9792015-03-05 16:11:36 -08001480 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08001481 break
Jon Hallefbd9792015-03-05 16:11:36 -08001482 if len( intentsId ) != len( dictList ):
1483 main.log.info( "Cannot find some of the intent ID state" )
kelvin-onlab07dbd012015-03-04 16:29:39 -08001484 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08001485 else:
1486 main.log.info("Invalid intents ID entry")
1487 return None
kelvin-onlab54400a92015-02-26 18:05:51 -08001488 except TypeError:
1489 main.log.exception( self.name + ": Object not as expected" )
1490 return None
1491 except pexpect.EOF:
1492 main.log.error( self.name + ": EOF exception found" )
1493 main.log.error( self.name + ": " + self.handle.before )
1494 main.cleanup()
1495 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001496 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08001497 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001498 main.cleanup()
1499 main.exit()
1500
kelvin-onlabd3b64892015-01-20 13:26:24 -08001501 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001502 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001503 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001504 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001505 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001506 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001507 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001508 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001509 if jsonFormat:
1510 cmdStr = "flows -j"
1511 handle = self.sendline( cmdStr )
1512 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1513 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001514 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001515 cmdStr = "flows"
1516 handle = self.sendline( cmdStr )
Jon Hall61282e32015-03-19 11:34:11 -07001517 if re.search( "Error:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001518 main.log.error( self.name + ".flows() response: " +
1519 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001520 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001521 except TypeError:
1522 main.log.exception( self.name + ": Object not as expected" )
1523 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001524 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001525 main.log.error( self.name + ": EOF exception found" )
1526 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001527 main.cleanup()
1528 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001529 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001530 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001531 main.cleanup()
1532 main.exit()
1533
kelvin-onlabd3b64892015-01-20 13:26:24 -08001534 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
Jon Hallefbd9792015-03-05 16:11:36 -08001535 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001536 """
andrewonlab87852b02014-11-19 18:44:19 -05001537 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001538 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001539 a specific point-to-point intent definition
1540 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001541 * dpidSrc: specify source dpid
1542 * dpidDst: specify destination dpid
1543 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001544 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001545 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001546 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001547 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001548 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001549 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001550 """
andrewonlab87852b02014-11-19 18:44:19 -05001551 try:
kelvin8ec71442015-01-15 16:57:00 -08001552 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001553 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1554 str( numIntents )
1555 if numMult:
1556 cmd += " " + str( numMult )
1557 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001558 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001559 if appId:
1560 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001561 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001562 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1563 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001564 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001565 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001566 main.log.info( handle )
1567 # Split result by newline
1568 newline = handle.split( "\r\r\n" )
1569 # Ignore the first object of list, which is empty
1570 newline = newline[ 1: ]
1571 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001572 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001573 result = result.split( ": " )
1574 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001575 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1576 main.log.info( latResult )
1577 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001578 else:
1579 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001580 except TypeError:
1581 main.log.exception( self.name + ": Object not as expected" )
1582 return None
andrewonlab87852b02014-11-19 18:44:19 -05001583 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001584 main.log.error( self.name + ": EOF exception found" )
1585 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001586 main.cleanup()
1587 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001588 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001589 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001590 main.cleanup()
1591 main.exit()
1592
kelvin-onlabd3b64892015-01-20 13:26:24 -08001593 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001594 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001595 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001596 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001597 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001598 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001599 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001600 if jsonFormat:
1601 cmdStr = "intents-events-metrics -j"
1602 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001603 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001604 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1605 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001606 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001607 cmdStr = "intents-events-metrics"
1608 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001609 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001610 except TypeError:
1611 main.log.exception( self.name + ": Object not as expected" )
1612 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001613 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001614 main.log.error( self.name + ": EOF exception found" )
1615 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001616 main.cleanup()
1617 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001618 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001619 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001620 main.cleanup()
1621 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001622
kelvin-onlabd3b64892015-01-20 13:26:24 -08001623 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001624 """
1625 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001626 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001627 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001628 """
andrewonlab867212a2014-10-22 20:13:38 -04001629 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001630 if jsonFormat:
1631 cmdStr = "topology-events-metrics -j"
1632 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001633 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001634 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1635 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001636 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001637 cmdStr = "topology-events-metrics"
1638 handle = self.sendline( cmdStr )
jenkins7ead5a82015-03-13 10:28:21 -07001639 if handle:
1640 return handle
1641 else:
1642 # Return empty json
1643 return '{}'
Jon Halld4d4b372015-01-28 16:02:41 -08001644 except TypeError:
1645 main.log.exception( self.name + ": Object not as expected" )
1646 return None
andrewonlab867212a2014-10-22 20:13:38 -04001647 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001648 main.log.error( self.name + ": EOF exception found" )
1649 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001650 main.cleanup()
1651 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001652 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001653 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001654 main.cleanup()
1655 main.exit()
1656
kelvin8ec71442015-01-15 16:57:00 -08001657 # Wrapper functions ****************
1658 # Wrapper functions use existing driver
1659 # functions and extends their use case.
1660 # For example, we may use the output of
1661 # a normal driver function, and parse it
1662 # using a wrapper function
andrewonlab7e4d2d32014-10-15 13:23:21 -04001663
kelvin-onlabd3b64892015-01-20 13:26:24 -08001664 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001665 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001666 Description:
1667 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001668 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001669 try:
kelvin8ec71442015-01-15 16:57:00 -08001670 # Obtain output of intents function
kelvin-onlabfb521662015-02-27 09:52:40 -08001671 intentsStr = self.intents(jsonFormat=False)
kelvin-onlabd3b64892015-01-20 13:26:24 -08001672 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001673
kelvin8ec71442015-01-15 16:57:00 -08001674 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001675 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1676 for intents in intentsList:
kelvin-onlabfb521662015-02-27 09:52:40 -08001677 match = re.search('id=0x([\da-f]+),', intents)
1678 if match:
1679 tmpId = match.group()[3:-1]
1680 intentIdList.append( tmpId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001681 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001682
Jon Halld4d4b372015-01-28 16:02:41 -08001683 except TypeError:
1684 main.log.exception( self.name + ": Object not as expected" )
1685 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001686 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001687 main.log.error( self.name + ": EOF exception found" )
1688 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001689 main.cleanup()
1690 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001691 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001692 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001693 main.cleanup()
1694 main.exit()
1695
Jon Hall30b82fa2015-03-04 17:15:43 -08001696 def FlowAddedCount( self, deviceId ):
1697 """
1698 Determine the number of flow rules for the given device id that are
1699 in the added state
1700 """
1701 try:
1702 cmdStr = "flows any " + str( deviceId ) + " | " +\
1703 "grep 'state=ADDED' | wc -l"
1704 handle = self.sendline( cmdStr )
1705 return handle
1706 except pexpect.EOF:
1707 main.log.error( self.name + ": EOF exception found" )
1708 main.log.error( self.name + ": " + self.handle.before )
1709 main.cleanup()
1710 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001711 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -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 getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001717 """
andrewonlab95ce8322014-10-13 14:12:04 -04001718 Use 'devices' function to obtain list of all devices
1719 and parse the result to obtain a list of all device
1720 id's. Returns this list. Returns empty list if no
1721 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001722 List is ordered sequentially
1723
andrewonlab95ce8322014-10-13 14:12:04 -04001724 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001725 device id, and wish to execute other commands using
andrewonlab95ce8322014-10-13 14:12:04 -04001726 the ids. By obtaining the list of device ids on the fly,
1727 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001728 """
andrewonlab95ce8322014-10-13 14:12:04 -04001729 try:
kelvin8ec71442015-01-15 16:57:00 -08001730 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001731 devicesStr = self.devices( jsonFormat=False )
1732 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001733
kelvin-onlabd3b64892015-01-20 13:26:24 -08001734 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001735 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001736 return idList
kelvin8ec71442015-01-15 16:57:00 -08001737
1738 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001739 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001740 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001741 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001742 # Split list further into arguments before and after string
1743 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001744 # append to idList
1745 for arg in tempList:
1746 idList.append( arg.split( "id=" )[ 1 ] )
1747 return idList
andrewonlab95ce8322014-10-13 14:12:04 -04001748
Jon Halld4d4b372015-01-28 16:02:41 -08001749 except TypeError:
1750 main.log.exception( self.name + ": Object not as expected" )
1751 return None
andrewonlab95ce8322014-10-13 14:12:04 -04001752 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001753 main.log.error( self.name + ": EOF exception found" )
1754 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -04001755 main.cleanup()
1756 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001757 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001758 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -04001759 main.cleanup()
1760 main.exit()
1761
kelvin-onlabd3b64892015-01-20 13:26:24 -08001762 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001763 """
andrewonlab7c211572014-10-15 16:45:20 -04001764 Uses 'nodes' function to obtain list of all nodes
1765 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001766 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001767 Returns:
1768 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001769 """
andrewonlab7c211572014-10-15 16:45:20 -04001770 try:
Jon Hall5aa168b2015-03-23 14:23:09 -07001771 nodesStr = self.nodes( jsonFormat=True )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001772 idList = []
Jon Hall5aa168b2015-03-23 14:23:09 -07001773 # Sample nodesStr output
1774 # id=local, address=127.0.0.1:9876, state=ACTIVE *
kelvin-onlabd3b64892015-01-20 13:26:24 -08001775 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001776 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001777 return idList
Jon Hall5aa168b2015-03-23 14:23:09 -07001778 nodesJson = json.loads( nodesStr )
1779 idList = [ node.get('id') for node in nodesJson ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08001780 return idList
kelvin8ec71442015-01-15 16:57:00 -08001781
Jon Halld4d4b372015-01-28 16:02:41 -08001782 except TypeError:
1783 main.log.exception( self.name + ": Object not as expected" )
1784 return None
andrewonlab7c211572014-10-15 16:45:20 -04001785 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001786 main.log.error( self.name + ": EOF exception found" )
1787 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001788 main.cleanup()
1789 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001790 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001791 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001792 main.cleanup()
1793 main.exit()
andrewonlab95ce8322014-10-13 14:12:04 -04001794
kelvin-onlabd3b64892015-01-20 13:26:24 -08001795 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001796 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001797 Return the first device from the devices api whose 'id' contains 'dpid'
1798 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001799 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001800 try:
kelvin8ec71442015-01-15 16:57:00 -08001801 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001802 return None
1803 else:
kelvin8ec71442015-01-15 16:57:00 -08001804 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001805 rawDevices = self.devices()
1806 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001807 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001808 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001809 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1810 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001811 return device
1812 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001813 except TypeError:
1814 main.log.exception( self.name + ": Object not as expected" )
1815 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001816 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001817 main.log.error( self.name + ": EOF exception found" )
1818 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001819 main.cleanup()
1820 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001821 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001822 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001823 main.cleanup()
1824 main.exit()
1825
kelvin-onlabd3b64892015-01-20 13:26:24 -08001826 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001827 """
Jon Hallefbd9792015-03-05 16:11:36 -08001828 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08001829 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08001830 log level can be specified.
kelvin8ec71442015-01-15 16:57:00 -08001831
Jon Hall42db6dc2014-10-24 19:03:48 -04001832 Params: ip = ip used for the onos cli
1833 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08001834 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001835 logLevel = level to log to. Currently accepts
1836 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001837
1838
kelvin-onlabd3b64892015-01-20 13:26:24 -08001839 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001840
Jon Hallefbd9792015-03-05 16:11:36 -08001841 Returns: main.TRUE if the number of switches and links are correct,
1842 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04001843 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001844 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001845 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001846 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001847 if topology == {}:
1848 return main.ERROR
1849 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001850 # Is the number of switches is what we expected
1851 devices = topology.get( 'devices', False )
1852 links = topology.get( 'links', False )
kelvin-onlabfb521662015-02-27 09:52:40 -08001853 if devices is False or links is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04001854 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001855 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001856 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001857 linkCheck = ( int( links ) == int( numolink ) )
1858 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001859 # We expected the correct numbers
Jon Hallefbd9792015-03-05 16:11:36 -08001860 output += "The number of links and switches match " +\
1861 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001862 result = main.TRUE
1863 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001864 output += "The number of links and switches does not match " +\
1865 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001866 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001867 output = output + "\n ONOS sees %i devices (%i expected) \
1868 and %i links (%i expected)" % (
1869 int( devices ), int( numoswitch ), int( links ),
1870 int( numolink ) )
1871 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001872 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001873 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001874 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001875 else:
kelvin8ec71442015-01-15 16:57:00 -08001876 main.log.info( output )
1877 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001878 except TypeError:
1879 main.log.exception( self.name + ": Object not as expected" )
1880 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001881 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001882 main.log.error( self.name + ": EOF exception found" )
1883 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001884 main.cleanup()
1885 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001886 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001887 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001888 main.cleanup()
1889 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001890
kelvin-onlabd3b64892015-01-20 13:26:24 -08001891 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001892 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001893 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001894 deviceId must be the id of a device as seen in the onos devices command
1895 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001896 role must be either master, standby, or none
1897
Jon Halle3f39ff2015-01-13 11:50:53 -08001898 Returns:
1899 main.TRUE or main.FALSE based on argument verification and
1900 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001901 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001902 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001903 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001904 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001905 cmdStr = "device-role " +\
1906 str( deviceId ) + " " +\
1907 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001908 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001909 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001910 if re.search( "Error", handle ):
1911 # end color output to escape any colours
1912 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001913 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001914 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001915 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001916 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001917 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001918 main.log.error( "Invalid 'role' given to device_role(). " +
1919 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001920 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001921 except TypeError:
1922 main.log.exception( self.name + ": Object not as expected" )
1923 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001924 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001925 main.log.error( self.name + ": EOF exception found" )
1926 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001927 main.cleanup()
1928 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001929 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001930 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001931 main.cleanup()
1932 main.exit()
1933
kelvin-onlabd3b64892015-01-20 13:26:24 -08001934 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001935 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001936 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001937 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001938 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001939 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001940 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001941 if jsonFormat:
1942 cmdStr = "clusters -j"
1943 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001944 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001945 handle variable here contains some ANSI escape color code
1946 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001947 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001948 function. The repr( handle ) output when printed shows the ANSI
1949 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001950 variable is actually repr( somestring ) and json.loads would
1951 fail with the escape sequence. So we take off that escape
1952 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001953
kelvin-onlabd3b64892015-01-20 13:26:24 -08001954 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1955 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001956 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001957 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1958 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001959 return handle1
1960 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001961 cmdStr = "clusters"
1962 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001963 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001964 except TypeError:
1965 main.log.exception( self.name + ": Object not as expected" )
1966 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001967 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001968 main.log.error( self.name + ": EOF exception found" )
1969 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001970 main.cleanup()
1971 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001972 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001973 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001974 main.cleanup()
1975 main.exit()
1976
kelvin-onlabd3b64892015-01-20 13:26:24 -08001977 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001978 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001979 CLI command to get the current leader for the Election test application
1980 NOTE: Requires installation of the onos-app-election feature
1981 Returns: Node IP of the leader if one exists
1982 None if none exists
1983 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001984 """
Jon Hall94fd0472014-12-08 11:52:42 -08001985 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001986 cmdStr = "election-test-leader"
1987 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001988 # Leader
1989 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001990 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001991 nodeSearch = re.search( leaderPattern, response )
1992 if nodeSearch:
1993 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001994 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001995 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001996 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001997 # no leader
1998 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001999 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002000 nullSearch = re.search( nullPattern, response )
2001 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08002002 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002003 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08002004 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08002005 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002006 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08002007 if re.search( errorPattern, response ):
2008 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08002009 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08002010 return main.FALSE
2011 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002012 main.log.error( "Error in election_test_leader: " +
2013 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08002014 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002015 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002016 except TypeError:
2017 main.log.exception( self.name + ": Object not as expected" )
2018 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002019 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002020 main.log.error( self.name + ": EOF exception found" )
2021 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002022 main.cleanup()
2023 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002024 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002025 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002026 main.cleanup()
2027 main.exit()
2028
kelvin-onlabd3b64892015-01-20 13:26:24 -08002029 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002030 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002031 CLI command to run for leadership of the Election test application.
2032 NOTE: Requires installation of the onos-app-election feature
2033 Returns: Main.TRUE on success
2034 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08002035 """
Jon Hall94fd0472014-12-08 11:52:42 -08002036 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002037 cmdStr = "election-test-run"
2038 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002039 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002040 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002041 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002042 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08002043 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08002044 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002045 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002046 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002047 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002048 errorPattern = "Command\snot\sfound"
2049 if re.search( errorPattern, response ):
2050 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002051 return main.FALSE
2052 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002053 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002054 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002055 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002056 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002057 except TypeError:
2058 main.log.exception( self.name + ": Object not as expected" )
2059 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002060 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002061 main.log.error( self.name + ": EOF exception found" )
2062 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002063 main.cleanup()
2064 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002065 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002066 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002067 main.cleanup()
2068 main.exit()
2069
kelvin-onlabd3b64892015-01-20 13:26:24 -08002070 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08002071 """
Jon Hall94fd0472014-12-08 11:52:42 -08002072 * CLI command to withdraw the local node from leadership election for
2073 * the Election test application.
2074 #NOTE: Requires installation of the onos-app-election feature
2075 Returns: Main.TRUE on success
2076 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08002077 """
Jon Hall94fd0472014-12-08 11:52:42 -08002078 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002079 cmdStr = "election-test-withdraw"
2080 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002081 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002082 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002083 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002084 if re.search( successPattern, response ):
2085 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002086 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002087 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002088 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002089 errorPattern = "Command\snot\sfound"
2090 if re.search( errorPattern, response ):
2091 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002092 return main.FALSE
2093 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002094 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002095 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002096 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002097 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002098 except TypeError:
2099 main.log.exception( self.name + ": Object not as expected" )
2100 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002101 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002102 main.log.error( self.name + ": EOF exception found" )
2103 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002104 main.cleanup()
2105 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002106 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002107 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002108 main.cleanup()
2109 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002110
kelvin8ec71442015-01-15 16:57:00 -08002111 def getDevicePortsEnabledCount( self, dpid ):
2112 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002113 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002114 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002115 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08002116 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002117 cmdStr = "onos:ports -e " + dpid + " | wc -l"
2118 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002119 if re.search( "No such device", output ):
2120 main.log.error( "Error in getting ports" )
2121 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002122 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002123 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002124 except TypeError:
2125 main.log.exception( self.name + ": Object not as expected" )
2126 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002127 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002128 main.log.error( self.name + ": EOF exception found" )
2129 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002130 main.cleanup()
2131 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002132 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002133 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002134 main.cleanup()
2135 main.exit()
2136
kelvin8ec71442015-01-15 16:57:00 -08002137 def getDeviceLinksActiveCount( self, dpid ):
2138 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002139 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002140 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002141 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002142 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002143 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
2144 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002145 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002146 main.log.error( "Error in getting ports " )
2147 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002148 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002149 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002150 except TypeError:
2151 main.log.exception( self.name + ": Object not as expected" )
2152 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002153 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002154 main.log.error( self.name + ": EOF exception found" )
2155 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002156 main.cleanup()
2157 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002158 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002159 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002160 main.cleanup()
2161 main.exit()
2162
kelvin8ec71442015-01-15 16:57:00 -08002163 def getAllIntentIds( self ):
2164 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002165 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002166 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002167 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002168 cmdStr = "onos:intents | grep id="
2169 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002170 if re.search( "Error", output ):
2171 main.log.error( "Error in getting ports" )
2172 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002173 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002174 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002175 except TypeError:
2176 main.log.exception( self.name + ": Object not as expected" )
2177 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002178 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002179 main.log.error( self.name + ": EOF exception found" )
2180 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002181 main.cleanup()
2182 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002183 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002184 main.log.exception( self.name + ": Uncaught exception!" )
2185 main.cleanup()
2186 main.exit()
2187
Jon Hall73509952015-02-24 16:42:56 -08002188 def intentSummary( self ):
2189 """
Jon Hallefbd9792015-03-05 16:11:36 -08002190 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08002191 """
2192 try:
2193 intents = self.intents( )
2194 intentStates = []
Jon Hall5aa168b2015-03-23 14:23:09 -07002195 for intent in json.loads( intents ):
Jon Hall73509952015-02-24 16:42:56 -08002196 intentStates.append( intent.get( 'state', None ) )
Jon Hall63604932015-02-26 17:09:50 -08002197 out = [ (i, intentStates.count( i ) ) for i in set( intentStates ) ]
2198 main.log.info( dict( out ) )
Jon Hall73509952015-02-24 16:42:56 -08002199 return dict( out )
2200 except TypeError:
2201 main.log.exception( self.name + ": Object not as expected" )
2202 return None
2203 except pexpect.EOF:
2204 main.log.error( self.name + ": EOF exception found" )
2205 main.log.error( self.name + ": " + self.handle.before )
2206 main.cleanup()
2207 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002208 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08002209 main.log.exception( self.name + ": Uncaught exception!" )
2210 main.cleanup()
2211 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002212
Jon Hall61282e32015-03-19 11:34:11 -07002213 def leaders( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08002214 """
2215 Returns the output of the leaders command.
Jon Hall61282e32015-03-19 11:34:11 -07002216 Optional argument:
2217 * jsonFormat - boolean indicating if you want output in json
Jon Hall63604932015-02-26 17:09:50 -08002218 """
2219 # FIXME: add json output
Jon Hall61282e32015-03-19 11:34:11 -07002220 # Sample JSON
2221 # {
2222 # "electedTime": "13m ago",
2223 # "epoch": 4,
2224 # "leader": "10.128.30.17",
2225 # "topic": "intent-partition-3"
2226 # },
Jon Hall63604932015-02-26 17:09:50 -08002227 try:
Jon Hall61282e32015-03-19 11:34:11 -07002228 if jsonFormat:
2229 cmdStr = "onos:leaders -j"
2230 output = self.sendline( cmdStr )
2231 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
2232 cleanedOutput = ansiEscape.sub( '', output )
2233 return cleanedOutput
2234 else:
2235 cmdStr = "onos:leaders"
2236 output = self.sendline( cmdStr )
2237 return output
Jon Hall63604932015-02-26 17:09:50 -08002238 except TypeError:
2239 main.log.exception( self.name + ": Object not as expected" )
2240 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002241 except pexpect.EOF:
2242 main.log.error( self.name + ": EOF exception found" )
2243 main.log.error( self.name + ": " + self.handle.before )
2244 main.cleanup()
2245 main.exit()
2246 except:
Jon Hall63604932015-02-26 17:09:50 -08002247 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002248 main.cleanup()
2249 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002250
Jon Hall61282e32015-03-19 11:34:11 -07002251 def pendingMap( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08002252 """
2253 Returns the output of the intent Pending map.
2254 """
Jon Hall63604932015-02-26 17:09:50 -08002255 try:
Jon Hall61282e32015-03-19 11:34:11 -07002256 if jsonFormat:
2257 cmdStr = "onos:intents -p -j"
2258 output = self.sendline( cmdStr )
2259 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
2260 cleanedOutput = ansiEscape.sub( '', output )
2261 return cleanedOutput
2262 else:
2263 cmdStr = "onos:intents -p"
2264 output = self.sendline( cmdStr )
2265 return output
Jon Hall63604932015-02-26 17:09:50 -08002266 except TypeError:
2267 main.log.exception( self.name + ": Object not as expected" )
2268 return None
2269 except pexpect.EOF:
2270 main.log.error( self.name + ": EOF exception found" )
2271 main.log.error( self.name + ": " + self.handle.before )
2272 main.cleanup()
2273 main.exit()
2274 except:
2275 main.log.exception( self.name + ": Uncaught exception!" )
2276 main.cleanup()
2277 main.exit()
2278
Jon Hall61282e32015-03-19 11:34:11 -07002279 def partitions( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08002280 """
2281 Returns the output of the raft partitions command for ONOS.
2282 """
Jon Hall61282e32015-03-19 11:34:11 -07002283 # Sample JSON
2284 # {
2285 # "leader": "tcp://10.128.30.11:7238",
2286 # "members": [
2287 # "tcp://10.128.30.11:7238",
2288 # "tcp://10.128.30.17:7238",
2289 # "tcp://10.128.30.13:7238",
2290 # ],
2291 # "name": "p1",
2292 # "term": 3
2293 # },
Jon Hall63604932015-02-26 17:09:50 -08002294 try:
Jon Hall61282e32015-03-19 11:34:11 -07002295 if jsonFormat:
2296 cmdStr = "onos:partitions -j"
2297 output = self.sendline( cmdStr )
2298 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
2299 cleanedOutput = ansiEscape.sub( '', output )
2300 return cleanedOutput
2301 else:
2302 cmdStr = "onos:partitions"
2303 output = self.sendline( cmdStr )
2304 return output
Jon Hall63604932015-02-26 17:09:50 -08002305 except TypeError:
2306 main.log.exception( self.name + ": Object not as expected" )
2307 return None
2308 except pexpect.EOF:
2309 main.log.error( self.name + ": EOF exception found" )
2310 main.log.error( self.name + ": " + self.handle.before )
2311 main.cleanup()
2312 main.exit()
2313 except:
2314 main.log.exception( self.name + ": Uncaught exception!" )
2315 main.cleanup()
2316 main.exit()
2317