blob: 2f9e752c48eca98c595e8cd2a1718290fc9042f8 [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-onlabd3b64892015-01-20 13:26:24 -080050 userName=self.userName,
51 ipAddress=self.ipAddress,
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
Cameron Franke9c94fb02015-01-21 10:20:20 -0800145 self.handle.expect(str(cellname))
146
andrew@onlab.usc400b112015-01-21 15:33:19 -0800147 handleBefore = self.handle.before
148 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800149 # Get the rest of the handle
Cameron Franke9c94fb02015-01-21 10:20:20 -0800150 self.handle.sendline("")
151 self.handle.expect("\$")
andrew@onlab.usc400b112015-01-21 15:33:19 -0800152 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400153
kelvin-onlabd3b64892015-01-20 13:26:24 -0800154 main.log.info( "Cell call returned: " + handleBefore +
155 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400156
157 return main.TRUE
158
159 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800160 main.log.error( self.name + ": eof exception found" )
161 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400162 main.cleanup()
163 main.exit()
164 except:
kelvin8ec71442015-01-15 16:57:00 -0800165 main.log.info( self.name + " ::::::" )
166 main.log.error( traceback.print_exc() )
167 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400168 main.cleanup()
169 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800170
kelvin-onlabd3b64892015-01-20 13:26:24 -0800171 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800172 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800173 karafTimeout is an optional arugument. karafTimeout value passed
174 by user would be used to set the current karaf shell idle timeout.
175 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800176 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800177 Below is an example to start a session with 60 seconds idle timeout
178 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800179
Hari Krishna25d42f72015-01-05 15:08:28 -0800180 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800181 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800182
kelvin-onlabd3b64892015-01-20 13:26:24 -0800183 Note: karafTimeout is left as str so that this could be read
184 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800185 """
andrewonlab95ce8322014-10-13 14:12:04 -0400186 try:
kelvin8ec71442015-01-15 16:57:00 -0800187 self.handle.sendline( "" )
188 x = self.handle.expect( [
189 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500190
191 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800192 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500193 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400194
kelvin8ec71442015-01-15 16:57:00 -0800195 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800196 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800197 i = self.handle.expect( [
198 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800199 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400200
201 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800202 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800203 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800204 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800205 "config:property-set -p org.apache.karaf.shel\
206 l sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800207 karafTimeout )
208 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800209 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800210 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400211 return main.TRUE
212 else:
kelvin8ec71442015-01-15 16:57:00 -0800213 # If failed, send ctrl+c to process and try again
214 main.log.info( "Starting CLI failed. Retrying..." )
215 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800216 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800217 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
218 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400219 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800220 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800221 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800222 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800223 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800224 "config:property-set -p org.apache.karaf.shell\
225 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800226 karafTimeout )
227 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800228 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800229 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400230 return main.TRUE
231 else:
kelvin8ec71442015-01-15 16:57:00 -0800232 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800233 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400234 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400235
236 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800237 main.log.error( self.name + ": EOF exception found" )
238 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400239 main.cleanup()
240 main.exit()
241 except:
kelvin8ec71442015-01-15 16:57:00 -0800242 main.log.info( self.name + " ::::::" )
243 main.log.error( traceback.print_exc() )
244 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400245 main.cleanup()
246 main.exit()
247
kelvin-onlabd3b64892015-01-20 13:26:24 -0800248 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800249 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800250 Send a completely user specified string to
251 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400252 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800253
andrewonlaba18f6bf2014-10-13 19:31:54 -0400254 Warning: There are no sanity checking to commands
255 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800256 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400257 try:
kelvin8ec71442015-01-15 16:57:00 -0800258 self.handle.sendline( "" )
259 self.handle.expect( "onos>" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400260
kelvin-onlab898a6c62015-01-16 14:13:53 -0800261 self.handle.sendline( "log:log \"Sending CLI command: '"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800262 + cmdStr + "'\"" )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800263 self.handle.expect( "onos>" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800264 self.handle.sendline( cmdStr )
265 self.handle.expect( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800266 self.handle.expect( "onos>" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400267
268 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800269
270 self.handle.sendline( "" )
271 self.handle.expect( "onos>" )
272
kelvin-onlabd3b64892015-01-20 13:26:24 -0800273 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
kelvin-onlab898a6c62015-01-16 14:13:53 -0800274 + self.name + "." )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800275 ansiEscape = re.compile( r'\x1b[^m]*m' )
276 handle = ansiEscape.sub( '', handle )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400277
278 return handle
279 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800280 main.log.error( self.name + ": EOF exception found" )
281 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400282 main.cleanup()
283 main.exit()
284 except:
kelvin8ec71442015-01-15 16:57:00 -0800285 main.log.info( self.name + " ::::::" )
286 main.log.error( traceback.print_exc() )
287 main.log.info( self.name + " ::::::" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400288 main.cleanup()
289 main.exit()
290
kelvin8ec71442015-01-15 16:57:00 -0800291 # IMPORTANT NOTE:
292 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800293 # the cli command changing 'a:b' with 'aB'.
294 # Ex ) onos:topology > onosTopology
295 # onos:links > onosLinks
296 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800297
kelvin-onlabd3b64892015-01-20 13:26:24 -0800298 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800299 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400300 Adds a new cluster node by ID and address information.
301 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800302 * nodeId
303 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400304 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800305 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800306 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400307 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800308 cmdStr = "add-node " + str( nodeId ) + " " +\
309 str( ONOSIp ) + " " + str( tcpPort )
310 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800311 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800312 main.log.error( "Error in adding node" )
313 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800314 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400315 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800316 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400317 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400318 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800319 main.log.error( self.name + ": EOF exception found" )
320 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400321 main.cleanup()
322 main.exit()
323 except:
kelvin8ec71442015-01-15 16:57:00 -0800324 main.log.info( self.name + " ::::::" )
325 main.log.error( traceback.print_exc() )
326 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400327 main.cleanup()
328 main.exit()
329
kelvin-onlabd3b64892015-01-20 13:26:24 -0800330 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800331 """
andrewonlab86dc3082014-10-13 18:18:38 -0400332 Removes a cluster by ID
333 Issues command: 'remove-node [<node-id>]'
334 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800335 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800336 """
andrewonlab86dc3082014-10-13 18:18:38 -0400337 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400338
kelvin-onlabd3b64892015-01-20 13:26:24 -0800339 cmdStr = "remove-node " + str( nodeId )
340 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800341 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400342
343 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800344
andrewonlab86dc3082014-10-13 18:18:38 -0400345 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800346 main.log.error( self.name + ": EOF exception found" )
347 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400348 main.cleanup()
349 main.exit()
350 except:
kelvin8ec71442015-01-15 16:57:00 -0800351 main.log.info( self.name + " ::::::" )
352 main.log.error( traceback.print_exc() )
353 main.log.info( self.name + " ::::::" )
andrewonlab86dc3082014-10-13 18:18:38 -0400354 main.cleanup()
355 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400356
kelvin8ec71442015-01-15 16:57:00 -0800357 def nodes( self ):
358 """
andrewonlab7c211572014-10-15 16:45:20 -0400359 List the nodes currently visible
360 Issues command: 'nodes'
361 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800362 """
andrewonlab7c211572014-10-15 16:45:20 -0400363 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800364 cmdStr = "nodes"
365 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400366 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400367 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800368 main.log.error( self.name + ": EOF exception found" )
369 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400370 main.cleanup()
371 main.exit()
372 except:
kelvin8ec71442015-01-15 16:57:00 -0800373 main.log.info( self.name + " ::::::" )
374 main.log.error( traceback.print_exc() )
375 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -0400376 main.cleanup()
377 main.exit()
378
kelvin8ec71442015-01-15 16:57:00 -0800379 def topology( self ):
380 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400381 Shows the current state of the topology
382 by issusing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800383 """
andrewonlab95ce8322014-10-13 14:12:04 -0400384 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800385 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800386 cmdStr = "onos:topology"
387 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800388 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400389 return handle
andrewonlab95ce8322014-10-13 14:12:04 -0400390 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800391 main.log.error( self.name + ": EOF exception found" )
392 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400393 main.cleanup()
394 main.exit()
395 except:
kelvin8ec71442015-01-15 16:57:00 -0800396 main.log.info( self.name + " ::::::" )
397 main.log.error( traceback.print_exc() )
398 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400399 main.cleanup()
400 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800401
kelvin-onlabd3b64892015-01-20 13:26:24 -0800402 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800403 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800404 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400405 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800406 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400407 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800408 cmdStr = "feature:install " + str( featureStr )
409 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800410 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400411 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400412 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800413 main.log.error( self.name + ": EOF exception found" )
414 main.log.error( self.name + ": " + self.handle.before )
415 main.log.report( "Failed to install feature" )
416 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400417 main.cleanup()
418 main.exit()
419 except:
kelvin8ec71442015-01-15 16:57:00 -0800420 main.log.info( self.name + " ::::::" )
421 main.log.error( traceback.print_exc() )
422 main.log.report( "Failed to install feature" )
423 main.log.report( "Exiting test" )
424 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400425 main.cleanup()
426 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800427
kelvin-onlabd3b64892015-01-20 13:26:24 -0800428 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800429 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400430 Uninstalls a specified feature
431 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800432 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400433 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800434 cmdStr = "feature:uninstall " + str( featureStr )
435 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800436 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400437 return main.TRUE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400438 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800439 main.log.error( self.name + ": EOF exception found" )
440 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400441 main.cleanup()
442 main.exit()
443 except:
kelvin8ec71442015-01-15 16:57:00 -0800444 main.log.info( self.name + " ::::::" )
445 main.log.error( traceback.print_exc() )
446 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400447 main.cleanup()
448 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800449
kelvin-onlabd3b64892015-01-20 13:26:24 -0800450 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800451 """
Jon Hall7b02d952014-10-17 20:14:54 -0400452 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400453 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800454 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800455 """
andrewonlab86dc3082014-10-13 18:18:38 -0400456 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800457 if jsonFormat:
458 cmdStr = "devices -j"
459 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800460 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800461 handle variable here contains some ANSI escape color code
462 sequences at the end which are invisible in the print command
463 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800464 function. The repr( handle ) output when printed shows the
465 ANSI escape sequences. In json.loads( somestring ), this
466 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800467 json.loads would fail with the escape sequence. So we take off
468 that escape sequence using:
469
kelvin-onlabd3b64892015-01-20 13:26:24 -0800470 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
471 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800472 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800473 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
474 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400475 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400476 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800477 cmdStr = "devices"
478 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400479 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400480 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800481 main.log.error( self.name + ": EOF exception found" )
482 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400483 main.cleanup()
484 main.exit()
485 except:
kelvin8ec71442015-01-15 16:57:00 -0800486 main.log.info( self.name + " ::::::" )
487 main.log.error( traceback.print_exc() )
488 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -0400489 main.cleanup()
490 main.exit()
491
kelvin-onlabd3b64892015-01-20 13:26:24 -0800492 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800493 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800494 This balances the devices across all controllers
495 by issuing command: 'onos> onos:balance-masters'
496 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800497 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800498 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800499 cmdStr = "onos:balance-masters"
500 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800501 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800502 return main.TRUE
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800503 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800504 main.log.error( self.name + ": EOF exception found" )
505 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800506 main.cleanup()
507 main.exit()
508 except:
kelvin8ec71442015-01-15 16:57:00 -0800509 main.log.info( self.name + " ::::::" )
510 main.log.error( traceback.print_exc() )
511 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800512 main.cleanup()
513 main.exit()
514
kelvin-onlabd3b64892015-01-20 13:26:24 -0800515 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800516 """
Jon Halle8217482014-10-17 13:49:14 -0400517 Lists all core links
518 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800519 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800520 """
Jon Halle8217482014-10-17 13:49:14 -0400521 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800522 if jsonFormat:
523 cmdStr = "links -j"
524 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800525 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800526 handle variable here contains some ANSI escape color code
527 sequences at the end which are invisible in the print command
528 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800529 function. The repr( handle ) output when printed shows the ANSI
530 escape sequences. In json.loads( somestring ), this somestring
531 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800532 fail with the escape sequence. So we take off that escape
533 sequence using:
534
kelvin-onlabd3b64892015-01-20 13:26:24 -0800535 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
536 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800537 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800538 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
539 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400540 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400541 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800542 cmdStr = "links"
543 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400544 return handle
Jon Halle8217482014-10-17 13:49:14 -0400545 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800546 main.log.error( self.name + ": EOF exception found" )
547 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400548 main.cleanup()
549 main.exit()
550 except:
kelvin8ec71442015-01-15 16:57:00 -0800551 main.log.info( self.name + " ::::::" )
552 main.log.error( traceback.print_exc() )
553 main.log.info( self.name + " ::::::" )
Jon Halle8217482014-10-17 13:49:14 -0400554 main.cleanup()
555 main.exit()
556
kelvin-onlabd3b64892015-01-20 13:26:24 -0800557 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800558 """
Jon Halle8217482014-10-17 13:49:14 -0400559 Lists all ports
560 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800561 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800562 """
Jon Halle8217482014-10-17 13:49:14 -0400563 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800564 if jsonFormat:
565 cmdStr = "ports -j"
566 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800567 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800568 handle variable here contains some ANSI escape color code
569 sequences at the end which are invisible in the print command
570 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800571 function. The repr( handle ) output when printed shows the ANSI
572 escape sequences. In json.loads( somestring ), this somestring
573 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800574 fail with the escape sequence. So we take off that escape
575 sequence using the following commads:
576
kelvin-onlabd3b64892015-01-20 13:26:24 -0800577 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
578 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800579 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800580 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
581 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400582 return handle1
583
Jon Halle8217482014-10-17 13:49:14 -0400584 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800585 cmdStr = "ports"
586 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800587 return handle
Jon Halle8217482014-10-17 13:49:14 -0400588 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800589 main.log.error( self.name + ": EOF exception found" )
590 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400591 main.cleanup()
592 main.exit()
593 except:
kelvin8ec71442015-01-15 16:57:00 -0800594 main.log.info( self.name + " ::::::" )
595 main.log.error( traceback.print_exc() )
596 main.log.info( self.name + " ::::::" )
Jon Halle8217482014-10-17 13:49:14 -0400597 main.cleanup()
598 main.exit()
599
kelvin-onlabd3b64892015-01-20 13:26:24 -0800600 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800601 """
Jon Hall983a1702014-10-28 18:44:22 -0400602 Lists all devices and the controllers with roles assigned to them
603 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800604 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800605 """
andrewonlab7c211572014-10-15 16:45:20 -0400606 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800607 if jsonFormat:
608 cmdStr = "roles -j"
609 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800610 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800611 handle variable here contains some ANSI escape color code
612 sequences at the end which are invisible in the print command
613 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800614 function. The repr( handle ) output when printed shows the ANSI
615 escape sequences. In json.loads( somestring ), this somestring
616 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800617 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500618
Jon Halle3f39ff2015-01-13 11:50:53 -0800619 So we take off that escape sequence using the following
620 commads:
621
kelvin-onlabd3b64892015-01-20 13:26:24 -0800622 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
623 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800624 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800625 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
626 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400627 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400628
andrewonlab7c211572014-10-15 16:45:20 -0400629 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800630 cmdStr = "roles"
631 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800632 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400633 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800634 main.log.error( self.name + ": EOF exception found" )
635 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400636 main.cleanup()
637 main.exit()
638 except:
kelvin8ec71442015-01-15 16:57:00 -0800639 main.log.info( self.name + " ::::::" )
640 main.log.error( traceback.print_exc() )
641 main.log.info( self.name + " ::::::" )
Jon Hall983a1702014-10-28 18:44:22 -0400642 main.cleanup()
643 main.exit()
644
kelvin-onlabd3b64892015-01-20 13:26:24 -0800645 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800646 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800647 Given the a string containing the json representation of the "roles"
648 cli command and a partial or whole device id, returns a json object
649 containing the roles output for the first device whose id contains
650 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400651
652 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800653 A dict of the role assignments for the given device or
654 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800655 """
Jon Hall983a1702014-10-28 18:44:22 -0400656 try:
657 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800658 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400659 return None
660 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800661 rawRoles = self.roles()
662 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800663 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800664 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800665 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800666 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400667 return device
668 return None
andrewonlab7c211572014-10-15 16:45:20 -0400669
andrewonlab86dc3082014-10-13 18:18:38 -0400670 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800671 main.log.error( self.name + ": EOF exception found" )
672 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400673 main.cleanup()
674 main.exit()
675 except:
kelvin8ec71442015-01-15 16:57:00 -0800676 main.log.info( self.name + " ::::::" )
677 main.log.error( traceback.print_exc() )
678 main.log.info( self.name + " ::::::" )
andrewonlab86dc3082014-10-13 18:18:38 -0400679 main.cleanup()
680 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800681
kelvin-onlabd3b64892015-01-20 13:26:24 -0800682 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800683 """
Jon Hall94fd0472014-12-08 11:52:42 -0800684 Iterates through each device and checks if there is a master assigned
685 Returns: main.TRUE if each device has a master
686 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800687 """
Jon Hall94fd0472014-12-08 11:52:42 -0800688 try:
689 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800690 rawRoles = self.roles()
691 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800692 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800693 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800694 # print device
695 if device[ 'master' ] == "none":
696 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800697 return main.FALSE
698 return main.TRUE
699
700 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800701 main.log.error( self.name + ": EOF exception found" )
702 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800703 main.cleanup()
704 main.exit()
705 except:
kelvin8ec71442015-01-15 16:57:00 -0800706 main.log.info( self.name + " ::::::" )
707 main.log.error( traceback.print_exc() )
708 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -0800709 main.cleanup()
710 main.exit()
711
kelvin-onlabd3b64892015-01-20 13:26:24 -0800712 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800713 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400714 Returns string of paths, and the cost.
715 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800716 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400717 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800718 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
719 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800720 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800721 main.log.error( "Error in getting paths" )
722 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400723 else:
kelvin8ec71442015-01-15 16:57:00 -0800724 path = handle.split( ";" )[ 0 ]
725 cost = handle.split( ";" )[ 1 ]
726 return ( path, cost )
andrewonlab3e15ead2014-10-15 14:21:34 -0400727 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800728 main.log.error( self.name + ": EOF exception found" )
729 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400730 main.cleanup()
731 main.exit()
732 except:
kelvin8ec71442015-01-15 16:57:00 -0800733 main.log.info( self.name + " ::::::" )
734 main.log.error( traceback.print_exc() )
735 main.log.info( self.name + " ::::::" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400736 main.cleanup()
737 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800738
kelvin-onlabd3b64892015-01-20 13:26:24 -0800739 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800740 """
Jon Hallffb386d2014-11-21 13:43:38 -0800741 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400742 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800743 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800744 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400745 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800746 if jsonFormat:
747 cmdStr = "hosts -j"
748 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800749 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800750 handle variable here contains some ANSI escape color code
751 sequences at the end which are invisible in the print command
752 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800753 function. The repr( handle ) output when printed shows the ANSI
754 escape sequences. In json.loads( somestring ), this somestring
755 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800756 fail with the escape sequence. So we take off that escape
757 sequence using:
758
kelvin-onlabd3b64892015-01-20 13:26:24 -0800759 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
760 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800761 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800762 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
763 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400764 return handle1
765 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800766 cmdStr = "hosts"
767 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400768 return handle
769 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800770 main.log.error( self.name + ": EOF exception found" )
771 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400772 main.cleanup()
773 main.exit()
774 except:
kelvin8ec71442015-01-15 16:57:00 -0800775 main.log.info( self.name + " ::::::" )
776 main.log.error( traceback.print_exc() )
777 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400778 main.cleanup()
779 main.exit()
780
kelvin-onlabd3b64892015-01-20 13:26:24 -0800781 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800782 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400783 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800784
785 Note: mac must be a colon seperated mac address, but could be a
786 partial mac address
787
Jon Hall42db6dc2014-10-24 19:03:48 -0400788 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800789 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400790 import json
791 try:
kelvin8ec71442015-01-15 16:57:00 -0800792 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400793 return None
794 else:
795 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800796 rawHosts = self.hosts()
797 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800798 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800799 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800800 # print "%s in %s?" % ( mac, host[ 'id' ] )
801 if mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400802 return host
803 return None
804 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800805 main.log.error( self.name + ": EOF exception found" )
806 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400807 main.cleanup()
808 main.exit()
809 except:
kelvin8ec71442015-01-15 16:57:00 -0800810 main.log.info( self.name + " ::::::" )
811 main.log.error( traceback.print_exc() )
812 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400813 main.cleanup()
814 main.exit()
815
kelvin-onlabd3b64892015-01-20 13:26:24 -0800816 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800817 """
818 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400819 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800820
andrewonlab3f0a4af2014-10-17 12:25:14 -0400821 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800822 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400823 IMPORTANT:
824 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800825 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400826 Furthermore, it assumes that value of VLAN is '-1'
827 Description:
kelvin8ec71442015-01-15 16:57:00 -0800828 Converts mininet hosts ( h1, h2, h3... ) into
829 ONOS format ( 00:00:00:00:00:01/-1 , ... )
830 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400831 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800832 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400833
kelvin-onlabd3b64892015-01-20 13:26:24 -0800834 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800835 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800836 hostHex = hex( int( host ) ).zfill( 12 )
837 hostHex = str( hostHex ).replace( 'x', '0' )
838 i = iter( str( hostHex ) )
839 hostHex = ":".join( a + b for a, b in zip( i, i ) )
840 hostHex = hostHex + "/-1"
841 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400842
kelvin-onlabd3b64892015-01-20 13:26:24 -0800843 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400844
845 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800846 main.log.error( self.name + ": EOF exception found" )
847 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400848 main.cleanup()
849 main.exit()
850 except:
kelvin8ec71442015-01-15 16:57:00 -0800851 main.log.info( self.name + " ::::::" )
852 main.log.error( traceback.print_exc() )
853 main.log.info( self.name + " ::::::" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400854 main.cleanup()
855 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400856
kelvin-onlabd3b64892015-01-20 13:26:24 -0800857 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800858 """
andrewonlabe6745342014-10-17 14:29:13 -0400859 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800860 * hostIdOne: ONOS host id for host1
861 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400862 Description:
kelvin8ec71442015-01-15 16:57:00 -0800863 Adds a host-to-host intent ( bidrectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500864 specifying the two hosts.
kelvin8ec71442015-01-15 16:57:00 -0800865 """
andrewonlabe6745342014-10-17 14:29:13 -0400866 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800867 cmdStr = "add-host-intent " + str( hostIdOne ) +\
868 " " + str( hostIdTwo )
869 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800870 main.log.info( "Host intent installed between " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800871 str( hostIdOne ) + " and " + str( hostIdTwo ) )
andrewonlabe6745342014-10-17 14:29:13 -0400872 return handle
andrewonlabe6745342014-10-17 14:29:13 -0400873 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800874 main.log.error( self.name + ": EOF exception found" )
875 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400876 main.cleanup()
877 main.exit()
878 except:
kelvin8ec71442015-01-15 16:57:00 -0800879 main.log.info( self.name + " ::::::" )
880 main.log.error( traceback.print_exc() )
881 main.log.info( self.name + " ::::::" )
andrewonlabe6745342014-10-17 14:29:13 -0400882 main.cleanup()
883 main.exit()
884
kelvin-onlabd3b64892015-01-20 13:26:24 -0800885 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800886 """
andrewonlab7b31d232014-10-24 13:31:47 -0400887 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800888 * ingressDevice: device id of ingress device
889 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400890 Optional:
891 TODO: Still needs to be implemented via dev side
kelvin-onlab898a6c62015-01-16 14:13:53 -0800892 """
andrewonlab7b31d232014-10-24 13:31:47 -0400893 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800894 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
895 " " + str( egressDevice )
896 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800897 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800898 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400899 return handle
900 else:
901 return main.TRUE
andrewonlab7b31d232014-10-24 13:31:47 -0400902 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800903 main.log.error( self.name + ": EOF exception found" )
904 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400905 main.cleanup()
906 main.exit()
907 except:
kelvin8ec71442015-01-15 16:57:00 -0800908 main.log.info( self.name + " ::::::" )
909 main.log.error( traceback.print_exc() )
910 main.log.info( self.name + " ::::::" )
andrewonlab7b31d232014-10-24 13:31:47 -0400911 main.cleanup()
912 main.exit()
913
kelvin-onlabd3b64892015-01-20 13:26:24 -0800914 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800915 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800916 ingressDevice,
917 egressDevice,
918 portIngress="",
919 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800920 ethType="",
921 ethSrc="",
922 ethDst="",
923 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -0800924 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800925 ipProto="",
926 ipSrc="",
927 ipDst="",
928 tcpSrc="",
929 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -0800930 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400931 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800932 * ingressDevice: device id of ingress device
933 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -0400934 Optional:
935 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -0800936 * ethSrc: specify ethSrc ( i.e. src mac addr )
937 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500938 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -0800939 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -0500940 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -0800941 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -0500942 * ipSrc: specify ip source address
943 * ipDst: specify ip destination address
944 * tcpSrc: specify tcp source port
945 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -0400946 Description:
kelvin8ec71442015-01-15 16:57:00 -0800947 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -0400948 specifying device id's and optional fields
949
Jon Halle3f39ff2015-01-13 11:50:53 -0800950 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -0400951 options developers provide for point-to-point
952 intent via cli
kelvin8ec71442015-01-15 16:57:00 -0800953 """
andrewonlab4dbb4d82014-10-17 18:22:31 -0400954 try:
andrewonlab289e4b72014-10-21 21:24:18 -0400955 cmd = ""
956
kelvin8ec71442015-01-15 16:57:00 -0800957 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500958 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -0800959 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -0500960 and not ipProto and not ipSrc and not ipDst \
961 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -0500962 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -0500963
andrewonlab289e4b72014-10-21 21:24:18 -0400964 else:
andrewonlab36af3822014-11-18 17:48:18 -0500965 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -0800966
andrewonlab0c0a6772014-10-22 12:31:18 -0400967 if ethType:
kelvin8ec71442015-01-15 16:57:00 -0800968 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -0400969 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -0800970 cmd += " --ethSrc " + str( ethSrc )
971 if ethDst:
972 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -0500973 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -0800974 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800975 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -0500976 cmd += " --lambda "
977 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -0800978 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -0500979 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -0800980 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -0500981 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -0800982 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -0500983 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -0800984 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -0500985 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -0800986 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -0400987
kelvin8ec71442015-01-15 16:57:00 -0800988 # Check whether the user appended the port
989 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -0800990 if "/" in ingressDevice:
991 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -0500992 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800993 if not portIngress:
kelvin8ec71442015-01-15 16:57:00 -0800994 main.log.error( "You must specify " +
995 "the ingress port" )
996 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -0500997 return main.FALSE
998
kelvin8ec71442015-01-15 16:57:00 -0800999 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001000 str( ingressDevice ) + "/" +\
1001 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001002
kelvin-onlabd3b64892015-01-20 13:26:24 -08001003 if "/" in egressDevice:
1004 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001005 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001006 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001007 main.log.error( "You must specify " +
1008 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001009 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001010
kelvin8ec71442015-01-15 16:57:00 -08001011 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001012 str( egressDevice ) + "/" +\
1013 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001014
kelvin-onlab898a6c62015-01-16 14:13:53 -08001015 handle = self.sendline( cmd )
1016 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001017 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001018 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001019 else:
1020 return main.TRUE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001021 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001022 main.log.error( self.name + ": EOF exception found" )
1023 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001024 main.cleanup()
1025 main.exit()
1026 except:
kelvin8ec71442015-01-15 16:57:00 -08001027 main.log.info( self.name + " ::::::" )
1028 main.log.error( traceback.print_exc() )
1029 main.log.info( self.name + " ::::::" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001030 main.cleanup()
1031 main.exit()
1032
kelvin-onlabd3b64892015-01-20 13:26:24 -08001033 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001034 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001035 ingressDevice1,
1036 ingressDevice2,
1037 egressDevice,
1038 portIngress="",
1039 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001040 ethType="",
1041 ethSrc="",
1042 ethDst="",
1043 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001044 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001045 ipProto="",
1046 ipSrc="",
1047 ipDst="",
1048 tcpSrc="",
1049 tcpDst="",
1050 setEthSrc="",
1051 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001052 """
shahshreyad0c80432014-12-04 16:56:05 -08001053 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001054 This function assumes that there would be 2 ingress devices and
1055 one egress device. For more number of ingress devices, this
1056 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001057 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001058 * ingressDevice1: device id of ingress device1
1059 * ingressDevice2: device id of ingress device2
1060 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001061 Optional:
1062 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001063 * ethSrc: specify ethSrc ( i.e. src mac addr )
1064 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001065 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001066 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001067 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001068 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001069 * ipSrc: specify ip source address
1070 * ipDst: specify ip destination address
1071 * tcpSrc: specify tcp source port
1072 * tcpDst: specify tcp destination port
1073 * setEthSrc: action to Rewrite Source MAC Address
1074 * setEthDst: action to Rewrite Destination MAC Address
1075 Description:
kelvin8ec71442015-01-15 16:57:00 -08001076 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001077 specifying device id's and optional fields
1078
Jon Halle3f39ff2015-01-13 11:50:53 -08001079 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001080 options developers provide for multipointpoint-to-singlepoint
1081 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001082 """
shahshreyad0c80432014-12-04 16:56:05 -08001083 try:
1084 cmd = ""
1085
kelvin8ec71442015-01-15 16:57:00 -08001086 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001087 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001088 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001089 and not ipProto and not ipSrc and not ipDst\
1090 and not tcpSrc and not tcpDst and not setEthSrc\
1091 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001092 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001093
1094 else:
1095 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001096
shahshreyad0c80432014-12-04 16:56:05 -08001097 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001098 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001099 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001100 cmd += " --ethSrc " + str( ethSrc )
1101 if ethDst:
1102 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001103 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001104 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001105 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001106 cmd += " --lambda "
1107 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001108 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001109 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001110 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001111 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001112 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001113 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001114 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001115 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001116 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001117 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001118 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001119 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001120 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001121
kelvin8ec71442015-01-15 16:57:00 -08001122 # Check whether the user appended the port
1123 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001124 if "/" in ingressDevice1:
1125 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001126 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001127 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001128 main.log.error( "You must specify " +
1129 "the ingress port1" )
1130 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001131 return main.FALSE
1132
kelvin8ec71442015-01-15 16:57:00 -08001133 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001134 str( ingressDevice1 ) + "/" +\
1135 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001136
kelvin-onlabd3b64892015-01-20 13:26:24 -08001137 if "/" in ingressDevice2:
1138 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001139 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001140 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001141 main.log.error( "You must specify " +
1142 "the ingress port2" )
1143 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001144 return main.FALSE
1145
kelvin8ec71442015-01-15 16:57:00 -08001146 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001147 str( ingressDevice2 ) + "/" +\
1148 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001149
kelvin-onlabd3b64892015-01-20 13:26:24 -08001150 if "/" in egressDevice:
1151 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001152 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001153 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001154 main.log.error( "You must specify " +
1155 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001156 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001157
kelvin8ec71442015-01-15 16:57:00 -08001158 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001159 str( egressDevice ) + "/" +\
1160 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001161 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001162 handle = self.sendline( cmd )
1163 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001164 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001165 return self.handle
1166 else:
1167 return main.TRUE
shahshreyad0c80432014-12-04 16:56:05 -08001168 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001169 main.log.error( self.name + ": EOF exception found" )
1170 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001171 main.cleanup()
1172 main.exit()
1173 except:
kelvin8ec71442015-01-15 16:57:00 -08001174 main.log.info( self.name + " ::::::" )
1175 main.log.error( traceback.print_exc() )
1176 main.log.info( self.name + " ::::::" )
shahshreyad0c80432014-12-04 16:56:05 -08001177 main.cleanup()
1178 main.exit()
1179
kelvin-onlabd3b64892015-01-20 13:26:24 -08001180 def removeIntent( self, intentId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001181 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001182 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001183
1184 Returns:
1185 main.False on error and
1186 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001187 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001188 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001189 cmdStr = "remove-intent " + str( intentId )
1190 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001191 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001192 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001193 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001194 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001195 # TODO: Should this be main.TRUE
1196 return handle
andrewonlab9a50dfe2014-10-17 17:22:31 -04001197 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001198 main.log.error( self.name + ": EOF exception found" )
1199 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001200 main.cleanup()
1201 main.exit()
1202 except:
kelvin8ec71442015-01-15 16:57:00 -08001203 main.log.info( self.name + " ::::::" )
1204 main.log.error( traceback.print_exc() )
1205 main.log.info( self.name + " ::::::" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001206 main.cleanup()
1207 main.exit()
1208
kelvin-onlabd3b64892015-01-20 13:26:24 -08001209 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001210 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001211 NOTE: This method should be used after installing application:
1212 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001213 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001214 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001215 Description:
1216 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001217 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001218 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001219 if jsonFormat:
1220 cmdStr = "routes -j"
1221 handleTmp = self.sendline( cmdStr )
1222 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1223 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001224 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001225 cmdStr = "routes"
1226 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001227 return handle
pingping-lin8b306ac2014-11-17 18:13:51 -08001228 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001229 main.log.error( self.name + ": EOF exception found" )
1230 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001231 main.cleanup()
1232 main.exit()
1233 except:
kelvin8ec71442015-01-15 16:57:00 -08001234 main.log.info( self.name + " ::::::" )
1235 main.log.error( traceback.print_exc() )
1236 main.log.info( self.name + " ::::::" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001237 main.cleanup()
1238 main.exit()
1239
kelvin-onlabd3b64892015-01-20 13:26:24 -08001240 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001241 """
andrewonlab377693f2014-10-21 16:00:30 -04001242 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001243 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001244 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001245 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001246 """
andrewonlabe6745342014-10-17 14:29:13 -04001247 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001248 if jsonFormat:
1249 cmdStr = "intents -j"
1250 handle = self.sendline( cmdStr )
1251 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1252 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001253 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001254 cmdStr = "intents"
1255 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001256 return handle
andrewonlabe6745342014-10-17 14:29:13 -04001257 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001258 main.log.error( self.name + ": EOF exception found" )
1259 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001260 main.cleanup()
1261 main.exit()
1262 except:
kelvin8ec71442015-01-15 16:57:00 -08001263 main.log.info( self.name + " ::::::" )
1264 main.log.error( traceback.print_exc() )
1265 main.log.info( self.name + " ::::::" )
andrewonlabe6745342014-10-17 14:29:13 -04001266 main.cleanup()
1267 main.exit()
1268
kelvin-onlabd3b64892015-01-20 13:26:24 -08001269 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001270 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001271 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001272 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001273 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001274 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001275 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001276 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001277 if jsonFormat:
1278 cmdStr = "flows -j"
1279 handle = self.sendline( cmdStr )
1280 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1281 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001282 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001283 cmdStr = "flows"
1284 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001285 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001286 main.log.error( self.name + ".flows() response: " +
1287 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001288 return handle
Shreya Shah0f01c812014-10-26 20:15:28 -04001289 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001290 main.log.error( self.name + ": EOF exception found" )
1291 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001292 main.cleanup()
1293 main.exit()
1294 except:
kelvin8ec71442015-01-15 16:57:00 -08001295 main.log.info( self.name + " ::::::" )
1296 main.log.error( traceback.print_exc() )
1297 main.log.info( self.name + " ::::::" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001298 main.cleanup()
1299 main.exit()
1300
kelvin-onlabd3b64892015-01-20 13:26:24 -08001301 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
1302 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001303 """
andrewonlab87852b02014-11-19 18:44:19 -05001304 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001305 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001306 a specific point-to-point intent definition
1307 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001308 * dpidSrc: specify source dpid
1309 * dpidDst: specify destination dpid
1310 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001311 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001312 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001313 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001314 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001315 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001316 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001317 """
andrewonlab87852b02014-11-19 18:44:19 -05001318 try:
kelvin8ec71442015-01-15 16:57:00 -08001319 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001320 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1321 str( numIntents )
1322 if numMult:
1323 cmd += " " + str( numMult )
1324 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001325 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001326 if appId:
1327 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001328 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001329 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1330 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001331 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001332 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001333 main.log.info( handle )
1334 # Split result by newline
1335 newline = handle.split( "\r\r\n" )
1336 # Ignore the first object of list, which is empty
1337 newline = newline[ 1: ]
1338 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001339 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001340 result = result.split( ": " )
1341 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001342 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1343 main.log.info( latResult )
1344 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001345 else:
1346 return main.TRUE
andrewonlab87852b02014-11-19 18:44:19 -05001347 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001348 main.log.error( self.name + ": EOF exception found" )
1349 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001350 main.cleanup()
1351 main.exit()
1352 except:
kelvin8ec71442015-01-15 16:57:00 -08001353 main.log.info( self.name + " ::::::" )
1354 main.log.error( traceback.print_exc() )
1355 main.log.info( self.name + " ::::::" )
andrewonlab87852b02014-11-19 18:44:19 -05001356 main.cleanup()
1357 main.exit()
1358
kelvin-onlabd3b64892015-01-20 13:26:24 -08001359 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001360 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001361 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001362 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001363 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001364 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001365 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001366 if jsonFormat:
1367 cmdStr = "intents-events-metrics -j"
1368 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001369 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001370 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1371 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001372 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001373 cmdStr = "intents-events-metrics"
1374 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001375 return handle
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001376 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001377 main.log.error( self.name + ": EOF exception found" )
1378 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001379 main.cleanup()
1380 main.exit()
1381 except:
kelvin8ec71442015-01-15 16:57:00 -08001382 main.log.info( self.name + " ::::::" )
1383 main.log.error( traceback.print_exc() )
1384 main.log.info( self.name + " ::::::" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001385 main.cleanup()
1386 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001387
kelvin-onlabd3b64892015-01-20 13:26:24 -08001388 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001389 """
1390 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001391 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001392 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001393 """
andrewonlab867212a2014-10-22 20:13:38 -04001394 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001395 if jsonFormat:
1396 cmdStr = "topology-events-metrics -j"
1397 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001398 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001399 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1400 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001401 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001402 cmdStr = "topology-events-metrics"
1403 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001404 return handle
andrewonlab867212a2014-10-22 20:13:38 -04001405 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001406 main.log.error( self.name + ": EOF exception found" )
1407 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001408 main.cleanup()
1409 main.exit()
1410 except:
kelvin8ec71442015-01-15 16:57:00 -08001411 main.log.info( self.name + " ::::::" )
1412 main.log.error( traceback.print_exc() )
1413 main.log.info( self.name + " ::::::" )
andrewonlab867212a2014-10-22 20:13:38 -04001414 main.cleanup()
1415 main.exit()
1416
kelvin8ec71442015-01-15 16:57:00 -08001417 # Wrapper functions ****************
1418 # Wrapper functions use existing driver
1419 # functions and extends their use case.
1420 # For example, we may use the output of
1421 # a normal driver function, and parse it
1422 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001423
kelvin-onlabd3b64892015-01-20 13:26:24 -08001424 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001425 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001426 Description:
1427 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001428 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001429 try:
kelvin8ec71442015-01-15 16:57:00 -08001430 # Obtain output of intents function
kelvin-onlabd3b64892015-01-20 13:26:24 -08001431 intentsStr = self.intents()
1432 allIntentList = []
1433 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001434
kelvin8ec71442015-01-15 16:57:00 -08001435 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001436 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1437 for intents in intentsList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001438 if "onos>" in intents:
1439 continue
1440 elif "intents" in intents:
1441 continue
1442 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001443 lineList = intents.split( " " )
1444 allIntentList.append( lineList[ 0 ] )
kelvin8ec71442015-01-15 16:57:00 -08001445
kelvin-onlabd3b64892015-01-20 13:26:24 -08001446 allIntentList = allIntentList[ 1:-2 ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001447
kelvin-onlabd3b64892015-01-20 13:26:24 -08001448 for intents in allIntentList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001449 if not intents:
1450 continue
1451 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001452 intentIdList.append( intents )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001453
kelvin-onlabd3b64892015-01-20 13:26:24 -08001454 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001455
1456 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001457 main.log.error( self.name + ": EOF exception found" )
1458 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001459 main.cleanup()
1460 main.exit()
1461 except:
kelvin8ec71442015-01-15 16:57:00 -08001462 main.log.info( self.name + " ::::::" )
1463 main.log.error( traceback.print_exc() )
1464 main.log.info( self.name + " ::::::" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001465 main.cleanup()
1466 main.exit()
1467
kelvin-onlabd3b64892015-01-20 13:26:24 -08001468 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001469 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001470 Use 'devices' function to obtain list of all devices
1471 and parse the result to obtain a list of all device
1472 id's. Returns this list. Returns empty list if no
1473 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001474 List is ordered sequentially
1475
andrewonlab3e15ead2014-10-15 14:21:34 -04001476 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001477 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001478 the ids. By obtaining the list of device ids on the fly,
1479 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001480 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001481 try:
kelvin8ec71442015-01-15 16:57:00 -08001482 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001483 devicesStr = self.devices( jsonFormat=False )
1484 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001485
kelvin-onlabd3b64892015-01-20 13:26:24 -08001486 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001487 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001488 return idList
kelvin8ec71442015-01-15 16:57:00 -08001489
1490 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001491 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001492 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001493 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001494 # Split list further into arguments before and after string
1495 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001496 # append to idList
1497 for arg in tempList:
1498 idList.append( arg.split( "id=" )[ 1 ] )
1499 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001500
1501 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001502 main.log.error( self.name + ": EOF exception found" )
1503 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001504 main.cleanup()
1505 main.exit()
1506 except:
kelvin8ec71442015-01-15 16:57:00 -08001507 main.log.info( self.name + " ::::::" )
1508 main.log.error( traceback.print_exc() )
1509 main.log.info( self.name + " ::::::" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001510 main.cleanup()
1511 main.exit()
1512
kelvin-onlabd3b64892015-01-20 13:26:24 -08001513 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001514 """
andrewonlab7c211572014-10-15 16:45:20 -04001515 Uses 'nodes' function to obtain list of all nodes
1516 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001517 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001518 Returns:
1519 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001520 """
andrewonlab7c211572014-10-15 16:45:20 -04001521 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001522 nodesStr = self.nodes()
1523 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001524
kelvin-onlabd3b64892015-01-20 13:26:24 -08001525 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001526 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001527 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001528
kelvin-onlabd3b64892015-01-20 13:26:24 -08001529 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001530 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001531
kelvin8ec71442015-01-15 16:57:00 -08001532 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001533 nodesList = nodesStr.split( "," )
1534 tempList = [ node for node in nodesList if "id=" in node ]
1535 for arg in tempList:
1536 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001537
kelvin-onlabd3b64892015-01-20 13:26:24 -08001538 return idList
kelvin8ec71442015-01-15 16:57:00 -08001539
andrewonlab7c211572014-10-15 16:45:20 -04001540 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001541 main.log.error( self.name + ": EOF exception found" )
1542 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001543 main.cleanup()
1544 main.exit()
1545 except:
kelvin8ec71442015-01-15 16:57:00 -08001546 main.log.info( self.name + " ::::::" )
1547 main.log.error( traceback.print_exc() )
1548 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -04001549 main.cleanup()
1550 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001551
kelvin-onlabd3b64892015-01-20 13:26:24 -08001552 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001553 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001554 Return the first device from the devices api whose 'id' contains 'dpid'
1555 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001556 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001557 import json
1558 try:
kelvin8ec71442015-01-15 16:57:00 -08001559 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001560 return None
1561 else:
kelvin8ec71442015-01-15 16:57:00 -08001562 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001563 rawDevices = self.devices()
1564 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001565 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001566 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001567 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1568 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001569 return device
1570 return None
1571 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001572 main.log.error( self.name + ": EOF exception found" )
1573 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001574 main.cleanup()
1575 main.exit()
1576 except:
kelvin8ec71442015-01-15 16:57:00 -08001577 main.log.info( self.name + " ::::::" )
1578 main.log.error( traceback.print_exc() )
1579 main.log.info( self.name + " ::::::" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001580 main.cleanup()
1581 main.exit()
1582
kelvin-onlabd3b64892015-01-20 13:26:24 -08001583 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001584 """
1585 Checks the number of swithes & links that ONOS sees against the
1586 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001587 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001588
Jon Hall42db6dc2014-10-24 19:03:48 -04001589 Params: ip = ip used for the onos cli
1590 numoswitch = expected number of switches
1591 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001592 logLevel = level to log to. Currently accepts
1593 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001594
1595
kelvin-onlabd3b64892015-01-20 13:26:24 -08001596 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001597
kelvin8ec71442015-01-15 16:57:00 -08001598 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001599 main.FALSE if the numer of switches and links is incorrect,
1600 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001601 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001602 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001603 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001604 if topology == {}:
1605 return main.ERROR
1606 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001607 # Is the number of switches is what we expected
1608 devices = topology.get( 'devices', False )
1609 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001610 if devices == False or links == False:
1611 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001612 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001613 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001614 linkCheck = ( int( links ) == int( numolink ) )
1615 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001616 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001617 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001618 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001619 result = main.TRUE
1620 else:
1621 output = output + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001622 "The number of links and switches does not matc\
1623 h what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001624 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001625 output = output + "\n ONOS sees %i devices (%i expected) \
1626 and %i links (%i expected)" % (
1627 int( devices ), int( numoswitch ), int( links ),
1628 int( numolink ) )
1629 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001630 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001631 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001632 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001633 else:
kelvin8ec71442015-01-15 16:57:00 -08001634 main.log.info( output )
1635 return result
Jon Hall42db6dc2014-10-24 19:03:48 -04001636 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001637 main.log.error( self.name + ": EOF exception found" )
1638 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001639 main.cleanup()
1640 main.exit()
1641 except:
kelvin8ec71442015-01-15 16:57:00 -08001642 main.log.info( self.name + " ::::::" )
1643 main.log.error( traceback.print_exc() )
1644 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001645 main.cleanup()
1646 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001647
kelvin-onlabd3b64892015-01-20 13:26:24 -08001648 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001649 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001650 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001651 deviceId must be the id of a device as seen in the onos devices command
1652 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001653 role must be either master, standby, or none
1654
Jon Halle3f39ff2015-01-13 11:50:53 -08001655 Returns:
1656 main.TRUE or main.FALSE based on argument verification and
1657 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001658 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001659 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001660 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001661 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001662 cmdStr = "device-role " +\
1663 str( deviceId ) + " " +\
1664 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001665 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001666 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001667 if re.search( "Error", handle ):
1668 # end color output to escape any colours
1669 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001670 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001671 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001672 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001673 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001674 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001675 main.log.error( "Invalid 'role' given to device_role(). " +
1676 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001677 return main.FALSE
Jon Hall1c9e8732014-10-27 19:29:27 -04001678 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001679 main.log.error( self.name + ": EOF exception found" )
1680 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001681 main.cleanup()
1682 main.exit()
1683 except:
kelvin8ec71442015-01-15 16:57:00 -08001684 main.log.info( self.name + " ::::::" )
1685 main.log.error( traceback.print_exc() )
1686 main.log.info( self.name + " ::::::" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001687 main.cleanup()
1688 main.exit()
1689
kelvin-onlabd3b64892015-01-20 13:26:24 -08001690 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001691 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001692 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001693 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001694 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001695 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001696 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001697 if jsonFormat:
1698 cmdStr = "clusters -j"
1699 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001700 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001701 handle variable here contains some ANSI escape color code
1702 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001703 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001704 function. The repr( handle ) output when printed shows the ANSI
1705 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001706 variable is actually repr( somestring ) and json.loads would
1707 fail with the escape sequence. So we take off that escape
1708 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001709
kelvin-onlabd3b64892015-01-20 13:26:24 -08001710 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1711 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001712 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001713 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1714 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001715 return handle1
1716 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001717 cmdStr = "clusters"
1718 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001719 return handle
1720 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001721 main.log.error( self.name + ": EOF exception found" )
1722 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001723 main.cleanup()
1724 main.exit()
1725 except:
kelvin8ec71442015-01-15 16:57:00 -08001726 main.log.info( self.name + " ::::::" )
1727 main.log.error( traceback.print_exc() )
1728 main.log.info( self.name + " ::::::" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001729 main.cleanup()
1730 main.exit()
1731
kelvin-onlabd3b64892015-01-20 13:26:24 -08001732 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001733 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001734 CLI command to get the current leader for the Election test application
1735 NOTE: Requires installation of the onos-app-election feature
1736 Returns: Node IP of the leader if one exists
1737 None if none exists
1738 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001739 """
Jon Hall94fd0472014-12-08 11:52:42 -08001740 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001741 cmdStr = "election-test-leader"
1742 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001743 # Leader
1744 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001745 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001746 nodeSearch = re.search( leaderPattern, response )
1747 if nodeSearch:
1748 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001749 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001750 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001751 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001752 # no leader
1753 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001754 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001755 nullSearch = re.search( nullPattern, response )
1756 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001757 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001758 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001759 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001760 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001761 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001762 if re.search( errorPattern, response ):
1763 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001764 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001765 return main.FALSE
1766 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001767 main.log.error( "Error in election_test_leader: " +
1768 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001769 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001770 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001771 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001772 main.log.error( self.name + ": EOF exception found" )
1773 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001774 main.cleanup()
1775 main.exit()
1776 except:
kelvin8ec71442015-01-15 16:57:00 -08001777 main.log.info( self.name + " ::::::" )
1778 main.log.error( traceback.print_exc() )
1779 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001780 main.cleanup()
1781 main.exit()
1782
kelvin-onlabd3b64892015-01-20 13:26:24 -08001783 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001784 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001785 CLI command to run for leadership of the Election test application.
1786 NOTE: Requires installation of the onos-app-election feature
1787 Returns: Main.TRUE on success
1788 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001789 """
Jon Hall94fd0472014-12-08 11:52:42 -08001790 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001791 cmdStr = "election-test-run"
1792 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001793 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001794 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001795 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001796 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001797 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001798 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001799 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001800 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001801 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001802 errorPattern = "Command\snot\sfound"
1803 if re.search( errorPattern, response ):
1804 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001805 return main.FALSE
1806 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001807 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001808 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001809 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001810 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001811 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001812 main.log.error( self.name + ": EOF exception found" )
1813 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001814 main.cleanup()
1815 main.exit()
1816 except:
kelvin8ec71442015-01-15 16:57:00 -08001817 main.log.info( self.name + " ::::::" )
1818 main.log.error( traceback.print_exc() )
1819 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001820 main.cleanup()
1821 main.exit()
1822
kelvin-onlabd3b64892015-01-20 13:26:24 -08001823 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08001824 """
Jon Hall94fd0472014-12-08 11:52:42 -08001825 * CLI command to withdraw the local node from leadership election for
1826 * the Election test application.
1827 #NOTE: Requires installation of the onos-app-election feature
1828 Returns: Main.TRUE on success
1829 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001830 """
Jon Hall94fd0472014-12-08 11:52:42 -08001831 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001832 cmdStr = "election-test-withdraw"
1833 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001834 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001835 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001836 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001837 if re.search( successPattern, response ):
1838 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001839 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001840 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001841 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001842 errorPattern = "Command\snot\sfound"
1843 if re.search( errorPattern, response ):
1844 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001845 return main.FALSE
1846 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001847 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001848 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001849 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001850 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001851 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001852 main.log.error( self.name + ": EOF exception found" )
1853 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001854 main.cleanup()
1855 main.exit()
1856 except:
kelvin8ec71442015-01-15 16:57:00 -08001857 main.log.info( self.name + " ::::::" )
1858 main.log.error( traceback.print_exc() )
1859 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001860 main.cleanup()
1861 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001862
kelvin8ec71442015-01-15 16:57:00 -08001863 def getDevicePortsEnabledCount( self, dpid ):
1864 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001865 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001866 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001867 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001868 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001869 cmdStr = "onos:ports -e " + dpid + " | wc -l"
1870 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001871 if re.search( "No such device", output ):
1872 main.log.error( "Error in getting ports" )
1873 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001874 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001875 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001876 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001877 main.log.error( self.name + ": EOF exception found" )
1878 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001879 main.cleanup()
1880 main.exit()
1881 except:
kelvin8ec71442015-01-15 16:57:00 -08001882 main.log.info( self.name + " ::::::" )
1883 main.log.error( traceback.print_exc() )
1884 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001885 main.cleanup()
1886 main.exit()
1887
kelvin8ec71442015-01-15 16:57:00 -08001888 def getDeviceLinksActiveCount( self, dpid ):
1889 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001890 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001891 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001892 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001893 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001894 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
1895 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001896 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001897 main.log.error( "Error in getting ports " )
1898 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001899 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001900 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001901 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001902 main.log.error( self.name + ": EOF exception found" )
1903 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001904 main.cleanup()
1905 main.exit()
1906 except:
kelvin8ec71442015-01-15 16:57:00 -08001907 main.log.info( self.name + " ::::::" )
1908 main.log.error( traceback.print_exc() )
1909 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001910 main.cleanup()
1911 main.exit()
1912
kelvin8ec71442015-01-15 16:57:00 -08001913 def getAllIntentIds( self ):
1914 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001915 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08001916 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001917 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001918 cmdStr = "onos:intents | grep id="
1919 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001920 if re.search( "Error", output ):
1921 main.log.error( "Error in getting ports" )
1922 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001923 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001924 return output
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001925 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001926 main.log.error( self.name + ": EOF exception found" )
1927 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001928 main.cleanup()
1929 main.exit()
1930 except:
kelvin8ec71442015-01-15 16:57:00 -08001931 main.log.info( self.name + " ::::::" )
1932 main.log.error( traceback.print_exc() )
1933 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001934 main.cleanup()
1935 main.exit()