blob: f30289f272f63bf423f6f246eb74cda9c5e05f1a [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
kelvin8ec71442015-01-15 16:57:00 -08003"""
andrewonlab95ce8322014-10-13 14:12:04 -04004This driver enters the onos> prompt to issue commands.
5
kelvin8ec71442015-01-15 16:57:00 -08006Please follow the coding style demonstrated by existing
andrewonlab95ce8322014-10-13 14:12:04 -04007functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12jhall@onlab.us
13andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040014shreya@onlab.us
andrewonlab95ce8322014-10-13 14:12:04 -040015
16OCT 13 2014
17
kelvin8ec71442015-01-15 16:57:00 -080018"""
andrewonlab95ce8322014-10-13 14:12:04 -040019import sys
andrewonlab95ce8322014-10-13 14:12:04 -040020import pexpect
21import re
22import traceback
kelvin8ec71442015-01-15 16:57:00 -080023sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040024from drivers.common.clidriver import CLI
25
andrewonlab95ce8322014-10-13 14:12:04 -040026
kelvin8ec71442015-01-15 16:57:00 -080027class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040028
kelvin8ec71442015-01-15 16:57:00 -080029 def __init__( self ):
30 """
31 Initialize client
32 """
33 super( CLI, self ).__init__()
34
35 def connect( self, **connectargs ):
36 """
andrewonlab95ce8322014-10-13 14:12:04 -040037 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080038 """
andrewonlab95ce8322014-10-13 14:12:04 -040039 try:
40 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080041 vars( self )[ key ] = connectargs[ key ]
andrewonlab95ce8322014-10-13 14:12:04 -040042 self.home = "~/ONOS"
43 for key in self.options:
44 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080045 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040046 break
47
kelvin8ec71442015-01-15 16:57:00 -080048 self.name = self.options[ 'name' ]
49 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080050 user_name=self.user_name,
51 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080052 port=self.port,
53 pwd=self.pwd,
54 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040055
kelvin8ec71442015-01-15 16:57:00 -080056 self.handle.sendline( "cd " + self.home )
57 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040058 if self.handle:
59 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080060 else:
61 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040062 return main.FALSE
63 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080064 main.log.error( self.name + ": EOF exception found" )
65 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040066 main.cleanup()
67 main.exit()
68 except:
kelvin8ec71442015-01-15 16:57:00 -080069 main.log.info( self.name + ":::::::::::::::::::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -040070 main.log.error( traceback.print_exc() )
kelvin8ec71442015-01-15 16:57:00 -080071 main.log.info( ":::::::::::::::::::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -040072 main.cleanup()
73 main.exit()
74
kelvin8ec71442015-01-15 16:57:00 -080075 def disconnect( self ):
76 """
andrewonlab95ce8322014-10-13 14:12:04 -040077 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080078 """
andrewonlab95ce8322014-10-13 14:12:04 -040079 response = ''
80 try:
kelvin8ec71442015-01-15 16:57:00 -080081 self.handle.sendline( "" )
82 i = self.handle.expect( [ "onos>", "\$" ] )
Jon Hall7e5b9172014-10-22 12:32:47 -040083 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -080084 self.handle.sendline( "system:shutdown" )
85 self.handle.expect( "Confirm" )
86 self.handle.sendline( "yes" )
87 self.handle.expect( "\$" )
88 self.handle.sendline( "" )
89 self.handle.expect( "\$" )
90 self.handle.sendline( "exit" )
91 self.handle.expect( "closed" )
andrewonlabc2d05aa2014-10-13 16:51:10 -040092
andrewonlab95ce8322014-10-13 14:12:04 -040093 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080094 main.log.error( self.name + ": EOF exception found" )
95 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040096 except:
kelvin8ec71442015-01-15 16:57:00 -080097 main.log.error( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -040098 response = main.FALSE
99 return response
100
kelvin8ec71442015-01-15 16:57:00 -0800101 def logout( self ):
102 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500103 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800104 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500105 try:
kelvin8ec71442015-01-15 16:57:00 -0800106 self.handle.sendline( "" )
107 i = self.handle.expect( [
andrewonlab9627f432014-11-14 12:45:10 -0500108 "onos>",
kelvin8ec71442015-01-15 16:57:00 -0800109 "\$" ], timeout=10 )
andrewonlab9627f432014-11-14 12:45:10 -0500110 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800111 self.handle.sendline( "logout" )
112 self.handle.expect( "\$" )
andrewonlab9627f432014-11-14 12:45:10 -0500113 elif i == 1:
114 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800115
andrewonlab38d2b4a2014-11-13 16:28:47 -0500116 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800117 main.log.error( self.name + ": eof exception found" )
118 main.log.error( self.name + ": " +
119 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500120 main.cleanup()
121 main.exit()
122 except:
kelvin8ec71442015-01-15 16:57:00 -0800123 main.log.info( self.name + " ::::::" )
124 main.log.error( traceback.print_exc() )
125 main.log.info( self.name + " ::::::" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500126 main.cleanup()
127 main.exit()
128
kelvin-onlabd3b64892015-01-20 13:26:24 -0800129 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800130 """
andrewonlab95ce8322014-10-13 14:12:04 -0400131 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800132
andrewonlab95ce8322014-10-13 14:12:04 -0400133 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800134 """
andrewonlab95ce8322014-10-13 14:12:04 -0400135 try:
136 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800137 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400138 main.cleanup()
139 main.exit()
140 else:
kelvin8ec71442015-01-15 16:57:00 -0800141 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800142 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800143 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400144 # and that this driver will have to change accordingly
kelvin8ec71442015-01-15 16:57:00 -0800145 self.handle.expect( "ONOS_CELL=" + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800146 handleBefore = self.handle.before
147 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800148 # Get the rest of the handle
149 self.handle.sendline( "" )
150 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800151 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400152
kelvin-onlabd3b64892015-01-20 13:26:24 -0800153 main.log.info( "Cell call returned: " + handleBefore +
154 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400155
156 return main.TRUE
157
158 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800159 main.log.error( self.name + ": eof exception found" )
160 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400161 main.cleanup()
162 main.exit()
163 except:
kelvin8ec71442015-01-15 16:57:00 -0800164 main.log.info( self.name + " ::::::" )
165 main.log.error( traceback.print_exc() )
166 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400167 main.cleanup()
168 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800169
kelvin-onlabd3b64892015-01-20 13:26:24 -0800170 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800171 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800172 karafTimeout is an optional arugument. karafTimeout value passed
173 by user would be used to set the current karaf shell idle timeout.
174 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800175 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800176 Below is an example to start a session with 60 seconds idle timeout
177 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800178
Hari Krishna25d42f72015-01-05 15:08:28 -0800179 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800180 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800181
kelvin-onlabd3b64892015-01-20 13:26:24 -0800182 Note: karafTimeout is left as str so that this could be read
183 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800184 """
andrewonlab95ce8322014-10-13 14:12:04 -0400185 try:
kelvin8ec71442015-01-15 16:57:00 -0800186 self.handle.sendline( "" )
187 x = self.handle.expect( [
188 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500189
190 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800191 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500192 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400193
kelvin8ec71442015-01-15 16:57:00 -0800194 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800195 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800196 i = self.handle.expect( [
197 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800198 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400199
200 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800201 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800202 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800203 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800204 "config:property-set -p org.apache.karaf.shel\
205 l sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800206 karafTimeout )
207 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800208 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800209 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400210 return main.TRUE
211 else:
kelvin8ec71442015-01-15 16:57:00 -0800212 # If failed, send ctrl+c to process and try again
213 main.log.info( "Starting CLI failed. Retrying..." )
214 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800216 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
217 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400218 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800219 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800220 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800221 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800222 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800223 "config:property-set -p org.apache.karaf.shell\
224 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800225 karafTimeout )
226 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800227 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800228 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400229 return main.TRUE
230 else:
kelvin8ec71442015-01-15 16:57:00 -0800231 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800232 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400233 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400234
235 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800236 main.log.error( self.name + ": EOF exception found" )
237 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400238 main.cleanup()
239 main.exit()
240 except:
kelvin8ec71442015-01-15 16:57:00 -0800241 main.log.info( self.name + " ::::::" )
242 main.log.error( traceback.print_exc() )
243 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400244 main.cleanup()
245 main.exit()
246
kelvin-onlabd3b64892015-01-20 13:26:24 -0800247 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800248 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800249 Send a completely user specified string to
250 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400251 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800252
andrewonlaba18f6bf2014-10-13 19:31:54 -0400253 Warning: There are no sanity checking to commands
254 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800255 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400256 try:
kelvin8ec71442015-01-15 16:57:00 -0800257 self.handle.sendline( "" )
258 self.handle.expect( "onos>" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400259
kelvin-onlab898a6c62015-01-16 14:13:53 -0800260 self.handle.sendline( "log:log \"Sending CLI command: '"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800261 + cmdStr + "'\"" )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800262 self.handle.expect( "onos>" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800263 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800264 self.handle.expect( "onos>" )
Jon Hallaea67aa2015-01-23 13:30:57 -0800265 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
266 + self.name + "." )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400267
268 handle = self.handle.before
Jon Hall7bdfc122015-01-23 11:45:32 -0800269 # Remove control strings from output
270 ansiEscape = re.compile( r'\x1b[^m]*m' )
271 handle = ansiEscape.sub( '', handle )
Jon Hall44225f82015-01-23 13:45:14 -0800272 #Remove extra return chars that get added
Jon Hallaea67aa2015-01-23 13:30:57 -0800273 handle = re.sub( r"\s\r", "", handle )
274 handle = handle.strip()
Jon Hall7bdfc122015-01-23 11:45:32 -0800275 # parse for just the output, remove the cmd from handle
276 output = handle.split( cmdStr, 1 )[1]
kelvin8ec71442015-01-15 16:57:00 -0800277
andrewonlaba18f6bf2014-10-13 19:31:54 -0400278
Jon Hall7bdfc122015-01-23 11:45:32 -0800279 return output
andrewonlaba18f6bf2014-10-13 19:31:54 -0400280 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800281 main.log.error( self.name + ": EOF exception found" )
282 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400283 main.cleanup()
284 main.exit()
285 except:
kelvin8ec71442015-01-15 16:57:00 -0800286 main.log.info( self.name + " ::::::" )
287 main.log.error( traceback.print_exc() )
288 main.log.info( self.name + " ::::::" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400289 main.cleanup()
290 main.exit()
291
kelvin8ec71442015-01-15 16:57:00 -0800292 # IMPORTANT NOTE:
293 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800294 # the cli command changing 'a:b' with 'aB'.
295 # Ex ) onos:topology > onosTopology
296 # onos:links > onosLinks
297 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800298
kelvin-onlabd3b64892015-01-20 13:26:24 -0800299 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800300 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400301 Adds a new cluster node by ID and address information.
302 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800303 * nodeId
304 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400305 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800306 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800307 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400308 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800309 cmdStr = "add-node " + str( nodeId ) + " " +\
310 str( ONOSIp ) + " " + str( tcpPort )
311 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800312 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800313 main.log.error( "Error in adding node" )
314 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800315 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400316 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800317 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400318 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400319 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800320 main.log.error( self.name + ": EOF exception found" )
321 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400322 main.cleanup()
323 main.exit()
324 except:
kelvin8ec71442015-01-15 16:57:00 -0800325 main.log.info( self.name + " ::::::" )
326 main.log.error( traceback.print_exc() )
327 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400328 main.cleanup()
329 main.exit()
330
kelvin-onlabd3b64892015-01-20 13:26:24 -0800331 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800332 """
andrewonlab86dc3082014-10-13 18:18:38 -0400333 Removes a cluster by ID
334 Issues command: 'remove-node [<node-id>]'
335 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800336 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800337 """
andrewonlab86dc3082014-10-13 18:18:38 -0400338 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400339
kelvin-onlabd3b64892015-01-20 13:26:24 -0800340 cmdStr = "remove-node " + str( nodeId )
341 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800342 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400343
344 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800345
andrewonlab86dc3082014-10-13 18:18:38 -0400346 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800347 main.log.error( self.name + ": EOF exception found" )
348 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400349 main.cleanup()
350 main.exit()
351 except:
kelvin8ec71442015-01-15 16:57:00 -0800352 main.log.info( self.name + " ::::::" )
353 main.log.error( traceback.print_exc() )
354 main.log.info( self.name + " ::::::" )
andrewonlab86dc3082014-10-13 18:18:38 -0400355 main.cleanup()
356 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400357
kelvin8ec71442015-01-15 16:57:00 -0800358 def nodes( self ):
359 """
andrewonlab7c211572014-10-15 16:45:20 -0400360 List the nodes currently visible
361 Issues command: 'nodes'
362 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800363 """
andrewonlab7c211572014-10-15 16:45:20 -0400364 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800365 cmdStr = "nodes"
366 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400367 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400368 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800369 main.log.error( self.name + ": EOF exception found" )
370 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400371 main.cleanup()
372 main.exit()
373 except:
kelvin8ec71442015-01-15 16:57:00 -0800374 main.log.info( self.name + " ::::::" )
375 main.log.error( traceback.print_exc() )
376 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -0400377 main.cleanup()
378 main.exit()
379
kelvin8ec71442015-01-15 16:57:00 -0800380 def topology( self ):
381 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400382 Shows the current state of the topology
383 by issusing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800384 """
andrewonlab95ce8322014-10-13 14:12:04 -0400385 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800386 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800387 cmdStr = "onos:topology"
388 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800389 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400390 return handle
andrewonlab95ce8322014-10-13 14:12:04 -0400391 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800392 main.log.error( self.name + ": EOF exception found" )
393 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400394 main.cleanup()
395 main.exit()
396 except:
kelvin8ec71442015-01-15 16:57:00 -0800397 main.log.info( self.name + " ::::::" )
398 main.log.error( traceback.print_exc() )
399 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400400 main.cleanup()
401 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800402
kelvin-onlabd3b64892015-01-20 13:26:24 -0800403 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800404 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800405 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400406 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800407 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400408 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800409 cmdStr = "feature:install " + str( featureStr )
410 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800411 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400412 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400413 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800414 main.log.error( self.name + ": EOF exception found" )
415 main.log.error( self.name + ": " + self.handle.before )
416 main.log.report( "Failed to install feature" )
417 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400418 main.cleanup()
419 main.exit()
420 except:
kelvin8ec71442015-01-15 16:57:00 -0800421 main.log.info( self.name + " ::::::" )
422 main.log.error( traceback.print_exc() )
423 main.log.report( "Failed to install feature" )
424 main.log.report( "Exiting test" )
425 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400426 main.cleanup()
427 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800428
kelvin-onlabd3b64892015-01-20 13:26:24 -0800429 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800430 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400431 Uninstalls a specified feature
432 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800433 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400434 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800435 cmdStr = "feature:uninstall " + str( featureStr )
436 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800437 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400438 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400439 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800440 main.log.error( self.name + ": EOF exception found" )
441 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400442 main.cleanup()
443 main.exit()
444 except:
kelvin8ec71442015-01-15 16:57:00 -0800445 main.log.info( self.name + " ::::::" )
446 main.log.error( traceback.print_exc() )
447 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400448 main.cleanup()
449 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800450
kelvin-onlabd3b64892015-01-20 13:26:24 -0800451 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800452 """
Jon Hall7b02d952014-10-17 20:14:54 -0400453 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400454 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800455 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800456 """
andrewonlab86dc3082014-10-13 18:18:38 -0400457 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800458 if jsonFormat:
459 cmdStr = "devices -j"
460 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800461 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800462 handle variable here contains some ANSI escape color code
463 sequences at the end which are invisible in the print command
464 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800465 function. The repr( handle ) output when printed shows the
466 ANSI escape sequences. In json.loads( somestring ), this
467 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800468 json.loads would fail with the escape sequence. So we take off
469 that escape sequence using:
470
kelvin-onlabd3b64892015-01-20 13:26:24 -0800471 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
472 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800473 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800474 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
475 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400476 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400477 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800478 cmdStr = "devices"
479 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400480 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400481 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800482 main.log.error( self.name + ": EOF exception found" )
483 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400484 main.cleanup()
485 main.exit()
486 except:
kelvin8ec71442015-01-15 16:57:00 -0800487 main.log.info( self.name + " ::::::" )
488 main.log.error( traceback.print_exc() )
489 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -0400490 main.cleanup()
491 main.exit()
492
kelvin-onlabd3b64892015-01-20 13:26:24 -0800493 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800494 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800495 This balances the devices across all controllers
496 by issuing command: 'onos> onos:balance-masters'
497 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800498 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800499 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800500 cmdStr = "onos:balance-masters"
501 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800502 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800503 return main.TRUE
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800504 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800505 main.log.error( self.name + ": EOF exception found" )
506 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800507 main.cleanup()
508 main.exit()
509 except:
kelvin8ec71442015-01-15 16:57:00 -0800510 main.log.info( self.name + " ::::::" )
511 main.log.error( traceback.print_exc() )
512 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800513 main.cleanup()
514 main.exit()
515
kelvin-onlabd3b64892015-01-20 13:26:24 -0800516 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800517 """
Jon Halle8217482014-10-17 13:49:14 -0400518 Lists all core links
519 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800520 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800521 """
Jon Halle8217482014-10-17 13:49:14 -0400522 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800523 if jsonFormat:
524 cmdStr = "links -j"
525 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800526 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800527 handle variable here contains some ANSI escape color code
528 sequences at the end which are invisible in the print command
529 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800530 function. The repr( handle ) output when printed shows the ANSI
531 escape sequences. In json.loads( somestring ), this somestring
532 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800533 fail with the escape sequence. So we take off that escape
534 sequence using:
535
kelvin-onlabd3b64892015-01-20 13:26:24 -0800536 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
537 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800538 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800539 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
540 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400541 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400542 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800543 cmdStr = "links"
544 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400545 return handle
Jon Halle8217482014-10-17 13:49:14 -0400546 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800547 main.log.error( self.name + ": EOF exception found" )
548 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400549 main.cleanup()
550 main.exit()
551 except:
kelvin8ec71442015-01-15 16:57:00 -0800552 main.log.info( self.name + " ::::::" )
553 main.log.error( traceback.print_exc() )
554 main.log.info( self.name + " ::::::" )
Jon Halle8217482014-10-17 13:49:14 -0400555 main.cleanup()
556 main.exit()
557
kelvin-onlabd3b64892015-01-20 13:26:24 -0800558 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800559 """
Jon Halle8217482014-10-17 13:49:14 -0400560 Lists all ports
561 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800562 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800563 """
Jon Halle8217482014-10-17 13:49:14 -0400564 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800565 if jsonFormat:
566 cmdStr = "ports -j"
567 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800568 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800569 handle variable here contains some ANSI escape color code
570 sequences at the end which are invisible in the print command
571 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800572 function. The repr( handle ) output when printed shows the ANSI
573 escape sequences. In json.loads( somestring ), this somestring
574 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800575 fail with the escape sequence. So we take off that escape
576 sequence using the following commads:
577
kelvin-onlabd3b64892015-01-20 13:26:24 -0800578 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
579 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800580 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800581 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
582 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400583 return handle1
584
Jon Halle8217482014-10-17 13:49:14 -0400585 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800586 cmdStr = "ports"
587 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800588 return handle
Jon Halle8217482014-10-17 13:49:14 -0400589 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800590 main.log.error( self.name + ": EOF exception found" )
591 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400592 main.cleanup()
593 main.exit()
594 except:
kelvin8ec71442015-01-15 16:57:00 -0800595 main.log.info( self.name + " ::::::" )
596 main.log.error( traceback.print_exc() )
597 main.log.info( self.name + " ::::::" )
Jon Halle8217482014-10-17 13:49:14 -0400598 main.cleanup()
599 main.exit()
600
kelvin-onlabd3b64892015-01-20 13:26:24 -0800601 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800602 """
Jon Hall983a1702014-10-28 18:44:22 -0400603 Lists all devices and the controllers with roles assigned to them
604 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800605 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800606 """
andrewonlab7c211572014-10-15 16:45:20 -0400607 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800608 if jsonFormat:
609 cmdStr = "roles -j"
610 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800611 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800612 handle variable here contains some ANSI escape color code
613 sequences at the end which are invisible in the print command
614 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800615 function. The repr( handle ) output when printed shows the ANSI
616 escape sequences. In json.loads( somestring ), this somestring
617 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800618 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500619
Jon Halle3f39ff2015-01-13 11:50:53 -0800620 So we take off that escape sequence using the following
621 commads:
622
kelvin-onlabd3b64892015-01-20 13:26:24 -0800623 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
624 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800625 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800626 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
627 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400628 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400629
andrewonlab7c211572014-10-15 16:45:20 -0400630 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800631 cmdStr = "roles"
632 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800633 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400634 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800635 main.log.error( self.name + ": EOF exception found" )
636 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400637 main.cleanup()
638 main.exit()
639 except:
kelvin8ec71442015-01-15 16:57:00 -0800640 main.log.info( self.name + " ::::::" )
641 main.log.error( traceback.print_exc() )
642 main.log.info( self.name + " ::::::" )
Jon Hall983a1702014-10-28 18:44:22 -0400643 main.cleanup()
644 main.exit()
645
kelvin-onlabd3b64892015-01-20 13:26:24 -0800646 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800647 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800648 Given the a string containing the json representation of the "roles"
649 cli command and a partial or whole device id, returns a json object
650 containing the roles output for the first device whose id contains
651 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400652
653 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800654 A dict of the role assignments for the given device or
655 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800656 """
Jon Hall983a1702014-10-28 18:44:22 -0400657 try:
658 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800659 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400660 return None
661 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800662 rawRoles = self.roles()
663 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800664 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800665 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800666 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800667 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400668 return device
669 return None
andrewonlab7c211572014-10-15 16:45:20 -0400670
andrewonlab86dc3082014-10-13 18:18:38 -0400671 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800672 main.log.error( self.name + ": EOF exception found" )
673 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400674 main.cleanup()
675 main.exit()
676 except:
kelvin8ec71442015-01-15 16:57:00 -0800677 main.log.info( self.name + " ::::::" )
678 main.log.error( traceback.print_exc() )
679 main.log.info( self.name + " ::::::" )
andrewonlab86dc3082014-10-13 18:18:38 -0400680 main.cleanup()
681 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800682
kelvin-onlabd3b64892015-01-20 13:26:24 -0800683 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800684 """
Jon Hall94fd0472014-12-08 11:52:42 -0800685 Iterates through each device and checks if there is a master assigned
686 Returns: main.TRUE if each device has a master
687 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800688 """
Jon Hall94fd0472014-12-08 11:52:42 -0800689 try:
690 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800691 rawRoles = self.roles()
692 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800693 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800694 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800695 # print device
696 if device[ 'master' ] == "none":
697 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800698 return main.FALSE
699 return main.TRUE
700
701 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800702 main.log.error( self.name + ": EOF exception found" )
703 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800704 main.cleanup()
705 main.exit()
706 except:
kelvin8ec71442015-01-15 16:57:00 -0800707 main.log.info( self.name + " ::::::" )
708 main.log.error( traceback.print_exc() )
709 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -0800710 main.cleanup()
711 main.exit()
712
kelvin-onlabd3b64892015-01-20 13:26:24 -0800713 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800714 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400715 Returns string of paths, and the cost.
716 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800717 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400718 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800719 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
720 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800721 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800722 main.log.error( "Error in getting paths" )
723 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400724 else:
kelvin8ec71442015-01-15 16:57:00 -0800725 path = handle.split( ";" )[ 0 ]
726 cost = handle.split( ";" )[ 1 ]
727 return ( path, cost )
andrewonlab3e15ead2014-10-15 14:21:34 -0400728 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800729 main.log.error( self.name + ": EOF exception found" )
730 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400731 main.cleanup()
732 main.exit()
733 except:
kelvin8ec71442015-01-15 16:57:00 -0800734 main.log.info( self.name + " ::::::" )
735 main.log.error( traceback.print_exc() )
736 main.log.info( self.name + " ::::::" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400737 main.cleanup()
738 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800739
kelvin-onlabd3b64892015-01-20 13:26:24 -0800740 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800741 """
Jon Hallffb386d2014-11-21 13:43:38 -0800742 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400743 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800744 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800745 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400746 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800747 if jsonFormat:
748 cmdStr = "hosts -j"
749 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800750 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800751 handle variable here contains some ANSI escape color code
752 sequences at the end which are invisible in the print command
753 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800754 function. The repr( handle ) output when printed shows the ANSI
755 escape sequences. In json.loads( somestring ), this somestring
756 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800757 fail with the escape sequence. So we take off that escape
758 sequence using:
759
kelvin-onlabd3b64892015-01-20 13:26:24 -0800760 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
761 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800762 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800763 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
764 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400765 return handle1
766 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800767 cmdStr = "hosts"
768 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400769 return handle
770 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800771 main.log.error( self.name + ": EOF exception found" )
772 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400773 main.cleanup()
774 main.exit()
775 except:
kelvin8ec71442015-01-15 16:57:00 -0800776 main.log.info( self.name + " ::::::" )
777 main.log.error( traceback.print_exc() )
778 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400779 main.cleanup()
780 main.exit()
781
kelvin-onlabd3b64892015-01-20 13:26:24 -0800782 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800783 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400784 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800785
786 Note: mac must be a colon seperated mac address, but could be a
787 partial mac address
788
Jon Hall42db6dc2014-10-24 19:03:48 -0400789 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800790 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400791 import json
792 try:
kelvin8ec71442015-01-15 16:57:00 -0800793 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400794 return None
795 else:
796 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800797 rawHosts = self.hosts()
798 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800799 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800800 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800801 # print "%s in %s?" % ( mac, host[ 'id' ] )
802 if mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400803 return host
804 return None
805 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800806 main.log.error( self.name + ": EOF exception found" )
807 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400808 main.cleanup()
809 main.exit()
810 except:
kelvin8ec71442015-01-15 16:57:00 -0800811 main.log.info( self.name + " ::::::" )
812 main.log.error( traceback.print_exc() )
813 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400814 main.cleanup()
815 main.exit()
816
kelvin-onlabd3b64892015-01-20 13:26:24 -0800817 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800818 """
819 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400820 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800821
andrewonlab3f0a4af2014-10-17 12:25:14 -0400822 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800823 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400824 IMPORTANT:
825 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800826 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400827 Furthermore, it assumes that value of VLAN is '-1'
828 Description:
kelvin8ec71442015-01-15 16:57:00 -0800829 Converts mininet hosts ( h1, h2, h3... ) into
830 ONOS format ( 00:00:00:00:00:01/-1 , ... )
831 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400832 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800833 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400834
kelvin-onlabd3b64892015-01-20 13:26:24 -0800835 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800836 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800837 hostHex = hex( int( host ) ).zfill( 12 )
838 hostHex = str( hostHex ).replace( 'x', '0' )
839 i = iter( str( hostHex ) )
840 hostHex = ":".join( a + b for a, b in zip( i, i ) )
841 hostHex = hostHex + "/-1"
842 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400843
kelvin-onlabd3b64892015-01-20 13:26:24 -0800844 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400845
846 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800847 main.log.error( self.name + ": EOF exception found" )
848 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400849 main.cleanup()
850 main.exit()
851 except:
kelvin8ec71442015-01-15 16:57:00 -0800852 main.log.info( self.name + " ::::::" )
853 main.log.error( traceback.print_exc() )
854 main.log.info( self.name + " ::::::" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400855 main.cleanup()
856 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400857
kelvin-onlabd3b64892015-01-20 13:26:24 -0800858 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800859 """
andrewonlabe6745342014-10-17 14:29:13 -0400860 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800861 * hostIdOne: ONOS host id for host1
862 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400863 Description:
kelvin8ec71442015-01-15 16:57:00 -0800864 Adds a host-to-host intent ( bidrectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500865 specifying the two hosts.
kelvin8ec71442015-01-15 16:57:00 -0800866 """
andrewonlabe6745342014-10-17 14:29:13 -0400867 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800868 cmdStr = "add-host-intent " + str( hostIdOne ) +\
869 " " + str( hostIdTwo )
870 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800871 main.log.info( "Host intent installed between " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800872 str( hostIdOne ) + " and " + str( hostIdTwo ) )
andrewonlabe6745342014-10-17 14:29:13 -0400873 return handle
andrewonlabe6745342014-10-17 14:29:13 -0400874 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800875 main.log.error( self.name + ": EOF exception found" )
876 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400877 main.cleanup()
878 main.exit()
879 except:
kelvin8ec71442015-01-15 16:57:00 -0800880 main.log.info( self.name + " ::::::" )
881 main.log.error( traceback.print_exc() )
882 main.log.info( self.name + " ::::::" )
andrewonlabe6745342014-10-17 14:29:13 -0400883 main.cleanup()
884 main.exit()
885
kelvin-onlabd3b64892015-01-20 13:26:24 -0800886 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800887 """
andrewonlab7b31d232014-10-24 13:31:47 -0400888 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800889 * ingressDevice: device id of ingress device
890 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400891 Optional:
892 TODO: Still needs to be implemented via dev side
kelvin-onlab898a6c62015-01-16 14:13:53 -0800893 """
andrewonlab7b31d232014-10-24 13:31:47 -0400894 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800895 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
896 " " + str( egressDevice )
897 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800898 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800899 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400900 return handle
901 else:
902 return main.TRUE
andrewonlab7b31d232014-10-24 13:31:47 -0400903 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800904 main.log.error( self.name + ": EOF exception found" )
905 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400906 main.cleanup()
907 main.exit()
908 except:
kelvin8ec71442015-01-15 16:57:00 -0800909 main.log.info( self.name + " ::::::" )
910 main.log.error( traceback.print_exc() )
911 main.log.info( self.name + " ::::::" )
andrewonlab7b31d232014-10-24 13:31:47 -0400912 main.cleanup()
913 main.exit()
914
kelvin-onlabd3b64892015-01-20 13:26:24 -0800915 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800916 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800917 ingressDevice,
918 egressDevice,
919 portIngress="",
920 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800921 ethType="",
922 ethSrc="",
923 ethDst="",
924 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800925 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800926 ipProto="",
927 ipSrc="",
928 ipDst="",
929 tcpSrc="",
930 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -0800931 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400932 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800933 * ingressDevice: device id of ingress device
934 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400935 Optional:
936 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -0800937 * ethSrc: specify ethSrc ( i.e. src mac addr )
938 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500939 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -0800940 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -0500941 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -0800942 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -0500943 * ipSrc: specify ip source address
944 * ipDst: specify ip destination address
945 * tcpSrc: specify tcp source port
946 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400947 Description:
kelvin8ec71442015-01-15 16:57:00 -0800948 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -0400949 specifying device id's and optional fields
950
Jon Halle3f39ff2015-01-13 11:50:53 -0800951 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -0400952 options developers provide for point-to-point
953 intent via cli
kelvin8ec71442015-01-15 16:57:00 -0800954 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400955 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400956 cmd = ""
957
kelvin8ec71442015-01-15 16:57:00 -0800958 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500959 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800960 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -0500961 and not ipProto and not ipSrc and not ipDst \
962 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -0500963 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -0500964
andrewonlab289e4b72014-10-21 21:24:18 -0400965 else:
andrewonlab36af3822014-11-18 17:48:18 -0500966 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -0800967
andrewonlab0c0a6772014-10-22 12:31:18 -0400968 if ethType:
kelvin8ec71442015-01-15 16:57:00 -0800969 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -0400970 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -0800971 cmd += " --ethSrc " + str( ethSrc )
972 if ethDst:
973 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500974 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -0800975 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800976 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -0500977 cmd += " --lambda "
978 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -0800979 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -0500980 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -0800981 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -0500982 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -0800983 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -0500984 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -0800985 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -0500986 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -0800987 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -0400988
kelvin8ec71442015-01-15 16:57:00 -0800989 # Check whether the user appended the port
990 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -0800991 if "/" in ingressDevice:
992 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -0500993 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800994 if not portIngress:
kelvin8ec71442015-01-15 16:57:00 -0800995 main.log.error( "You must specify " +
996 "the ingress port" )
997 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -0500998 return main.FALSE
999
kelvin8ec71442015-01-15 16:57:00 -08001000 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001001 str( ingressDevice ) + "/" +\
1002 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001003
kelvin-onlabd3b64892015-01-20 13:26:24 -08001004 if "/" in egressDevice:
1005 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001006 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001007 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001008 main.log.error( "You must specify " +
1009 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001010 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001011
kelvin8ec71442015-01-15 16:57:00 -08001012 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001013 str( egressDevice ) + "/" +\
1014 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001015
kelvin-onlab898a6c62015-01-16 14:13:53 -08001016 handle = self.sendline( cmd )
1017 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001018 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001019 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001020 else:
1021 return main.TRUE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001022 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001023 main.log.error( self.name + ": EOF exception found" )
1024 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001025 main.cleanup()
1026 main.exit()
1027 except:
kelvin8ec71442015-01-15 16:57:00 -08001028 main.log.info( self.name + " ::::::" )
1029 main.log.error( traceback.print_exc() )
1030 main.log.info( self.name + " ::::::" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001031 main.cleanup()
1032 main.exit()
1033
kelvin-onlabd3b64892015-01-20 13:26:24 -08001034 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001035 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001036 ingressDevice1,
1037 ingressDevice2,
1038 egressDevice,
1039 portIngress="",
1040 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001041 ethType="",
1042 ethSrc="",
1043 ethDst="",
1044 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001045 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001046 ipProto="",
1047 ipSrc="",
1048 ipDst="",
1049 tcpSrc="",
1050 tcpDst="",
1051 setEthSrc="",
1052 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001053 """
shahshreyad0c80432014-12-04 16:56:05 -08001054 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001055 This function assumes that there would be 2 ingress devices and
1056 one egress device. For more number of ingress devices, this
1057 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001058 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001059 * ingressDevice1: device id of ingress device1
1060 * ingressDevice2: device id of ingress device2
1061 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001062 Optional:
1063 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001064 * ethSrc: specify ethSrc ( i.e. src mac addr )
1065 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001066 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001067 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001068 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001069 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001070 * ipSrc: specify ip source address
1071 * ipDst: specify ip destination address
1072 * tcpSrc: specify tcp source port
1073 * tcpDst: specify tcp destination port
1074 * setEthSrc: action to Rewrite Source MAC Address
1075 * setEthDst: action to Rewrite Destination MAC Address
1076 Description:
kelvin8ec71442015-01-15 16:57:00 -08001077 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001078 specifying device id's and optional fields
1079
Jon Halle3f39ff2015-01-13 11:50:53 -08001080 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001081 options developers provide for multipointpoint-to-singlepoint
1082 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001083 """
shahshreyad0c80432014-12-04 16:56:05 -08001084 try:
1085 cmd = ""
1086
kelvin8ec71442015-01-15 16:57:00 -08001087 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001088 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001089 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001090 and not ipProto and not ipSrc and not ipDst\
1091 and not tcpSrc and not tcpDst and not setEthSrc\
1092 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001093 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001094
1095 else:
1096 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001097
shahshreyad0c80432014-12-04 16:56:05 -08001098 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001099 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001100 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001101 cmd += " --ethSrc " + str( ethSrc )
1102 if ethDst:
1103 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001104 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001105 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001106 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001107 cmd += " --lambda "
1108 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001109 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001110 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001111 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001112 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001113 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001114 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001115 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001116 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001117 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001118 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001119 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001120 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001121 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001122
kelvin8ec71442015-01-15 16:57:00 -08001123 # Check whether the user appended the port
1124 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001125 if "/" in ingressDevice1:
1126 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001127 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001128 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001129 main.log.error( "You must specify " +
1130 "the ingress port1" )
1131 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001132 return main.FALSE
1133
kelvin8ec71442015-01-15 16:57:00 -08001134 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001135 str( ingressDevice1 ) + "/" +\
1136 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001137
kelvin-onlabd3b64892015-01-20 13:26:24 -08001138 if "/" in ingressDevice2:
1139 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001140 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001141 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001142 main.log.error( "You must specify " +
1143 "the ingress port2" )
1144 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001145 return main.FALSE
1146
kelvin8ec71442015-01-15 16:57:00 -08001147 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001148 str( ingressDevice2 ) + "/" +\
1149 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001150
kelvin-onlabd3b64892015-01-20 13:26:24 -08001151 if "/" in egressDevice:
1152 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001153 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001154 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001155 main.log.error( "You must specify " +
1156 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001157 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001158
kelvin8ec71442015-01-15 16:57:00 -08001159 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001160 str( egressDevice ) + "/" +\
1161 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001162 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001163 handle = self.sendline( cmd )
1164 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001165 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001166 return self.handle
1167 else:
1168 return main.TRUE
shahshreyad0c80432014-12-04 16:56:05 -08001169 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001170 main.log.error( self.name + ": EOF exception found" )
1171 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001172 main.cleanup()
1173 main.exit()
1174 except:
kelvin8ec71442015-01-15 16:57:00 -08001175 main.log.info( self.name + " ::::::" )
1176 main.log.error( traceback.print_exc() )
1177 main.log.info( self.name + " ::::::" )
shahshreyad0c80432014-12-04 16:56:05 -08001178 main.cleanup()
1179 main.exit()
1180
kelvin-onlabd3b64892015-01-20 13:26:24 -08001181 def removeIntent( self, intentId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001182 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001183 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001184
1185 Returns:
1186 main.False on error and
1187 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001188 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001189 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001190 cmdStr = "remove-intent " + str( intentId )
1191 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001192 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001193 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001194 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001195 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001196 # TODO: Should this be main.TRUE
1197 return handle
andrewonlab9a50dfe2014-10-17 17:22:31 -04001198 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001199 main.log.error( self.name + ": EOF exception found" )
1200 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001201 main.cleanup()
1202 main.exit()
1203 except:
kelvin8ec71442015-01-15 16:57:00 -08001204 main.log.info( self.name + " ::::::" )
1205 main.log.error( traceback.print_exc() )
1206 main.log.info( self.name + " ::::::" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001207 main.cleanup()
1208 main.exit()
1209
kelvin-onlabd3b64892015-01-20 13:26:24 -08001210 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001211 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001212 NOTE: This method should be used after installing application:
1213 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001214 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001215 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001216 Description:
1217 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001218 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001219 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001220 if jsonFormat:
1221 cmdStr = "routes -j"
1222 handleTmp = self.sendline( cmdStr )
1223 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1224 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001225 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001226 cmdStr = "routes"
1227 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001228 return handle
pingping-lin8b306ac2014-11-17 18:13:51 -08001229 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001230 main.log.error( self.name + ": EOF exception found" )
1231 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001232 main.cleanup()
1233 main.exit()
1234 except:
kelvin8ec71442015-01-15 16:57:00 -08001235 main.log.info( self.name + " ::::::" )
1236 main.log.error( traceback.print_exc() )
1237 main.log.info( self.name + " ::::::" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001238 main.cleanup()
1239 main.exit()
1240
kelvin-onlabd3b64892015-01-20 13:26:24 -08001241 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001242 """
andrewonlab377693f2014-10-21 16:00:30 -04001243 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001244 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001245 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001246 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001247 """
andrewonlabe6745342014-10-17 14:29:13 -04001248 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001249 if jsonFormat:
1250 cmdStr = "intents -j"
1251 handle = self.sendline( cmdStr )
1252 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1253 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001254 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001255 cmdStr = "intents"
1256 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001257 return handle
andrewonlabe6745342014-10-17 14:29:13 -04001258 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001259 main.log.error( self.name + ": EOF exception found" )
1260 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001261 main.cleanup()
1262 main.exit()
1263 except:
kelvin8ec71442015-01-15 16:57:00 -08001264 main.log.info( self.name + " ::::::" )
1265 main.log.error( traceback.print_exc() )
1266 main.log.info( self.name + " ::::::" )
andrewonlabe6745342014-10-17 14:29:13 -04001267 main.cleanup()
1268 main.exit()
1269
kelvin-onlabd3b64892015-01-20 13:26:24 -08001270 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001271 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001272 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001273 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001274 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001275 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001276 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001277 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001278 if jsonFormat:
1279 cmdStr = "flows -j"
1280 handle = self.sendline( cmdStr )
1281 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1282 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001283 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001284 cmdStr = "flows"
1285 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001286 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001287 main.log.error( self.name + ".flows() response: " +
1288 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001289 return handle
Shreya Shah0f01c812014-10-26 20:15:28 -04001290 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001291 main.log.error( self.name + ": EOF exception found" )
1292 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001293 main.cleanup()
1294 main.exit()
1295 except:
kelvin8ec71442015-01-15 16:57:00 -08001296 main.log.info( self.name + " ::::::" )
1297 main.log.error( traceback.print_exc() )
1298 main.log.info( self.name + " ::::::" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001299 main.cleanup()
1300 main.exit()
1301
kelvin-onlabd3b64892015-01-20 13:26:24 -08001302 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
1303 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001304 """
andrewonlab87852b02014-11-19 18:44:19 -05001305 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001306 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001307 a specific point-to-point intent definition
1308 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001309 * dpidSrc: specify source dpid
1310 * dpidDst: specify destination dpid
1311 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001312 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001313 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001314 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001315 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001316 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001317 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001318 """
andrewonlab87852b02014-11-19 18:44:19 -05001319 try:
kelvin8ec71442015-01-15 16:57:00 -08001320 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001321 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1322 str( numIntents )
1323 if numMult:
1324 cmd += " " + str( numMult )
1325 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001326 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001327 if appId:
1328 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001329 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001330 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1331 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001332 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001333 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001334 main.log.info( handle )
1335 # Split result by newline
1336 newline = handle.split( "\r\r\n" )
1337 # Ignore the first object of list, which is empty
1338 newline = newline[ 1: ]
1339 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001340 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001341 result = result.split( ": " )
1342 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001343 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1344 main.log.info( latResult )
1345 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001346 else:
1347 return main.TRUE
andrewonlab87852b02014-11-19 18:44:19 -05001348 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001349 main.log.error( self.name + ": EOF exception found" )
1350 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001351 main.cleanup()
1352 main.exit()
1353 except:
kelvin8ec71442015-01-15 16:57:00 -08001354 main.log.info( self.name + " ::::::" )
1355 main.log.error( traceback.print_exc() )
1356 main.log.info( self.name + " ::::::" )
andrewonlab87852b02014-11-19 18:44:19 -05001357 main.cleanup()
1358 main.exit()
1359
kelvin-onlabd3b64892015-01-20 13:26:24 -08001360 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001361 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001362 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001363 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001364 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001365 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001366 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001367 if jsonFormat:
1368 cmdStr = "intents-events-metrics -j"
1369 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001370 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001371 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1372 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001373 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001374 cmdStr = "intents-events-metrics"
1375 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001376 return handle
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001377 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001378 main.log.error( self.name + ": EOF exception found" )
1379 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001380 main.cleanup()
1381 main.exit()
1382 except:
kelvin8ec71442015-01-15 16:57:00 -08001383 main.log.info( self.name + " ::::::" )
1384 main.log.error( traceback.print_exc() )
1385 main.log.info( self.name + " ::::::" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001386 main.cleanup()
1387 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001388
kelvin-onlabd3b64892015-01-20 13:26:24 -08001389 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001390 """
1391 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001392 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001393 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001394 """
andrewonlab867212a2014-10-22 20:13:38 -04001395 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001396 if jsonFormat:
1397 cmdStr = "topology-events-metrics -j"
1398 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001399 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001400 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1401 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001402 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001403 cmdStr = "topology-events-metrics"
1404 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001405 return handle
andrewonlab867212a2014-10-22 20:13:38 -04001406 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001407 main.log.error( self.name + ": EOF exception found" )
1408 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001409 main.cleanup()
1410 main.exit()
1411 except:
kelvin8ec71442015-01-15 16:57:00 -08001412 main.log.info( self.name + " ::::::" )
1413 main.log.error( traceback.print_exc() )
1414 main.log.info( self.name + " ::::::" )
andrewonlab867212a2014-10-22 20:13:38 -04001415 main.cleanup()
1416 main.exit()
1417
kelvin8ec71442015-01-15 16:57:00 -08001418 # Wrapper functions ****************
1419 # Wrapper functions use existing driver
1420 # functions and extends their use case.
1421 # For example, we may use the output of
1422 # a normal driver function, and parse it
1423 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001424
kelvin-onlabd3b64892015-01-20 13:26:24 -08001425 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001426 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001427 Description:
1428 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001429 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001430 try:
kelvin8ec71442015-01-15 16:57:00 -08001431 # Obtain output of intents function
kelvin-onlabd3b64892015-01-20 13:26:24 -08001432 intentsStr = self.intents()
1433 allIntentList = []
1434 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001435
kelvin8ec71442015-01-15 16:57:00 -08001436 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001437 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1438 for intents in intentsList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001439 if "onos>" in intents:
1440 continue
1441 elif "intents" in intents:
1442 continue
1443 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001444 lineList = intents.split( " " )
1445 allIntentList.append( lineList[ 0 ] )
kelvin8ec71442015-01-15 16:57:00 -08001446
kelvin-onlabd3b64892015-01-20 13:26:24 -08001447 allIntentList = allIntentList[ 1:-2 ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001448
kelvin-onlabd3b64892015-01-20 13:26:24 -08001449 for intents in allIntentList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001450 if not intents:
1451 continue
1452 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001453 intentIdList.append( intents )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001454
kelvin-onlabd3b64892015-01-20 13:26:24 -08001455 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001456
1457 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001458 main.log.error( self.name + ": EOF exception found" )
1459 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001460 main.cleanup()
1461 main.exit()
1462 except:
kelvin8ec71442015-01-15 16:57:00 -08001463 main.log.info( self.name + " ::::::" )
1464 main.log.error( traceback.print_exc() )
1465 main.log.info( self.name + " ::::::" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001466 main.cleanup()
1467 main.exit()
1468
kelvin-onlabd3b64892015-01-20 13:26:24 -08001469 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001470 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001471 Use 'devices' function to obtain list of all devices
1472 and parse the result to obtain a list of all device
1473 id's. Returns this list. Returns empty list if no
1474 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001475 List is ordered sequentially
1476
andrewonlab3e15ead2014-10-15 14:21:34 -04001477 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001478 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001479 the ids. By obtaining the list of device ids on the fly,
1480 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001481 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001482 try:
kelvin8ec71442015-01-15 16:57:00 -08001483 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001484 devicesStr = self.devices( jsonFormat=False )
1485 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001486
kelvin-onlabd3b64892015-01-20 13:26:24 -08001487 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001488 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001489 return idList
kelvin8ec71442015-01-15 16:57:00 -08001490
1491 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001492 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001493 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001494 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001495 # Split list further into arguments before and after string
1496 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001497 # append to idList
1498 for arg in tempList:
1499 idList.append( arg.split( "id=" )[ 1 ] )
1500 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001501
1502 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001503 main.log.error( self.name + ": EOF exception found" )
1504 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001505 main.cleanup()
1506 main.exit()
1507 except:
kelvin8ec71442015-01-15 16:57:00 -08001508 main.log.info( self.name + " ::::::" )
1509 main.log.error( traceback.print_exc() )
1510 main.log.info( self.name + " ::::::" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001511 main.cleanup()
1512 main.exit()
1513
kelvin-onlabd3b64892015-01-20 13:26:24 -08001514 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001515 """
andrewonlab7c211572014-10-15 16:45:20 -04001516 Uses 'nodes' function to obtain list of all nodes
1517 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001518 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001519 Returns:
1520 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001521 """
andrewonlab7c211572014-10-15 16:45:20 -04001522 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001523 nodesStr = self.nodes()
1524 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001525
kelvin-onlabd3b64892015-01-20 13:26:24 -08001526 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001527 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001528 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001529
kelvin-onlabd3b64892015-01-20 13:26:24 -08001530 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001531 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001532
kelvin8ec71442015-01-15 16:57:00 -08001533 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001534 nodesList = nodesStr.split( "," )
1535 tempList = [ node for node in nodesList if "id=" in node ]
1536 for arg in tempList:
1537 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001538
kelvin-onlabd3b64892015-01-20 13:26:24 -08001539 return idList
kelvin8ec71442015-01-15 16:57:00 -08001540
andrewonlab7c211572014-10-15 16:45:20 -04001541 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001542 main.log.error( self.name + ": EOF exception found" )
1543 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001544 main.cleanup()
1545 main.exit()
1546 except:
kelvin8ec71442015-01-15 16:57:00 -08001547 main.log.info( self.name + " ::::::" )
1548 main.log.error( traceback.print_exc() )
1549 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -04001550 main.cleanup()
1551 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001552
kelvin-onlabd3b64892015-01-20 13:26:24 -08001553 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001554 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001555 Return the first device from the devices api whose 'id' contains 'dpid'
1556 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001557 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001558 import json
1559 try:
kelvin8ec71442015-01-15 16:57:00 -08001560 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001561 return None
1562 else:
kelvin8ec71442015-01-15 16:57:00 -08001563 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001564 rawDevices = self.devices()
1565 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001566 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001567 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001568 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1569 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001570 return device
1571 return None
1572 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001573 main.log.error( self.name + ": EOF exception found" )
1574 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001575 main.cleanup()
1576 main.exit()
1577 except:
kelvin8ec71442015-01-15 16:57:00 -08001578 main.log.info( self.name + " ::::::" )
1579 main.log.error( traceback.print_exc() )
1580 main.log.info( self.name + " ::::::" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001581 main.cleanup()
1582 main.exit()
1583
kelvin-onlabd3b64892015-01-20 13:26:24 -08001584 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001585 """
1586 Checks the number of swithes & links that ONOS sees against the
1587 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001588 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001589
Jon Hall42db6dc2014-10-24 19:03:48 -04001590 Params: ip = ip used for the onos cli
1591 numoswitch = expected number of switches
1592 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001593 logLevel = level to log to. Currently accepts
1594 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001595
1596
kelvin-onlabd3b64892015-01-20 13:26:24 -08001597 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001598
kelvin8ec71442015-01-15 16:57:00 -08001599 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001600 main.FALSE if the numer of switches and links is incorrect,
1601 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001602 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001603 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001604 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001605 if topology == {}:
1606 return main.ERROR
1607 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001608 # Is the number of switches is what we expected
1609 devices = topology.get( 'devices', False )
1610 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001611 if devices == False or links == False:
1612 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001613 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001614 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001615 linkCheck = ( int( links ) == int( numolink ) )
1616 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001617 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001618 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001619 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001620 result = main.TRUE
1621 else:
1622 output = output + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001623 "The number of links and switches does not matc\
1624 h what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001625 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001626 output = output + "\n ONOS sees %i devices (%i expected) \
1627 and %i links (%i expected)" % (
1628 int( devices ), int( numoswitch ), int( links ),
1629 int( numolink ) )
1630 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001631 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001632 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001633 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001634 else:
kelvin8ec71442015-01-15 16:57:00 -08001635 main.log.info( output )
1636 return result
Jon Hall42db6dc2014-10-24 19:03:48 -04001637 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001638 main.log.error( self.name + ": EOF exception found" )
1639 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001640 main.cleanup()
1641 main.exit()
1642 except:
kelvin8ec71442015-01-15 16:57:00 -08001643 main.log.info( self.name + " ::::::" )
1644 main.log.error( traceback.print_exc() )
1645 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001646 main.cleanup()
1647 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001648
kelvin-onlabd3b64892015-01-20 13:26:24 -08001649 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001650 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001651 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001652 deviceId must be the id of a device as seen in the onos devices command
1653 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001654 role must be either master, standby, or none
1655
Jon Halle3f39ff2015-01-13 11:50:53 -08001656 Returns:
1657 main.TRUE or main.FALSE based on argument verification and
1658 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001659 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001660 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001661 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001662 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001663 cmdStr = "device-role " +\
1664 str( deviceId ) + " " +\
1665 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001666 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001667 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001668 if re.search( "Error", handle ):
1669 # end color output to escape any colours
1670 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001671 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001672 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001673 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001674 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001675 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001676 main.log.error( "Invalid 'role' given to device_role(). " +
1677 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001678 return main.FALSE
Jon Hall1c9e8732014-10-27 19:29:27 -04001679 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001680 main.log.error( self.name + ": EOF exception found" )
1681 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001682 main.cleanup()
1683 main.exit()
1684 except:
kelvin8ec71442015-01-15 16:57:00 -08001685 main.log.info( self.name + " ::::::" )
1686 main.log.error( traceback.print_exc() )
1687 main.log.info( self.name + " ::::::" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001688 main.cleanup()
1689 main.exit()
1690
kelvin-onlabd3b64892015-01-20 13:26:24 -08001691 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001692 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001693 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001694 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001695 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001696 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001697 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001698 if jsonFormat:
1699 cmdStr = "clusters -j"
1700 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001701 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001702 handle variable here contains some ANSI escape color code
1703 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001704 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001705 function. The repr( handle ) output when printed shows the ANSI
1706 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001707 variable is actually repr( somestring ) and json.loads would
1708 fail with the escape sequence. So we take off that escape
1709 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001710
kelvin-onlabd3b64892015-01-20 13:26:24 -08001711 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1712 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001713 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001714 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1715 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001716 return handle1
1717 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001718 cmdStr = "clusters"
1719 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001720 return handle
1721 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001722 main.log.error( self.name + ": EOF exception found" )
1723 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001724 main.cleanup()
1725 main.exit()
1726 except:
kelvin8ec71442015-01-15 16:57:00 -08001727 main.log.info( self.name + " ::::::" )
1728 main.log.error( traceback.print_exc() )
1729 main.log.info( self.name + " ::::::" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001730 main.cleanup()
1731 main.exit()
1732
kelvin-onlabd3b64892015-01-20 13:26:24 -08001733 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001734 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001735 CLI command to get the current leader for the Election test application
1736 NOTE: Requires installation of the onos-app-election feature
1737 Returns: Node IP of the leader if one exists
1738 None if none exists
1739 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001740 """
Jon Hall94fd0472014-12-08 11:52:42 -08001741 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001742 cmdStr = "election-test-leader"
1743 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001744 # Leader
1745 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001746 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001747 nodeSearch = re.search( leaderPattern, response )
1748 if nodeSearch:
1749 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001750 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001751 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001752 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001753 # no leader
1754 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001755 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001756 nullSearch = re.search( nullPattern, response )
1757 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001758 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001759 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001760 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001761 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001762 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001763 if re.search( errorPattern, response ):
1764 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001765 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001766 return main.FALSE
1767 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001768 main.log.error( "Error in election_test_leader: " +
1769 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001770 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001771 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001772 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001773 main.log.error( self.name + ": EOF exception found" )
1774 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001775 main.cleanup()
1776 main.exit()
1777 except:
kelvin8ec71442015-01-15 16:57:00 -08001778 main.log.info( self.name + " ::::::" )
1779 main.log.error( traceback.print_exc() )
1780 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001781 main.cleanup()
1782 main.exit()
1783
kelvin-onlabd3b64892015-01-20 13:26:24 -08001784 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001785 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001786 CLI command to run for leadership of the Election test application.
1787 NOTE: Requires installation of the onos-app-election feature
1788 Returns: Main.TRUE on success
1789 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001790 """
Jon Hall94fd0472014-12-08 11:52:42 -08001791 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001792 cmdStr = "election-test-run"
1793 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001794 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001795 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001796 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001797 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001798 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001799 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001800 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001801 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001802 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001803 errorPattern = "Command\snot\sfound"
1804 if re.search( errorPattern, response ):
1805 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001806 return main.FALSE
1807 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001808 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001809 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001810 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001811 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001812 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001813 main.log.error( self.name + ": EOF exception found" )
1814 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001815 main.cleanup()
1816 main.exit()
1817 except:
kelvin8ec71442015-01-15 16:57:00 -08001818 main.log.info( self.name + " ::::::" )
1819 main.log.error( traceback.print_exc() )
1820 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001821 main.cleanup()
1822 main.exit()
1823
kelvin-onlabd3b64892015-01-20 13:26:24 -08001824 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08001825 """
Jon Hall94fd0472014-12-08 11:52:42 -08001826 * CLI command to withdraw the local node from leadership election for
1827 * the Election test application.
1828 #NOTE: Requires installation of the onos-app-election feature
1829 Returns: Main.TRUE on success
1830 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001831 """
Jon Hall94fd0472014-12-08 11:52:42 -08001832 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001833 cmdStr = "election-test-withdraw"
1834 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001835 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001836 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001837 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001838 if re.search( successPattern, response ):
1839 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001840 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001841 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001842 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001843 errorPattern = "Command\snot\sfound"
1844 if re.search( errorPattern, response ):
1845 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001846 return main.FALSE
1847 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001848 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001849 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001850 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001851 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001852 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001853 main.log.error( self.name + ": EOF exception found" )
1854 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001855 main.cleanup()
1856 main.exit()
1857 except:
kelvin8ec71442015-01-15 16:57:00 -08001858 main.log.info( self.name + " ::::::" )
1859 main.log.error( traceback.print_exc() )
1860 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001861 main.cleanup()
1862 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001863
kelvin8ec71442015-01-15 16:57:00 -08001864 def getDevicePortsEnabledCount( self, dpid ):
1865 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001866 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001867 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001868 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001869 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001870 cmdStr = "onos:ports -e " + dpid + " | wc -l"
1871 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001872 if re.search( "No such device", output ):
1873 main.log.error( "Error in getting ports" )
1874 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001875 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001876 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001877 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001878 main.log.error( self.name + ": EOF exception found" )
1879 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001880 main.cleanup()
1881 main.exit()
1882 except:
kelvin8ec71442015-01-15 16:57:00 -08001883 main.log.info( self.name + " ::::::" )
1884 main.log.error( traceback.print_exc() )
1885 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001886 main.cleanup()
1887 main.exit()
1888
kelvin8ec71442015-01-15 16:57:00 -08001889 def getDeviceLinksActiveCount( self, dpid ):
1890 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001891 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001892 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001893 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001894 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001895 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
1896 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001897 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001898 main.log.error( "Error in getting ports " )
1899 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001900 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001901 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001902 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001903 main.log.error( self.name + ": EOF exception found" )
1904 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001905 main.cleanup()
1906 main.exit()
1907 except:
kelvin8ec71442015-01-15 16:57:00 -08001908 main.log.info( self.name + " ::::::" )
1909 main.log.error( traceback.print_exc() )
1910 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001911 main.cleanup()
1912 main.exit()
1913
kelvin8ec71442015-01-15 16:57:00 -08001914 def getAllIntentIds( self ):
1915 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001916 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08001917 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001918 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001919 cmdStr = "onos:intents | grep id="
1920 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001921 if re.search( "Error", output ):
1922 main.log.error( "Error in getting ports" )
1923 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001924 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001925 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001926 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001927 main.log.error( self.name + ": EOF exception found" )
1928 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001929 main.cleanup()
1930 main.exit()
1931 except:
kelvin8ec71442015-01-15 16:57:00 -08001932 main.log.info( self.name + " ::::::" )
1933 main.log.error( traceback.print_exc() )
1934 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001935 main.cleanup()
1936 main.exit()