blob: 32aef33bb8e304bb47ed85864ad8200bb0755ea0 [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 )
1809 type = args[ "TYPE" ] if args[ "TYPE" ] is not None else ""
1810 if jsonFormat and summary and ( type != "" ):
1811 jsonResult = json.loads( handle )
1812 if type in jsonResult.keys():
1813 return jsonResult[ type ]
1814 else:
1815 main.log.error( "unknown TYPE, return all types of intents" )
1816 return handle
1817 else:
1818 return handle
pingping-lin54b03372015-08-13 14:43:10 -07001819
1820 except TypeError:
1821 main.log.exception( self.name + ": Object not as expected" )
1822 return None
1823 except pexpect.EOF:
1824 main.log.error( self.name + ": EOF exception found" )
1825 main.log.error( self.name + ": " + self.handle.before )
1826 main.cleanup()
1827 main.exit()
1828 except Exception:
1829 main.log.exception( self.name + ": Uncaught exception!" )
1830 main.cleanup()
1831 main.exit()
1832
pingping-lin8244a3b2015-09-16 13:36:56 -07001833
kelvin-onlab54400a92015-02-26 18:05:51 -08001834 def getIntentState(self, intentsId, intentsJson=None):
1835 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001836 Check intent state.
1837 Accepts a single intent ID (string type) or a list of intent IDs.
1838 Returns the state(string type) of the id if a single intent ID is
1839 accepted.
Jon Hallefbd9792015-03-05 16:11:36 -08001840 Returns a dictionary with intent IDs as the key and its
1841 corresponding states as the values
kelvin-onlabfb521662015-02-27 09:52:40 -08001842 Parameters:
kelvin-onlab54400a92015-02-26 18:05:51 -08001843 intentId: intent ID (string type)
1844 intentsJson: parsed json object from the onos:intents api
1845 Returns:
1846 state = An intent's state- INSTALL,WITHDRAWN etc.
1847 stateDict = Dictionary of intent's state. intent ID as the keys and
1848 state as the values.
1849 """
kelvin-onlab54400a92015-02-26 18:05:51 -08001850 try:
1851 state = "State is Undefined"
1852 if not intentsJson:
Jon Hallefbd9792015-03-05 16:11:36 -08001853 intentsJsonTemp = json.loads( self.intents() )
kelvin-onlab54400a92015-02-26 18:05:51 -08001854 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001855 intentsJsonTemp = json.loads( intentsJson )
1856 if isinstance( intentsId, types.StringType ):
kelvin-onlab54400a92015-02-26 18:05:51 -08001857 for intent in intentsJsonTemp:
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001858 if intentsId == intent[ 'id' ]:
1859 state = intent[ 'state' ]
kelvin-onlab54400a92015-02-26 18:05:51 -08001860 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001861 main.log.info( "Cannot find intent ID" + str( intentsId ) +
1862 " on the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001863 return state
Jon Hallefbd9792015-03-05 16:11:36 -08001864 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001865 dictList = []
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001866 for i in xrange( len( intentsId ) ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08001867 stateDict = {}
kelvin-onlab54400a92015-02-26 18:05:51 -08001868 for intents in intentsJsonTemp:
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001869 if intentsId[ i ] == intents[ 'id' ]:
1870 stateDict[ 'state' ] = intents[ 'state' ]
1871 stateDict[ 'id' ] = intentsId[ i ]
Jon Hallefbd9792015-03-05 16:11:36 -08001872 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08001873 break
Jon Hallefbd9792015-03-05 16:11:36 -08001874 if len( intentsId ) != len( dictList ):
1875 main.log.info( "Cannot find some of the intent ID state" )
kelvin-onlab07dbd012015-03-04 16:29:39 -08001876 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08001877 else:
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001878 main.log.info( "Invalid intents ID entry" )
kelvin-onlab54400a92015-02-26 18:05:51 -08001879 return None
kelvin-onlab54400a92015-02-26 18:05:51 -08001880 except TypeError:
1881 main.log.exception( self.name + ": Object not as expected" )
1882 return None
1883 except pexpect.EOF:
1884 main.log.error( self.name + ": EOF exception found" )
1885 main.log.error( self.name + ": " + self.handle.before )
1886 main.cleanup()
1887 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001888 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08001889 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001890 main.cleanup()
1891 main.exit()
Jon Hall390696c2015-05-05 17:13:41 -07001892
kelvin-onlabf512e942015-06-08 19:42:59 -07001893 def checkIntentState( self, intentsId, expectedState='INSTALLED' ):
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001894 """
1895 Description:
1896 Check intents state
1897 Required:
1898 intentsId - List of intents ID to be checked
1899 Optional:
kelvin-onlabf512e942015-06-08 19:42:59 -07001900 expectedState - Check the expected state(s) of each intents
1901 state in the list.
1902 *NOTE: You can pass in a list of expected state,
1903 Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ]
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001904 Return:
kelvin-onlabf512e942015-06-08 19:42:59 -07001905 Returns main.TRUE only if all intent are the same as expected states
1906 , otherwise, returns main.FALSE.
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001907 """
1908 try:
1909 # Generating a dictionary: intent id as a key and state as value
kelvin-onlabf512e942015-06-08 19:42:59 -07001910 returnValue = main.TRUE
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001911 intentsDict = self.getIntentState( intentsId )
kelvin-onlabf512e942015-06-08 19:42:59 -07001912
Jon Hall390696c2015-05-05 17:13:41 -07001913 #print "len of intentsDict ", str( len( intentsDict ) )
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001914 if len( intentsId ) != len( intentsDict ):
1915 main.log.info( self.name + "There is something wrong " +
1916 "getting intents state" )
1917 return main.FALSE
kelvin-onlabf512e942015-06-08 19:42:59 -07001918
1919 if isinstance( expectedState, types.StringType ):
1920 for intents in intentsDict:
1921 if intents.get( 'state' ) != expectedState:
kelvin-onlaba297c4d2015-06-01 13:53:55 -07001922 main.log.debug( self.name + " : Intent ID - " +
1923 intents.get( 'id' ) +
kelvin-onlabf512e942015-06-08 19:42:59 -07001924 " actual state = " +
1925 intents.get( 'state' )
1926 + " does not equal expected state = "
1927 + expectedState )
kelvin-onlaba297c4d2015-06-01 13:53:55 -07001928 returnValue = main.FALSE
kelvin-onlabf512e942015-06-08 19:42:59 -07001929
1930 elif isinstance( expectedState, types.ListType ):
1931 for intents in intentsDict:
1932 if not any( state == intents.get( 'state' ) for state in
1933 expectedState ):
1934 main.log.debug( self.name + " : Intent ID - " +
1935 intents.get( 'id' ) +
1936 " actual state = " +
1937 intents.get( 'state' ) +
1938 " does not equal expected states = "
1939 + str( expectedState ) )
1940 returnValue = main.FALSE
1941
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001942 if returnValue == main.TRUE:
1943 main.log.info( self.name + ": All " +
1944 str( len( intentsDict ) ) +
kelvin-onlabf512e942015-06-08 19:42:59 -07001945 " intents are in " + str( expectedState ) +
1946 " state" )
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07001947 return returnValue
1948 except TypeError:
1949 main.log.exception( self.name + ": Object not as expected" )
1950 return None
1951 except pexpect.EOF:
1952 main.log.error( self.name + ": EOF exception found" )
1953 main.log.error( self.name + ": " + self.handle.before )
1954 main.cleanup()
1955 main.exit()
1956 except Exception:
1957 main.log.exception( self.name + ": Uncaught exception!" )
1958 main.cleanup()
1959 main.exit()
andrewonlabe6745342014-10-17 14:29:13 -04001960
kelvin-onlabd3b64892015-01-20 13:26:24 -08001961 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001962 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001963 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001964 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001965 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001966 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001967 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001968 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07001969 cmdStr = "flows"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001970 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07001971 cmdStr += " -j"
1972 handle = self.sendline( cmdStr )
Jon Hall61282e32015-03-19 11:34:11 -07001973 if re.search( "Error:", handle ):
kelvin-onlaba297c4d2015-06-01 13:53:55 -07001974 main.log.error( self.name + ": flows() response: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001975 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001976 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001977 except TypeError:
1978 main.log.exception( self.name + ": Object not as expected" )
1979 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001980 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001981 main.log.error( self.name + ": EOF exception found" )
1982 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001983 main.cleanup()
1984 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001985 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001986 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001987 main.cleanup()
1988 main.exit()
1989
kelvin-onlab4df89f22015-04-13 18:10:23 -07001990 def checkFlowsState( self ):
1991 """
1992 Description:
1993 Check the if all the current flows are in ADDED state or
1994 PENDING_ADD state
1995 Return:
1996 returnValue - Returns main.TRUE only if all flows are in
1997 ADDED state or PENDING_ADD, return main.FALSE
1998 otherwise.
1999 """
2000 try:
2001 tempFlows = json.loads( self.flows() )
kelvin-onlabf0594d72015-05-19 17:25:12 -07002002 #print tempFlows[0]
kelvin-onlab4df89f22015-04-13 18:10:23 -07002003 returnValue = main.TRUE
kelvin-onlabf0594d72015-05-19 17:25:12 -07002004
kelvin-onlab4df89f22015-04-13 18:10:23 -07002005 for device in tempFlows:
2006 for flow in device.get( 'flows' ):
2007 if flow.get( 'state' ) != 'ADDED' and flow.get( 'state' ) != \
2008 'PENDING_ADD':
kelvin-onlabf2ec6e02015-05-27 14:15:28 -07002009
kelvin-onlab4df89f22015-04-13 18:10:23 -07002010 main.log.info( self.name + ": flow Id: " +
kelvin-onlabf2ec6e02015-05-27 14:15:28 -07002011 str( flow.get( 'groupId' ) ) +
2012 " | state:" +
2013 str( flow.get( 'state' ) ) )
kelvin-onlab4df89f22015-04-13 18:10:23 -07002014 returnValue = main.FALSE
kelvin-onlabf0594d72015-05-19 17:25:12 -07002015
kelvin-onlab4df89f22015-04-13 18:10:23 -07002016 return returnValue
2017 except TypeError:
2018 main.log.exception( self.name + ": Object not as expected" )
2019 return None
2020 except pexpect.EOF:
2021 main.log.error( self.name + ": EOF exception found" )
2022 main.log.error( self.name + ": " + self.handle.before )
2023 main.cleanup()
2024 main.exit()
2025 except Exception:
2026 main.log.exception( self.name + ": Uncaught exception!" )
2027 main.cleanup()
2028 main.exit()
2029
kelvin-onlabd3b64892015-01-20 13:26:24 -08002030 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
Jon Hallefbd9792015-03-05 16:11:36 -08002031 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08002032 """
andrewonlab87852b02014-11-19 18:44:19 -05002033 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08002034 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05002035 a specific point-to-point intent definition
2036 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002037 * dpidSrc: specify source dpid
2038 * dpidDst: specify destination dpid
2039 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05002040 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002041 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05002042 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08002043 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05002044 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05002045 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08002046 """
andrewonlab87852b02014-11-19 18:44:19 -05002047 try:
kelvin8ec71442015-01-15 16:57:00 -08002048 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08002049 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
2050 str( numIntents )
2051 if numMult:
2052 cmd += " " + str( numMult )
2053 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08002054 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08002055 if appId:
2056 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002057 handle = self.sendline( cmd )
andrewonlab87852b02014-11-19 18:44:19 -05002058 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002059 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08002060 main.log.info( handle )
2061 # Split result by newline
2062 newline = handle.split( "\r\r\n" )
2063 # Ignore the first object of list, which is empty
2064 newline = newline[ 1: ]
2065 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05002066 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08002067 result = result.split( ": " )
2068 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08002069 latResult.append( result[ 1 ].split( " " )[ 0 ] )
2070 main.log.info( latResult )
2071 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05002072 else:
2073 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08002074 except TypeError:
2075 main.log.exception( self.name + ": Object not as expected" )
2076 return None
andrewonlab87852b02014-11-19 18:44:19 -05002077 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002078 main.log.error( self.name + ": EOF exception found" )
2079 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05002080 main.cleanup()
2081 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002082 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002083 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05002084 main.cleanup()
2085 main.exit()
2086
kelvin-onlabd3b64892015-01-20 13:26:24 -08002087 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08002088 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002089 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002090 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002091 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08002092 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002093 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002094 cmdStr = "intents-events-metrics"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002095 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002096 cmdStr += " -j"
2097 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002098 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08002099 except TypeError:
2100 main.log.exception( self.name + ": Object not as expected" )
2101 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002102 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002103 main.log.error( self.name + ": EOF exception found" )
2104 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002105 main.cleanup()
2106 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002107 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002108 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002109 main.cleanup()
2110 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04002111
kelvin-onlabd3b64892015-01-20 13:26:24 -08002112 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08002113 """
2114 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04002115 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002116 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08002117 """
andrewonlab867212a2014-10-22 20:13:38 -04002118 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002119 cmdStr = "topology-events-metrics"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002120 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002121 cmdStr += " -j"
2122 handle = self.sendline( cmdStr )
jenkins7ead5a82015-03-13 10:28:21 -07002123 if handle:
2124 return handle
Jon Hallc6358dd2015-04-10 12:44:28 -07002125 elif jsonFormat:
Jon Hallbe379602015-03-24 13:39:32 -07002126 # Return empty json
jenkins7ead5a82015-03-13 10:28:21 -07002127 return '{}'
Jon Hallc6358dd2015-04-10 12:44:28 -07002128 else:
2129 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08002130 except TypeError:
2131 main.log.exception( self.name + ": Object not as expected" )
2132 return None
andrewonlab867212a2014-10-22 20:13:38 -04002133 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002134 main.log.error( self.name + ": EOF exception found" )
2135 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04002136 main.cleanup()
2137 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002138 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002139 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04002140 main.cleanup()
2141 main.exit()
2142
kelvin8ec71442015-01-15 16:57:00 -08002143 # Wrapper functions ****************
2144 # Wrapper functions use existing driver
2145 # functions and extends their use case.
2146 # For example, we may use the output of
2147 # a normal driver function, and parse it
2148 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04002149
kelvin-onlabd3b64892015-01-20 13:26:24 -08002150 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08002151 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04002152 Description:
2153 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08002154 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04002155 try:
kelvin8ec71442015-01-15 16:57:00 -08002156 # Obtain output of intents function
kelvin-onlabfb521662015-02-27 09:52:40 -08002157 intentsStr = self.intents(jsonFormat=False)
kelvin-onlabd3b64892015-01-20 13:26:24 -08002158 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04002159
kelvin8ec71442015-01-15 16:57:00 -08002160 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08002161 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
2162 for intents in intentsList:
kelvin-onlabfb521662015-02-27 09:52:40 -08002163 match = re.search('id=0x([\da-f]+),', intents)
2164 if match:
2165 tmpId = match.group()[3:-1]
2166 intentIdList.append( tmpId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002167 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04002168
Jon Halld4d4b372015-01-28 16:02:41 -08002169 except TypeError:
2170 main.log.exception( self.name + ": Object not as expected" )
2171 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04002172 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002173 main.log.error( self.name + ": EOF exception found" )
2174 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04002175 main.cleanup()
2176 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002177 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002178 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04002179 main.cleanup()
2180 main.exit()
2181
Jon Hall30b82fa2015-03-04 17:15:43 -08002182 def FlowAddedCount( self, deviceId ):
2183 """
2184 Determine the number of flow rules for the given device id that are
2185 in the added state
2186 """
2187 try:
2188 cmdStr = "flows any " + str( deviceId ) + " | " +\
2189 "grep 'state=ADDED' | wc -l"
2190 handle = self.sendline( cmdStr )
2191 return handle
2192 except pexpect.EOF:
2193 main.log.error( self.name + ": EOF exception found" )
2194 main.log.error( self.name + ": " + self.handle.before )
2195 main.cleanup()
2196 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002197 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -08002198 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -04002199 main.cleanup()
2200 main.exit()
2201
kelvin-onlabd3b64892015-01-20 13:26:24 -08002202 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08002203 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04002204 Use 'devices' function to obtain list of all devices
2205 and parse the result to obtain a list of all device
2206 id's. Returns this list. Returns empty list if no
2207 devices exist
kelvin8ec71442015-01-15 16:57:00 -08002208 List is ordered sequentially
2209
andrewonlab3e15ead2014-10-15 14:21:34 -04002210 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08002211 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04002212 the ids. By obtaining the list of device ids on the fly,
2213 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08002214 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04002215 try:
kelvin8ec71442015-01-15 16:57:00 -08002216 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08002217 devicesStr = self.devices( jsonFormat=False )
2218 idList = []
kelvin8ec71442015-01-15 16:57:00 -08002219
kelvin-onlabd3b64892015-01-20 13:26:24 -08002220 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08002221 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002222 return idList
kelvin8ec71442015-01-15 16:57:00 -08002223
2224 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08002225 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08002226 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08002227 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08002228 # Split list further into arguments before and after string
2229 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08002230 # append to idList
2231 for arg in tempList:
2232 idList.append( arg.split( "id=" )[ 1 ] )
2233 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04002234
Jon Halld4d4b372015-01-28 16:02:41 -08002235 except TypeError:
2236 main.log.exception( self.name + ": Object not as expected" )
2237 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04002238 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002239 main.log.error( self.name + ": EOF exception found" )
2240 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04002241 main.cleanup()
2242 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002243 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002244 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04002245 main.cleanup()
2246 main.exit()
2247
kelvin-onlabd3b64892015-01-20 13:26:24 -08002248 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08002249 """
andrewonlab7c211572014-10-15 16:45:20 -04002250 Uses 'nodes' function to obtain list of all nodes
2251 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08002252 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04002253 Returns:
2254 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08002255 """
andrewonlab7c211572014-10-15 16:45:20 -04002256 try:
Jon Hall5aa168b2015-03-23 14:23:09 -07002257 nodesStr = self.nodes( jsonFormat=True )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002258 idList = []
Jon Hall5aa168b2015-03-23 14:23:09 -07002259 # Sample nodesStr output
2260 # id=local, address=127.0.0.1:9876, state=ACTIVE *
kelvin-onlabd3b64892015-01-20 13:26:24 -08002261 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08002262 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002263 return idList
Jon Hall5aa168b2015-03-23 14:23:09 -07002264 nodesJson = json.loads( nodesStr )
2265 idList = [ node.get('id') for node in nodesJson ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08002266 return idList
kelvin8ec71442015-01-15 16:57:00 -08002267
Jon Halld4d4b372015-01-28 16:02:41 -08002268 except TypeError:
2269 main.log.exception( self.name + ": Object not as expected" )
2270 return None
andrewonlab7c211572014-10-15 16:45:20 -04002271 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002272 main.log.error( self.name + ": EOF exception found" )
2273 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04002274 main.cleanup()
2275 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002276 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002277 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04002278 main.cleanup()
2279 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04002280
kelvin-onlabd3b64892015-01-20 13:26:24 -08002281 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08002282 """
Jon Halla91c4dc2014-10-22 12:57:04 -04002283 Return the first device from the devices api whose 'id' contains 'dpid'
2284 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08002285 """
Jon Halla91c4dc2014-10-22 12:57:04 -04002286 try:
kelvin8ec71442015-01-15 16:57:00 -08002287 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04002288 return None
2289 else:
kelvin8ec71442015-01-15 16:57:00 -08002290 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002291 rawDevices = self.devices()
2292 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08002293 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08002294 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08002295 # print "%s in %s?" % ( dpid, device[ 'id' ] )
2296 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04002297 return device
2298 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002299 except TypeError:
2300 main.log.exception( self.name + ": Object not as expected" )
2301 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04002302 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002303 main.log.error( self.name + ": EOF exception found" )
2304 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04002305 main.cleanup()
2306 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002307 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002308 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04002309 main.cleanup()
2310 main.exit()
2311
kelvin-onlabd3b64892015-01-20 13:26:24 -08002312 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08002313 """
Jon Hallefbd9792015-03-05 16:11:36 -08002314 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08002315 supplied values. By default this will report to main.log, but the
Jon Hallefbd9792015-03-05 16:11:36 -08002316 log level can be specified.
kelvin8ec71442015-01-15 16:57:00 -08002317
Jon Hall42db6dc2014-10-24 19:03:48 -04002318 Params: ip = ip used for the onos cli
2319 numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08002320 numolink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08002321 logLevel = level to log to. Currently accepts
2322 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04002323
2324
kelvin-onlabd3b64892015-01-20 13:26:24 -08002325 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04002326
Jon Hallefbd9792015-03-05 16:11:36 -08002327 Returns: main.TRUE if the number of switches and links are correct,
2328 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04002329 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08002330 """
Jon Hall42db6dc2014-10-24 19:03:48 -04002331 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002332 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04002333 if topology == {}:
2334 return main.ERROR
2335 output = ""
kelvin8ec71442015-01-15 16:57:00 -08002336 # Is the number of switches is what we expected
2337 devices = topology.get( 'devices', False )
2338 links = topology.get( 'links', False )
kelvin-onlabfb521662015-02-27 09:52:40 -08002339 if devices is False or links is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04002340 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08002341 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08002342 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08002343 linkCheck = ( int( links ) == int( numolink ) )
2344 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08002345 # We expected the correct numbers
Jon Hallefbd9792015-03-05 16:11:36 -08002346 output += "The number of links and switches match " +\
2347 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04002348 result = main.TRUE
2349 else:
Jon Hallefbd9792015-03-05 16:11:36 -08002350 output += "The number of links and switches does not match " +\
2351 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04002352 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08002353 output = output + "\n ONOS sees %i devices (%i expected) \
2354 and %i links (%i expected)" % (
2355 int( devices ), int( numoswitch ), int( links ),
2356 int( numolink ) )
2357 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08002358 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002359 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08002360 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04002361 else:
Jon Hall390696c2015-05-05 17:13:41 -07002362 main.log.info( self.name + ": " + output )
kelvin8ec71442015-01-15 16:57:00 -08002363 return result
Jon Halld4d4b372015-01-28 16:02:41 -08002364 except TypeError:
2365 main.log.exception( self.name + ": Object not as expected" )
2366 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04002367 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002368 main.log.error( self.name + ": EOF exception found" )
2369 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04002370 main.cleanup()
2371 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002372 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002373 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04002374 main.cleanup()
2375 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002376
kelvin-onlabd3b64892015-01-20 13:26:24 -08002377 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08002378 """
Jon Hall1c9e8732014-10-27 19:29:27 -04002379 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08002380 deviceId must be the id of a device as seen in the onos devices command
2381 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04002382 role must be either master, standby, or none
2383
Jon Halle3f39ff2015-01-13 11:50:53 -08002384 Returns:
2385 main.TRUE or main.FALSE based on argument verification and
2386 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08002387 """
Jon Hall1c9e8732014-10-27 19:29:27 -04002388 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08002389 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04002390 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08002391 cmdStr = "device-role " +\
2392 str( deviceId ) + " " +\
2393 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002394 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002395 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002396 if re.search( "Error", handle ):
2397 # end color output to escape any colours
2398 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08002399 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002400 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08002401 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08002402 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04002403 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002404 main.log.error( "Invalid 'role' given to device_role(). " +
2405 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04002406 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002407 except TypeError:
2408 main.log.exception( self.name + ": Object not as expected" )
2409 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04002410 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002411 main.log.error( self.name + ": EOF exception found" )
2412 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04002413 main.cleanup()
2414 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002415 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002416 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04002417 main.cleanup()
2418 main.exit()
2419
kelvin-onlabd3b64892015-01-20 13:26:24 -08002420 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08002421 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08002422 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08002423 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002424 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08002425 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08002426 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002427 cmdStr = "clusters"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002428 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002429 cmdStr += " -j"
2430 handle = self.sendline( cmdStr )
2431 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08002432 except TypeError:
2433 main.log.exception( self.name + ": Object not as expected" )
2434 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08002435 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002436 main.log.error( self.name + ": EOF exception found" )
2437 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08002438 main.cleanup()
2439 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002440 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002441 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08002442 main.cleanup()
2443 main.exit()
2444
kelvin-onlabd3b64892015-01-20 13:26:24 -08002445 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002446 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002447 CLI command to get the current leader for the Election test application
2448 NOTE: Requires installation of the onos-app-election feature
2449 Returns: Node IP of the leader if one exists
2450 None if none exists
2451 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08002452 """
Jon Hall94fd0472014-12-08 11:52:42 -08002453 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002454 cmdStr = "election-test-leader"
2455 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002456 # Leader
2457 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002458 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08002459 nodeSearch = re.search( leaderPattern, response )
2460 if nodeSearch:
2461 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08002462 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002463 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08002464 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08002465 # no leader
2466 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002467 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002468 nullSearch = re.search( nullPattern, response )
2469 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08002470 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002471 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08002472 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08002473 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002474 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08002475 if re.search( errorPattern, response ):
2476 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08002477 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08002478 return main.FALSE
2479 else:
Jon Hall390696c2015-05-05 17:13:41 -07002480 main.log.error( "Error in electionTestLeader on " + self.name +
2481 ": " + "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08002482 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002483 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002484 except TypeError:
2485 main.log.exception( self.name + ": Object not as expected" )
2486 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002487 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002488 main.log.error( self.name + ": EOF exception found" )
2489 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002490 main.cleanup()
2491 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002492 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002493 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002494 main.cleanup()
2495 main.exit()
2496
kelvin-onlabd3b64892015-01-20 13:26:24 -08002497 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002498 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002499 CLI command to run for leadership of the Election test application.
2500 NOTE: Requires installation of the onos-app-election feature
2501 Returns: Main.TRUE on success
2502 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08002503 """
Jon Hall94fd0472014-12-08 11:52:42 -08002504 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002505 cmdStr = "election-test-run"
2506 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002507 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002508 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002509 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002510 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08002511 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08002512 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002513 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002514 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002515 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002516 errorPattern = "Command\snot\sfound"
2517 if re.search( errorPattern, response ):
2518 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002519 return main.FALSE
2520 else:
Jon Hall390696c2015-05-05 17:13:41 -07002521 main.log.error( "Error in electionTestRun on " + self.name +
2522 ": " + "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002523 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002524 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002525 except TypeError:
2526 main.log.exception( self.name + ": Object not as expected" )
2527 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002528 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002529 main.log.error( self.name + ": EOF exception found" )
2530 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002531 main.cleanup()
2532 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002533 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002534 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002535 main.cleanup()
2536 main.exit()
2537
kelvin-onlabd3b64892015-01-20 13:26:24 -08002538 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08002539 """
Jon Hall94fd0472014-12-08 11:52:42 -08002540 * CLI command to withdraw the local node from leadership election for
2541 * the Election test application.
2542 #NOTE: Requires installation of the onos-app-election feature
2543 Returns: Main.TRUE on success
2544 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08002545 """
Jon Hall94fd0472014-12-08 11:52:42 -08002546 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002547 cmdStr = "election-test-withdraw"
2548 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08002549 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08002550 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08002551 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08002552 if re.search( successPattern, response ):
2553 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08002554 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08002555 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08002556 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08002557 errorPattern = "Command\snot\sfound"
2558 if re.search( errorPattern, response ):
2559 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002560 return main.FALSE
2561 else:
Jon Hall390696c2015-05-05 17:13:41 -07002562 main.log.error( "Error in electionTestWithdraw on " +
2563 self.name + ": " + "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002564 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002565 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08002566 except TypeError:
2567 main.log.exception( self.name + ": Object not as expected" )
2568 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002569 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002570 main.log.error( self.name + ": EOF exception found" )
2571 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002572 main.cleanup()
2573 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002574 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002575 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08002576 main.cleanup()
2577 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002578
kelvin8ec71442015-01-15 16:57:00 -08002579 def getDevicePortsEnabledCount( self, dpid ):
2580 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002581 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002582 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002583 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08002584 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002585 cmdStr = "onos:ports -e " + dpid + " | wc -l"
2586 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002587 if re.search( "No such device", output ):
2588 main.log.error( "Error in getting ports" )
2589 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002590 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002591 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002592 except TypeError:
2593 main.log.exception( self.name + ": Object not as expected" )
2594 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002595 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002596 main.log.error( self.name + ": EOF exception found" )
2597 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002598 main.cleanup()
2599 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002600 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002601 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002602 main.cleanup()
2603 main.exit()
2604
kelvin8ec71442015-01-15 16:57:00 -08002605 def getDeviceLinksActiveCount( self, dpid ):
2606 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002607 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002608 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002609 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002610 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002611 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
2612 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002613 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002614 main.log.error( "Error in getting ports " )
2615 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002616 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002617 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002618 except TypeError:
2619 main.log.exception( self.name + ": Object not as expected" )
2620 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002621 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002622 main.log.error( self.name + ": EOF exception found" )
2623 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002624 main.cleanup()
2625 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002626 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002627 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002628 main.cleanup()
2629 main.exit()
2630
kelvin8ec71442015-01-15 16:57:00 -08002631 def getAllIntentIds( self ):
2632 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002633 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002634 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002635 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002636 cmdStr = "onos:intents | grep id="
2637 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002638 if re.search( "Error", output ):
2639 main.log.error( "Error in getting ports" )
2640 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002641 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002642 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002643 except TypeError:
2644 main.log.exception( self.name + ": Object not as expected" )
2645 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002646 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002647 main.log.error( self.name + ": EOF exception found" )
2648 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002649 main.cleanup()
2650 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002651 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002652 main.log.exception( self.name + ": Uncaught exception!" )
2653 main.cleanup()
2654 main.exit()
2655
Jon Hall73509952015-02-24 16:42:56 -08002656 def intentSummary( self ):
2657 """
Jon Hallefbd9792015-03-05 16:11:36 -08002658 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08002659 """
2660 try:
2661 intents = self.intents( )
Jon Hall08f61bc2015-04-13 16:00:30 -07002662 states = []
Jon Hall5aa168b2015-03-23 14:23:09 -07002663 for intent in json.loads( intents ):
Jon Hall08f61bc2015-04-13 16:00:30 -07002664 states.append( intent.get( 'state', None ) )
2665 out = [ ( i, states.count( i ) ) for i in set( states ) ]
Jon Hall63604932015-02-26 17:09:50 -08002666 main.log.info( dict( out ) )
Jon Hall73509952015-02-24 16:42:56 -08002667 return dict( out )
2668 except TypeError:
2669 main.log.exception( self.name + ": Object not as expected" )
2670 return None
2671 except pexpect.EOF:
2672 main.log.error( self.name + ": EOF exception found" )
2673 main.log.error( self.name + ": " + self.handle.before )
2674 main.cleanup()
2675 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002676 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08002677 main.log.exception( self.name + ": Uncaught exception!" )
2678 main.cleanup()
2679 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002680
Jon Hall61282e32015-03-19 11:34:11 -07002681 def leaders( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08002682 """
2683 Returns the output of the leaders command.
Jon Hall61282e32015-03-19 11:34:11 -07002684 Optional argument:
2685 * jsonFormat - boolean indicating if you want output in json
Jon Hall63604932015-02-26 17:09:50 -08002686 """
Jon Hall63604932015-02-26 17:09:50 -08002687 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002688 cmdStr = "onos:leaders"
Jon Hall61282e32015-03-19 11:34:11 -07002689 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002690 cmdStr += " -j"
2691 output = self.sendline( cmdStr )
2692 return output
Jon Hall63604932015-02-26 17:09:50 -08002693 except TypeError:
2694 main.log.exception( self.name + ": Object not as expected" )
2695 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002696 except pexpect.EOF:
2697 main.log.error( self.name + ": EOF exception found" )
2698 main.log.error( self.name + ": " + self.handle.before )
2699 main.cleanup()
2700 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002701 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08002702 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002703 main.cleanup()
2704 main.exit()
Jon Hall63604932015-02-26 17:09:50 -08002705
acsmarsa4a4d1e2015-07-10 16:01:24 -07002706 def leaderCandidates( self, jsonFormat=True ):
2707 """
2708 Returns the output of the leaders -c command.
2709 Optional argument:
2710 * jsonFormat - boolean indicating if you want output in json
2711 """
2712 try:
2713 cmdStr = "onos:leaders -c"
2714 if jsonFormat:
2715 cmdStr += " -j"
2716 output = self.sendline( cmdStr )
2717 return output
2718 except TypeError:
2719 main.log.exception( self.name + ": Object not as expected" )
2720 return None
2721 except pexpect.EOF:
2722 main.log.error( self.name + ": EOF exception found" )
2723 main.log.error( self.name + ": " + self.handle.before )
2724 main.cleanup()
2725 main.exit()
2726 except Exception:
2727 main.log.exception( self.name + ": Uncaught exception!" )
2728 main.cleanup()
2729 main.exit()
2730
2731 def specificLeaderCandidate(self,topic):
2732 """
2733 Returns a list in format [leader,candidate1,candidate2,...] for a given
2734 topic parameter and an empty list if the topic doesn't exist
2735 If no leader is elected leader in the returned list will be "none"
2736 Returns None if there is a type error processing the json object
2737 """
2738 try:
2739 cmdStr = "onos:leaders -c -j"
2740 output = self.sendline( cmdStr )
2741 output = json.loads(output)
2742 results = []
2743 for dict in output:
2744 if dict["topic"] == topic:
2745 leader = dict["leader"]
2746 candidates = re.split(", ",dict["candidates"][1:-1])
2747 results.append(leader)
2748 results.extend(candidates)
2749 return results
2750 except TypeError:
2751 main.log.exception( self.name + ": Object not as expected" )
2752 return None
2753 except pexpect.EOF:
2754 main.log.error( self.name + ": EOF exception found" )
2755 main.log.error( self.name + ": " + self.handle.before )
2756 main.cleanup()
2757 main.exit()
2758 except Exception:
2759 main.log.exception( self.name + ": Uncaught exception!" )
2760 main.cleanup()
2761 main.exit()
2762
Jon Hall61282e32015-03-19 11:34:11 -07002763 def pendingMap( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08002764 """
2765 Returns the output of the intent Pending map.
2766 """
Jon Hall63604932015-02-26 17:09:50 -08002767 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002768 cmdStr = "onos:intents -p"
Jon Hall61282e32015-03-19 11:34:11 -07002769 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002770 cmdStr += " -j"
2771 output = self.sendline( cmdStr )
2772 return output
Jon Hall63604932015-02-26 17:09:50 -08002773 except TypeError:
2774 main.log.exception( self.name + ": Object not as expected" )
2775 return None
2776 except pexpect.EOF:
2777 main.log.error( self.name + ": EOF exception found" )
2778 main.log.error( self.name + ": " + self.handle.before )
2779 main.cleanup()
2780 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002781 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08002782 main.log.exception( self.name + ": Uncaught exception!" )
2783 main.cleanup()
2784 main.exit()
2785
Jon Hall61282e32015-03-19 11:34:11 -07002786 def partitions( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08002787 """
2788 Returns the output of the raft partitions command for ONOS.
2789 """
Jon Hall61282e32015-03-19 11:34:11 -07002790 # Sample JSON
2791 # {
2792 # "leader": "tcp://10.128.30.11:7238",
2793 # "members": [
2794 # "tcp://10.128.30.11:7238",
2795 # "tcp://10.128.30.17:7238",
2796 # "tcp://10.128.30.13:7238",
2797 # ],
2798 # "name": "p1",
2799 # "term": 3
2800 # },
Jon Hall63604932015-02-26 17:09:50 -08002801 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002802 cmdStr = "onos:partitions"
Jon Hall61282e32015-03-19 11:34:11 -07002803 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002804 cmdStr += " -j"
2805 output = self.sendline( cmdStr )
2806 return output
Jon Hall63604932015-02-26 17:09:50 -08002807 except TypeError:
2808 main.log.exception( self.name + ": Object not as expected" )
2809 return None
2810 except pexpect.EOF:
2811 main.log.error( self.name + ": EOF exception found" )
2812 main.log.error( self.name + ": " + self.handle.before )
2813 main.cleanup()
2814 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002815 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08002816 main.log.exception( self.name + ": Uncaught exception!" )
2817 main.cleanup()
2818 main.exit()
2819
Jon Hallbe379602015-03-24 13:39:32 -07002820 def apps( self, jsonFormat=True ):
2821 """
2822 Returns the output of the apps command for ONOS. This command lists
2823 information about installed ONOS applications
2824 """
2825 # Sample JSON object
2826 # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0",
2827 # "description":"ONOS OpenFlow protocol southbound providers",
2828 # "origin":"ON.Lab","permissions":"[]","featuresRepo":"",
2829 # "features":"[onos-openflow]","state":"ACTIVE"}]
2830 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002831 cmdStr = "onos:apps"
Jon Hallbe379602015-03-24 13:39:32 -07002832 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002833 cmdStr += " -j"
2834 output = self.sendline( cmdStr )
2835 assert "Error executing command" not in output
2836 return output
Jon Hallbe379602015-03-24 13:39:32 -07002837 # FIXME: look at specific exceptions/Errors
2838 except AssertionError:
2839 main.log.error( "Error in processing onos:app command: " +
2840 str( output ) )
2841 return None
2842 except TypeError:
2843 main.log.exception( self.name + ": Object not as expected" )
2844 return None
2845 except pexpect.EOF:
2846 main.log.error( self.name + ": EOF exception found" )
2847 main.log.error( self.name + ": " + self.handle.before )
2848 main.cleanup()
2849 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002850 except Exception:
Jon Hallbe379602015-03-24 13:39:32 -07002851 main.log.exception( self.name + ": Uncaught exception!" )
2852 main.cleanup()
2853 main.exit()
2854
Jon Hall146f1522015-03-24 15:33:24 -07002855 def appStatus( self, appName ):
2856 """
2857 Uses the onos:apps cli command to return the status of an application.
2858 Returns:
2859 "ACTIVE" - If app is installed and activated
2860 "INSTALLED" - If app is installed and deactivated
2861 "UNINSTALLED" - If app is not installed
2862 None - on error
2863 """
Jon Hall146f1522015-03-24 15:33:24 -07002864 try:
2865 if not isinstance( appName, types.StringType ):
2866 main.log.error( self.name + ".appStatus(): appName must be" +
2867 " a string" )
2868 return None
2869 output = self.apps( jsonFormat=True )
2870 appsJson = json.loads( output )
2871 state = None
2872 for app in appsJson:
2873 if appName == app.get('name'):
2874 state = app.get('state')
2875 break
2876 if state == "ACTIVE" or state == "INSTALLED":
2877 return state
2878 elif state is None:
2879 return "UNINSTALLED"
2880 elif state:
2881 main.log.error( "Unexpected state from 'onos:apps': " +
2882 str( state ) )
2883 return state
2884 except TypeError:
2885 main.log.exception( self.name + ": Object not as expected" )
2886 return None
2887 except pexpect.EOF:
2888 main.log.error( self.name + ": EOF exception found" )
2889 main.log.error( self.name + ": " + self.handle.before )
2890 main.cleanup()
2891 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002892 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07002893 main.log.exception( self.name + ": Uncaught exception!" )
2894 main.cleanup()
2895 main.exit()
2896
Jon Hallbe379602015-03-24 13:39:32 -07002897 def app( self, appName, option ):
2898 """
2899 Interacts with the app command for ONOS. This command manages
2900 application inventory.
2901 """
Jon Hallbe379602015-03-24 13:39:32 -07002902 try:
Jon Hallbd16b922015-03-26 17:53:15 -07002903 # Validate argument types
2904 valid = True
2905 if not isinstance( appName, types.StringType ):
2906 main.log.error( self.name + ".app(): appName must be a " +
2907 "string" )
2908 valid = False
2909 if not isinstance( option, types.StringType ):
2910 main.log.error( self.name + ".app(): option must be a string" )
2911 valid = False
2912 if not valid:
2913 return main.FALSE
2914 # Validate Option
2915 option = option.lower()
2916 # NOTE: Install may become a valid option
2917 if option == "activate":
2918 pass
2919 elif option == "deactivate":
2920 pass
2921 elif option == "uninstall":
2922 pass
2923 else:
2924 # Invalid option
2925 main.log.error( "The ONOS app command argument only takes " +
2926 "the values: (activate|deactivate|uninstall)" +
2927 "; was given '" + option + "'")
2928 return main.FALSE
Jon Hall146f1522015-03-24 15:33:24 -07002929 cmdStr = "onos:app " + option + " " + appName
Jon Hallbe379602015-03-24 13:39:32 -07002930 output = self.sendline( cmdStr )
Jon Hallbe379602015-03-24 13:39:32 -07002931 if "Error executing command" in output:
2932 main.log.error( "Error in processing onos:app command: " +
2933 str( output ) )
Jon Hall146f1522015-03-24 15:33:24 -07002934 return main.FALSE
Jon Hallbe379602015-03-24 13:39:32 -07002935 elif "No such application" in output:
2936 main.log.error( "The application '" + appName +
2937 "' is not installed in ONOS" )
Jon Hall146f1522015-03-24 15:33:24 -07002938 return main.FALSE
2939 elif "Command not found:" in output:
2940 main.log.error( "Error in processing onos:app command: " +
2941 str( output ) )
2942 return main.FALSE
Jon Hallbd16b922015-03-26 17:53:15 -07002943 elif "Unsupported command:" in output:
2944 main.log.error( "Incorrect command given to 'app': " +
2945 str( output ) )
Jon Hallbe379602015-03-24 13:39:32 -07002946 # NOTE: we may need to add more checks here
Jon Hallbd16b922015-03-26 17:53:15 -07002947 # else: Command was successful
Jon Hall08f61bc2015-04-13 16:00:30 -07002948 # main.log.debug( "app response: " + repr( output ) )
Jon Hallbe379602015-03-24 13:39:32 -07002949 return main.TRUE
2950 except TypeError:
2951 main.log.exception( self.name + ": Object not as expected" )
2952 return main.ERROR
2953 except pexpect.EOF:
2954 main.log.error( self.name + ": EOF exception found" )
2955 main.log.error( self.name + ": " + self.handle.before )
2956 main.cleanup()
2957 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07002958 except Exception:
Jon Hallbe379602015-03-24 13:39:32 -07002959 main.log.exception( self.name + ": Uncaught exception!" )
2960 main.cleanup()
2961 main.exit()
Jon Hall146f1522015-03-24 15:33:24 -07002962
Jon Hallbd16b922015-03-26 17:53:15 -07002963 def activateApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07002964 """
2965 Activate an app that is already installed in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07002966 appName is the hierarchical app name, not the feature name
2967 If check is True, method will check the status of the app after the
2968 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07002969 Returns main.TRUE if the command was successfully sent
2970 main.FALSE if the cli responded with an error or given
2971 incorrect input
2972 """
2973 try:
2974 if not isinstance( appName, types.StringType ):
2975 main.log.error( self.name + ".activateApp(): appName must be" +
2976 " a string" )
2977 return main.FALSE
2978 status = self.appStatus( appName )
2979 if status == "INSTALLED":
2980 response = self.app( appName, "activate" )
Jon Hallbd16b922015-03-26 17:53:15 -07002981 if check and response == main.TRUE:
2982 for i in range(10): # try 10 times then give up
2983 # TODO: Check with Thomas about this delay
2984 status = self.appStatus( appName )
2985 if status == "ACTIVE":
2986 return main.TRUE
2987 else:
Jon Hall050e1bd2015-03-30 13:33:02 -07002988 main.log.debug( "The state of application " +
2989 appName + " is " + status )
Jon Hallbd16b922015-03-26 17:53:15 -07002990 time.sleep( 1 )
2991 return main.FALSE
2992 else: # not 'check' or command didn't succeed
2993 return response
Jon Hall146f1522015-03-24 15:33:24 -07002994 elif status == "ACTIVE":
2995 return main.TRUE
2996 elif status == "UNINSTALLED":
2997 main.log.error( self.name + ": Tried to activate the " +
2998 "application '" + appName + "' which is not " +
2999 "installed." )
3000 else:
3001 main.log.error( "Unexpected return value from appStatus: " +
3002 str( status ) )
3003 return main.ERROR
3004 except TypeError:
3005 main.log.exception( self.name + ": Object not as expected" )
3006 return main.ERROR
3007 except pexpect.EOF:
3008 main.log.error( self.name + ": EOF exception found" )
3009 main.log.error( self.name + ": " + self.handle.before )
3010 main.cleanup()
3011 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003012 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003013 main.log.exception( self.name + ": Uncaught exception!" )
3014 main.cleanup()
3015 main.exit()
3016
Jon Hallbd16b922015-03-26 17:53:15 -07003017 def deactivateApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07003018 """
3019 Deactivate an app that is already activated in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07003020 appName is the hierarchical app name, not the feature name
3021 If check is True, method will check the status of the app after the
3022 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07003023 Returns main.TRUE if the command was successfully sent
3024 main.FALSE if the cli responded with an error or given
3025 incorrect input
3026 """
3027 try:
3028 if not isinstance( appName, types.StringType ):
3029 main.log.error( self.name + ".deactivateApp(): appName must " +
3030 "be a string" )
3031 return main.FALSE
3032 status = self.appStatus( appName )
3033 if status == "INSTALLED":
3034 return main.TRUE
3035 elif status == "ACTIVE":
3036 response = self.app( appName, "deactivate" )
Jon Hallbd16b922015-03-26 17:53:15 -07003037 if check and response == main.TRUE:
3038 for i in range(10): # try 10 times then give up
3039 status = self.appStatus( appName )
3040 if status == "INSTALLED":
3041 return main.TRUE
3042 else:
3043 time.sleep( 1 )
3044 return main.FALSE
3045 else: # not check or command didn't succeed
3046 return response
Jon Hall146f1522015-03-24 15:33:24 -07003047 elif status == "UNINSTALLED":
3048 main.log.warn( self.name + ": Tried to deactivate the " +
3049 "application '" + appName + "' which is not " +
3050 "installed." )
3051 return main.TRUE
3052 else:
3053 main.log.error( "Unexpected return value from appStatus: " +
3054 str( status ) )
3055 return main.ERROR
3056 except TypeError:
3057 main.log.exception( self.name + ": Object not as expected" )
3058 return main.ERROR
3059 except pexpect.EOF:
3060 main.log.error( self.name + ": EOF exception found" )
3061 main.log.error( self.name + ": " + self.handle.before )
3062 main.cleanup()
3063 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003064 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003065 main.log.exception( self.name + ": Uncaught exception!" )
3066 main.cleanup()
3067 main.exit()
3068
Jon Hallbd16b922015-03-26 17:53:15 -07003069 def uninstallApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07003070 """
3071 Uninstall an app that is already installed in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07003072 appName is the hierarchical app name, not the feature name
3073 If check is True, method will check the status of the app after the
3074 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07003075 Returns main.TRUE if the command was successfully sent
3076 main.FALSE if the cli responded with an error or given
3077 incorrect input
3078 """
3079 # TODO: check with Thomas about the state machine for apps
3080 try:
3081 if not isinstance( appName, types.StringType ):
3082 main.log.error( self.name + ".uninstallApp(): appName must " +
3083 "be a string" )
3084 return main.FALSE
3085 status = self.appStatus( appName )
3086 if status == "INSTALLED":
3087 response = self.app( appName, "uninstall" )
Jon Hallbd16b922015-03-26 17:53:15 -07003088 if check and response == main.TRUE:
3089 for i in range(10): # try 10 times then give up
3090 status = self.appStatus( appName )
3091 if status == "UNINSTALLED":
3092 return main.TRUE
3093 else:
3094 time.sleep( 1 )
3095 return main.FALSE
3096 else: # not check or command didn't succeed
3097 return response
Jon Hall146f1522015-03-24 15:33:24 -07003098 elif status == "ACTIVE":
3099 main.log.warn( self.name + ": Tried to uninstall the " +
3100 "application '" + appName + "' which is " +
3101 "currently active." )
3102 response = self.app( appName, "uninstall" )
Jon Hallbd16b922015-03-26 17:53:15 -07003103 if check and response == main.TRUE:
3104 for i in range(10): # try 10 times then give up
3105 status = self.appStatus( appName )
3106 if status == "UNINSTALLED":
3107 return main.TRUE
3108 else:
3109 time.sleep( 1 )
3110 return main.FALSE
3111 else: # not check or command didn't succeed
3112 return response
Jon Hall146f1522015-03-24 15:33:24 -07003113 elif status == "UNINSTALLED":
3114 return main.TRUE
3115 else:
3116 main.log.error( "Unexpected return value from appStatus: " +
3117 str( status ) )
3118 return main.ERROR
3119 except TypeError:
3120 main.log.exception( self.name + ": Object not as expected" )
3121 return main.ERROR
3122 except pexpect.EOF:
3123 main.log.error( self.name + ": EOF exception found" )
3124 main.log.error( self.name + ": " + self.handle.before )
3125 main.cleanup()
3126 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003127 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003128 main.log.exception( self.name + ": Uncaught exception!" )
3129 main.cleanup()
3130 main.exit()
Jon Hallbd16b922015-03-26 17:53:15 -07003131
3132 def appIDs( self, jsonFormat=True ):
3133 """
3134 Show the mappings between app id and app names given by the 'app-ids'
3135 cli command
3136 """
3137 try:
3138 cmdStr = "app-ids"
3139 if jsonFormat:
3140 cmdStr += " -j"
Jon Hallc6358dd2015-04-10 12:44:28 -07003141 output = self.sendline( cmdStr )
3142 assert "Error executing command" not in output
3143 return output
Jon Hallbd16b922015-03-26 17:53:15 -07003144 except AssertionError:
3145 main.log.error( "Error in processing onos:app-ids command: " +
3146 str( output ) )
3147 return None
3148 except TypeError:
3149 main.log.exception( self.name + ": Object not as expected" )
3150 return None
3151 except pexpect.EOF:
3152 main.log.error( self.name + ": EOF exception found" )
3153 main.log.error( self.name + ": " + self.handle.before )
3154 main.cleanup()
3155 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003156 except Exception:
Jon Hallbd16b922015-03-26 17:53:15 -07003157 main.log.exception( self.name + ": Uncaught exception!" )
3158 main.cleanup()
3159 main.exit()
3160
3161 def appToIDCheck( self ):
3162 """
3163 This method will check that each application's ID listed in 'apps' is
3164 the same as the ID listed in 'app-ids'. The check will also check that
3165 there are no duplicate IDs issued. Note that an app ID should be
3166 a globaly unique numerical identifier for app/app-like features. Once
3167 an ID is registered, the ID is never freed up so that if an app is
3168 reinstalled it will have the same ID.
3169
3170 Returns: main.TRUE if the check passes and
3171 main.FALSE if the check fails or
3172 main.ERROR if there is some error in processing the test
3173 """
3174 try:
Jon Hall390696c2015-05-05 17:13:41 -07003175 bail = False
3176 ids = self.appIDs( jsonFormat=True )
3177 if ids:
3178 ids = json.loads( ids )
3179 else:
3180 main.log.error( "app-ids returned nothing:" + repr( ids ) )
3181 bail = True
3182 apps = self.apps( jsonFormat=True )
3183 if apps:
3184 apps = json.loads( apps )
3185 else:
3186 main.log.error( "apps returned nothing:" + repr( apps ) )
3187 bail = True
3188 if bail:
3189 return main.FALSE
Jon Hallbd16b922015-03-26 17:53:15 -07003190 result = main.TRUE
3191 for app in apps:
3192 appID = app.get( 'id' )
3193 if appID is None:
3194 main.log.error( "Error parsing app: " + str( app ) )
3195 result = main.FALSE
3196 appName = app.get( 'name' )
3197 if appName is None:
3198 main.log.error( "Error parsing app: " + str( app ) )
3199 result = main.FALSE
3200 # get the entry in ids that has the same appID
Jon Hall390696c2015-05-05 17:13:41 -07003201 current = filter( lambda item: item[ 'id' ] == appID, ids )
Jon Hall050e1bd2015-03-30 13:33:02 -07003202 # main.log.debug( "Comparing " + str( app ) + " to " +
3203 # str( current ) )
Jon Hallbd16b922015-03-26 17:53:15 -07003204 if not current: # if ids doesn't have this id
3205 result = main.FALSE
3206 main.log.error( "'app-ids' does not have the ID for " +
3207 str( appName ) + " that apps does." )
3208 elif len( current ) > 1:
3209 # there is more than one app with this ID
3210 result = main.FALSE
3211 # We will log this later in the method
3212 elif not current[0][ 'name' ] == appName:
3213 currentName = current[0][ 'name' ]
3214 result = main.FALSE
3215 main.log.error( "'app-ids' has " + str( currentName ) +
3216 " registered under id:" + str( appID ) +
3217 " but 'apps' has " + str( appName ) )
3218 else:
3219 pass # id and name match!
3220 # now make sure that app-ids has no duplicates
3221 idsList = []
3222 namesList = []
3223 for item in ids:
3224 idsList.append( item[ 'id' ] )
3225 namesList.append( item[ 'name' ] )
3226 if len( idsList ) != len( set( idsList ) ) or\
3227 len( namesList ) != len( set( namesList ) ):
3228 main.log.error( "'app-ids' has some duplicate entries: \n"
3229 + json.dumps( ids,
3230 sort_keys=True,
3231 indent=4,
3232 separators=( ',', ': ' ) ) )
3233 result = main.FALSE
3234 return result
3235 except ( ValueError, TypeError ):
3236 main.log.exception( self.name + ": Object not as expected" )
3237 return main.ERROR
3238 except pexpect.EOF:
3239 main.log.error( self.name + ": EOF exception found" )
3240 main.log.error( self.name + ": " + self.handle.before )
3241 main.cleanup()
3242 main.exit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003243 except Exception:
Jon Hallbd16b922015-03-26 17:53:15 -07003244 main.log.exception( self.name + ": Uncaught exception!" )
3245 main.cleanup()
3246 main.exit()
3247
Jon Hallfb760a02015-04-13 15:35:03 -07003248 def getCfg( self, component=None, propName=None, short=False,
3249 jsonFormat=True ):
3250 """
3251 Get configuration settings from onos cli
3252 Optional arguments:
3253 component - Optionally only list configurations for a specific
3254 component. If None, all components with configurations
3255 are displayed. Case Sensitive string.
3256 propName - If component is specified, propName option will show
3257 only this specific configuration from that component.
3258 Case Sensitive string.
3259 jsonFormat - Returns output as json. Note that this will override
3260 the short option
3261 short - Short, less verbose, version of configurations.
3262 This is overridden by the json option
3263 returns:
3264 Output from cli as a string or None on error
3265 """
3266 try:
3267 baseStr = "cfg"
3268 cmdStr = " get"
3269 componentStr = ""
3270 if component:
3271 componentStr += " " + component
3272 if propName:
3273 componentStr += " " + propName
3274 if jsonFormat:
3275 baseStr += " -j"
3276 elif short:
3277 baseStr += " -s"
3278 output = self.sendline( baseStr + cmdStr + componentStr )
3279 assert "Error executing command" not in output
3280 return output
3281 except AssertionError:
3282 main.log.error( "Error in processing 'cfg get' command: " +
3283 str( output ) )
3284 return None
3285 except TypeError:
3286 main.log.exception( self.name + ": Object not as expected" )
3287 return None
3288 except pexpect.EOF:
3289 main.log.error( self.name + ": EOF exception found" )
3290 main.log.error( self.name + ": " + self.handle.before )
3291 main.cleanup()
3292 main.exit()
3293 except Exception:
3294 main.log.exception( self.name + ": Uncaught exception!" )
3295 main.cleanup()
3296 main.exit()
3297
3298 def setCfg( self, component, propName, value=None, check=True ):
3299 """
3300 Set/Unset configuration settings from ONOS cli
Jon Hall390696c2015-05-05 17:13:41 -07003301 Required arguments:
Jon Hallfb760a02015-04-13 15:35:03 -07003302 component - The case sensitive name of the component whose
3303 property is to be set
3304 propName - The case sensitive name of the property to be set/unset
Jon Hall390696c2015-05-05 17:13:41 -07003305 Optional arguments:
Jon Hallfb760a02015-04-13 15:35:03 -07003306 value - The value to set the property to. If None, will unset the
3307 property and revert it to it's default value(if applicable)
3308 check - Boolean, Check whether the option was successfully set this
3309 only applies when a value is given.
3310 returns:
3311 main.TRUE on success or main.FALSE on failure. If check is False,
3312 will return main.TRUE unless there is an error
3313 """
3314 try:
3315 baseStr = "cfg"
3316 cmdStr = " set " + str( component ) + " " + str( propName )
3317 if value is not None:
3318 cmdStr += " " + str( value )
3319 output = self.sendline( baseStr + cmdStr )
3320 assert "Error executing command" not in output
3321 if value and check:
3322 results = self.getCfg( component=str( component ),
3323 propName=str( propName ),
3324 jsonFormat=True )
3325 # Check if current value is what we just set
3326 try:
3327 jsonOutput = json.loads( results )
3328 current = jsonOutput[ 'value' ]
3329 except ( ValueError, TypeError ):
3330 main.log.exception( "Error parsing cfg output" )
3331 main.log.error( "output:" + repr( results ) )
3332 return main.FALSE
3333 if current == str( value ):
3334 return main.TRUE
3335 return main.FALSE
3336 return main.TRUE
3337 except AssertionError:
3338 main.log.error( "Error in processing 'cfg set' command: " +
3339 str( output ) )
3340 return main.FALSE
3341 except TypeError:
3342 main.log.exception( self.name + ": Object not as expected" )
3343 return main.FALSE
3344 except pexpect.EOF:
3345 main.log.error( self.name + ": EOF exception found" )
3346 main.log.error( self.name + ": " + self.handle.before )
3347 main.cleanup()
3348 main.exit()
3349 except Exception:
3350 main.log.exception( self.name + ": Uncaught exception!" )
3351 main.cleanup()
3352 main.exit()
3353
Jon Hall390696c2015-05-05 17:13:41 -07003354 def setTestAdd( self, setName, values ):
3355 """
3356 CLI command to add elements to a distributed set.
3357 Arguments:
3358 setName - The name of the set to add to.
3359 values - The value(s) to add to the set, space seperated.
3360 Example usages:
3361 setTestAdd( "set1", "a b c" )
3362 setTestAdd( "set2", "1" )
3363 returns:
3364 main.TRUE on success OR
3365 main.FALSE if elements were already in the set OR
3366 main.ERROR on error
3367 """
3368 try:
3369 cmdStr = "set-test-add " + str( setName ) + " " + str( values )
3370 output = self.sendline( cmdStr )
Jon Hallfeff3082015-05-19 10:23:26 -07003371 try:
3372 # TODO: Maybe make this less hardcoded
3373 # ConsistentMap Exceptions
3374 assert "org.onosproject.store.service" not in output
3375 # Node not leader
3376 assert "java.lang.IllegalStateException" not in output
3377 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003378 main.log.error( "Error in processing '" + cmdStr + "' " +
Jon Hallfeff3082015-05-19 10:23:26 -07003379 "command: " + str( output ) )
3380 retryTime = 30 # Conservative time, given by Madan
3381 main.log.info( "Waiting " + str( retryTime ) +
3382 "seconds before retrying." )
3383 time.sleep( retryTime ) # Due to change in mastership
3384 output = self.sendline( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07003385 assert "Error executing command" not in output
3386 positiveMatch = "\[(.*)\] was added to the set " + str( setName )
3387 negativeMatch = "\[(.*)\] was already in set " + str( setName )
3388 main.log.info( self.name + ": " + output )
3389 if re.search( positiveMatch, output):
3390 return main.TRUE
3391 elif re.search( negativeMatch, output):
3392 return main.FALSE
3393 else:
3394 main.log.error( self.name + ": setTestAdd did not" +
3395 " match expected output" )
Jon Hall390696c2015-05-05 17:13:41 -07003396 main.log.debug( self.name + " actual: " + repr( output ) )
3397 return main.ERROR
3398 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003399 main.log.error( "Error in processing '" + cmdStr + "' command: " +
Jon Hall390696c2015-05-05 17:13:41 -07003400 str( output ) )
3401 return main.ERROR
3402 except TypeError:
3403 main.log.exception( self.name + ": Object not as expected" )
3404 return main.ERROR
3405 except pexpect.EOF:
3406 main.log.error( self.name + ": EOF exception found" )
3407 main.log.error( self.name + ": " + self.handle.before )
3408 main.cleanup()
3409 main.exit()
3410 except Exception:
3411 main.log.exception( self.name + ": Uncaught exception!" )
3412 main.cleanup()
3413 main.exit()
3414
3415 def setTestRemove( self, setName, values, clear=False, retain=False ):
3416 """
3417 CLI command to remove elements from a distributed set.
3418 Required arguments:
3419 setName - The name of the set to remove from.
3420 values - The value(s) to remove from the set, space seperated.
3421 Optional arguments:
3422 clear - Clear all elements from the set
3423 retain - Retain only the given values. (intersection of the
3424 original set and the given set)
3425 returns:
3426 main.TRUE on success OR
3427 main.FALSE if the set was not changed OR
3428 main.ERROR on error
3429 """
3430 try:
3431 cmdStr = "set-test-remove "
3432 if clear:
3433 cmdStr += "-c " + str( setName )
3434 elif retain:
3435 cmdStr += "-r " + str( setName ) + " " + str( values )
3436 else:
3437 cmdStr += str( setName ) + " " + str( values )
3438 output = self.sendline( cmdStr )
Jon Hallfeff3082015-05-19 10:23:26 -07003439 try:
3440 # TODO: Maybe make this less hardcoded
3441 # ConsistentMap Exceptions
3442 assert "org.onosproject.store.service" not in output
3443 # Node not leader
3444 assert "java.lang.IllegalStateException" not in output
3445 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003446 main.log.error( "Error in processing '" + cmdStr + "' " +
Jon Hallfeff3082015-05-19 10:23:26 -07003447 "command: " + str( output ) )
3448 retryTime = 30 # Conservative time, given by Madan
3449 main.log.info( "Waiting " + str( retryTime ) +
3450 "seconds before retrying." )
3451 time.sleep( retryTime ) # Due to change in mastership
3452 output = self.sendline( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07003453 assert "Error executing command" not in output
3454 main.log.info( self.name + ": " + output )
3455 if clear:
3456 pattern = "Set " + str( setName ) + " cleared"
3457 if re.search( pattern, output ):
3458 return main.TRUE
3459 elif retain:
3460 positivePattern = str( setName ) + " was pruned to contain " +\
3461 "only elements of set \[(.*)\]"
3462 negativePattern = str( setName ) + " was not changed by " +\
3463 "retaining only elements of the set " +\
3464 "\[(.*)\]"
3465 if re.search( positivePattern, output ):
3466 return main.TRUE
3467 elif re.search( negativePattern, output ):
3468 return main.FALSE
3469 else:
3470 positivePattern = "\[(.*)\] was removed from the set " +\
3471 str( setName )
3472 if ( len( values.split() ) == 1 ):
3473 negativePattern = "\[(.*)\] was not in set " +\
3474 str( setName )
3475 else:
3476 negativePattern = "No element of \[(.*)\] was in set " +\
3477 str( setName )
3478 if re.search( positivePattern, output ):
3479 return main.TRUE
3480 elif re.search( negativePattern, output ):
3481 return main.FALSE
3482 main.log.error( self.name + ": setTestRemove did not" +
3483 " match expected output" )
3484 main.log.debug( self.name + " expected: " + pattern )
3485 main.log.debug( self.name + " actual: " + repr( output ) )
3486 return main.ERROR
3487 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003488 main.log.error( "Error in processing '" + cmdStr + "' command: " +
Jon Hall390696c2015-05-05 17:13:41 -07003489 str( output ) )
3490 return main.ERROR
3491 except TypeError:
3492 main.log.exception( self.name + ": Object not as expected" )
3493 return main.ERROR
3494 except pexpect.EOF:
3495 main.log.error( self.name + ": EOF exception found" )
3496 main.log.error( self.name + ": " + self.handle.before )
3497 main.cleanup()
3498 main.exit()
3499 except Exception:
3500 main.log.exception( self.name + ": Uncaught exception!" )
3501 main.cleanup()
3502 main.exit()
3503
3504 def setTestGet( self, setName, values="" ):
3505 """
3506 CLI command to get the elements in a distributed set.
3507 Required arguments:
3508 setName - The name of the set to remove from.
3509 Optional arguments:
3510 values - The value(s) to check if in the set, space seperated.
3511 returns:
3512 main.ERROR on error OR
3513 A list of elements in the set if no optional arguments are
3514 supplied OR
3515 A tuple containing the list then:
3516 main.FALSE if the given values are not in the set OR
3517 main.TRUE if the given values are in the set OR
3518 """
3519 try:
3520 values = str( values ).strip()
3521 setName = str( setName ).strip()
3522 length = len( values.split() )
3523 containsCheck = None
3524 # Patterns to match
3525 setPattern = "\[(.*)\]"
3526 pattern = "Items in set " + setName + ":\n" + setPattern
3527 containsTrue = "Set " + setName + " contains the value " + values
3528 containsFalse = "Set " + setName + " did not contain the value " +\
3529 values
3530 containsAllTrue = "Set " + setName + " contains the the subset " +\
3531 setPattern
3532 containsAllFalse = "Set " + setName + " did not contain the the" +\
3533 " subset " + setPattern
3534
3535 cmdStr = "set-test-get "
3536 cmdStr += setName + " " + values
3537 output = self.sendline( cmdStr )
Jon Hallfeff3082015-05-19 10:23:26 -07003538 try:
3539 # TODO: Maybe make this less hardcoded
3540 # ConsistentMap Exceptions
3541 assert "org.onosproject.store.service" not in output
3542 # Node not leader
3543 assert "java.lang.IllegalStateException" not in output
3544 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003545 main.log.error( "Error in processing '" + cmdStr + "' " +
Jon Hallfeff3082015-05-19 10:23:26 -07003546 "command: " + str( output ) )
3547 retryTime = 30 # Conservative time, given by Madan
3548 main.log.info( "Waiting " + str( retryTime ) +
3549 "seconds before retrying." )
3550 time.sleep( retryTime ) # Due to change in mastership
3551 output = self.sendline( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07003552 assert "Error executing command" not in output
3553 main.log.info( self.name + ": " + output )
3554
3555 if length == 0:
3556 match = re.search( pattern, output )
3557 else: # if given values
3558 if length == 1: # Contains output
3559 patternTrue = pattern + "\n" + containsTrue
3560 patternFalse = pattern + "\n" + containsFalse
3561 else: # ContainsAll output
3562 patternTrue = pattern + "\n" + containsAllTrue
3563 patternFalse = pattern + "\n" + containsAllFalse
3564 matchTrue = re.search( patternTrue, output )
3565 matchFalse = re.search( patternFalse, output )
3566 if matchTrue:
3567 containsCheck = main.TRUE
3568 match = matchTrue
3569 elif matchFalse:
3570 containsCheck = main.FALSE
3571 match = matchFalse
3572 else:
3573 main.log.error( self.name + " setTestGet did not match " +\
3574 "expected output" )
3575 main.log.debug( self.name + " expected: " + pattern )
3576 main.log.debug( self.name + " actual: " + repr( output ) )
3577 match = None
3578 if match:
3579 setMatch = match.group( 1 )
3580 if setMatch == '':
3581 setList = []
3582 else:
3583 setList = setMatch.split( ", " )
3584 if length > 0:
3585 return ( setList, containsCheck )
3586 else:
3587 return setList
3588 else: # no match
3589 main.log.error( self.name + ": setTestGet did not" +
3590 " match expected output" )
3591 main.log.debug( self.name + " expected: " + pattern )
3592 main.log.debug( self.name + " actual: " + repr( output ) )
3593 return main.ERROR
3594 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003595 main.log.error( "Error in processing '" + cmdStr + "' command: " +
Jon Hall390696c2015-05-05 17:13:41 -07003596 str( output ) )
3597 return main.ERROR
3598 except TypeError:
3599 main.log.exception( self.name + ": Object not as expected" )
3600 return main.ERROR
3601 except pexpect.EOF:
3602 main.log.error( self.name + ": EOF exception found" )
3603 main.log.error( self.name + ": " + self.handle.before )
3604 main.cleanup()
3605 main.exit()
3606 except Exception:
3607 main.log.exception( self.name + ": Uncaught exception!" )
3608 main.cleanup()
3609 main.exit()
3610
3611 def setTestSize( self, setName ):
3612 """
3613 CLI command to get the elements in a distributed set.
3614 Required arguments:
3615 setName - The name of the set to remove from.
3616 returns:
Jon Hallfeff3082015-05-19 10:23:26 -07003617 The integer value of the size returned or
Jon Hall390696c2015-05-05 17:13:41 -07003618 None on error
3619 """
3620 try:
3621 # TODO: Should this check against the number of elements returned
3622 # and then return true/false based on that?
3623 setName = str( setName ).strip()
3624 # Patterns to match
3625 setPattern = "\[(.*)\]"
3626 pattern = "There are (\d+) items in set " + setName + ":\n" +\
3627 setPattern
3628 cmdStr = "set-test-get -s "
3629 cmdStr += setName
3630 output = self.sendline( cmdStr )
Jon Hallfeff3082015-05-19 10:23:26 -07003631 try:
3632 # TODO: Maybe make this less hardcoded
3633 # ConsistentMap Exceptions
3634 assert "org.onosproject.store.service" not in output
3635 # Node not leader
3636 assert "java.lang.IllegalStateException" not in output
3637 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003638 main.log.error( "Error in processing '" + cmdStr + "' " +
Jon Hallfeff3082015-05-19 10:23:26 -07003639 "command: " + str( output ) )
3640 retryTime = 30 # Conservative time, given by Madan
3641 main.log.info( "Waiting " + str( retryTime ) +
3642 "seconds before retrying." )
3643 time.sleep( retryTime ) # Due to change in mastership
3644 output = self.sendline( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07003645 assert "Error executing command" not in output
3646 main.log.info( self.name + ": " + output )
3647 match = re.search( pattern, output )
3648 if match:
3649 setSize = int( match.group( 1 ) )
3650 setMatch = match.group( 2 )
3651 if len( setMatch.split() ) == setSize:
3652 main.log.info( "The size returned by " + self.name +
3653 " matches the number of elements in " +
3654 "the returned set" )
3655 else:
3656 main.log.error( "The size returned by " + self.name +
3657 " does not match the number of " +
3658 "elements in the returned set." )
3659 return setSize
3660 else: # no match
3661 main.log.error( self.name + ": setTestGet did not" +
3662 " match expected output" )
3663 main.log.debug( self.name + " expected: " + pattern )
3664 main.log.debug( self.name + " actual: " + repr( output ) )
3665 return None
3666 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003667 main.log.error( "Error in processing '" + cmdStr + "' command: " +
Jon Hall390696c2015-05-05 17:13:41 -07003668 str( output ) )
acsmarsa4a4d1e2015-07-10 16:01:24 -07003669 return None
Jon Hall390696c2015-05-05 17:13:41 -07003670 except TypeError:
3671 main.log.exception( self.name + ": Object not as expected" )
3672 return None
3673 except pexpect.EOF:
3674 main.log.error( self.name + ": EOF exception found" )
3675 main.log.error( self.name + ": " + self.handle.before )
3676 main.cleanup()
3677 main.exit()
3678 except Exception:
3679 main.log.exception( self.name + ": Uncaught exception!" )
3680 main.cleanup()
3681 main.exit()
3682
Jon Hall80daded2015-05-27 16:07:00 -07003683 def counters( self, jsonFormat=True ):
Jon Hall390696c2015-05-05 17:13:41 -07003684 """
3685 Command to list the various counters in the system.
3686 returns:
Jon Hall80daded2015-05-27 16:07:00 -07003687 if jsonFormat, a string of the json object returned by the cli
3688 command
3689 if not jsonFormat, the normal string output of the cli command
Jon Hall390696c2015-05-05 17:13:41 -07003690 None on error
3691 """
Jon Hall390696c2015-05-05 17:13:41 -07003692 try:
3693 counters = {}
3694 cmdStr = "counters"
Jon Hall80daded2015-05-27 16:07:00 -07003695 if jsonFormat:
3696 cmdStr += " -j"
Jon Hall390696c2015-05-05 17:13:41 -07003697 output = self.sendline( cmdStr )
3698 assert "Error executing command" not in output
3699 main.log.info( self.name + ": " + output )
Jon Hall80daded2015-05-27 16:07:00 -07003700 return output
Jon Hall390696c2015-05-05 17:13:41 -07003701 except AssertionError:
3702 main.log.error( "Error in processing 'counters' command: " +
3703 str( output ) )
Jon Hall80daded2015-05-27 16:07:00 -07003704 return None
Jon Hall390696c2015-05-05 17:13:41 -07003705 except TypeError:
3706 main.log.exception( self.name + ": Object not as expected" )
Jon Hall80daded2015-05-27 16:07:00 -07003707 return None
Jon Hall390696c2015-05-05 17:13:41 -07003708 except pexpect.EOF:
3709 main.log.error( self.name + ": EOF exception found" )
3710 main.log.error( self.name + ": " + self.handle.before )
3711 main.cleanup()
3712 main.exit()
3713 except Exception:
3714 main.log.exception( self.name + ": Uncaught exception!" )
3715 main.cleanup()
3716 main.exit()
3717
Jon Halle1a3b752015-07-22 13:02:46 -07003718 def counterTestAddAndGet( self, counter, delta=1, inMemory=False ):
Jon Hall390696c2015-05-05 17:13:41 -07003719 """
Jon Halle1a3b752015-07-22 13:02:46 -07003720 CLI command to add a delta to then get a distributed counter.
Jon Hall390696c2015-05-05 17:13:41 -07003721 Required arguments:
3722 counter - The name of the counter to increment.
3723 Optional arguments:
Jon Halle1a3b752015-07-22 13:02:46 -07003724 delta - The long to add to the counter
Jon Hall390696c2015-05-05 17:13:41 -07003725 inMemory - use in memory map for the counter
3726 returns:
3727 integer value of the counter or
3728 None on Error
3729 """
3730 try:
3731 counter = str( counter )
Jon Halle1a3b752015-07-22 13:02:46 -07003732 delta = int( delta )
Jon Hall390696c2015-05-05 17:13:41 -07003733 cmdStr = "counter-test-increment "
3734 if inMemory:
3735 cmdStr += "-i "
3736 cmdStr += counter
Jon Halle1a3b752015-07-22 13:02:46 -07003737 if delta != 1:
3738 cmdStr += " " + str( delta )
Jon Hall390696c2015-05-05 17:13:41 -07003739 output = self.sendline( cmdStr )
Jon Hallfeff3082015-05-19 10:23:26 -07003740 try:
3741 # TODO: Maybe make this less hardcoded
3742 # ConsistentMap Exceptions
3743 assert "org.onosproject.store.service" not in output
3744 # Node not leader
3745 assert "java.lang.IllegalStateException" not in output
3746 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003747 main.log.error( "Error in processing '" + cmdStr + "' " +
Jon Hallfeff3082015-05-19 10:23:26 -07003748 "command: " + str( output ) )
3749 retryTime = 30 # Conservative time, given by Madan
3750 main.log.info( "Waiting " + str( retryTime ) +
3751 "seconds before retrying." )
3752 time.sleep( retryTime ) # Due to change in mastership
3753 output = self.sendline( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07003754 assert "Error executing command" not in output
3755 main.log.info( self.name + ": " + output )
Jon Halle1a3b752015-07-22 13:02:46 -07003756 pattern = counter + " was updated to (-?\d+)"
Jon Hall390696c2015-05-05 17:13:41 -07003757 match = re.search( pattern, output )
3758 if match:
3759 return int( match.group( 1 ) )
3760 else:
Jon Halle1a3b752015-07-22 13:02:46 -07003761 main.log.error( self.name + ": counterTestAddAndGet did not" +
Jon Hall390696c2015-05-05 17:13:41 -07003762 " match expected output." )
3763 main.log.debug( self.name + " expected: " + pattern )
3764 main.log.debug( self.name + " actual: " + repr( output ) )
3765 return None
3766 except AssertionError:
Jon Halle1a3b752015-07-22 13:02:46 -07003767 main.log.error( "Error in processing '" + cmdStr + "'" +
Jon Hall390696c2015-05-05 17:13:41 -07003768 " command: " + str( output ) )
3769 return None
3770 except TypeError:
3771 main.log.exception( self.name + ": Object not as expected" )
3772 return None
3773 except pexpect.EOF:
3774 main.log.error( self.name + ": EOF exception found" )
3775 main.log.error( self.name + ": " + self.handle.before )
3776 main.cleanup()
3777 main.exit()
3778 except Exception:
3779 main.log.exception( self.name + ": Uncaught exception!" )
3780 main.cleanup()
3781 main.exit()
3782
Jon Halle1a3b752015-07-22 13:02:46 -07003783 def counterTestGetAndAdd( self, counter, delta=1, inMemory=False ):
3784 """
3785 CLI command to get a distributed counter then add a delta to it.
3786 Required arguments:
3787 counter - The name of the counter to increment.
3788 Optional arguments:
3789 delta - The long to add to the counter
3790 inMemory - use in memory map for the counter
3791 returns:
3792 integer value of the counter or
3793 None on Error
3794 """
3795 try:
3796 counter = str( counter )
3797 delta = int( delta )
3798 cmdStr = "counter-test-increment -g "
3799 if inMemory:
3800 cmdStr += "-i "
3801 cmdStr += counter
3802 if delta != 1:
3803 cmdStr += " " + str( delta )
3804 output = self.sendline( cmdStr )
3805 try:
3806 # TODO: Maybe make this less hardcoded
3807 # ConsistentMap Exceptions
3808 assert "org.onosproject.store.service" not in output
3809 # Node not leader
3810 assert "java.lang.IllegalStateException" not in output
3811 except AssertionError:
3812 main.log.error( "Error in processing '" + cmdStr + "' " +
3813 "command: " + str( output ) )
3814 retryTime = 30 # Conservative time, given by Madan
3815 main.log.info( "Waiting " + str( retryTime ) +
3816 "seconds before retrying." )
3817 time.sleep( retryTime ) # Due to change in mastership
3818 output = self.sendline( cmdStr )
3819 assert "Error executing command" not in output
3820 main.log.info( self.name + ": " + output )
3821 pattern = counter + " was updated to (-?\d+)"
3822 match = re.search( pattern, output )
3823 if match:
3824 return int( match.group( 1 ) )
3825 else:
3826 main.log.error( self.name + ": counterTestGetAndAdd did not" +
3827 " match expected output." )
3828 main.log.debug( self.name + " expected: " + pattern )
3829 main.log.debug( self.name + " actual: " + repr( output ) )
3830 return None
3831 except AssertionError:
3832 main.log.error( "Error in processing '" + cmdStr + "'" +
3833 " command: " + str( output ) )
3834 return None
3835 except TypeError:
3836 main.log.exception( self.name + ": Object not as expected" )
3837 return None
3838 except pexpect.EOF:
3839 main.log.error( self.name + ": EOF exception found" )
3840 main.log.error( self.name + ": " + self.handle.before )
3841 main.cleanup()
3842 main.exit()
3843 except Exception:
3844 main.log.exception( self.name + ": Uncaught exception!" )
3845 main.cleanup()
3846 main.exit()
3847
kelvin-onlaba297c4d2015-06-01 13:53:55 -07003848 def summary( self, jsonFormat=True ):
3849 """
3850 Description: Execute summary command in onos
3851 Returns: json object ( summary -j ), returns main.FALSE if there is
3852 no output
3853
3854 """
3855 try:
3856 cmdStr = "summary"
3857 if jsonFormat:
3858 cmdStr += " -j"
3859 handle = self.sendline( cmdStr )
3860
3861 if re.search( "Error:", handle ):
3862 main.log.error( self.name + ": summary() response: " +
3863 str( handle ) )
3864 if not handle:
3865 main.log.error( self.name + ": There is no output in " +
3866 "summary command" )
3867 return main.FALSE
3868 return handle
3869 except TypeError:
3870 main.log.exception( self.name + ": Object not as expected" )
3871 return None
3872 except pexpect.EOF:
3873 main.log.error( self.name + ": EOF exception found" )
3874 main.log.error( self.name + ": " + self.handle.before )
3875 main.cleanup()
3876 main.exit()
3877 except Exception:
3878 main.log.exception( self.name + ": Uncaught exception!" )
3879 main.cleanup()
3880 main.exit()
Jon Hall2a5002c2015-08-21 16:49:11 -07003881
3882 def transactionalMapGet( self, keyName, inMemory=False ):
3883 """
3884 CLI command to get the value of a key in a consistent map using
3885 transactions. This a test function and can only get keys from the
3886 test map hard coded into the cli command
3887 Required arguments:
3888 keyName - The name of the key to get
3889 Optional arguments:
3890 inMemory - use in memory map for the counter
3891 returns:
3892 The string value of the key or
3893 None on Error
3894 """
3895 try:
3896 keyName = str( keyName )
3897 cmdStr = "transactional-map-test-get "
3898 if inMemory:
3899 cmdStr += "-i "
3900 cmdStr += keyName
3901 output = self.sendline( cmdStr )
3902 try:
3903 # TODO: Maybe make this less hardcoded
3904 # ConsistentMap Exceptions
3905 assert "org.onosproject.store.service" not in output
3906 # Node not leader
3907 assert "java.lang.IllegalStateException" not in output
3908 except AssertionError:
3909 main.log.error( "Error in processing '" + cmdStr + "' " +
3910 "command: " + str( output ) )
3911 return None
3912 pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found."
3913 if "Key " + keyName + " not found." in output:
3914 return None
3915 else:
3916 match = re.search( pattern, output )
3917 if match:
3918 return match.groupdict()[ 'value' ]
3919 else:
3920 main.log.error( self.name + ": transactionlMapGet did not" +
3921 " match expected output." )
3922 main.log.debug( self.name + " expected: " + pattern )
3923 main.log.debug( self.name + " actual: " + repr( output ) )
3924 return None
3925 except TypeError:
3926 main.log.exception( self.name + ": Object not as expected" )
3927 return None
3928 except pexpect.EOF:
3929 main.log.error( self.name + ": EOF exception found" )
3930 main.log.error( self.name + ": " + self.handle.before )
3931 main.cleanup()
3932 main.exit()
3933 except Exception:
3934 main.log.exception( self.name + ": Uncaught exception!" )
3935 main.cleanup()
3936 main.exit()
3937
3938 def transactionalMapPut( self, numKeys, value, inMemory=False ):
3939 """
3940 CLI command to put a value into 'numKeys' number of keys in a
3941 consistent map using transactions. This a test function and can only
3942 put into keys named 'Key#' of the test map hard coded into the cli command
3943 Required arguments:
3944 numKeys - Number of keys to add the value to
3945 value - The string value to put into the keys
3946 Optional arguments:
3947 inMemory - use in memory map for the counter
3948 returns:
3949 A dictionary whose keys are the name of the keys put into the map
3950 and the values of the keys are dictionaries whose key-values are
3951 'value': value put into map and optionaly
3952 'oldValue': Previous value in the key or
3953 None on Error
3954
3955 Example output
3956 { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'},
3957 'Key2': {'value': 'Testing'} }
3958 """
3959 try:
3960 numKeys = str( numKeys )
3961 value = str( value )
3962 cmdStr = "transactional-map-test-put "
3963 if inMemory:
3964 cmdStr += "-i "
3965 cmdStr += numKeys + " " + value
3966 output = self.sendline( cmdStr )
3967 try:
3968 # TODO: Maybe make this less hardcoded
3969 # ConsistentMap Exceptions
3970 assert "org.onosproject.store.service" not in output
3971 # Node not leader
3972 assert "java.lang.IllegalStateException" not in output
3973 except AssertionError:
3974 main.log.error( "Error in processing '" + cmdStr + "' " +
3975 "command: " + str( output ) )
3976 return None
3977 newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.'
3978 updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\."
3979 results = {}
3980 for line in output.splitlines():
3981 new = re.search( newPattern, line )
3982 updated = re.search( updatedPattern, line )
3983 if new:
3984 results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] }
3985 elif updated:
3986 results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ],
3987 'oldValue': updated.groupdict()[ 'oldValue' ] }
3988 else:
3989 main.log.error( self.name + ": transactionlMapGet did not" +
3990 " match expected output." )
3991 main.log.debug( self.name + " expected: " + pattern )
3992 main.log.debug( self.name + " actual: " + repr( output ) )
3993 return results
3994 except TypeError:
3995 main.log.exception( self.name + ": Object not as expected" )
3996 return None
3997 except pexpect.EOF:
3998 main.log.error( self.name + ": EOF exception found" )
3999 main.log.error( self.name + ": " + self.handle.before )
4000 main.cleanup()
4001 main.exit()
4002 except Exception:
4003 main.log.exception( self.name + ": Uncaught exception!" )
4004 main.cleanup()
4005 main.exit()
acsmarsdaea66c2015-09-03 11:44:06 -07004006 def maps( self, jsonFormat=True ):
4007 """
4008 Description: Returns result of onos:maps
4009 Optional:
4010 * jsonFormat: enable json formatting of output
4011 """
4012 try:
4013 cmdStr = "maps"
4014 if jsonFormat:
4015 cmdStr += " -j"
4016 handle = self.sendline( cmdStr )
4017 return handle
4018 except TypeError:
4019 main.log.exception( self.name + ": Object not as expected" )
4020 return None
4021 except pexpect.EOF:
4022 main.log.error( self.name + ": EOF exception found" )
4023 main.log.error( self.name + ": " + self.handle.before )
4024 main.cleanup()
4025 main.exit()
4026 except Exception:
4027 main.log.exception( self.name + ": Uncaught exception!" )
4028 main.cleanup()
4029 main.exit()