blob: cb9549b1d70b17b0b546a497e8d325f929c9db34 [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
Hari Krishna33a64672015-02-17 16:56:17 -0800904<<<<<<< HEAD
Jon Halld4d4b372015-01-28 16:02:41 -0800905 except TypeError:
906 main.log.exception( self.name + ": Object not as expected" )
907 return None
Hari Krishna33a64672015-02-17 16:56:17 -0800908=======
909>>>>>>> 1c7509b87a65d457eaf41f5551e560f1aa2ea0df
andrewonlabe6745342014-10-17 14:29:13 -0400910 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800911 main.log.error( self.name + ": EOF exception found" )
912 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400913 main.cleanup()
914 main.exit()
915 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800916 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400917 main.cleanup()
918 main.exit()
919
kelvin-onlabd3b64892015-01-20 13:26:24 -0800920 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800921 """
andrewonlab7b31d232014-10-24 13:31:47 -0400922 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800923 * ingressDevice: device id of ingress device
924 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400925 Optional:
926 TODO: Still needs to be implemented via dev side
kelvin-onlab898a6c62015-01-16 14:13:53 -0800927 """
andrewonlab7b31d232014-10-24 13:31:47 -0400928 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800929 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
930 " " + str( egressDevice )
931 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800932 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800933 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400934 return handle
935 else:
936 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800937 except TypeError:
938 main.log.exception( self.name + ": Object not as expected" )
939 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400940 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800941 main.log.error( self.name + ": EOF exception found" )
942 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400943 main.cleanup()
944 main.exit()
945 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800946 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -0400947 main.cleanup()
948 main.exit()
949
kelvin-onlabd3b64892015-01-20 13:26:24 -0800950 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800951 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800952 ingressDevice,
953 egressDevice,
954 portIngress="",
955 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800956 ethType="",
957 ethSrc="",
958 ethDst="",
959 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800960 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800961 ipProto="",
962 ipSrc="",
963 ipDst="",
964 tcpSrc="",
965 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -0800966 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400967 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800968 * ingressDevice: device id of ingress device
969 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400970 Optional:
971 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -0800972 * ethSrc: specify ethSrc ( i.e. src mac addr )
973 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500974 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -0800975 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -0500976 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -0800977 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -0500978 * ipSrc: specify ip source address
979 * ipDst: specify ip destination address
980 * tcpSrc: specify tcp source port
981 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400982 Description:
kelvin8ec71442015-01-15 16:57:00 -0800983 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -0400984 specifying device id's and optional fields
985
Jon Halle3f39ff2015-01-13 11:50:53 -0800986 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -0400987 options developers provide for point-to-point
988 intent via cli
kelvin8ec71442015-01-15 16:57:00 -0800989 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400990 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400991 cmd = ""
992
kelvin8ec71442015-01-15 16:57:00 -0800993 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500994 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800995 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -0500996 and not ipProto and not ipSrc and not ipDst \
997 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -0500998 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -0500999
andrewonlab289e4b72014-10-21 21:24:18 -04001000 else:
andrewonlab36af3822014-11-18 17:48:18 -05001001 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001002
andrewonlab0c0a6772014-10-22 12:31:18 -04001003 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001004 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001005 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001006 cmd += " --ethSrc " + str( ethSrc )
1007 if ethDst:
1008 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001009 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001010 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001011 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001012 cmd += " --lambda "
1013 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001014 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001015 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001016 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001017 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001018 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001019 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001020 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001021 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001022 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001023
kelvin8ec71442015-01-15 16:57:00 -08001024 # Check whether the user appended the port
1025 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001026 if "/" in ingressDevice:
1027 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001028 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001029 if not portIngress:
kelvin8ec71442015-01-15 16:57:00 -08001030 main.log.error( "You must specify " +
1031 "the ingress port" )
1032 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -05001033 return main.FALSE
1034
kelvin8ec71442015-01-15 16:57:00 -08001035 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001036 str( ingressDevice ) + "/" +\
1037 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001038
kelvin-onlabd3b64892015-01-20 13:26:24 -08001039 if "/" in egressDevice:
1040 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001041 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001042 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001043 main.log.error( "You must specify " +
1044 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001045 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001046
kelvin8ec71442015-01-15 16:57:00 -08001047 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001048 str( egressDevice ) + "/" +\
1049 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001050
kelvin-onlab898a6c62015-01-16 14:13:53 -08001051 handle = self.sendline( cmd )
1052 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001053 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001054 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001055 else:
1056 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001057 except TypeError:
1058 main.log.exception( self.name + ": Object not as expected" )
1059 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001060 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001061 main.log.error( self.name + ": EOF exception found" )
1062 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001063 main.cleanup()
1064 main.exit()
1065 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001066 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001067 main.cleanup()
1068 main.exit()
1069
kelvin-onlabd3b64892015-01-20 13:26:24 -08001070 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001071 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001072 ingressDevice1,
1073 ingressDevice2,
1074 egressDevice,
1075 portIngress="",
1076 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001077 ethType="",
1078 ethSrc="",
1079 ethDst="",
1080 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001081 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001082 ipProto="",
1083 ipSrc="",
1084 ipDst="",
1085 tcpSrc="",
1086 tcpDst="",
1087 setEthSrc="",
1088 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001089 """
shahshreyad0c80432014-12-04 16:56:05 -08001090 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001091 This function assumes that there would be 2 ingress devices and
1092 one egress device. For more number of ingress devices, this
1093 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001094 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001095 * ingressDevice1: device id of ingress device1
1096 * ingressDevice2: device id of ingress device2
1097 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001098 Optional:
1099 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001100 * ethSrc: specify ethSrc ( i.e. src mac addr )
1101 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001102 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001103 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001104 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001105 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001106 * ipSrc: specify ip source address
1107 * ipDst: specify ip destination address
1108 * tcpSrc: specify tcp source port
1109 * tcpDst: specify tcp destination port
1110 * setEthSrc: action to Rewrite Source MAC Address
1111 * setEthDst: action to Rewrite Destination MAC Address
1112 Description:
kelvin8ec71442015-01-15 16:57:00 -08001113 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001114 specifying device id's and optional fields
1115
Jon Halle3f39ff2015-01-13 11:50:53 -08001116 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001117 options developers provide for multipointpoint-to-singlepoint
1118 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001119 """
shahshreyad0c80432014-12-04 16:56:05 -08001120 try:
1121 cmd = ""
1122
kelvin8ec71442015-01-15 16:57:00 -08001123 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001124 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001125 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001126 and not ipProto and not ipSrc and not ipDst\
1127 and not tcpSrc and not tcpDst and not setEthSrc\
1128 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001129 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001130
1131 else:
1132 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001133
shahshreyad0c80432014-12-04 16:56:05 -08001134 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001135 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001136 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001137 cmd += " --ethSrc " + str( ethSrc )
1138 if ethDst:
1139 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001140 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001141 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001142 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001143 cmd += " --lambda "
1144 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001145 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001146 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001147 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001148 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001149 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001150 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001151 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001152 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001153 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001154 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001155 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001156 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001157 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001158
kelvin8ec71442015-01-15 16:57:00 -08001159 # Check whether the user appended the port
1160 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001161 if "/" in ingressDevice1:
1162 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001163 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001164 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001165 main.log.error( "You must specify " +
1166 "the ingress port1" )
1167 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001168 return main.FALSE
1169
kelvin8ec71442015-01-15 16:57:00 -08001170 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001171 str( ingressDevice1 ) + "/" +\
1172 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001173
kelvin-onlabd3b64892015-01-20 13:26:24 -08001174 if "/" in ingressDevice2:
1175 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001176 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001177 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001178 main.log.error( "You must specify " +
1179 "the ingress port2" )
1180 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001181 return main.FALSE
1182
kelvin8ec71442015-01-15 16:57:00 -08001183 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001184 str( ingressDevice2 ) + "/" +\
1185 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001186
kelvin-onlabd3b64892015-01-20 13:26:24 -08001187 if "/" in egressDevice:
1188 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001189 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001190 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001191 main.log.error( "You must specify " +
1192 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001193 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001194
kelvin8ec71442015-01-15 16:57:00 -08001195 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001196 str( egressDevice ) + "/" +\
1197 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001198 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001199 handle = self.sendline( cmd )
1200 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001201 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001202 return self.handle
1203 else:
1204 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001205 except TypeError:
1206 main.log.exception( self.name + ": Object not as expected" )
1207 return None
shahshreyad0c80432014-12-04 16:56:05 -08001208 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001209 main.log.error( self.name + ": EOF exception found" )
1210 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001211 main.cleanup()
1212 main.exit()
1213 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001214 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001215 main.cleanup()
1216 main.exit()
1217
kelvin-onlabd3b64892015-01-20 13:26:24 -08001218 def removeIntent( self, intentId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001219 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001220 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001221
1222 Returns:
1223 main.False on error and
1224 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001225 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001226 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001227 cmdStr = "remove-intent " + str( intentId )
1228 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001229 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001230 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001231 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001232 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001233 # TODO: Should this be main.TRUE
1234 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001235 except TypeError:
1236 main.log.exception( self.name + ": Object not as expected" )
1237 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001238 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001239 main.log.error( self.name + ": EOF exception found" )
1240 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001241 main.cleanup()
1242 main.exit()
1243 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001244 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001245 main.cleanup()
1246 main.exit()
1247
kelvin-onlabd3b64892015-01-20 13:26:24 -08001248 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001249 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001250 NOTE: This method should be used after installing application:
1251 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001252 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001253 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001254 Description:
1255 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001256 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001257 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001258 if jsonFormat:
1259 cmdStr = "routes -j"
1260 handleTmp = self.sendline( cmdStr )
1261 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1262 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001263 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001264 cmdStr = "routes"
1265 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001266 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001267 except TypeError:
1268 main.log.exception( self.name + ": Object not as expected" )
1269 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001270 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001271 main.log.error( self.name + ": EOF exception found" )
1272 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001273 main.cleanup()
1274 main.exit()
1275 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001276 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001277 main.cleanup()
1278 main.exit()
1279
kelvin-onlabd3b64892015-01-20 13:26:24 -08001280 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001281 """
andrewonlab377693f2014-10-21 16:00:30 -04001282 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001283 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001284 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001285 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001286 """
andrewonlabe6745342014-10-17 14:29:13 -04001287 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001288 if jsonFormat:
1289 cmdStr = "intents -j"
1290 handle = self.sendline( cmdStr )
1291 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1292 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001293 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001294 cmdStr = "intents"
1295 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001296 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001297 except TypeError:
1298 main.log.exception( self.name + ": Object not as expected" )
1299 return None
andrewonlabe6745342014-10-17 14:29:13 -04001300 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001301 main.log.error( self.name + ": EOF exception found" )
1302 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001303 main.cleanup()
1304 main.exit()
1305 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001306 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001307 main.cleanup()
1308 main.exit()
1309
kelvin-onlabd3b64892015-01-20 13:26:24 -08001310 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001311 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001312 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001313 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001314 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001315 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001316 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001317 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001318 if jsonFormat:
1319 cmdStr = "flows -j"
1320 handle = self.sendline( cmdStr )
1321 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1322 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001323 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001324 cmdStr = "flows"
1325 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001326 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001327 main.log.error( self.name + ".flows() response: " +
1328 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001329 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001330 except TypeError:
1331 main.log.exception( self.name + ": Object not as expected" )
1332 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001333 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001334 main.log.error( self.name + ": EOF exception found" )
1335 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001336 main.cleanup()
1337 main.exit()
1338 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001339 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001340 main.cleanup()
1341 main.exit()
1342
kelvin-onlabd3b64892015-01-20 13:26:24 -08001343 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
1344 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001345 """
andrewonlab87852b02014-11-19 18:44:19 -05001346 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001347 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001348 a specific point-to-point intent definition
1349 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001350 * dpidSrc: specify source dpid
1351 * dpidDst: specify destination dpid
1352 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001353 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001354 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001355 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001356 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001357 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001358 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001359 """
andrewonlab87852b02014-11-19 18:44:19 -05001360 try:
kelvin8ec71442015-01-15 16:57:00 -08001361 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001362 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1363 str( numIntents )
1364 if numMult:
1365 cmd += " " + str( numMult )
1366 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001367 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001368 if appId:
1369 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001370 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001371 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1372 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001373 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001374 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001375 main.log.info( handle )
1376 # Split result by newline
1377 newline = handle.split( "\r\r\n" )
1378 # Ignore the first object of list, which is empty
1379 newline = newline[ 1: ]
1380 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001381 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001382 result = result.split( ": " )
1383 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001384 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1385 main.log.info( latResult )
1386 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001387 else:
1388 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001389 except TypeError:
1390 main.log.exception( self.name + ": Object not as expected" )
1391 return None
andrewonlab87852b02014-11-19 18:44:19 -05001392 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001393 main.log.error( self.name + ": EOF exception found" )
1394 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001395 main.cleanup()
1396 main.exit()
1397 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001398 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001399 main.cleanup()
1400 main.exit()
1401
kelvin-onlabd3b64892015-01-20 13:26:24 -08001402 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001403 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001404 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001405 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001406 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001407 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001408 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001409 if jsonFormat:
1410 cmdStr = "intents-events-metrics -j"
1411 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001412 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001413 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1414 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001415 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001416 cmdStr = "intents-events-metrics"
1417 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001418 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001419 except TypeError:
1420 main.log.exception( self.name + ": Object not as expected" )
1421 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001422 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001423 main.log.error( self.name + ": EOF exception found" )
1424 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001425 main.cleanup()
1426 main.exit()
1427 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001428 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001429 main.cleanup()
1430 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001431
kelvin-onlabd3b64892015-01-20 13:26:24 -08001432 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001433 """
1434 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001435 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001436 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001437 """
andrewonlab867212a2014-10-22 20:13:38 -04001438 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001439 if jsonFormat:
1440 cmdStr = "topology-events-metrics -j"
1441 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001442 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001443 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1444 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001445 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001446 cmdStr = "topology-events-metrics"
1447 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001448 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001449 except TypeError:
1450 main.log.exception( self.name + ": Object not as expected" )
1451 return None
andrewonlab867212a2014-10-22 20:13:38 -04001452 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001453 main.log.error( self.name + ": EOF exception found" )
1454 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001455 main.cleanup()
1456 main.exit()
1457 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001458 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001459 main.cleanup()
1460 main.exit()
1461
kelvin8ec71442015-01-15 16:57:00 -08001462 # Wrapper functions ****************
1463 # Wrapper functions use existing driver
1464 # functions and extends their use case.
1465 # For example, we may use the output of
1466 # a normal driver function, and parse it
1467 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001468
kelvin-onlabd3b64892015-01-20 13:26:24 -08001469 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001470 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001471 Description:
1472 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001473 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001474 try:
kelvin8ec71442015-01-15 16:57:00 -08001475 # Obtain output of intents function
kelvin-onlabd3b64892015-01-20 13:26:24 -08001476 intentsStr = self.intents()
1477 allIntentList = []
1478 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001479
kelvin8ec71442015-01-15 16:57:00 -08001480 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001481 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1482 for intents in intentsList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001483 if "onos>" in intents:
1484 continue
1485 elif "intents" in intents:
1486 continue
1487 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001488 lineList = intents.split( " " )
1489 allIntentList.append( lineList[ 0 ] )
kelvin8ec71442015-01-15 16:57:00 -08001490
kelvin-onlabd3b64892015-01-20 13:26:24 -08001491 allIntentList = allIntentList[ 1:-2 ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001492
kelvin-onlabd3b64892015-01-20 13:26:24 -08001493 for intents in allIntentList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001494 if not intents:
1495 continue
1496 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001497 intentIdList.append( intents )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001498
kelvin-onlabd3b64892015-01-20 13:26:24 -08001499 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001500
Jon Halld4d4b372015-01-28 16:02:41 -08001501 except TypeError:
1502 main.log.exception( self.name + ": Object not as expected" )
1503 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001504 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001505 main.log.error( self.name + ": EOF exception found" )
1506 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001507 main.cleanup()
1508 main.exit()
1509 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001510 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001511 main.cleanup()
1512 main.exit()
1513
kelvin-onlabd3b64892015-01-20 13:26:24 -08001514 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001515 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001516 Use 'devices' function to obtain list of all devices
1517 and parse the result to obtain a list of all device
1518 id's. Returns this list. Returns empty list if no
1519 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001520 List is ordered sequentially
1521
andrewonlab3e15ead2014-10-15 14:21:34 -04001522 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001523 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001524 the ids. By obtaining the list of device ids on the fly,
1525 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001526 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001527 try:
kelvin8ec71442015-01-15 16:57:00 -08001528 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001529 devicesStr = self.devices( jsonFormat=False )
1530 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001531
kelvin-onlabd3b64892015-01-20 13:26:24 -08001532 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001533 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001534 return idList
kelvin8ec71442015-01-15 16:57:00 -08001535
1536 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001537 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001538 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001539 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001540 # Split list further into arguments before and after string
1541 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001542 # append to idList
1543 for arg in tempList:
1544 idList.append( arg.split( "id=" )[ 1 ] )
1545 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001546
Jon Halld4d4b372015-01-28 16:02:41 -08001547 except TypeError:
1548 main.log.exception( self.name + ": Object not as expected" )
1549 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04001550 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001551 main.log.error( self.name + ": EOF exception found" )
1552 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001553 main.cleanup()
1554 main.exit()
1555 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001556 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001557 main.cleanup()
1558 main.exit()
1559
kelvin-onlabd3b64892015-01-20 13:26:24 -08001560 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001561 """
andrewonlab7c211572014-10-15 16:45:20 -04001562 Uses 'nodes' function to obtain list of all nodes
1563 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001564 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001565 Returns:
1566 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001567 """
andrewonlab7c211572014-10-15 16:45:20 -04001568 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001569 nodesStr = self.nodes()
1570 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001571
kelvin-onlabd3b64892015-01-20 13:26:24 -08001572 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001573 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001574 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001575
kelvin-onlabd3b64892015-01-20 13:26:24 -08001576 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001577 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001578
kelvin8ec71442015-01-15 16:57:00 -08001579 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001580 nodesList = nodesStr.split( "," )
1581 tempList = [ node for node in nodesList if "id=" in node ]
1582 for arg in tempList:
1583 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001584
kelvin-onlabd3b64892015-01-20 13:26:24 -08001585 return idList
kelvin8ec71442015-01-15 16:57:00 -08001586
Jon Halld4d4b372015-01-28 16:02:41 -08001587 except TypeError:
1588 main.log.exception( self.name + ": Object not as expected" )
1589 return None
andrewonlab7c211572014-10-15 16:45:20 -04001590 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001591 main.log.error( self.name + ": EOF exception found" )
1592 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001593 main.cleanup()
1594 main.exit()
1595 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001596 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001597 main.cleanup()
1598 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001599
kelvin-onlabd3b64892015-01-20 13:26:24 -08001600 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001601 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001602 Return the first device from the devices api whose 'id' contains 'dpid'
1603 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001604 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001605 import json
1606 try:
kelvin8ec71442015-01-15 16:57:00 -08001607 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001608 return None
1609 else:
kelvin8ec71442015-01-15 16:57:00 -08001610 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001611 rawDevices = self.devices()
1612 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001613 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001614 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001615 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1616 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001617 return device
1618 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001619 except TypeError:
1620 main.log.exception( self.name + ": Object not as expected" )
1621 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001622 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001623 main.log.error( self.name + ": EOF exception found" )
1624 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001625 main.cleanup()
1626 main.exit()
1627 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001628 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001629 main.cleanup()
1630 main.exit()
1631
kelvin-onlabd3b64892015-01-20 13:26:24 -08001632 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001633 """
1634 Checks the number of swithes & links that ONOS sees against the
1635 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001636 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001637
Jon Hall42db6dc2014-10-24 19:03:48 -04001638 Params: ip = ip used for the onos cli
1639 numoswitch = expected number of switches
1640 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001641 logLevel = level to log to. Currently accepts
1642 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001643
1644
kelvin-onlabd3b64892015-01-20 13:26:24 -08001645 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001646
kelvin8ec71442015-01-15 16:57:00 -08001647 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001648 main.FALSE if the numer of switches and links is incorrect,
1649 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001650 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001651 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001652 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001653 if topology == {}:
1654 return main.ERROR
1655 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001656 # Is the number of switches is what we expected
1657 devices = topology.get( 'devices', False )
1658 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001659 if devices == False or links == False:
1660 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001661 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001662 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001663 linkCheck = ( int( links ) == int( numolink ) )
1664 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001665 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001666 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001667 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001668 result = main.TRUE
1669 else:
1670 output = output + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001671 "The number of links and switches does not matc\
1672 h what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001673 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001674 output = output + "\n ONOS sees %i devices (%i expected) \
1675 and %i links (%i expected)" % (
1676 int( devices ), int( numoswitch ), int( links ),
1677 int( numolink ) )
1678 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001679 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001680 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001681 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001682 else:
kelvin8ec71442015-01-15 16:57:00 -08001683 main.log.info( output )
1684 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001685 except TypeError:
1686 main.log.exception( self.name + ": Object not as expected" )
1687 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001688 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001689 main.log.error( self.name + ": EOF exception found" )
1690 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001691 main.cleanup()
1692 main.exit()
1693 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001694 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001695 main.cleanup()
1696 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001697
kelvin-onlabd3b64892015-01-20 13:26:24 -08001698 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001699 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001700 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001701 deviceId must be the id of a device as seen in the onos devices command
1702 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001703 role must be either master, standby, or none
1704
Jon Halle3f39ff2015-01-13 11:50:53 -08001705 Returns:
1706 main.TRUE or main.FALSE based on argument verification and
1707 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001708 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001709 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001710 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001711 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001712 cmdStr = "device-role " +\
1713 str( deviceId ) + " " +\
1714 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001715 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001716 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001717 if re.search( "Error", handle ):
1718 # end color output to escape any colours
1719 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001720 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001721 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001722 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001723 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001724 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001725 main.log.error( "Invalid 'role' given to device_role(). " +
1726 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001727 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001728 except TypeError:
1729 main.log.exception( self.name + ": Object not as expected" )
1730 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001731 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001732 main.log.error( self.name + ": EOF exception found" )
1733 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001734 main.cleanup()
1735 main.exit()
1736 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001737 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001738 main.cleanup()
1739 main.exit()
1740
kelvin-onlabd3b64892015-01-20 13:26:24 -08001741 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001742 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001743 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001744 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001745 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001746 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001747 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001748 if jsonFormat:
1749 cmdStr = "clusters -j"
1750 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001751 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001752 handle variable here contains some ANSI escape color code
1753 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001754 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001755 function. The repr( handle ) output when printed shows the ANSI
1756 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001757 variable is actually repr( somestring ) and json.loads would
1758 fail with the escape sequence. So we take off that escape
1759 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001760
kelvin-onlabd3b64892015-01-20 13:26:24 -08001761 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1762 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001763 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001764 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1765 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001766 return handle1
1767 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001768 cmdStr = "clusters"
1769 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001770 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001771 except TypeError:
1772 main.log.exception( self.name + ": Object not as expected" )
1773 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001774 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001775 main.log.error( self.name + ": EOF exception found" )
1776 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001777 main.cleanup()
1778 main.exit()
1779 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001780 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001781 main.cleanup()
1782 main.exit()
1783
kelvin-onlabd3b64892015-01-20 13:26:24 -08001784 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001785 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001786 CLI command to get the current leader for the Election test application
1787 NOTE: Requires installation of the onos-app-election feature
1788 Returns: Node IP of the leader if one exists
1789 None if none exists
1790 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001791 """
Jon Hall94fd0472014-12-08 11:52:42 -08001792 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001793 cmdStr = "election-test-leader"
1794 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001795 # Leader
1796 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001797 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001798 nodeSearch = re.search( leaderPattern, response )
1799 if nodeSearch:
1800 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001801 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001802 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001803 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001804 # no leader
1805 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001806 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001807 nullSearch = re.search( nullPattern, response )
1808 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001809 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001810 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001811 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001812 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001813 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001814 if re.search( errorPattern, response ):
1815 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001816 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001817 return main.FALSE
1818 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001819 main.log.error( "Error in election_test_leader: " +
1820 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001821 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001822 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001823 except TypeError:
1824 main.log.exception( self.name + ": Object not as expected" )
1825 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001826 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001827 main.log.error( self.name + ": EOF exception found" )
1828 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001829 main.cleanup()
1830 main.exit()
1831 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001832 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001833 main.cleanup()
1834 main.exit()
1835
kelvin-onlabd3b64892015-01-20 13:26:24 -08001836 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001837 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001838 CLI command to run for leadership of the Election test application.
1839 NOTE: Requires installation of the onos-app-election feature
1840 Returns: Main.TRUE on success
1841 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001842 """
Jon Hall94fd0472014-12-08 11:52:42 -08001843 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001844 cmdStr = "election-test-run"
1845 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001846 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001847 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001848 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001849 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001850 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001851 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001852 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001853 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001854 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001855 errorPattern = "Command\snot\sfound"
1856 if re.search( errorPattern, response ):
1857 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001858 return main.FALSE
1859 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001860 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001861 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001862 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001863 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001864 except TypeError:
1865 main.log.exception( self.name + ": Object not as expected" )
1866 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001867 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001868 main.log.error( self.name + ": EOF exception found" )
1869 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001870 main.cleanup()
1871 main.exit()
1872 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001873 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001874 main.cleanup()
1875 main.exit()
1876
kelvin-onlabd3b64892015-01-20 13:26:24 -08001877 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08001878 """
Jon Hall94fd0472014-12-08 11:52:42 -08001879 * CLI command to withdraw the local node from leadership election for
1880 * the Election test application.
1881 #NOTE: Requires installation of the onos-app-election feature
1882 Returns: Main.TRUE on success
1883 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001884 """
Jon Hall94fd0472014-12-08 11:52:42 -08001885 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001886 cmdStr = "election-test-withdraw"
1887 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001888 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001889 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001890 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001891 if re.search( successPattern, response ):
1892 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001893 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001894 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001895 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001896 errorPattern = "Command\snot\sfound"
1897 if re.search( errorPattern, response ):
1898 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001899 return main.FALSE
1900 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001901 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001902 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001903 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001904 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001905 except TypeError:
1906 main.log.exception( self.name + ": Object not as expected" )
1907 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001908 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001909 main.log.error( self.name + ": EOF exception found" )
1910 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001911 main.cleanup()
1912 main.exit()
1913 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001914 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001915 main.cleanup()
1916 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001917
kelvin8ec71442015-01-15 16:57:00 -08001918 def getDevicePortsEnabledCount( self, dpid ):
1919 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001920 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001921 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001922 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001923 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001924 cmdStr = "onos:ports -e " + dpid + " | wc -l"
1925 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001926 if re.search( "No such device", output ):
1927 main.log.error( "Error in getting ports" )
1928 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001929 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001930 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001931 except TypeError:
1932 main.log.exception( self.name + ": Object not as expected" )
1933 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001934 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001935 main.log.error( self.name + ": EOF exception found" )
1936 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001937 main.cleanup()
1938 main.exit()
1939 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001940 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001941 main.cleanup()
1942 main.exit()
1943
kelvin8ec71442015-01-15 16:57:00 -08001944 def getDeviceLinksActiveCount( self, dpid ):
1945 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001946 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001947 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001948 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001949 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001950 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
1951 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001952 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001953 main.log.error( "Error in getting ports " )
1954 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001955 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001956 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001957 except TypeError:
1958 main.log.exception( self.name + ": Object not as expected" )
1959 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001960 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001961 main.log.error( self.name + ": EOF exception found" )
1962 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001963 main.cleanup()
1964 main.exit()
1965 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001966 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001967 main.cleanup()
1968 main.exit()
1969
kelvin8ec71442015-01-15 16:57:00 -08001970 def getAllIntentIds( self ):
1971 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001972 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08001973 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001974 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001975 cmdStr = "onos:intents | grep id="
1976 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001977 if re.search( "Error", output ):
1978 main.log.error( "Error in getting ports" )
1979 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001980 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001981 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001982 except TypeError:
1983 main.log.exception( self.name + ": Object not as expected" )
1984 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001985 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001986 main.log.error( self.name + ": EOF exception found" )
1987 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001988 main.cleanup()
1989 main.exit()
1990 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001991 main.log.exception( self.name + ": Uncaught exception!" )
1992 main.cleanup()
1993 main.exit()
1994
1995 def testExceptions( self, obj ):
1996 """
1997 Test exception logging
1998 """
1999 # FIXME: Remove this before you commit
2000
2001 try:
2002 return obj[ 'dedf' ]
2003 except TypeError:
2004 main.log.exception( self.name + ": Object not as expected" )
2005 return None
2006 except pexpect.EOF:
2007 main.log.error( self.name + ": EOF exception found" )
2008 main.log.error( self.name + ": " + self.handle.before )
2009 main.cleanup()
2010 main.exit()
2011 except:
2012 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002013 main.cleanup()
2014 main.exit()