blob: ecff2f596cd2619bcb1e4b86941067aaf435477c [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:
Jon Hall7bdfc122015-01-23 11:45:32 -0800186 self.handle.setecho(False)
kelvin8ec71442015-01-15 16:57:00 -0800187 self.handle.sendline( "" )
188 x = self.handle.expect( [
189 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500190
191 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800192 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500193 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400194
kelvin8ec71442015-01-15 16:57:00 -0800195 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800196 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800197 i = self.handle.expect( [
198 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800199 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400200
201 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800202 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800203 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800204 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800205 "config:property-set -p org.apache.karaf.shel\
206 l sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800207 karafTimeout )
208 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800209 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800210 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400211 return main.TRUE
212 else:
kelvin8ec71442015-01-15 16:57:00 -0800213 # If failed, send ctrl+c to process and try again
214 main.log.info( "Starting CLI failed. Retrying..." )
215 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800216 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800217 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
218 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400219 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800220 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800221 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800222 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800223 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800224 "config:property-set -p org.apache.karaf.shell\
225 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800226 karafTimeout )
227 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800228 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800229 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400230 return main.TRUE
231 else:
kelvin8ec71442015-01-15 16:57:00 -0800232 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800233 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400234 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400235
236 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800237 main.log.error( self.name + ": EOF exception found" )
238 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400239 main.cleanup()
240 main.exit()
241 except:
kelvin8ec71442015-01-15 16:57:00 -0800242 main.log.info( self.name + " ::::::" )
243 main.log.error( traceback.print_exc() )
244 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400245 main.cleanup()
246 main.exit()
247
kelvin-onlabd3b64892015-01-20 13:26:24 -0800248 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800249 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800250 Send a completely user specified string to
251 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400252 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800253
andrewonlaba18f6bf2014-10-13 19:31:54 -0400254 Warning: There are no sanity checking to commands
255 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800256 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400257 try:
kelvin8ec71442015-01-15 16:57:00 -0800258 self.handle.sendline( "" )
259 self.handle.expect( "onos>" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400260
kelvin-onlab898a6c62015-01-16 14:13:53 -0800261 self.handle.sendline( "log:log \"Sending CLI command: '"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800262 + cmdStr + "'\"" )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800263 self.handle.expect( "onos>" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800264 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800265 self.handle.expect( "onos>" )
Jon Hallaea67aa2015-01-23 13:30:57 -0800266 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
267 + self.name + "." )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400268
269 handle = self.handle.before
Jon Hallaea67aa2015-01-23 13:30:57 -0800270 print "handle before anything:",
Jon Hall7bdfc122015-01-23 11:45:32 -0800271 print repr( handle )
272 # Remove control strings from output
273 ansiEscape = re.compile( r'\x1b[^m]*m' )
274 handle = ansiEscape.sub( '', handle )
Jon Hallaea67aa2015-01-23 13:30:57 -0800275 handle = re.sub( r"\s\r", "", handle )
276 handle = handle.strip()
277 print "handle after stripping:",
278 print repr(handle)
Jon Hall7bdfc122015-01-23 11:45:32 -0800279 # parse for just the output, remove the cmd from handle
280 output = handle.split( cmdStr, 1 )[1]
Jon Hallaea67aa2015-01-23 13:30:57 -0800281 print "output:",
Jon Hall7bdfc122015-01-23 11:45:32 -0800282 print repr( output )
kelvin8ec71442015-01-15 16:57:00 -0800283
andrewonlaba18f6bf2014-10-13 19:31:54 -0400284
Jon Hall7bdfc122015-01-23 11:45:32 -0800285 return output
andrewonlaba18f6bf2014-10-13 19:31:54 -0400286 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800287 main.log.error( self.name + ": EOF exception found" )
288 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400289 main.cleanup()
290 main.exit()
291 except:
kelvin8ec71442015-01-15 16:57:00 -0800292 main.log.info( self.name + " ::::::" )
293 main.log.error( traceback.print_exc() )
294 main.log.info( self.name + " ::::::" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400295 main.cleanup()
296 main.exit()
297
kelvin8ec71442015-01-15 16:57:00 -0800298 # IMPORTANT NOTE:
299 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800300 # the cli command changing 'a:b' with 'aB'.
301 # Ex ) onos:topology > onosTopology
302 # onos:links > onosLinks
303 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800304
kelvin-onlabd3b64892015-01-20 13:26:24 -0800305 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800306 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400307 Adds a new cluster node by ID and address information.
308 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800309 * nodeId
310 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400311 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800312 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800313 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400314 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800315 cmdStr = "add-node " + str( nodeId ) + " " +\
316 str( ONOSIp ) + " " + str( tcpPort )
317 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800318 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800319 main.log.error( "Error in adding node" )
320 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800321 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400322 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800323 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400324 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400325 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800326 main.log.error( self.name + ": EOF exception found" )
327 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400328 main.cleanup()
329 main.exit()
330 except:
kelvin8ec71442015-01-15 16:57:00 -0800331 main.log.info( self.name + " ::::::" )
332 main.log.error( traceback.print_exc() )
333 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400334 main.cleanup()
335 main.exit()
336
kelvin-onlabd3b64892015-01-20 13:26:24 -0800337 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800338 """
andrewonlab86dc3082014-10-13 18:18:38 -0400339 Removes a cluster by ID
340 Issues command: 'remove-node [<node-id>]'
341 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800342 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800343 """
andrewonlab86dc3082014-10-13 18:18:38 -0400344 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400345
kelvin-onlabd3b64892015-01-20 13:26:24 -0800346 cmdStr = "remove-node " + str( nodeId )
347 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800348 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400349
350 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800351
andrewonlab86dc3082014-10-13 18:18:38 -0400352 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800353 main.log.error( self.name + ": EOF exception found" )
354 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400355 main.cleanup()
356 main.exit()
357 except:
kelvin8ec71442015-01-15 16:57:00 -0800358 main.log.info( self.name + " ::::::" )
359 main.log.error( traceback.print_exc() )
360 main.log.info( self.name + " ::::::" )
andrewonlab86dc3082014-10-13 18:18:38 -0400361 main.cleanup()
362 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400363
kelvin8ec71442015-01-15 16:57:00 -0800364 def nodes( self ):
365 """
andrewonlab7c211572014-10-15 16:45:20 -0400366 List the nodes currently visible
367 Issues command: 'nodes'
368 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800369 """
andrewonlab7c211572014-10-15 16:45:20 -0400370 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800371 cmdStr = "nodes"
372 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400373 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400374 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800375 main.log.error( self.name + ": EOF exception found" )
376 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400377 main.cleanup()
378 main.exit()
379 except:
kelvin8ec71442015-01-15 16:57:00 -0800380 main.log.info( self.name + " ::::::" )
381 main.log.error( traceback.print_exc() )
382 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -0400383 main.cleanup()
384 main.exit()
385
kelvin8ec71442015-01-15 16:57:00 -0800386 def topology( self ):
387 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400388 Shows the current state of the topology
389 by issusing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800390 """
andrewonlab95ce8322014-10-13 14:12:04 -0400391 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800392 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800393 cmdStr = "onos:topology"
394 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800395 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400396 return handle
andrewonlab95ce8322014-10-13 14:12:04 -0400397 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800398 main.log.error( self.name + ": EOF exception found" )
399 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400400 main.cleanup()
401 main.exit()
402 except:
kelvin8ec71442015-01-15 16:57:00 -0800403 main.log.info( self.name + " ::::::" )
404 main.log.error( traceback.print_exc() )
405 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400406 main.cleanup()
407 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800408
kelvin-onlabd3b64892015-01-20 13:26:24 -0800409 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800410 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800411 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400412 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800413 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400414 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800415 cmdStr = "feature:install " + str( featureStr )
416 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800417 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400418 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400419 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800420 main.log.error( self.name + ": EOF exception found" )
421 main.log.error( self.name + ": " + self.handle.before )
422 main.log.report( "Failed to install feature" )
423 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400424 main.cleanup()
425 main.exit()
426 except:
kelvin8ec71442015-01-15 16:57:00 -0800427 main.log.info( self.name + " ::::::" )
428 main.log.error( traceback.print_exc() )
429 main.log.report( "Failed to install feature" )
430 main.log.report( "Exiting test" )
431 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400432 main.cleanup()
433 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800434
kelvin-onlabd3b64892015-01-20 13:26:24 -0800435 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800436 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400437 Uninstalls a specified feature
438 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800439 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400440 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800441 cmdStr = "feature:uninstall " + str( featureStr )
442 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800443 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400444 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400445 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800446 main.log.error( self.name + ": EOF exception found" )
447 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400448 main.cleanup()
449 main.exit()
450 except:
kelvin8ec71442015-01-15 16:57:00 -0800451 main.log.info( self.name + " ::::::" )
452 main.log.error( traceback.print_exc() )
453 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400454 main.cleanup()
455 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800456
kelvin-onlabd3b64892015-01-20 13:26:24 -0800457 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800458 """
Jon Hall7b02d952014-10-17 20:14:54 -0400459 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400460 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800461 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800462 """
andrewonlab86dc3082014-10-13 18:18:38 -0400463 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800464 if jsonFormat:
465 cmdStr = "devices -j"
466 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800467 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800468 handle variable here contains some ANSI escape color code
469 sequences at the end which are invisible in the print command
470 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800471 function. The repr( handle ) output when printed shows the
472 ANSI escape sequences. In json.loads( somestring ), this
473 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800474 json.loads would fail with the escape sequence. So we take off
475 that escape sequence using:
476
kelvin-onlabd3b64892015-01-20 13:26:24 -0800477 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
478 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800479 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800480 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
481 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400482 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400483 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800484 cmdStr = "devices"
485 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400486 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400487 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800488 main.log.error( self.name + ": EOF exception found" )
489 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400490 main.cleanup()
491 main.exit()
492 except:
kelvin8ec71442015-01-15 16:57:00 -0800493 main.log.info( self.name + " ::::::" )
494 main.log.error( traceback.print_exc() )
495 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -0400496 main.cleanup()
497 main.exit()
498
kelvin-onlabd3b64892015-01-20 13:26:24 -0800499 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800500 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800501 This balances the devices across all controllers
502 by issuing command: 'onos> onos:balance-masters'
503 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800504 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800505 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800506 cmdStr = "onos:balance-masters"
507 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800508 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800509 return main.TRUE
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800510 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800511 main.log.error( self.name + ": EOF exception found" )
512 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800513 main.cleanup()
514 main.exit()
515 except:
kelvin8ec71442015-01-15 16:57:00 -0800516 main.log.info( self.name + " ::::::" )
517 main.log.error( traceback.print_exc() )
518 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800519 main.cleanup()
520 main.exit()
521
kelvin-onlabd3b64892015-01-20 13:26:24 -0800522 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800523 """
Jon Halle8217482014-10-17 13:49:14 -0400524 Lists all core links
525 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800526 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800527 """
Jon Halle8217482014-10-17 13:49:14 -0400528 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800529 if jsonFormat:
530 cmdStr = "links -j"
531 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800532 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800533 handle variable here contains some ANSI escape color code
534 sequences at the end which are invisible in the print command
535 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800536 function. The repr( handle ) output when printed shows the ANSI
537 escape sequences. In json.loads( somestring ), this somestring
538 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800539 fail with the escape sequence. So we take off that escape
540 sequence using:
541
kelvin-onlabd3b64892015-01-20 13:26:24 -0800542 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
543 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800544 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800545 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
546 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400547 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400548 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800549 cmdStr = "links"
550 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400551 return handle
Jon Halle8217482014-10-17 13:49:14 -0400552 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800553 main.log.error( self.name + ": EOF exception found" )
554 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400555 main.cleanup()
556 main.exit()
557 except:
kelvin8ec71442015-01-15 16:57:00 -0800558 main.log.info( self.name + " ::::::" )
559 main.log.error( traceback.print_exc() )
560 main.log.info( self.name + " ::::::" )
Jon Halle8217482014-10-17 13:49:14 -0400561 main.cleanup()
562 main.exit()
563
kelvin-onlabd3b64892015-01-20 13:26:24 -0800564 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800565 """
Jon Halle8217482014-10-17 13:49:14 -0400566 Lists all ports
567 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800568 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800569 """
Jon Halle8217482014-10-17 13:49:14 -0400570 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800571 if jsonFormat:
572 cmdStr = "ports -j"
573 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800574 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800575 handle variable here contains some ANSI escape color code
576 sequences at the end which are invisible in the print command
577 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800578 function. The repr( handle ) output when printed shows the ANSI
579 escape sequences. In json.loads( somestring ), this somestring
580 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800581 fail with the escape sequence. So we take off that escape
582 sequence using the following commads:
583
kelvin-onlabd3b64892015-01-20 13:26:24 -0800584 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
585 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800586 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800587 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
588 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400589 return handle1
590
Jon Halle8217482014-10-17 13:49:14 -0400591 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800592 cmdStr = "ports"
593 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800594 return handle
Jon Halle8217482014-10-17 13:49:14 -0400595 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800596 main.log.error( self.name + ": EOF exception found" )
597 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400598 main.cleanup()
599 main.exit()
600 except:
kelvin8ec71442015-01-15 16:57:00 -0800601 main.log.info( self.name + " ::::::" )
602 main.log.error( traceback.print_exc() )
603 main.log.info( self.name + " ::::::" )
Jon Halle8217482014-10-17 13:49:14 -0400604 main.cleanup()
605 main.exit()
606
kelvin-onlabd3b64892015-01-20 13:26:24 -0800607 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800608 """
Jon Hall983a1702014-10-28 18:44:22 -0400609 Lists all devices and the controllers with roles assigned to them
610 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800611 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800612 """
andrewonlab7c211572014-10-15 16:45:20 -0400613 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800614 if jsonFormat:
615 cmdStr = "roles -j"
616 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800617 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800618 handle variable here contains some ANSI escape color code
619 sequences at the end which are invisible in the print command
620 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800621 function. The repr( handle ) output when printed shows the ANSI
622 escape sequences. In json.loads( somestring ), this somestring
623 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800624 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500625
Jon Halle3f39ff2015-01-13 11:50:53 -0800626 So we take off that escape sequence using the following
627 commads:
628
kelvin-onlabd3b64892015-01-20 13:26:24 -0800629 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
630 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800631 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800632 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
633 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400634 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400635
andrewonlab7c211572014-10-15 16:45:20 -0400636 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800637 cmdStr = "roles"
638 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800639 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400640 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800641 main.log.error( self.name + ": EOF exception found" )
642 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400643 main.cleanup()
644 main.exit()
645 except:
kelvin8ec71442015-01-15 16:57:00 -0800646 main.log.info( self.name + " ::::::" )
647 main.log.error( traceback.print_exc() )
648 main.log.info( self.name + " ::::::" )
Jon Hall983a1702014-10-28 18:44:22 -0400649 main.cleanup()
650 main.exit()
651
kelvin-onlabd3b64892015-01-20 13:26:24 -0800652 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800653 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800654 Given the a string containing the json representation of the "roles"
655 cli command and a partial or whole device id, returns a json object
656 containing the roles output for the first device whose id contains
657 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400658
659 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800660 A dict of the role assignments for the given device or
661 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800662 """
Jon Hall983a1702014-10-28 18:44:22 -0400663 try:
664 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800665 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400666 return None
667 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800668 rawRoles = self.roles()
669 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800670 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800671 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800672 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800673 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400674 return device
675 return None
andrewonlab7c211572014-10-15 16:45:20 -0400676
andrewonlab86dc3082014-10-13 18:18:38 -0400677 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800678 main.log.error( self.name + ": EOF exception found" )
679 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400680 main.cleanup()
681 main.exit()
682 except:
kelvin8ec71442015-01-15 16:57:00 -0800683 main.log.info( self.name + " ::::::" )
684 main.log.error( traceback.print_exc() )
685 main.log.info( self.name + " ::::::" )
andrewonlab86dc3082014-10-13 18:18:38 -0400686 main.cleanup()
687 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800688
kelvin-onlabd3b64892015-01-20 13:26:24 -0800689 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800690 """
Jon Hall94fd0472014-12-08 11:52:42 -0800691 Iterates through each device and checks if there is a master assigned
692 Returns: main.TRUE if each device has a master
693 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800694 """
Jon Hall94fd0472014-12-08 11:52:42 -0800695 try:
696 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800697 rawRoles = self.roles()
698 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800699 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800700 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800701 # print device
702 if device[ 'master' ] == "none":
703 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800704 return main.FALSE
705 return main.TRUE
706
707 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800708 main.log.error( self.name + ": EOF exception found" )
709 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800710 main.cleanup()
711 main.exit()
712 except:
kelvin8ec71442015-01-15 16:57:00 -0800713 main.log.info( self.name + " ::::::" )
714 main.log.error( traceback.print_exc() )
715 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -0800716 main.cleanup()
717 main.exit()
718
kelvin-onlabd3b64892015-01-20 13:26:24 -0800719 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800720 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400721 Returns string of paths, and the cost.
722 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800723 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400724 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800725 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
726 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800727 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800728 main.log.error( "Error in getting paths" )
729 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400730 else:
kelvin8ec71442015-01-15 16:57:00 -0800731 path = handle.split( ";" )[ 0 ]
732 cost = handle.split( ";" )[ 1 ]
733 return ( path, cost )
andrewonlab3e15ead2014-10-15 14:21:34 -0400734 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800735 main.log.error( self.name + ": EOF exception found" )
736 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400737 main.cleanup()
738 main.exit()
739 except:
kelvin8ec71442015-01-15 16:57:00 -0800740 main.log.info( self.name + " ::::::" )
741 main.log.error( traceback.print_exc() )
742 main.log.info( self.name + " ::::::" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400743 main.cleanup()
744 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800745
kelvin-onlabd3b64892015-01-20 13:26:24 -0800746 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800747 """
Jon Hallffb386d2014-11-21 13:43:38 -0800748 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400749 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800750 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800751 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400752 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800753 if jsonFormat:
754 cmdStr = "hosts -j"
755 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800756 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800757 handle variable here contains some ANSI escape color code
758 sequences at the end which are invisible in the print command
759 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800760 function. The repr( handle ) output when printed shows the ANSI
761 escape sequences. In json.loads( somestring ), this somestring
762 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800763 fail with the escape sequence. So we take off that escape
764 sequence using:
765
kelvin-onlabd3b64892015-01-20 13:26:24 -0800766 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
767 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800768 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800769 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
770 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400771 return handle1
772 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800773 cmdStr = "hosts"
774 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400775 return handle
776 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800777 main.log.error( self.name + ": EOF exception found" )
778 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400779 main.cleanup()
780 main.exit()
781 except:
kelvin8ec71442015-01-15 16:57:00 -0800782 main.log.info( self.name + " ::::::" )
783 main.log.error( traceback.print_exc() )
784 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400785 main.cleanup()
786 main.exit()
787
kelvin-onlabd3b64892015-01-20 13:26:24 -0800788 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800789 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400790 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800791
792 Note: mac must be a colon seperated mac address, but could be a
793 partial mac address
794
Jon Hall42db6dc2014-10-24 19:03:48 -0400795 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800796 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400797 import json
798 try:
kelvin8ec71442015-01-15 16:57:00 -0800799 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400800 return None
801 else:
802 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800803 rawHosts = self.hosts()
804 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800805 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800806 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800807 # print "%s in %s?" % ( mac, host[ 'id' ] )
808 if mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400809 return host
810 return None
811 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800812 main.log.error( self.name + ": EOF exception found" )
813 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400814 main.cleanup()
815 main.exit()
816 except:
kelvin8ec71442015-01-15 16:57:00 -0800817 main.log.info( self.name + " ::::::" )
818 main.log.error( traceback.print_exc() )
819 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400820 main.cleanup()
821 main.exit()
822
kelvin-onlabd3b64892015-01-20 13:26:24 -0800823 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800824 """
825 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400826 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800827
andrewonlab3f0a4af2014-10-17 12:25:14 -0400828 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800829 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400830 IMPORTANT:
831 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800832 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400833 Furthermore, it assumes that value of VLAN is '-1'
834 Description:
kelvin8ec71442015-01-15 16:57:00 -0800835 Converts mininet hosts ( h1, h2, h3... ) into
836 ONOS format ( 00:00:00:00:00:01/-1 , ... )
837 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400838 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800839 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400840
kelvin-onlabd3b64892015-01-20 13:26:24 -0800841 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800842 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800843 hostHex = hex( int( host ) ).zfill( 12 )
844 hostHex = str( hostHex ).replace( 'x', '0' )
845 i = iter( str( hostHex ) )
846 hostHex = ":".join( a + b for a, b in zip( i, i ) )
847 hostHex = hostHex + "/-1"
848 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400849
kelvin-onlabd3b64892015-01-20 13:26:24 -0800850 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400851
852 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800853 main.log.error( self.name + ": EOF exception found" )
854 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400855 main.cleanup()
856 main.exit()
857 except:
kelvin8ec71442015-01-15 16:57:00 -0800858 main.log.info( self.name + " ::::::" )
859 main.log.error( traceback.print_exc() )
860 main.log.info( self.name + " ::::::" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400861 main.cleanup()
862 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400863
kelvin-onlabd3b64892015-01-20 13:26:24 -0800864 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800865 """
andrewonlabe6745342014-10-17 14:29:13 -0400866 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800867 * hostIdOne: ONOS host id for host1
868 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400869 Description:
kelvin8ec71442015-01-15 16:57:00 -0800870 Adds a host-to-host intent ( bidrectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500871 specifying the two hosts.
kelvin8ec71442015-01-15 16:57:00 -0800872 """
andrewonlabe6745342014-10-17 14:29:13 -0400873 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800874 cmdStr = "add-host-intent " + str( hostIdOne ) +\
875 " " + str( hostIdTwo )
876 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800877 main.log.info( "Host intent installed between " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800878 str( hostIdOne ) + " and " + str( hostIdTwo ) )
andrewonlabe6745342014-10-17 14:29:13 -0400879 return handle
andrewonlabe6745342014-10-17 14:29:13 -0400880 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800881 main.log.error( self.name + ": EOF exception found" )
882 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400883 main.cleanup()
884 main.exit()
885 except:
kelvin8ec71442015-01-15 16:57:00 -0800886 main.log.info( self.name + " ::::::" )
887 main.log.error( traceback.print_exc() )
888 main.log.info( self.name + " ::::::" )
andrewonlabe6745342014-10-17 14:29:13 -0400889 main.cleanup()
890 main.exit()
891
kelvin-onlabd3b64892015-01-20 13:26:24 -0800892 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800893 """
andrewonlab7b31d232014-10-24 13:31:47 -0400894 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800895 * ingressDevice: device id of ingress device
896 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400897 Optional:
898 TODO: Still needs to be implemented via dev side
kelvin-onlab898a6c62015-01-16 14:13:53 -0800899 """
andrewonlab7b31d232014-10-24 13:31:47 -0400900 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800901 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
902 " " + str( egressDevice )
903 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800904 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800905 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400906 return handle
907 else:
908 return main.TRUE
andrewonlab7b31d232014-10-24 13:31:47 -0400909 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800910 main.log.error( self.name + ": EOF exception found" )
911 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400912 main.cleanup()
913 main.exit()
914 except:
kelvin8ec71442015-01-15 16:57:00 -0800915 main.log.info( self.name + " ::::::" )
916 main.log.error( traceback.print_exc() )
917 main.log.info( self.name + " ::::::" )
andrewonlab7b31d232014-10-24 13:31:47 -0400918 main.cleanup()
919 main.exit()
920
kelvin-onlabd3b64892015-01-20 13:26:24 -0800921 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800922 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800923 ingressDevice,
924 egressDevice,
925 portIngress="",
926 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800927 ethType="",
928 ethSrc="",
929 ethDst="",
930 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800931 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800932 ipProto="",
933 ipSrc="",
934 ipDst="",
935 tcpSrc="",
936 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -0800937 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400938 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800939 * ingressDevice: device id of ingress device
940 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400941 Optional:
942 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -0800943 * ethSrc: specify ethSrc ( i.e. src mac addr )
944 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500945 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -0800946 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -0500947 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -0800948 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -0500949 * ipSrc: specify ip source address
950 * ipDst: specify ip destination address
951 * tcpSrc: specify tcp source port
952 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400953 Description:
kelvin8ec71442015-01-15 16:57:00 -0800954 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -0400955 specifying device id's and optional fields
956
Jon Halle3f39ff2015-01-13 11:50:53 -0800957 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -0400958 options developers provide for point-to-point
959 intent via cli
kelvin8ec71442015-01-15 16:57:00 -0800960 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400961 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400962 cmd = ""
963
kelvin8ec71442015-01-15 16:57:00 -0800964 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500965 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800966 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -0500967 and not ipProto and not ipSrc and not ipDst \
968 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -0500969 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -0500970
andrewonlab289e4b72014-10-21 21:24:18 -0400971 else:
andrewonlab36af3822014-11-18 17:48:18 -0500972 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -0800973
andrewonlab0c0a6772014-10-22 12:31:18 -0400974 if ethType:
kelvin8ec71442015-01-15 16:57:00 -0800975 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -0400976 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -0800977 cmd += " --ethSrc " + str( ethSrc )
978 if ethDst:
979 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500980 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -0800981 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800982 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -0500983 cmd += " --lambda "
984 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -0800985 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -0500986 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -0800987 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -0500988 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -0800989 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -0500990 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -0800991 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -0500992 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -0800993 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -0400994
kelvin8ec71442015-01-15 16:57:00 -0800995 # Check whether the user appended the port
996 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -0800997 if "/" in ingressDevice:
998 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -0500999 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001000 if not portIngress:
kelvin8ec71442015-01-15 16:57:00 -08001001 main.log.error( "You must specify " +
1002 "the ingress port" )
1003 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -05001004 return main.FALSE
1005
kelvin8ec71442015-01-15 16:57:00 -08001006 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001007 str( ingressDevice ) + "/" +\
1008 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001009
kelvin-onlabd3b64892015-01-20 13:26:24 -08001010 if "/" in egressDevice:
1011 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001012 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001013 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001014 main.log.error( "You must specify " +
1015 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001016 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001017
kelvin8ec71442015-01-15 16:57:00 -08001018 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001019 str( egressDevice ) + "/" +\
1020 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001021
kelvin-onlab898a6c62015-01-16 14:13:53 -08001022 handle = self.sendline( cmd )
1023 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001024 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001025 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001026 else:
1027 return main.TRUE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001028 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001029 main.log.error( self.name + ": EOF exception found" )
1030 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001031 main.cleanup()
1032 main.exit()
1033 except:
kelvin8ec71442015-01-15 16:57:00 -08001034 main.log.info( self.name + " ::::::" )
1035 main.log.error( traceback.print_exc() )
1036 main.log.info( self.name + " ::::::" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001037 main.cleanup()
1038 main.exit()
1039
kelvin-onlabd3b64892015-01-20 13:26:24 -08001040 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001041 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001042 ingressDevice1,
1043 ingressDevice2,
1044 egressDevice,
1045 portIngress="",
1046 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001047 ethType="",
1048 ethSrc="",
1049 ethDst="",
1050 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001051 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001052 ipProto="",
1053 ipSrc="",
1054 ipDst="",
1055 tcpSrc="",
1056 tcpDst="",
1057 setEthSrc="",
1058 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001059 """
shahshreyad0c80432014-12-04 16:56:05 -08001060 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001061 This function assumes that there would be 2 ingress devices and
1062 one egress device. For more number of ingress devices, this
1063 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001064 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001065 * ingressDevice1: device id of ingress device1
1066 * ingressDevice2: device id of ingress device2
1067 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001068 Optional:
1069 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001070 * ethSrc: specify ethSrc ( i.e. src mac addr )
1071 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001072 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001073 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001074 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001075 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001076 * ipSrc: specify ip source address
1077 * ipDst: specify ip destination address
1078 * tcpSrc: specify tcp source port
1079 * tcpDst: specify tcp destination port
1080 * setEthSrc: action to Rewrite Source MAC Address
1081 * setEthDst: action to Rewrite Destination MAC Address
1082 Description:
kelvin8ec71442015-01-15 16:57:00 -08001083 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001084 specifying device id's and optional fields
1085
Jon Halle3f39ff2015-01-13 11:50:53 -08001086 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001087 options developers provide for multipointpoint-to-singlepoint
1088 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001089 """
shahshreyad0c80432014-12-04 16:56:05 -08001090 try:
1091 cmd = ""
1092
kelvin8ec71442015-01-15 16:57:00 -08001093 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001094 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001095 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001096 and not ipProto and not ipSrc and not ipDst\
1097 and not tcpSrc and not tcpDst and not setEthSrc\
1098 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001099 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001100
1101 else:
1102 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001103
shahshreyad0c80432014-12-04 16:56:05 -08001104 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001105 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001106 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001107 cmd += " --ethSrc " + str( ethSrc )
1108 if ethDst:
1109 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001110 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001111 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001112 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001113 cmd += " --lambda "
1114 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001115 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001116 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001117 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001118 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001119 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001120 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001121 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001122 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001123 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001124 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001125 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001126 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001127 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001128
kelvin8ec71442015-01-15 16:57:00 -08001129 # Check whether the user appended the port
1130 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001131 if "/" in ingressDevice1:
1132 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001133 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001134 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001135 main.log.error( "You must specify " +
1136 "the ingress port1" )
1137 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001138 return main.FALSE
1139
kelvin8ec71442015-01-15 16:57:00 -08001140 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001141 str( ingressDevice1 ) + "/" +\
1142 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001143
kelvin-onlabd3b64892015-01-20 13:26:24 -08001144 if "/" in ingressDevice2:
1145 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001146 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001147 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001148 main.log.error( "You must specify " +
1149 "the ingress port2" )
1150 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001151 return main.FALSE
1152
kelvin8ec71442015-01-15 16:57:00 -08001153 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001154 str( ingressDevice2 ) + "/" +\
1155 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001156
kelvin-onlabd3b64892015-01-20 13:26:24 -08001157 if "/" in egressDevice:
1158 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001159 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001160 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001161 main.log.error( "You must specify " +
1162 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001163 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001164
kelvin8ec71442015-01-15 16:57:00 -08001165 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001166 str( egressDevice ) + "/" +\
1167 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001168 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001169 handle = self.sendline( cmd )
1170 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001171 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001172 return self.handle
1173 else:
1174 return main.TRUE
shahshreyad0c80432014-12-04 16:56:05 -08001175 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001176 main.log.error( self.name + ": EOF exception found" )
1177 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001178 main.cleanup()
1179 main.exit()
1180 except:
kelvin8ec71442015-01-15 16:57:00 -08001181 main.log.info( self.name + " ::::::" )
1182 main.log.error( traceback.print_exc() )
1183 main.log.info( self.name + " ::::::" )
shahshreyad0c80432014-12-04 16:56:05 -08001184 main.cleanup()
1185 main.exit()
1186
kelvin-onlabd3b64892015-01-20 13:26:24 -08001187 def removeIntent( self, intentId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001188 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001189 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001190
1191 Returns:
1192 main.False on error and
1193 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001194 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001195 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001196 cmdStr = "remove-intent " + str( intentId )
1197 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001198 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001199 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001200 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001201 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001202 # TODO: Should this be main.TRUE
1203 return handle
andrewonlab9a50dfe2014-10-17 17:22:31 -04001204 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001205 main.log.error( self.name + ": EOF exception found" )
1206 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001207 main.cleanup()
1208 main.exit()
1209 except:
kelvin8ec71442015-01-15 16:57:00 -08001210 main.log.info( self.name + " ::::::" )
1211 main.log.error( traceback.print_exc() )
1212 main.log.info( self.name + " ::::::" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001213 main.cleanup()
1214 main.exit()
1215
kelvin-onlabd3b64892015-01-20 13:26:24 -08001216 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001217 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001218 NOTE: This method should be used after installing application:
1219 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001220 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001221 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001222 Description:
1223 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001224 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001225 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001226 if jsonFormat:
1227 cmdStr = "routes -j"
1228 handleTmp = self.sendline( cmdStr )
1229 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1230 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001231 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001232 cmdStr = "routes"
1233 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001234 return handle
pingping-lin8b306ac2014-11-17 18:13:51 -08001235 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001236 main.log.error( self.name + ": EOF exception found" )
1237 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001238 main.cleanup()
1239 main.exit()
1240 except:
kelvin8ec71442015-01-15 16:57:00 -08001241 main.log.info( self.name + " ::::::" )
1242 main.log.error( traceback.print_exc() )
1243 main.log.info( self.name + " ::::::" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001244 main.cleanup()
1245 main.exit()
1246
kelvin-onlabd3b64892015-01-20 13:26:24 -08001247 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001248 """
andrewonlab377693f2014-10-21 16:00:30 -04001249 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001250 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001251 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001252 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001253 """
andrewonlabe6745342014-10-17 14:29:13 -04001254 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001255 if jsonFormat:
1256 cmdStr = "intents -j"
1257 handle = self.sendline( cmdStr )
1258 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1259 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001260 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001261 cmdStr = "intents"
1262 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001263 return handle
andrewonlabe6745342014-10-17 14:29:13 -04001264 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001265 main.log.error( self.name + ": EOF exception found" )
1266 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001267 main.cleanup()
1268 main.exit()
1269 except:
kelvin8ec71442015-01-15 16:57:00 -08001270 main.log.info( self.name + " ::::::" )
1271 main.log.error( traceback.print_exc() )
1272 main.log.info( self.name + " ::::::" )
andrewonlabe6745342014-10-17 14:29:13 -04001273 main.cleanup()
1274 main.exit()
1275
kelvin-onlabd3b64892015-01-20 13:26:24 -08001276 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001277 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001278 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001279 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001280 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001281 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001282 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001283 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001284 if jsonFormat:
1285 cmdStr = "flows -j"
1286 handle = self.sendline( cmdStr )
1287 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1288 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001289 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001290 cmdStr = "flows"
1291 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001292 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001293 main.log.error( self.name + ".flows() response: " +
1294 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001295 return handle
Shreya Shah0f01c812014-10-26 20:15:28 -04001296 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001297 main.log.error( self.name + ": EOF exception found" )
1298 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001299 main.cleanup()
1300 main.exit()
1301 except:
kelvin8ec71442015-01-15 16:57:00 -08001302 main.log.info( self.name + " ::::::" )
1303 main.log.error( traceback.print_exc() )
1304 main.log.info( self.name + " ::::::" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001305 main.cleanup()
1306 main.exit()
1307
kelvin-onlabd3b64892015-01-20 13:26:24 -08001308 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
1309 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001310 """
andrewonlab87852b02014-11-19 18:44:19 -05001311 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001312 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001313 a specific point-to-point intent definition
1314 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001315 * dpidSrc: specify source dpid
1316 * dpidDst: specify destination dpid
1317 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001318 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001319 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001320 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001321 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001322 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001323 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001324 """
andrewonlab87852b02014-11-19 18:44:19 -05001325 try:
kelvin8ec71442015-01-15 16:57:00 -08001326 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001327 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1328 str( numIntents )
1329 if numMult:
1330 cmd += " " + str( numMult )
1331 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001332 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001333 if appId:
1334 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001335 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001336 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1337 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001338 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001339 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001340 main.log.info( handle )
1341 # Split result by newline
1342 newline = handle.split( "\r\r\n" )
1343 # Ignore the first object of list, which is empty
1344 newline = newline[ 1: ]
1345 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001346 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001347 result = result.split( ": " )
1348 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001349 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1350 main.log.info( latResult )
1351 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001352 else:
1353 return main.TRUE
andrewonlab87852b02014-11-19 18:44:19 -05001354 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001355 main.log.error( self.name + ": EOF exception found" )
1356 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001357 main.cleanup()
1358 main.exit()
1359 except:
kelvin8ec71442015-01-15 16:57:00 -08001360 main.log.info( self.name + " ::::::" )
1361 main.log.error( traceback.print_exc() )
1362 main.log.info( self.name + " ::::::" )
andrewonlab87852b02014-11-19 18:44:19 -05001363 main.cleanup()
1364 main.exit()
1365
kelvin-onlabd3b64892015-01-20 13:26:24 -08001366 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001367 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001368 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001369 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001370 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001371 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001372 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001373 if jsonFormat:
1374 cmdStr = "intents-events-metrics -j"
1375 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001376 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001377 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1378 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001379 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001380 cmdStr = "intents-events-metrics"
1381 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001382 return handle
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001383 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001384 main.log.error( self.name + ": EOF exception found" )
1385 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001386 main.cleanup()
1387 main.exit()
1388 except:
kelvin8ec71442015-01-15 16:57:00 -08001389 main.log.info( self.name + " ::::::" )
1390 main.log.error( traceback.print_exc() )
1391 main.log.info( self.name + " ::::::" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001392 main.cleanup()
1393 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001394
kelvin-onlabd3b64892015-01-20 13:26:24 -08001395 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001396 """
1397 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001398 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001399 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001400 """
andrewonlab867212a2014-10-22 20:13:38 -04001401 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001402 if jsonFormat:
1403 cmdStr = "topology-events-metrics -j"
1404 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001405 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001406 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1407 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001408 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001409 cmdStr = "topology-events-metrics"
1410 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001411 return handle
andrewonlab867212a2014-10-22 20:13:38 -04001412 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001413 main.log.error( self.name + ": EOF exception found" )
1414 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001415 main.cleanup()
1416 main.exit()
1417 except:
kelvin8ec71442015-01-15 16:57:00 -08001418 main.log.info( self.name + " ::::::" )
1419 main.log.error( traceback.print_exc() )
1420 main.log.info( self.name + " ::::::" )
andrewonlab867212a2014-10-22 20:13:38 -04001421 main.cleanup()
1422 main.exit()
1423
kelvin8ec71442015-01-15 16:57:00 -08001424 # Wrapper functions ****************
1425 # Wrapper functions use existing driver
1426 # functions and extends their use case.
1427 # For example, we may use the output of
1428 # a normal driver function, and parse it
1429 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001430
kelvin-onlabd3b64892015-01-20 13:26:24 -08001431 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001432 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001433 Description:
1434 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001435 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001436 try:
kelvin8ec71442015-01-15 16:57:00 -08001437 # Obtain output of intents function
kelvin-onlabd3b64892015-01-20 13:26:24 -08001438 intentsStr = self.intents()
1439 allIntentList = []
1440 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001441
kelvin8ec71442015-01-15 16:57:00 -08001442 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001443 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1444 for intents in intentsList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001445 if "onos>" in intents:
1446 continue
1447 elif "intents" in intents:
1448 continue
1449 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001450 lineList = intents.split( " " )
1451 allIntentList.append( lineList[ 0 ] )
kelvin8ec71442015-01-15 16:57:00 -08001452
kelvin-onlabd3b64892015-01-20 13:26:24 -08001453 allIntentList = allIntentList[ 1:-2 ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001454
kelvin-onlabd3b64892015-01-20 13:26:24 -08001455 for intents in allIntentList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001456 if not intents:
1457 continue
1458 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001459 intentIdList.append( intents )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001460
kelvin-onlabd3b64892015-01-20 13:26:24 -08001461 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001462
1463 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001464 main.log.error( self.name + ": EOF exception found" )
1465 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001466 main.cleanup()
1467 main.exit()
1468 except:
kelvin8ec71442015-01-15 16:57:00 -08001469 main.log.info( self.name + " ::::::" )
1470 main.log.error( traceback.print_exc() )
1471 main.log.info( self.name + " ::::::" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001472 main.cleanup()
1473 main.exit()
1474
kelvin-onlabd3b64892015-01-20 13:26:24 -08001475 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001476 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001477 Use 'devices' function to obtain list of all devices
1478 and parse the result to obtain a list of all device
1479 id's. Returns this list. Returns empty list if no
1480 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001481 List is ordered sequentially
1482
andrewonlab3e15ead2014-10-15 14:21:34 -04001483 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001484 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001485 the ids. By obtaining the list of device ids on the fly,
1486 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001487 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001488 try:
kelvin8ec71442015-01-15 16:57:00 -08001489 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001490 devicesStr = self.devices( jsonFormat=False )
1491 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001492
kelvin-onlabd3b64892015-01-20 13:26:24 -08001493 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001494 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001495 return idList
kelvin8ec71442015-01-15 16:57:00 -08001496
1497 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001498 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001499 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001500 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001501 # Split list further into arguments before and after string
1502 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001503 # append to idList
1504 for arg in tempList:
1505 idList.append( arg.split( "id=" )[ 1 ] )
1506 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001507
1508 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001509 main.log.error( self.name + ": EOF exception found" )
1510 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001511 main.cleanup()
1512 main.exit()
1513 except:
kelvin8ec71442015-01-15 16:57:00 -08001514 main.log.info( self.name + " ::::::" )
1515 main.log.error( traceback.print_exc() )
1516 main.log.info( self.name + " ::::::" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001517 main.cleanup()
1518 main.exit()
1519
kelvin-onlabd3b64892015-01-20 13:26:24 -08001520 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001521 """
andrewonlab7c211572014-10-15 16:45:20 -04001522 Uses 'nodes' function to obtain list of all nodes
1523 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001524 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001525 Returns:
1526 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001527 """
andrewonlab7c211572014-10-15 16:45:20 -04001528 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001529 nodesStr = self.nodes()
1530 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001531
kelvin-onlabd3b64892015-01-20 13:26:24 -08001532 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001533 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001534 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001535
kelvin-onlabd3b64892015-01-20 13:26:24 -08001536 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001537 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001538
kelvin8ec71442015-01-15 16:57:00 -08001539 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001540 nodesList = nodesStr.split( "," )
1541 tempList = [ node for node in nodesList if "id=" in node ]
1542 for arg in tempList:
1543 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001544
kelvin-onlabd3b64892015-01-20 13:26:24 -08001545 return idList
kelvin8ec71442015-01-15 16:57:00 -08001546
andrewonlab7c211572014-10-15 16:45:20 -04001547 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001548 main.log.error( self.name + ": EOF exception found" )
1549 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001550 main.cleanup()
1551 main.exit()
1552 except:
kelvin8ec71442015-01-15 16:57:00 -08001553 main.log.info( self.name + " ::::::" )
1554 main.log.error( traceback.print_exc() )
1555 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -04001556 main.cleanup()
1557 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001558
kelvin-onlabd3b64892015-01-20 13:26:24 -08001559 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001560 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001561 Return the first device from the devices api whose 'id' contains 'dpid'
1562 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001563 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001564 import json
1565 try:
kelvin8ec71442015-01-15 16:57:00 -08001566 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001567 return None
1568 else:
kelvin8ec71442015-01-15 16:57:00 -08001569 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001570 rawDevices = self.devices()
1571 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001572 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001573 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001574 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1575 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001576 return device
1577 return None
1578 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001579 main.log.error( self.name + ": EOF exception found" )
1580 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001581 main.cleanup()
1582 main.exit()
1583 except:
kelvin8ec71442015-01-15 16:57:00 -08001584 main.log.info( self.name + " ::::::" )
1585 main.log.error( traceback.print_exc() )
1586 main.log.info( self.name + " ::::::" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001587 main.cleanup()
1588 main.exit()
1589
kelvin-onlabd3b64892015-01-20 13:26:24 -08001590 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001591 """
1592 Checks the number of swithes & links that ONOS sees against the
1593 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001594 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001595
Jon Hall42db6dc2014-10-24 19:03:48 -04001596 Params: ip = ip used for the onos cli
1597 numoswitch = expected number of switches
1598 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001599 logLevel = level to log to. Currently accepts
1600 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001601
1602
kelvin-onlabd3b64892015-01-20 13:26:24 -08001603 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001604
kelvin8ec71442015-01-15 16:57:00 -08001605 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001606 main.FALSE if the numer of switches and links is incorrect,
1607 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001608 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001609 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001610 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001611 if topology == {}:
1612 return main.ERROR
1613 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001614 # Is the number of switches is what we expected
1615 devices = topology.get( 'devices', False )
1616 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001617 if devices == False or links == False:
1618 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001619 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001620 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001621 linkCheck = ( int( links ) == int( numolink ) )
1622 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001623 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001624 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001625 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001626 result = main.TRUE
1627 else:
1628 output = output + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001629 "The number of links and switches does not matc\
1630 h what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001631 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001632 output = output + "\n ONOS sees %i devices (%i expected) \
1633 and %i links (%i expected)" % (
1634 int( devices ), int( numoswitch ), int( links ),
1635 int( numolink ) )
1636 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001637 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001638 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001639 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001640 else:
kelvin8ec71442015-01-15 16:57:00 -08001641 main.log.info( output )
1642 return result
Jon Hall42db6dc2014-10-24 19:03:48 -04001643 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001644 main.log.error( self.name + ": EOF exception found" )
1645 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001646 main.cleanup()
1647 main.exit()
1648 except:
kelvin8ec71442015-01-15 16:57:00 -08001649 main.log.info( self.name + " ::::::" )
1650 main.log.error( traceback.print_exc() )
1651 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001652 main.cleanup()
1653 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001654
kelvin-onlabd3b64892015-01-20 13:26:24 -08001655 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001656 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001657 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001658 deviceId must be the id of a device as seen in the onos devices command
1659 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001660 role must be either master, standby, or none
1661
Jon Halle3f39ff2015-01-13 11:50:53 -08001662 Returns:
1663 main.TRUE or main.FALSE based on argument verification and
1664 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001665 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001666 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001667 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001668 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001669 cmdStr = "device-role " +\
1670 str( deviceId ) + " " +\
1671 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001672 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001673 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001674 if re.search( "Error", handle ):
1675 # end color output to escape any colours
1676 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001677 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001678 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001679 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001680 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001681 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001682 main.log.error( "Invalid 'role' given to device_role(). " +
1683 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001684 return main.FALSE
Jon Hall1c9e8732014-10-27 19:29:27 -04001685 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001686 main.log.error( self.name + ": EOF exception found" )
1687 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001688 main.cleanup()
1689 main.exit()
1690 except:
kelvin8ec71442015-01-15 16:57:00 -08001691 main.log.info( self.name + " ::::::" )
1692 main.log.error( traceback.print_exc() )
1693 main.log.info( self.name + " ::::::" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001694 main.cleanup()
1695 main.exit()
1696
kelvin-onlabd3b64892015-01-20 13:26:24 -08001697 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001698 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001699 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001700 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001701 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001702 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001703 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001704 if jsonFormat:
1705 cmdStr = "clusters -j"
1706 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001707 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001708 handle variable here contains some ANSI escape color code
1709 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001710 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001711 function. The repr( handle ) output when printed shows the ANSI
1712 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001713 variable is actually repr( somestring ) and json.loads would
1714 fail with the escape sequence. So we take off that escape
1715 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001716
kelvin-onlabd3b64892015-01-20 13:26:24 -08001717 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1718 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001719 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001720 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1721 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001722 return handle1
1723 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001724 cmdStr = "clusters"
1725 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001726 return handle
1727 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001728 main.log.error( self.name + ": EOF exception found" )
1729 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001730 main.cleanup()
1731 main.exit()
1732 except:
kelvin8ec71442015-01-15 16:57:00 -08001733 main.log.info( self.name + " ::::::" )
1734 main.log.error( traceback.print_exc() )
1735 main.log.info( self.name + " ::::::" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001736 main.cleanup()
1737 main.exit()
1738
kelvin-onlabd3b64892015-01-20 13:26:24 -08001739 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001740 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001741 CLI command to get the current leader for the Election test application
1742 NOTE: Requires installation of the onos-app-election feature
1743 Returns: Node IP of the leader if one exists
1744 None if none exists
1745 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001746 """
Jon Hall94fd0472014-12-08 11:52:42 -08001747 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001748 cmdStr = "election-test-leader"
1749 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001750 # Leader
1751 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001752 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001753 nodeSearch = re.search( leaderPattern, response )
1754 if nodeSearch:
1755 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001756 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001757 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001758 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001759 # no leader
1760 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001761 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001762 nullSearch = re.search( nullPattern, response )
1763 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001764 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001765 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001766 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001767 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001768 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001769 if re.search( errorPattern, response ):
1770 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001771 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001772 return main.FALSE
1773 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001774 main.log.error( "Error in election_test_leader: " +
1775 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001776 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001777 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001778 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001779 main.log.error( self.name + ": EOF exception found" )
1780 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001781 main.cleanup()
1782 main.exit()
1783 except:
kelvin8ec71442015-01-15 16:57:00 -08001784 main.log.info( self.name + " ::::::" )
1785 main.log.error( traceback.print_exc() )
1786 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001787 main.cleanup()
1788 main.exit()
1789
kelvin-onlabd3b64892015-01-20 13:26:24 -08001790 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001791 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001792 CLI command to run for leadership of the Election test application.
1793 NOTE: Requires installation of the onos-app-election feature
1794 Returns: Main.TRUE on success
1795 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001796 """
Jon Hall94fd0472014-12-08 11:52:42 -08001797 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001798 cmdStr = "election-test-run"
1799 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001800 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001801 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001802 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001803 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001804 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001805 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001806 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001807 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001808 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001809 errorPattern = "Command\snot\sfound"
1810 if re.search( errorPattern, response ):
1811 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001812 return main.FALSE
1813 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001814 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001815 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001816 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001817 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001818 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001819 main.log.error( self.name + ": EOF exception found" )
1820 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001821 main.cleanup()
1822 main.exit()
1823 except:
kelvin8ec71442015-01-15 16:57:00 -08001824 main.log.info( self.name + " ::::::" )
1825 main.log.error( traceback.print_exc() )
1826 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001827 main.cleanup()
1828 main.exit()
1829
kelvin-onlabd3b64892015-01-20 13:26:24 -08001830 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08001831 """
Jon Hall94fd0472014-12-08 11:52:42 -08001832 * CLI command to withdraw the local node from leadership election for
1833 * the Election test application.
1834 #NOTE: Requires installation of the onos-app-election feature
1835 Returns: Main.TRUE on success
1836 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001837 """
Jon Hall94fd0472014-12-08 11:52:42 -08001838 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001839 cmdStr = "election-test-withdraw"
1840 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001841 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001842 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001843 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001844 if re.search( successPattern, response ):
1845 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001846 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001847 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001848 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001849 errorPattern = "Command\snot\sfound"
1850 if re.search( errorPattern, response ):
1851 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001852 return main.FALSE
1853 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001854 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001855 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001856 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001857 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001858 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001859 main.log.error( self.name + ": EOF exception found" )
1860 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001861 main.cleanup()
1862 main.exit()
1863 except:
kelvin8ec71442015-01-15 16:57:00 -08001864 main.log.info( self.name + " ::::::" )
1865 main.log.error( traceback.print_exc() )
1866 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001867 main.cleanup()
1868 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001869
kelvin8ec71442015-01-15 16:57:00 -08001870 def getDevicePortsEnabledCount( self, dpid ):
1871 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001872 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001873 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001874 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001875 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001876 cmdStr = "onos:ports -e " + dpid + " | wc -l"
1877 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001878 if re.search( "No such device", output ):
1879 main.log.error( "Error in getting ports" )
1880 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001881 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001882 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001883 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001884 main.log.error( self.name + ": EOF exception found" )
1885 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001886 main.cleanup()
1887 main.exit()
1888 except:
kelvin8ec71442015-01-15 16:57:00 -08001889 main.log.info( self.name + " ::::::" )
1890 main.log.error( traceback.print_exc() )
1891 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001892 main.cleanup()
1893 main.exit()
1894
kelvin8ec71442015-01-15 16:57:00 -08001895 def getDeviceLinksActiveCount( self, dpid ):
1896 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001897 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001898 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001899 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001900 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001901 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
1902 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001903 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001904 main.log.error( "Error in getting ports " )
1905 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001906 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001907 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001908 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001909 main.log.error( self.name + ": EOF exception found" )
1910 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001911 main.cleanup()
1912 main.exit()
1913 except:
kelvin8ec71442015-01-15 16:57:00 -08001914 main.log.info( self.name + " ::::::" )
1915 main.log.error( traceback.print_exc() )
1916 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001917 main.cleanup()
1918 main.exit()
1919
kelvin8ec71442015-01-15 16:57:00 -08001920 def getAllIntentIds( self ):
1921 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001922 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08001923 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001924 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001925 cmdStr = "onos:intents | grep id="
1926 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001927 if re.search( "Error", output ):
1928 main.log.error( "Error in getting ports" )
1929 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001930 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001931 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001932 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001933 main.log.error( self.name + ": EOF exception found" )
1934 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001935 main.cleanup()
1936 main.exit()
1937 except:
kelvin8ec71442015-01-15 16:57:00 -08001938 main.log.info( self.name + " ::::::" )
1939 main.log.error( traceback.print_exc() )
1940 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001941 main.cleanup()
1942 main.exit()