blob: 9f08e0686fd7af522b527c561140508477682f31 [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
kelvin8ec71442015-01-15 16:57:00 -08003"""
andrewonlab95ce8322014-10-13 14:12:04 -04004This driver enters the onos> prompt to issue commands.
5
kelvin8ec71442015-01-15 16:57:00 -08006Please follow the coding style demonstrated by existing
andrewonlab95ce8322014-10-13 14:12:04 -04007functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040014shreya@onlab.us
andrewonlab95ce8322014-10-13 14:12:04 -040015
16OCT 13 2014
17
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab95ce8322014-10-13 14:12:04 -040019import sys
andrewonlab95ce8322014-10-13 14:12:04 -040020import pexpect
21import re
22import traceback
kelvin8ec71442015-01-15 16:57:00 -080023sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040024from drivers.common.clidriver import CLI
25
andrewonlab95ce8322014-10-13 14:12:04 -040026
kelvin8ec71442015-01-15 16:57:00 -080027class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040028
kelvin8ec71442015-01-15 16:57:00 -080029 def __init__( self ):
30 """
31 Initialize client
32 """
33 super( CLI, self ).__init__()
34
35 def connect( self, **connectargs ):
36 """
andrewonlab95ce8322014-10-13 14:12:04 -040037 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080038 """
andrewonlab95ce8322014-10-13 14:12:04 -040039 try:
40 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080041 vars( self )[ key ] = connectargs[ key ]
andrewonlab95ce8322014-10-13 14:12:04 -040042 self.home = "~/ONOS"
43 for key in self.options:
44 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080045 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040046 break
47
kelvin8ec71442015-01-15 16:57:00 -080048 self.name = self.options[ 'name' ]
49 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080050 user_name=self.user_name,
51 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080052 port=self.port,
53 pwd=self.pwd,
54 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040055
kelvin8ec71442015-01-15 16:57:00 -080056 self.handle.sendline( "cd " + self.home )
57 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040058 if self.handle:
59 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080060 else:
61 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040062 return main.FALSE
63 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080064 main.log.error( self.name + ": EOF exception found" )
65 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040066 main.cleanup()
67 main.exit()
68 except:
kelvin8ec71442015-01-15 16:57:00 -080069 main.log.info( self.name + ":::::::::::::::::::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -040070 main.log.error( traceback.print_exc() )
kelvin8ec71442015-01-15 16:57:00 -080071 main.log.info( ":::::::::::::::::::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -040072 main.cleanup()
73 main.exit()
74
kelvin8ec71442015-01-15 16:57:00 -080075 def disconnect( self ):
76 """
andrewonlab95ce8322014-10-13 14:12:04 -040077 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080078 """
andrewonlab95ce8322014-10-13 14:12:04 -040079 response = ''
80 try:
kelvin8ec71442015-01-15 16:57:00 -080081 self.handle.sendline( "" )
82 i = self.handle.expect( [ "onos>", "\$" ] )
Jon Hall7e5b9172014-10-22 12:32:47 -040083 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -080084 self.handle.sendline( "system:shutdown" )
85 self.handle.expect( "Confirm" )
86 self.handle.sendline( "yes" )
87 self.handle.expect( "\$" )
88 self.handle.sendline( "" )
89 self.handle.expect( "\$" )
90 self.handle.sendline( "exit" )
91 self.handle.expect( "closed" )
andrewonlabc2d05aa2014-10-13 16:51:10 -040092
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 except:
kelvin8ec71442015-01-15 16:57:00 -080097 main.log.error( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -040098 response = main.FALSE
99 return response
100
kelvin8ec71442015-01-15 16:57:00 -0800101 def logout( self ):
102 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500103 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800104 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500105 try:
kelvin8ec71442015-01-15 16:57:00 -0800106 self.handle.sendline( "" )
107 i = self.handle.expect( [
andrewonlab9627f432014-11-14 12:45:10 -0500108 "onos>",
kelvin8ec71442015-01-15 16:57:00 -0800109 "\$" ], timeout=10 )
andrewonlab9627f432014-11-14 12:45:10 -0500110 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800111 self.handle.sendline( "logout" )
112 self.handle.expect( "\$" )
andrewonlab9627f432014-11-14 12:45:10 -0500113 elif i == 1:
114 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800115
andrewonlab38d2b4a2014-11-13 16:28:47 -0500116 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800117 main.log.error( self.name + ": eof exception found" )
118 main.log.error( self.name + ": " +
119 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500120 main.cleanup()
121 main.exit()
122 except:
kelvin8ec71442015-01-15 16:57:00 -0800123 main.log.info( self.name + " ::::::" )
124 main.log.error( traceback.print_exc() )
125 main.log.info( self.name + " ::::::" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500126 main.cleanup()
127 main.exit()
128
kelvin-onlabd3b64892015-01-20 13:26:24 -0800129 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800130 """
andrewonlab95ce8322014-10-13 14:12:04 -0400131 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800132
andrewonlab95ce8322014-10-13 14:12:04 -0400133 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800134 """
andrewonlab95ce8322014-10-13 14:12:04 -0400135 try:
136 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800137 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400138 main.cleanup()
139 main.exit()
140 else:
kelvin8ec71442015-01-15 16:57:00 -0800141 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800142 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800143 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400144 # and that this driver will have to change accordingly
kelvin8ec71442015-01-15 16:57:00 -0800145 self.handle.expect( "ONOS_CELL=" + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800146 handleBefore = self.handle.before
147 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800148 # Get the rest of the handle
149 self.handle.sendline( "" )
150 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800151 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400152
kelvin-onlabd3b64892015-01-20 13:26:24 -0800153 main.log.info( "Cell call returned: " + handleBefore +
154 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400155
156 return main.TRUE
157
158 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800159 main.log.error( self.name + ": eof exception found" )
160 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400161 main.cleanup()
162 main.exit()
163 except:
kelvin8ec71442015-01-15 16:57:00 -0800164 main.log.info( self.name + " ::::::" )
165 main.log.error( traceback.print_exc() )
166 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400167 main.cleanup()
168 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800169
kelvin-onlabd3b64892015-01-20 13:26:24 -0800170 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800171 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800172 karafTimeout is an optional arugument. karafTimeout value passed
173 by user would be used to set the current karaf shell idle timeout.
174 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800175 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800176 Below is an example to start a session with 60 seconds idle timeout
177 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800178
Hari Krishna25d42f72015-01-05 15:08:28 -0800179 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800180 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800181
kelvin-onlabd3b64892015-01-20 13:26:24 -0800182 Note: karafTimeout is left as str so that this could be read
183 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800184 """
andrewonlab95ce8322014-10-13 14:12:04 -0400185 try:
kelvin8ec71442015-01-15 16:57:00 -0800186 self.handle.sendline( "" )
187 x = self.handle.expect( [
188 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500189
190 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800191 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500192 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400193
kelvin8ec71442015-01-15 16:57:00 -0800194 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800195 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800196 i = self.handle.expect( [
197 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800198 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400199
200 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800201 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800202 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800203 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800204 "config:property-set -p org.apache.karaf.shel\
205 l sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800206 karafTimeout )
207 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800208 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800209 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400210 return main.TRUE
211 else:
kelvin8ec71442015-01-15 16:57:00 -0800212 # If failed, send ctrl+c to process and try again
213 main.log.info( "Starting CLI failed. Retrying..." )
214 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800216 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
217 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400218 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800219 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800220 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800221 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800222 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800223 "config:property-set -p org.apache.karaf.shell\
224 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800225 karafTimeout )
226 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800227 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800228 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400229 return main.TRUE
230 else:
kelvin8ec71442015-01-15 16:57:00 -0800231 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800232 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400233 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400234
235 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800236 main.log.error( self.name + ": EOF exception found" )
237 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400238 main.cleanup()
239 main.exit()
240 except:
kelvin8ec71442015-01-15 16:57:00 -0800241 main.log.info( self.name + " ::::::" )
242 main.log.error( traceback.print_exc() )
243 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400244 main.cleanup()
245 main.exit()
246
kelvin-onlabd3b64892015-01-20 13:26:24 -0800247 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800248 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800249 Send a completely user specified string to
250 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400251 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800252
andrewonlaba18f6bf2014-10-13 19:31:54 -0400253 Warning: There are no sanity checking to commands
254 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800255 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400256 try:
kelvin8ec71442015-01-15 16:57:00 -0800257 self.handle.sendline( "" )
258 self.handle.expect( "onos>" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400259
kelvin-onlab898a6c62015-01-16 14:13:53 -0800260 self.handle.sendline( "log:log \"Sending CLI command: '"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800261 + cmdStr + "'\"" )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800262 self.handle.expect( "onos>" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800263 self.handle.sendline( cmdStr )
264 self.handle.expect( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800265 self.handle.expect( "onos>" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400266
267 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800268
269 self.handle.sendline( "" )
270 self.handle.expect( "onos>" )
271
kelvin-onlabd3b64892015-01-20 13:26:24 -0800272 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
kelvin-onlab898a6c62015-01-16 14:13:53 -0800273 + self.name + "." )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800274 ansiEscape = re.compile( r'\x1b[^m]*m' )
275 handle = ansiEscape.sub( '', handle )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400276
277 return handle
278 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800279 main.log.error( self.name + ": EOF exception found" )
280 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400281 main.cleanup()
282 main.exit()
283 except:
kelvin8ec71442015-01-15 16:57:00 -0800284 main.log.info( self.name + " ::::::" )
285 main.log.error( traceback.print_exc() )
286 main.log.info( self.name + " ::::::" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400287 main.cleanup()
288 main.exit()
289
kelvin8ec71442015-01-15 16:57:00 -0800290 # IMPORTANT NOTE:
291 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800292 # the cli command changing 'a:b' with 'aB'.
293 # Ex ) onos:topology > onosTopology
294 # onos:links > onosLinks
295 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800296
kelvin-onlabd3b64892015-01-20 13:26:24 -0800297 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800298 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400299 Adds a new cluster node by ID and address information.
300 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800301 * nodeId
302 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400303 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800304 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800305 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400306 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800307 cmdStr = "add-node " + str( nodeId ) + " " +\
308 str( ONOSIp ) + " " + str( tcpPort )
309 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800310 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800311 main.log.error( "Error in adding node" )
312 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800313 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400314 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800315 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400316 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400317 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800318 main.log.error( self.name + ": EOF exception found" )
319 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400320 main.cleanup()
321 main.exit()
322 except:
kelvin8ec71442015-01-15 16:57:00 -0800323 main.log.info( self.name + " ::::::" )
324 main.log.error( traceback.print_exc() )
325 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400326 main.cleanup()
327 main.exit()
328
kelvin-onlabd3b64892015-01-20 13:26:24 -0800329 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800330 """
andrewonlab86dc3082014-10-13 18:18:38 -0400331 Removes a cluster by ID
332 Issues command: 'remove-node [<node-id>]'
333 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800334 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800335 """
andrewonlab86dc3082014-10-13 18:18:38 -0400336 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400337
kelvin-onlabd3b64892015-01-20 13:26:24 -0800338 cmdStr = "remove-node " + str( nodeId )
339 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800340 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400341
342 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800343
andrewonlab86dc3082014-10-13 18:18:38 -0400344 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800345 main.log.error( self.name + ": EOF exception found" )
346 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400347 main.cleanup()
348 main.exit()
349 except:
kelvin8ec71442015-01-15 16:57:00 -0800350 main.log.info( self.name + " ::::::" )
351 main.log.error( traceback.print_exc() )
352 main.log.info( self.name + " ::::::" )
andrewonlab86dc3082014-10-13 18:18:38 -0400353 main.cleanup()
354 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400355
kelvin8ec71442015-01-15 16:57:00 -0800356 def nodes( self ):
357 """
andrewonlab7c211572014-10-15 16:45:20 -0400358 List the nodes currently visible
359 Issues command: 'nodes'
360 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800361 """
andrewonlab7c211572014-10-15 16:45:20 -0400362 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800363 cmdStr = "nodes"
364 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400365 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400366 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800367 main.log.error( self.name + ": EOF exception found" )
368 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400369 main.cleanup()
370 main.exit()
371 except:
kelvin8ec71442015-01-15 16:57:00 -0800372 main.log.info( self.name + " ::::::" )
373 main.log.error( traceback.print_exc() )
374 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -0400375 main.cleanup()
376 main.exit()
377
kelvin8ec71442015-01-15 16:57:00 -0800378 def topology( self ):
379 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400380 Shows the current state of the topology
381 by issusing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800382 """
andrewonlab95ce8322014-10-13 14:12:04 -0400383 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800384 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800385 cmdStr = "onos:topology"
386 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800387 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400388 return handle
andrewonlab95ce8322014-10-13 14:12:04 -0400389 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800390 main.log.error( self.name + ": EOF exception found" )
391 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400392 main.cleanup()
393 main.exit()
394 except:
kelvin8ec71442015-01-15 16:57:00 -0800395 main.log.info( self.name + " ::::::" )
396 main.log.error( traceback.print_exc() )
397 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400398 main.cleanup()
399 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800400
kelvin-onlabd3b64892015-01-20 13:26:24 -0800401 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800402 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800403 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400404 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800405 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400406 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800407 cmdStr = "feature:install " + str( featureStr )
408 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800409 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400410 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400411 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800412 main.log.error( self.name + ": EOF exception found" )
413 main.log.error( self.name + ": " + self.handle.before )
414 main.log.report( "Failed to install feature" )
415 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400416 main.cleanup()
417 main.exit()
418 except:
kelvin8ec71442015-01-15 16:57:00 -0800419 main.log.info( self.name + " ::::::" )
420 main.log.error( traceback.print_exc() )
421 main.log.report( "Failed to install feature" )
422 main.log.report( "Exiting test" )
423 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400424 main.cleanup()
425 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800426
kelvin-onlabd3b64892015-01-20 13:26:24 -0800427 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800428 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400429 Uninstalls a specified feature
430 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800431 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400432 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800433 cmdStr = "feature:uninstall " + str( featureStr )
434 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800435 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400436 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400437 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800438 main.log.error( self.name + ": EOF exception found" )
439 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400440 main.cleanup()
441 main.exit()
442 except:
kelvin8ec71442015-01-15 16:57:00 -0800443 main.log.info( self.name + " ::::::" )
444 main.log.error( traceback.print_exc() )
445 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400446 main.cleanup()
447 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800448
kelvin-onlabd3b64892015-01-20 13:26:24 -0800449 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800450 """
Jon Hall7b02d952014-10-17 20:14:54 -0400451 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400452 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800453 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800454 """
andrewonlab86dc3082014-10-13 18:18:38 -0400455 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800456 if jsonFormat:
457 cmdStr = "devices -j"
458 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800459 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800460 handle variable here contains some ANSI escape color code
461 sequences at the end which are invisible in the print command
462 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800463 function. The repr( handle ) output when printed shows the
464 ANSI escape sequences. In json.loads( somestring ), this
465 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800466 json.loads would fail with the escape sequence. So we take off
467 that escape sequence using:
468
kelvin-onlabd3b64892015-01-20 13:26:24 -0800469 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
470 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800471 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800472 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
473 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400474 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400475 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800476 cmdStr = "devices"
477 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400478 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400479 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800480 main.log.error( self.name + ": EOF exception found" )
481 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400482 main.cleanup()
483 main.exit()
484 except:
kelvin8ec71442015-01-15 16:57:00 -0800485 main.log.info( self.name + " ::::::" )
486 main.log.error( traceback.print_exc() )
487 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -0400488 main.cleanup()
489 main.exit()
490
kelvin-onlabd3b64892015-01-20 13:26:24 -0800491 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800492 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800493 This balances the devices across all controllers
494 by issuing command: 'onos> onos:balance-masters'
495 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800496 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800497 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800498 cmdStr = "onos:balance-masters"
499 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800500 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800501 return main.TRUE
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800502 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800503 main.log.error( self.name + ": EOF exception found" )
504 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800505 main.cleanup()
506 main.exit()
507 except:
kelvin8ec71442015-01-15 16:57:00 -0800508 main.log.info( self.name + " ::::::" )
509 main.log.error( traceback.print_exc() )
510 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800511 main.cleanup()
512 main.exit()
513
kelvin-onlabd3b64892015-01-20 13:26:24 -0800514 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800515 """
Jon Halle8217482014-10-17 13:49:14 -0400516 Lists all core links
517 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800518 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800519 """
Jon Halle8217482014-10-17 13:49:14 -0400520 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800521 if jsonFormat:
522 cmdStr = "links -j"
523 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800524 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800525 handle variable here contains some ANSI escape color code
526 sequences at the end which are invisible in the print command
527 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800528 function. The repr( handle ) output when printed shows the ANSI
529 escape sequences. In json.loads( somestring ), this somestring
530 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800531 fail with the escape sequence. So we take off that escape
532 sequence using:
533
kelvin-onlabd3b64892015-01-20 13:26:24 -0800534 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
535 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800536 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800537 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
538 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400539 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400540 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800541 cmdStr = "links"
542 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400543 return handle
Jon Halle8217482014-10-17 13:49:14 -0400544 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800545 main.log.error( self.name + ": EOF exception found" )
546 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400547 main.cleanup()
548 main.exit()
549 except:
kelvin8ec71442015-01-15 16:57:00 -0800550 main.log.info( self.name + " ::::::" )
551 main.log.error( traceback.print_exc() )
552 main.log.info( self.name + " ::::::" )
Jon Halle8217482014-10-17 13:49:14 -0400553 main.cleanup()
554 main.exit()
555
kelvin-onlabd3b64892015-01-20 13:26:24 -0800556 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800557 """
Jon Halle8217482014-10-17 13:49:14 -0400558 Lists all ports
559 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800560 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800561 """
Jon Halle8217482014-10-17 13:49:14 -0400562 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800563 if jsonFormat:
564 cmdStr = "ports -j"
565 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800566 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800567 handle variable here contains some ANSI escape color code
568 sequences at the end which are invisible in the print command
569 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800570 function. The repr( handle ) output when printed shows the ANSI
571 escape sequences. In json.loads( somestring ), this somestring
572 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800573 fail with the escape sequence. So we take off that escape
574 sequence using the following commads:
575
kelvin-onlabd3b64892015-01-20 13:26:24 -0800576 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
577 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800578 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800579 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
580 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400581 return handle1
582
Jon Halle8217482014-10-17 13:49:14 -0400583 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800584 cmdStr = "ports"
585 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800586 return handle
Jon Halle8217482014-10-17 13:49:14 -0400587 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800588 main.log.error( self.name + ": EOF exception found" )
589 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400590 main.cleanup()
591 main.exit()
592 except:
kelvin8ec71442015-01-15 16:57:00 -0800593 main.log.info( self.name + " ::::::" )
594 main.log.error( traceback.print_exc() )
595 main.log.info( self.name + " ::::::" )
Jon Halle8217482014-10-17 13:49:14 -0400596 main.cleanup()
597 main.exit()
598
kelvin-onlabd3b64892015-01-20 13:26:24 -0800599 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800600 """
Jon Hall983a1702014-10-28 18:44:22 -0400601 Lists all devices and the controllers with roles assigned to them
602 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800603 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800604 """
andrewonlab7c211572014-10-15 16:45:20 -0400605 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800606 if jsonFormat:
607 cmdStr = "roles -j"
608 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800609 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800610 handle variable here contains some ANSI escape color code
611 sequences at the end which are invisible in the print command
612 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800613 function. The repr( handle ) output when printed shows the ANSI
614 escape sequences. In json.loads( somestring ), this somestring
615 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800616 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500617
Jon Halle3f39ff2015-01-13 11:50:53 -0800618 So we take off that escape sequence using the following
619 commads:
620
kelvin-onlabd3b64892015-01-20 13:26:24 -0800621 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
622 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800623 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800624 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
625 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400626 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400627
andrewonlab7c211572014-10-15 16:45:20 -0400628 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800629 cmdStr = "roles"
630 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800631 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400632 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800633 main.log.error( self.name + ": EOF exception found" )
634 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400635 main.cleanup()
636 main.exit()
637 except:
kelvin8ec71442015-01-15 16:57:00 -0800638 main.log.info( self.name + " ::::::" )
639 main.log.error( traceback.print_exc() )
640 main.log.info( self.name + " ::::::" )
Jon Hall983a1702014-10-28 18:44:22 -0400641 main.cleanup()
642 main.exit()
643
kelvin-onlabd3b64892015-01-20 13:26:24 -0800644 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800645 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800646 Given the a string containing the json representation of the "roles"
647 cli command and a partial or whole device id, returns a json object
648 containing the roles output for the first device whose id contains
649 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400650
651 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800652 A dict of the role assignments for the given device or
653 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800654 """
Jon Hall983a1702014-10-28 18:44:22 -0400655 try:
656 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800657 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400658 return None
659 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800660 rawRoles = self.roles()
661 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800662 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800663 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800664 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800665 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400666 return device
667 return None
andrewonlab7c211572014-10-15 16:45:20 -0400668
andrewonlab86dc3082014-10-13 18:18:38 -0400669 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800670 main.log.error( self.name + ": EOF exception found" )
671 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400672 main.cleanup()
673 main.exit()
674 except:
kelvin8ec71442015-01-15 16:57:00 -0800675 main.log.info( self.name + " ::::::" )
676 main.log.error( traceback.print_exc() )
677 main.log.info( self.name + " ::::::" )
andrewonlab86dc3082014-10-13 18:18:38 -0400678 main.cleanup()
679 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800680
kelvin-onlabd3b64892015-01-20 13:26:24 -0800681 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800682 """
Jon Hall94fd0472014-12-08 11:52:42 -0800683 Iterates through each device and checks if there is a master assigned
684 Returns: main.TRUE if each device has a master
685 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800686 """
Jon Hall94fd0472014-12-08 11:52:42 -0800687 try:
688 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800689 rawRoles = self.roles()
690 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800691 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800692 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800693 # print device
694 if device[ 'master' ] == "none":
695 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800696 return main.FALSE
697 return main.TRUE
698
699 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800700 main.log.error( self.name + ": EOF exception found" )
701 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800702 main.cleanup()
703 main.exit()
704 except:
kelvin8ec71442015-01-15 16:57:00 -0800705 main.log.info( self.name + " ::::::" )
706 main.log.error( traceback.print_exc() )
707 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -0800708 main.cleanup()
709 main.exit()
710
kelvin-onlabd3b64892015-01-20 13:26:24 -0800711 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800712 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400713 Returns string of paths, and the cost.
714 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800715 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400716 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800717 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
718 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800719 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800720 main.log.error( "Error in getting paths" )
721 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400722 else:
kelvin8ec71442015-01-15 16:57:00 -0800723 path = handle.split( ";" )[ 0 ]
724 cost = handle.split( ";" )[ 1 ]
725 return ( path, cost )
andrewonlab3e15ead2014-10-15 14:21:34 -0400726 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800727 main.log.error( self.name + ": EOF exception found" )
728 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400729 main.cleanup()
730 main.exit()
731 except:
kelvin8ec71442015-01-15 16:57:00 -0800732 main.log.info( self.name + " ::::::" )
733 main.log.error( traceback.print_exc() )
734 main.log.info( self.name + " ::::::" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400735 main.cleanup()
736 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800737
kelvin-onlabd3b64892015-01-20 13:26:24 -0800738 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800739 """
Jon Hallffb386d2014-11-21 13:43:38 -0800740 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400741 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800742 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800743 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400744 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800745 if jsonFormat:
746 cmdStr = "hosts -j"
747 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800748 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800749 handle variable here contains some ANSI escape color code
750 sequences at the end which are invisible in the print command
751 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800752 function. The repr( handle ) output when printed shows the ANSI
753 escape sequences. In json.loads( somestring ), this somestring
754 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800755 fail with the escape sequence. So we take off that escape
756 sequence using:
757
kelvin-onlabd3b64892015-01-20 13:26:24 -0800758 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
759 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800760 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800761 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
762 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400763 return handle1
764 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800765 cmdStr = "hosts"
766 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400767 return handle
768 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800769 main.log.error( self.name + ": EOF exception found" )
770 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400771 main.cleanup()
772 main.exit()
773 except:
kelvin8ec71442015-01-15 16:57:00 -0800774 main.log.info( self.name + " ::::::" )
775 main.log.error( traceback.print_exc() )
776 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400777 main.cleanup()
778 main.exit()
779
kelvin-onlabd3b64892015-01-20 13:26:24 -0800780 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800781 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400782 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800783
784 Note: mac must be a colon seperated mac address, but could be a
785 partial mac address
786
Jon Hall42db6dc2014-10-24 19:03:48 -0400787 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800788 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400789 import json
790 try:
kelvin8ec71442015-01-15 16:57:00 -0800791 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400792 return None
793 else:
794 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800795 rawHosts = self.hosts()
796 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800797 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800798 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800799 # print "%s in %s?" % ( mac, host[ 'id' ] )
800 if mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400801 return host
802 return None
803 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800804 main.log.error( self.name + ": EOF exception found" )
805 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400806 main.cleanup()
807 main.exit()
808 except:
kelvin8ec71442015-01-15 16:57:00 -0800809 main.log.info( self.name + " ::::::" )
810 main.log.error( traceback.print_exc() )
811 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400812 main.cleanup()
813 main.exit()
814
kelvin-onlabd3b64892015-01-20 13:26:24 -0800815 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800816 """
817 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400818 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800819
andrewonlab3f0a4af2014-10-17 12:25:14 -0400820 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800821 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400822 IMPORTANT:
823 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800824 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400825 Furthermore, it assumes that value of VLAN is '-1'
826 Description:
kelvin8ec71442015-01-15 16:57:00 -0800827 Converts mininet hosts ( h1, h2, h3... ) into
828 ONOS format ( 00:00:00:00:00:01/-1 , ... )
829 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400830 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800831 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400832
kelvin-onlabd3b64892015-01-20 13:26:24 -0800833 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800834 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800835 hostHex = hex( int( host ) ).zfill( 12 )
836 hostHex = str( hostHex ).replace( 'x', '0' )
837 i = iter( str( hostHex ) )
838 hostHex = ":".join( a + b for a, b in zip( i, i ) )
839 hostHex = hostHex + "/-1"
840 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400841
kelvin-onlabd3b64892015-01-20 13:26:24 -0800842 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400843
844 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800845 main.log.error( self.name + ": EOF exception found" )
846 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400847 main.cleanup()
848 main.exit()
849 except:
kelvin8ec71442015-01-15 16:57:00 -0800850 main.log.info( self.name + " ::::::" )
851 main.log.error( traceback.print_exc() )
852 main.log.info( self.name + " ::::::" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400853 main.cleanup()
854 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400855
kelvin-onlabd3b64892015-01-20 13:26:24 -0800856 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800857 """
andrewonlabe6745342014-10-17 14:29:13 -0400858 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800859 * hostIdOne: ONOS host id for host1
860 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400861 Description:
kelvin8ec71442015-01-15 16:57:00 -0800862 Adds a host-to-host intent ( bidrectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500863 specifying the two hosts.
kelvin8ec71442015-01-15 16:57:00 -0800864 """
andrewonlabe6745342014-10-17 14:29:13 -0400865 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800866 cmdStr = "add-host-intent " + str( hostIdOne ) +\
867 " " + str( hostIdTwo )
868 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800869 main.log.info( "Host intent installed between " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800870 str( hostIdOne ) + " and " + str( hostIdTwo ) )
andrewonlabe6745342014-10-17 14:29:13 -0400871 return handle
andrewonlabe6745342014-10-17 14:29:13 -0400872 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800873 main.log.error( self.name + ": EOF exception found" )
874 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400875 main.cleanup()
876 main.exit()
877 except:
kelvin8ec71442015-01-15 16:57:00 -0800878 main.log.info( self.name + " ::::::" )
879 main.log.error( traceback.print_exc() )
880 main.log.info( self.name + " ::::::" )
andrewonlabe6745342014-10-17 14:29:13 -0400881 main.cleanup()
882 main.exit()
883
kelvin-onlabd3b64892015-01-20 13:26:24 -0800884 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800885 """
andrewonlab7b31d232014-10-24 13:31:47 -0400886 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800887 * ingressDevice: device id of ingress device
888 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400889 Optional:
890 TODO: Still needs to be implemented via dev side
kelvin-onlab898a6c62015-01-16 14:13:53 -0800891 """
andrewonlab7b31d232014-10-24 13:31:47 -0400892 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800893 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
894 " " + str( egressDevice )
895 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800896 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800897 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400898 return handle
899 else:
900 return main.TRUE
andrewonlab7b31d232014-10-24 13:31:47 -0400901 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800902 main.log.error( self.name + ": EOF exception found" )
903 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400904 main.cleanup()
905 main.exit()
906 except:
kelvin8ec71442015-01-15 16:57:00 -0800907 main.log.info( self.name + " ::::::" )
908 main.log.error( traceback.print_exc() )
909 main.log.info( self.name + " ::::::" )
andrewonlab7b31d232014-10-24 13:31:47 -0400910 main.cleanup()
911 main.exit()
912
kelvin-onlabd3b64892015-01-20 13:26:24 -0800913 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800914 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800915 ingressDevice,
916 egressDevice,
917 portIngress="",
918 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800919 ethType="",
920 ethSrc="",
921 ethDst="",
922 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800923 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800924 ipProto="",
925 ipSrc="",
926 ipDst="",
927 tcpSrc="",
928 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -0800929 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400930 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800931 * ingressDevice: device id of ingress device
932 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400933 Optional:
934 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -0800935 * ethSrc: specify ethSrc ( i.e. src mac addr )
936 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500937 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -0800938 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -0500939 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -0800940 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -0500941 * ipSrc: specify ip source address
942 * ipDst: specify ip destination address
943 * tcpSrc: specify tcp source port
944 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400945 Description:
kelvin8ec71442015-01-15 16:57:00 -0800946 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -0400947 specifying device id's and optional fields
948
Jon Halle3f39ff2015-01-13 11:50:53 -0800949 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -0400950 options developers provide for point-to-point
951 intent via cli
kelvin8ec71442015-01-15 16:57:00 -0800952 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400953 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400954 cmd = ""
955
kelvin8ec71442015-01-15 16:57:00 -0800956 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500957 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800958 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -0500959 and not ipProto and not ipSrc and not ipDst \
960 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -0500961 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -0500962
andrewonlab289e4b72014-10-21 21:24:18 -0400963 else:
andrewonlab36af3822014-11-18 17:48:18 -0500964 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -0800965
andrewonlab0c0a6772014-10-22 12:31:18 -0400966 if ethType:
kelvin8ec71442015-01-15 16:57:00 -0800967 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -0400968 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -0800969 cmd += " --ethSrc " + str( ethSrc )
970 if ethDst:
971 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500972 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -0800973 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800974 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -0500975 cmd += " --lambda "
976 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -0800977 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -0500978 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -0800979 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -0500980 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -0800981 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -0500982 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -0800983 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -0500984 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -0800985 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -0400986
kelvin8ec71442015-01-15 16:57:00 -0800987 # Check whether the user appended the port
988 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -0800989 if "/" in ingressDevice:
990 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -0500991 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800992 if not portIngress:
kelvin8ec71442015-01-15 16:57:00 -0800993 main.log.error( "You must specify " +
994 "the ingress port" )
995 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -0500996 return main.FALSE
997
kelvin8ec71442015-01-15 16:57:00 -0800998 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -0800999 str( ingressDevice ) + "/" +\
1000 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001001
kelvin-onlabd3b64892015-01-20 13:26:24 -08001002 if "/" in egressDevice:
1003 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001004 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001005 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001006 main.log.error( "You must specify " +
1007 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001008 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001009
kelvin8ec71442015-01-15 16:57:00 -08001010 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001011 str( egressDevice ) + "/" +\
1012 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001013
kelvin-onlab898a6c62015-01-16 14:13:53 -08001014 handle = self.sendline( cmd )
1015 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001016 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001017 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001018 else:
1019 return main.TRUE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001020 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001021 main.log.error( self.name + ": EOF exception found" )
1022 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001023 main.cleanup()
1024 main.exit()
1025 except:
kelvin8ec71442015-01-15 16:57:00 -08001026 main.log.info( self.name + " ::::::" )
1027 main.log.error( traceback.print_exc() )
1028 main.log.info( self.name + " ::::::" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001029 main.cleanup()
1030 main.exit()
1031
kelvin-onlabd3b64892015-01-20 13:26:24 -08001032 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001033 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001034 ingressDevice1,
1035 ingressDevice2,
1036 egressDevice,
1037 portIngress="",
1038 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001039 ethType="",
1040 ethSrc="",
1041 ethDst="",
1042 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001043 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001044 ipProto="",
1045 ipSrc="",
1046 ipDst="",
1047 tcpSrc="",
1048 tcpDst="",
1049 setEthSrc="",
1050 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001051 """
shahshreyad0c80432014-12-04 16:56:05 -08001052 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001053 This function assumes that there would be 2 ingress devices and
1054 one egress device. For more number of ingress devices, this
1055 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001056 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001057 * ingressDevice1: device id of ingress device1
1058 * ingressDevice2: device id of ingress device2
1059 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001060 Optional:
1061 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001062 * ethSrc: specify ethSrc ( i.e. src mac addr )
1063 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001064 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001065 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001066 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001067 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001068 * ipSrc: specify ip source address
1069 * ipDst: specify ip destination address
1070 * tcpSrc: specify tcp source port
1071 * tcpDst: specify tcp destination port
1072 * setEthSrc: action to Rewrite Source MAC Address
1073 * setEthDst: action to Rewrite Destination MAC Address
1074 Description:
kelvin8ec71442015-01-15 16:57:00 -08001075 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001076 specifying device id's and optional fields
1077
Jon Halle3f39ff2015-01-13 11:50:53 -08001078 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001079 options developers provide for multipointpoint-to-singlepoint
1080 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001081 """
shahshreyad0c80432014-12-04 16:56:05 -08001082 try:
1083 cmd = ""
1084
kelvin8ec71442015-01-15 16:57:00 -08001085 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001086 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001087 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001088 and not ipProto and not ipSrc and not ipDst\
1089 and not tcpSrc and not tcpDst and not setEthSrc\
1090 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001091 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001092
1093 else:
1094 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001095
shahshreyad0c80432014-12-04 16:56:05 -08001096 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001097 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001098 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001099 cmd += " --ethSrc " + str( ethSrc )
1100 if ethDst:
1101 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001102 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001103 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001104 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001105 cmd += " --lambda "
1106 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001107 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001108 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001109 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001110 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001111 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001112 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001113 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001114 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001115 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001116 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001117 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001118 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001119 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001120
kelvin8ec71442015-01-15 16:57:00 -08001121 # Check whether the user appended the port
1122 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001123 if "/" in ingressDevice1:
1124 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001125 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001126 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001127 main.log.error( "You must specify " +
1128 "the ingress port1" )
1129 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001130 return main.FALSE
1131
kelvin8ec71442015-01-15 16:57:00 -08001132 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001133 str( ingressDevice1 ) + "/" +\
1134 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001135
kelvin-onlabd3b64892015-01-20 13:26:24 -08001136 if "/" in ingressDevice2:
1137 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001138 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001139 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001140 main.log.error( "You must specify " +
1141 "the ingress port2" )
1142 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001143 return main.FALSE
1144
kelvin8ec71442015-01-15 16:57:00 -08001145 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001146 str( ingressDevice2 ) + "/" +\
1147 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001148
kelvin-onlabd3b64892015-01-20 13:26:24 -08001149 if "/" in egressDevice:
1150 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001151 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001152 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001153 main.log.error( "You must specify " +
1154 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001155 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001156
kelvin8ec71442015-01-15 16:57:00 -08001157 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001158 str( egressDevice ) + "/" +\
1159 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001160 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001161 handle = self.sendline( cmd )
1162 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001163 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001164 return self.handle
1165 else:
1166 return main.TRUE
shahshreyad0c80432014-12-04 16:56:05 -08001167 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001168 main.log.error( self.name + ": EOF exception found" )
1169 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001170 main.cleanup()
1171 main.exit()
1172 except:
kelvin8ec71442015-01-15 16:57:00 -08001173 main.log.info( self.name + " ::::::" )
1174 main.log.error( traceback.print_exc() )
1175 main.log.info( self.name + " ::::::" )
shahshreyad0c80432014-12-04 16:56:05 -08001176 main.cleanup()
1177 main.exit()
1178
kelvin-onlabd3b64892015-01-20 13:26:24 -08001179 def removeIntent( self, intentId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001180 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001181 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001182
1183 Returns:
1184 main.False on error and
1185 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001186 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001187 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001188 cmdStr = "remove-intent " + str( intentId )
1189 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001190 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001191 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001192 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001193 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001194 # TODO: Should this be main.TRUE
1195 return handle
andrewonlab9a50dfe2014-10-17 17:22:31 -04001196 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001197 main.log.error( self.name + ": EOF exception found" )
1198 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001199 main.cleanup()
1200 main.exit()
1201 except:
kelvin8ec71442015-01-15 16:57:00 -08001202 main.log.info( self.name + " ::::::" )
1203 main.log.error( traceback.print_exc() )
1204 main.log.info( self.name + " ::::::" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001205 main.cleanup()
1206 main.exit()
1207
kelvin-onlabd3b64892015-01-20 13:26:24 -08001208 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001209 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001210 NOTE: This method should be used after installing application:
1211 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001212 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001213 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001214 Description:
1215 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001216 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001217 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001218 if jsonFormat:
1219 cmdStr = "routes -j"
1220 handleTmp = self.sendline( cmdStr )
1221 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1222 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001223 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001224 cmdStr = "routes"
1225 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001226 return handle
pingping-lin8b306ac2014-11-17 18:13:51 -08001227 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001228 main.log.error( self.name + ": EOF exception found" )
1229 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001230 main.cleanup()
1231 main.exit()
1232 except:
kelvin8ec71442015-01-15 16:57:00 -08001233 main.log.info( self.name + " ::::::" )
1234 main.log.error( traceback.print_exc() )
1235 main.log.info( self.name + " ::::::" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001236 main.cleanup()
1237 main.exit()
1238
kelvin-onlabd3b64892015-01-20 13:26:24 -08001239 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001240 """
andrewonlab377693f2014-10-21 16:00:30 -04001241 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001242 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001243 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001244 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001245 """
andrewonlabe6745342014-10-17 14:29:13 -04001246 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001247 if jsonFormat:
1248 cmdStr = "intents -j"
1249 handle = self.sendline( cmdStr )
1250 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1251 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001252 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001253 cmdStr = "intents"
1254 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001255 return handle
andrewonlabe6745342014-10-17 14:29:13 -04001256 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001257 main.log.error( self.name + ": EOF exception found" )
1258 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001259 main.cleanup()
1260 main.exit()
1261 except:
kelvin8ec71442015-01-15 16:57:00 -08001262 main.log.info( self.name + " ::::::" )
1263 main.log.error( traceback.print_exc() )
1264 main.log.info( self.name + " ::::::" )
andrewonlabe6745342014-10-17 14:29:13 -04001265 main.cleanup()
1266 main.exit()
1267
kelvin-onlabd3b64892015-01-20 13:26:24 -08001268 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001269 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001270 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001271 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001272 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001273 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001274 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001275 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001276 if jsonFormat:
1277 cmdStr = "flows -j"
1278 handle = self.sendline( cmdStr )
1279 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1280 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001281 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001282 cmdStr = "flows"
1283 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001284 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001285 main.log.error( self.name + ".flows() response: " +
1286 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001287 return handle
Shreya Shah0f01c812014-10-26 20:15:28 -04001288 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001289 main.log.error( self.name + ": EOF exception found" )
1290 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001291 main.cleanup()
1292 main.exit()
1293 except:
kelvin8ec71442015-01-15 16:57:00 -08001294 main.log.info( self.name + " ::::::" )
1295 main.log.error( traceback.print_exc() )
1296 main.log.info( self.name + " ::::::" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001297 main.cleanup()
1298 main.exit()
1299
kelvin-onlabd3b64892015-01-20 13:26:24 -08001300 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
1301 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001302 """
andrewonlab87852b02014-11-19 18:44:19 -05001303 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001304 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001305 a specific point-to-point intent definition
1306 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001307 * dpidSrc: specify source dpid
1308 * dpidDst: specify destination dpid
1309 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001310 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001311 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001312 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001313 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001314 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001315 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001316 """
andrewonlab87852b02014-11-19 18:44:19 -05001317 try:
kelvin8ec71442015-01-15 16:57:00 -08001318 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001319 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1320 str( numIntents )
1321 if numMult:
1322 cmd += " " + str( numMult )
1323 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001324 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001325 if appId:
1326 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001327 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001328 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1329 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001330 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001331 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001332 main.log.info( handle )
1333 # Split result by newline
1334 newline = handle.split( "\r\r\n" )
1335 # Ignore the first object of list, which is empty
1336 newline = newline[ 1: ]
1337 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001338 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001339 result = result.split( ": " )
1340 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001341 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1342 main.log.info( latResult )
1343 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001344 else:
1345 return main.TRUE
andrewonlab87852b02014-11-19 18:44:19 -05001346 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001347 main.log.error( self.name + ": EOF exception found" )
1348 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001349 main.cleanup()
1350 main.exit()
1351 except:
kelvin8ec71442015-01-15 16:57:00 -08001352 main.log.info( self.name + " ::::::" )
1353 main.log.error( traceback.print_exc() )
1354 main.log.info( self.name + " ::::::" )
andrewonlab87852b02014-11-19 18:44:19 -05001355 main.cleanup()
1356 main.exit()
1357
kelvin-onlabd3b64892015-01-20 13:26:24 -08001358 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001359 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001360 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001361 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001362 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001363 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001364 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001365 if jsonFormat:
1366 cmdStr = "intents-events-metrics -j"
1367 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001368 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001369 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1370 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001371 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001372 cmdStr = "intents-events-metrics"
1373 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001374 return handle
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001375 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001376 main.log.error( self.name + ": EOF exception found" )
1377 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001378 main.cleanup()
1379 main.exit()
1380 except:
kelvin8ec71442015-01-15 16:57:00 -08001381 main.log.info( self.name + " ::::::" )
1382 main.log.error( traceback.print_exc() )
1383 main.log.info( self.name + " ::::::" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001384 main.cleanup()
1385 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001386
kelvin-onlabd3b64892015-01-20 13:26:24 -08001387 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001388 """
1389 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001390 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001391 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001392 """
andrewonlab867212a2014-10-22 20:13:38 -04001393 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001394 if jsonFormat:
1395 cmdStr = "topology-events-metrics -j"
1396 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001397 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001398 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1399 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001400 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001401 cmdStr = "topology-events-metrics"
1402 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001403 return handle
andrewonlab867212a2014-10-22 20:13:38 -04001404 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001405 main.log.error( self.name + ": EOF exception found" )
1406 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001407 main.cleanup()
1408 main.exit()
1409 except:
kelvin8ec71442015-01-15 16:57:00 -08001410 main.log.info( self.name + " ::::::" )
1411 main.log.error( traceback.print_exc() )
1412 main.log.info( self.name + " ::::::" )
andrewonlab867212a2014-10-22 20:13:38 -04001413 main.cleanup()
1414 main.exit()
1415
kelvin8ec71442015-01-15 16:57:00 -08001416 # Wrapper functions ****************
1417 # Wrapper functions use existing driver
1418 # functions and extends their use case.
1419 # For example, we may use the output of
1420 # a normal driver function, and parse it
1421 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001422
kelvin-onlabd3b64892015-01-20 13:26:24 -08001423 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001424 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001425 Description:
1426 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001427 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001428 try:
kelvin8ec71442015-01-15 16:57:00 -08001429 # Obtain output of intents function
kelvin-onlabd3b64892015-01-20 13:26:24 -08001430 intentsStr = self.intents()
1431 allIntentList = []
1432 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001433
kelvin8ec71442015-01-15 16:57:00 -08001434 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001435 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1436 for intents in intentsList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001437 if "onos>" in intents:
1438 continue
1439 elif "intents" in intents:
1440 continue
1441 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001442 lineList = intents.split( " " )
1443 allIntentList.append( lineList[ 0 ] )
kelvin8ec71442015-01-15 16:57:00 -08001444
kelvin-onlabd3b64892015-01-20 13:26:24 -08001445 allIntentList = allIntentList[ 1:-2 ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001446
kelvin-onlabd3b64892015-01-20 13:26:24 -08001447 for intents in allIntentList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001448 if not intents:
1449 continue
1450 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001451 intentIdList.append( intents )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001452
kelvin-onlabd3b64892015-01-20 13:26:24 -08001453 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001454
1455 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001456 main.log.error( self.name + ": EOF exception found" )
1457 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001458 main.cleanup()
1459 main.exit()
1460 except:
kelvin8ec71442015-01-15 16:57:00 -08001461 main.log.info( self.name + " ::::::" )
1462 main.log.error( traceback.print_exc() )
1463 main.log.info( self.name + " ::::::" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001464 main.cleanup()
1465 main.exit()
1466
kelvin-onlabd3b64892015-01-20 13:26:24 -08001467 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001468 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001469 Use 'devices' function to obtain list of all devices
1470 and parse the result to obtain a list of all device
1471 id's. Returns this list. Returns empty list if no
1472 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001473 List is ordered sequentially
1474
andrewonlab3e15ead2014-10-15 14:21:34 -04001475 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001476 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001477 the ids. By obtaining the list of device ids on the fly,
1478 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001479 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001480 try:
kelvin8ec71442015-01-15 16:57:00 -08001481 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001482 devicesStr = self.devices( jsonFormat=False )
1483 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001484
kelvin-onlabd3b64892015-01-20 13:26:24 -08001485 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001486 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001487 return idList
kelvin8ec71442015-01-15 16:57:00 -08001488
1489 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001490 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001491 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001492 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001493 # Split list further into arguments before and after string
1494 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001495 # append to idList
1496 for arg in tempList:
1497 idList.append( arg.split( "id=" )[ 1 ] )
1498 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001499
1500 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001501 main.log.error( self.name + ": EOF exception found" )
1502 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001503 main.cleanup()
1504 main.exit()
1505 except:
kelvin8ec71442015-01-15 16:57:00 -08001506 main.log.info( self.name + " ::::::" )
1507 main.log.error( traceback.print_exc() )
1508 main.log.info( self.name + " ::::::" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001509 main.cleanup()
1510 main.exit()
1511
kelvin-onlabd3b64892015-01-20 13:26:24 -08001512 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001513 """
andrewonlab7c211572014-10-15 16:45:20 -04001514 Uses 'nodes' function to obtain list of all nodes
1515 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001516 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001517 Returns:
1518 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001519 """
andrewonlab7c211572014-10-15 16:45:20 -04001520 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001521 nodesStr = self.nodes()
1522 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001523
kelvin-onlabd3b64892015-01-20 13:26:24 -08001524 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001525 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001526 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001527
kelvin-onlabd3b64892015-01-20 13:26:24 -08001528 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001529 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001530
kelvin8ec71442015-01-15 16:57:00 -08001531 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001532 nodesList = nodesStr.split( "," )
1533 tempList = [ node for node in nodesList if "id=" in node ]
1534 for arg in tempList:
1535 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001536
kelvin-onlabd3b64892015-01-20 13:26:24 -08001537 return idList
kelvin8ec71442015-01-15 16:57:00 -08001538
andrewonlab7c211572014-10-15 16:45:20 -04001539 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001540 main.log.error( self.name + ": EOF exception found" )
1541 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001542 main.cleanup()
1543 main.exit()
1544 except:
kelvin8ec71442015-01-15 16:57:00 -08001545 main.log.info( self.name + " ::::::" )
1546 main.log.error( traceback.print_exc() )
1547 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -04001548 main.cleanup()
1549 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001550
kelvin-onlabd3b64892015-01-20 13:26:24 -08001551 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001552 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001553 Return the first device from the devices api whose 'id' contains 'dpid'
1554 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001555 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001556 import json
1557 try:
kelvin8ec71442015-01-15 16:57:00 -08001558 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001559 return None
1560 else:
kelvin8ec71442015-01-15 16:57:00 -08001561 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001562 rawDevices = self.devices()
1563 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001564 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001565 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001566 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1567 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001568 return device
1569 return None
1570 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001571 main.log.error( self.name + ": EOF exception found" )
1572 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001573 main.cleanup()
1574 main.exit()
1575 except:
kelvin8ec71442015-01-15 16:57:00 -08001576 main.log.info( self.name + " ::::::" )
1577 main.log.error( traceback.print_exc() )
1578 main.log.info( self.name + " ::::::" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001579 main.cleanup()
1580 main.exit()
1581
kelvin-onlabd3b64892015-01-20 13:26:24 -08001582 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001583 """
1584 Checks the number of swithes & links that ONOS sees against the
1585 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001586 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001587
Jon Hall42db6dc2014-10-24 19:03:48 -04001588 Params: ip = ip used for the onos cli
1589 numoswitch = expected number of switches
1590 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001591 logLevel = level to log to. Currently accepts
1592 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001593
1594
kelvin-onlabd3b64892015-01-20 13:26:24 -08001595 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001596
kelvin8ec71442015-01-15 16:57:00 -08001597 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001598 main.FALSE if the numer of switches and links is incorrect,
1599 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001600 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001601 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001602 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001603 if topology == {}:
1604 return main.ERROR
1605 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001606 # Is the number of switches is what we expected
1607 devices = topology.get( 'devices', False )
1608 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001609 if devices == False or links == False:
1610 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001611 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001612 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001613 linkCheck = ( int( links ) == int( numolink ) )
1614 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001615 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001616 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001617 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001618 result = main.TRUE
1619 else:
1620 output = output + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001621 "The number of links and switches does not matc\
1622 h what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001623 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001624 output = output + "\n ONOS sees %i devices (%i expected) \
1625 and %i links (%i expected)" % (
1626 int( devices ), int( numoswitch ), int( links ),
1627 int( numolink ) )
1628 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001629 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001630 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001631 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001632 else:
kelvin8ec71442015-01-15 16:57:00 -08001633 main.log.info( output )
1634 return result
Jon Hall42db6dc2014-10-24 19:03:48 -04001635 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001636 main.log.error( self.name + ": EOF exception found" )
1637 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001638 main.cleanup()
1639 main.exit()
1640 except:
kelvin8ec71442015-01-15 16:57:00 -08001641 main.log.info( self.name + " ::::::" )
1642 main.log.error( traceback.print_exc() )
1643 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001644 main.cleanup()
1645 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001646
kelvin-onlabd3b64892015-01-20 13:26:24 -08001647 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001648 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001649 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001650 deviceId must be the id of a device as seen in the onos devices command
1651 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001652 role must be either master, standby, or none
1653
Jon Halle3f39ff2015-01-13 11:50:53 -08001654 Returns:
1655 main.TRUE or main.FALSE based on argument verification and
1656 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001657 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001658 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001659 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001660 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001661 cmdStr = "device-role " +\
1662 str( deviceId ) + " " +\
1663 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001664 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001665 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001666 if re.search( "Error", handle ):
1667 # end color output to escape any colours
1668 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001669 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001670 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001671 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001672 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001673 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001674 main.log.error( "Invalid 'role' given to device_role(). " +
1675 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001676 return main.FALSE
Jon Hall1c9e8732014-10-27 19:29:27 -04001677 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001678 main.log.error( self.name + ": EOF exception found" )
1679 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001680 main.cleanup()
1681 main.exit()
1682 except:
kelvin8ec71442015-01-15 16:57:00 -08001683 main.log.info( self.name + " ::::::" )
1684 main.log.error( traceback.print_exc() )
1685 main.log.info( self.name + " ::::::" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001686 main.cleanup()
1687 main.exit()
1688
kelvin-onlabd3b64892015-01-20 13:26:24 -08001689 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001690 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001691 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001692 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001693 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001694 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001695 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001696 if jsonFormat:
1697 cmdStr = "clusters -j"
1698 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001699 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001700 handle variable here contains some ANSI escape color code
1701 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001702 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001703 function. The repr( handle ) output when printed shows the ANSI
1704 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001705 variable is actually repr( somestring ) and json.loads would
1706 fail with the escape sequence. So we take off that escape
1707 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001708
kelvin-onlabd3b64892015-01-20 13:26:24 -08001709 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1710 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001711 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001712 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1713 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001714 return handle1
1715 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001716 cmdStr = "clusters"
1717 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001718 return handle
1719 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001720 main.log.error( self.name + ": EOF exception found" )
1721 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001722 main.cleanup()
1723 main.exit()
1724 except:
kelvin8ec71442015-01-15 16:57:00 -08001725 main.log.info( self.name + " ::::::" )
1726 main.log.error( traceback.print_exc() )
1727 main.log.info( self.name + " ::::::" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001728 main.cleanup()
1729 main.exit()
1730
kelvin-onlabd3b64892015-01-20 13:26:24 -08001731 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001732 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001733 CLI command to get the current leader for the Election test application
1734 NOTE: Requires installation of the onos-app-election feature
1735 Returns: Node IP of the leader if one exists
1736 None if none exists
1737 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001738 """
Jon Hall94fd0472014-12-08 11:52:42 -08001739 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001740 cmdStr = "election-test-leader"
1741 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001742 # Leader
1743 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001744 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001745 nodeSearch = re.search( leaderPattern, response )
1746 if nodeSearch:
1747 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001748 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001749 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001750 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001751 # no leader
1752 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001753 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001754 nullSearch = re.search( nullPattern, response )
1755 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001756 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001757 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001758 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001759 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001760 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001761 if re.search( errorPattern, response ):
1762 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001763 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001764 return main.FALSE
1765 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001766 main.log.error( "Error in election_test_leader: " +
1767 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001768 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001769 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001770 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001771 main.log.error( self.name + ": EOF exception found" )
1772 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001773 main.cleanup()
1774 main.exit()
1775 except:
kelvin8ec71442015-01-15 16:57:00 -08001776 main.log.info( self.name + " ::::::" )
1777 main.log.error( traceback.print_exc() )
1778 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001779 main.cleanup()
1780 main.exit()
1781
kelvin-onlabd3b64892015-01-20 13:26:24 -08001782 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001783 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001784 CLI command to run for leadership of the Election test application.
1785 NOTE: Requires installation of the onos-app-election feature
1786 Returns: Main.TRUE on success
1787 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001788 """
Jon Hall94fd0472014-12-08 11:52:42 -08001789 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001790 cmdStr = "election-test-run"
1791 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001792 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001793 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001794 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001795 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001796 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001797 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001798 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001799 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001800 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001801 errorPattern = "Command\snot\sfound"
1802 if re.search( errorPattern, response ):
1803 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001804 return main.FALSE
1805 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001806 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001807 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001808 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001809 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001810 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001811 main.log.error( self.name + ": EOF exception found" )
1812 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001813 main.cleanup()
1814 main.exit()
1815 except:
kelvin8ec71442015-01-15 16:57:00 -08001816 main.log.info( self.name + " ::::::" )
1817 main.log.error( traceback.print_exc() )
1818 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001819 main.cleanup()
1820 main.exit()
1821
kelvin-onlabd3b64892015-01-20 13:26:24 -08001822 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08001823 """
Jon Hall94fd0472014-12-08 11:52:42 -08001824 * CLI command to withdraw the local node from leadership election for
1825 * the Election test application.
1826 #NOTE: Requires installation of the onos-app-election feature
1827 Returns: Main.TRUE on success
1828 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001829 """
Jon Hall94fd0472014-12-08 11:52:42 -08001830 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001831 cmdStr = "election-test-withdraw"
1832 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001833 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001834 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001835 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001836 if re.search( successPattern, response ):
1837 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001838 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001839 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001840 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001841 errorPattern = "Command\snot\sfound"
1842 if re.search( errorPattern, response ):
1843 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001844 return main.FALSE
1845 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001846 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001847 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001848 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001849 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001850 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001851 main.log.error( self.name + ": EOF exception found" )
1852 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001853 main.cleanup()
1854 main.exit()
1855 except:
kelvin8ec71442015-01-15 16:57:00 -08001856 main.log.info( self.name + " ::::::" )
1857 main.log.error( traceback.print_exc() )
1858 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001859 main.cleanup()
1860 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001861
kelvin8ec71442015-01-15 16:57:00 -08001862 def getDevicePortsEnabledCount( self, dpid ):
1863 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001864 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001865 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001866 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001867 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001868 cmdStr = "onos:ports -e " + dpid + " | wc -l"
1869 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001870 if re.search( "No such device", output ):
1871 main.log.error( "Error in getting ports" )
1872 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001873 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001874 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001875 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001876 main.log.error( self.name + ": EOF exception found" )
1877 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001878 main.cleanup()
1879 main.exit()
1880 except:
kelvin8ec71442015-01-15 16:57:00 -08001881 main.log.info( self.name + " ::::::" )
1882 main.log.error( traceback.print_exc() )
1883 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001884 main.cleanup()
1885 main.exit()
1886
kelvin8ec71442015-01-15 16:57:00 -08001887 def getDeviceLinksActiveCount( self, dpid ):
1888 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001889 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001890 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001891 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001892 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001893 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
1894 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001895 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001896 main.log.error( "Error in getting ports " )
1897 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001898 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001899 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001900 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001901 main.log.error( self.name + ": EOF exception found" )
1902 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001903 main.cleanup()
1904 main.exit()
1905 except:
kelvin8ec71442015-01-15 16:57:00 -08001906 main.log.info( self.name + " ::::::" )
1907 main.log.error( traceback.print_exc() )
1908 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001909 main.cleanup()
1910 main.exit()
1911
kelvin8ec71442015-01-15 16:57:00 -08001912 def getAllIntentIds( self ):
1913 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001914 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08001915 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001916 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001917 cmdStr = "onos:intents | grep id="
1918 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001919 if re.search( "Error", output ):
1920 main.log.error( "Error in getting ports" )
1921 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001922 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001923 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001924 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001925 main.log.error( self.name + ": EOF exception found" )
1926 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001927 main.cleanup()
1928 main.exit()
1929 except:
kelvin8ec71442015-01-15 16:57:00 -08001930 main.log.info( self.name + " ::::::" )
1931 main.log.error( traceback.print_exc() )
1932 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001933 main.cleanup()
1934 main.exit()