blob: c38da9f3ba2a99413bddf7a730b8d12096ad649b [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
kelvin8ec71442015-01-15 16:57:00 -080022sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040023from drivers.common.clidriver import CLI
24
andrewonlab95ce8322014-10-13 14:12:04 -040025
kelvin8ec71442015-01-15 16:57:00 -080026class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040027
kelvin8ec71442015-01-15 16:57:00 -080028 def __init__( self ):
29 """
30 Initialize client
31 """
32 super( CLI, self ).__init__()
33
34 def connect( self, **connectargs ):
35 """
andrewonlab95ce8322014-10-13 14:12:04 -040036 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080037 """
andrewonlab95ce8322014-10-13 14:12:04 -040038 try:
39 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080040 vars( self )[ key ] = connectargs[ key ]
andrewonlab95ce8322014-10-13 14:12:04 -040041 self.home = "~/ONOS"
42 for key in self.options:
43 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080044 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040045 break
46
kelvin8ec71442015-01-15 16:57:00 -080047 self.name = self.options[ 'name' ]
48 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080049 user_name=self.user_name,
50 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080051 port=self.port,
52 pwd=self.pwd,
53 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040054
kelvin8ec71442015-01-15 16:57:00 -080055 self.handle.sendline( "cd " + self.home )
56 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040057 if self.handle:
58 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080059 else:
60 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040061 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080062 except TypeError:
63 main.log.exception( self.name + ": Object not as expected" )
64 return None
andrewonlab95ce8322014-10-13 14:12:04 -040065 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080066 main.log.error( self.name + ": EOF exception found" )
67 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040068 main.cleanup()
69 main.exit()
70 except:
Jon Halld4d4b372015-01-28 16:02:41 -080071 main.log.exception( self.name + ": Uncaught exception!" )
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 """
Jon Halld61331b2015-02-17 16:35:47 -080079 response = main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -040080 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
Jon Halld4d4b372015-01-28 16:02:41 -080093 except TypeError:
94 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -080095 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -040096 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080097 main.log.error( self.name + ": EOF exception found" )
98 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040099 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800100 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400101 response = main.FALSE
102 return response
103
kelvin8ec71442015-01-15 16:57:00 -0800104 def logout( self ):
105 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500106 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800107 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500108 try:
kelvin8ec71442015-01-15 16:57:00 -0800109 self.handle.sendline( "" )
110 i = self.handle.expect( [
andrewonlab9627f432014-11-14 12:45:10 -0500111 "onos>",
kelvin8ec71442015-01-15 16:57:00 -0800112 "\$" ], timeout=10 )
andrewonlab9627f432014-11-14 12:45:10 -0500113 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800114 self.handle.sendline( "logout" )
115 self.handle.expect( "\$" )
andrewonlab9627f432014-11-14 12:45:10 -0500116 elif i == 1:
117 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800118
Jon Halld4d4b372015-01-28 16:02:41 -0800119 except TypeError:
120 main.log.exception( self.name + ": Object not as expected" )
121 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500122 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800123 main.log.error( self.name + ": eof exception found" )
124 main.log.error( self.name + ": " +
125 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500126 main.cleanup()
127 main.exit()
128 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800129 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500130 main.cleanup()
131 main.exit()
132
kelvin-onlabd3b64892015-01-20 13:26:24 -0800133 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800134 """
andrewonlab95ce8322014-10-13 14:12:04 -0400135 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800136
andrewonlab95ce8322014-10-13 14:12:04 -0400137 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800138 """
andrewonlab95ce8322014-10-13 14:12:04 -0400139 try:
140 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800141 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400142 main.cleanup()
143 main.exit()
144 else:
kelvin8ec71442015-01-15 16:57:00 -0800145 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800146 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800147 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400148 # and that this driver will have to change accordingly
kelvin8ec71442015-01-15 16:57:00 -0800149 self.handle.expect( "ONOS_CELL=" + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800150 handleBefore = self.handle.before
151 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800152 # Get the rest of the handle
153 self.handle.sendline( "" )
154 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800155 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400156
kelvin-onlabd3b64892015-01-20 13:26:24 -0800157 main.log.info( "Cell call returned: " + handleBefore +
158 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400159
160 return main.TRUE
161
Jon Halld4d4b372015-01-28 16:02:41 -0800162 except TypeError:
163 main.log.exception( self.name + ": Object not as expected" )
164 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400165 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800166 main.log.error( self.name + ": eof exception found" )
167 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400168 main.cleanup()
169 main.exit()
170 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800171 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400172 main.cleanup()
173 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800174
kelvin-onlabd3b64892015-01-20 13:26:24 -0800175 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800176 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800177 karafTimeout is an optional arugument. karafTimeout value passed
178 by user would be used to set the current karaf shell idle timeout.
179 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800180 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800181 Below is an example to start a session with 60 seconds idle timeout
182 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800183
Hari Krishna25d42f72015-01-05 15:08:28 -0800184 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800185 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800186
kelvin-onlabd3b64892015-01-20 13:26:24 -0800187 Note: karafTimeout is left as str so that this could be read
188 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800189 """
andrewonlab95ce8322014-10-13 14:12:04 -0400190 try:
kelvin8ec71442015-01-15 16:57:00 -0800191 self.handle.sendline( "" )
192 x = self.handle.expect( [
193 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500194
195 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800196 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500197 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400198
kelvin8ec71442015-01-15 16:57:00 -0800199 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800200 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800201 i = self.handle.expect( [
202 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800203 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400204
205 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800206 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800207 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800208 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800209 "config:property-set -p org.apache.karaf.shell\
210 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800211 karafTimeout )
212 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800213 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800214 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400215 return main.TRUE
216 else:
kelvin8ec71442015-01-15 16:57:00 -0800217 # If failed, send ctrl+c to process and try again
218 main.log.info( "Starting CLI failed. Retrying..." )
219 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800220 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800221 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
222 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400223 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800224 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800225 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800226 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800227 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800228 "config:property-set -p org.apache.karaf.shell\
229 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800230 karafTimeout )
231 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800232 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800233 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400234 return main.TRUE
235 else:
kelvin8ec71442015-01-15 16:57:00 -0800236 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800237 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400238 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400239
Jon Halld4d4b372015-01-28 16:02:41 -0800240 except TypeError:
241 main.log.exception( self.name + ": Object not as expected" )
242 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400243 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800244 main.log.error( self.name + ": EOF exception found" )
245 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400246 main.cleanup()
247 main.exit()
248 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800249 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400250 main.cleanup()
251 main.exit()
252
kelvin-onlabd3b64892015-01-20 13:26:24 -0800253 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800254 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800255 Send a completely user specified string to
256 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400257 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800258
andrewonlaba18f6bf2014-10-13 19:31:54 -0400259 Warning: There are no sanity checking to commands
260 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800261 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400262 try:
kelvin8ec71442015-01-15 16:57:00 -0800263 self.handle.sendline( "" )
264 self.handle.expect( "onos>" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400265
kelvin-onlab898a6c62015-01-16 14:13:53 -0800266 self.handle.sendline( "log:log \"Sending CLI command: '"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800267 + cmdStr + "'\"" )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800268 self.handle.expect( "onos>" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800269 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800270 self.handle.expect( "onos>" )
Jon Hallaea67aa2015-01-23 13:30:57 -0800271 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
272 + self.name + "." )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400273
274 handle = self.handle.before
Jon Hall7bdfc122015-01-23 11:45:32 -0800275 # Remove control strings from output
276 ansiEscape = re.compile( r'\x1b[^m]*m' )
277 handle = ansiEscape.sub( '', handle )
Jon Hall44225f82015-01-23 13:45:14 -0800278 #Remove extra return chars that get added
Jon Hallaea67aa2015-01-23 13:30:57 -0800279 handle = re.sub( r"\s\r", "", handle )
280 handle = handle.strip()
Jon Hall7bdfc122015-01-23 11:45:32 -0800281 # parse for just the output, remove the cmd from handle
282 output = handle.split( cmdStr, 1 )[1]
kelvin8ec71442015-01-15 16:57:00 -0800283
andrewonlaba18f6bf2014-10-13 19:31:54 -0400284
Jon Hall7bdfc122015-01-23 11:45:32 -0800285 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800286 except TypeError:
287 main.log.exception( self.name + ": Object not as expected" )
288 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400289 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800290 main.log.error( self.name + ": EOF exception found" )
291 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400292 main.cleanup()
293 main.exit()
294 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800295 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400296 main.cleanup()
297 main.exit()
298
kelvin8ec71442015-01-15 16:57:00 -0800299 # IMPORTANT NOTE:
300 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800301 # the cli command changing 'a:b' with 'aB'.
302 # Ex ) onos:topology > onosTopology
303 # onos:links > onosLinks
304 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800305
kelvin-onlabd3b64892015-01-20 13:26:24 -0800306 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800307 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400308 Adds a new cluster node by ID and address information.
309 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800310 * nodeId
311 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400312 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800313 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800314 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400315 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800316 cmdStr = "add-node " + str( nodeId ) + " " +\
317 str( ONOSIp ) + " " + str( tcpPort )
318 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800319 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800320 main.log.error( "Error in adding node" )
321 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800322 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400323 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800324 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400325 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800326 except TypeError:
327 main.log.exception( self.name + ": Object not as expected" )
328 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400329 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800330 main.log.error( self.name + ": EOF exception found" )
331 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400332 main.cleanup()
333 main.exit()
334 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800335 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400336 main.cleanup()
337 main.exit()
338
kelvin-onlabd3b64892015-01-20 13:26:24 -0800339 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800340 """
andrewonlab86dc3082014-10-13 18:18:38 -0400341 Removes a cluster by ID
342 Issues command: 'remove-node [<node-id>]'
343 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800344 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800345 """
andrewonlab86dc3082014-10-13 18:18:38 -0400346 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400347
kelvin-onlabd3b64892015-01-20 13:26:24 -0800348 cmdStr = "remove-node " + str( nodeId )
349 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800350 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400351
352 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800353
Jon Halld4d4b372015-01-28 16:02:41 -0800354 except TypeError:
355 main.log.exception( self.name + ": Object not as expected" )
356 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400357 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800358 main.log.error( self.name + ": EOF exception found" )
359 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400360 main.cleanup()
361 main.exit()
362 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800363 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400364 main.cleanup()
365 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400366
kelvin8ec71442015-01-15 16:57:00 -0800367 def nodes( self ):
368 """
andrewonlab7c211572014-10-15 16:45:20 -0400369 List the nodes currently visible
370 Issues command: 'nodes'
371 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800372 """
andrewonlab7c211572014-10-15 16:45:20 -0400373 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800374 cmdStr = "nodes"
375 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400376 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800377 except TypeError:
378 main.log.exception( self.name + ": Object not as expected" )
379 return None
andrewonlab7c211572014-10-15 16:45:20 -0400380 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800381 main.log.error( self.name + ": EOF exception found" )
382 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400383 main.cleanup()
384 main.exit()
385 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800386 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400387 main.cleanup()
388 main.exit()
389
kelvin8ec71442015-01-15 16:57:00 -0800390 def topology( self ):
391 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400392 Shows the current state of the topology
393 by issusing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800394 """
andrewonlab95ce8322014-10-13 14:12:04 -0400395 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800396 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800397 cmdStr = "onos:topology"
398 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800399 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400400 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800401 except TypeError:
402 main.log.exception( self.name + ": Object not as expected" )
403 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400404 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800405 main.log.error( self.name + ": EOF exception found" )
406 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400407 main.cleanup()
408 main.exit()
409 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800410 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400411 main.cleanup()
412 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800413
kelvin-onlabd3b64892015-01-20 13:26:24 -0800414 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800415 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800416 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400417 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800418 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400419 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800420 cmdStr = "feature:install " + str( featureStr )
421 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800422 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400423 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800424 except TypeError:
425 main.log.exception( self.name + ": Object not as expected" )
426 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400427 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800428 main.log.error( self.name + ": EOF exception found" )
429 main.log.error( self.name + ": " + self.handle.before )
430 main.log.report( "Failed to install feature" )
431 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400432 main.cleanup()
433 main.exit()
434 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800435 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800436 main.log.report( "Failed to install feature" )
437 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400438 main.cleanup()
439 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800440
kelvin-onlabd3b64892015-01-20 13:26:24 -0800441 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800442 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400443 Uninstalls a specified feature
444 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800445 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400446 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800447 cmdStr = "feature:uninstall " + str( featureStr )
448 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800449 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400450 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800451 except TypeError:
452 main.log.exception( self.name + ": Object not as expected" )
453 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400454 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800455 main.log.error( self.name + ": EOF exception found" )
456 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400457 main.cleanup()
458 main.exit()
459 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800460 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400461 main.cleanup()
462 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800463
kelvin-onlabd3b64892015-01-20 13:26:24 -0800464 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800465 """
Jon Hall7b02d952014-10-17 20:14:54 -0400466 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400467 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800468 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800469 """
andrewonlab86dc3082014-10-13 18:18:38 -0400470 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800471 if jsonFormat:
472 cmdStr = "devices -j"
473 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800474 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800475 handle variable here contains some ANSI escape color code
476 sequences at the end which are invisible in the print command
477 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800478 function. The repr( handle ) output when printed shows the
479 ANSI escape sequences. In json.loads( somestring ), this
480 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800481 json.loads would fail with the escape sequence. So we take off
482 that escape sequence using:
483
kelvin-onlabd3b64892015-01-20 13:26:24 -0800484 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
485 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800486 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800487 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
488 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400489 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400490 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800491 cmdStr = "devices"
492 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400493 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800494 except TypeError:
495 main.log.exception( self.name + ": Object not as expected" )
496 return None
andrewonlab7c211572014-10-15 16:45:20 -0400497 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800498 main.log.error( self.name + ": EOF exception found" )
499 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400500 main.cleanup()
501 main.exit()
502 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800503 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400504 main.cleanup()
505 main.exit()
506
kelvin-onlabd3b64892015-01-20 13:26:24 -0800507 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800508 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800509 This balances the devices across all controllers
510 by issuing command: 'onos> onos:balance-masters'
511 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800512 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800513 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800514 cmdStr = "onos:balance-masters"
515 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800516 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800517 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800518 except TypeError:
519 main.log.exception( self.name + ": Object not as expected" )
520 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800521 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800522 main.log.error( self.name + ": EOF exception found" )
523 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800524 main.cleanup()
525 main.exit()
526 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800527 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800528 main.cleanup()
529 main.exit()
530
kelvin-onlabd3b64892015-01-20 13:26:24 -0800531 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800532 """
Jon Halle8217482014-10-17 13:49:14 -0400533 Lists all core links
534 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800535 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800536 """
Jon Halle8217482014-10-17 13:49:14 -0400537 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800538 if jsonFormat:
539 cmdStr = "links -j"
540 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800541 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800542 handle variable here contains some ANSI escape color code
543 sequences at the end which are invisible in the print command
544 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800545 function. The repr( handle ) output when printed shows the ANSI
546 escape sequences. In json.loads( somestring ), this somestring
547 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800548 fail with the escape sequence. So we take off that escape
549 sequence using:
550
kelvin-onlabd3b64892015-01-20 13:26:24 -0800551 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
552 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800553 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800554 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
555 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400556 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400557 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800558 cmdStr = "links"
559 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400560 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800561 except TypeError:
562 main.log.exception( self.name + ": Object not as expected" )
563 return None
Jon Halle8217482014-10-17 13:49:14 -0400564 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800565 main.log.error( self.name + ": EOF exception found" )
566 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400567 main.cleanup()
568 main.exit()
569 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800570 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400571 main.cleanup()
572 main.exit()
573
kelvin-onlabd3b64892015-01-20 13:26:24 -0800574 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800575 """
Jon Halle8217482014-10-17 13:49:14 -0400576 Lists all ports
577 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800578 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800579 """
Jon Halle8217482014-10-17 13:49:14 -0400580 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800581 if jsonFormat:
582 cmdStr = "ports -j"
583 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800584 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800585 handle variable here contains some ANSI escape color code
586 sequences at the end which are invisible in the print command
587 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800588 function. The repr( handle ) output when printed shows the ANSI
589 escape sequences. In json.loads( somestring ), this somestring
590 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800591 fail with the escape sequence. So we take off that escape
592 sequence using the following commads:
593
kelvin-onlabd3b64892015-01-20 13:26:24 -0800594 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
595 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800596 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800597 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
598 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400599 return handle1
600
Jon Halle8217482014-10-17 13:49:14 -0400601 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800602 cmdStr = "ports"
603 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800604 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800605 except TypeError:
606 main.log.exception( self.name + ": Object not as expected" )
607 return None
Jon Halle8217482014-10-17 13:49:14 -0400608 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800609 main.log.error( self.name + ": EOF exception found" )
610 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400611 main.cleanup()
612 main.exit()
613 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800614 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400615 main.cleanup()
616 main.exit()
617
kelvin-onlabd3b64892015-01-20 13:26:24 -0800618 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800619 """
Jon Hall983a1702014-10-28 18:44:22 -0400620 Lists all devices and the controllers with roles assigned to them
621 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800622 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800623 """
andrewonlab7c211572014-10-15 16:45:20 -0400624 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800625 if jsonFormat:
626 cmdStr = "roles -j"
627 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800628 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800629 handle variable here contains some ANSI escape color code
630 sequences at the end which are invisible in the print command
631 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800632 function. The repr( handle ) output when printed shows the ANSI
633 escape sequences. In json.loads( somestring ), this somestring
634 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800635 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500636
Jon Halle3f39ff2015-01-13 11:50:53 -0800637 So we take off that escape sequence using the following
638 commads:
639
kelvin-onlabd3b64892015-01-20 13:26:24 -0800640 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
641 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800642 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800643 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
644 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400645 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400646
andrewonlab7c211572014-10-15 16:45:20 -0400647 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800648 cmdStr = "roles"
649 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800650 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800651 except TypeError:
652 main.log.exception( self.name + ": Object not as expected" )
653 return None
Jon Hall983a1702014-10-28 18:44:22 -0400654 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800655 main.log.error( self.name + ": EOF exception found" )
656 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400657 main.cleanup()
658 main.exit()
659 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800660 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400661 main.cleanup()
662 main.exit()
663
kelvin-onlabd3b64892015-01-20 13:26:24 -0800664 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800665 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800666 Given the a string containing the json representation of the "roles"
667 cli command and a partial or whole device id, returns a json object
668 containing the roles output for the first device whose id contains
669 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400670
671 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800672 A dict of the role assignments for the given device or
673 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800674 """
Jon Hall983a1702014-10-28 18:44:22 -0400675 try:
676 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800677 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400678 return None
679 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800680 rawRoles = self.roles()
681 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800682 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800683 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800684 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800685 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400686 return device
687 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800688 except TypeError:
689 main.log.exception( self.name + ": Object not as expected" )
690 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400691 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800692 main.log.error( self.name + ": EOF exception found" )
693 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400694 main.cleanup()
695 main.exit()
696 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800697 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400698 main.cleanup()
699 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800700
kelvin-onlabd3b64892015-01-20 13:26:24 -0800701 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800702 """
Jon Hall94fd0472014-12-08 11:52:42 -0800703 Iterates through each device and checks if there is a master assigned
704 Returns: main.TRUE if each device has a master
705 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800706 """
Jon Hall94fd0472014-12-08 11:52:42 -0800707 try:
708 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800709 rawRoles = self.roles()
710 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800711 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800712 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800713 # print device
714 if device[ 'master' ] == "none":
715 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800716 return main.FALSE
717 return main.TRUE
718
Jon Halld4d4b372015-01-28 16:02:41 -0800719 except TypeError:
720 main.log.exception( self.name + ": Object not as expected" )
721 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800722 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800723 main.log.error( self.name + ": EOF exception found" )
724 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800725 main.cleanup()
726 main.exit()
727 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800728 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800729 main.cleanup()
730 main.exit()
731
kelvin-onlabd3b64892015-01-20 13:26:24 -0800732 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800733 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400734 Returns string of paths, and the cost.
735 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800736 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400737 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800738 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
739 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800740 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800741 main.log.error( "Error in getting paths" )
742 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400743 else:
kelvin8ec71442015-01-15 16:57:00 -0800744 path = handle.split( ";" )[ 0 ]
745 cost = handle.split( ";" )[ 1 ]
746 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800747 except TypeError:
748 main.log.exception( self.name + ": Object not as expected" )
749 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400750 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800751 main.log.error( self.name + ": EOF exception found" )
752 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400753 main.cleanup()
754 main.exit()
755 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800756 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400757 main.cleanup()
758 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800759
kelvin-onlabd3b64892015-01-20 13:26:24 -0800760 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800761 """
Jon Hallffb386d2014-11-21 13:43:38 -0800762 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400763 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800764 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800765 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400766 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800767 if jsonFormat:
768 cmdStr = "hosts -j"
769 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800770 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800771 handle variable here contains some ANSI escape color code
772 sequences at the end which are invisible in the print command
773 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800774 function. The repr( handle ) output when printed shows the ANSI
775 escape sequences. In json.loads( somestring ), this somestring
776 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800777 fail with the escape sequence. So we take off that escape
778 sequence using:
779
kelvin-onlabd3b64892015-01-20 13:26:24 -0800780 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
781 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800782 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800783 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
784 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400785 return handle1
786 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800787 cmdStr = "hosts"
788 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400789 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800790 except TypeError:
791 main.log.exception( self.name + ": Object not as expected" )
792 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400793 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800794 main.log.error( self.name + ": EOF exception found" )
795 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400796 main.cleanup()
797 main.exit()
798 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800799 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400800 main.cleanup()
801 main.exit()
802
kelvin-onlabd3b64892015-01-20 13:26:24 -0800803 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800804 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400805 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800806
807 Note: mac must be a colon seperated mac address, but could be a
808 partial mac address
809
Jon Hall42db6dc2014-10-24 19:03:48 -0400810 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800811 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400812 import json
813 try:
kelvin8ec71442015-01-15 16:57:00 -0800814 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400815 return None
816 else:
817 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800818 rawHosts = self.hosts()
819 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800820 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800821 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800822 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800823 if not host:
824 pass
825 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400826 return host
827 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800828 except TypeError:
829 main.log.exception( self.name + ": Object not as expected" )
830 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400831 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800832 main.log.error( self.name + ": EOF exception found" )
833 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400834 main.cleanup()
835 main.exit()
836 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800837 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400838 main.cleanup()
839 main.exit()
840
kelvin-onlabd3b64892015-01-20 13:26:24 -0800841 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800842 """
843 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400844 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800845
andrewonlab3f0a4af2014-10-17 12:25:14 -0400846 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800847 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400848 IMPORTANT:
849 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800850 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400851 Furthermore, it assumes that value of VLAN is '-1'
852 Description:
kelvin8ec71442015-01-15 16:57:00 -0800853 Converts mininet hosts ( h1, h2, h3... ) into
854 ONOS format ( 00:00:00:00:00:01/-1 , ... )
855 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400856 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800857 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400858
kelvin-onlabd3b64892015-01-20 13:26:24 -0800859 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800860 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800861 hostHex = hex( int( host ) ).zfill( 12 )
862 hostHex = str( hostHex ).replace( 'x', '0' )
863 i = iter( str( hostHex ) )
864 hostHex = ":".join( a + b for a, b in zip( i, i ) )
865 hostHex = hostHex + "/-1"
866 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400867
kelvin-onlabd3b64892015-01-20 13:26:24 -0800868 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400869
Jon Halld4d4b372015-01-28 16:02:41 -0800870 except TypeError:
871 main.log.exception( self.name + ": Object not as expected" )
872 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400873 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800874 main.log.error( self.name + ": EOF exception found" )
875 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400876 main.cleanup()
877 main.exit()
878 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800879 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400880 main.cleanup()
881 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400882
kelvin-onlabd3b64892015-01-20 13:26:24 -0800883 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800884 """
andrewonlabe6745342014-10-17 14:29:13 -0400885 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800886 * hostIdOne: ONOS host id for host1
887 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400888 Description:
kelvin8ec71442015-01-15 16:57:00 -0800889 Adds a host-to-host intent ( bidrectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500890 specifying the two hosts.
kelvin8ec71442015-01-15 16:57:00 -0800891 """
andrewonlabe6745342014-10-17 14:29:13 -0400892 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800893 cmdStr = "add-host-intent " + str( hostIdOne ) +\
894 " " + str( hostIdTwo )
895 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800896 if re.search( "Error", handle ):
897 main.log.error( "Error in adding Host intent" )
898 return handle
899 else:
900 main.log.info( "Host intent installed between " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800901 str( hostIdOne ) + " and " + str( hostIdTwo ) )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800902 return main.TRUE
903
Jon Halld4d4b372015-01-28 16:02:41 -0800904 except TypeError:
905 main.log.exception( self.name + ": Object not as expected" )
906 return None
Hari Krishnaccfb0d52015-02-19 09:38:29 -0800907
andrewonlabe6745342014-10-17 14:29:13 -0400908 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800909 main.log.error( self.name + ": EOF exception found" )
910 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400911 main.cleanup()
912 main.exit()
913 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800914 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400915 main.cleanup()
916 main.exit()
917
kelvin-onlabd3b64892015-01-20 13:26:24 -0800918 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800919 """
andrewonlab7b31d232014-10-24 13:31:47 -0400920 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800921 * ingressDevice: device id of ingress device
922 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400923 Optional:
924 TODO: Still needs to be implemented via dev side
kelvin-onlab898a6c62015-01-16 14:13:53 -0800925 """
andrewonlab7b31d232014-10-24 13:31:47 -0400926 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800927 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
928 " " + str( egressDevice )
929 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800930 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800931 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400932 return handle
933 else:
934 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800935 except TypeError:
936 main.log.exception( self.name + ": Object not as expected" )
937 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400938 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800939 main.log.error( self.name + ": EOF exception found" )
940 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400941 main.cleanup()
942 main.exit()
943 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800944 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -0400945 main.cleanup()
946 main.exit()
947
kelvin-onlabd3b64892015-01-20 13:26:24 -0800948 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800949 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800950 ingressDevice,
951 egressDevice,
952 portIngress="",
953 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800954 ethType="",
955 ethSrc="",
956 ethDst="",
957 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800958 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800959 ipProto="",
960 ipSrc="",
961 ipDst="",
962 tcpSrc="",
963 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -0800964 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400965 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800966 * ingressDevice: device id of ingress device
967 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400968 Optional:
969 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -0800970 * ethSrc: specify ethSrc ( i.e. src mac addr )
971 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500972 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -0800973 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -0500974 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -0800975 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -0500976 * ipSrc: specify ip source address
977 * ipDst: specify ip destination address
978 * tcpSrc: specify tcp source port
979 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400980 Description:
kelvin8ec71442015-01-15 16:57:00 -0800981 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -0400982 specifying device id's and optional fields
983
Jon Halle3f39ff2015-01-13 11:50:53 -0800984 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -0400985 options developers provide for point-to-point
986 intent via cli
kelvin8ec71442015-01-15 16:57:00 -0800987 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400988 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400989 cmd = ""
990
kelvin8ec71442015-01-15 16:57:00 -0800991 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500992 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800993 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -0500994 and not ipProto and not ipSrc and not ipDst \
995 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -0500996 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -0500997
andrewonlab289e4b72014-10-21 21:24:18 -0400998 else:
andrewonlab36af3822014-11-18 17:48:18 -0500999 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001000
andrewonlab0c0a6772014-10-22 12:31:18 -04001001 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001002 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001003 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001004 cmd += " --ethSrc " + str( ethSrc )
1005 if ethDst:
1006 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001007 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001008 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001009 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001010 cmd += " --lambda "
1011 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001012 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001013 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001014 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001015 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001016 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001017 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001018 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001019 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001020 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001021
kelvin8ec71442015-01-15 16:57:00 -08001022 # Check whether the user appended the port
1023 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001024 if "/" in ingressDevice:
1025 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001026 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001027 if not portIngress:
kelvin8ec71442015-01-15 16:57:00 -08001028 main.log.error( "You must specify " +
1029 "the ingress port" )
1030 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -05001031 return main.FALSE
1032
kelvin8ec71442015-01-15 16:57:00 -08001033 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001034 str( ingressDevice ) + "/" +\
1035 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001036
kelvin-onlabd3b64892015-01-20 13:26:24 -08001037 if "/" in egressDevice:
1038 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001039 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001040 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001041 main.log.error( "You must specify " +
1042 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001043 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001044
kelvin8ec71442015-01-15 16:57:00 -08001045 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001046 str( egressDevice ) + "/" +\
1047 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001048
kelvin-onlab898a6c62015-01-16 14:13:53 -08001049 handle = self.sendline( cmd )
1050 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001051 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001052 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001053 else:
1054 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001055 except TypeError:
1056 main.log.exception( self.name + ": Object not as expected" )
1057 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001058 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001059 main.log.error( self.name + ": EOF exception found" )
1060 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001061 main.cleanup()
1062 main.exit()
1063 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001064 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001065 main.cleanup()
1066 main.exit()
1067
kelvin-onlabd3b64892015-01-20 13:26:24 -08001068 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001069 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001070 ingressDevice1,
1071 ingressDevice2,
1072 egressDevice,
1073 portIngress="",
1074 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001075 ethType="",
1076 ethSrc="",
1077 ethDst="",
1078 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001079 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001080 ipProto="",
1081 ipSrc="",
1082 ipDst="",
1083 tcpSrc="",
1084 tcpDst="",
1085 setEthSrc="",
1086 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001087 """
shahshreyad0c80432014-12-04 16:56:05 -08001088 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001089 This function assumes that there would be 2 ingress devices and
1090 one egress device. For more number of ingress devices, this
1091 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001092 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001093 * ingressDevice1: device id of ingress device1
1094 * ingressDevice2: device id of ingress device2
1095 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001096 Optional:
1097 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001098 * ethSrc: specify ethSrc ( i.e. src mac addr )
1099 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001100 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001101 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001102 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001103 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001104 * ipSrc: specify ip source address
1105 * ipDst: specify ip destination address
1106 * tcpSrc: specify tcp source port
1107 * tcpDst: specify tcp destination port
1108 * setEthSrc: action to Rewrite Source MAC Address
1109 * setEthDst: action to Rewrite Destination MAC Address
1110 Description:
kelvin8ec71442015-01-15 16:57:00 -08001111 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001112 specifying device id's and optional fields
1113
Jon Halle3f39ff2015-01-13 11:50:53 -08001114 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001115 options developers provide for multipointpoint-to-singlepoint
1116 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001117 """
shahshreyad0c80432014-12-04 16:56:05 -08001118 try:
1119 cmd = ""
1120
kelvin8ec71442015-01-15 16:57:00 -08001121 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001122 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001123 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001124 and not ipProto and not ipSrc and not ipDst\
1125 and not tcpSrc and not tcpDst and not setEthSrc\
1126 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001127 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001128
1129 else:
1130 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001131
shahshreyad0c80432014-12-04 16:56:05 -08001132 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001133 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001134 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001135 cmd += " --ethSrc " + str( ethSrc )
1136 if ethDst:
1137 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001138 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001139 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001140 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001141 cmd += " --lambda "
1142 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001143 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001144 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001145 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001146 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001147 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001148 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001149 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001150 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001151 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001152 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001153 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001154 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001155 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001156
kelvin8ec71442015-01-15 16:57:00 -08001157 # Check whether the user appended the port
1158 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001159 if "/" in ingressDevice1:
1160 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001161 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001162 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001163 main.log.error( "You must specify " +
1164 "the ingress port1" )
1165 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001166 return main.FALSE
1167
kelvin8ec71442015-01-15 16:57:00 -08001168 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001169 str( ingressDevice1 ) + "/" +\
1170 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001171
kelvin-onlabd3b64892015-01-20 13:26:24 -08001172 if "/" in ingressDevice2:
1173 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001174 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001175 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001176 main.log.error( "You must specify " +
1177 "the ingress port2" )
1178 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001179 return main.FALSE
1180
kelvin8ec71442015-01-15 16:57:00 -08001181 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001182 str( ingressDevice2 ) + "/" +\
1183 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001184
kelvin-onlabd3b64892015-01-20 13:26:24 -08001185 if "/" in egressDevice:
1186 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001187 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001188 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001189 main.log.error( "You must specify " +
1190 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001191 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001192
kelvin8ec71442015-01-15 16:57:00 -08001193 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001194 str( egressDevice ) + "/" +\
1195 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001196 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001197 handle = self.sendline( cmd )
1198 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001199 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001200 return self.handle
1201 else:
1202 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001203 except TypeError:
1204 main.log.exception( self.name + ": Object not as expected" )
1205 return None
shahshreyad0c80432014-12-04 16:56:05 -08001206 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001207 main.log.error( self.name + ": EOF exception found" )
1208 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001209 main.cleanup()
1210 main.exit()
1211 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001212 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001213 main.cleanup()
1214 main.exit()
1215
kelvin-onlabd3b64892015-01-20 13:26:24 -08001216 def removeIntent( self, intentId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001217 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001218 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001219
1220 Returns:
1221 main.False on error and
1222 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001223 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001224 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001225 cmdStr = "remove-intent " + str( intentId )
1226 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001227 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001228 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001229 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001230 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001231 # TODO: Should this be main.TRUE
1232 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001233 except TypeError:
1234 main.log.exception( self.name + ": Object not as expected" )
1235 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001236 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001237 main.log.error( self.name + ": EOF exception found" )
1238 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001239 main.cleanup()
1240 main.exit()
1241 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001242 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001243 main.cleanup()
1244 main.exit()
1245
kelvin-onlabd3b64892015-01-20 13:26:24 -08001246 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001247 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001248 NOTE: This method should be used after installing application:
1249 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001250 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001251 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001252 Description:
1253 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001254 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001255 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001256 if jsonFormat:
1257 cmdStr = "routes -j"
1258 handleTmp = self.sendline( cmdStr )
1259 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1260 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001261 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001262 cmdStr = "routes"
1263 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001264 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001265 except TypeError:
1266 main.log.exception( self.name + ": Object not as expected" )
1267 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001268 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001269 main.log.error( self.name + ": EOF exception found" )
1270 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001271 main.cleanup()
1272 main.exit()
1273 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001274 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001275 main.cleanup()
1276 main.exit()
1277
kelvin-onlabd3b64892015-01-20 13:26:24 -08001278 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001279 """
andrewonlab377693f2014-10-21 16:00:30 -04001280 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001281 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001282 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001283 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001284 """
andrewonlabe6745342014-10-17 14:29:13 -04001285 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001286 if jsonFormat:
1287 cmdStr = "intents -j"
1288 handle = self.sendline( cmdStr )
1289 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1290 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001291 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001292 cmdStr = "intents"
1293 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001294 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001295 except TypeError:
1296 main.log.exception( self.name + ": Object not as expected" )
1297 return None
andrewonlabe6745342014-10-17 14:29:13 -04001298 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001299 main.log.error( self.name + ": EOF exception found" )
1300 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001301 main.cleanup()
1302 main.exit()
1303 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001304 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001305 main.cleanup()
1306 main.exit()
1307
kelvin-onlabd3b64892015-01-20 13:26:24 -08001308 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001309 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001310 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001311 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001312 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001313 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001314 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001315 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001316 if jsonFormat:
1317 cmdStr = "flows -j"
1318 handle = self.sendline( cmdStr )
1319 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1320 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001321 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001322 cmdStr = "flows"
1323 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001324 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001325 main.log.error( self.name + ".flows() response: " +
1326 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001327 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001328 except TypeError:
1329 main.log.exception( self.name + ": Object not as expected" )
1330 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001331 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001332 main.log.error( self.name + ": EOF exception found" )
1333 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001334 main.cleanup()
1335 main.exit()
1336 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001337 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001338 main.cleanup()
1339 main.exit()
1340
kelvin-onlabd3b64892015-01-20 13:26:24 -08001341 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
1342 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001343 """
andrewonlab87852b02014-11-19 18:44:19 -05001344 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001345 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001346 a specific point-to-point intent definition
1347 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001348 * dpidSrc: specify source dpid
1349 * dpidDst: specify destination dpid
1350 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001351 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001352 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001353 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001354 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001355 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001356 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001357 """
andrewonlab87852b02014-11-19 18:44:19 -05001358 try:
kelvin8ec71442015-01-15 16:57:00 -08001359 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001360 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1361 str( numIntents )
1362 if numMult:
1363 cmd += " " + str( numMult )
1364 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001365 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001366 if appId:
1367 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001368 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001369 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1370 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001371 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001372 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001373 main.log.info( handle )
1374 # Split result by newline
1375 newline = handle.split( "\r\r\n" )
1376 # Ignore the first object of list, which is empty
1377 newline = newline[ 1: ]
1378 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001379 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001380 result = result.split( ": " )
1381 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001382 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1383 main.log.info( latResult )
1384 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001385 else:
1386 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001387 except TypeError:
1388 main.log.exception( self.name + ": Object not as expected" )
1389 return None
andrewonlab87852b02014-11-19 18:44:19 -05001390 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001391 main.log.error( self.name + ": EOF exception found" )
1392 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001393 main.cleanup()
1394 main.exit()
1395 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001396 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001397 main.cleanup()
1398 main.exit()
1399
kelvin-onlabd3b64892015-01-20 13:26:24 -08001400 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001401 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001402 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001403 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001404 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001405 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001406 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001407 if jsonFormat:
1408 cmdStr = "intents-events-metrics -j"
1409 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001410 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001411 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1412 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001413 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001414 cmdStr = "intents-events-metrics"
1415 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001416 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001417 except TypeError:
1418 main.log.exception( self.name + ": Object not as expected" )
1419 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001420 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001421 main.log.error( self.name + ": EOF exception found" )
1422 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001423 main.cleanup()
1424 main.exit()
1425 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001426 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001427 main.cleanup()
1428 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001429
kelvin-onlabd3b64892015-01-20 13:26:24 -08001430 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001431 """
1432 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001433 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001434 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001435 """
andrewonlab867212a2014-10-22 20:13:38 -04001436 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001437 if jsonFormat:
1438 cmdStr = "topology-events-metrics -j"
1439 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001440 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001441 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1442 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001443 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001444 cmdStr = "topology-events-metrics"
1445 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001446 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001447 except TypeError:
1448 main.log.exception( self.name + ": Object not as expected" )
1449 return None
andrewonlab867212a2014-10-22 20:13:38 -04001450 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001451 main.log.error( self.name + ": EOF exception found" )
1452 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001453 main.cleanup()
1454 main.exit()
1455 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001456 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001457 main.cleanup()
1458 main.exit()
1459
kelvin8ec71442015-01-15 16:57:00 -08001460 # Wrapper functions ****************
1461 # Wrapper functions use existing driver
1462 # functions and extends their use case.
1463 # For example, we may use the output of
1464 # a normal driver function, and parse it
1465 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001466
kelvin-onlabd3b64892015-01-20 13:26:24 -08001467 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001468 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001469 Description:
1470 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001471 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001472 try:
kelvin8ec71442015-01-15 16:57:00 -08001473 # Obtain output of intents function
kelvin-onlabd3b64892015-01-20 13:26:24 -08001474 intentsStr = self.intents()
1475 allIntentList = []
1476 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001477
kelvin8ec71442015-01-15 16:57:00 -08001478 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001479 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1480 for intents in intentsList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001481 if "onos>" in intents:
1482 continue
1483 elif "intents" in intents:
1484 continue
1485 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001486 lineList = intents.split( " " )
1487 allIntentList.append( lineList[ 0 ] )
kelvin8ec71442015-01-15 16:57:00 -08001488
kelvin-onlabd3b64892015-01-20 13:26:24 -08001489 allIntentList = allIntentList[ 1:-2 ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001490
kelvin-onlabd3b64892015-01-20 13:26:24 -08001491 for intents in allIntentList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001492 if not intents:
1493 continue
1494 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001495 intentIdList.append( intents )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001496
kelvin-onlabd3b64892015-01-20 13:26:24 -08001497 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001498
Jon Halld4d4b372015-01-28 16:02:41 -08001499 except TypeError:
1500 main.log.exception( self.name + ": Object not as expected" )
1501 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001502 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001503 main.log.error( self.name + ": EOF exception found" )
1504 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001505 main.cleanup()
1506 main.exit()
1507 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001508 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001509 main.cleanup()
1510 main.exit()
1511
kelvin-onlabd3b64892015-01-20 13:26:24 -08001512 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001513 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001514 Use 'devices' function to obtain list of all devices
1515 and parse the result to obtain a list of all device
1516 id's. Returns this list. Returns empty list if no
1517 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001518 List is ordered sequentially
1519
andrewonlab3e15ead2014-10-15 14:21:34 -04001520 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001521 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001522 the ids. By obtaining the list of device ids on the fly,
1523 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001524 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001525 try:
kelvin8ec71442015-01-15 16:57:00 -08001526 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001527 devicesStr = self.devices( jsonFormat=False )
1528 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001529
kelvin-onlabd3b64892015-01-20 13:26:24 -08001530 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001531 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001532 return idList
kelvin8ec71442015-01-15 16:57:00 -08001533
1534 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001535 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001536 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001537 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001538 # Split list further into arguments before and after string
1539 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001540 # append to idList
1541 for arg in tempList:
1542 idList.append( arg.split( "id=" )[ 1 ] )
1543 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001544
Jon Halld4d4b372015-01-28 16:02:41 -08001545 except TypeError:
1546 main.log.exception( self.name + ": Object not as expected" )
1547 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04001548 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001549 main.log.error( self.name + ": EOF exception found" )
1550 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001551 main.cleanup()
1552 main.exit()
1553 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001554 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001555 main.cleanup()
1556 main.exit()
1557
kelvin-onlabd3b64892015-01-20 13:26:24 -08001558 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001559 """
andrewonlab7c211572014-10-15 16:45:20 -04001560 Uses 'nodes' function to obtain list of all nodes
1561 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001562 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001563 Returns:
1564 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001565 """
andrewonlab7c211572014-10-15 16:45:20 -04001566 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001567 nodesStr = self.nodes()
1568 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001569
kelvin-onlabd3b64892015-01-20 13:26:24 -08001570 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001571 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001572 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001573
kelvin-onlabd3b64892015-01-20 13:26:24 -08001574 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001575 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001576
kelvin8ec71442015-01-15 16:57:00 -08001577 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001578 nodesList = nodesStr.split( "," )
1579 tempList = [ node for node in nodesList if "id=" in node ]
1580 for arg in tempList:
1581 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001582
kelvin-onlabd3b64892015-01-20 13:26:24 -08001583 return idList
kelvin8ec71442015-01-15 16:57:00 -08001584
Jon Halld4d4b372015-01-28 16:02:41 -08001585 except TypeError:
1586 main.log.exception( self.name + ": Object not as expected" )
1587 return None
andrewonlab7c211572014-10-15 16:45:20 -04001588 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001589 main.log.error( self.name + ": EOF exception found" )
1590 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001591 main.cleanup()
1592 main.exit()
1593 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001594 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001595 main.cleanup()
1596 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001597
kelvin-onlabd3b64892015-01-20 13:26:24 -08001598 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001599 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001600 Return the first device from the devices api whose 'id' contains 'dpid'
1601 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001602 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001603 import json
1604 try:
kelvin8ec71442015-01-15 16:57:00 -08001605 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001606 return None
1607 else:
kelvin8ec71442015-01-15 16:57:00 -08001608 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001609 rawDevices = self.devices()
1610 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001611 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001612 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001613 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1614 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001615 return device
1616 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001617 except TypeError:
1618 main.log.exception( self.name + ": Object not as expected" )
1619 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001620 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001621 main.log.error( self.name + ": EOF exception found" )
1622 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001623 main.cleanup()
1624 main.exit()
1625 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001626 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001627 main.cleanup()
1628 main.exit()
1629
kelvin-onlabd3b64892015-01-20 13:26:24 -08001630 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001631 """
1632 Checks the number of swithes & links that ONOS sees against the
1633 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001634 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001635
Jon Hall42db6dc2014-10-24 19:03:48 -04001636 Params: ip = ip used for the onos cli
1637 numoswitch = expected number of switches
1638 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001639 logLevel = level to log to. Currently accepts
1640 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001641
1642
kelvin-onlabd3b64892015-01-20 13:26:24 -08001643 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001644
kelvin8ec71442015-01-15 16:57:00 -08001645 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001646 main.FALSE if the numer of switches and links is incorrect,
1647 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001648 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001649 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001650 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001651 if topology == {}:
1652 return main.ERROR
1653 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001654 # Is the number of switches is what we expected
1655 devices = topology.get( 'devices', False )
1656 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001657 if devices == False or links == False:
1658 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001659 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001660 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001661 linkCheck = ( int( links ) == int( numolink ) )
1662 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001663 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001664 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001665 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001666 result = main.TRUE
1667 else:
1668 output = output + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001669 "The number of links and switches does not matc\
1670 h what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001671 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001672 output = output + "\n ONOS sees %i devices (%i expected) \
1673 and %i links (%i expected)" % (
1674 int( devices ), int( numoswitch ), int( links ),
1675 int( numolink ) )
1676 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001677 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001678 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001679 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001680 else:
kelvin8ec71442015-01-15 16:57:00 -08001681 main.log.info( output )
1682 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001683 except TypeError:
1684 main.log.exception( self.name + ": Object not as expected" )
1685 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001686 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001687 main.log.error( self.name + ": EOF exception found" )
1688 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001689 main.cleanup()
1690 main.exit()
1691 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001692 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001693 main.cleanup()
1694 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001695
kelvin-onlabd3b64892015-01-20 13:26:24 -08001696 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001697 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001698 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001699 deviceId must be the id of a device as seen in the onos devices command
1700 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001701 role must be either master, standby, or none
1702
Jon Halle3f39ff2015-01-13 11:50:53 -08001703 Returns:
1704 main.TRUE or main.FALSE based on argument verification and
1705 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001706 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001707 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001708 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001709 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001710 cmdStr = "device-role " +\
1711 str( deviceId ) + " " +\
1712 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001713 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001714 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001715 if re.search( "Error", handle ):
1716 # end color output to escape any colours
1717 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001718 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001719 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001720 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001721 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001722 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001723 main.log.error( "Invalid 'role' given to device_role(). " +
1724 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001725 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001726 except TypeError:
1727 main.log.exception( self.name + ": Object not as expected" )
1728 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001729 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001730 main.log.error( self.name + ": EOF exception found" )
1731 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001732 main.cleanup()
1733 main.exit()
1734 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001735 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001736 main.cleanup()
1737 main.exit()
1738
kelvin-onlabd3b64892015-01-20 13:26:24 -08001739 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001740 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001741 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001742 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001743 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001744 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001745 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001746 if jsonFormat:
1747 cmdStr = "clusters -j"
1748 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001749 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001750 handle variable here contains some ANSI escape color code
1751 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001752 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001753 function. The repr( handle ) output when printed shows the ANSI
1754 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001755 variable is actually repr( somestring ) and json.loads would
1756 fail with the escape sequence. So we take off that escape
1757 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001758
kelvin-onlabd3b64892015-01-20 13:26:24 -08001759 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1760 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001761 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001762 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1763 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001764 return handle1
1765 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001766 cmdStr = "clusters"
1767 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001768 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001769 except TypeError:
1770 main.log.exception( self.name + ": Object not as expected" )
1771 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001772 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001773 main.log.error( self.name + ": EOF exception found" )
1774 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001775 main.cleanup()
1776 main.exit()
1777 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001778 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001779 main.cleanup()
1780 main.exit()
1781
kelvin-onlabd3b64892015-01-20 13:26:24 -08001782 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001783 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001784 CLI command to get the current leader for the Election test application
1785 NOTE: Requires installation of the onos-app-election feature
1786 Returns: Node IP of the leader if one exists
1787 None if none exists
1788 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001789 """
Jon Hall94fd0472014-12-08 11:52:42 -08001790 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001791 cmdStr = "election-test-leader"
1792 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001793 # Leader
1794 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001795 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001796 nodeSearch = re.search( leaderPattern, response )
1797 if nodeSearch:
1798 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001799 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001800 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001801 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001802 # no leader
1803 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001804 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001805 nullSearch = re.search( nullPattern, response )
1806 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001807 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001808 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001809 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001810 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001811 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001812 if re.search( errorPattern, response ):
1813 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001814 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001815 return main.FALSE
1816 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001817 main.log.error( "Error in election_test_leader: " +
1818 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001819 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001820 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001821 except TypeError:
1822 main.log.exception( self.name + ": Object not as expected" )
1823 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001824 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001825 main.log.error( self.name + ": EOF exception found" )
1826 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001827 main.cleanup()
1828 main.exit()
1829 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001830 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001831 main.cleanup()
1832 main.exit()
1833
kelvin-onlabd3b64892015-01-20 13:26:24 -08001834 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001835 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001836 CLI command to run for leadership of the Election test application.
1837 NOTE: Requires installation of the onos-app-election feature
1838 Returns: Main.TRUE on success
1839 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001840 """
Jon Hall94fd0472014-12-08 11:52:42 -08001841 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001842 cmdStr = "election-test-run"
1843 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001844 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001845 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001846 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001847 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001848 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001849 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001850 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001851 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001852 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001853 errorPattern = "Command\snot\sfound"
1854 if re.search( errorPattern, response ):
1855 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001856 return main.FALSE
1857 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001858 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001859 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001860 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001861 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001862 except TypeError:
1863 main.log.exception( self.name + ": Object not as expected" )
1864 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001865 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001866 main.log.error( self.name + ": EOF exception found" )
1867 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001868 main.cleanup()
1869 main.exit()
1870 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001871 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001872 main.cleanup()
1873 main.exit()
1874
kelvin-onlabd3b64892015-01-20 13:26:24 -08001875 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08001876 """
Jon Hall94fd0472014-12-08 11:52:42 -08001877 * CLI command to withdraw the local node from leadership election for
1878 * the Election test application.
1879 #NOTE: Requires installation of the onos-app-election feature
1880 Returns: Main.TRUE on success
1881 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001882 """
Jon Hall94fd0472014-12-08 11:52:42 -08001883 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001884 cmdStr = "election-test-withdraw"
1885 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001886 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001887 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001888 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001889 if re.search( successPattern, response ):
1890 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001891 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001892 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001893 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001894 errorPattern = "Command\snot\sfound"
1895 if re.search( errorPattern, response ):
1896 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001897 return main.FALSE
1898 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001899 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001900 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001901 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001902 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001903 except TypeError:
1904 main.log.exception( self.name + ": Object not as expected" )
1905 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001906 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001907 main.log.error( self.name + ": EOF exception found" )
1908 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001909 main.cleanup()
1910 main.exit()
1911 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001912 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001913 main.cleanup()
1914 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001915
kelvin8ec71442015-01-15 16:57:00 -08001916 def getDevicePortsEnabledCount( self, dpid ):
1917 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001918 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001919 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001920 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001921 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001922 cmdStr = "onos:ports -e " + dpid + " | wc -l"
1923 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001924 if re.search( "No such device", output ):
1925 main.log.error( "Error in getting ports" )
1926 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001927 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001928 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001929 except TypeError:
1930 main.log.exception( self.name + ": Object not as expected" )
1931 return ( output, "Error" )
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:
Jon Halld4d4b372015-01-28 16:02:41 -08001938 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001939 main.cleanup()
1940 main.exit()
1941
kelvin8ec71442015-01-15 16:57:00 -08001942 def getDeviceLinksActiveCount( self, dpid ):
1943 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001944 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001945 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001946 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001947 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001948 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
1949 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001950 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001951 main.log.error( "Error in getting ports " )
1952 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001953 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001954 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001955 except TypeError:
1956 main.log.exception( self.name + ": Object not as expected" )
1957 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001958 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001959 main.log.error( self.name + ": EOF exception found" )
1960 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001961 main.cleanup()
1962 main.exit()
1963 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001964 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001965 main.cleanup()
1966 main.exit()
1967
kelvin8ec71442015-01-15 16:57:00 -08001968 def getAllIntentIds( self ):
1969 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001970 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08001971 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001972 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001973 cmdStr = "onos:intents | grep id="
1974 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001975 if re.search( "Error", output ):
1976 main.log.error( "Error in getting ports" )
1977 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001978 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001979 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001980 except TypeError:
1981 main.log.exception( self.name + ": Object not as expected" )
1982 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001983 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001984 main.log.error( self.name + ": EOF exception found" )
1985 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001986 main.cleanup()
1987 main.exit()
1988 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001989 main.log.exception( self.name + ": Uncaught exception!" )
1990 main.cleanup()
1991 main.exit()
1992
1993 def testExceptions( self, obj ):
1994 """
1995 Test exception logging
1996 """
1997 # FIXME: Remove this before you commit
1998
1999 try:
2000 return obj[ 'dedf' ]
2001 except TypeError:
2002 main.log.exception( self.name + ": Object not as expected" )
2003 return None
2004 except pexpect.EOF:
2005 main.log.error( self.name + ": EOF exception found" )
2006 main.log.error( self.name + ": " + self.handle.before )
2007 main.cleanup()
2008 main.exit()
2009 except:
2010 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002011 main.cleanup()
2012 main.exit()