blob: cf2f54a455801dfa84c94a62da6c1be070748111 [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 pexpect
20import re
Jon Hall30b82fa2015-03-04 17:15:43 -080021import json
22import types
Jon Hallbd16b922015-03-26 17:53:15 -070023import time
kelvin-onlaba4074292015-07-09 15:19:49 -070024import os
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
kelvin-onlaba4074292015-07-09 15:19:49 -070054 for key in self.options:
55 if key == 'onosIp':
56 self.onosIp = self.options[ 'onosIp' ]
57 break
58
kelvin8ec71442015-01-15 16:57:00 -080059 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070060
61 try:
62 if os.getenv( str( self.ip_address ) ) != None:
63 self.ip_address = os.getenv( str( self.ip_address ) )
64 else:
65 main.log.info( self.name +
66 ": Trying to connect to " +
67 self.ip_address )
68
69 except KeyError:
70 main.log.info( "Invalid host name," +
71 " connecting to local host instead" )
72 self.ip_address = 'localhost'
73 except Exception as inst:
74 main.log.error( "Uncaught exception: " + str( inst ) )
75
kelvin8ec71442015-01-15 16:57:00 -080076 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080077 user_name=self.user_name,
78 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080079 port=self.port,
80 pwd=self.pwd,
81 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040082
kelvin8ec71442015-01-15 16:57:00 -080083 self.handle.sendline( "cd " + self.home )
84 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040085 if self.handle:
86 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080087 else:
88 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040089 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080090 except TypeError:
91 main.log.exception( self.name + ": Object not as expected" )
92 return None
andrewonlab95ce8322014-10-13 14:12:04 -040093 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080094 main.log.error( self.name + ": EOF exception found" )
95 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040096 main.cleanup()
97 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -080098 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -080099 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400100 main.cleanup()
101 main.exit()
102
kelvin8ec71442015-01-15 16:57:00 -0800103 def disconnect( self ):
104 """
andrewonlab95ce8322014-10-13 14:12:04 -0400105 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800106 """
Jon Halld61331b2015-02-17 16:35:47 -0800107 response = main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400108 try:
Jon Hall61282e32015-03-19 11:34:11 -0700109 if self.handle:
110 i = self.logout()
111 if i == main.TRUE:
112 self.handle.sendline( "" )
113 self.handle.expect( "\$" )
114 self.handle.sendline( "exit" )
115 self.handle.expect( "closed" )
Jon Halld4d4b372015-01-28 16:02:41 -0800116 except TypeError:
117 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -0800118 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400119 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800120 main.log.error( self.name + ": EOF exception found" )
121 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700122 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700123 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700124 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800125 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800126 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400127 response = main.FALSE
128 return response
129
kelvin8ec71442015-01-15 16:57:00 -0800130 def logout( self ):
131 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500132 Sends 'logout' command to ONOS cli
Jon Hall61282e32015-03-19 11:34:11 -0700133 Returns main.TRUE if exited CLI and
134 main.FALSE on timeout (not guranteed you are disconnected)
135 None on TypeError
136 Exits test on unknown error or pexpect exits unexpectedly
kelvin8ec71442015-01-15 16:57:00 -0800137 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500138 try:
Jon Hall61282e32015-03-19 11:34:11 -0700139 if self.handle:
140 self.handle.sendline( "" )
141 i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ],
142 timeout=10 )
143 if i == 0: # In ONOS CLI
144 self.handle.sendline( "logout" )
145 self.handle.expect( "\$" )
146 return main.TRUE
147 elif i == 1: # not in CLI
148 return main.TRUE
149 elif i == 3: # Timeout
150 return main.FALSE
151 else:
andrewonlab9627f432014-11-14 12:45:10 -0500152 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800153 except TypeError:
154 main.log.exception( self.name + ": Object not as expected" )
155 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500156 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800157 main.log.error( self.name + ": eof exception found" )
Jon Hall61282e32015-03-19 11:34:11 -0700158 main.log.error( self.name + ": " + self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500159 main.cleanup()
160 main.exit()
Jon Hall61282e32015-03-19 11:34:11 -0700161 except ValueError:
Jon Hall5aa168b2015-03-23 14:23:09 -0700162 main.log.error( self.name +
163 "ValueError exception in logout method" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800164 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800165 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500166 main.cleanup()
167 main.exit()
168
kelvin-onlabd3b64892015-01-20 13:26:24 -0800169 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800170 """
andrewonlab95ce8322014-10-13 14:12:04 -0400171 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800172
andrewonlab95ce8322014-10-13 14:12:04 -0400173 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800174 """
andrewonlab95ce8322014-10-13 14:12:04 -0400175 try:
176 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800177 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400178 main.cleanup()
179 main.exit()
180 else:
kelvin8ec71442015-01-15 16:57:00 -0800181 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800182 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800183 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400184 # and that this driver will have to change accordingly
Cameron Franke9c94fb02015-01-21 10:20:20 -0800185 self.handle.expect(str(cellname))
andrew@onlab.usc400b112015-01-21 15:33:19 -0800186 handleBefore = self.handle.before
187 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800188 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800189 self.handle.sendline("")
190 self.handle.expect("\$")
andrew@onlab.usc400b112015-01-21 15:33:19 -0800191 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400192
kelvin-onlabd3b64892015-01-20 13:26:24 -0800193 main.log.info( "Cell call returned: " + handleBefore +
194 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400195
196 return main.TRUE
197
Jon Halld4d4b372015-01-28 16:02:41 -0800198 except TypeError:
199 main.log.exception( self.name + ": Object not as expected" )
200 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400201 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800202 main.log.error( self.name + ": eof exception found" )
203 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400204 main.cleanup()
205 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800206 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800207 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400208 main.cleanup()
209 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800210
pingping-lin57a56ce2015-05-20 16:43:48 -0700211 def startOnosCli( self, ONOSIp, karafTimeout="",
212 commandlineTimeout=10, onosStartTimeout=60 ):
kelvin8ec71442015-01-15 16:57:00 -0800213 """
Jon Hallefbd9792015-03-05 16:11:36 -0800214 karafTimeout is an optional argument. karafTimeout value passed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 by user would be used to set the current karaf shell idle timeout.
216 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800217 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800218 Below is an example to start a session with 60 seconds idle timeout
219 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800220
Hari Krishna25d42f72015-01-05 15:08:28 -0800221 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800222 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800223
kelvin-onlabd3b64892015-01-20 13:26:24 -0800224 Note: karafTimeout is left as str so that this could be read
225 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800226 """
andrewonlab95ce8322014-10-13 14:12:04 -0400227 try:
kelvin8ec71442015-01-15 16:57:00 -0800228 self.handle.sendline( "" )
229 x = self.handle.expect( [
pingping-lin57a56ce2015-05-20 16:43:48 -0700230 "\$", "onos>" ], commandlineTimeout)
andrewonlab48829f62014-11-17 13:49:01 -0500231
232 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800233 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500234 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400235
kelvin8ec71442015-01-15 16:57:00 -0800236 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800237 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800238 i = self.handle.expect( [
239 "onos>",
pingping-lin57a56ce2015-05-20 16:43:48 -0700240 pexpect.TIMEOUT ], onosStartTimeout )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400241
242 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800243 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800244 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800245 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800246 "config:property-set -p org.apache.karaf.shell\
247 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800248 karafTimeout )
249 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800250 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800251 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400252 return main.TRUE
253 else:
kelvin8ec71442015-01-15 16:57:00 -0800254 # If failed, send ctrl+c to process and try again
255 main.log.info( "Starting CLI failed. Retrying..." )
256 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800257 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800258 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
259 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400260 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800261 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800262 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800263 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800264 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800265 "config:property-set -p org.apache.karaf.shell\
266 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800267 karafTimeout )
268 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800269 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800270 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400271 return main.TRUE
272 else:
kelvin8ec71442015-01-15 16:57:00 -0800273 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800274 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400275 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400276
Jon Halld4d4b372015-01-28 16:02:41 -0800277 except TypeError:
278 main.log.exception( self.name + ": Object not as expected" )
279 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400280 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800281 main.log.error( self.name + ": EOF exception found" )
282 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400283 main.cleanup()
284 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800285 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800286 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400287 main.cleanup()
288 main.exit()
289
Jon Hallefbd9792015-03-05 16:11:36 -0800290 def log( self, cmdStr, level="" ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800291 """
292 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800293 returns main.TRUE on success
Jon Hallefbd9792015-03-05 16:11:36 -0800294 returns main.FALSE if Error occurred
kelvin-onlab338f5512015-02-06 10:53:16 -0800295 Available level: DEBUG, TRACE, INFO, WARN, ERROR
296 Level defaults to INFO
kelvin-onlab9f541032015-02-04 16:19:53 -0800297 """
298 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800299 lvlStr = ""
300 if level:
301 lvlStr = "--level=" + level
302
kelvin-onlab9f541032015-02-04 16:19:53 -0800303 self.handle.sendline( "" )
Jon Hallc9eabec2015-06-10 14:33:14 -0700304 i = self.handle.expect( [ "onos>","\$", pexpect.TIMEOUT ] )
Jon Hall80daded2015-05-27 16:07:00 -0700305 if i == 1:
Jon Hallc9eabec2015-06-10 14:33:14 -0700306 main.log.error( self.name + ": onos cli session closed." )
307 main.cleanup()
308 main.exit()
309 if i == 2:
Jon Hall80daded2015-05-27 16:07:00 -0700310 self.handle.sendline( "" )
311 self.handle.expect( "onos>" )
kelvin-onlab338f5512015-02-06 10:53:16 -0800312 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -0700313 self.handle.expect( "log:log" )
kelvin-onlab9f541032015-02-04 16:19:53 -0800314 self.handle.expect( "onos>" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800315
kelvin-onlab9f541032015-02-04 16:19:53 -0800316 response = self.handle.before
317 if re.search( "Error", response ):
318 return main.FALSE
319 return main.TRUE
Jon Hall80daded2015-05-27 16:07:00 -0700320 except pexpect.TIMEOUT:
321 main.log.exception( self.name + ": TIMEOUT exception found" )
322 main.cleanup()
323 main.exit()
kelvin-onlab9f541032015-02-04 16:19:53 -0800324 except pexpect.EOF:
325 main.log.error( self.name + ": EOF exception found" )
326 main.log.error( self.name + ": " + self.handle.before )
327 main.cleanup()
328 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800329 except Exception:
kelvin-onlabfb521662015-02-27 09:52:40 -0800330 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400331 main.cleanup()
332 main.exit()
333
Jon Hallc6358dd2015-04-10 12:44:28 -0700334 def sendline( self, cmdStr, debug=False ):
kelvin8ec71442015-01-15 16:57:00 -0800335 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800336 Send a completely user specified string to
337 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400338 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800339
andrewonlaba18f6bf2014-10-13 19:31:54 -0400340 Warning: There are no sanity checking to commands
341 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800342 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400343 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800344 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
345 self.log( logStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800346 self.handle.sendline( cmdStr )
Jon Hall63604932015-02-26 17:09:50 -0800347 i = self.handle.expect( ["onos>", "\$", pexpect.TIMEOUT] )
348 response = self.handle.before
349 if i == 2:
350 self.handle.sendline()
Jon Hallc6358dd2015-04-10 12:44:28 -0700351 self.handle.expect( ["\$", pexpect.TIMEOUT] )
Jon Hall63604932015-02-26 17:09:50 -0800352 response += self.handle.before
353 print response
354 try:
355 print self.handle.after
Jon Hall77ba41c2015-04-06 10:25:40 -0700356 except TypeError:
Jon Hall63604932015-02-26 17:09:50 -0800357 pass
358 # TODO: do something with i
kelvin-onlabd3b64892015-01-20 13:26:24 -0800359 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
kelvin-onlab898a6c62015-01-16 14:13:53 -0800360 + self.name + "." )
Jon Hallc6358dd2015-04-10 12:44:28 -0700361 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700362 main.log.debug( self.name + ": Raw output" )
363 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700364
365 # Remove ANSI color control strings from output
kelvin-onlabd3b64892015-01-20 13:26:24 -0800366 ansiEscape = re.compile( r'\x1b[^m]*m' )
Jon Hall63604932015-02-26 17:09:50 -0800367 response = ansiEscape.sub( '', response )
Jon Hallc6358dd2015-04-10 12:44:28 -0700368 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700369 main.log.debug( self.name + ": ansiEscape output" )
370 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700371
kelvin-onlabfb521662015-02-27 09:52:40 -0800372 # Remove extra return chars that get added
Jon Hall63604932015-02-26 17:09:50 -0800373 response = re.sub( r"\s\r", "", response )
Jon Hallc6358dd2015-04-10 12:44:28 -0700374 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700375 main.log.debug( self.name + ": Removed extra returns " +
376 "from output" )
377 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700378
379 # Strip excess whitespace
Jon Hall63604932015-02-26 17:09:50 -0800380 response = response.strip()
Jon Hallc6358dd2015-04-10 12:44:28 -0700381 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700382 main.log.debug( self.name + ": parsed and stripped output" )
383 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700384
Jon Hall63604932015-02-26 17:09:50 -0800385 # parse for just the output, remove the cmd from response
Jon Hallc6358dd2015-04-10 12:44:28 -0700386 output = response.split( cmdStr.strip(), 1 )
387 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700388 main.log.debug( self.name + ": split output" )
Jon Hallc6358dd2015-04-10 12:44:28 -0700389 for r in output:
Jon Hall390696c2015-05-05 17:13:41 -0700390 main.log.debug( self.name + ": " + repr( r ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700391 return output[1].strip()
392 except IndexError:
393 main.log.exception( self.name + ": Object not as expected" )
394 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800395 except TypeError:
396 main.log.exception( self.name + ": Object not as expected" )
397 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400398 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800399 main.log.error( self.name + ": EOF exception found" )
400 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400401 main.cleanup()
402 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800403 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800404 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400405 main.cleanup()
406 main.exit()
407
kelvin8ec71442015-01-15 16:57:00 -0800408 # IMPORTANT NOTE:
409 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800410 # the cli command changing 'a:b' with 'aB'.
411 # Ex ) onos:topology > onosTopology
412 # onos:links > onosLinks
413 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800414
kelvin-onlabd3b64892015-01-20 13:26:24 -0800415 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800416 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400417 Adds a new cluster node by ID and address information.
418 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800419 * nodeId
420 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400421 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800422 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800423 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400424 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800425 cmdStr = "add-node " + str( nodeId ) + " " +\
426 str( ONOSIp ) + " " + str( tcpPort )
427 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800428 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800429 main.log.error( "Error in adding node" )
430 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800431 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400432 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800433 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400434 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800435 except TypeError:
436 main.log.exception( self.name + ": Object not as expected" )
437 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400438 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800439 main.log.error( self.name + ": EOF exception found" )
440 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400441 main.cleanup()
442 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800443 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800444 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400445 main.cleanup()
446 main.exit()
447
kelvin-onlabd3b64892015-01-20 13:26:24 -0800448 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800449 """
andrewonlab86dc3082014-10-13 18:18:38 -0400450 Removes a cluster by ID
451 Issues command: 'remove-node [<node-id>]'
452 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800453 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800454 """
andrewonlab86dc3082014-10-13 18:18:38 -0400455 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400456
kelvin-onlabd3b64892015-01-20 13:26:24 -0800457 cmdStr = "remove-node " + str( nodeId )
Jon Hall08f61bc2015-04-13 16:00:30 -0700458 handle = self.sendline( cmdStr )
Jon Hallc6358dd2015-04-10 12:44:28 -0700459 if re.search( "Error", handle ):
460 main.log.error( "Error in removing node" )
461 main.log.error( handle )
462 return main.FALSE
463 else:
464 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800465 except TypeError:
466 main.log.exception( self.name + ": Object not as expected" )
467 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400468 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800469 main.log.error( self.name + ": EOF exception found" )
470 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400471 main.cleanup()
472 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800473 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800474 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400475 main.cleanup()
476 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400477
Jon Hall61282e32015-03-19 11:34:11 -0700478 def nodes( self, jsonFormat=True):
kelvin8ec71442015-01-15 16:57:00 -0800479 """
andrewonlab7c211572014-10-15 16:45:20 -0400480 List the nodes currently visible
481 Issues command: 'nodes'
Jon Hall61282e32015-03-19 11:34:11 -0700482 Optional argument:
483 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800484 """
andrewonlab7c211572014-10-15 16:45:20 -0400485 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700486 cmdStr = "nodes"
Jon Hall61282e32015-03-19 11:34:11 -0700487 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700488 cmdStr += " -j"
489 output = self.sendline( cmdStr )
490 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800491 except TypeError:
492 main.log.exception( self.name + ": Object not as expected" )
493 return None
andrewonlab7c211572014-10-15 16:45:20 -0400494 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800495 main.log.error( self.name + ": EOF exception found" )
496 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400497 main.cleanup()
498 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800499 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800500 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400501 main.cleanup()
502 main.exit()
503
kelvin8ec71442015-01-15 16:57:00 -0800504 def topology( self ):
505 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700506 Definition:
Jon Hall390696c2015-05-05 17:13:41 -0700507 Returns the output of topology command.
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700508 Return:
509 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -0800510 """
andrewonlab95ce8322014-10-13 14:12:04 -0400511 try:
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700512 cmdStr = "topology -j"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800513 handle = self.sendline( cmdStr )
Jon Hallc6358dd2015-04-10 12:44:28 -0700514 main.log.info( cmdStr + " returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400515 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800516 except TypeError:
517 main.log.exception( self.name + ": Object not as expected" )
518 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400519 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800520 main.log.error( self.name + ": EOF exception found" )
521 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400522 main.cleanup()
523 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800524 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800525 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400526 main.cleanup()
527 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800528
kelvin-onlabd3b64892015-01-20 13:26:24 -0800529 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800530 """
Jon Hallc6358dd2015-04-10 12:44:28 -0700531 Installs a specified feature by issuing command:
532 'feature:install <feature_str>'
533 NOTE: This is now deprecated, you should use the activateApp method
534 instead
kelvin8ec71442015-01-15 16:57:00 -0800535 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400536 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800537 cmdStr = "feature:install " + str( featureStr )
Jon Hallc6358dd2015-04-10 12:44:28 -0700538 handle = self.sendline( cmdStr )
539 if re.search( "Error", handle ):
540 main.log.error( "Error in installing feature" )
541 main.log.error( handle )
542 return main.FALSE
543 else:
544 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800545 except TypeError:
546 main.log.exception( self.name + ": Object not as expected" )
547 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400548 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800549 main.log.error( self.name + ": EOF exception found" )
550 main.log.error( self.name + ": " + self.handle.before )
551 main.log.report( "Failed to install feature" )
552 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400553 main.cleanup()
554 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800555 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800556 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800557 main.log.report( "Failed to install feature" )
558 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400559 main.cleanup()
560 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800561
kelvin-onlabd3b64892015-01-20 13:26:24 -0800562 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800563 """
Jon Hallc6358dd2015-04-10 12:44:28 -0700564 Uninstalls a specified feature by issuing command:
565 'feature:uninstall <feature_str>'
566 NOTE: This is now deprecated, you should use the deactivateApp method
567 instead
kelvin8ec71442015-01-15 16:57:00 -0800568 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400569 try:
Jon Hall30b82fa2015-03-04 17:15:43 -0800570 cmdStr = 'feature:list -i | grep "' + featureStr + '"'
571 handle = self.sendline( cmdStr )
572 if handle != '':
573 cmdStr = "feature:uninstall " + str( featureStr )
Jon Hallc6358dd2015-04-10 12:44:28 -0700574 output = self.sendline( cmdStr )
Jon Hall30b82fa2015-03-04 17:15:43 -0800575 # TODO: Check for possible error responses from karaf
576 else:
Jon Hallefbd9792015-03-05 16:11:36 -0800577 main.log.info( "Feature needs to be installed before " +
578 "uninstalling it" )
Jon Hallc6358dd2015-04-10 12:44:28 -0700579 return main.TRUE
580 if re.search( "Error", output ):
581 main.log.error( "Error in uninstalling feature" )
582 main.log.error( output )
583 return main.FALSE
584 else:
585 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800586 except TypeError:
587 main.log.exception( self.name + ": Object not as expected" )
588 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400589 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800590 main.log.error( self.name + ": EOF exception found" )
591 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400592 main.cleanup()
593 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800594 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800595 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400596 main.cleanup()
597 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800598
jenkins7ead5a82015-03-13 10:28:21 -0700599 def deviceRemove( self, deviceId ):
600 """
601 Removes particular device from storage
602
603 TODO: refactor this function
604 """
605 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700606 cmdStr = "device-remove " + str( deviceId )
607 handle = self.sendline( cmdStr )
608 if re.search( "Error", handle ):
609 main.log.error( "Error in removing device" )
610 main.log.error( handle )
611 return main.FALSE
612 else:
613 return main.TRUE
jenkins7ead5a82015-03-13 10:28:21 -0700614 except TypeError:
615 main.log.exception( self.name + ": Object not as expected" )
616 return None
617 except pexpect.EOF:
618 main.log.error( self.name + ": EOF exception found" )
619 main.log.error( self.name + ": " + self.handle.before )
620 main.cleanup()
621 main.exit()
622 except Exception:
623 main.log.exception( self.name + ": Uncaught exception!" )
624 main.cleanup()
625 main.exit()
jenkins7ead5a82015-03-13 10:28:21 -0700626
kelvin-onlabd3b64892015-01-20 13:26:24 -0800627 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800628 """
Jon Hall7b02d952014-10-17 20:14:54 -0400629 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400630 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800631 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800632 """
andrewonlab86dc3082014-10-13 18:18:38 -0400633 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700634 cmdStr = "devices"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800635 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700636 cmdStr += " -j"
637 handle = self.sendline( cmdStr )
638 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800639 except TypeError:
640 main.log.exception( self.name + ": Object not as expected" )
641 return None
andrewonlab7c211572014-10-15 16:45:20 -0400642 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800643 main.log.error( self.name + ": EOF exception found" )
644 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400645 main.cleanup()
646 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800647 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800648 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400649 main.cleanup()
650 main.exit()
651
kelvin-onlabd3b64892015-01-20 13:26:24 -0800652 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800653 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800654 This balances the devices across all controllers
655 by issuing command: 'onos> onos:balance-masters'
656 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800657 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800658 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800659 cmdStr = "onos:balance-masters"
Jon Hallc6358dd2015-04-10 12:44:28 -0700660 handle = self.sendline( cmdStr )
661 if re.search( "Error", handle ):
662 main.log.error( "Error in balancing masters" )
663 main.log.error( handle )
664 return main.FALSE
665 else:
666 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800667 except TypeError:
668 main.log.exception( self.name + ": Object not as expected" )
669 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800670 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800671 main.log.error( self.name + ": EOF exception found" )
672 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800673 main.cleanup()
674 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800675 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800676 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800677 main.cleanup()
678 main.exit()
679
acsmars24950022015-07-30 18:00:43 -0700680 def checkMasters( self,jsonFormat=True ):
681 """
682 Returns the output of the masters command.
683 Optional argument:
684 * jsonFormat - boolean indicating if you want output in json
685 """
686 try:
687 cmdStr = "onos:masters"
688 if jsonFormat:
689 cmdStr += " -j"
690 output = self.sendline( cmdStr )
691 return output
692 except TypeError:
693 main.log.exception( self.name + ": Object not as expected" )
694 return None
695 except pexpect.EOF:
696 main.log.error( self.name + ": EOF exception found" )
697 main.log.error( self.name + ": " + self.handle.before )
698 main.cleanup()
699 main.exit()
700 except Exception:
701 main.log.exception( self.name + ": Uncaught exception!" )
702 main.cleanup()
703 main.exit()
704
705 def checkBalanceMasters( self,jsonFormat=True ):
706 """
707 Uses the master command to check that the devices' leadership
708 is evenly divided
709
710 Dependencies: checkMasters() and summary()
711
712 Returns main.True if the devices are balanced
713 Returns main.False if the devices are unbalanced
714 Exits on Exception
715 Returns None on TypeError
716 """
717 try:
718 totalDevices = json.loads( self.summary() )[ "devices" ]
719 totalOwnedDevices = 0
720 masters = json.loads( self.checkMasters() )
721 first = masters[ 0 ][ "size" ]
722 for master in masters:
723 totalOwnedDevices += master[ "size" ]
724 if master[ "size" ] > first + 1 or master[ "size" ] < first - 1:
725 main.log.error( "Mastership not balanced" )
726 main.log.info( "\n" + self.checkMasters( False ) )
727 return main.FALSE
728 main.log.info( "Mastership balanced between " \
729 + str( len(masters) ) + " masters" )
730 return main.TRUE
731 except TypeError:
732 main.log.exception( self.name + ": Object not as expected" )
733 return None
734 except pexpect.EOF:
735 main.log.error( self.name + ": EOF exception found" )
736 main.log.error( self.name + ": " + self.handle.before )
737 main.cleanup()
738 main.exit()
739 except Exception:
740 main.log.exception( self.name + ": Uncaught exception!" )
741 main.cleanup()
742 main.exit()
743
kelvin-onlabd3b64892015-01-20 13:26:24 -0800744 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800745 """
Jon Halle8217482014-10-17 13:49:14 -0400746 Lists all core links
747 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800748 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800749 """
Jon Halle8217482014-10-17 13:49:14 -0400750 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700751 cmdStr = "links"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800752 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700753 cmdStr += " -j"
754 handle = self.sendline( cmdStr )
755 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800756 except TypeError:
757 main.log.exception( self.name + ": Object not as expected" )
758 return None
Jon Halle8217482014-10-17 13:49:14 -0400759 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800760 main.log.error( self.name + ": EOF exception found" )
761 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400762 main.cleanup()
763 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800764 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800765 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400766 main.cleanup()
767 main.exit()
768
kelvin-onlabd3b64892015-01-20 13:26:24 -0800769 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800770 """
Jon Halle8217482014-10-17 13:49:14 -0400771 Lists all ports
772 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800773 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800774 """
Jon Halle8217482014-10-17 13:49:14 -0400775 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700776 cmdStr = "ports"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800777 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700778 cmdStr += " -j"
779 handle = self.sendline( cmdStr )
780 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800781 except TypeError:
782 main.log.exception( self.name + ": Object not as expected" )
783 return None
Jon Halle8217482014-10-17 13:49:14 -0400784 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800785 main.log.error( self.name + ": EOF exception found" )
786 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400787 main.cleanup()
788 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800789 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800790 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400791 main.cleanup()
792 main.exit()
793
kelvin-onlabd3b64892015-01-20 13:26:24 -0800794 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800795 """
Jon Hall983a1702014-10-28 18:44:22 -0400796 Lists all devices and the controllers with roles assigned to them
797 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800798 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800799 """
andrewonlab7c211572014-10-15 16:45:20 -0400800 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700801 cmdStr = "roles"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800802 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700803 cmdStr += " -j"
804 handle = self.sendline( cmdStr )
805 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800806 except TypeError:
807 main.log.exception( self.name + ": Object not as expected" )
808 return None
Jon Hall983a1702014-10-28 18:44:22 -0400809 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800810 main.log.error( self.name + ": EOF exception found" )
811 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400812 main.cleanup()
813 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800814 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800815 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400816 main.cleanup()
817 main.exit()
818
kelvin-onlabd3b64892015-01-20 13:26:24 -0800819 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800820 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800821 Given the a string containing the json representation of the "roles"
822 cli command and a partial or whole device id, returns a json object
823 containing the roles output for the first device whose id contains
824 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400825
826 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800827 A dict of the role assignments for the given device or
828 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800829 """
Jon Hall983a1702014-10-28 18:44:22 -0400830 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800831 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400832 return None
833 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800834 rawRoles = self.roles()
835 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800836 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800837 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800838 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800839 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400840 return device
841 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800842 except TypeError:
843 main.log.exception( self.name + ": Object not as expected" )
844 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400845 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800846 main.log.error( self.name + ": EOF exception found" )
847 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400848 main.cleanup()
849 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800850 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800851 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400852 main.cleanup()
853 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800854
kelvin-onlabd3b64892015-01-20 13:26:24 -0800855 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800856 """
Jon Hall94fd0472014-12-08 11:52:42 -0800857 Iterates through each device and checks if there is a master assigned
858 Returns: main.TRUE if each device has a master
859 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800860 """
Jon Hall94fd0472014-12-08 11:52:42 -0800861 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800862 rawRoles = self.roles()
863 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800864 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800865 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800866 # print device
867 if device[ 'master' ] == "none":
868 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800869 return main.FALSE
870 return main.TRUE
871
Jon Halld4d4b372015-01-28 16:02:41 -0800872 except TypeError:
873 main.log.exception( self.name + ": Object not as expected" )
874 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800875 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800876 main.log.error( self.name + ": EOF exception found" )
877 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800878 main.cleanup()
879 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800880 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800881 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800882 main.cleanup()
883 main.exit()
884
kelvin-onlabd3b64892015-01-20 13:26:24 -0800885 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800886 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400887 Returns string of paths, and the cost.
888 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800889 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400890 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800891 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
892 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800893 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800894 main.log.error( "Error in getting paths" )
895 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400896 else:
kelvin8ec71442015-01-15 16:57:00 -0800897 path = handle.split( ";" )[ 0 ]
898 cost = handle.split( ";" )[ 1 ]
899 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800900 except TypeError:
901 main.log.exception( self.name + ": Object not as expected" )
902 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400903 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800904 main.log.error( self.name + ": EOF exception found" )
905 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400906 main.cleanup()
907 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800908 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800909 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400910 main.cleanup()
911 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800912
kelvin-onlabd3b64892015-01-20 13:26:24 -0800913 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800914 """
Jon Hallffb386d2014-11-21 13:43:38 -0800915 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400916 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800917 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800918 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400919 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700920 cmdStr = "hosts"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800921 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700922 cmdStr += " -j"
923 handle = self.sendline( cmdStr )
924 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800925 except TypeError:
926 main.log.exception( self.name + ": Object not as expected" )
927 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400928 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800929 main.log.error( self.name + ": EOF exception found" )
930 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400931 main.cleanup()
932 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800933 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800934 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400935 main.cleanup()
936 main.exit()
937
kelvin-onlabd3b64892015-01-20 13:26:24 -0800938 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800939 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400940 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800941
Jon Hallefbd9792015-03-05 16:11:36 -0800942 Note: mac must be a colon separated mac address, but could be a
Jon Halle3f39ff2015-01-13 11:50:53 -0800943 partial mac address
944
Jon Hall42db6dc2014-10-24 19:03:48 -0400945 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800946 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400947 try:
kelvin8ec71442015-01-15 16:57:00 -0800948 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400949 return None
950 else:
951 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800952 rawHosts = self.hosts()
953 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800954 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800955 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800956 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800957 if not host:
958 pass
959 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400960 return host
961 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800962 except TypeError:
963 main.log.exception( self.name + ": Object not as expected" )
964 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400965 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800966 main.log.error( self.name + ": EOF exception found" )
967 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400968 main.cleanup()
969 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800970 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800971 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400972 main.cleanup()
973 main.exit()
974
kelvin-onlabd3b64892015-01-20 13:26:24 -0800975 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800976 """
977 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400978 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800979
andrewonlab3f0a4af2014-10-17 12:25:14 -0400980 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800981 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400982 IMPORTANT:
983 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800984 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400985 Furthermore, it assumes that value of VLAN is '-1'
986 Description:
kelvin8ec71442015-01-15 16:57:00 -0800987 Converts mininet hosts ( h1, h2, h3... ) into
988 ONOS format ( 00:00:00:00:00:01/-1 , ... )
989 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400990 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800991 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400992
kelvin-onlabd3b64892015-01-20 13:26:24 -0800993 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800994 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800995 hostHex = hex( int( host ) ).zfill( 12 )
996 hostHex = str( hostHex ).replace( 'x', '0' )
997 i = iter( str( hostHex ) )
998 hostHex = ":".join( a + b for a, b in zip( i, i ) )
999 hostHex = hostHex + "/-1"
1000 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001001
kelvin-onlabd3b64892015-01-20 13:26:24 -08001002 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -04001003
Jon Halld4d4b372015-01-28 16:02:41 -08001004 except TypeError:
1005 main.log.exception( self.name + ": Object not as expected" )
1006 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -04001007 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001008 main.log.error( self.name + ": EOF exception found" )
1009 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001010 main.cleanup()
1011 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001012 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001013 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001014 main.cleanup()
1015 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -04001016
kelvin-onlabd3b64892015-01-20 13:26:24 -08001017 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -08001018 """
andrewonlabe6745342014-10-17 14:29:13 -04001019 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001020 * hostIdOne: ONOS host id for host1
1021 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -04001022 Description:
Jon Hallefbd9792015-03-05 16:11:36 -08001023 Adds a host-to-host intent ( bidirectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -05001024 specifying the two hosts.
kelvin-onlabfb521662015-02-27 09:52:40 -08001025 Returns:
1026 A string of the intent id or None on Error
kelvin8ec71442015-01-15 16:57:00 -08001027 """
andrewonlabe6745342014-10-17 14:29:13 -04001028 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001029 cmdStr = "add-host-intent " + str( hostIdOne ) +\
1030 " " + str( hostIdTwo )
1031 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -08001032 if re.search( "Error", handle ):
1033 main.log.error( "Error in adding Host intent" )
Jon Hall61282e32015-03-19 11:34:11 -07001034 main.log.debug( "Response from ONOS was: " + repr( handle ) )
kelvin-onlabfb521662015-02-27 09:52:40 -08001035 return None
Hari Krishnaac4e1782015-01-26 12:09:12 -08001036 else:
1037 main.log.info( "Host intent installed between " +
kelvin-onlabfb521662015-02-27 09:52:40 -08001038 str( hostIdOne ) + " and " + str( hostIdTwo ) )
1039 match = re.search('id=0x([\da-f]+),', handle)
1040 if match:
1041 return match.group()[3:-1]
1042 else:
1043 main.log.error( "Error, intent ID not found" )
Jon Hall61282e32015-03-19 11:34:11 -07001044 main.log.debug( "Response from ONOS was: " +
1045 repr( handle ) )
kelvin-onlabfb521662015-02-27 09:52:40 -08001046 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001047 except TypeError:
1048 main.log.exception( self.name + ": Object not as expected" )
1049 return None
andrewonlabe6745342014-10-17 14:29:13 -04001050 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001051 main.log.error( self.name + ": EOF exception found" )
1052 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001053 main.cleanup()
1054 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001055 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001056 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001057 main.cleanup()
1058 main.exit()
1059
kelvin-onlabd3b64892015-01-20 13:26:24 -08001060 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -08001061 """
andrewonlab7b31d232014-10-24 13:31:47 -04001062 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001063 * ingressDevice: device id of ingress device
1064 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -04001065 Optional:
1066 TODO: Still needs to be implemented via dev side
kelvin-onlabfb521662015-02-27 09:52:40 -08001067 Description:
1068 Adds an optical intent by specifying an ingress and egress device
1069 Returns:
1070 A string of the intent id or None on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001071 """
andrewonlab7b31d232014-10-24 13:31:47 -04001072 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001073 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
1074 " " + str( egressDevice )
1075 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001076 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -08001077 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001078 main.log.error( "Error in adding Optical intent" )
1079 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001080 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001081 main.log.info( "Optical intent installed between " +
1082 str( ingressDevice ) + " and " +
1083 str( egressDevice ) )
1084 match = re.search('id=0x([\da-f]+),', handle)
1085 if match:
1086 return match.group()[3:-1]
1087 else:
1088 main.log.error( "Error, intent ID not found" )
1089 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001090 except TypeError:
1091 main.log.exception( self.name + ": Object not as expected" )
1092 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001093 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001094 main.log.error( self.name + ": EOF exception found" )
1095 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -04001096 main.cleanup()
1097 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001098 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001099 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -04001100 main.cleanup()
1101 main.exit()
1102
kelvin-onlabd3b64892015-01-20 13:26:24 -08001103 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001104 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001105 ingressDevice,
1106 egressDevice,
1107 portIngress="",
1108 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001109 ethType="",
1110 ethSrc="",
1111 ethDst="",
1112 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001113 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001114 ipProto="",
1115 ipSrc="",
1116 ipDst="",
1117 tcpSrc="",
1118 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001119 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001120 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001121 * ingressDevice: device id of ingress device
1122 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001123 Optional:
1124 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001125 * ethSrc: specify ethSrc ( i.e. src mac addr )
1126 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001127 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001128 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001129 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001130 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001131 * ipSrc: specify ip source address
1132 * ipDst: specify ip destination address
1133 * tcpSrc: specify tcp source port
1134 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001135 Description:
kelvin8ec71442015-01-15 16:57:00 -08001136 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001137 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001138 Returns:
1139 A string of the intent id or None on error
andrewonlab289e4b72014-10-21 21:24:18 -04001140
Jon Halle3f39ff2015-01-13 11:50:53 -08001141 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001142 options developers provide for point-to-point
1143 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001144 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001145 try:
kelvin8ec71442015-01-15 16:57:00 -08001146 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001147 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001148 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001149 and not ipProto and not ipSrc and not ipDst \
1150 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001151 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001152
andrewonlab289e4b72014-10-21 21:24:18 -04001153 else:
andrewonlab36af3822014-11-18 17:48:18 -05001154 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001155
andrewonlab0c0a6772014-10-22 12:31:18 -04001156 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001157 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001158 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001159 cmd += " --ethSrc " + str( ethSrc )
1160 if ethDst:
1161 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001162 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001163 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001164 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001165 cmd += " --lambda "
1166 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001167 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001168 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001169 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001170 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001171 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001172 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001173 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001174 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001175 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001176
kelvin8ec71442015-01-15 16:57:00 -08001177 # Check whether the user appended the port
1178 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001179 if "/" in ingressDevice:
1180 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001181 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001182 if not portIngress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001183 main.log.error( "You must specify the ingress port" )
kelvin8ec71442015-01-15 16:57:00 -08001184 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001185 # Would it make sense to throw an exception and exit
1186 # the test?
1187 return None
andrewonlab36af3822014-11-18 17:48:18 -05001188
kelvin8ec71442015-01-15 16:57:00 -08001189 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001190 str( ingressDevice ) + "/" +\
1191 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001192
kelvin-onlabd3b64892015-01-20 13:26:24 -08001193 if "/" in egressDevice:
1194 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001195 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001196 if not portEgress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001197 main.log.error( "You must specify the egress port" )
1198 return None
Jon Halle3f39ff2015-01-13 11:50:53 -08001199
kelvin8ec71442015-01-15 16:57:00 -08001200 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001201 str( egressDevice ) + "/" +\
1202 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001203
kelvin-onlab898a6c62015-01-16 14:13:53 -08001204 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001205 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001206 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001207 main.log.error( "Error in adding point-to-point intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001208 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001209 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001210 # TODO: print out all the options in this message?
1211 main.log.info( "Point-to-point intent installed between " +
1212 str( ingressDevice ) + " and " +
1213 str( egressDevice ) )
1214 match = re.search('id=0x([\da-f]+),', handle)
1215 if match:
1216 return match.group()[3:-1]
1217 else:
1218 main.log.error( "Error, intent ID not found" )
1219 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001220 except TypeError:
1221 main.log.exception( self.name + ": Object not as expected" )
1222 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001223 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001224 main.log.error( self.name + ": EOF exception found" )
1225 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001226 main.cleanup()
1227 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001228 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001229 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001230 main.cleanup()
1231 main.exit()
1232
kelvin-onlabd3b64892015-01-20 13:26:24 -08001233 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001234 self,
shahshreyac2f97072015-03-19 17:04:29 -07001235 ingressDeviceList,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001236 egressDevice,
shahshreyac2f97072015-03-19 17:04:29 -07001237 portIngressList=None,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001238 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001239 ethType="",
1240 ethSrc="",
1241 ethDst="",
1242 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001243 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001244 ipProto="",
1245 ipSrc="",
1246 ipDst="",
1247 tcpSrc="",
1248 tcpDst="",
1249 setEthSrc="",
1250 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001251 """
shahshreyad0c80432014-12-04 16:56:05 -08001252 Note:
shahshreya70622b12015-03-19 17:19:00 -07001253 This function assumes the format of all ingress devices
Jon Hallbe379602015-03-24 13:39:32 -07001254 is same. That is, all ingress devices include port numbers
1255 with a "/" or all ingress devices could specify device
1256 ids and port numbers seperately.
shahshreyad0c80432014-12-04 16:56:05 -08001257 Required:
Jon Hallbe379602015-03-24 13:39:32 -07001258 * ingressDeviceList: List of device ids of ingress device
shahshreyac2f97072015-03-19 17:04:29 -07001259 ( Atleast 2 ingress devices required in the list )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001260 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001261 Optional:
1262 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001263 * ethSrc: specify ethSrc ( i.e. src mac addr )
1264 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001265 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001266 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001267 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001268 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001269 * ipSrc: specify ip source address
1270 * ipDst: specify ip destination address
1271 * tcpSrc: specify tcp source port
1272 * tcpDst: specify tcp destination port
1273 * setEthSrc: action to Rewrite Source MAC Address
1274 * setEthDst: action to Rewrite Destination MAC Address
1275 Description:
kelvin8ec71442015-01-15 16:57:00 -08001276 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001277 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001278 Returns:
1279 A string of the intent id or None on error
shahshreyad0c80432014-12-04 16:56:05 -08001280
Jon Halle3f39ff2015-01-13 11:50:53 -08001281 NOTE: This function may change depending on the
Jon Hallefbd9792015-03-05 16:11:36 -08001282 options developers provide for multipoint-to-singlepoint
shahshreyad0c80432014-12-04 16:56:05 -08001283 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001284 """
shahshreyad0c80432014-12-04 16:56:05 -08001285 try:
kelvin8ec71442015-01-15 16:57:00 -08001286 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001287 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001288 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001289 and not ipProto and not ipSrc and not ipDst\
1290 and not tcpSrc and not tcpDst and not setEthSrc\
1291 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001292 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001293
1294 else:
1295 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001296
shahshreyad0c80432014-12-04 16:56:05 -08001297 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001298 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001299 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001300 cmd += " --ethSrc " + str( ethSrc )
1301 if ethDst:
1302 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001303 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001304 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001305 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001306 cmd += " --lambda "
1307 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001308 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001309 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001310 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001311 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001312 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001313 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001314 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001315 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001316 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001317 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001318 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001319 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001320 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001321
kelvin8ec71442015-01-15 16:57:00 -08001322 # Check whether the user appended the port
1323 # or provided it as an input
shahshreyac2f97072015-03-19 17:04:29 -07001324
1325 if portIngressList is None:
1326 for ingressDevice in ingressDeviceList:
1327 if "/" in ingressDevice:
1328 cmd += " " + str( ingressDevice )
1329 else:
1330 main.log.error( "You must specify " +
Jon Hallbe379602015-03-24 13:39:32 -07001331 "the ingress port" )
shahshreyac2f97072015-03-19 17:04:29 -07001332 # TODO: perhaps more meaningful return
1333 return main.FALSE
shahshreyad0c80432014-12-04 16:56:05 -08001334 else:
Jon Hall71ce4e72015-03-23 14:05:58 -07001335 if len( ingressDeviceList ) == len( portIngressList ):
Jon Hall08f61bc2015-04-13 16:00:30 -07001336 for ingressDevice, portIngress in zip( ingressDeviceList,
1337 portIngressList ):
shahshreya70622b12015-03-19 17:19:00 -07001338 cmd += " " + \
1339 str( ingressDevice ) + "/" +\
1340 str( portIngress ) + " "
kelvin-onlab38143812015-04-01 15:03:01 -07001341 else:
Jon Hall08f61bc2015-04-13 16:00:30 -07001342 main.log.error( "Device list and port list does not " +
1343 "have the same length" )
kelvin-onlab38143812015-04-01 15:03:01 -07001344 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001345 if "/" in egressDevice:
1346 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001347 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001348 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001349 main.log.error( "You must specify " +
1350 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001351 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001352
kelvin8ec71442015-01-15 16:57:00 -08001353 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001354 str( egressDevice ) + "/" +\
1355 str( portEgress )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001356 handle = self.sendline( cmd )
kelvin-onlabfb521662015-02-27 09:52:40 -08001357 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001358 if re.search( "Error", handle ):
kelvin-onlabfb521662015-02-27 09:52:40 -08001359 main.log.error( "Error in adding multipoint-to-singlepoint " +
1360 "intent" )
1361 return None
shahshreyad0c80432014-12-04 16:56:05 -08001362 else:
kelvin-onlabb9408212015-04-01 13:34:04 -07001363 match = re.search('id=0x([\da-f]+),', handle)
1364 if match:
1365 return match.group()[3:-1]
1366 else:
1367 main.log.error( "Error, intent ID not found" )
1368 return None
1369 except TypeError:
1370 main.log.exception( self.name + ": Object not as expected" )
1371 return None
1372 except pexpect.EOF:
1373 main.log.error( self.name + ": EOF exception found" )
1374 main.log.error( self.name + ": " + self.handle.before )
1375 main.cleanup()
1376 main.exit()
1377 except Exception:
1378 main.log.exception( self.name + ": Uncaught exception!" )
1379 main.cleanup()
1380 main.exit()
1381
1382 def addSinglepointToMultipointIntent(
1383 self,
1384 ingressDevice,
1385 egressDeviceList,
1386 portIngress="",
1387 portEgressList=None,
1388 ethType="",
1389 ethSrc="",
1390 ethDst="",
1391 bandwidth="",
1392 lambdaAlloc=False,
1393 ipProto="",
1394 ipSrc="",
1395 ipDst="",
1396 tcpSrc="",
1397 tcpDst="",
1398 setEthSrc="",
1399 setEthDst="" ):
1400 """
1401 Note:
1402 This function assumes the format of all egress devices
1403 is same. That is, all egress devices include port numbers
1404 with a "/" or all egress devices could specify device
1405 ids and port numbers seperately.
1406 Required:
1407 * EgressDeviceList: List of device ids of egress device
1408 ( Atleast 2 eress devices required in the list )
1409 * ingressDevice: device id of ingress device
1410 Optional:
1411 * ethType: specify ethType
1412 * ethSrc: specify ethSrc ( i.e. src mac addr )
1413 * ethDst: specify ethDst ( i.e. dst mac addr )
1414 * bandwidth: specify bandwidth capacity of link
1415 * lambdaAlloc: if True, intent will allocate lambda
1416 for the specified intent
1417 * ipProto: specify ip protocol
1418 * ipSrc: specify ip source address
1419 * ipDst: specify ip destination address
1420 * tcpSrc: specify tcp source port
1421 * tcpDst: specify tcp destination port
1422 * setEthSrc: action to Rewrite Source MAC Address
1423 * setEthDst: action to Rewrite Destination MAC Address
1424 Description:
1425 Adds a singlepoint-to-multipoint intent ( uni-directional ) by
1426 specifying device id's and optional fields
1427 Returns:
1428 A string of the intent id or None on error
1429
1430 NOTE: This function may change depending on the
1431 options developers provide for singlepoint-to-multipoint
1432 intent via cli
1433 """
1434 try:
1435 # If there are no optional arguments
1436 if not ethType and not ethSrc and not ethDst\
1437 and not bandwidth and not lambdaAlloc\
1438 and not ipProto and not ipSrc and not ipDst\
1439 and not tcpSrc and not tcpDst and not setEthSrc\
1440 and not setEthDst:
1441 cmd = "add-single-to-multi-intent"
1442
1443 else:
1444 cmd = "add-single-to-multi-intent"
1445
1446 if ethType:
1447 cmd += " --ethType " + str( ethType )
1448 if ethSrc:
1449 cmd += " --ethSrc " + str( ethSrc )
1450 if ethDst:
1451 cmd += " --ethDst " + str( ethDst )
1452 if bandwidth:
1453 cmd += " --bandwidth " + str( bandwidth )
1454 if lambdaAlloc:
1455 cmd += " --lambda "
1456 if ipProto:
1457 cmd += " --ipProto " + str( ipProto )
1458 if ipSrc:
1459 cmd += " --ipSrc " + str( ipSrc )
1460 if ipDst:
1461 cmd += " --ipDst " + str( ipDst )
1462 if tcpSrc:
1463 cmd += " --tcpSrc " + str( tcpSrc )
1464 if tcpDst:
1465 cmd += " --tcpDst " + str( tcpDst )
1466 if setEthSrc:
1467 cmd += " --setEthSrc " + str( setEthSrc )
1468 if setEthDst:
1469 cmd += " --setEthDst " + str( setEthDst )
1470
1471 # Check whether the user appended the port
1472 # or provided it as an input
Jon Hall08f61bc2015-04-13 16:00:30 -07001473
kelvin-onlabb9408212015-04-01 13:34:04 -07001474 if "/" in ingressDevice:
1475 cmd += " " + str( ingressDevice )
1476 else:
1477 if not portIngress:
1478 main.log.error( "You must specify " +
1479 "the Ingress port" )
1480 return main.FALSE
1481
1482 cmd += " " +\
1483 str( ingressDevice ) + "/" +\
1484 str( portIngress )
1485
1486 if portEgressList is None:
1487 for egressDevice in egressDeviceList:
1488 if "/" in egressDevice:
1489 cmd += " " + str( egressDevice )
1490 else:
1491 main.log.error( "You must specify " +
1492 "the egress port" )
1493 # TODO: perhaps more meaningful return
1494 return main.FALSE
1495 else:
1496 if len( egressDeviceList ) == len( portEgressList ):
Jon Hall08f61bc2015-04-13 16:00:30 -07001497 for egressDevice, portEgress in zip( egressDeviceList,
1498 portEgressList ):
kelvin-onlabb9408212015-04-01 13:34:04 -07001499 cmd += " " + \
1500 str( egressDevice ) + "/" +\
1501 str( portEgress )
kelvin-onlab38143812015-04-01 15:03:01 -07001502 else:
Jon Hall08f61bc2015-04-13 16:00:30 -07001503 main.log.error( "Device list and port list does not " +
1504 "have the same length" )
kelvin-onlab38143812015-04-01 15:03:01 -07001505 return main.FALSE
kelvin-onlabb9408212015-04-01 13:34:04 -07001506 handle = self.sendline( cmd )
1507 # If error, return error message
1508 if re.search( "Error", handle ):
1509 main.log.error( "Error in adding singlepoint-to-multipoint " +
1510 "intent" )
shahshreyac2f97072015-03-19 17:04:29 -07001511 return None
kelvin-onlabb9408212015-04-01 13:34:04 -07001512 else:
1513 match = re.search('id=0x([\da-f]+),', handle)
1514 if match:
1515 return match.group()[3:-1]
1516 else:
1517 main.log.error( "Error, intent ID not found" )
1518 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001519 except TypeError:
1520 main.log.exception( self.name + ": Object not as expected" )
1521 return None
shahshreyad0c80432014-12-04 16:56:05 -08001522 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001523 main.log.error( self.name + ": EOF exception found" )
1524 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001525 main.cleanup()
1526 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001527 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001528 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001529 main.cleanup()
1530 main.exit()
1531
Hari Krishna9e232602015-04-13 17:29:08 -07001532 def addMplsIntent(
1533 self,
1534 ingressDevice,
1535 egressDevice,
Hari Krishna87a17f12015-04-13 17:42:23 -07001536 ingressPort="",
1537 egressPort="",
Hari Krishna9e232602015-04-13 17:29:08 -07001538 ethType="",
1539 ethSrc="",
1540 ethDst="",
1541 bandwidth="",
1542 lambdaAlloc=False,
1543 ipProto="",
1544 ipSrc="",
1545 ipDst="",
1546 tcpSrc="",
1547 tcpDst="",
Hari Krishna87a17f12015-04-13 17:42:23 -07001548 ingressLabel="",
Hari Krishnadfff6672015-04-13 17:53:27 -07001549 egressLabel="",
Hari Krishna9e232602015-04-13 17:29:08 -07001550 priority=""):
1551 """
1552 Required:
1553 * ingressDevice: device id of ingress device
1554 * egressDevice: device id of egress device
1555 Optional:
1556 * ethType: specify ethType
1557 * ethSrc: specify ethSrc ( i.e. src mac addr )
1558 * ethDst: specify ethDst ( i.e. dst mac addr )
1559 * bandwidth: specify bandwidth capacity of link
1560 * lambdaAlloc: if True, intent will allocate lambda
1561 for the specified intent
1562 * ipProto: specify ip protocol
1563 * ipSrc: specify ip source address
1564 * ipDst: specify ip destination address
1565 * tcpSrc: specify tcp source port
1566 * tcpDst: specify tcp destination port
1567 * ingressLabel: Ingress MPLS label
1568 * egressLabel: Egress MPLS label
1569 Description:
1570 Adds MPLS intent by
1571 specifying device id's and optional fields
1572 Returns:
1573 A string of the intent id or None on error
1574
1575 NOTE: This function may change depending on the
1576 options developers provide for MPLS
1577 intent via cli
1578 """
1579 try:
1580 # If there are no optional arguments
1581 if not ethType and not ethSrc and not ethDst\
1582 and not bandwidth and not lambdaAlloc \
1583 and not ipProto and not ipSrc and not ipDst \
1584 and not tcpSrc and not tcpDst and not ingressLabel \
1585 and not egressLabel:
1586 cmd = "add-mpls-intent"
1587
1588 else:
1589 cmd = "add-mpls-intent"
1590
1591 if ethType:
1592 cmd += " --ethType " + str( ethType )
1593 if ethSrc:
1594 cmd += " --ethSrc " + str( ethSrc )
1595 if ethDst:
1596 cmd += " --ethDst " + str( ethDst )
1597 if bandwidth:
1598 cmd += " --bandwidth " + str( bandwidth )
1599 if lambdaAlloc:
1600 cmd += " --lambda "
1601 if ipProto:
1602 cmd += " --ipProto " + str( ipProto )
1603 if ipSrc:
1604 cmd += " --ipSrc " + str( ipSrc )
1605 if ipDst:
1606 cmd += " --ipDst " + str( ipDst )
1607 if tcpSrc:
1608 cmd += " --tcpSrc " + str( tcpSrc )
1609 if tcpDst:
1610 cmd += " --tcpDst " + str( tcpDst )
1611 if ingressLabel:
1612 cmd += " --ingressLabel " + str( ingressLabel )
1613 if egressLabel:
1614 cmd += " --egressLabel " + str( egressLabel )
1615 if priority:
1616 cmd += " --priority " + str( priority )
1617
1618 # Check whether the user appended the port
1619 # or provided it as an input
1620 if "/" in ingressDevice:
1621 cmd += " " + str( ingressDevice )
1622 else:
Hari Krishna87a17f12015-04-13 17:42:23 -07001623 if not ingressPort:
Hari Krishna9e232602015-04-13 17:29:08 -07001624 main.log.error( "You must specify the ingress port" )
1625 return None
1626
1627 cmd += " " + \
1628 str( ingressDevice ) + "/" +\
Hari Krishna87a17f12015-04-13 17:42:23 -07001629 str( ingressPort ) + " "
Hari Krishna9e232602015-04-13 17:29:08 -07001630
1631 if "/" in egressDevice:
1632 cmd += " " + str( egressDevice )
1633 else:
Hari Krishna87a17f12015-04-13 17:42:23 -07001634 if not egressPort:
Hari Krishna9e232602015-04-13 17:29:08 -07001635 main.log.error( "You must specify the egress port" )
1636 return None
1637
1638 cmd += " " +\
1639 str( egressDevice ) + "/" +\
Hari Krishna87a17f12015-04-13 17:42:23 -07001640 str( egressPort )
Hari Krishna9e232602015-04-13 17:29:08 -07001641
1642 handle = self.sendline( cmd )
1643 # If error, return error message
1644 if re.search( "Error", handle ):
1645 main.log.error( "Error in adding mpls intent" )
1646 return None
1647 else:
1648 # TODO: print out all the options in this message?
1649 main.log.info( "MPLS intent installed between " +
1650 str( ingressDevice ) + " and " +
1651 str( egressDevice ) )
1652 match = re.search('id=0x([\da-f]+),', handle)
1653 if match:
1654 return match.group()[3:-1]
1655 else:
1656 main.log.error( "Error, intent ID not found" )
1657 return None
1658 except TypeError:
1659 main.log.exception( self.name + ": Object not as expected" )
1660 return None
1661 except pexpect.EOF:
1662 main.log.error( self.name + ": EOF exception found" )
1663 main.log.error( self.name + ": " + self.handle.before )
1664 main.cleanup()
1665 main.exit()
1666 except Exception:
1667 main.log.exception( self.name + ": Uncaught exception!" )
1668 main.cleanup()
1669 main.exit()
1670
Jon Hallefbd9792015-03-05 16:11:36 -08001671 def removeIntent( self, intentId, app='org.onosproject.cli',
1672 purge=False, sync=False ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001673 """
shahshreya1c818fc2015-02-26 13:44:08 -08001674 Remove intent for specified application id and intent id
Jon Hall61282e32015-03-19 11:34:11 -07001675 Optional args:-
shahshreya1c818fc2015-02-26 13:44:08 -08001676 -s or --sync: Waits for the removal before returning
Jon Hall61282e32015-03-19 11:34:11 -07001677 -p or --purge: Purge the intent from the store after removal
1678
Jon Halle3f39ff2015-01-13 11:50:53 -08001679 Returns:
1680 main.False on error and
1681 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001682 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001683 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07001684 cmdStr = "remove-intent"
shahshreya1c818fc2015-02-26 13:44:08 -08001685 if purge:
1686 cmdStr += " -p"
1687 if sync:
1688 cmdStr += " -s"
1689
1690 cmdStr += " " + app + " " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001691 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001692 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001693 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001694 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001695 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001696 # TODO: Should this be main.TRUE
1697 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001698 except TypeError:
1699 main.log.exception( self.name + ": Object not as expected" )
1700 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001701 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001702 main.log.error( self.name + ": EOF exception found" )
1703 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001704 main.cleanup()
1705 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001706 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001707 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001708 main.cleanup()
1709 main.exit()
1710
Hari Krishnaacabd5a2015-07-01 17:10:19 -07001711 def purgeWithdrawnIntents( self ):
Hari Krishna0ce0e152015-06-23 09:55:29 -07001712 """
1713 Purges all WITHDRAWN Intents
1714 """
1715 try:
1716 cmdStr = "purge-intents"
1717 handle = self.sendline( cmdStr )
1718 if re.search( "Error", handle ):
1719 main.log.error( "Error in purging intents" )
1720 return main.FALSE
1721 else:
1722 return main.TRUE
1723 except TypeError:
1724 main.log.exception( self.name + ": Object not as expected" )
1725 return None
1726 except pexpect.EOF:
1727 main.log.error( self.name + ": EOF exception found" )
1728 main.log.error( self.name + ": " + self.handle.before )
1729 main.cleanup()
1730 main.exit()
1731 except Exception:
1732 main.log.exception( self.name + ": Uncaught exception!" )
1733 main.cleanup()
1734 main.exit()
1735
kelvin-onlabd3b64892015-01-20 13:26:24 -08001736 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001737 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001738 NOTE: This method should be used after installing application:
1739 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001740 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001741 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001742 Description:
1743 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001744 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001745 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07001746 cmdStr = "routes"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001747 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07001748 cmdStr += " -j"
1749 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001750 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001751 except TypeError:
1752 main.log.exception( self.name + ": Object not as expected" )
1753 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001754 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001755 main.log.error( self.name + ": EOF exception found" )
1756 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001757 main.cleanup()
1758 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001759 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001760 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001761 main.cleanup()
1762 main.exit()
1763
pingping-lin54b03372015-08-13 14:43:10 -07001764 def ipv4RouteNumber( self ):
1765 """
1766 NOTE: This method should be used after installing application:
1767 onos-app-sdnip
1768 Description:
1769 Obtain the total IPv4 routes number in the system
1770 """
1771 try:
1772 cmdStr = "routes -s -j"
1773 handle = self.sendline( cmdStr )
1774 jsonResult = json.loads( handle )
1775 return jsonResult['totalRoutes4']
1776
1777 except TypeError:
1778 main.log.exception( self.name + ": Object not as expected" )
1779 return None
1780 except pexpect.EOF:
1781 main.log.error( self.name + ": EOF exception found" )
1782 main.log.error( self.name + ": " + self.handle.before )
1783 main.cleanup()
1784 main.exit()
1785 except Exception:
1786 main.log.exception( self.name + ": Uncaught exception!" )
1787 main.cleanup()
1788 main.exit()
1789
pingping-lin8244a3b2015-09-16 13:36:56 -07001790 def intents( self, jsonFormat = True, summary = False, **intentargs):
kelvin8ec71442015-01-15 16:57:00 -08001791 """
andrewonlab377693f2014-10-21 16:00:30 -04001792 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001793 * jsonFormat: enable output formatting in json
pingping-lin8244a3b2015-09-16 13:36:56 -07001794 * summary: whether only output the intent summary
1795 * type: only output a certain type of intent
1796 This options is valid only when jsonFormat is true and summary is
1797 true
andrewonlabe6745342014-10-17 14:29:13 -04001798 Description:
pingping-lin8244a3b2015-09-16 13:36:56 -07001799 Obtain intents
kelvin-onlab898a6c62015-01-16 14:13:53 -08001800 """
andrewonlabe6745342014-10-17 14:29:13 -04001801 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07001802 cmdStr = "intents"
pingping-lin8244a3b2015-09-16 13:36:56 -07001803 if summary:
1804 cmdStr += " -s"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001805 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07001806 cmdStr += " -j"
1807 handle = self.sendline( cmdStr )
pingping-lin8244a3b2015-09-16 13:36:56 -07001808 args = utilities.parse_args( [ "TYPE" ], **intentargs )
acsmars5b5fbaf2015-09-18 10:38:20 -07001809 if "TYPE" in args.keys():
1810 type = args[ "TYPE" ]
1811 else:
1812 type = ""
pingping-lin8244a3b2015-09-16 13:36:56 -07001813 if jsonFormat and summary and ( type != "" ):
1814 jsonResult = json.loads( handle )
1815 if type in jsonResult.keys():
1816 return jsonResult[ type ]
1817 else:
1818 main.log.error( "unknown TYPE, return all types of intents" )
1819 return handle
1820 else:
1821 return handle
pingping-lin54b03372015-08-13 14:43:10 -07001822
1823 except TypeError:
1824 main.log.exception( self.name + ": Object not as expected" )
1825 return None
1826 except pexpect.EOF:
1827 main.log.error( self.name + ": EOF exception found" )
1828 main.log.error( self.name + ": " + self.handle.before )
1829 main.cleanup()
1830 main.exit()
1831 except Exception:
1832 main.log.exception( self.name + ": Uncaught exception!" )
1833 main.cleanup()
1834 main.exit()
1835
pingping-lin8244a3b2015-09-16 13:36:56 -07001836
kelvin-onlab54400a92015-02-26 18:05:51 -08001837 def getIntentState(self, intentsId, intentsJson=None):
1838 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001839 Check intent state.
1840 Accepts a single intent ID (string type) or a list of intent IDs.
1841 Returns the state(string type) of the id if a single intent ID is
1842 accepted.
Jon Hallefbd9792015-03-05 16:11:36 -08001843 Returns a dictionary with intent IDs as the key and its
1844 corresponding states as the values
kelvin-onlabfb521662015-02-27 09:52:40 -08001845 Parameters:
kelvin-onlab54400a92015-02-26 18:05:51 -08001846 intentId: intent ID (string type)
1847 intentsJson: parsed json object from the onos:intents api
1848 Returns:
1849 state = An intent's state- INSTALL,WITHDRAWN etc.
1850 stateDict = Dictionary of intent's state. intent ID as the keys and
1851 state as the values.
1852 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001853 try:
1854 state = "State is Undefined"
1855 if not intentsJson:
Jon Hallefbd9792015-03-05 16:11:36 -08001856 intentsJsonTemp = json.loads( self.intents() )
kelvin-onlab54400a92015-02-26 18:05:51 -08001857 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001858 intentsJsonTemp = json.loads( intentsJson )
1859 if isinstance( intentsId, types.StringType ):
kelvin-onlab54400a92015-02-26 18:05:51 -08001860 for intent in intentsJsonTemp:
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001861 if intentsId == intent[ 'id' ]:
1862 state = intent[ 'state' ]
kelvin-onlab54400a92015-02-26 18:05:51 -08001863 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001864 main.log.info( "Cannot find intent ID" + str( intentsId ) +
1865 " on the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001866 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001867 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001868 dictList = []
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001869 for i in xrange( len( intentsId ) ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001870 stateDict = {}
kelvin-onlab54400a92015-02-26 18:05:51 -08001871 for intents in intentsJsonTemp:
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001872 if intentsId[ i ] == intents[ 'id' ]:
1873 stateDict[ 'state' ] = intents[ 'state' ]
1874 stateDict[ 'id' ] = intentsId[ i ]
Jon Hallefbd9792015-03-05 16:11:36 -08001875 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08001876 break
Jon Hallefbd9792015-03-05 16:11:36 -08001877 if len( intentsId ) != len( dictList ):
1878 main.log.info( "Cannot find some of the intent ID state" )
kelvin-onlab07dbd012015-03-04 16:29:39 -08001879 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08001880 else:
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001881 main.log.info( "Invalid intents ID entry" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001882 return None
kelvin-onlab54400a92015-02-26 18:05:51 -08001883 except TypeError:
1884 main.log.exception( self.name + ": Object not as expected" )
1885 return None
1886 except pexpect.EOF:
1887 main.log.error( self.name + ": EOF exception found" )
1888 main.log.error( self.name + ": " + self.handle.before )
1889 main.cleanup()
1890 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001891 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08001892 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001893 main.cleanup()
1894 main.exit()
Jon Hall390696c2015-05-05 17:13:41 -07001895
kelvin-onlabf512e942015-06-08 19:42:59 -07001896 def checkIntentState( self, intentsId, expectedState='INSTALLED' ):
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001897 """
1898 Description:
1899 Check intents state
1900 Required:
1901 intentsId - List of intents ID to be checked
1902 Optional:
kelvin-onlabf512e942015-06-08 19:42:59 -07001903 expectedState - Check the expected state(s) of each intents
1904 state in the list.
1905 *NOTE: You can pass in a list of expected state,
1906 Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ]
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001907 Return:
kelvin-onlabf512e942015-06-08 19:42:59 -07001908 Returns main.TRUE only if all intent are the same as expected states
1909 , otherwise, returns main.FALSE.
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001910 """
1911 try:
1912 # Generating a dictionary: intent id as a key and state as value
kelvin-onlabf512e942015-06-08 19:42:59 -07001913 returnValue = main.TRUE
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001914 intentsDict = self.getIntentState( intentsId )
kelvin-onlabf512e942015-06-08 19:42:59 -07001915
Jon Hall390696c2015-05-05 17:13:41 -07001916 #print "len of intentsDict ", str( len( intentsDict ) )
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001917 if len( intentsId ) != len( intentsDict ):
1918 main.log.info( self.name + "There is something wrong " +
1919 "getting intents state" )
1920 return main.FALSE
kelvin-onlabf512e942015-06-08 19:42:59 -07001921
1922 if isinstance( expectedState, types.StringType ):
1923 for intents in intentsDict:
1924 if intents.get( 'state' ) != expectedState:
kelvin-onlaba297c4d2015-06-01 13:53:55 -07001925 main.log.debug( self.name + " : Intent ID - " +
1926 intents.get( 'id' ) +
kelvin-onlabf512e942015-06-08 19:42:59 -07001927 " actual state = " +
1928 intents.get( 'state' )
1929 + " does not equal expected state = "
1930 + expectedState )
kelvin-onlaba297c4d2015-06-01 13:53:55 -07001931 returnValue = main.FALSE
kelvin-onlabf512e942015-06-08 19:42:59 -07001932
1933 elif isinstance( expectedState, types.ListType ):
1934 for intents in intentsDict:
1935 if not any( state == intents.get( 'state' ) for state in
1936 expectedState ):
1937 main.log.debug( self.name + " : Intent ID - " +
1938 intents.get( 'id' ) +
1939 " actual state = " +
1940 intents.get( 'state' ) +
1941 " does not equal expected states = "
1942 + str( expectedState ) )
1943 returnValue = main.FALSE
1944
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001945 if returnValue == main.TRUE:
1946 main.log.info( self.name + ": All " +
1947 str( len( intentsDict ) ) +
kelvin-onlabf512e942015-06-08 19:42:59 -07001948 " intents are in " + str( expectedState ) +
1949 " state" )
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001950 return returnValue
1951 except TypeError:
1952 main.log.exception( self.name + ": Object not as expected" )
1953 return None
1954 except pexpect.EOF:
1955 main.log.error( self.name + ": EOF exception found" )
1956 main.log.error( self.name + ": " + self.handle.before )
1957 main.cleanup()
1958 main.exit()
1959 except Exception:
1960 main.log.exception( self.name + ": Uncaught exception!" )
1961 main.cleanup()
1962 main.exit()
andrewonlabe6745342014-10-17 14:29:13 -04001963
kelvin-onlabd3b64892015-01-20 13:26:24 -08001964 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001965 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001966 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001967 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001968 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001969 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001970 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001971 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07001972 cmdStr = "flows"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001973 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07001974 cmdStr += " -j"
1975 handle = self.sendline( cmdStr )
Jon Hall61282e32015-03-19 11:34:11 -07001976 if re.search( "Error:", handle ):
kelvin-onlaba297c4d2015-06-01 13:53:55 -07001977 main.log.error( self.name + ": flows() response: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001978 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001979 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001980 except TypeError:
1981 main.log.exception( self.name + ": Object not as expected" )
1982 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001983 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001984 main.log.error( self.name + ": EOF exception found" )
1985 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001986 main.cleanup()
1987 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001988 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001989 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001990 main.cleanup()
1991 main.exit()
1992
kelvin-onlab4df89f22015-04-13 18:10:23 -07001993 def checkFlowsState( self ):
1994 """
1995 Description:
1996 Check the if all the current flows are in ADDED state or
1997 PENDING_ADD state
1998 Return:
1999 returnValue - Returns main.TRUE only if all flows are in
2000 ADDED state or PENDING_ADD, return main.FALSE
2001 otherwise.
2002 """
2003 try:
2004 tempFlows = json.loads( self.flows() )
kelvin-onlabf0594d72015-05-19 17:25:12 -07002005 #print tempFlows[0]
kelvin-onlab4df89f22015-04-13 18:10:23 -07002006 returnValue = main.TRUE
kelvin-onlabf0594d72015-05-19 17:25:12 -07002007
kelvin-onlab4df89f22015-04-13 18:10:23 -07002008 for device in tempFlows:
2009 for flow in device.get( 'flows' ):
2010 if flow.get( 'state' ) != 'ADDED' and flow.get( 'state' ) != \
2011 'PENDING_ADD':
kelvin-onlabf2ec6e02015-05-27 14:15:28 -07002012
kelvin-onlab4df89f22015-04-13 18:10:23 -07002013 main.log.info( self.name + ": flow Id: " +
kelvin-onlabf2ec6e02015-05-27 14:15:28 -07002014 str( flow.get( 'groupId' ) ) +
2015 " | state:" +
2016 str( flow.get( 'state' ) ) )
kelvin-onlab4df89f22015-04-13 18:10:23 -07002017 returnValue = main.FALSE
kelvin-onlabf0594d72015-05-19 17:25:12 -07002018
kelvin-onlab4df89f22015-04-13 18:10:23 -07002019 return returnValue
2020 except TypeError:
2021 main.log.exception( self.name + ": Object not as expected" )
2022 return None
2023 except pexpect.EOF:
2024 main.log.error( self.name + ": EOF exception found" )
2025 main.log.error( self.name + ": " + self.handle.before )
2026 main.cleanup()
2027 main.exit()
2028 except Exception:
2029 main.log.exception( self.name + ": Uncaught exception!" )
2030 main.cleanup()
2031 main.exit()
2032
kelvin-onlabd3b64892015-01-20 13:26:24 -08002033 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
Jon Hallefbd9792015-03-05 16:11:36 -08002034 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08002035 """
andrewonlab87852b02014-11-19 18:44:19 -05002036 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08002037 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05002038 a specific point-to-point intent definition
2039 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002040 * dpidSrc: specify source dpid
2041 * dpidDst: specify destination dpid
2042 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05002043 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002044 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05002045 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08002046 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05002047 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05002048 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08002049 """
andrewonlab87852b02014-11-19 18:44:19 -05002050 try:
kelvin8ec71442015-01-15 16:57:00 -08002051 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08002052 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
2053 str( numIntents )
2054 if numMult:
2055 cmd += " " + str( numMult )
2056 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08002057 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08002058 if appId:
2059 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002060 handle = self.sendline( cmd )
andrewonlab87852b02014-11-19 18:44:19 -05002061 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002062 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08002063 main.log.info( handle )
2064 # Split result by newline
2065 newline = handle.split( "\r\r\n" )
2066 # Ignore the first object of list, which is empty
2067 newline = newline[ 1: ]
2068 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05002069 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08002070 result = result.split( ": " )
2071 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08002072 latResult.append( result[ 1 ].split( " " )[ 0 ] )
2073 main.log.info( latResult )
2074 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05002075 else:
2076 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08002077 except TypeError:
2078 main.log.exception( self.name + ": Object not as expected" )
2079 return None
andrewonlab87852b02014-11-19 18:44:19 -05002080 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002081 main.log.error( self.name + ": EOF exception found" )
2082 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05002083 main.cleanup()
2084 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002085 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002086 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05002087 main.cleanup()
2088 main.exit()
2089
kelvin-onlabd3b64892015-01-20 13:26:24 -08002090 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08002091 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002092 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002093 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002094 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08002095 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002096 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002097 cmdStr = "intents-events-metrics"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002098 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002099 cmdStr += " -j"
2100 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002101 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08002102 except TypeError:
2103 main.log.exception( self.name + ": Object not as expected" )
2104 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002105 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002106 main.log.error( self.name + ": EOF exception found" )
2107 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002108 main.cleanup()
2109 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002110 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002111 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002112 main.cleanup()
2113 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04002114
kelvin-onlabd3b64892015-01-20 13:26:24 -08002115 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08002116 """
2117 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04002118 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002119 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08002120 """
andrewonlab867212a2014-10-22 20:13:38 -04002121 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002122 cmdStr = "topology-events-metrics"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002123 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002124 cmdStr += " -j"
2125 handle = self.sendline( cmdStr )
jenkins7ead5a82015-03-13 10:28:21 -07002126 if handle:
2127 return handle
Jon Hallc6358dd2015-04-10 12:44:28 -07002128 elif jsonFormat:
Jon Hallbe379602015-03-24 13:39:32 -07002129 # Return empty json
jenkins7ead5a82015-03-13 10:28:21 -07002130 return '{}'
Jon Hallc6358dd2015-04-10 12:44:28 -07002131 else:
2132 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08002133 except TypeError:
2134 main.log.exception( self.name + ": Object not as expected" )
2135 return None
andrewonlab867212a2014-10-22 20:13:38 -04002136 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002137 main.log.error( self.name + ": EOF exception found" )
2138 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04002139 main.cleanup()
2140 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002141 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002142 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04002143 main.cleanup()
2144 main.exit()
2145
kelvin8ec71442015-01-15 16:57:00 -08002146 # Wrapper functions ****************
2147 # Wrapper functions use existing driver
2148 # functions and extends their use case.
2149 # For example, we may use the output of
2150 # a normal driver function, and parse it
2151 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04002152
kelvin-onlabd3b64892015-01-20 13:26:24 -08002153 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08002154 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04002155 Description:
2156 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08002157 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04002158 try:
kelvin8ec71442015-01-15 16:57:00 -08002159 # Obtain output of intents function
kelvin-onlabfb521662015-02-27 09:52:40 -08002160 intentsStr = self.intents(jsonFormat=False)
kelvin-onlabd3b64892015-01-20 13:26:24 -08002161 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04002162
kelvin8ec71442015-01-15 16:57:00 -08002163 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08002164 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
2165 for intents in intentsList:
kelvin-onlabfb521662015-02-27 09:52:40 -08002166 match = re.search('id=0x([\da-f]+),', intents)
2167 if match:
2168 tmpId = match.group()[3:-1]
2169 intentIdList.append( tmpId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002170 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04002171
Jon Halld4d4b372015-01-28 16:02:41 -08002172 except TypeError:
2173 main.log.exception( self.name + ": Object not as expected" )
2174 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04002175 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002176 main.log.error( self.name + ": EOF exception found" )
2177 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04002178 main.cleanup()
2179 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002180 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002181 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04002182 main.cleanup()
2183 main.exit()
2184
Jon Hall30b82fa2015-03-04 17:15:43 -08002185 def FlowAddedCount( self, deviceId ):
2186 """
2187 Determine the number of flow rules for the given device id that are
2188 in the added state
2189 """
2190 try:
2191 cmdStr = "flows any " + str( deviceId ) + " | " +\
2192 "grep 'state=ADDED' | wc -l"
2193 handle = self.sendline( cmdStr )
2194 return handle
2195 except pexpect.EOF:
2196 main.log.error( self.name + ": EOF exception found" )
2197 main.log.error( self.name + ": " + self.handle.before )
2198 main.cleanup()
2199 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002200 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -08002201 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -04002202 main.cleanup()
2203 main.exit()
2204
kelvin-onlabd3b64892015-01-20 13:26:24 -08002205 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08002206 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04002207 Use 'devices' function to obtain list of all devices
2208 and parse the result to obtain a list of all device
2209 id's. Returns this list. Returns empty list if no
2210 devices exist
kelvin8ec71442015-01-15 16:57:00 -08002211 List is ordered sequentially
2212
andrewonlab3e15ead2014-10-15 14:21:34 -04002213 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08002214 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04002215 the ids. By obtaining the list of device ids on the fly,
2216 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08002217 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04002218 try:
kelvin8ec71442015-01-15 16:57:00 -08002219 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08002220 devicesStr = self.devices( jsonFormat=False )
2221 idList = []
kelvin8ec71442015-01-15 16:57:00 -08002222
kelvin-onlabd3b64892015-01-20 13:26:24 -08002223 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08002224 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002225 return idList
kelvin8ec71442015-01-15 16:57:00 -08002226
2227 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08002228 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08002229 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08002230 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08002231 # Split list further into arguments before and after string
2232 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08002233 # append to idList
2234 for arg in tempList:
2235 idList.append( arg.split( "id=" )[ 1 ] )
2236 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04002237
Jon Halld4d4b372015-01-28 16:02:41 -08002238 except TypeError:
2239 main.log.exception( self.name + ": Object not as expected" )
2240 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04002241 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002242 main.log.error( self.name + ": EOF exception found" )
2243 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04002244 main.cleanup()
2245 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002246 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002247 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04002248 main.cleanup()
2249 main.exit()
2250
kelvin-onlabd3b64892015-01-20 13:26:24 -08002251 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08002252 """
andrewonlab7c211572014-10-15 16:45:20 -04002253 Uses 'nodes' function to obtain list of all nodes
2254 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08002255 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04002256 Returns:
2257 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08002258 """
andrewonlab7c211572014-10-15 16:45:20 -04002259 try:
Jon Hall5aa168b2015-03-23 14:23:09 -07002260 nodesStr = self.nodes( jsonFormat=True )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002261 idList = []
Jon Hall5aa168b2015-03-23 14:23:09 -07002262 # Sample nodesStr output
2263 # id=local, address=127.0.0.1:9876, state=ACTIVE *
kelvin-onlabd3b64892015-01-20 13:26:24 -08002264 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08002265 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002266 return idList
Jon Hall5aa168b2015-03-23 14:23:09 -07002267 nodesJson = json.loads( nodesStr )
2268 idList = [ node.get('id') for node in nodesJson ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08002269 return idList
kelvin8ec71442015-01-15 16:57:00 -08002270
Jon Halld4d4b372015-01-28 16:02:41 -08002271 except TypeError:
2272 main.log.exception( self.name + ": Object not as expected" )
2273 return None
andrewonlab7c211572014-10-15 16:45:20 -04002274 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002275 main.log.error( self.name + ": EOF exception found" )
2276 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04002277 main.cleanup()
2278 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002279 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002280 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04002281 main.cleanup()
2282 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04002283
kelvin-onlabd3b64892015-01-20 13:26:24 -08002284 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08002285 """
Jon Halla91c4dc2014-10-22 12:57:04 -04002286 Return the first device from the devices api whose 'id' contains 'dpid'
2287 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08002288 """
Jon Halla91c4dc2014-10-22 12:57:04 -04002289 try:
kelvin8ec71442015-01-15 16:57:00 -08002290 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04002291 return None
2292 else:
kelvin8ec71442015-01-15 16:57:00 -08002293 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002294 rawDevices = self.devices()
2295 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08002296 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08002297 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08002298 # print "%s in %s?" % ( dpid, device[ 'id' ] )
2299 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04002300 return device
2301 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002302 except TypeError:
2303 main.log.exception( self.name + ": Object not as expected" )
2304 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04002305 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002306 main.log.error( self.name + ": EOF exception found" )
2307 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04002308 main.cleanup()
2309 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002310 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002311 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04002312 main.cleanup()
2313 main.exit()
2314
kelvin-onlabd3b64892015-01-20 13:26:24 -08002315 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08002316 """
Jon Hallefbd9792015-03-05 16:11:36 -08002317 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08002318 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08002319 log level can be specified.
kelvin8ec71442015-01-15 16:57:00 -08002320
Jon Hall42db6dc2014-10-24 19:03:48 -04002321 Params: ip = ip used for the onos cli
2322 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08002323 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08002324 logLevel = level to log to. Currently accepts
2325 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04002326
2327
kelvin-onlabd3b64892015-01-20 13:26:24 -08002328 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04002329
Jon Hallefbd9792015-03-05 16:11:36 -08002330 Returns: main.TRUE if the number of switches and links are correct,
2331 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04002332 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08002333 """
Jon Hall42db6dc2014-10-24 19:03:48 -04002334 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002335 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04002336 if topology == {}:
2337 return main.ERROR
2338 output = ""
kelvin8ec71442015-01-15 16:57:00 -08002339 # Is the number of switches is what we expected
2340 devices = topology.get( 'devices', False )
2341 links = topology.get( 'links', False )
kelvin-onlabfb521662015-02-27 09:52:40 -08002342 if devices is False or links is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04002343 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08002344 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08002345 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08002346 linkCheck = ( int( links ) == int( numolink ) )
2347 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08002348 # We expected the correct numbers
Jon Hallefbd9792015-03-05 16:11:36 -08002349 output += "The number of links and switches match " +\
2350 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04002351 result = main.TRUE
2352 else:
Jon Hallefbd9792015-03-05 16:11:36 -08002353 output += "The number of links and switches does not match " +\
2354 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04002355 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08002356 output = output + "\n ONOS sees %i devices (%i expected) \
2357 and %i links (%i expected)" % (
2358 int( devices ), int( numoswitch ), int( links ),
2359 int( numolink ) )
2360 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08002361 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002362 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08002363 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04002364 else:
Jon Hall390696c2015-05-05 17:13:41 -07002365 main.log.info( self.name + ": " + output )
kelvin8ec71442015-01-15 16:57:00 -08002366 return result
Jon Halld4d4b372015-01-28 16:02:41 -08002367 except TypeError:
2368 main.log.exception( self.name + ": Object not as expected" )
2369 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04002370 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002371 main.log.error( self.name + ": EOF exception found" )
2372 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04002373 main.cleanup()
2374 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002375 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002376 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04002377 main.cleanup()
2378 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002379
kelvin-onlabd3b64892015-01-20 13:26:24 -08002380 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08002381 """
Jon Hall1c9e8732014-10-27 19:29:27 -04002382 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08002383 deviceId must be the id of a device as seen in the onos devices command
2384 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04002385 role must be either master, standby, or none
2386
Jon Halle3f39ff2015-01-13 11:50:53 -08002387 Returns:
2388 main.TRUE or main.FALSE based on argument verification and
2389 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08002390 """
Jon Hall1c9e8732014-10-27 19:29:27 -04002391 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08002392 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04002393 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08002394 cmdStr = "device-role " +\
2395 str( deviceId ) + " " +\
2396 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002397 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002398 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002399 if re.search( "Error", handle ):
2400 # end color output to escape any colours
2401 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08002402 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002403 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08002404 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08002405 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04002406 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002407 main.log.error( "Invalid 'role' given to device_role(). " +
2408 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04002409 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002410 except TypeError:
2411 main.log.exception( self.name + ": Object not as expected" )
2412 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04002413 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002414 main.log.error( self.name + ": EOF exception found" )
2415 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04002416 main.cleanup()
2417 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002418 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002419 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04002420 main.cleanup()
2421 main.exit()
2422
kelvin-onlabd3b64892015-01-20 13:26:24 -08002423 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08002424 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08002425 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08002426 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002427 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08002428 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08002429 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002430 cmdStr = "clusters"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002431 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002432 cmdStr += " -j"
2433 handle = self.sendline( cmdStr )
2434 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08002435 except TypeError:
2436 main.log.exception( self.name + ": Object not as expected" )
2437 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08002438 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002439 main.log.error( self.name + ": EOF exception found" )
2440 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08002441 main.cleanup()
2442 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002443 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002444 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08002445 main.cleanup()
2446 main.exit()
2447
kelvin-onlabd3b64892015-01-20 13:26:24 -08002448 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002449 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002450 CLI command to get the current leader for the Election test application
2451 NOTE: Requires installation of the onos-app-election feature
2452 Returns: Node IP of the leader if one exists
2453 None if none exists
2454 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08002455 """
Jon Hall94fd0472014-12-08 11:52:42 -08002456 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002457 cmdStr = "election-test-leader"
2458 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002459 # Leader
2460 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002461 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08002462 nodeSearch = re.search( leaderPattern, response )
2463 if nodeSearch:
2464 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08002465 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002466 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08002467 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08002468 # no leader
2469 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002470 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002471 nullSearch = re.search( nullPattern, response )
2472 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08002473 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002474 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08002475 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08002476 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002477 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08002478 if re.search( errorPattern, response ):
2479 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08002480 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08002481 return main.FALSE
2482 else:
Jon Hall390696c2015-05-05 17:13:41 -07002483 main.log.error( "Error in electionTestLeader on " + self.name +
2484 ": " + "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08002485 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002486 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002487 except TypeError:
2488 main.log.exception( self.name + ": Object not as expected" )
2489 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002490 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002491 main.log.error( self.name + ": EOF exception found" )
2492 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002493 main.cleanup()
2494 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002495 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002496 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002497 main.cleanup()
2498 main.exit()
2499
kelvin-onlabd3b64892015-01-20 13:26:24 -08002500 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002501 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002502 CLI command to run for leadership of the Election test application.
2503 NOTE: Requires installation of the onos-app-election feature
2504 Returns: Main.TRUE on success
2505 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08002506 """
Jon Hall94fd0472014-12-08 11:52:42 -08002507 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002508 cmdStr = "election-test-run"
2509 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002510 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002511 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002512 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002513 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08002514 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08002515 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002516 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002517 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002518 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002519 errorPattern = "Command\snot\sfound"
2520 if re.search( errorPattern, response ):
2521 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002522 return main.FALSE
2523 else:
Jon Hall390696c2015-05-05 17:13:41 -07002524 main.log.error( "Error in electionTestRun on " + self.name +
2525 ": " + "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002526 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002527 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002528 except TypeError:
2529 main.log.exception( self.name + ": Object not as expected" )
2530 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002531 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002532 main.log.error( self.name + ": EOF exception found" )
2533 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002534 main.cleanup()
2535 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002536 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002537 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002538 main.cleanup()
2539 main.exit()
2540
kelvin-onlabd3b64892015-01-20 13:26:24 -08002541 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08002542 """
Jon Hall94fd0472014-12-08 11:52:42 -08002543 * CLI command to withdraw the local node from leadership election for
2544 * the Election test application.
2545 #NOTE: Requires installation of the onos-app-election feature
2546 Returns: Main.TRUE on success
2547 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08002548 """
Jon Hall94fd0472014-12-08 11:52:42 -08002549 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002550 cmdStr = "election-test-withdraw"
2551 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002552 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002553 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002554 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002555 if re.search( successPattern, response ):
2556 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002557 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002558 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002559 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002560 errorPattern = "Command\snot\sfound"
2561 if re.search( errorPattern, response ):
2562 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002563 return main.FALSE
2564 else:
Jon Hall390696c2015-05-05 17:13:41 -07002565 main.log.error( "Error in electionTestWithdraw on " +
2566 self.name + ": " + "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002567 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002568 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002569 except TypeError:
2570 main.log.exception( self.name + ": Object not as expected" )
2571 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002572 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002573 main.log.error( self.name + ": EOF exception found" )
2574 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002575 main.cleanup()
2576 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002577 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002578 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002579 main.cleanup()
2580 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002581
kelvin8ec71442015-01-15 16:57:00 -08002582 def getDevicePortsEnabledCount( self, dpid ):
2583 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002584 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002585 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002586 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08002587 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002588 cmdStr = "onos:ports -e " + dpid + " | wc -l"
2589 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002590 if re.search( "No such device", output ):
2591 main.log.error( "Error in getting ports" )
2592 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002593 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002594 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002595 except TypeError:
2596 main.log.exception( self.name + ": Object not as expected" )
2597 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002598 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002599 main.log.error( self.name + ": EOF exception found" )
2600 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002601 main.cleanup()
2602 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002603 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002604 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002605 main.cleanup()
2606 main.exit()
2607
kelvin8ec71442015-01-15 16:57:00 -08002608 def getDeviceLinksActiveCount( self, dpid ):
2609 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002610 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002611 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002612 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002613 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002614 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
2615 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002616 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002617 main.log.error( "Error in getting ports " )
2618 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002619 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002620 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002621 except TypeError:
2622 main.log.exception( self.name + ": Object not as expected" )
2623 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002624 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002625 main.log.error( self.name + ": EOF exception found" )
2626 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002627 main.cleanup()
2628 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002629 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002630 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002631 main.cleanup()
2632 main.exit()
2633
kelvin8ec71442015-01-15 16:57:00 -08002634 def getAllIntentIds( self ):
2635 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002636 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002637 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002638 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002639 cmdStr = "onos:intents | grep id="
2640 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002641 if re.search( "Error", output ):
2642 main.log.error( "Error in getting ports" )
2643 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002644 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002645 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002646 except TypeError:
2647 main.log.exception( self.name + ": Object not as expected" )
2648 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002649 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002650 main.log.error( self.name + ": EOF exception found" )
2651 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002652 main.cleanup()
2653 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002654 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002655 main.log.exception( self.name + ": Uncaught exception!" )
2656 main.cleanup()
2657 main.exit()
2658
Jon Hall73509952015-02-24 16:42:56 -08002659 def intentSummary( self ):
2660 """
Jon Hallefbd9792015-03-05 16:11:36 -08002661 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08002662 """
2663 try:
2664 intents = self.intents( )
Jon Hall08f61bc2015-04-13 16:00:30 -07002665 states = []
Jon Hall5aa168b2015-03-23 14:23:09 -07002666 for intent in json.loads( intents ):
Jon Hall08f61bc2015-04-13 16:00:30 -07002667 states.append( intent.get( 'state', None ) )
2668 out = [ ( i, states.count( i ) ) for i in set( states ) ]
Jon Hall63604932015-02-26 17:09:50 -08002669 main.log.info( dict( out ) )
Jon Hall73509952015-02-24 16:42:56 -08002670 return dict( out )
2671 except TypeError:
2672 main.log.exception( self.name + ": Object not as expected" )
2673 return None
2674 except pexpect.EOF:
2675 main.log.error( self.name + ": EOF exception found" )
2676 main.log.error( self.name + ": " + self.handle.before )
2677 main.cleanup()
2678 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002679 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08002680 main.log.exception( self.name + ": Uncaught exception!" )
2681 main.cleanup()
2682 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002683
Jon Hall61282e32015-03-19 11:34:11 -07002684 def leaders( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08002685 """
2686 Returns the output of the leaders command.
Jon Hall61282e32015-03-19 11:34:11 -07002687 Optional argument:
2688 * jsonFormat - boolean indicating if you want output in json
Jon Hall63604932015-02-26 17:09:50 -08002689 """
Jon Hall63604932015-02-26 17:09:50 -08002690 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002691 cmdStr = "onos:leaders"
Jon Hall61282e32015-03-19 11:34:11 -07002692 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002693 cmdStr += " -j"
2694 output = self.sendline( cmdStr )
2695 return output
Jon Hall63604932015-02-26 17:09:50 -08002696 except TypeError:
2697 main.log.exception( self.name + ": Object not as expected" )
2698 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002699 except pexpect.EOF:
2700 main.log.error( self.name + ": EOF exception found" )
2701 main.log.error( self.name + ": " + self.handle.before )
2702 main.cleanup()
2703 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002704 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08002705 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002706 main.cleanup()
2707 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002708
acsmarsa4a4d1e2015-07-10 16:01:24 -07002709 def leaderCandidates( self, jsonFormat=True ):
2710 """
2711 Returns the output of the leaders -c command.
2712 Optional argument:
2713 * jsonFormat - boolean indicating if you want output in json
2714 """
2715 try:
2716 cmdStr = "onos:leaders -c"
2717 if jsonFormat:
2718 cmdStr += " -j"
2719 output = self.sendline( cmdStr )
2720 return output
2721 except TypeError:
2722 main.log.exception( self.name + ": Object not as expected" )
2723 return None
2724 except pexpect.EOF:
2725 main.log.error( self.name + ": EOF exception found" )
2726 main.log.error( self.name + ": " + self.handle.before )
2727 main.cleanup()
2728 main.exit()
2729 except Exception:
2730 main.log.exception( self.name + ": Uncaught exception!" )
2731 main.cleanup()
2732 main.exit()
2733
2734 def specificLeaderCandidate(self,topic):
2735 """
2736 Returns a list in format [leader,candidate1,candidate2,...] for a given
2737 topic parameter and an empty list if the topic doesn't exist
2738 If no leader is elected leader in the returned list will be "none"
2739 Returns None if there is a type error processing the json object
2740 """
2741 try:
2742 cmdStr = "onos:leaders -c -j"
2743 output = self.sendline( cmdStr )
2744 output = json.loads(output)
2745 results = []
2746 for dict in output:
2747 if dict["topic"] == topic:
2748 leader = dict["leader"]
2749 candidates = re.split(", ",dict["candidates"][1:-1])
2750 results.append(leader)
2751 results.extend(candidates)
2752 return results
2753 except TypeError:
2754 main.log.exception( self.name + ": Object not as expected" )
2755 return None
2756 except pexpect.EOF:
2757 main.log.error( self.name + ": EOF exception found" )
2758 main.log.error( self.name + ": " + self.handle.before )
2759 main.cleanup()
2760 main.exit()
2761 except Exception:
2762 main.log.exception( self.name + ": Uncaught exception!" )
2763 main.cleanup()
2764 main.exit()
2765
Jon Hall61282e32015-03-19 11:34:11 -07002766 def pendingMap( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08002767 """
2768 Returns the output of the intent Pending map.
2769 """
Jon Hall63604932015-02-26 17:09:50 -08002770 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002771 cmdStr = "onos:intents -p"
Jon Hall61282e32015-03-19 11:34:11 -07002772 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002773 cmdStr += " -j"
2774 output = self.sendline( cmdStr )
2775 return output
Jon Hall63604932015-02-26 17:09:50 -08002776 except TypeError:
2777 main.log.exception( self.name + ": Object not as expected" )
2778 return None
2779 except pexpect.EOF:
2780 main.log.error( self.name + ": EOF exception found" )
2781 main.log.error( self.name + ": " + self.handle.before )
2782 main.cleanup()
2783 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002784 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08002785 main.log.exception( self.name + ": Uncaught exception!" )
2786 main.cleanup()
2787 main.exit()
2788
Jon Hall61282e32015-03-19 11:34:11 -07002789 def partitions( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08002790 """
2791 Returns the output of the raft partitions command for ONOS.
2792 """
Jon Hall61282e32015-03-19 11:34:11 -07002793 # Sample JSON
2794 # {
2795 # "leader": "tcp://10.128.30.11:7238",
2796 # "members": [
2797 # "tcp://10.128.30.11:7238",
2798 # "tcp://10.128.30.17:7238",
2799 # "tcp://10.128.30.13:7238",
2800 # ],
2801 # "name": "p1",
2802 # "term": 3
2803 # },
Jon Hall63604932015-02-26 17:09:50 -08002804 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002805 cmdStr = "onos:partitions"
Jon Hall61282e32015-03-19 11:34:11 -07002806 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002807 cmdStr += " -j"
2808 output = self.sendline( cmdStr )
2809 return output
Jon Hall63604932015-02-26 17:09:50 -08002810 except TypeError:
2811 main.log.exception( self.name + ": Object not as expected" )
2812 return None
2813 except pexpect.EOF:
2814 main.log.error( self.name + ": EOF exception found" )
2815 main.log.error( self.name + ": " + self.handle.before )
2816 main.cleanup()
2817 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002818 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08002819 main.log.exception( self.name + ": Uncaught exception!" )
2820 main.cleanup()
2821 main.exit()
2822
Jon Hallbe379602015-03-24 13:39:32 -07002823 def apps( self, jsonFormat=True ):
2824 """
2825 Returns the output of the apps command for ONOS. This command lists
2826 information about installed ONOS applications
2827 """
2828 # Sample JSON object
2829 # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0",
2830 # "description":"ONOS OpenFlow protocol southbound providers",
2831 # "origin":"ON.Lab","permissions":"[]","featuresRepo":"",
2832 # "features":"[onos-openflow]","state":"ACTIVE"}]
2833 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002834 cmdStr = "onos:apps"
Jon Hallbe379602015-03-24 13:39:32 -07002835 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002836 cmdStr += " -j"
2837 output = self.sendline( cmdStr )
2838 assert "Error executing command" not in output
2839 return output
Jon Hallbe379602015-03-24 13:39:32 -07002840 # FIXME: look at specific exceptions/Errors
2841 except AssertionError:
2842 main.log.error( "Error in processing onos:app command: " +
2843 str( output ) )
2844 return None
2845 except TypeError:
2846 main.log.exception( self.name + ": Object not as expected" )
2847 return None
2848 except pexpect.EOF:
2849 main.log.error( self.name + ": EOF exception found" )
2850 main.log.error( self.name + ": " + self.handle.before )
2851 main.cleanup()
2852 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002853 except Exception:
Jon Hallbe379602015-03-24 13:39:32 -07002854 main.log.exception( self.name + ": Uncaught exception!" )
2855 main.cleanup()
2856 main.exit()
2857
Jon Hall146f1522015-03-24 15:33:24 -07002858 def appStatus( self, appName ):
2859 """
2860 Uses the onos:apps cli command to return the status of an application.
2861 Returns:
2862 "ACTIVE" - If app is installed and activated
2863 "INSTALLED" - If app is installed and deactivated
2864 "UNINSTALLED" - If app is not installed
2865 None - on error
2866 """
Jon Hall146f1522015-03-24 15:33:24 -07002867 try:
2868 if not isinstance( appName, types.StringType ):
2869 main.log.error( self.name + ".appStatus(): appName must be" +
2870 " a string" )
2871 return None
2872 output = self.apps( jsonFormat=True )
2873 appsJson = json.loads( output )
2874 state = None
2875 for app in appsJson:
2876 if appName == app.get('name'):
2877 state = app.get('state')
2878 break
2879 if state == "ACTIVE" or state == "INSTALLED":
2880 return state
2881 elif state is None:
2882 return "UNINSTALLED"
2883 elif state:
2884 main.log.error( "Unexpected state from 'onos:apps': " +
2885 str( state ) )
2886 return state
2887 except TypeError:
2888 main.log.exception( self.name + ": Object not as expected" )
2889 return None
2890 except pexpect.EOF:
2891 main.log.error( self.name + ": EOF exception found" )
2892 main.log.error( self.name + ": " + self.handle.before )
2893 main.cleanup()
2894 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002895 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07002896 main.log.exception( self.name + ": Uncaught exception!" )
2897 main.cleanup()
2898 main.exit()
2899
Jon Hallbe379602015-03-24 13:39:32 -07002900 def app( self, appName, option ):
2901 """
2902 Interacts with the app command for ONOS. This command manages
2903 application inventory.
2904 """
Jon Hallbe379602015-03-24 13:39:32 -07002905 try:
Jon Hallbd16b922015-03-26 17:53:15 -07002906 # Validate argument types
2907 valid = True
2908 if not isinstance( appName, types.StringType ):
2909 main.log.error( self.name + ".app(): appName must be a " +
2910 "string" )
2911 valid = False
2912 if not isinstance( option, types.StringType ):
2913 main.log.error( self.name + ".app(): option must be a string" )
2914 valid = False
2915 if not valid:
2916 return main.FALSE
2917 # Validate Option
2918 option = option.lower()
2919 # NOTE: Install may become a valid option
2920 if option == "activate":
2921 pass
2922 elif option == "deactivate":
2923 pass
2924 elif option == "uninstall":
2925 pass
2926 else:
2927 # Invalid option
2928 main.log.error( "The ONOS app command argument only takes " +
2929 "the values: (activate|deactivate|uninstall)" +
2930 "; was given '" + option + "'")
2931 return main.FALSE
Jon Hall146f1522015-03-24 15:33:24 -07002932 cmdStr = "onos:app " + option + " " + appName
Jon Hallbe379602015-03-24 13:39:32 -07002933 output = self.sendline( cmdStr )
Jon Hallbe379602015-03-24 13:39:32 -07002934 if "Error executing command" in output:
2935 main.log.error( "Error in processing onos:app command: " +
2936 str( output ) )
Jon Hall146f1522015-03-24 15:33:24 -07002937 return main.FALSE
Jon Hallbe379602015-03-24 13:39:32 -07002938 elif "No such application" in output:
2939 main.log.error( "The application '" + appName +
2940 "' is not installed in ONOS" )
Jon Hall146f1522015-03-24 15:33:24 -07002941 return main.FALSE
2942 elif "Command not found:" in output:
2943 main.log.error( "Error in processing onos:app command: " +
2944 str( output ) )
2945 return main.FALSE
Jon Hallbd16b922015-03-26 17:53:15 -07002946 elif "Unsupported command:" in output:
2947 main.log.error( "Incorrect command given to 'app': " +
2948 str( output ) )
Jon Hallbe379602015-03-24 13:39:32 -07002949 # NOTE: we may need to add more checks here
Jon Hallbd16b922015-03-26 17:53:15 -07002950 # else: Command was successful
Jon Hall08f61bc2015-04-13 16:00:30 -07002951 # main.log.debug( "app response: " + repr( output ) )
Jon Hallbe379602015-03-24 13:39:32 -07002952 return main.TRUE
2953 except TypeError:
2954 main.log.exception( self.name + ": Object not as expected" )
2955 return main.ERROR
2956 except pexpect.EOF:
2957 main.log.error( self.name + ": EOF exception found" )
2958 main.log.error( self.name + ": " + self.handle.before )
2959 main.cleanup()
2960 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002961 except Exception:
Jon Hallbe379602015-03-24 13:39:32 -07002962 main.log.exception( self.name + ": Uncaught exception!" )
2963 main.cleanup()
2964 main.exit()
Jon Hall146f1522015-03-24 15:33:24 -07002965
Jon Hallbd16b922015-03-26 17:53:15 -07002966 def activateApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07002967 """
2968 Activate an app that is already installed in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07002969 appName is the hierarchical app name, not the feature name
2970 If check is True, method will check the status of the app after the
2971 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07002972 Returns main.TRUE if the command was successfully sent
2973 main.FALSE if the cli responded with an error or given
2974 incorrect input
2975 """
2976 try:
2977 if not isinstance( appName, types.StringType ):
2978 main.log.error( self.name + ".activateApp(): appName must be" +
2979 " a string" )
2980 return main.FALSE
2981 status = self.appStatus( appName )
2982 if status == "INSTALLED":
2983 response = self.app( appName, "activate" )
Jon Hallbd16b922015-03-26 17:53:15 -07002984 if check and response == main.TRUE:
2985 for i in range(10): # try 10 times then give up
2986 # TODO: Check with Thomas about this delay
2987 status = self.appStatus( appName )
2988 if status == "ACTIVE":
2989 return main.TRUE
2990 else:
Jon Hall050e1bd2015-03-30 13:33:02 -07002991 main.log.debug( "The state of application " +
2992 appName + " is " + status )
Jon Hallbd16b922015-03-26 17:53:15 -07002993 time.sleep( 1 )
2994 return main.FALSE
2995 else: # not 'check' or command didn't succeed
2996 return response
Jon Hall146f1522015-03-24 15:33:24 -07002997 elif status == "ACTIVE":
2998 return main.TRUE
2999 elif status == "UNINSTALLED":
3000 main.log.error( self.name + ": Tried to activate the " +
3001 "application '" + appName + "' which is not " +
3002 "installed." )
3003 else:
3004 main.log.error( "Unexpected return value from appStatus: " +
3005 str( status ) )
3006 return main.ERROR
3007 except TypeError:
3008 main.log.exception( self.name + ": Object not as expected" )
3009 return main.ERROR
3010 except pexpect.EOF:
3011 main.log.error( self.name + ": EOF exception found" )
3012 main.log.error( self.name + ": " + self.handle.before )
3013 main.cleanup()
3014 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003015 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003016 main.log.exception( self.name + ": Uncaught exception!" )
3017 main.cleanup()
3018 main.exit()
3019
Jon Hallbd16b922015-03-26 17:53:15 -07003020 def deactivateApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07003021 """
3022 Deactivate an app that is already activated in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07003023 appName is the hierarchical app name, not the feature name
3024 If check is True, method will check the status of the app after the
3025 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07003026 Returns main.TRUE if the command was successfully sent
3027 main.FALSE if the cli responded with an error or given
3028 incorrect input
3029 """
3030 try:
3031 if not isinstance( appName, types.StringType ):
3032 main.log.error( self.name + ".deactivateApp(): appName must " +
3033 "be a string" )
3034 return main.FALSE
3035 status = self.appStatus( appName )
3036 if status == "INSTALLED":
3037 return main.TRUE
3038 elif status == "ACTIVE":
3039 response = self.app( appName, "deactivate" )
Jon Hallbd16b922015-03-26 17:53:15 -07003040 if check and response == main.TRUE:
3041 for i in range(10): # try 10 times then give up
3042 status = self.appStatus( appName )
3043 if status == "INSTALLED":
3044 return main.TRUE
3045 else:
3046 time.sleep( 1 )
3047 return main.FALSE
3048 else: # not check or command didn't succeed
3049 return response
Jon Hall146f1522015-03-24 15:33:24 -07003050 elif status == "UNINSTALLED":
3051 main.log.warn( self.name + ": Tried to deactivate the " +
3052 "application '" + appName + "' which is not " +
3053 "installed." )
3054 return main.TRUE
3055 else:
3056 main.log.error( "Unexpected return value from appStatus: " +
3057 str( status ) )
3058 return main.ERROR
3059 except TypeError:
3060 main.log.exception( self.name + ": Object not as expected" )
3061 return main.ERROR
3062 except pexpect.EOF:
3063 main.log.error( self.name + ": EOF exception found" )
3064 main.log.error( self.name + ": " + self.handle.before )
3065 main.cleanup()
3066 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003067 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003068 main.log.exception( self.name + ": Uncaught exception!" )
3069 main.cleanup()
3070 main.exit()
3071
Jon Hallbd16b922015-03-26 17:53:15 -07003072 def uninstallApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07003073 """
3074 Uninstall an app that is already installed in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07003075 appName is the hierarchical app name, not the feature name
3076 If check is True, method will check the status of the app after the
3077 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07003078 Returns main.TRUE if the command was successfully sent
3079 main.FALSE if the cli responded with an error or given
3080 incorrect input
3081 """
3082 # TODO: check with Thomas about the state machine for apps
3083 try:
3084 if not isinstance( appName, types.StringType ):
3085 main.log.error( self.name + ".uninstallApp(): appName must " +
3086 "be a string" )
3087 return main.FALSE
3088 status = self.appStatus( appName )
3089 if status == "INSTALLED":
3090 response = self.app( appName, "uninstall" )
Jon Hallbd16b922015-03-26 17:53:15 -07003091 if check and response == main.TRUE:
3092 for i in range(10): # try 10 times then give up
3093 status = self.appStatus( appName )
3094 if status == "UNINSTALLED":
3095 return main.TRUE
3096 else:
3097 time.sleep( 1 )
3098 return main.FALSE
3099 else: # not check or command didn't succeed
3100 return response
Jon Hall146f1522015-03-24 15:33:24 -07003101 elif status == "ACTIVE":
3102 main.log.warn( self.name + ": Tried to uninstall the " +
3103 "application '" + appName + "' which is " +
3104 "currently active." )
3105 response = self.app( appName, "uninstall" )
Jon Hallbd16b922015-03-26 17:53:15 -07003106 if check and response == main.TRUE:
3107 for i in range(10): # try 10 times then give up
3108 status = self.appStatus( appName )
3109 if status == "UNINSTALLED":
3110 return main.TRUE
3111 else:
3112 time.sleep( 1 )
3113 return main.FALSE
3114 else: # not check or command didn't succeed
3115 return response
Jon Hall146f1522015-03-24 15:33:24 -07003116 elif status == "UNINSTALLED":
3117 return main.TRUE
3118 else:
3119 main.log.error( "Unexpected return value from appStatus: " +
3120 str( status ) )
3121 return main.ERROR
3122 except TypeError:
3123 main.log.exception( self.name + ": Object not as expected" )
3124 return main.ERROR
3125 except pexpect.EOF:
3126 main.log.error( self.name + ": EOF exception found" )
3127 main.log.error( self.name + ": " + self.handle.before )
3128 main.cleanup()
3129 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003130 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003131 main.log.exception( self.name + ": Uncaught exception!" )
3132 main.cleanup()
3133 main.exit()
Jon Hallbd16b922015-03-26 17:53:15 -07003134
3135 def appIDs( self, jsonFormat=True ):
3136 """
3137 Show the mappings between app id and app names given by the 'app-ids'
3138 cli command
3139 """
3140 try:
3141 cmdStr = "app-ids"
3142 if jsonFormat:
3143 cmdStr += " -j"
Jon Hallc6358dd2015-04-10 12:44:28 -07003144 output = self.sendline( cmdStr )
3145 assert "Error executing command" not in output
3146 return output
Jon Hallbd16b922015-03-26 17:53:15 -07003147 except AssertionError:
3148 main.log.error( "Error in processing onos:app-ids command: " +
3149 str( output ) )
3150 return None
3151 except TypeError:
3152 main.log.exception( self.name + ": Object not as expected" )
3153 return None
3154 except pexpect.EOF:
3155 main.log.error( self.name + ": EOF exception found" )
3156 main.log.error( self.name + ": " + self.handle.before )
3157 main.cleanup()
3158 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003159 except Exception:
Jon Hallbd16b922015-03-26 17:53:15 -07003160 main.log.exception( self.name + ": Uncaught exception!" )
3161 main.cleanup()
3162 main.exit()
3163
3164 def appToIDCheck( self ):
3165 """
3166 This method will check that each application's ID listed in 'apps' is
3167 the same as the ID listed in 'app-ids'. The check will also check that
3168 there are no duplicate IDs issued. Note that an app ID should be
3169 a globaly unique numerical identifier for app/app-like features. Once
3170 an ID is registered, the ID is never freed up so that if an app is
3171 reinstalled it will have the same ID.
3172
3173 Returns: main.TRUE if the check passes and
3174 main.FALSE if the check fails or
3175 main.ERROR if there is some error in processing the test
3176 """
3177 try:
Jon Hall390696c2015-05-05 17:13:41 -07003178 bail = False
3179 ids = self.appIDs( jsonFormat=True )
3180 if ids:
3181 ids = json.loads( ids )
3182 else:
3183 main.log.error( "app-ids returned nothing:" + repr( ids ) )
3184 bail = True
3185 apps = self.apps( jsonFormat=True )
3186 if apps:
3187 apps = json.loads( apps )
3188 else:
3189 main.log.error( "apps returned nothing:" + repr( apps ) )
3190 bail = True
3191 if bail:
3192 return main.FALSE
Jon Hallbd16b922015-03-26 17:53:15 -07003193 result = main.TRUE
3194 for app in apps:
3195 appID = app.get( 'id' )
3196 if appID is None:
3197 main.log.error( "Error parsing app: " + str( app ) )
3198 result = main.FALSE
3199 appName = app.get( 'name' )
3200 if appName is None:
3201 main.log.error( "Error parsing app: " + str( app ) )
3202 result = main.FALSE
3203 # get the entry in ids that has the same appID
Jon Hall390696c2015-05-05 17:13:41 -07003204 current = filter( lambda item: item[ 'id' ] == appID, ids )
Jon Hall050e1bd2015-03-30 13:33:02 -07003205 # main.log.debug( "Comparing " + str( app ) + " to " +
3206 # str( current ) )
Jon Hallbd16b922015-03-26 17:53:15 -07003207 if not current: # if ids doesn't have this id
3208 result = main.FALSE
3209 main.log.error( "'app-ids' does not have the ID for " +
3210 str( appName ) + " that apps does." )
3211 elif len( current ) > 1:
3212 # there is more than one app with this ID
3213 result = main.FALSE
3214 # We will log this later in the method
3215 elif not current[0][ 'name' ] == appName:
3216 currentName = current[0][ 'name' ]
3217 result = main.FALSE
3218 main.log.error( "'app-ids' has " + str( currentName ) +
3219 " registered under id:" + str( appID ) +
3220 " but 'apps' has " + str( appName ) )
3221 else:
3222 pass # id and name match!
3223 # now make sure that app-ids has no duplicates
3224 idsList = []
3225 namesList = []
3226 for item in ids:
3227 idsList.append( item[ 'id' ] )
3228 namesList.append( item[ 'name' ] )
3229 if len( idsList ) != len( set( idsList ) ) or\
3230 len( namesList ) != len( set( namesList ) ):
3231 main.log.error( "'app-ids' has some duplicate entries: \n"
3232 + json.dumps( ids,
3233 sort_keys=True,
3234 indent=4,
3235 separators=( ',', ': ' ) ) )
3236 result = main.FALSE
3237 return result
3238 except ( ValueError, TypeError ):
3239 main.log.exception( self.name + ": Object not as expected" )
3240 return main.ERROR
3241 except pexpect.EOF:
3242 main.log.error( self.name + ": EOF exception found" )
3243 main.log.error( self.name + ": " + self.handle.before )
3244 main.cleanup()
3245 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003246 except Exception:
Jon Hallbd16b922015-03-26 17:53:15 -07003247 main.log.exception( self.name + ": Uncaught exception!" )
3248 main.cleanup()
3249 main.exit()
3250
Jon Hallfb760a02015-04-13 15:35:03 -07003251 def getCfg( self, component=None, propName=None, short=False,
3252 jsonFormat=True ):
3253 """
3254 Get configuration settings from onos cli
3255 Optional arguments:
3256 component - Optionally only list configurations for a specific
3257 component. If None, all components with configurations
3258 are displayed. Case Sensitive string.
3259 propName - If component is specified, propName option will show
3260 only this specific configuration from that component.
3261 Case Sensitive string.
3262 jsonFormat - Returns output as json. Note that this will override
3263 the short option
3264 short - Short, less verbose, version of configurations.
3265 This is overridden by the json option
3266 returns:
3267 Output from cli as a string or None on error
3268 """
3269 try:
3270 baseStr = "cfg"
3271 cmdStr = " get"
3272 componentStr = ""
3273 if component:
3274 componentStr += " " + component
3275 if propName:
3276 componentStr += " " + propName
3277 if jsonFormat:
3278 baseStr += " -j"
3279 elif short:
3280 baseStr += " -s"
3281 output = self.sendline( baseStr + cmdStr + componentStr )
3282 assert "Error executing command" not in output
3283 return output
3284 except AssertionError:
3285 main.log.error( "Error in processing 'cfg get' command: " +
3286 str( output ) )
3287 return None
3288 except TypeError:
3289 main.log.exception( self.name + ": Object not as expected" )
3290 return None
3291 except pexpect.EOF:
3292 main.log.error( self.name + ": EOF exception found" )
3293 main.log.error( self.name + ": " + self.handle.before )
3294 main.cleanup()
3295 main.exit()
3296 except Exception:
3297 main.log.exception( self.name + ": Uncaught exception!" )
3298 main.cleanup()
3299 main.exit()
3300
3301 def setCfg( self, component, propName, value=None, check=True ):
3302 """
3303 Set/Unset configuration settings from ONOS cli
Jon Hall390696c2015-05-05 17:13:41 -07003304 Required arguments:
Jon Hallfb760a02015-04-13 15:35:03 -07003305 component - The case sensitive name of the component whose
3306 property is to be set
3307 propName - The case sensitive name of the property to be set/unset
Jon Hall390696c2015-05-05 17:13:41 -07003308 Optional arguments:
Jon Hallfb760a02015-04-13 15:35:03 -07003309 value - The value to set the property to. If None, will unset the
3310 property and revert it to it's default value(if applicable)
3311 check - Boolean, Check whether the option was successfully set this
3312 only applies when a value is given.
3313 returns:
3314 main.TRUE on success or main.FALSE on failure. If check is False,
3315 will return main.TRUE unless there is an error
3316 """
3317 try:
3318 baseStr = "cfg"
3319 cmdStr = " set " + str( component ) + " " + str( propName )
3320 if value is not None:
3321 cmdStr += " " + str( value )
3322 output = self.sendline( baseStr + cmdStr )
3323 assert "Error executing command" not in output
3324 if value and check:
3325 results = self.getCfg( component=str( component ),
3326 propName=str( propName ),
3327 jsonFormat=True )
3328 # Check if current value is what we just set
3329 try:
3330 jsonOutput = json.loads( results )
3331 current = jsonOutput[ 'value' ]
3332 except ( ValueError, TypeError ):
3333 main.log.exception( "Error parsing cfg output" )
3334 main.log.error( "output:" + repr( results ) )
3335 return main.FALSE
3336 if current == str( value ):
3337 return main.TRUE
3338 return main.FALSE
3339 return main.TRUE
3340 except AssertionError:
3341 main.log.error( "Error in processing 'cfg set' command: " +
3342 str( output ) )
3343 return main.FALSE
3344 except TypeError:
3345 main.log.exception( self.name + ": Object not as expected" )
3346 return main.FALSE
3347 except pexpect.EOF:
3348 main.log.error( self.name + ": EOF exception found" )
3349 main.log.error( self.name + ": " + self.handle.before )
3350 main.cleanup()
3351 main.exit()
3352 except Exception:
3353 main.log.exception( self.name + ": Uncaught exception!" )
3354 main.cleanup()
3355 main.exit()
3356
Jon Hall390696c2015-05-05 17:13:41 -07003357 def setTestAdd( self, setName, values ):
3358 """
3359 CLI command to add elements to a distributed set.
3360 Arguments:
3361 setName - The name of the set to add to.
3362 values - The value(s) to add to the set, space seperated.
3363 Example usages:
3364 setTestAdd( "set1", "a b c" )
3365 setTestAdd( "set2", "1" )
3366 returns:
3367 main.TRUE on success OR
3368 main.FALSE if elements were already in the set OR
3369 main.ERROR on error
3370 """
3371 try:
3372 cmdStr = "set-test-add " + str( setName ) + " " + str( values )
3373 output = self.sendline( cmdStr )
Jon Hallfeff3082015-05-19 10:23:26 -07003374 try:
3375 # TODO: Maybe make this less hardcoded
3376 # ConsistentMap Exceptions
3377 assert "org.onosproject.store.service" not in output
3378 # Node not leader
3379 assert "java.lang.IllegalStateException" not in output
3380 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003381 main.log.error( "Error in processing '" + cmdStr + "' " +
Jon Hallfeff3082015-05-19 10:23:26 -07003382 "command: " + str( output ) )
3383 retryTime = 30 # Conservative time, given by Madan
3384 main.log.info( "Waiting " + str( retryTime ) +
3385 "seconds before retrying." )
3386 time.sleep( retryTime ) # Due to change in mastership
3387 output = self.sendline( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07003388 assert "Error executing command" not in output
3389 positiveMatch = "\[(.*)\] was added to the set " + str( setName )
3390 negativeMatch = "\[(.*)\] was already in set " + str( setName )
3391 main.log.info( self.name + ": " + output )
3392 if re.search( positiveMatch, output):
3393 return main.TRUE
3394 elif re.search( negativeMatch, output):
3395 return main.FALSE
3396 else:
3397 main.log.error( self.name + ": setTestAdd did not" +
3398 " match expected output" )
Jon Hall390696c2015-05-05 17:13:41 -07003399 main.log.debug( self.name + " actual: " + repr( output ) )
3400 return main.ERROR
3401 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003402 main.log.error( "Error in processing '" + cmdStr + "' command: " +
Jon Hall390696c2015-05-05 17:13:41 -07003403 str( output ) )
3404 return main.ERROR
3405 except TypeError:
3406 main.log.exception( self.name + ": Object not as expected" )
3407 return main.ERROR
3408 except pexpect.EOF:
3409 main.log.error( self.name + ": EOF exception found" )
3410 main.log.error( self.name + ": " + self.handle.before )
3411 main.cleanup()
3412 main.exit()
3413 except Exception:
3414 main.log.exception( self.name + ": Uncaught exception!" )
3415 main.cleanup()
3416 main.exit()
3417
3418 def setTestRemove( self, setName, values, clear=False, retain=False ):
3419 """
3420 CLI command to remove elements from a distributed set.
3421 Required arguments:
3422 setName - The name of the set to remove from.
3423 values - The value(s) to remove from the set, space seperated.
3424 Optional arguments:
3425 clear - Clear all elements from the set
3426 retain - Retain only the given values. (intersection of the
3427 original set and the given set)
3428 returns:
3429 main.TRUE on success OR
3430 main.FALSE if the set was not changed OR
3431 main.ERROR on error
3432 """
3433 try:
3434 cmdStr = "set-test-remove "
3435 if clear:
3436 cmdStr += "-c " + str( setName )
3437 elif retain:
3438 cmdStr += "-r " + str( setName ) + " " + str( values )
3439 else:
3440 cmdStr += str( setName ) + " " + str( values )
3441 output = self.sendline( cmdStr )
Jon Hallfeff3082015-05-19 10:23:26 -07003442 try:
3443 # TODO: Maybe make this less hardcoded
3444 # ConsistentMap Exceptions
3445 assert "org.onosproject.store.service" not in output
3446 # Node not leader
3447 assert "java.lang.IllegalStateException" not in output
3448 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003449 main.log.error( "Error in processing '" + cmdStr + "' " +
Jon Hallfeff3082015-05-19 10:23:26 -07003450 "command: " + str( output ) )
3451 retryTime = 30 # Conservative time, given by Madan
3452 main.log.info( "Waiting " + str( retryTime ) +
3453 "seconds before retrying." )
3454 time.sleep( retryTime ) # Due to change in mastership
3455 output = self.sendline( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07003456 assert "Error executing command" not in output
3457 main.log.info( self.name + ": " + output )
3458 if clear:
3459 pattern = "Set " + str( setName ) + " cleared"
3460 if re.search( pattern, output ):
3461 return main.TRUE
3462 elif retain:
3463 positivePattern = str( setName ) + " was pruned to contain " +\
3464 "only elements of set \[(.*)\]"
3465 negativePattern = str( setName ) + " was not changed by " +\
3466 "retaining only elements of the set " +\
3467 "\[(.*)\]"
3468 if re.search( positivePattern, output ):
3469 return main.TRUE
3470 elif re.search( negativePattern, output ):
3471 return main.FALSE
3472 else:
3473 positivePattern = "\[(.*)\] was removed from the set " +\
3474 str( setName )
3475 if ( len( values.split() ) == 1 ):
3476 negativePattern = "\[(.*)\] was not in set " +\
3477 str( setName )
3478 else:
3479 negativePattern = "No element of \[(.*)\] was in set " +\
3480 str( setName )
3481 if re.search( positivePattern, output ):
3482 return main.TRUE
3483 elif re.search( negativePattern, output ):
3484 return main.FALSE
3485 main.log.error( self.name + ": setTestRemove did not" +
3486 " match expected output" )
3487 main.log.debug( self.name + " expected: " + pattern )
3488 main.log.debug( self.name + " actual: " + repr( output ) )
3489 return main.ERROR
3490 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003491 main.log.error( "Error in processing '" + cmdStr + "' command: " +
Jon Hall390696c2015-05-05 17:13:41 -07003492 str( output ) )
3493 return main.ERROR
3494 except TypeError:
3495 main.log.exception( self.name + ": Object not as expected" )
3496 return main.ERROR
3497 except pexpect.EOF:
3498 main.log.error( self.name + ": EOF exception found" )
3499 main.log.error( self.name + ": " + self.handle.before )
3500 main.cleanup()
3501 main.exit()
3502 except Exception:
3503 main.log.exception( self.name + ": Uncaught exception!" )
3504 main.cleanup()
3505 main.exit()
3506
3507 def setTestGet( self, setName, values="" ):
3508 """
3509 CLI command to get the elements in a distributed set.
3510 Required arguments:
3511 setName - The name of the set to remove from.
3512 Optional arguments:
3513 values - The value(s) to check if in the set, space seperated.
3514 returns:
3515 main.ERROR on error OR
3516 A list of elements in the set if no optional arguments are
3517 supplied OR
3518 A tuple containing the list then:
3519 main.FALSE if the given values are not in the set OR
3520 main.TRUE if the given values are in the set OR
3521 """
3522 try:
3523 values = str( values ).strip()
3524 setName = str( setName ).strip()
3525 length = len( values.split() )
3526 containsCheck = None
3527 # Patterns to match
3528 setPattern = "\[(.*)\]"
3529 pattern = "Items in set " + setName + ":\n" + setPattern
3530 containsTrue = "Set " + setName + " contains the value " + values
3531 containsFalse = "Set " + setName + " did not contain the value " +\
3532 values
3533 containsAllTrue = "Set " + setName + " contains the the subset " +\
3534 setPattern
3535 containsAllFalse = "Set " + setName + " did not contain the the" +\
3536 " subset " + setPattern
3537
3538 cmdStr = "set-test-get "
3539 cmdStr += setName + " " + values
3540 output = self.sendline( cmdStr )
Jon Hallfeff3082015-05-19 10:23:26 -07003541 try:
3542 # TODO: Maybe make this less hardcoded
3543 # ConsistentMap Exceptions
3544 assert "org.onosproject.store.service" not in output
3545 # Node not leader
3546 assert "java.lang.IllegalStateException" not in output
3547 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003548 main.log.error( "Error in processing '" + cmdStr + "' " +
Jon Hallfeff3082015-05-19 10:23:26 -07003549 "command: " + str( output ) )
3550 retryTime = 30 # Conservative time, given by Madan
3551 main.log.info( "Waiting " + str( retryTime ) +
3552 "seconds before retrying." )
3553 time.sleep( retryTime ) # Due to change in mastership
3554 output = self.sendline( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07003555 assert "Error executing command" not in output
3556 main.log.info( self.name + ": " + output )
3557
3558 if length == 0:
3559 match = re.search( pattern, output )
3560 else: # if given values
3561 if length == 1: # Contains output
3562 patternTrue = pattern + "\n" + containsTrue
3563 patternFalse = pattern + "\n" + containsFalse
3564 else: # ContainsAll output
3565 patternTrue = pattern + "\n" + containsAllTrue
3566 patternFalse = pattern + "\n" + containsAllFalse
3567 matchTrue = re.search( patternTrue, output )
3568 matchFalse = re.search( patternFalse, output )
3569 if matchTrue:
3570 containsCheck = main.TRUE
3571 match = matchTrue
3572 elif matchFalse:
3573 containsCheck = main.FALSE
3574 match = matchFalse
3575 else:
3576 main.log.error( self.name + " setTestGet did not match " +\
3577 "expected output" )
3578 main.log.debug( self.name + " expected: " + pattern )
3579 main.log.debug( self.name + " actual: " + repr( output ) )
3580 match = None
3581 if match:
3582 setMatch = match.group( 1 )
3583 if setMatch == '':
3584 setList = []
3585 else:
3586 setList = setMatch.split( ", " )
3587 if length > 0:
3588 return ( setList, containsCheck )
3589 else:
3590 return setList
3591 else: # no match
3592 main.log.error( self.name + ": setTestGet did not" +
3593 " match expected output" )
3594 main.log.debug( self.name + " expected: " + pattern )
3595 main.log.debug( self.name + " actual: " + repr( output ) )
3596 return main.ERROR
3597 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003598 main.log.error( "Error in processing '" + cmdStr + "' command: " +
Jon Hall390696c2015-05-05 17:13:41 -07003599 str( output ) )
3600 return main.ERROR
3601 except TypeError:
3602 main.log.exception( self.name + ": Object not as expected" )
3603 return main.ERROR
3604 except pexpect.EOF:
3605 main.log.error( self.name + ": EOF exception found" )
3606 main.log.error( self.name + ": " + self.handle.before )
3607 main.cleanup()
3608 main.exit()
3609 except Exception:
3610 main.log.exception( self.name + ": Uncaught exception!" )
3611 main.cleanup()
3612 main.exit()
3613
3614 def setTestSize( self, setName ):
3615 """
3616 CLI command to get the elements in a distributed set.
3617 Required arguments:
3618 setName - The name of the set to remove from.
3619 returns:
Jon Hallfeff3082015-05-19 10:23:26 -07003620 The integer value of the size returned or
Jon Hall390696c2015-05-05 17:13:41 -07003621 None on error
3622 """
3623 try:
3624 # TODO: Should this check against the number of elements returned
3625 # and then return true/false based on that?
3626 setName = str( setName ).strip()
3627 # Patterns to match
3628 setPattern = "\[(.*)\]"
3629 pattern = "There are (\d+) items in set " + setName + ":\n" +\
3630 setPattern
3631 cmdStr = "set-test-get -s "
3632 cmdStr += setName
3633 output = self.sendline( cmdStr )
Jon Hallfeff3082015-05-19 10:23:26 -07003634 try:
3635 # TODO: Maybe make this less hardcoded
3636 # ConsistentMap Exceptions
3637 assert "org.onosproject.store.service" not in output
3638 # Node not leader
3639 assert "java.lang.IllegalStateException" not in output
3640 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003641 main.log.error( "Error in processing '" + cmdStr + "' " +
Jon Hallfeff3082015-05-19 10:23:26 -07003642 "command: " + str( output ) )
3643 retryTime = 30 # Conservative time, given by Madan
3644 main.log.info( "Waiting " + str( retryTime ) +
3645 "seconds before retrying." )
3646 time.sleep( retryTime ) # Due to change in mastership
3647 output = self.sendline( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07003648 assert "Error executing command" not in output
3649 main.log.info( self.name + ": " + output )
3650 match = re.search( pattern, output )
3651 if match:
3652 setSize = int( match.group( 1 ) )
3653 setMatch = match.group( 2 )
3654 if len( setMatch.split() ) == setSize:
3655 main.log.info( "The size returned by " + self.name +
3656 " matches the number of elements in " +
3657 "the returned set" )
3658 else:
3659 main.log.error( "The size returned by " + self.name +
3660 " does not match the number of " +
3661 "elements in the returned set." )
3662 return setSize
3663 else: # no match
3664 main.log.error( self.name + ": setTestGet did not" +
3665 " match expected output" )
3666 main.log.debug( self.name + " expected: " + pattern )
3667 main.log.debug( self.name + " actual: " + repr( output ) )
3668 return None
3669 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003670 main.log.error( "Error in processing '" + cmdStr + "' command: " +
Jon Hall390696c2015-05-05 17:13:41 -07003671 str( output ) )
acsmarsa4a4d1e2015-07-10 16:01:24 -07003672 return None
Jon Hall390696c2015-05-05 17:13:41 -07003673 except TypeError:
3674 main.log.exception( self.name + ": Object not as expected" )
3675 return None
3676 except pexpect.EOF:
3677 main.log.error( self.name + ": EOF exception found" )
3678 main.log.error( self.name + ": " + self.handle.before )
3679 main.cleanup()
3680 main.exit()
3681 except Exception:
3682 main.log.exception( self.name + ": Uncaught exception!" )
3683 main.cleanup()
3684 main.exit()
3685
Jon Hall80daded2015-05-27 16:07:00 -07003686 def counters( self, jsonFormat=True ):
Jon Hall390696c2015-05-05 17:13:41 -07003687 """
3688 Command to list the various counters in the system.
3689 returns:
Jon Hall80daded2015-05-27 16:07:00 -07003690 if jsonFormat, a string of the json object returned by the cli
3691 command
3692 if not jsonFormat, the normal string output of the cli command
Jon Hall390696c2015-05-05 17:13:41 -07003693 None on error
3694 """
Jon Hall390696c2015-05-05 17:13:41 -07003695 try:
3696 counters = {}
3697 cmdStr = "counters"
Jon Hall80daded2015-05-27 16:07:00 -07003698 if jsonFormat:
3699 cmdStr += " -j"
Jon Hall390696c2015-05-05 17:13:41 -07003700 output = self.sendline( cmdStr )
3701 assert "Error executing command" not in output
3702 main.log.info( self.name + ": " + output )
Jon Hall80daded2015-05-27 16:07:00 -07003703 return output
Jon Hall390696c2015-05-05 17:13:41 -07003704 except AssertionError:
3705 main.log.error( "Error in processing 'counters' command: " +
3706 str( output ) )
Jon Hall80daded2015-05-27 16:07:00 -07003707 return None
Jon Hall390696c2015-05-05 17:13:41 -07003708 except TypeError:
3709 main.log.exception( self.name + ": Object not as expected" )
Jon Hall80daded2015-05-27 16:07:00 -07003710 return None
Jon Hall390696c2015-05-05 17:13:41 -07003711 except pexpect.EOF:
3712 main.log.error( self.name + ": EOF exception found" )
3713 main.log.error( self.name + ": " + self.handle.before )
3714 main.cleanup()
3715 main.exit()
3716 except Exception:
3717 main.log.exception( self.name + ": Uncaught exception!" )
3718 main.cleanup()
3719 main.exit()
3720
Jon Halle1a3b752015-07-22 13:02:46 -07003721 def counterTestAddAndGet( self, counter, delta=1, inMemory=False ):
Jon Hall390696c2015-05-05 17:13:41 -07003722 """
Jon Halle1a3b752015-07-22 13:02:46 -07003723 CLI command to add a delta to then get a distributed counter.
Jon Hall390696c2015-05-05 17:13:41 -07003724 Required arguments:
3725 counter - The name of the counter to increment.
3726 Optional arguments:
Jon Halle1a3b752015-07-22 13:02:46 -07003727 delta - The long to add to the counter
Jon Hall390696c2015-05-05 17:13:41 -07003728 inMemory - use in memory map for the counter
3729 returns:
3730 integer value of the counter or
3731 None on Error
3732 """
3733 try:
3734 counter = str( counter )
Jon Halle1a3b752015-07-22 13:02:46 -07003735 delta = int( delta )
Jon Hall390696c2015-05-05 17:13:41 -07003736 cmdStr = "counter-test-increment "
3737 if inMemory:
3738 cmdStr += "-i "
3739 cmdStr += counter
Jon Halle1a3b752015-07-22 13:02:46 -07003740 if delta != 1:
3741 cmdStr += " " + str( delta )
Jon Hall390696c2015-05-05 17:13:41 -07003742 output = self.sendline( cmdStr )
Jon Hallfeff3082015-05-19 10:23:26 -07003743 try:
3744 # TODO: Maybe make this less hardcoded
3745 # ConsistentMap Exceptions
3746 assert "org.onosproject.store.service" not in output
3747 # Node not leader
3748 assert "java.lang.IllegalStateException" not in output
3749 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003750 main.log.error( "Error in processing '" + cmdStr + "' " +
Jon Hallfeff3082015-05-19 10:23:26 -07003751 "command: " + str( output ) )
3752 retryTime = 30 # Conservative time, given by Madan
3753 main.log.info( "Waiting " + str( retryTime ) +
3754 "seconds before retrying." )
3755 time.sleep( retryTime ) # Due to change in mastership
3756 output = self.sendline( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07003757 assert "Error executing command" not in output
3758 main.log.info( self.name + ": " + output )
Jon Halle1a3b752015-07-22 13:02:46 -07003759 pattern = counter + " was updated to (-?\d+)"
Jon Hall390696c2015-05-05 17:13:41 -07003760 match = re.search( pattern, output )
3761 if match:
3762 return int( match.group( 1 ) )
3763 else:
Jon Halle1a3b752015-07-22 13:02:46 -07003764 main.log.error( self.name + ": counterTestAddAndGet did not" +
Jon Hall390696c2015-05-05 17:13:41 -07003765 " match expected output." )
3766 main.log.debug( self.name + " expected: " + pattern )
3767 main.log.debug( self.name + " actual: " + repr( output ) )
3768 return None
3769 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003770 main.log.error( "Error in processing '" + cmdStr + "'" +
Jon Hall390696c2015-05-05 17:13:41 -07003771 " command: " + str( output ) )
3772 return None
3773 except TypeError:
3774 main.log.exception( self.name + ": Object not as expected" )
3775 return None
3776 except pexpect.EOF:
3777 main.log.error( self.name + ": EOF exception found" )
3778 main.log.error( self.name + ": " + self.handle.before )
3779 main.cleanup()
3780 main.exit()
3781 except Exception:
3782 main.log.exception( self.name + ": Uncaught exception!" )
3783 main.cleanup()
3784 main.exit()
3785
Jon Halle1a3b752015-07-22 13:02:46 -07003786 def counterTestGetAndAdd( self, counter, delta=1, inMemory=False ):
3787 """
3788 CLI command to get a distributed counter then add a delta to it.
3789 Required arguments:
3790 counter - The name of the counter to increment.
3791 Optional arguments:
3792 delta - The long to add to the counter
3793 inMemory - use in memory map for the counter
3794 returns:
3795 integer value of the counter or
3796 None on Error
3797 """
3798 try:
3799 counter = str( counter )
3800 delta = int( delta )
3801 cmdStr = "counter-test-increment -g "
3802 if inMemory:
3803 cmdStr += "-i "
3804 cmdStr += counter
3805 if delta != 1:
3806 cmdStr += " " + str( delta )
3807 output = self.sendline( cmdStr )
3808 try:
3809 # TODO: Maybe make this less hardcoded
3810 # ConsistentMap Exceptions
3811 assert "org.onosproject.store.service" not in output
3812 # Node not leader
3813 assert "java.lang.IllegalStateException" not in output
3814 except AssertionError:
3815 main.log.error( "Error in processing '" + cmdStr + "' " +
3816 "command: " + str( output ) )
3817 retryTime = 30 # Conservative time, given by Madan
3818 main.log.info( "Waiting " + str( retryTime ) +
3819 "seconds before retrying." )
3820 time.sleep( retryTime ) # Due to change in mastership
3821 output = self.sendline( cmdStr )
3822 assert "Error executing command" not in output
3823 main.log.info( self.name + ": " + output )
3824 pattern = counter + " was updated to (-?\d+)"
3825 match = re.search( pattern, output )
3826 if match:
3827 return int( match.group( 1 ) )
3828 else:
3829 main.log.error( self.name + ": counterTestGetAndAdd did not" +
3830 " match expected output." )
3831 main.log.debug( self.name + " expected: " + pattern )
3832 main.log.debug( self.name + " actual: " + repr( output ) )
3833 return None
3834 except AssertionError:
3835 main.log.error( "Error in processing '" + cmdStr + "'" +
3836 " command: " + str( output ) )
3837 return None
3838 except TypeError:
3839 main.log.exception( self.name + ": Object not as expected" )
3840 return None
3841 except pexpect.EOF:
3842 main.log.error( self.name + ": EOF exception found" )
3843 main.log.error( self.name + ": " + self.handle.before )
3844 main.cleanup()
3845 main.exit()
3846 except Exception:
3847 main.log.exception( self.name + ": Uncaught exception!" )
3848 main.cleanup()
3849 main.exit()
3850
kelvin-onlaba297c4d2015-06-01 13:53:55 -07003851 def summary( self, jsonFormat=True ):
3852 """
3853 Description: Execute summary command in onos
3854 Returns: json object ( summary -j ), returns main.FALSE if there is
3855 no output
3856
3857 """
3858 try:
3859 cmdStr = "summary"
3860 if jsonFormat:
3861 cmdStr += " -j"
3862 handle = self.sendline( cmdStr )
3863
3864 if re.search( "Error:", handle ):
3865 main.log.error( self.name + ": summary() response: " +
3866 str( handle ) )
3867 if not handle:
3868 main.log.error( self.name + ": There is no output in " +
3869 "summary command" )
3870 return main.FALSE
3871 return handle
3872 except TypeError:
3873 main.log.exception( self.name + ": Object not as expected" )
3874 return None
3875 except pexpect.EOF:
3876 main.log.error( self.name + ": EOF exception found" )
3877 main.log.error( self.name + ": " + self.handle.before )
3878 main.cleanup()
3879 main.exit()
3880 except Exception:
3881 main.log.exception( self.name + ": Uncaught exception!" )
3882 main.cleanup()
3883 main.exit()
Jon Hall2a5002c2015-08-21 16:49:11 -07003884
3885 def transactionalMapGet( self, keyName, inMemory=False ):
3886 """
3887 CLI command to get the value of a key in a consistent map using
3888 transactions. This a test function and can only get keys from the
3889 test map hard coded into the cli command
3890 Required arguments:
3891 keyName - The name of the key to get
3892 Optional arguments:
3893 inMemory - use in memory map for the counter
3894 returns:
3895 The string value of the key or
3896 None on Error
3897 """
3898 try:
3899 keyName = str( keyName )
3900 cmdStr = "transactional-map-test-get "
3901 if inMemory:
3902 cmdStr += "-i "
3903 cmdStr += keyName
3904 output = self.sendline( cmdStr )
3905 try:
3906 # TODO: Maybe make this less hardcoded
3907 # ConsistentMap Exceptions
3908 assert "org.onosproject.store.service" not in output
3909 # Node not leader
3910 assert "java.lang.IllegalStateException" not in output
3911 except AssertionError:
3912 main.log.error( "Error in processing '" + cmdStr + "' " +
3913 "command: " + str( output ) )
3914 return None
3915 pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found."
3916 if "Key " + keyName + " not found." in output:
3917 return None
3918 else:
3919 match = re.search( pattern, output )
3920 if match:
3921 return match.groupdict()[ 'value' ]
3922 else:
3923 main.log.error( self.name + ": transactionlMapGet did not" +
3924 " match expected output." )
3925 main.log.debug( self.name + " expected: " + pattern )
3926 main.log.debug( self.name + " actual: " + repr( output ) )
3927 return None
3928 except TypeError:
3929 main.log.exception( self.name + ": Object not as expected" )
3930 return None
3931 except pexpect.EOF:
3932 main.log.error( self.name + ": EOF exception found" )
3933 main.log.error( self.name + ": " + self.handle.before )
3934 main.cleanup()
3935 main.exit()
3936 except Exception:
3937 main.log.exception( self.name + ": Uncaught exception!" )
3938 main.cleanup()
3939 main.exit()
3940
3941 def transactionalMapPut( self, numKeys, value, inMemory=False ):
3942 """
3943 CLI command to put a value into 'numKeys' number of keys in a
3944 consistent map using transactions. This a test function and can only
3945 put into keys named 'Key#' of the test map hard coded into the cli command
3946 Required arguments:
3947 numKeys - Number of keys to add the value to
3948 value - The string value to put into the keys
3949 Optional arguments:
3950 inMemory - use in memory map for the counter
3951 returns:
3952 A dictionary whose keys are the name of the keys put into the map
3953 and the values of the keys are dictionaries whose key-values are
3954 'value': value put into map and optionaly
3955 'oldValue': Previous value in the key or
3956 None on Error
3957
3958 Example output
3959 { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'},
3960 'Key2': {'value': 'Testing'} }
3961 """
3962 try:
3963 numKeys = str( numKeys )
3964 value = str( value )
3965 cmdStr = "transactional-map-test-put "
3966 if inMemory:
3967 cmdStr += "-i "
3968 cmdStr += numKeys + " " + value
3969 output = self.sendline( cmdStr )
3970 try:
3971 # TODO: Maybe make this less hardcoded
3972 # ConsistentMap Exceptions
3973 assert "org.onosproject.store.service" not in output
3974 # Node not leader
3975 assert "java.lang.IllegalStateException" not in output
3976 except AssertionError:
3977 main.log.error( "Error in processing '" + cmdStr + "' " +
3978 "command: " + str( output ) )
3979 return None
3980 newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.'
3981 updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\."
3982 results = {}
3983 for line in output.splitlines():
3984 new = re.search( newPattern, line )
3985 updated = re.search( updatedPattern, line )
3986 if new:
3987 results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] }
3988 elif updated:
3989 results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ],
3990 'oldValue': updated.groupdict()[ 'oldValue' ] }
3991 else:
3992 main.log.error( self.name + ": transactionlMapGet did not" +
3993 " match expected output." )
3994 main.log.debug( self.name + " expected: " + pattern )
3995 main.log.debug( self.name + " actual: " + repr( output ) )
3996 return results
3997 except TypeError:
3998 main.log.exception( self.name + ": Object not as expected" )
3999 return None
4000 except pexpect.EOF:
4001 main.log.error( self.name + ": EOF exception found" )
4002 main.log.error( self.name + ": " + self.handle.before )
4003 main.cleanup()
4004 main.exit()
4005 except Exception:
4006 main.log.exception( self.name + ": Uncaught exception!" )
4007 main.cleanup()
4008 main.exit()
acsmarsdaea66c2015-09-03 11:44:06 -07004009 def maps( self, jsonFormat=True ):
4010 """
4011 Description: Returns result of onos:maps
4012 Optional:
4013 * jsonFormat: enable json formatting of output
4014 """
4015 try:
4016 cmdStr = "maps"
4017 if jsonFormat:
4018 cmdStr += " -j"
4019 handle = self.sendline( cmdStr )
4020 return handle
4021 except TypeError:
4022 main.log.exception( self.name + ": Object not as expected" )
4023 return None
4024 except pexpect.EOF:
4025 main.log.error( self.name + ": EOF exception found" )
4026 main.log.error( self.name + ": " + self.handle.before )
4027 main.cleanup()
4028 main.exit()
4029 except Exception:
4030 main.log.exception( self.name + ": Uncaught exception!" )
4031 main.cleanup()
4032 main.exit()