blob: 32fcc35dff7ec426413bf7c9a9b47f9eeb7f8f29 [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 -080023# import os.path
24sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040025from drivers.common.clidriver import CLI
26
andrewonlab95ce8322014-10-13 14:12:04 -040027
kelvin8ec71442015-01-15 16:57:00 -080028class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040029
kelvin8ec71442015-01-15 16:57:00 -080030 def __init__( self ):
31 """
32 Initialize client
33 """
34 super( CLI, self ).__init__()
35
36 def connect( self, **connectargs ):
37 """
andrewonlab95ce8322014-10-13 14:12:04 -040038 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080039 """
andrewonlab95ce8322014-10-13 14:12:04 -040040 try:
41 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080042 vars( self )[ key ] = connectargs[ key ]
andrewonlab95ce8322014-10-13 14:12:04 -040043 self.home = "~/ONOS"
44 for key in self.options:
45 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080046 self.home = self.options[ 'home' ]
andrewonlab95ce8322014-10-13 14:12:04 -040047 break
48
kelvin8ec71442015-01-15 16:57:00 -080049 self.name = self.options[ 'name' ]
50 self.handle = super( OnosCliDriver, self ).connect(
51 user_name=self.user_name,
52 ip_address=self.ip_address,
53 port=self.port,
54 pwd=self.pwd,
55 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040056
kelvin8ec71442015-01-15 16:57:00 -080057 self.handle.sendline( "cd " + self.home )
58 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040059 if self.handle:
60 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080061 else:
62 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040063 return main.FALSE
64 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080065 main.log.error( self.name + ": EOF exception found" )
66 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040067 main.cleanup()
68 main.exit()
69 except:
kelvin8ec71442015-01-15 16:57:00 -080070 main.log.info( self.name + ":::::::::::::::::::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -040071 main.log.error( traceback.print_exc() )
kelvin8ec71442015-01-15 16:57:00 -080072 main.log.info( ":::::::::::::::::::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -040073 main.cleanup()
74 main.exit()
75
kelvin8ec71442015-01-15 16:57:00 -080076 def disconnect( self ):
77 """
andrewonlab95ce8322014-10-13 14:12:04 -040078 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080079 """
andrewonlab95ce8322014-10-13 14:12:04 -040080 response = ''
81 try:
kelvin8ec71442015-01-15 16:57:00 -080082 self.handle.sendline( "" )
83 i = self.handle.expect( [ "onos>", "\$" ] )
Jon Hall7e5b9172014-10-22 12:32:47 -040084 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -080085 self.handle.sendline( "system:shutdown" )
86 self.handle.expect( "Confirm" )
87 self.handle.sendline( "yes" )
88 self.handle.expect( "\$" )
89 self.handle.sendline( "" )
90 self.handle.expect( "\$" )
91 self.handle.sendline( "exit" )
92 self.handle.expect( "closed" )
andrewonlabc2d05aa2014-10-13 16:51:10 -040093
andrewonlab95ce8322014-10-13 14:12:04 -040094 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080095 main.log.error( self.name + ": EOF exception found" )
96 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040097 except:
kelvin8ec71442015-01-15 16:57:00 -080098 main.log.error( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -040099 response = main.FALSE
100 return response
101
kelvin8ec71442015-01-15 16:57:00 -0800102 def logout( self ):
103 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500104 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800105 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500106 try:
kelvin8ec71442015-01-15 16:57:00 -0800107 self.handle.sendline( "" )
108 i = self.handle.expect( [
andrewonlab9627f432014-11-14 12:45:10 -0500109 "onos>",
kelvin8ec71442015-01-15 16:57:00 -0800110 "\$" ], timeout=10 )
andrewonlab9627f432014-11-14 12:45:10 -0500111 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800112 self.handle.sendline( "logout" )
113 self.handle.expect( "\$" )
andrewonlab9627f432014-11-14 12:45:10 -0500114 elif i == 1:
115 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800116
andrewonlab38d2b4a2014-11-13 16:28:47 -0500117 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800118 main.log.error( self.name + ": eof exception found" )
119 main.log.error( self.name + ": " +
120 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500121 main.cleanup()
122 main.exit()
123 except:
kelvin8ec71442015-01-15 16:57:00 -0800124 main.log.info( self.name + " ::::::" )
125 main.log.error( traceback.print_exc() )
126 main.log.info( self.name + " ::::::" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500127 main.cleanup()
128 main.exit()
129
kelvin8ec71442015-01-15 16:57:00 -0800130 def set_cell( self, cellname ):
131 """
andrewonlab95ce8322014-10-13 14:12:04 -0400132 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800133
andrewonlab95ce8322014-10-13 14:12:04 -0400134 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800135 """
andrewonlab95ce8322014-10-13 14:12:04 -0400136 try:
137 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800138 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400139 main.cleanup()
140 main.exit()
141 else:
kelvin8ec71442015-01-15 16:57:00 -0800142 self.handle.sendline( "cell " + str( cellname ) )
143 # Expect the cellname in the ONOS_CELL variable.
144 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400145 # and that this driver will have to change accordingly
kelvin8ec71442015-01-15 16:57:00 -0800146 self.handle.expect( "ONOS_CELL=" + str( cellname ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400147 handle_before = self.handle.before
148 handle_after = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800149 # Get the rest of the handle
150 self.handle.sendline( "" )
151 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -0400152 handle_more = self.handle.before
153
kelvin8ec71442015-01-15 16:57:00 -0800154 main.log.info( "Cell call returned: " + handle_before +
155 handle_after + handle_more )
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
171 def start_onos_cli( self, ONOS_ip, karafTimeout="" ):
172 """
173 karafTimeout is an optional arugument. karafTimeout value passed by user would be used to set the
Hari Krishnad7b9c202015-01-05 10:38:14 -0800174 current karaf shell idle timeout. Note that when ever this property is modified the shell will exit and
175 the subsequent login would reflect new idle timeout.
kelvin8ec71442015-01-15 16:57:00 -0800176 Below is an example to start a session with 60 seconds idle timeout ( input value is in milliseconds ):
177
Hari Krishna25d42f72015-01-05 15:08:28 -0800178 tValue = "60000"
kelvin8ec71442015-01-15 16:57:00 -0800179 main.ONOScli1.start_onos_cli( ONOS_ip, karafTimeout=tValue )
180
Hari Krishna25d42f72015-01-05 15:08:28 -0800181 Note: karafTimeout is left as str so that this could be read and passed to start_onos_cli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800182 """
andrewonlab95ce8322014-10-13 14:12:04 -0400183 try:
kelvin8ec71442015-01-15 16:57:00 -0800184 self.handle.sendline( "" )
185 x = self.handle.expect( [
186 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500187
188 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800189 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500190 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400191
kelvin8ec71442015-01-15 16:57:00 -0800192 # Wait for onos start ( -w ) and enter onos cli
193 self.handle.sendline( "onos -w " + str( ONOS_ip ) )
194 i = self.handle.expect( [
195 "onos>",
196 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400197
198 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800199 main.log.info( str( ONOS_ip ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800200 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800201 self.handle.sendline(
202 "config:property-set -p org.apache.karaf.shell sshIdleTimeout " +
203 karafTimeout )
204 self.handle.expect( "\$" )
205 self.handle.sendline( "onos -w " + str( ONOS_ip ) )
206 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400207 return main.TRUE
208 else:
kelvin8ec71442015-01-15 16:57:00 -0800209 # If failed, send ctrl+c to process and try again
210 main.log.info( "Starting CLI failed. Retrying..." )
211 self.handle.send( "\x03" )
212 self.handle.sendline( "onos -w " + str( ONOS_ip ) )
213 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
214 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400215 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800216 main.log.info( str( ONOS_ip ) + " CLI Started " +
217 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800218 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800219 self.handle.sendline(
220 "config:property-set -p org.apache.karaf.shell sshIdleTimeout " +
221 karafTimeout )
222 self.handle.expect( "\$" )
223 self.handle.sendline( "onos -w " + str( ONOS_ip ) )
224 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400225 return main.TRUE
226 else:
kelvin8ec71442015-01-15 16:57:00 -0800227 main.log.error( "Connection to CLI " +
228 str( ONOS_ip ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400229 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400230
231 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800232 main.log.error( self.name + ": EOF exception found" )
233 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400234 main.cleanup()
235 main.exit()
236 except:
kelvin8ec71442015-01-15 16:57:00 -0800237 main.log.info( self.name + " ::::::" )
238 main.log.error( traceback.print_exc() )
239 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400240 main.cleanup()
241 main.exit()
242
kelvin8ec71442015-01-15 16:57:00 -0800243 def sendline( self, cmd_str ):
244 """
245 Send a completely user specified string to
246 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400247 a very specific command to send.
kelvin8ec71442015-01-15 16:57:00 -0800248
andrewonlaba18f6bf2014-10-13 19:31:54 -0400249 Warning: There are no sanity checking to commands
250 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800251 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400252 try:
kelvin8ec71442015-01-15 16:57:00 -0800253 self.handle.sendline( "" )
254 self.handle.expect( "onos>" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400255
kelvin8ec71442015-01-15 16:57:00 -0800256 self.handle.sendline( cmd_str )
257 self.handle.expect( "onos>" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400258
259 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800260
261 self.handle.sendline( "" )
262 self.handle.expect( "onos>" )
263
andrewonlaba18f6bf2014-10-13 19:31:54 -0400264 handle += self.handle.before
265 handle += self.handle.after
266
kelvin8ec71442015-01-15 16:57:00 -0800267 main.log.info( "Command sent." )
268 ansi_escape = re.compile( r'\x1b[^m]*m' )
269 handle = ansi_escape.sub( '', handle )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400270
271 return handle
272 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800273 main.log.error( self.name + ": EOF exception found" )
274 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400275 main.cleanup()
276 main.exit()
277 except:
kelvin8ec71442015-01-15 16:57:00 -0800278 main.log.info( self.name + " ::::::" )
279 main.log.error( traceback.print_exc() )
280 main.log.info( self.name + " ::::::" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400281 main.cleanup()
282 main.exit()
283
kelvin8ec71442015-01-15 16:57:00 -0800284 # IMPORTANT NOTE:
285 # For all cli commands, naming convention should match
286 # the cli command replacing ':' with '_'.
287 # Ex ) onos:topology > onos_topology
andrewonlab95ce8322014-10-13 14:12:04 -0400288 # onos:links > onos_links
289 # feature:list > feature_list
kelvin8ec71442015-01-15 16:57:00 -0800290
291 def add_node( self, node_id, ONOS_ip, tcp_port="" ):
292 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400293 Adds a new cluster node by ID and address information.
294 Required:
295 * node_id
296 * ONOS_ip
297 Optional:
298 * tcp_port
kelvin8ec71442015-01-15 16:57:00 -0800299 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400300 try:
kelvin8ec71442015-01-15 16:57:00 -0800301 self.handle.sendline( "" )
302 self.handle.expect( "onos>" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400303
kelvin8ec71442015-01-15 16:57:00 -0800304 self.handle.sendline( "add-node " +
305 str( node_id ) + " " +
306 str( ONOS_ip ) + " " +
307 str( tcp_port ) )
308
309 i = self.handle.expect( [
andrewonlabc2d05aa2014-10-13 16:51:10 -0400310 "Error",
kelvin8ec71442015-01-15 16:57:00 -0800311 "onos>" ] )
312
313 # Clear handle to get previous output
314 self.handle.sendline( "" )
315 self.handle.expect( "onos>" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400316
317 handle = self.handle.before
318
319 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800320 main.log.error( "Error in adding node" )
321 main.log.error( handle )
322 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400323 else:
kelvin8ec71442015-01-15 16:57:00 -0800324 main.log.info( "Node " + str( ONOS_ip ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400325 return main.TRUE
326
327 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800328 main.log.error( self.name + ": EOF exception found" )
329 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400330 main.cleanup()
331 main.exit()
332 except:
kelvin8ec71442015-01-15 16:57:00 -0800333 main.log.info( self.name + " ::::::" )
334 main.log.error( traceback.print_exc() )
335 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400336 main.cleanup()
337 main.exit()
338
kelvin8ec71442015-01-15 16:57:00 -0800339 def remove_node( self, node_id ):
340 """
andrewonlab86dc3082014-10-13 18:18:38 -0400341 Removes a cluster by ID
342 Issues command: 'remove-node [<node-id>]'
343 Required:
344 * node_id
kelvin8ec71442015-01-15 16:57:00 -0800345 """
andrewonlab86dc3082014-10-13 18:18:38 -0400346 try:
kelvin8ec71442015-01-15 16:57:00 -0800347 self.handle.sendline( "" )
348 self.handle.expect( "onos>" )
andrewonlab86dc3082014-10-13 18:18:38 -0400349
kelvin8ec71442015-01-15 16:57:00 -0800350 self.handle.sendline( "remove-node " + str( node_id ) )
351 self.handle.expect( "onos>" )
andrewonlab86dc3082014-10-13 18:18:38 -0400352
353 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800354
andrewonlab86dc3082014-10-13 18:18:38 -0400355 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800356 main.log.error( self.name + ": EOF exception found" )
357 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400358 main.cleanup()
359 main.exit()
360 except:
kelvin8ec71442015-01-15 16:57:00 -0800361 main.log.info( self.name + " ::::::" )
362 main.log.error( traceback.print_exc() )
363 main.log.info( self.name + " ::::::" )
andrewonlab86dc3082014-10-13 18:18:38 -0400364 main.cleanup()
365 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400366
kelvin8ec71442015-01-15 16:57:00 -0800367 def nodes( self ):
368 """
andrewonlab7c211572014-10-15 16:45:20 -0400369 List the nodes currently visible
370 Issues command: 'nodes'
371 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800372 """
andrewonlab7c211572014-10-15 16:45:20 -0400373 try:
kelvin8ec71442015-01-15 16:57:00 -0800374 self.handle.sendline( "" )
375 self.handle.expect( "onos>" )
andrewonlab7c211572014-10-15 16:45:20 -0400376
kelvin8ec71442015-01-15 16:57:00 -0800377 self.handle.sendline( "nodes" )
378 self.handle.expect( "onos>" )
andrewonlab7c211572014-10-15 16:45:20 -0400379
kelvin8ec71442015-01-15 16:57:00 -0800380 self.handle.sendline( "" )
381 self.handle.expect( "onos>" )
andrewonlab7c211572014-10-15 16:45:20 -0400382
383 handle = self.handle.before
384
385 return handle
386
387 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800388 main.log.error( self.name + ": EOF exception found" )
389 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400390 main.cleanup()
391 main.exit()
392 except:
kelvin8ec71442015-01-15 16:57:00 -0800393 main.log.info( self.name + " ::::::" )
394 main.log.error( traceback.print_exc() )
395 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -0400396 main.cleanup()
397 main.exit()
398
kelvin8ec71442015-01-15 16:57:00 -0800399 def topology( self ):
400 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400401 Shows the current state of the topology
402 by issusing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800403 """
andrewonlab95ce8322014-10-13 14:12:04 -0400404 try:
kelvin8ec71442015-01-15 16:57:00 -0800405 self.handle.sendline( "" )
406 self.handle.expect( "onos>" )
407 # either onos:topology or 'topology' will work in CLI
408 self.handle.sendline( "onos:topology" )
409 self.handle.expect( "onos>" )
andrewonlab95ce8322014-10-13 14:12:04 -0400410
411 handle = self.handle.before
412
kelvin8ec71442015-01-15 16:57:00 -0800413 main.log.info( "onos:topology returned: " +
414 str( handle ) )
415
andrewonlab95ce8322014-10-13 14:12:04 -0400416 return handle
kelvin8ec71442015-01-15 16:57:00 -0800417
andrewonlab95ce8322014-10-13 14:12:04 -0400418 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800419 main.log.error( self.name + ": EOF exception found" )
420 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400421 main.cleanup()
422 main.exit()
423 except:
kelvin8ec71442015-01-15 16:57:00 -0800424 main.log.info( self.name + " ::::::" )
425 main.log.error( traceback.print_exc() )
426 main.log.info( self.name + " ::::::" )
andrewonlab95ce8322014-10-13 14:12:04 -0400427 main.cleanup()
428 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400429
kelvin8ec71442015-01-15 16:57:00 -0800430 def feature_install( self, feature_str ):
431 """
432 Installs a specified feature
433 by issuing command: 'onos> feature:install <feature_str>'
434 """
435 try:
436 self.handle.sendline( "" )
437 self.handle.expect( "onos>" )
438
439 self.handle.sendline( "feature:install " + str( feature_str ) )
440 self.handle.expect( "onos>" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400441
442 return main.TRUE
443
444 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800445 main.log.error( self.name + ": EOF exception found" )
446 main.log.error( self.name + ": " + self.handle.before )
447 main.log.report( "Failed to install feature" )
448 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400449 main.cleanup()
450 main.exit()
451 except:
kelvin8ec71442015-01-15 16:57:00 -0800452 main.log.info( self.name + " ::::::" )
453 main.log.error( traceback.print_exc() )
454 main.log.report( "Failed to install feature" )
455 main.log.report( "Exiting test" )
456 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400457 main.cleanup()
458 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800459
460 def feature_uninstall( self, feature_str ):
461 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400462 Uninstalls a specified feature
463 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800464 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400465 try:
kelvin8ec71442015-01-15 16:57:00 -0800466 self.handle.sendline( "" )
467 self.handle.expect( "onos>" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400468
kelvin8ec71442015-01-15 16:57:00 -0800469 self.handle.sendline( "feature:uninstall " + str( feature_str ) )
470 self.handle.expect( "onos>" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400471
472 return main.TRUE
473
474 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800475 main.log.error( self.name + ": EOF exception found" )
476 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400477 main.cleanup()
478 main.exit()
479 except:
kelvin8ec71442015-01-15 16:57:00 -0800480 main.log.info( self.name + " ::::::" )
481 main.log.error( traceback.print_exc() )
482 main.log.info( self.name + " ::::::" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400483 main.cleanup()
484 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800485
kelvin8ec71442015-01-15 16:57:00 -0800486 def devices( self, json_format=True ):
487 """
Jon Hall7b02d952014-10-17 20:14:54 -0400488 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400489 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800490 * json_format - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800491 """
andrewonlab86dc3082014-10-13 18:18:38 -0400492 try:
kelvin8ec71442015-01-15 16:57:00 -0800493 self.handle.sendline( "" )
494 self.handle.expect( "onos>" )
andrewonlabb66dfa12014-12-02 15:51:10 -0500495
Jon Halle8217482014-10-17 13:49:14 -0400496 if json_format:
kelvin8ec71442015-01-15 16:57:00 -0800497 self.handle.sendline( "devices -j" )
498 self.handle.expect( "devices -j" )
499 self.handle.expect( "onos>" )
Jon Halla001c392014-10-17 18:50:59 -0400500 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800501 """
Jon Halld815ce42014-10-17 19:52:30 -0400502 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
kelvin8ec71442015-01-15 16:57:00 -0800503 To make that escape sequence visible, use repr() function. The repr( handle ) output when printed shows the ANSI escape sequences.
504 In json.loads( somestring ), this somestring variable is actually repr( somestring ) and json.loads would fail with the escape sequence.
Jon Hall983a1702014-10-28 18:44:22 -0400505 So we take off that escape sequence using
kelvin8ec71442015-01-15 16:57:00 -0800506 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
507 handle1 = ansi_escape.sub( '', handle )
508 """
509 # print "repr(handle) =", repr( handle )
510 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
511 handle1 = ansi_escape.sub( '', handle )
512 # print "repr(handle1) = ", repr( handle1 )
Jon Halla001c392014-10-17 18:50:59 -0400513 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400514 else:
kelvin8ec71442015-01-15 16:57:00 -0800515 self.handle.sendline( "devices" )
516 self.handle.expect( "onos>" )
Jon Hallcd707292014-10-17 19:06:17 -0400517 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800518 # print "handle =",handle
Jon Hallcd707292014-10-17 19:06:17 -0400519 return handle
andrewonlab7c211572014-10-15 16:45:20 -0400520 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800521 main.log.error( self.name + ": EOF exception found" )
522 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400523 main.cleanup()
524 main.exit()
525 except:
kelvin8ec71442015-01-15 16:57:00 -0800526 main.log.info( self.name + " ::::::" )
527 main.log.error( traceback.print_exc() )
528 main.log.info( self.name + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -0400529 main.cleanup()
530 main.exit()
531
kelvin8ec71442015-01-15 16:57:00 -0800532 def balance_masters( self ):
533 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800534 This balances the devices across all controllers
535 by issuing command: 'onos> onos:balance-masters'
536 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800537 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800538 try:
kelvin8ec71442015-01-15 16:57:00 -0800539 self.handle.sendline( "" )
540 self.handle.expect( "onos>" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800541
kelvin8ec71442015-01-15 16:57:00 -0800542 self.handle.sendline( "onos:balance-masters" )
543 self.handle.expect( "onos>" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800544 return main.TRUE
545
546 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800547 main.log.error( self.name + ": EOF exception found" )
548 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800549 main.cleanup()
550 main.exit()
551 except:
kelvin8ec71442015-01-15 16:57:00 -0800552 main.log.info( self.name + " ::::::" )
553 main.log.error( traceback.print_exc() )
554 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800555 main.cleanup()
556 main.exit()
557
kelvin8ec71442015-01-15 16:57:00 -0800558 def links( self, json_format=True ):
559 """
Jon Halle8217482014-10-17 13:49:14 -0400560 Lists all core links
561 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800562 * json_format - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800563 """
Jon Halle8217482014-10-17 13:49:14 -0400564 try:
kelvin8ec71442015-01-15 16:57:00 -0800565 self.handle.sendline( "" )
566 self.handle.expect( "onos>" )
Jon Hallffb386d2014-11-21 13:43:38 -0800567
Jon Halle8217482014-10-17 13:49:14 -0400568 if json_format:
kelvin8ec71442015-01-15 16:57:00 -0800569 self.handle.sendline( "links -j" )
570 self.handle.expect( "links -j" )
571 self.handle.expect( "onos>" )
Jon Hallcd707292014-10-17 19:06:17 -0400572 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800573 """
Jon Halld815ce42014-10-17 19:52:30 -0400574 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
kelvin8ec71442015-01-15 16:57:00 -0800575 To make that escape sequence visible, use repr() function. The repr( handle ) output when printed shows the ANSI escape sequences.
576 In json.loads( somestring ), this somestring variable is actually repr( somestring ) and json.loads would fail with the escape sequence.
Jon Hallffb386d2014-11-21 13:43:38 -0800577 So we take off that escape sequence using
kelvin8ec71442015-01-15 16:57:00 -0800578 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
579 handle1 = ansi_escape.sub( '', handle )
580 """
581 # print "repr(handle) =", repr( handle )
582 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
583 handle1 = ansi_escape.sub( '', handle )
584 # print "repr(handle1) = ", repr( handle1 )
Jon Halla001c392014-10-17 18:50:59 -0400585 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400586 else:
kelvin8ec71442015-01-15 16:57:00 -0800587 self.handle.sendline( "links" )
588 self.handle.expect( "onos>" )
Jon Halla001c392014-10-17 18:50:59 -0400589 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800590 # print "handle =",handle
Jon Halla001c392014-10-17 18:50:59 -0400591 return handle
Jon Halle8217482014-10-17 13:49:14 -0400592 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800593 main.log.error( self.name + ": EOF exception found" )
594 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400595 main.cleanup()
596 main.exit()
597 except:
kelvin8ec71442015-01-15 16:57:00 -0800598 main.log.info( self.name + " ::::::" )
599 main.log.error( traceback.print_exc() )
600 main.log.info( self.name + " ::::::" )
Jon Halle8217482014-10-17 13:49:14 -0400601 main.cleanup()
602 main.exit()
603
kelvin8ec71442015-01-15 16:57:00 -0800604 def ports( self, json_format=True ):
605 """
Jon Halle8217482014-10-17 13:49:14 -0400606 Lists all ports
607 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800608 * json_format - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800609 """
Jon Halle8217482014-10-17 13:49:14 -0400610 try:
kelvin8ec71442015-01-15 16:57:00 -0800611 self.handle.sendline( "" )
612 self.handle.expect( "onos>" )
Jon Hallffb386d2014-11-21 13:43:38 -0800613
Jon Halle8217482014-10-17 13:49:14 -0400614 if json_format:
kelvin8ec71442015-01-15 16:57:00 -0800615 self.handle.sendline( "ports -j" )
616 self.handle.expect( "ports -j" )
617 self.handle.expect( "onos>" )
Jon Hallcd707292014-10-17 19:06:17 -0400618 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800619 """
Jon Halld815ce42014-10-17 19:52:30 -0400620 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
kelvin8ec71442015-01-15 16:57:00 -0800621 To make that escape sequence visible, use repr() function. The repr( handle ) output when printed shows the ANSI escape sequences.
622 In json.loads( somestring ), this somestring variable is actually repr( somestring ) and json.loads would fail with the escape sequence.
Jon Hallffb386d2014-11-21 13:43:38 -0800623 So we take off that escape sequence using the following commads:
kelvin8ec71442015-01-15 16:57:00 -0800624 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
625 handle1 = ansi_escape.sub( '', handle )
626 """
627 # print "repr(handle) =", repr( handle )
628 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
629 handle1 = ansi_escape.sub( '', handle )
630 # print "repr(handle1) = ", repr( handle1 )
Jon Halla001c392014-10-17 18:50:59 -0400631 return handle1
632
Jon Halle8217482014-10-17 13:49:14 -0400633 else:
kelvin8ec71442015-01-15 16:57:00 -0800634 self.handle.sendline( "ports" )
635 self.handle.expect( "onos>" )
636 self.handle.sendline( "" )
637 self.handle.expect( "onos>" )
Jon Halla001c392014-10-17 18:50:59 -0400638 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800639 # print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800640 return handle
Jon Halle8217482014-10-17 13:49:14 -0400641 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800642 main.log.error( self.name + ": EOF exception found" )
643 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400644 main.cleanup()
645 main.exit()
646 except:
kelvin8ec71442015-01-15 16:57:00 -0800647 main.log.info( self.name + " ::::::" )
648 main.log.error( traceback.print_exc() )
649 main.log.info( self.name + " ::::::" )
Jon Halle8217482014-10-17 13:49:14 -0400650 main.cleanup()
651 main.exit()
652
kelvin8ec71442015-01-15 16:57:00 -0800653 def roles( self, json_format=True ):
654 """
Jon Hall983a1702014-10-28 18:44:22 -0400655 Lists all devices and the controllers with roles assigned to them
656 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800657 * json_format - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800658 """
andrewonlab7c211572014-10-15 16:45:20 -0400659 try:
kelvin8ec71442015-01-15 16:57:00 -0800660 self.handle.sendline( "" )
661 self.handle.expect( "onos>" )
Jon Hallb1290e82014-11-18 16:17:48 -0500662
Jon Hall983a1702014-10-28 18:44:22 -0400663 if json_format:
kelvin8ec71442015-01-15 16:57:00 -0800664 self.handle.sendline( "roles -j" )
665 self.handle.expect( "roles -j" )
666 self.handle.expect( "onos>" )
Jon Hall983a1702014-10-28 18:44:22 -0400667 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800668 """
Jon Hallb1290e82014-11-18 16:17:48 -0500669 handle variable here contains some ANSI escape color code sequences at the
670 end which are invisible in the print command output. To make that escape
kelvin8ec71442015-01-15 16:57:00 -0800671 sequence visible, use repr() function. The repr( handle ) output when printed
672 shows the ANSI escape sequences. In json.loads( somestring ), this somestring
673 variable is actually repr( somestring ) and json.loads would fail with the escape
Jon Hallb1290e82014-11-18 16:17:48 -0500674 sequence.
675
676 So we take off that escape sequence using the following commads:
kelvin8ec71442015-01-15 16:57:00 -0800677 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
678 handle1 = ansi_escape.sub( '', handle )
679 """
680 # print "repr(handle) =", repr( handle )
681 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
682 handle1 = ansi_escape.sub( '', handle )
683 # print "repr(handle1) = ", repr( handle1 )
Jon Hall983a1702014-10-28 18:44:22 -0400684 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400685
andrewonlab7c211572014-10-15 16:45:20 -0400686 else:
kelvin8ec71442015-01-15 16:57:00 -0800687 self.handle.sendline( "roles" )
688 self.handle.expect( "onos>" )
689 self.handle.sendline( "" )
690 self.handle.expect( "onos>" )
Jon Hall983a1702014-10-28 18:44:22 -0400691 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800692 # print "handle =",handle
Jon Hallffb386d2014-11-21 13:43:38 -0800693 return handle
Jon Hall983a1702014-10-28 18:44:22 -0400694 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800695 main.log.error( self.name + ": EOF exception found" )
696 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400697 main.cleanup()
698 main.exit()
699 except:
kelvin8ec71442015-01-15 16:57:00 -0800700 main.log.info( self.name + " ::::::" )
701 main.log.error( traceback.print_exc() )
702 main.log.info( self.name + " ::::::" )
Jon Hall983a1702014-10-28 18:44:22 -0400703 main.cleanup()
704 main.exit()
705
kelvin8ec71442015-01-15 16:57:00 -0800706 def get_role( self, device_id ):
707 """
708 Given the a string containing the json representation of the "roles" cli command and a
Jon Hall983a1702014-10-28 18:44:22 -0400709 partial or whole device id, returns a json object containing the
710 roles output for the first device whose id contains "device_id"
711
712 Returns:
713 Dict of the role assignments for the given device or
714 None if not match
kelvin8ec71442015-01-15 16:57:00 -0800715 """
Jon Hall983a1702014-10-28 18:44:22 -0400716 try:
717 import json
kelvin8ec71442015-01-15 16:57:00 -0800718 if device_id is None:
Jon Hall983a1702014-10-28 18:44:22 -0400719 return None
720 else:
721 raw_roles = self.roles()
kelvin8ec71442015-01-15 16:57:00 -0800722 roles_json = json.loads( raw_roles )
723 # search json for the device with id then return the device
Jon Hall983a1702014-10-28 18:44:22 -0400724 for device in roles_json:
kelvin8ec71442015-01-15 16:57:00 -0800725 # print device
726 if str( device_id ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400727 return device
728 return None
andrewonlab7c211572014-10-15 16:45:20 -0400729
andrewonlab86dc3082014-10-13 18:18:38 -0400730 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800731 main.log.error( self.name + ": EOF exception found" )
732 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400733 main.cleanup()
734 main.exit()
735 except:
kelvin8ec71442015-01-15 16:57:00 -0800736 main.log.info( self.name + " ::::::" )
737 main.log.error( traceback.print_exc() )
738 main.log.info( self.name + " ::::::" )
andrewonlab86dc3082014-10-13 18:18:38 -0400739 main.cleanup()
740 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800741
kelvin8ec71442015-01-15 16:57:00 -0800742 def roles_not_null( self ):
743 """
Jon Hall94fd0472014-12-08 11:52:42 -0800744 Iterates through each device and checks if there is a master assigned
745 Returns: main.TRUE if each device has a master
746 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800747 """
Jon Hall94fd0472014-12-08 11:52:42 -0800748 try:
749 import json
750 raw_roles = self.roles()
kelvin8ec71442015-01-15 16:57:00 -0800751 roles_json = json.loads( raw_roles )
752 # search json for the device with id then return the device
Jon Hall94fd0472014-12-08 11:52:42 -0800753 for device in roles_json:
kelvin8ec71442015-01-15 16:57:00 -0800754 # print device
755 if device[ 'master' ] == "none":
756 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800757 return main.FALSE
758 return main.TRUE
759
760 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800761 main.log.error( self.name + ": EOF exception found" )
762 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800763 main.cleanup()
764 main.exit()
765 except:
kelvin8ec71442015-01-15 16:57:00 -0800766 main.log.info( self.name + " ::::::" )
767 main.log.error( traceback.print_exc() )
768 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -0800769 main.cleanup()
770 main.exit()
771
kelvin8ec71442015-01-15 16:57:00 -0800772 def paths( self, src_id, dst_id ):
773 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400774 Returns string of paths, and the cost.
775 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800776 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400777 try:
kelvin8ec71442015-01-15 16:57:00 -0800778 self.handle.sendline( "" )
779 self.handle.expect( "onos>" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400780
kelvin8ec71442015-01-15 16:57:00 -0800781 self.handle.sendline( "onos:paths " +
782 str( src_id ) + " " + str( dst_id ) )
783 i = self.handle.expect( [
andrewonlab3e15ead2014-10-15 14:21:34 -0400784 "Error",
kelvin8ec71442015-01-15 16:57:00 -0800785 "onos>" ] )
786
787 self.handle.sendline( "" )
788 self.handle.expect( "onos>" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400789
790 handle = self.handle.before
791
792 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800793 main.log.error( "Error in getting paths" )
794 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400795 else:
kelvin8ec71442015-01-15 16:57:00 -0800796 path = handle.split( ";" )[ 0 ]
797 cost = handle.split( ";" )[ 1 ]
798 return ( path, cost )
799
andrewonlab3e15ead2014-10-15 14:21:34 -0400800 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800801 main.log.error( self.name + ": EOF exception found" )
802 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400803 main.cleanup()
804 main.exit()
805 except:
kelvin8ec71442015-01-15 16:57:00 -0800806 main.log.info( self.name + " ::::::" )
807 main.log.error( traceback.print_exc() )
808 main.log.info( self.name + " ::::::" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400809 main.cleanup()
810 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800811
kelvin8ec71442015-01-15 16:57:00 -0800812 def hosts( self, json_format=True ):
813 """
Jon Hallffb386d2014-11-21 13:43:38 -0800814 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400815 Optional argument:
Jon Hallffb386d2014-11-21 13:43:38 -0800816 * json_format - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800817 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400818 try:
kelvin8ec71442015-01-15 16:57:00 -0800819 self.handle.sendline( "" )
820 self.handle.expect( "onos>" )
Jon Hallffb386d2014-11-21 13:43:38 -0800821
Jon Hall42db6dc2014-10-24 19:03:48 -0400822 if json_format:
kelvin8ec71442015-01-15 16:57:00 -0800823 self.handle.sendline( "hosts -j" )
824 self.handle.expect( "hosts -j" )
825 self.handle.expect( "onos>" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400826 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800827 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400828 handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output
kelvin8ec71442015-01-15 16:57:00 -0800829 To make that escape sequence visible, use repr() function. The repr( handle ) output when printed shows the ANSI escape sequences.
830 In json.loads( somestring ), this somestring variable is actually repr( somestring ) and json.loads would fail with the escape sequence.
Jon Hallffb386d2014-11-21 13:43:38 -0800831 So we take off that escape sequence using
kelvin8ec71442015-01-15 16:57:00 -0800832 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
833 handle1 = ansi_escape.sub( '', handle )
834 """
835 # print "repr(handle) =", repr( handle )
836 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
837 handle1 = ansi_escape.sub( '', handle )
838 # print "repr(handle1) = ", repr( handle1 )
Jon Hall42db6dc2014-10-24 19:03:48 -0400839 return handle1
840 else:
kelvin8ec71442015-01-15 16:57:00 -0800841 self.handle.sendline( "hosts" )
842 self.handle.expect( "onos>" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400843 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800844 # print "handle =",handle
Jon Hall42db6dc2014-10-24 19:03:48 -0400845 return handle
846 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800847 main.log.error( self.name + ": EOF exception found" )
848 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400849 main.cleanup()
850 main.exit()
851 except:
kelvin8ec71442015-01-15 16:57:00 -0800852 main.log.info( self.name + " ::::::" )
853 main.log.error( traceback.print_exc() )
854 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400855 main.cleanup()
856 main.exit()
857
kelvin8ec71442015-01-15 16:57:00 -0800858 def get_host( self, mac ):
859 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400860 Return the first host from the hosts api whose 'id' contains 'mac'
861 Note: mac must be a colon seperated mac address, but could be a partial mac address
862 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800863 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400864 import json
865 try:
kelvin8ec71442015-01-15 16:57:00 -0800866 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400867 return None
868 else:
869 mac = mac
870 raw_hosts = self.hosts()
kelvin8ec71442015-01-15 16:57:00 -0800871 hosts_json = json.loads( raw_hosts )
872 # search json for the host with mac then return the device
Jon Hall42db6dc2014-10-24 19:03:48 -0400873 for host in hosts_json:
kelvin8ec71442015-01-15 16:57:00 -0800874 # print "%s in %s?" % ( mac, host[ 'id' ] )
875 if mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400876 return host
877 return None
878 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800879 main.log.error( self.name + ": EOF exception found" )
880 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400881 main.cleanup()
882 main.exit()
883 except:
kelvin8ec71442015-01-15 16:57:00 -0800884 main.log.info( self.name + " ::::::" )
885 main.log.error( traceback.print_exc() )
886 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400887 main.cleanup()
888 main.exit()
889
kelvin8ec71442015-01-15 16:57:00 -0800890 def get_hosts_id( self, host_list ):
891 """
892 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400893 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800894
andrewonlab3f0a4af2014-10-17 12:25:14 -0400895 Required:
896 * host_list: List of hosts obtained by Mininet
897 IMPORTANT:
898 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800899 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400900 Furthermore, it assumes that value of VLAN is '-1'
901 Description:
kelvin8ec71442015-01-15 16:57:00 -0800902 Converts mininet hosts ( h1, h2, h3... ) into
903 ONOS format ( 00:00:00:00:00:01/-1 , ... )
904 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400905 try:
andrewonlab3f0a4af2014-10-17 12:25:14 -0400906 onos_host_list = []
907
908 for host in host_list:
kelvin8ec71442015-01-15 16:57:00 -0800909 host = host.replace( "h", "" )
910 host_hex = hex( int( host ) ).zfill( 12 )
911 host_hex = str( host_hex ).replace( 'x', '0' )
912 i = iter( str( host_hex ) )
913 host_hex = ":".join( a + b for a, b in zip( i, i ) )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400914 host_hex = host_hex + "/-1"
kelvin8ec71442015-01-15 16:57:00 -0800915 onos_host_list.append( host_hex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400916
kelvin8ec71442015-01-15 16:57:00 -0800917 return onos_host_list
andrewonlab3f0a4af2014-10-17 12:25:14 -0400918
919 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800920 main.log.error( self.name + ": EOF exception found" )
921 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400922 main.cleanup()
923 main.exit()
924 except:
kelvin8ec71442015-01-15 16:57:00 -0800925 main.log.info( self.name + " ::::::" )
926 main.log.error( traceback.print_exc() )
927 main.log.info( self.name + " ::::::" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400928 main.cleanup()
929 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400930
kelvin8ec71442015-01-15 16:57:00 -0800931 def add_host_intent( self, host_id_one, host_id_two ):
932 """
andrewonlabe6745342014-10-17 14:29:13 -0400933 Required:
934 * host_id_one: ONOS host id for host1
935 * host_id_two: ONOS host id for host2
936 Description:
kelvin8ec71442015-01-15 16:57:00 -0800937 Adds a host-to-host intent ( bidrectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500938 specifying the two hosts.
kelvin8ec71442015-01-15 16:57:00 -0800939 """
andrewonlabe6745342014-10-17 14:29:13 -0400940 try:
kelvin8ec71442015-01-15 16:57:00 -0800941 self.handle.sendline( "" )
942 self.handle.expect( "onos>" )
Jon Hallb1290e82014-11-18 16:17:48 -0500943
kelvin8ec71442015-01-15 16:57:00 -0800944 self.handle.sendline( "add-host-intent " +
945 str( host_id_one ) + " " + str( host_id_two ) )
946 self.handle.expect( "onos>" )
andrewonlabe6745342014-10-17 14:29:13 -0400947
andrewonlabe6745342014-10-17 14:29:13 -0400948 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -0800949 # print "handle =", handle
andrewonlabe6745342014-10-17 14:29:13 -0400950
kelvin8ec71442015-01-15 16:57:00 -0800951 main.log.info( "Host intent installed between " +
952 str( host_id_one ) + " and " + str( host_id_two ) )
andrewonlabe6745342014-10-17 14:29:13 -0400953
954 return handle
Jon Hallb1290e82014-11-18 16:17:48 -0500955
andrewonlabe6745342014-10-17 14:29:13 -0400956 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800957 main.log.error( self.name + ": EOF exception found" )
958 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400959 main.cleanup()
960 main.exit()
961 except:
kelvin8ec71442015-01-15 16:57:00 -0800962 main.log.info( self.name + " ::::::" )
963 main.log.error( traceback.print_exc() )
964 main.log.info( self.name + " ::::::" )
andrewonlabe6745342014-10-17 14:29:13 -0400965 main.cleanup()
966 main.exit()
967
kelvin8ec71442015-01-15 16:57:00 -0800968 def add_optical_intent( self, ingress_device, egress_device ):
969 """
andrewonlab7b31d232014-10-24 13:31:47 -0400970 Required:
971 * ingress_device: device id of ingress device
972 * egress_device: device id of egress device
973 Optional:
974 TODO: Still needs to be implemented via dev side
kelvin8ec71442015-01-15 16:57:00 -0800975 """
andrewonlab7b31d232014-10-24 13:31:47 -0400976 try:
kelvin8ec71442015-01-15 16:57:00 -0800977 self.handle.sendline( "add-optical-intent " +
978 str( ingress_device ) + " " + str( egress_device ) )
979 self.handle.expect( "add-optical-intent" )
980 i = self.handle.expect( [
andrewonlab7b31d232014-10-24 13:31:47 -0400981 "Error",
kelvin8ec71442015-01-15 16:57:00 -0800982 "onos>" ] )
andrewonlab7b31d232014-10-24 13:31:47 -0400983
984 handle = self.handle.before
985
kelvin8ec71442015-01-15 16:57:00 -0800986 # If error, return error message
andrewonlab7b31d232014-10-24 13:31:47 -0400987 if i == 0:
988 return handle
989 else:
990 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800991
andrewonlab7b31d232014-10-24 13:31:47 -0400992 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800993 main.log.error( self.name + ": EOF exception found" )
994 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400995 main.cleanup()
996 main.exit()
997 except:
kelvin8ec71442015-01-15 16:57:00 -0800998 main.log.info( self.name + " ::::::" )
999 main.log.error( traceback.print_exc() )
1000 main.log.info( self.name + " ::::::" )
andrewonlab7b31d232014-10-24 13:31:47 -04001001 main.cleanup()
1002 main.exit()
1003
kelvin8ec71442015-01-15 16:57:00 -08001004 def add_point_intent( self, ingress_device, egress_device,
1005 port_ingress="", port_egress="", ethType="", ethSrc="",
1006 ethDst="", bandwidth="", lambda_alloc=False,
1007 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="" ):
1008 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001009 Required:
1010 * ingress_device: device id of ingress device
1011 * egress_device: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001012 Optional:
1013 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001014 * ethSrc: specify ethSrc ( i.e. src mac addr )
1015 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001016 * bandwidth: specify bandwidth capacity of link
kelvin8ec71442015-01-15 16:57:00 -08001017 * lambda_alloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001018 for the specified intent
kelvin8ec71442015-01-15 16:57:00 -08001019 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001020 * ipSrc: specify ip source address
1021 * ipDst: specify ip destination address
1022 * tcpSrc: specify tcp source port
1023 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001024 Description:
kelvin8ec71442015-01-15 16:57:00 -08001025 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001026 specifying device id's and optional fields
1027
kelvin8ec71442015-01-15 16:57:00 -08001028 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001029 options developers provide for point-to-point
1030 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001031 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001032 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001033 cmd = ""
1034
kelvin8ec71442015-01-15 16:57:00 -08001035 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001036 if not ethType and not ethSrc and not ethDst\
andrewonlabfa4ff502014-11-11 16:41:30 -05001037 and not bandwidth and not lambda_alloc \
1038 and not ipProto and not ipSrc and not ipDst \
1039 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001040 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001041
andrewonlab289e4b72014-10-21 21:24:18 -04001042 else:
andrewonlab36af3822014-11-18 17:48:18 -05001043 cmd = "add-point-intent"
kelvin8ec71442015-01-15 16:57:00 -08001044
andrewonlab0c0a6772014-10-22 12:31:18 -04001045 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001046 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001047 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001048 cmd += " --ethSrc " + str( ethSrc )
1049 if ethDst:
1050 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001051 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001052 cmd += " --bandwidth " + str( bandwidth )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001053 if lambda_alloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001054 cmd += " --lambda "
1055 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001056 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001057 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001058 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001059 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001060 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001061 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001062 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001063 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001064 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001065
kelvin8ec71442015-01-15 16:57:00 -08001066 # Check whether the user appended the port
1067 # or provided it as an input
andrewonlab36af3822014-11-18 17:48:18 -05001068 if "/" in ingress_device:
kelvin8ec71442015-01-15 16:57:00 -08001069 cmd += " " + str( ingress_device )
andrewonlab36af3822014-11-18 17:48:18 -05001070 else:
1071 if not port_ingress:
kelvin8ec71442015-01-15 16:57:00 -08001072 main.log.error( "You must specify " +
1073 "the ingress port" )
1074 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -05001075 return main.FALSE
1076
kelvin8ec71442015-01-15 16:57:00 -08001077 cmd += " " + \
1078 str( ingress_device ) + "/" +\
1079 str( port_ingress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001080
1081 if "/" in egress_device:
kelvin8ec71442015-01-15 16:57:00 -08001082 cmd += " " + str( egress_device )
andrewonlab36af3822014-11-18 17:48:18 -05001083 else:
1084 if not port_egress:
kelvin8ec71442015-01-15 16:57:00 -08001085 main.log.error( "You must specify " +
1086 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001087 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001088
kelvin8ec71442015-01-15 16:57:00 -08001089 cmd += " " +\
1090 str( egress_device ) + "/" +\
1091 str( port_egress )
1092
1093 self.handle.sendline( cmd )
1094
1095 main.log.info( cmd + " sent" )
1096 i = self.handle.expect( [
andrewonlab4dbb4d82014-10-17 18:22:31 -04001097 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001098 "onos>" ] )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001099
1100 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001101 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001102 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001103 else:
1104 return main.TRUE
andrewonlab289e4b72014-10-21 21:24:18 -04001105
andrewonlab4dbb4d82014-10-17 18:22:31 -04001106 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001107 main.log.error( self.name + ": EOF exception found" )
1108 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001109 main.cleanup()
1110 main.exit()
1111 except:
kelvin8ec71442015-01-15 16:57:00 -08001112 main.log.info( self.name + " ::::::" )
1113 main.log.error( traceback.print_exc() )
1114 main.log.info( self.name + " ::::::" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001115 main.cleanup()
1116 main.exit()
1117
kelvin8ec71442015-01-15 16:57:00 -08001118 def add_multipoint_to_singlepoint_intent(
1119 self, ingress_device1, ingress_device2, egress_device,
1120 port_ingress="", port_egress="", ethType="", ethSrc="",
1121 ethDst="", bandwidth="", lambda_alloc=False,
1122 ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="", setEthDst="" ):
1123 """
shahshreyad0c80432014-12-04 16:56:05 -08001124 Note:
1125 This function assumes that there would be 2 ingress devices and one egress device
1126 For more number of ingress devices, this function needs to be modified
1127 Required:
1128 * ingress_device1: device id of ingress device1
1129 * ingress_device2: device id of ingress device2
1130 * egress_device: device id of egress device
1131 Optional:
1132 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001133 * ethSrc: specify ethSrc ( i.e. src mac addr )
1134 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001135 * bandwidth: specify bandwidth capacity of link
kelvin8ec71442015-01-15 16:57:00 -08001136 * lambda_alloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001137 for the specified intent
kelvin8ec71442015-01-15 16:57:00 -08001138 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001139 * ipSrc: specify ip source address
1140 * ipDst: specify ip destination address
1141 * tcpSrc: specify tcp source port
1142 * tcpDst: specify tcp destination port
1143 * setEthSrc: action to Rewrite Source MAC Address
1144 * setEthDst: action to Rewrite Destination MAC Address
1145 Description:
kelvin8ec71442015-01-15 16:57:00 -08001146 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001147 specifying device id's and optional fields
1148
kelvin8ec71442015-01-15 16:57:00 -08001149 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001150 options developers provide for multipointpoint-to-singlepoint
1151 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001152 """
shahshreyad0c80432014-12-04 16:56:05 -08001153 try:
1154 cmd = ""
1155
kelvin8ec71442015-01-15 16:57:00 -08001156 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001157 if not ethType and not ethSrc and not ethDst\
1158 and not bandwidth and not lambda_alloc \
1159 and not ipProto and not ipSrc and not ipDst \
1160 and not tcpSrc and not tcpDst and not setEthSrc and not setEthDst:
1161 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001162
1163 else:
1164 cmd = "add-multi-to-single-intent"
kelvin8ec71442015-01-15 16:57:00 -08001165
shahshreyad0c80432014-12-04 16:56:05 -08001166 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001167 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001168 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001169 cmd += " --ethSrc " + str( ethSrc )
1170 if ethDst:
1171 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001172 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001173 cmd += " --bandwidth " + str( bandwidth )
shahshreyad0c80432014-12-04 16:56:05 -08001174 if lambda_alloc:
1175 cmd += " --lambda "
1176 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001177 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001178 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001179 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001180 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001181 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001182 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001183 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001184 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001185 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001186 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001187 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001188 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001189 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001190
kelvin8ec71442015-01-15 16:57:00 -08001191 # Check whether the user appended the port
1192 # or provided it as an input
shahshreyad0c80432014-12-04 16:56:05 -08001193 if "/" in ingress_device1:
kelvin8ec71442015-01-15 16:57:00 -08001194 cmd += " " + str( ingress_device1 )
shahshreyad0c80432014-12-04 16:56:05 -08001195 else:
1196 if not port_ingress1:
kelvin8ec71442015-01-15 16:57:00 -08001197 main.log.error( "You must specify " +
1198 "the ingress port1" )
1199 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001200 return main.FALSE
1201
kelvin8ec71442015-01-15 16:57:00 -08001202 cmd += " " + \
1203 str( ingress_device1 ) + "/" +\
1204 str( port_ingress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001205
1206 if "/" in ingress_device2:
kelvin8ec71442015-01-15 16:57:00 -08001207 cmd += " " + str( ingress_device2 )
shahshreyad0c80432014-12-04 16:56:05 -08001208 else:
1209 if not port_ingress2:
kelvin8ec71442015-01-15 16:57:00 -08001210 main.log.error( "You must specify " +
1211 "the ingress port2" )
1212 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001213 return main.FALSE
1214
kelvin8ec71442015-01-15 16:57:00 -08001215 cmd += " " + \
1216 str( ingress_device2 ) + "/" +\
1217 str( port_ingress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001218
1219 if "/" in egress_device:
kelvin8ec71442015-01-15 16:57:00 -08001220 cmd += " " + str( egress_device )
shahshreyad0c80432014-12-04 16:56:05 -08001221 else:
1222 if not port_egress:
kelvin8ec71442015-01-15 16:57:00 -08001223 main.log.error( "You must specify " +
1224 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001225 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001226
1227 cmd += " " +\
1228 str( egress_device ) + "/" +\
1229 str( port_egress )
1230 print "cmd= ", cmd
1231 self.handle.sendline( cmd )
1232
1233 main.log.info( cmd + " sent" )
1234 i = self.handle.expect( [
shahshreyad0c80432014-12-04 16:56:05 -08001235 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001236 "onos>" ] )
shahshreyad0c80432014-12-04 16:56:05 -08001237
1238 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001239 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001240 return self.handle
1241 else:
1242 return main.TRUE
1243
1244 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001245 main.log.error( self.name + ": EOF exception found" )
1246 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001247 main.cleanup()
1248 main.exit()
1249 except:
kelvin8ec71442015-01-15 16:57:00 -08001250 main.log.info( self.name + " ::::::" )
1251 main.log.error( traceback.print_exc() )
1252 main.log.info( self.name + " ::::::" )
shahshreyad0c80432014-12-04 16:56:05 -08001253 main.cleanup()
1254 main.exit()
1255
kelvin8ec71442015-01-15 16:57:00 -08001256 def remove_intent( self, intent_id ):
1257 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001258 Remove intent for specified intent id
kelvin8ec71442015-01-15 16:57:00 -08001259 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001260 try:
kelvin8ec71442015-01-15 16:57:00 -08001261 self.handle.sendline( "" )
1262 self.handle.expect( "onos>" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001263
kelvin8ec71442015-01-15 16:57:00 -08001264 self.handle.sendline( "remove-intent " + str( intent_id ) )
1265 i = self.handle.expect( [
andrewonlab9a50dfe2014-10-17 17:22:31 -04001266 "Error",
kelvin8ec71442015-01-15 16:57:00 -08001267 "onos>" ] )
1268
andrewonlab9a50dfe2014-10-17 17:22:31 -04001269 handle = self.handle.before
1270
1271 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001272 main.log.error( "Error in removing intent" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001273 return handle
1274 else:
kelvin8ec71442015-01-15 16:57:00 -08001275 return handle
1276
andrewonlab9a50dfe2014-10-17 17:22:31 -04001277 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001278 main.log.error( self.name + ": EOF exception found" )
1279 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001280 main.cleanup()
1281 main.exit()
1282 except:
kelvin8ec71442015-01-15 16:57:00 -08001283 main.log.info( self.name + " ::::::" )
1284 main.log.error( traceback.print_exc() )
1285 main.log.info( self.name + " ::::::" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001286 main.cleanup()
1287 main.exit()
1288
pingping-lindabe7972014-11-17 19:29:44 -08001289 # This method should be used after installing application: onos-app-sdnip
kelvin8ec71442015-01-15 16:57:00 -08001290 def routes( self, json_format=False ):
1291 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001292 Optional:
1293 * json_format: enable output formatting in json
1294 Description:
1295 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001296 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001297 try:
1298 if json_format:
kelvin8ec71442015-01-15 16:57:00 -08001299 self.handle.sendline( "routes -j" )
1300 self.handle.expect( "routes -j" )
1301 self.handle.expect( "onos>" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001302 handle_tmp = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001303
1304 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
1305 handle = ansi_escape.sub( '', handle_tmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001306
1307 else:
kelvin8ec71442015-01-15 16:57:00 -08001308 self.handle.sendline( "" )
1309 self.handle.expect( "onos>" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001310
kelvin8ec71442015-01-15 16:57:00 -08001311 self.handle.sendline( "routes" )
1312 self.handle.expect( "onos>" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001313 handle = self.handle.before
1314
1315 return handle
1316
1317 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001318 main.log.error( self.name + ": EOF exception found" )
1319 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001320 main.cleanup()
1321 main.exit()
1322 except:
kelvin8ec71442015-01-15 16:57:00 -08001323 main.log.info( self.name + " ::::::" )
1324 main.log.error( traceback.print_exc() )
1325 main.log.info( self.name + " ::::::" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001326 main.cleanup()
1327 main.exit()
1328
kelvin8ec71442015-01-15 16:57:00 -08001329 def intents( self, json_format=True ):
1330 """
andrewonlab377693f2014-10-21 16:00:30 -04001331 Optional:
1332 * json_format: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001333 Description:
kelvin8ec71442015-01-15 16:57:00 -08001334 Obtain intents currently installed
1335 """
andrewonlabe6745342014-10-17 14:29:13 -04001336 try:
andrewonlab377693f2014-10-21 16:00:30 -04001337 if json_format:
kelvin8ec71442015-01-15 16:57:00 -08001338 self.handle.sendline( "intents -j" )
1339 self.handle.expect( "intents -j" )
1340 self.handle.expect( "onos>" )
andrewonlab377693f2014-10-21 16:00:30 -04001341 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001342
kelvin8ec71442015-01-15 16:57:00 -08001343 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
1344 handle = ansi_escape.sub( '', handle )
1345 else:
1346 self.handle.sendline( "" )
1347 self.handle.expect( "onos>" )
1348
1349 self.handle.sendline( "intents" )
1350 self.handle.expect( "onos>" )
andrewonlab377693f2014-10-21 16:00:30 -04001351 handle = self.handle.before
andrewonlabe6745342014-10-17 14:29:13 -04001352
1353 return handle
1354
1355 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001356 main.log.error( self.name + ": EOF exception found" )
1357 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001358 main.cleanup()
1359 main.exit()
1360 except:
kelvin8ec71442015-01-15 16:57:00 -08001361 main.log.info( self.name + " ::::::" )
1362 main.log.error( traceback.print_exc() )
1363 main.log.info( self.name + " ::::::" )
andrewonlabe6745342014-10-17 14:29:13 -04001364 main.cleanup()
1365 main.exit()
1366
kelvin8ec71442015-01-15 16:57:00 -08001367 def flows( self, json_format=True ):
1368 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001369 Optional:
1370 * json_format: enable output formatting in json
1371 Description:
kelvin8ec71442015-01-15 16:57:00 -08001372 Obtain flows currently installed
1373 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001374 try:
1375 if json_format:
kelvin8ec71442015-01-15 16:57:00 -08001376 self.handle.sendline( "flows -j" )
1377 self.handle.expect( "flows -j" )
1378 self.handle.expect( "onos>" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001379 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001380 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
1381 handle = ansi_escape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001382
1383 else:
kelvin8ec71442015-01-15 16:57:00 -08001384 self.handle.sendline( "" )
1385 self.handle.expect( "onos>" )
1386 self.handle.sendline( "flows" )
1387 self.handle.expect( "onos>" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001388 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001389 if re.search( "Error\sexecuting\scommand:", handle ):
1390 main.log.error(
1391 self.name + ".flows() response: " + str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001392
1393 return handle
1394
1395 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001396 main.log.error( self.name + ": EOF exception found" )
1397 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001398 main.cleanup()
1399 main.exit()
1400 except:
kelvin8ec71442015-01-15 16:57:00 -08001401 main.log.info( self.name + " ::::::" )
1402 main.log.error( traceback.print_exc() )
1403 main.log.info( self.name + " ::::::" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001404 main.cleanup()
1405 main.exit()
1406
kelvin8ec71442015-01-15 16:57:00 -08001407 def push_test_intents( self, dpid_src, dpid_dst, num_intents,
1408 num_mult="", app_id="", report=True ):
1409 """
andrewonlab87852b02014-11-19 18:44:19 -05001410 Description:
kelvin8ec71442015-01-15 16:57:00 -08001411 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001412 a specific point-to-point intent definition
1413 Required:
1414 * dpid_src: specify source dpid
1415 * dpid_dst: specify destination dpid
1416 * num_intents: specify number of intents to push
1417 Optional:
andrewonlabb66dfa12014-12-02 15:51:10 -05001418 * num_mult: number multiplier for multiplying
1419 the number of intents specified
1420 * app_id: specify the application id init to further
1421 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001422 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001423 """
andrewonlab87852b02014-11-19 18:44:19 -05001424 try:
kelvin8ec71442015-01-15 16:57:00 -08001425 cmd = "push-test-intents " +\
1426 str( dpid_src ) + " " + str( dpid_dst ) + " " +\
1427 str( num_intents )
1428
andrewonlabb66dfa12014-12-02 15:51:10 -05001429 if num_mult:
kelvin8ec71442015-01-15 16:57:00 -08001430 cmd += " " + str( num_mult )
1431 # If app id is specified, then num_mult
1432 # must exist because of the way this command
1433 # takes in arguments
andrewonlabb66dfa12014-12-02 15:51:10 -05001434 if app_id:
kelvin8ec71442015-01-15 16:57:00 -08001435 cmd += " " + str( app_id )
1436
1437 self.handle.sendline( cmd )
1438 self.handle.expect( cmd )
1439 self.handle.expect( "onos>" )
1440
andrewonlab87852b02014-11-19 18:44:19 -05001441 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001442
1443 # Some color thing that we want to escape
1444 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
1445 handle = ansi_escape.sub( '', handle )
1446
andrewonlab87852b02014-11-19 18:44:19 -05001447 if report:
andrewonlabb66dfa12014-12-02 15:51:10 -05001448 lat_result = []
kelvin8ec71442015-01-15 16:57:00 -08001449 main.log.info( handle )
1450 # Split result by newline
1451 newline = handle.split( "\r\r\n" )
1452 # Ignore the first object of list, which is empty
1453 newline = newline[ 1: ]
1454 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001455 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001456 result = result.split( ": " )
1457 # Append the first result of second parse
1458 lat_result.append( result[ 1 ].split( " " )[ 0 ] )
andrewonlabb66dfa12014-12-02 15:51:10 -05001459
kelvin8ec71442015-01-15 16:57:00 -08001460 main.log.info( lat_result )
1461 return lat_result
andrewonlab87852b02014-11-19 18:44:19 -05001462 else:
1463 return main.TRUE
1464
1465 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001466 main.log.error( self.name + ": EOF exception found" )
1467 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001468 main.cleanup()
1469 main.exit()
1470 except:
kelvin8ec71442015-01-15 16:57:00 -08001471 main.log.info( self.name + " ::::::" )
1472 main.log.error( traceback.print_exc() )
1473 main.log.info( self.name + " ::::::" )
andrewonlab87852b02014-11-19 18:44:19 -05001474 main.cleanup()
1475 main.exit()
1476
kelvin8ec71442015-01-15 16:57:00 -08001477 def intents_events_metrics( self, json_format=True ):
1478 """
1479 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001480 Optional:
1481 * json_format: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001482 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001483 try:
1484 if json_format:
kelvin8ec71442015-01-15 16:57:00 -08001485 self.handle.sendline( "intents-events-metrics -j" )
1486 self.handle.expect( "intents-events-metrics -j" )
1487 self.handle.expect( "onos>" )
1488
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001489 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001490
1491 # Some color thing that we want to escape
1492 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
1493 handle = ansi_escape.sub( '', handle )
1494
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001495 else:
kelvin8ec71442015-01-15 16:57:00 -08001496 self.handle.sendline( "intents-events-metrics" )
1497 self.handle.expect( "intents-events-metrics" )
1498 self.handle.expect( "onos>" )
1499
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001500 handle = self.handle.before
Shreya Shah0f01c812014-10-26 20:15:28 -04001501
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001502 return handle
kelvin8ec71442015-01-15 16:57:00 -08001503
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001504 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001505 main.log.error( self.name + ": EOF exception found" )
1506 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001507 main.cleanup()
1508 main.exit()
1509 except:
kelvin8ec71442015-01-15 16:57:00 -08001510 main.log.info( self.name + " ::::::" )
1511 main.log.error( traceback.print_exc() )
1512 main.log.info( self.name + " ::::::" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001513 main.cleanup()
1514 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001515
kelvin8ec71442015-01-15 16:57:00 -08001516 def topology_events_metrics( self, json_format=True ):
1517 """
1518 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001519 Optional:
1520 * json_format: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001521 """
andrewonlab867212a2014-10-22 20:13:38 -04001522 try:
1523 if json_format:
kelvin8ec71442015-01-15 16:57:00 -08001524 self.handle.sendline( "topology-events-metrics -j" )
1525 self.handle.expect( "topology-events-metrics -j" )
1526 self.handle.expect( "onos>" )
1527
andrewonlab867212a2014-10-22 20:13:38 -04001528 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001529
1530 # Some color thing that we want to escape
1531 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
1532 handle = ansi_escape.sub( '', handle )
1533
andrewonlab867212a2014-10-22 20:13:38 -04001534 else:
kelvin8ec71442015-01-15 16:57:00 -08001535 self.handle.sendline( "topology-events-metrics" )
1536 self.handle.expect( "topology-events-metrics" )
1537 self.handle.expect( "onos>" )
1538
andrewonlab867212a2014-10-22 20:13:38 -04001539 handle = self.handle.before
1540
1541 return handle
kelvin8ec71442015-01-15 16:57:00 -08001542
andrewonlab867212a2014-10-22 20:13:38 -04001543 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001544 main.log.error( self.name + ": EOF exception found" )
1545 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001546 main.cleanup()
1547 main.exit()
1548 except:
kelvin8ec71442015-01-15 16:57:00 -08001549 main.log.info( self.name + " ::::::" )
1550 main.log.error( traceback.print_exc() )
1551 main.log.info( self.name + " ::::::" )
andrewonlab867212a2014-10-22 20:13:38 -04001552 main.cleanup()
1553 main.exit()
1554
kelvin8ec71442015-01-15 16:57:00 -08001555 # Wrapper functions ****************
1556 # Wrapper functions use existing driver
1557 # functions and extends their use case.
1558 # For example, we may use the output of
1559 # a normal driver function, and parse it
1560 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001561
kelvin8ec71442015-01-15 16:57:00 -08001562 def get_all_intents_id( self ):
1563 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001564 Description:
1565 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001566 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001567 try:
kelvin8ec71442015-01-15 16:57:00 -08001568 # Obtain output of intents function
andrewonlab9a50dfe2014-10-17 17:22:31 -04001569 intents_str = self.intents()
1570 all_intent_list = []
1571 intent_id_list = []
1572
kelvin8ec71442015-01-15 16:57:00 -08001573 # Parse the intents output for ID's
1574 intents_list = [ s.strip() for s in intents_str.splitlines() ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001575 for intents in intents_list:
1576 if "onos>" in intents:
1577 continue
1578 elif "intents" in intents:
1579 continue
1580 else:
kelvin8ec71442015-01-15 16:57:00 -08001581 line_list = intents.split( " " )
1582 all_intent_list.append( line_list[ 0 ] )
1583
1584 all_intent_list = all_intent_list[ 1:-2 ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001585
1586 for intents in all_intent_list:
1587 if not intents:
1588 continue
1589 else:
kelvin8ec71442015-01-15 16:57:00 -08001590 intent_id_list.append( intents )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001591
1592 return intent_id_list
1593
1594 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001595 main.log.error( self.name + ": EOF exception found" )
1596 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001597 main.cleanup()
1598 main.exit()
1599 except:
kelvin8ec71442015-01-15 16:57:00 -08001600 main.log.info( self.name + " ::::::" )
1601 main.log.error( traceback.print_exc() )
1602 main.log.info( self.name + " ::::::" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001603 main.cleanup()
1604 main.exit()
1605
kelvin8ec71442015-01-15 16:57:00 -08001606 def get_all_devices_id( self ):
1607 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001608 Use 'devices' function to obtain list of all devices
1609 and parse the result to obtain a list of all device
1610 id's. Returns this list. Returns empty list if no
1611 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001612 List is ordered sequentially
1613
andrewonlab3e15ead2014-10-15 14:21:34 -04001614 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001615 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001616 the ids. By obtaining the list of device ids on the fly,
1617 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001618 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001619 try:
kelvin8ec71442015-01-15 16:57:00 -08001620 # Call devices and store result string
1621 devices_str = self.devices( json_format=False )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001622 id_list = []
kelvin8ec71442015-01-15 16:57:00 -08001623
andrewonlab7e4d2d32014-10-15 13:23:21 -04001624 if not devices_str:
kelvin8ec71442015-01-15 16:57:00 -08001625 main.log.info( "There are no devices to get id from" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001626 return id_list
kelvin8ec71442015-01-15 16:57:00 -08001627
1628 # Split the string into list by comma
1629 device_list = devices_str.split( "," )
1630 # Get temporary list of all arguments with string 'id='
1631 temp_list = [ dev for dev in device_list if "id=" in dev ]
1632 # Split list further into arguments before and after string
1633 # 'id='. Get the latter portion ( the actual device id ) and
andrewonlab7e4d2d32014-10-15 13:23:21 -04001634 # append to id_list
1635 for arg in temp_list:
kelvin8ec71442015-01-15 16:57:00 -08001636 id_list.append( arg.split( "id=" )[ 1 ] )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001637 return id_list
1638
1639 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001640 main.log.error( self.name + ": EOF exception found" )
1641 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001642 main.cleanup()
1643 main.exit()
1644 except:
kelvin8ec71442015-01-15 16:57:00 -08001645 main.log.info( self.name + " ::::::" )
1646 main.log.error( traceback.print_exc() )
1647 main.log.info( self.name + " ::::::" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001648 main.cleanup()
1649 main.exit()
1650
kelvin8ec71442015-01-15 16:57:00 -08001651 def get_all_nodes_id( self ):
1652 """
andrewonlab7c211572014-10-15 16:45:20 -04001653 Uses 'nodes' function to obtain list of all nodes
1654 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001655 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001656 Returns:
1657 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001658 """
andrewonlab7c211572014-10-15 16:45:20 -04001659 try:
1660 nodes_str = self.nodes()
1661 id_list = []
1662
1663 if not nodes_str:
kelvin8ec71442015-01-15 16:57:00 -08001664 main.log.info( "There are no nodes to get id from" )
andrewonlab7c211572014-10-15 16:45:20 -04001665 return id_list
1666
kelvin8ec71442015-01-15 16:57:00 -08001667 # Sample nodes_str output
1668 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001669
kelvin8ec71442015-01-15 16:57:00 -08001670 # Split the string into list by comma
1671 nodes_list = nodes_str.split( "," )
1672 temp_list = [ node for node in nodes_list if "id=" in node ]
andrewonlab7c211572014-10-15 16:45:20 -04001673 for arg in temp_list:
kelvin8ec71442015-01-15 16:57:00 -08001674 id_list.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001675
1676 return id_list
kelvin8ec71442015-01-15 16:57:00 -08001677
andrewonlab7c211572014-10-15 16:45:20 -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 )
andrewonlab7c211572014-10-15 16:45:20 -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 + " ::::::" )
andrewonlab7c211572014-10-15 16:45:20 -04001687 main.cleanup()
1688 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001689
kelvin8ec71442015-01-15 16:57:00 -08001690 def get_device( self, dpid=None ):
1691 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001692 Return the first device from the devices api whose 'id' contains 'dpid'
1693 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001694 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001695 import json
1696 try:
kelvin8ec71442015-01-15 16:57:00 -08001697 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001698 return None
1699 else:
kelvin8ec71442015-01-15 16:57:00 -08001700 dpid = dpid.replace( ':', '' )
Jon Halla91c4dc2014-10-22 12:57:04 -04001701 raw_devices = self.devices()
kelvin8ec71442015-01-15 16:57:00 -08001702 devices_json = json.loads( raw_devices )
1703 # search json for the device with dpid then return the device
Jon Halla91c4dc2014-10-22 12:57:04 -04001704 for device in devices_json:
kelvin8ec71442015-01-15 16:57:00 -08001705 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1706 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001707 return device
1708 return None
1709 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001710 main.log.error( self.name + ": EOF exception found" )
1711 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001712 main.cleanup()
1713 main.exit()
1714 except:
kelvin8ec71442015-01-15 16:57:00 -08001715 main.log.info( self.name + " ::::::" )
1716 main.log.error( traceback.print_exc() )
1717 main.log.info( self.name + " ::::::" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001718 main.cleanup()
1719 main.exit()
1720
kelvin8ec71442015-01-15 16:57:00 -08001721 def check_status( self, ip, numoswitch, numolink, log_level="info" ):
1722 """
1723 Checks the number of swithes & links that ONOS sees against the
1724 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001725 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001726
Jon Hall42db6dc2014-10-24 19:03:48 -04001727 Params: ip = ip used for the onos cli
1728 numoswitch = expected number of switches
1729 numlink = expected number of links
1730 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1731
1732
1733 log_level can
1734
kelvin8ec71442015-01-15 16:57:00 -08001735 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001736 main.FALSE if the numer of switches and links is incorrect,
1737 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001738 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001739 try:
kelvin8ec71442015-01-15 16:57:00 -08001740 topology = self.get_topology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001741 if topology == {}:
1742 return main.ERROR
1743 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001744 # Is the number of switches is what we expected
1745 devices = topology.get( 'devices', False )
1746 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001747 if devices == False or links == False:
1748 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001749 switch_check = ( int( devices ) == int( numoswitch ) )
1750 # Is the number of links is what we expected
1751 link_check = ( int( links ) == int( numolink ) )
1752 if ( switch_check and link_check ):
1753 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001754 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001755 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001756 result = main.TRUE
1757 else:
1758 output = output + \
kelvin8ec71442015-01-15 16:57:00 -08001759 "The number of links and switches does not match what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001760 result = main.FALSE
1761 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
kelvin8ec71442015-01-15 16:57:00 -08001762 % ( int( devices ), int( numoswitch ), int( links ), int( numolink ) )
Jon Hall42db6dc2014-10-24 19:03:48 -04001763 if log_level == "report":
kelvin8ec71442015-01-15 16:57:00 -08001764 main.log.report( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001765 elif log_level == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001766 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001767 else:
kelvin8ec71442015-01-15 16:57:00 -08001768 main.log.info( output )
1769 return result
Jon Hall42db6dc2014-10-24 19:03:48 -04001770 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001771 main.log.error( self.name + ": EOF exception found" )
1772 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001773 main.cleanup()
1774 main.exit()
1775 except:
kelvin8ec71442015-01-15 16:57:00 -08001776 main.log.info( self.name + " ::::::" )
1777 main.log.error( traceback.print_exc() )
1778 main.log.info( self.name + " ::::::" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001779 main.cleanup()
1780 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001781
kelvin8ec71442015-01-15 16:57:00 -08001782 def device_role( self, device_id, onos_node, role="master" ):
1783 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001784 Calls the device-role cli command.
1785 device_id must be the id of a device as seen in the onos devices command
1786 onos_node is the ip of one of the onos nodes in the cluster
1787 role must be either master, standby, or none
1788
Jon Hall94fd0472014-12-08 11:52:42 -08001789 Returns main.TRUE or main.FALSE based on argument verification.
Jon Hall983a1702014-10-28 18:44:22 -04001790 When device-role supports errors this should be extended to
Jon Hall1c9e8732014-10-27 19:29:27 -04001791 support that output
kelvin8ec71442015-01-15 16:57:00 -08001792 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001793 try:
kelvin8ec71442015-01-15 16:57:00 -08001794 # print "beginning device_role... \n\tdevice_id:" + device_id
1795 # print "\tonos_node: " + onos_node
1796 # print "\trole: "+ role
Jon Hall1c9e8732014-10-27 19:29:27 -04001797 if role.lower() == "master" or \
1798 role.lower() == "standby" or \
1799 role.lower() == "none":
kelvin8ec71442015-01-15 16:57:00 -08001800 self.handle.sendline( "" )
1801 self.handle.expect( "onos>" )
1802 self.handle.sendline( "device-role " +
1803 str( device_id ) + " " +
1804 str( onos_node ) + " " +
1805 str( role ) )
1806 i = self.handle.expect( [ "Error", "onos>" ] )
1807 if i == 0:
1808 output = str( self.handle.before )
1809 self.handle.expect( "onos>" )
1810 output = output + str( self.handle.before )
1811 main.log.error( self.name + ": " +
1812 output + '\033[0m' ) # end color output to escape any colours from the cli
1813 return main.ERROR
1814 self.handle.sendline( "" )
1815 self.handle.expect( "onos>" )
1816 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001817 else:
1818 return main.FALSE
1819
1820 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001821 main.log.error( self.name + ": EOF exception found" )
1822 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001823 main.cleanup()
1824 main.exit()
1825 except:
kelvin8ec71442015-01-15 16:57:00 -08001826 main.log.info( self.name + " ::::::" )
1827 main.log.error( traceback.print_exc() )
1828 main.log.info( self.name + " ::::::" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001829 main.cleanup()
1830 main.exit()
1831
kelvin8ec71442015-01-15 16:57:00 -08001832 def clusters( self, json_format=True ):
1833 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001834 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001835 Optional argument:
1836 * json_format - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001837 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001838 try:
kelvin8ec71442015-01-15 16:57:00 -08001839 self.handle.sendline( "" )
1840 self.handle.expect( "onos>" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001841
1842 if json_format:
kelvin8ec71442015-01-15 16:57:00 -08001843 self.handle.sendline( "clusters -j" )
1844 self.handle.expect( "clusters -j" )
1845 self.handle.expect( "onos>" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001846 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001847 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001848 handle variable here contains some ANSI escape color code
1849 sequences at the end which are invisible in the print command
1850 output. To make that escape sequence visible, use repr() function.
kelvin8ec71442015-01-15 16:57:00 -08001851 The repr( handle ) output when printed shows the ANSI escape sequences.
1852 In json.loads( somestring ), this somestring variable is actually
1853 repr( somestring ) and json.loads would fail with the escape sequence.
Jon Hall73cf9cc2014-11-20 22:28:38 -08001854 So we take off that escape sequence using
kelvin8ec71442015-01-15 16:57:00 -08001855 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
1856 handle1 = ansi_escape.sub( '', handle )
1857 """
1858 # print "repr(handle) =", repr( handle )
1859 ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' )
1860 handle1 = ansi_escape.sub( '', handle )
1861 # print "repr(handle1) = ", repr( handle1 )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001862 return handle1
1863 else:
kelvin8ec71442015-01-15 16:57:00 -08001864 self.handle.sendline( "clusters" )
1865 self.handle.expect( "onos>" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001866 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001867 # print "handle =",handle
Jon Hall73cf9cc2014-11-20 22:28:38 -08001868 return handle
1869 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001870 main.log.error( self.name + ": EOF exception found" )
1871 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001872 main.cleanup()
1873 main.exit()
1874 except:
kelvin8ec71442015-01-15 16:57:00 -08001875 main.log.info( self.name + " ::::::" )
1876 main.log.error( traceback.print_exc() )
1877 main.log.info( self.name + " ::::::" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001878 main.cleanup()
1879 main.exit()
1880
kelvin8ec71442015-01-15 16:57:00 -08001881 def election_test_leader( self ):
1882 """
Jon Hall94fd0472014-12-08 11:52:42 -08001883 * CLI command to get the current leader for the Election test application.
1884 #NOTE: Requires installation of the onos-app-election feature
1885 Returns: Node IP of the leader if one exists
1886 None if none exists
1887 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001888 """
Jon Hall94fd0472014-12-08 11:52:42 -08001889 try:
kelvin8ec71442015-01-15 16:57:00 -08001890 self.handle.sendline( "election-test-leader" )
1891 self.handle.expect( "election-test-leader" )
1892 self.handle.expect( "onos>" )
Jon Hall94fd0472014-12-08 11:52:42 -08001893 response = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001894 # Leader
1895 node_search = re.search(
1896 "The\scurrent\sleader\sfor\sthe\sElection\sapp\sis\s(?P<node>.+)\.",
1897 response )
Jon Hall94fd0472014-12-08 11:52:42 -08001898 if node_search:
kelvin8ec71442015-01-15 16:57:00 -08001899 node = node_search.group( 'node' )
1900 main.log.info( "Election-test-leader on " + str(
1901 self.name ) + " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001902 return node
kelvin8ec71442015-01-15 16:57:00 -08001903 # no leader
1904 null_search = re.search(
1905 "There\sis\scurrently\sno\sleader\selected\sfor\sthe\sElection\sapp",
1906 response )
Jon Hall94fd0472014-12-08 11:52:42 -08001907 if null_search:
kelvin8ec71442015-01-15 16:57:00 -08001908 main.log.info(
1909 "Election-test-leader found no leader on " +
1910 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001911 return None
kelvin8ec71442015-01-15 16:57:00 -08001912 # error
1913 if re.search( "Command\snot\sfound", response ):
1914 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001915 return main.FALSE
1916 else:
kelvin8ec71442015-01-15 16:57:00 -08001917 main.log.error(
1918 "Error in election_test_leader: unexpected response" )
1919 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001920 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001921
1922 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001923 main.log.error( self.name + ": EOF exception found" )
1924 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001925 main.cleanup()
1926 main.exit()
1927 except:
kelvin8ec71442015-01-15 16:57:00 -08001928 main.log.info( self.name + " ::::::" )
1929 main.log.error( traceback.print_exc() )
1930 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001931 main.cleanup()
1932 main.exit()
1933
kelvin8ec71442015-01-15 16:57:00 -08001934 def election_test_run( self ):
1935 """
Jon Hall94fd0472014-12-08 11:52:42 -08001936 * CLI command to run for leadership of the Election test application.
1937 #NOTE: Requires installation of the onos-app-election feature
1938 Returns: Main.TRUE on success
1939 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001940 """
Jon Hall94fd0472014-12-08 11:52:42 -08001941 try:
kelvin8ec71442015-01-15 16:57:00 -08001942 self.handle.sendline( "election-test-run" )
1943 self.handle.expect( "election-test-run" )
1944 self.handle.expect( "onos>" )
Jon Hall94fd0472014-12-08 11:52:42 -08001945 response = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001946 # success
1947 search = re.search(
1948 "Entering\sleadership\selections\sfor\sthe\sElection\sapp.",
1949 response )
Jon Hall94fd0472014-12-08 11:52:42 -08001950 if search:
kelvin8ec71442015-01-15 16:57:00 -08001951 main.log.info(
1952 self.name +
1953 " entering leadership elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001954 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001955 # error
1956 if re.search( "Command\snot\sfound", response ):
1957 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001958 return main.FALSE
1959 else:
kelvin8ec71442015-01-15 16:57:00 -08001960 main.log.error(
1961 "Error in election_test_run: unexpected response" )
1962 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001963 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001964
1965 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001966 main.log.error( self.name + ": EOF exception found" )
1967 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001968 main.cleanup()
1969 main.exit()
1970 except:
kelvin8ec71442015-01-15 16:57:00 -08001971 main.log.info( self.name + " ::::::" )
1972 main.log.error( traceback.print_exc() )
1973 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08001974 main.cleanup()
1975 main.exit()
1976
kelvin8ec71442015-01-15 16:57:00 -08001977 def election_test_withdraw( self ):
1978 """
Jon Hall94fd0472014-12-08 11:52:42 -08001979 * CLI command to withdraw the local node from leadership election for
1980 * the Election test application.
1981 #NOTE: Requires installation of the onos-app-election feature
1982 Returns: Main.TRUE on success
1983 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001984 """
Jon Hall94fd0472014-12-08 11:52:42 -08001985 try:
kelvin8ec71442015-01-15 16:57:00 -08001986 self.handle.sendline( "election-test-withdraw" )
1987 self.handle.expect( "election-test-withdraw" )
1988 self.handle.expect( "onos>" )
Jon Hall94fd0472014-12-08 11:52:42 -08001989 response = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001990 # success
1991 search = re.search(
1992 "Withdrawing\sfrom\sleadership\selections\sfor\sthe\sElection\sapp.",
1993 response )
Jon Hall94fd0472014-12-08 11:52:42 -08001994 if search:
kelvin8ec71442015-01-15 16:57:00 -08001995 main.log.info(
1996 self.name +
1997 " withdrawing from leadership elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001998 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001999 # error
2000 if re.search( "Command\snot\sfound", response ):
2001 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08002002 return main.FALSE
2003 else:
kelvin8ec71442015-01-15 16:57:00 -08002004 main.log.error(
2005 "Error in election_test_withdraw: unexpected response" )
2006 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08002007 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08002008
Jon Hall94fd0472014-12-08 11:52:42 -08002009 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002010 main.log.error( self.name + ": EOF exception found" )
2011 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08002012 main.cleanup()
2013 main.exit()
2014 except:
kelvin8ec71442015-01-15 16:57:00 -08002015 main.log.info( self.name + " ::::::" )
2016 main.log.error( traceback.print_exc() )
2017 main.log.info( self.name + " ::::::" )
Jon Hall94fd0472014-12-08 11:52:42 -08002018 main.cleanup()
2019 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04002020
andrewonlab7e4d2d32014-10-15 13:23:21 -04002021 #***********************************
kelvin8ec71442015-01-15 16:57:00 -08002022 def getDevicePortsEnabledCount( self, dpid ):
2023 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002024 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002025 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002026 try:
kelvin8ec71442015-01-15 16:57:00 -08002027 dpid = str( dpid )
2028 self.handle.sendline( "" )
2029 self.handle.expect( "onos>" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002030
kelvin8ec71442015-01-15 16:57:00 -08002031 self.handle.sendline( "onos:ports -e " + dpid + " | wc -l" )
2032 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002033 "No such device",
kelvin8ec71442015-01-15 16:57:00 -08002034 "onos>" ] )
2035
2036 # self.handle.sendline( "" )
2037 # self.handle.expect( "onos>" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002038
2039 output = self.handle.before
2040
2041 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08002042 main.log.error( "Error in getting ports" )
2043 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002044 else:
2045 result = output
2046 return result
kelvin8ec71442015-01-15 16:57:00 -08002047
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002048 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002049 main.log.error( self.name + ": EOF exception found" )
2050 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002051 main.cleanup()
2052 main.exit()
2053 except:
kelvin8ec71442015-01-15 16:57:00 -08002054 main.log.info( self.name + " ::::::" )
2055 main.log.error( traceback.print_exc() )
2056 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002057 main.cleanup()
2058 main.exit()
2059
kelvin8ec71442015-01-15 16:57:00 -08002060 def getDeviceLinksActiveCount( self, dpid ):
2061 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002062 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002063 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002064 try:
kelvin8ec71442015-01-15 16:57:00 -08002065 dpid = str( dpid )
2066 self.handle.sendline( "" )
2067 self.handle.expect( "onos>" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002068
kelvin8ec71442015-01-15 16:57:00 -08002069 self.handle.sendline(
2070 "onos:links " +
2071 dpid +
2072 " | grep ACTIVE | wc -l" )
2073 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002074 "No such device",
kelvin8ec71442015-01-15 16:57:00 -08002075 "onos>" ] )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002076
2077 output = self.handle.before
2078
2079 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08002080 main.log.error( "Error in getting ports" )
2081 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002082 else:
2083 result = output
2084 return result
kelvin8ec71442015-01-15 16:57:00 -08002085
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002086 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002087 main.log.error( self.name + ": EOF exception found" )
2088 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002089 main.cleanup()
2090 main.exit()
2091 except:
kelvin8ec71442015-01-15 16:57:00 -08002092 main.log.info( self.name + " ::::::" )
2093 main.log.error( traceback.print_exc() )
2094 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002095 main.cleanup()
2096 main.exit()
2097
kelvin8ec71442015-01-15 16:57:00 -08002098 def getAllIntentIds( self ):
2099 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002100 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002101 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002102 try:
kelvin8ec71442015-01-15 16:57:00 -08002103 self.handle.sendline( "" )
2104 self.handle.expect( "onos>" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002105
kelvin8ec71442015-01-15 16:57:00 -08002106 self.handle.sendline( "onos:intents | grep id=" )
2107 i = self.handle.expect( [
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002108 "Error",
kelvin8ec71442015-01-15 16:57:00 -08002109 "onos>" ] )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002110
2111 output = self.handle.before
2112
2113 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08002114 main.log.error( "Error in getting ports" )
2115 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002116 else:
2117 result = output
2118 return result
kelvin8ec71442015-01-15 16:57:00 -08002119
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002120 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002121 main.log.error( self.name + ": EOF exception found" )
2122 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002123 main.cleanup()
2124 main.exit()
2125 except:
kelvin8ec71442015-01-15 16:57:00 -08002126 main.log.info( self.name + " ::::::" )
2127 main.log.error( traceback.print_exc() )
2128 main.log.info( self.name + " ::::::" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002129 main.cleanup()
2130 main.exit()
kelvin8ec71442015-01-15 16:57:00 -08002131