blob: aa85b8c387950a4c60462ca70ff0e38c377e8cc2 [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
Jon Hall3f45d112015-02-24 16:42:56 -080022import json
kelvin8ec71442015-01-15 16:57:00 -080023sys.path.append( "../" )
andrewonlab95ce8322014-10-13 14:12:04 -040024from drivers.common.clidriver import CLI
25
andrewonlab95ce8322014-10-13 14:12:04 -040026
Jon Hall3f45d112015-02-24 16:42:56 -080027
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
kelvin-onlabd6634ac2015-01-29 14:23:10 -080048 if self.home == None or self.home == "":
49 self.home = "~/ONOS"
50
kelvin8ec71442015-01-15 16:57:00 -080051 self.name = self.options[ 'name' ]
52 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080053 user_name=self.user_name,
54 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -080055 port=self.port,
56 pwd=self.pwd,
57 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -040058
kelvin8ec71442015-01-15 16:57:00 -080059 self.handle.sendline( "cd " + self.home )
60 self.handle.expect( "\$" )
andrewonlab95ce8322014-10-13 14:12:04 -040061 if self.handle:
62 return self.handle
kelvin8ec71442015-01-15 16:57:00 -080063 else:
64 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -040065 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -080066 except TypeError:
67 main.log.exception( self.name + ": Object not as expected" )
68 return None
andrewonlab95ce8322014-10-13 14:12:04 -040069 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -080070 main.log.error( self.name + ": EOF exception found" )
71 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -040072 main.cleanup()
73 main.exit()
74 except:
Jon Halld4d4b372015-01-28 16:02:41 -080075 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -040076 main.cleanup()
77 main.exit()
78
kelvin8ec71442015-01-15 16:57:00 -080079 def disconnect( self ):
80 """
andrewonlab95ce8322014-10-13 14:12:04 -040081 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -080082 """
Jon Halld61331b2015-02-17 16:35:47 -080083 response = main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -040084 try:
kelvin8ec71442015-01-15 16:57:00 -080085 self.handle.sendline( "" )
86 i = self.handle.expect( [ "onos>", "\$" ] )
Jon Hall7e5b9172014-10-22 12:32:47 -040087 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -080088 self.handle.sendline( "system:shutdown" )
89 self.handle.expect( "Confirm" )
90 self.handle.sendline( "yes" )
91 self.handle.expect( "\$" )
92 self.handle.sendline( "" )
93 self.handle.expect( "\$" )
94 self.handle.sendline( "exit" )
95 self.handle.expect( "closed" )
andrewonlabc2d05aa2014-10-13 16:51:10 -040096
Jon Halld4d4b372015-01-28 16:02:41 -080097 except TypeError:
98 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -080099 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400100 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800101 main.log.error( self.name + ": EOF exception found" )
102 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400103 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800104 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400105 response = main.FALSE
106 return response
107
kelvin8ec71442015-01-15 16:57:00 -0800108 def logout( self ):
109 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500110 Sends 'logout' command to ONOS cli
kelvin8ec71442015-01-15 16:57:00 -0800111 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500112 try:
kelvin8ec71442015-01-15 16:57:00 -0800113 self.handle.sendline( "" )
114 i = self.handle.expect( [
andrewonlab9627f432014-11-14 12:45:10 -0500115 "onos>",
kelvin8ec71442015-01-15 16:57:00 -0800116 "\$" ], timeout=10 )
andrewonlab9627f432014-11-14 12:45:10 -0500117 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800118 self.handle.sendline( "logout" )
119 self.handle.expect( "\$" )
andrewonlab9627f432014-11-14 12:45:10 -0500120 elif i == 1:
121 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800122
Jon Halld4d4b372015-01-28 16:02:41 -0800123 except TypeError:
124 main.log.exception( self.name + ": Object not as expected" )
125 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500126 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800127 main.log.error( self.name + ": eof exception found" )
128 main.log.error( self.name + ": " +
129 self.handle.before )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500130 main.cleanup()
131 main.exit()
132 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800133 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab38d2b4a2014-11-13 16:28:47 -0500134 main.cleanup()
135 main.exit()
136
kelvin-onlabd3b64892015-01-20 13:26:24 -0800137 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800138 """
andrewonlab95ce8322014-10-13 14:12:04 -0400139 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800140
andrewonlab95ce8322014-10-13 14:12:04 -0400141 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800142 """
andrewonlab95ce8322014-10-13 14:12:04 -0400143 try:
144 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800145 main.log.error( "Must define cellname" )
andrewonlab95ce8322014-10-13 14:12:04 -0400146 main.cleanup()
147 main.exit()
148 else:
kelvin8ec71442015-01-15 16:57:00 -0800149 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800150 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800151 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400152 # and that this driver will have to change accordingly
Jon Hall1e03cb62015-02-19 12:07:12 -0800153 self.handle.expect( "ONOS_CELL" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800154 handleBefore = self.handle.before
155 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800156 # Get the rest of the handle
157 self.handle.sendline( "" )
158 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800159 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400160
kelvin-onlabd3b64892015-01-20 13:26:24 -0800161 main.log.info( "Cell call returned: " + handleBefore +
162 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400163
164 return main.TRUE
165
Jon Halld4d4b372015-01-28 16:02:41 -0800166 except TypeError:
167 main.log.exception( self.name + ": Object not as expected" )
168 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400169 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800170 main.log.error( self.name + ": eof exception found" )
171 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400172 main.cleanup()
173 main.exit()
174 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800175 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400176 main.cleanup()
177 main.exit()
kelvin8ec71442015-01-15 16:57:00 -0800178
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 def startOnosCli( self, ONOSIp, karafTimeout="" ):
kelvin8ec71442015-01-15 16:57:00 -0800180 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800181 karafTimeout is an optional arugument. karafTimeout value passed
182 by user would be used to set the current karaf shell idle timeout.
183 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800184 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800185 Below is an example to start a session with 60 seconds idle timeout
186 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800187
Hari Krishna25d42f72015-01-05 15:08:28 -0800188 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800189 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800190
kelvin-onlabd3b64892015-01-20 13:26:24 -0800191 Note: karafTimeout is left as str so that this could be read
192 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800193 """
andrewonlab95ce8322014-10-13 14:12:04 -0400194 try:
kelvin8ec71442015-01-15 16:57:00 -0800195 self.handle.sendline( "" )
196 x = self.handle.expect( [
197 "\$", "onos>" ], timeout=10 )
andrewonlab48829f62014-11-17 13:49:01 -0500198
199 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800200 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500201 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400202
kelvin8ec71442015-01-15 16:57:00 -0800203 # Wait for onos start ( -w ) and enter onos cli
kelvin-onlabd3b64892015-01-20 13:26:24 -0800204 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800205 i = self.handle.expect( [
206 "onos>",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800207 pexpect.TIMEOUT ], timeout=60 )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400208
209 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800210 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800211 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800212 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800213 "config:property-set -p org.apache.karaf.shell\
214 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800215 karafTimeout )
216 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800217 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800218 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400219 return main.TRUE
220 else:
kelvin8ec71442015-01-15 16:57:00 -0800221 # If failed, send ctrl+c to process and try again
222 main.log.info( "Starting CLI failed. Retrying..." )
223 self.handle.send( "\x03" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800224 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800225 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
226 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400227 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800228 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800229 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800230 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800231 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800232 "config:property-set -p org.apache.karaf.shell\
233 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800234 karafTimeout )
235 self.handle.expect( "\$" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800236 self.handle.sendline( "onos -w " + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800237 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400238 return main.TRUE
239 else:
kelvin8ec71442015-01-15 16:57:00 -0800240 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800241 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400242 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400243
Jon Halld4d4b372015-01-28 16:02:41 -0800244 except TypeError:
245 main.log.exception( self.name + ": Object not as expected" )
246 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400247 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800248 main.log.error( self.name + ": EOF exception found" )
249 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400250 main.cleanup()
251 main.exit()
252 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800253 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400254 main.cleanup()
255 main.exit()
256
kelvin-onlab338f5512015-02-06 10:53:16 -0800257 def log( self, cmdStr , level = "" ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800258 """
259 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800260 returns main.TRUE on success
261 returns main.FALSE if Error occured
262 Available level: DEBUG, TRACE, INFO, WARN, ERROR
263 Level defaults to INFO
kelvin-onlab9f541032015-02-04 16:19:53 -0800264 """
265 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800266 lvlStr = ""
267 if level:
268 lvlStr = "--level=" + level
269
kelvin-onlab9f541032015-02-04 16:19:53 -0800270 self.handle.sendline( "" )
271 self.handle.expect( "onos>" )
kelvin-onlab338f5512015-02-06 10:53:16 -0800272 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
kelvin-onlab9f541032015-02-04 16:19:53 -0800273 self.handle.expect( "onos>" )
274
275 response = self.handle.before
276 if re.search( "Error", response ):
277 return main.FALSE
278 return main.TRUE
279
280 except pexpect.EOF:
281 main.log.error( self.name + ": EOF exception found" )
282 main.log.error( self.name + ": " + self.handle.before )
283 main.cleanup()
284 main.exit()
285 except:
286 main.log.info( self.name + " ::::::" )
287 main.log.error( traceback.print_exc() )
288 main.log.info( self.name + " ::::::" )
289 main.cleanup()
290 main.exit()
291
kelvin-onlabd3b64892015-01-20 13:26:24 -0800292 def sendline( self, cmdStr ):
kelvin8ec71442015-01-15 16:57:00 -0800293 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800294 Send a completely user specified string to
295 the onos> prompt. Use this function if you have
andrewonlaba18f6bf2014-10-13 19:31:54 -0400296 a very specific command to send.
Jon Halle3f39ff2015-01-13 11:50:53 -0800297
andrewonlaba18f6bf2014-10-13 19:31:54 -0400298 Warning: There are no sanity checking to commands
299 sent using this method.
kelvin8ec71442015-01-15 16:57:00 -0800300 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400301 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800302
303 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
304 self.log( logStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800305 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -0800306 self.handle.expect( "onos>" )
Jon Hallaea67aa2015-01-23 13:30:57 -0800307 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
308 + self.name + "." )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400309
310 handle = self.handle.before
Jon Hall7bdfc122015-01-23 11:45:32 -0800311 # Remove control strings from output
312 ansiEscape = re.compile( r'\x1b[^m]*m' )
313 handle = ansiEscape.sub( '', handle )
Jon Hall44225f82015-01-23 13:45:14 -0800314 #Remove extra return chars that get added
Jon Hallaea67aa2015-01-23 13:30:57 -0800315 handle = re.sub( r"\s\r", "", handle )
316 handle = handle.strip()
Jon Hall7bdfc122015-01-23 11:45:32 -0800317 # parse for just the output, remove the cmd from handle
318 output = handle.split( cmdStr, 1 )[1]
kelvin8ec71442015-01-15 16:57:00 -0800319
andrewonlaba18f6bf2014-10-13 19:31:54 -0400320
Jon Hall7bdfc122015-01-23 11:45:32 -0800321 return output
Jon Halld4d4b372015-01-28 16:02:41 -0800322 except TypeError:
323 main.log.exception( self.name + ": Object not as expected" )
324 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400325 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800326 main.log.error( self.name + ": EOF exception found" )
327 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400328 main.cleanup()
329 main.exit()
330 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800331 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlaba18f6bf2014-10-13 19:31:54 -0400332 main.cleanup()
333 main.exit()
334
kelvin8ec71442015-01-15 16:57:00 -0800335 # IMPORTANT NOTE:
336 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800337 # the cli command changing 'a:b' with 'aB'.
338 # Ex ) onos:topology > onosTopology
339 # onos:links > onosLinks
340 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800341
kelvin-onlabd3b64892015-01-20 13:26:24 -0800342 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800343 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400344 Adds a new cluster node by ID and address information.
345 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800346 * nodeId
347 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400348 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800349 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800350 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400351 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800352 cmdStr = "add-node " + str( nodeId ) + " " +\
353 str( ONOSIp ) + " " + str( tcpPort )
354 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800355 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800356 main.log.error( "Error in adding node" )
357 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800358 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400359 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800360 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400361 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800362 except TypeError:
363 main.log.exception( self.name + ": Object not as expected" )
364 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400365 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800366 main.log.error( self.name + ": EOF exception found" )
367 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400368 main.cleanup()
369 main.exit()
370 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800371 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400372 main.cleanup()
373 main.exit()
374
kelvin-onlabd3b64892015-01-20 13:26:24 -0800375 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800376 """
andrewonlab86dc3082014-10-13 18:18:38 -0400377 Removes a cluster by ID
378 Issues command: 'remove-node [<node-id>]'
379 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800380 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800381 """
andrewonlab86dc3082014-10-13 18:18:38 -0400382 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400383
kelvin-onlabd3b64892015-01-20 13:26:24 -0800384 cmdStr = "remove-node " + str( nodeId )
385 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800386 # TODO: add error checking. Does ONOS give any errors?
andrewonlab86dc3082014-10-13 18:18:38 -0400387
388 return main.TRUE
Jon Halle3f39ff2015-01-13 11:50:53 -0800389
Jon Halld4d4b372015-01-28 16:02:41 -0800390 except TypeError:
391 main.log.exception( self.name + ": Object not as expected" )
392 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400393 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800394 main.log.error( self.name + ": EOF exception found" )
395 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400396 main.cleanup()
397 main.exit()
398 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800399 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400400 main.cleanup()
401 main.exit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400402
kelvin8ec71442015-01-15 16:57:00 -0800403 def nodes( self ):
404 """
andrewonlab7c211572014-10-15 16:45:20 -0400405 List the nodes currently visible
406 Issues command: 'nodes'
407 Returns: entire handle of list of nodes
kelvin8ec71442015-01-15 16:57:00 -0800408 """
andrewonlab7c211572014-10-15 16:45:20 -0400409 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800410 cmdStr = "nodes"
411 handle = self.sendline( cmdStr )
andrewonlab7c211572014-10-15 16:45:20 -0400412 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800413 except TypeError:
414 main.log.exception( self.name + ": Object not as expected" )
415 return None
andrewonlab7c211572014-10-15 16:45:20 -0400416 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800417 main.log.error( self.name + ": EOF exception found" )
418 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400419 main.cleanup()
420 main.exit()
421 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800422 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400423 main.cleanup()
424 main.exit()
425
kelvin8ec71442015-01-15 16:57:00 -0800426 def topology( self ):
427 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400428 Shows the current state of the topology
429 by issusing command: 'onos> onos:topology'
kelvin8ec71442015-01-15 16:57:00 -0800430 """
andrewonlab95ce8322014-10-13 14:12:04 -0400431 try:
Jon Halle3f39ff2015-01-13 11:50:53 -0800432 # either onos:topology or 'topology' will work in CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800433 cmdStr = "onos:topology"
434 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800435 main.log.info( "onos:topology returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400436 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800437 except TypeError:
438 main.log.exception( self.name + ": Object not as expected" )
439 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400440 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800441 main.log.error( self.name + ": EOF exception found" )
442 main.log.error( self.name + ": " + self.handle.before )
andrewonlab95ce8322014-10-13 14:12:04 -0400443 main.cleanup()
444 main.exit()
445 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800446 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab95ce8322014-10-13 14:12:04 -0400447 main.cleanup()
448 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800449
kelvin-onlabd3b64892015-01-20 13:26:24 -0800450 def featureInstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800451 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800452 Installs a specified feature
andrewonlabc2d05aa2014-10-13 16:51:10 -0400453 by issuing command: 'onos> feature:install <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800454 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400455 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800456 cmdStr = "feature:install " + str( featureStr )
457 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800458 # TODO: Check for possible error responses from karaf
andrewonlabc2d05aa2014-10-13 16:51:10 -0400459 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800460 except TypeError:
461 main.log.exception( self.name + ": Object not as expected" )
462 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400463 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800464 main.log.error( self.name + ": EOF exception found" )
465 main.log.error( self.name + ": " + self.handle.before )
466 main.log.report( "Failed to install feature" )
467 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400468 main.cleanup()
469 main.exit()
470 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800471 main.log.exception( self.name + ": Uncaught exception!" )
kelvin8ec71442015-01-15 16:57:00 -0800472 main.log.report( "Failed to install feature" )
473 main.log.report( "Exiting test" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400474 main.cleanup()
475 main.exit()
Jon Halle3f39ff2015-01-13 11:50:53 -0800476
kelvin-onlabd3b64892015-01-20 13:26:24 -0800477 def featureUninstall( self, featureStr ):
kelvin8ec71442015-01-15 16:57:00 -0800478 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400479 Uninstalls a specified feature
480 by issuing command: 'onos> feature:uninstall <feature_str>'
kelvin8ec71442015-01-15 16:57:00 -0800481 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400482 try:
shahshreya74cca802015-02-26 12:24:01 -0800483 cmdStr = 'feature:list -i | grep "' + featureStr + '"'
484 handle = self.sendline( cmdStr )
485 print "handle = ", handle
486 searchStr = featureStr + " is not installed"
487 if re.search( searchStr, handle ):
488 main.log.info( "Feature needs to be installed before uninstalling it" )
489 else:
490 cmdStr = "feature:uninstall " + str( featureStr )
491 self.sendline( cmdStr )
492 # TODO: Check for possible error responses from karaf
493 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800494 except TypeError:
495 main.log.exception( self.name + ": Object not as expected" )
496 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400497 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800498 main.log.error( self.name + ": EOF exception found" )
499 main.log.error( self.name + ": " + self.handle.before )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400500 main.cleanup()
501 main.exit()
502 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800503 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400504 main.cleanup()
505 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800506
kelvin-onlabd3b64892015-01-20 13:26:24 -0800507 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800508 """
Jon Hall7b02d952014-10-17 20:14:54 -0400509 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400510 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800511 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800512 """
andrewonlab86dc3082014-10-13 18:18:38 -0400513 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800514 if jsonFormat:
515 cmdStr = "devices -j"
516 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800517 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800518 handle variable here contains some ANSI escape color code
519 sequences at the end which are invisible in the print command
520 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800521 function. The repr( handle ) output when printed shows the
522 ANSI escape sequences. In json.loads( somestring ), this
523 somestring variable is actually repr( somestring ) and
Jon Halle3f39ff2015-01-13 11:50:53 -0800524 json.loads would fail with the escape sequence. So we take off
525 that escape sequence using:
526
kelvin-onlabd3b64892015-01-20 13:26:24 -0800527 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
528 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800529 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800530 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
531 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400532 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400533 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800534 cmdStr = "devices"
535 handle = self.sendline( cmdStr )
Jon Hallcd707292014-10-17 19:06:17 -0400536 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800537 except TypeError:
538 main.log.exception( self.name + ": Object not as expected" )
539 return None
andrewonlab7c211572014-10-15 16:45:20 -0400540 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800541 main.log.error( self.name + ": EOF exception found" )
542 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -0400543 main.cleanup()
544 main.exit()
545 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800546 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -0400547 main.cleanup()
548 main.exit()
549
kelvin-onlabd3b64892015-01-20 13:26:24 -0800550 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800551 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800552 This balances the devices across all controllers
553 by issuing command: 'onos> onos:balance-masters'
554 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800555 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800556 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800557 cmdStr = "onos:balance-masters"
558 self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800559 # TODO: Check for error responses from ONOS
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800560 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800561 except TypeError:
562 main.log.exception( self.name + ": Object not as expected" )
563 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800564 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800565 main.log.error( self.name + ": EOF exception found" )
566 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800567 main.cleanup()
568 main.exit()
569 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800570 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800571 main.cleanup()
572 main.exit()
573
kelvin-onlabd3b64892015-01-20 13:26:24 -0800574 def links( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800575 """
Jon Halle8217482014-10-17 13:49:14 -0400576 Lists all core links
577 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800578 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800579 """
Jon Halle8217482014-10-17 13:49:14 -0400580 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800581 if jsonFormat:
582 cmdStr = "links -j"
583 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800584 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800585 handle variable here contains some ANSI escape color code
586 sequences at the end which are invisible in the print command
587 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800588 function. The repr( handle ) output when printed shows the ANSI
589 escape sequences. In json.loads( somestring ), this somestring
590 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800591 fail with the escape sequence. So we take off that escape
592 sequence using:
593
kelvin-onlabd3b64892015-01-20 13:26:24 -0800594 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
595 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800596 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800597 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
598 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400599 return handle1
Jon Halle8217482014-10-17 13:49:14 -0400600 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800601 cmdStr = "links"
602 handle = self.sendline( cmdStr )
Jon Halla001c392014-10-17 18:50:59 -0400603 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800604 except TypeError:
605 main.log.exception( self.name + ": Object not as expected" )
606 return None
Jon Halle8217482014-10-17 13:49:14 -0400607 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800608 main.log.error( self.name + ": EOF exception found" )
609 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400610 main.cleanup()
611 main.exit()
612 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800613 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400614 main.cleanup()
615 main.exit()
616
kelvin-onlabd3b64892015-01-20 13:26:24 -0800617 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800618 """
Jon Halle8217482014-10-17 13:49:14 -0400619 Lists all ports
620 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800621 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800622 """
Jon Halle8217482014-10-17 13:49:14 -0400623 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800624 if jsonFormat:
625 cmdStr = "ports -j"
626 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800627 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800628 handle variable here contains some ANSI escape color code
629 sequences at the end which are invisible in the print command
630 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800631 function. The repr( handle ) output when printed shows the ANSI
632 escape sequences. In json.loads( somestring ), this somestring
633 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800634 fail with the escape sequence. So we take off that escape
635 sequence using the following commads:
636
kelvin-onlabd3b64892015-01-20 13:26:24 -0800637 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
638 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800639 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800640 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
641 handle1 = ansiEscape.sub( '', handle )
Jon Halla001c392014-10-17 18:50:59 -0400642 return handle1
643
Jon Halle8217482014-10-17 13:49:14 -0400644 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800645 cmdStr = "ports"
646 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800647 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800648 except TypeError:
649 main.log.exception( self.name + ": Object not as expected" )
650 return None
Jon Halle8217482014-10-17 13:49:14 -0400651 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800652 main.log.error( self.name + ": EOF exception found" )
653 main.log.error( self.name + ": " + self.handle.before )
Jon Halle8217482014-10-17 13:49:14 -0400654 main.cleanup()
655 main.exit()
656 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800657 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halle8217482014-10-17 13:49:14 -0400658 main.cleanup()
659 main.exit()
660
kelvin-onlabd3b64892015-01-20 13:26:24 -0800661 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800662 """
Jon Hall983a1702014-10-28 18:44:22 -0400663 Lists all devices and the controllers with roles assigned to them
664 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800665 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800666 """
andrewonlab7c211572014-10-15 16:45:20 -0400667 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800668 if jsonFormat:
669 cmdStr = "roles -j"
670 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800671 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800672 handle variable here contains some ANSI escape color code
673 sequences at the end which are invisible in the print command
674 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800675 function. The repr( handle ) output when printed shows the ANSI
676 escape sequences. In json.loads( somestring ), this somestring
677 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800678 fail with the escape sequence.
Jon Hallb1290e82014-11-18 16:17:48 -0500679
Jon Halle3f39ff2015-01-13 11:50:53 -0800680 So we take off that escape sequence using the following
681 commads:
682
kelvin-onlabd3b64892015-01-20 13:26:24 -0800683 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
684 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800685 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800686 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
687 handle1 = ansiEscape.sub( '', handle )
Jon Hall983a1702014-10-28 18:44:22 -0400688 return handle1
andrewonlab7c211572014-10-15 16:45:20 -0400689
andrewonlab7c211572014-10-15 16:45:20 -0400690 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800691 cmdStr = "roles"
692 handle = self.sendline( cmdStr )
Jon Hallffb386d2014-11-21 13:43:38 -0800693 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800694 except TypeError:
695 main.log.exception( self.name + ": Object not as expected" )
696 return None
Jon Hall983a1702014-10-28 18:44:22 -0400697 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800698 main.log.error( self.name + ": EOF exception found" )
699 main.log.error( self.name + ": " + self.handle.before )
Jon Hall983a1702014-10-28 18:44:22 -0400700 main.cleanup()
701 main.exit()
702 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800703 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall983a1702014-10-28 18:44:22 -0400704 main.cleanup()
705 main.exit()
706
kelvin-onlabd3b64892015-01-20 13:26:24 -0800707 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800708 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800709 Given the a string containing the json representation of the "roles"
710 cli command and a partial or whole device id, returns a json object
711 containing the roles output for the first device whose id contains
712 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -0400713
714 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -0800715 A dict of the role assignments for the given device or
716 None if no match
kelvin8ec71442015-01-15 16:57:00 -0800717 """
Jon Hall983a1702014-10-28 18:44:22 -0400718 try:
719 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800720 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -0400721 return None
722 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800723 rawRoles = self.roles()
724 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800725 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800726 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800727 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800728 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -0400729 return device
730 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800731 except TypeError:
732 main.log.exception( self.name + ": Object not as expected" )
733 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400734 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800735 main.log.error( self.name + ": EOF exception found" )
736 main.log.error( self.name + ": " + self.handle.before )
andrewonlab86dc3082014-10-13 18:18:38 -0400737 main.cleanup()
738 main.exit()
739 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800740 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab86dc3082014-10-13 18:18:38 -0400741 main.cleanup()
742 main.exit()
Jon Hall94fd0472014-12-08 11:52:42 -0800743
kelvin-onlabd3b64892015-01-20 13:26:24 -0800744 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -0800745 """
Jon Hall94fd0472014-12-08 11:52:42 -0800746 Iterates through each device and checks if there is a master assigned
747 Returns: main.TRUE if each device has a master
748 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -0800749 """
Jon Hall94fd0472014-12-08 11:52:42 -0800750 try:
751 import json
kelvin-onlabd3b64892015-01-20 13:26:24 -0800752 rawRoles = self.roles()
753 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -0800754 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800755 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -0800756 # print device
757 if device[ 'master' ] == "none":
758 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -0800759 return main.FALSE
760 return main.TRUE
761
Jon Halld4d4b372015-01-28 16:02:41 -0800762 except TypeError:
763 main.log.exception( self.name + ": Object not as expected" )
764 return None
Jon Hall94fd0472014-12-08 11:52:42 -0800765 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800766 main.log.error( self.name + ": EOF exception found" )
767 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -0800768 main.cleanup()
769 main.exit()
770 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800771 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -0800772 main.cleanup()
773 main.exit()
774
kelvin-onlabd3b64892015-01-20 13:26:24 -0800775 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -0800776 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400777 Returns string of paths, and the cost.
778 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -0800779 """
andrewonlab3e15ead2014-10-15 14:21:34 -0400780 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800781 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
782 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -0800783 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -0800784 main.log.error( "Error in getting paths" )
785 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400786 else:
kelvin8ec71442015-01-15 16:57:00 -0800787 path = handle.split( ";" )[ 0 ]
788 cost = handle.split( ";" )[ 1 ]
789 return ( path, cost )
Jon Halld4d4b372015-01-28 16:02:41 -0800790 except TypeError:
791 main.log.exception( self.name + ": Object not as expected" )
792 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400793 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800794 main.log.error( self.name + ": EOF exception found" )
795 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3e15ead2014-10-15 14:21:34 -0400796 main.cleanup()
797 main.exit()
798 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800799 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3e15ead2014-10-15 14:21:34 -0400800 main.cleanup()
801 main.exit()
Jon Hallffb386d2014-11-21 13:43:38 -0800802
kelvin-onlabd3b64892015-01-20 13:26:24 -0800803 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800804 """
Jon Hallffb386d2014-11-21 13:43:38 -0800805 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -0400806 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800807 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800808 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400809 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800810 if jsonFormat:
811 cmdStr = "hosts -j"
812 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800813 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800814 handle variable here contains some ANSI escape color code
815 sequences at the end which are invisible in the print command
816 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -0800817 function. The repr( handle ) output when printed shows the ANSI
818 escape sequences. In json.loads( somestring ), this somestring
819 variable is actually repr( somestring ) and json.loads would
Jon Halle3f39ff2015-01-13 11:50:53 -0800820 fail with the escape sequence. So we take off that escape
821 sequence using:
822
kelvin-onlabd3b64892015-01-20 13:26:24 -0800823 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
824 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800825 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800826 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
827 handle1 = ansiEscape.sub( '', handle )
Jon Hall42db6dc2014-10-24 19:03:48 -0400828 return handle1
829 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800830 cmdStr = "hosts"
831 handle = self.sendline( cmdStr )
Jon Hall42db6dc2014-10-24 19:03:48 -0400832 return handle
Jon Halld4d4b372015-01-28 16:02:41 -0800833 except TypeError:
834 main.log.exception( self.name + ": Object not as expected" )
835 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400836 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800837 main.log.error( self.name + ": EOF exception found" )
838 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400839 main.cleanup()
840 main.exit()
841 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800842 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400843 main.cleanup()
844 main.exit()
845
kelvin-onlabd3b64892015-01-20 13:26:24 -0800846 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -0800847 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400848 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -0800849
850 Note: mac must be a colon seperated mac address, but could be a
851 partial mac address
852
Jon Hall42db6dc2014-10-24 19:03:48 -0400853 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -0800854 """
Jon Hall42db6dc2014-10-24 19:03:48 -0400855 import json
856 try:
kelvin8ec71442015-01-15 16:57:00 -0800857 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -0400858 return None
859 else:
860 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -0800861 rawHosts = self.hosts()
862 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -0800863 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -0800864 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -0800865 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -0800866 if not host:
867 pass
868 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -0400869 return host
870 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800871 except TypeError:
872 main.log.exception( self.name + ": Object not as expected" )
873 return None
Jon Hall42db6dc2014-10-24 19:03:48 -0400874 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800875 main.log.error( self.name + ": EOF exception found" )
876 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -0400877 main.cleanup()
878 main.exit()
879 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800880 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -0400881 main.cleanup()
882 main.exit()
883
kelvin-onlabd3b64892015-01-20 13:26:24 -0800884 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -0800885 """
886 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -0400887 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -0800888
andrewonlab3f0a4af2014-10-17 12:25:14 -0400889 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800890 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -0400891 IMPORTANT:
892 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -0800893 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -0400894 Furthermore, it assumes that value of VLAN is '-1'
895 Description:
kelvin8ec71442015-01-15 16:57:00 -0800896 Converts mininet hosts ( h1, h2, h3... ) into
897 ONOS format ( 00:00:00:00:00:01/-1 , ... )
898 """
andrewonlab3f0a4af2014-10-17 12:25:14 -0400899 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800900 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -0400901
kelvin-onlabd3b64892015-01-20 13:26:24 -0800902 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -0800903 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800904 hostHex = hex( int( host ) ).zfill( 12 )
905 hostHex = str( hostHex ).replace( 'x', '0' )
906 i = iter( str( hostHex ) )
907 hostHex = ":".join( a + b for a, b in zip( i, i ) )
908 hostHex = hostHex + "/-1"
909 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400910
kelvin-onlabd3b64892015-01-20 13:26:24 -0800911 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -0400912
Jon Halld4d4b372015-01-28 16:02:41 -0800913 except TypeError:
914 main.log.exception( self.name + ": Object not as expected" )
915 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -0400916 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800917 main.log.error( self.name + ": EOF exception found" )
918 main.log.error( self.name + ": " + self.handle.before )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400919 main.cleanup()
920 main.exit()
921 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800922 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab3f0a4af2014-10-17 12:25:14 -0400923 main.cleanup()
924 main.exit()
andrewonlab3e15ead2014-10-15 14:21:34 -0400925
kelvin-onlabd3b64892015-01-20 13:26:24 -0800926 def addHostIntent( self, hostIdOne, hostIdTwo ):
kelvin8ec71442015-01-15 16:57:00 -0800927 """
andrewonlabe6745342014-10-17 14:29:13 -0400928 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800929 * hostIdOne: ONOS host id for host1
930 * hostIdTwo: ONOS host id for host2
andrewonlabe6745342014-10-17 14:29:13 -0400931 Description:
kelvin8ec71442015-01-15 16:57:00 -0800932 Adds a host-to-host intent ( bidrectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -0500933 specifying the two hosts.
kelvin8ec71442015-01-15 16:57:00 -0800934 """
andrewonlabe6745342014-10-17 14:29:13 -0400935 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800936 cmdStr = "add-host-intent " + str( hostIdOne ) +\
937 " " + str( hostIdTwo )
938 handle = self.sendline( cmdStr )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800939 if re.search( "Error", handle ):
940 main.log.error( "Error in adding Host intent" )
941 return handle
942 else:
943 main.log.info( "Host intent installed between " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800944 str( hostIdOne ) + " and " + str( hostIdTwo ) )
Hari Krishnaac4e1782015-01-26 12:09:12 -0800945 return main.TRUE
946
Jon Halld4d4b372015-01-28 16:02:41 -0800947 except TypeError:
948 main.log.exception( self.name + ": Object not as expected" )
949 return None
Hari Krishnaccfb0d52015-02-19 09:38:29 -0800950
andrewonlabe6745342014-10-17 14:29:13 -0400951 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800952 main.log.error( self.name + ": EOF exception found" )
953 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -0400954 main.cleanup()
955 main.exit()
956 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800957 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -0400958 main.cleanup()
959 main.exit()
960
kelvin-onlabd3b64892015-01-20 13:26:24 -0800961 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -0800962 """
andrewonlab7b31d232014-10-24 13:31:47 -0400963 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800964 * ingressDevice: device id of ingress device
965 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -0400966 Optional:
967 TODO: Still needs to be implemented via dev side
kelvin-onlab898a6c62015-01-16 14:13:53 -0800968 """
andrewonlab7b31d232014-10-24 13:31:47 -0400969 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800970 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
971 " " + str( egressDevice )
972 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -0800973 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -0800974 if re.search( "Error", handle ):
andrewonlab7b31d232014-10-24 13:31:47 -0400975 return handle
976 else:
977 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800978 except TypeError:
979 main.log.exception( self.name + ": Object not as expected" )
980 return None
andrewonlab7b31d232014-10-24 13:31:47 -0400981 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800982 main.log.error( self.name + ": EOF exception found" )
983 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7b31d232014-10-24 13:31:47 -0400984 main.cleanup()
985 main.exit()
986 except:
Jon Halld4d4b372015-01-28 16:02:41 -0800987 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7b31d232014-10-24 13:31:47 -0400988 main.cleanup()
989 main.exit()
990
kelvin-onlabd3b64892015-01-20 13:26:24 -0800991 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -0800992 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800993 ingressDevice,
994 egressDevice,
995 portIngress="",
996 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -0800997 ethType="",
998 ethSrc="",
999 ethDst="",
1000 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001001 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001002 ipProto="",
1003 ipSrc="",
1004 ipDst="",
1005 tcpSrc="",
1006 tcpDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001007 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001008 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001009 * ingressDevice: device id of ingress device
1010 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001011 Optional:
1012 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001013 * ethSrc: specify ethSrc ( i.e. src mac addr )
1014 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001015 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001016 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001017 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001018 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001019 * ipSrc: specify ip source address
1020 * ipDst: specify ip destination address
1021 * tcpSrc: specify tcp source port
1022 * tcpDst: specify tcp destination port
andrewonlab4dbb4d82014-10-17 18:22:31 -04001023 Description:
kelvin8ec71442015-01-15 16:57:00 -08001024 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001025 specifying device id's and optional fields
1026
Jon Halle3f39ff2015-01-13 11:50:53 -08001027 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001028 options developers provide for point-to-point
1029 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001030 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001031 try:
andrewonlab289e4b72014-10-21 21:24:18 -04001032 cmd = ""
1033
kelvin8ec71442015-01-15 16:57:00 -08001034 # If there are no optional arguments
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001035 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001036 and not bandwidth and not lambdaAlloc \
andrewonlabfa4ff502014-11-11 16:41:30 -05001037 and not ipProto and not ipSrc and not ipDst \
1038 and not tcpSrc and not tcpDst:
andrewonlab36af3822014-11-18 17:48:18 -05001039 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001040
andrewonlab289e4b72014-10-21 21:24:18 -04001041 else:
andrewonlab36af3822014-11-18 17:48:18 -05001042 cmd = "add-point-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001043
andrewonlab0c0a6772014-10-22 12:31:18 -04001044 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001045 cmd += " --ethType " + str( ethType )
andrewonlab289e4b72014-10-21 21:24:18 -04001046 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001047 cmd += " --ethSrc " + str( ethSrc )
1048 if ethDst:
1049 cmd += " --ethDst " + str( ethDst )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001050 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001051 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001052 if lambdaAlloc:
andrewonlabfa4ff502014-11-11 16:41:30 -05001053 cmd += " --lambda "
1054 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001055 cmd += " --ipProto " + str( ipProto )
andrewonlabfa4ff502014-11-11 16:41:30 -05001056 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001057 cmd += " --ipSrc " + str( ipSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001058 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001059 cmd += " --ipDst " + str( ipDst )
andrewonlabfa4ff502014-11-11 16:41:30 -05001060 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001061 cmd += " --tcpSrc " + str( tcpSrc )
andrewonlabfa4ff502014-11-11 16:41:30 -05001062 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001063 cmd += " --tcpDst " + str( tcpDst )
andrewonlab289e4b72014-10-21 21:24:18 -04001064
kelvin8ec71442015-01-15 16:57:00 -08001065 # Check whether the user appended the port
1066 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001067 if "/" in ingressDevice:
1068 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001069 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001070 if not portIngress:
kelvin8ec71442015-01-15 16:57:00 -08001071 main.log.error( "You must specify " +
1072 "the ingress port" )
1073 # TODO: perhaps more meaningful return
andrewonlab36af3822014-11-18 17:48:18 -05001074 return main.FALSE
1075
kelvin8ec71442015-01-15 16:57:00 -08001076 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001077 str( ingressDevice ) + "/" +\
1078 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001079
kelvin-onlabd3b64892015-01-20 13:26:24 -08001080 if "/" in egressDevice:
1081 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001082 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001083 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001084 main.log.error( "You must specify " +
1085 "the egress port" )
andrewonlab36af3822014-11-18 17:48:18 -05001086 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001087
kelvin8ec71442015-01-15 16:57:00 -08001088 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001089 str( egressDevice ) + "/" +\
1090 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001091
kelvin-onlab898a6c62015-01-16 14:13:53 -08001092 handle = self.sendline( cmd )
1093 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001094 main.log.error( "Error in adding point-to-point intent" )
Jon Hall47a93fb2015-01-06 16:46:06 -08001095 return main.FALSE
andrewonlab4dbb4d82014-10-17 18:22:31 -04001096 else:
1097 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001098 except TypeError:
1099 main.log.exception( self.name + ": Object not as expected" )
1100 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001101 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001102 main.log.error( self.name + ": EOF exception found" )
1103 main.log.error( self.name + ": " + self.handle.before )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001104 main.cleanup()
1105 main.exit()
1106 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001107 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab4dbb4d82014-10-17 18:22:31 -04001108 main.cleanup()
1109 main.exit()
1110
kelvin-onlabd3b64892015-01-20 13:26:24 -08001111 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001112 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001113 ingressDevice1,
1114 ingressDevice2,
1115 egressDevice,
1116 portIngress="",
1117 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001118 ethType="",
1119 ethSrc="",
1120 ethDst="",
1121 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001122 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001123 ipProto="",
1124 ipSrc="",
1125 ipDst="",
1126 tcpSrc="",
1127 tcpDst="",
1128 setEthSrc="",
1129 setEthDst="" ):
kelvin8ec71442015-01-15 16:57:00 -08001130 """
shahshreyad0c80432014-12-04 16:56:05 -08001131 Note:
Jon Halle3f39ff2015-01-13 11:50:53 -08001132 This function assumes that there would be 2 ingress devices and
1133 one egress device. For more number of ingress devices, this
1134 function needs to be modified
shahshreyad0c80432014-12-04 16:56:05 -08001135 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001136 * ingressDevice1: device id of ingress device1
1137 * ingressDevice2: device id of ingress device2
1138 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001139 Optional:
1140 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001141 * ethSrc: specify ethSrc ( i.e. src mac addr )
1142 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001143 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001144 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001145 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001146 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001147 * ipSrc: specify ip source address
1148 * ipDst: specify ip destination address
1149 * tcpSrc: specify tcp source port
1150 * tcpDst: specify tcp destination port
1151 * setEthSrc: action to Rewrite Source MAC Address
1152 * setEthDst: action to Rewrite Destination MAC Address
1153 Description:
kelvin8ec71442015-01-15 16:57:00 -08001154 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001155 specifying device id's and optional fields
1156
Jon Halle3f39ff2015-01-13 11:50:53 -08001157 NOTE: This function may change depending on the
shahshreyad0c80432014-12-04 16:56:05 -08001158 options developers provide for multipointpoint-to-singlepoint
1159 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001160 """
shahshreyad0c80432014-12-04 16:56:05 -08001161 try:
1162 cmd = ""
1163
kelvin8ec71442015-01-15 16:57:00 -08001164 # If there are no optional arguments
shahshreyad0c80432014-12-04 16:56:05 -08001165 if not ethType and not ethSrc and not ethDst\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001166 and not bandwidth and not lambdaAlloc\
Jon Halle3f39ff2015-01-13 11:50:53 -08001167 and not ipProto and not ipSrc and not ipDst\
1168 and not tcpSrc and not tcpDst and not setEthSrc\
1169 and not setEthDst:
shahshreyad0c80432014-12-04 16:56:05 -08001170 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001171
1172 else:
1173 cmd = "add-multi-to-single-intent"
Jon Halle3f39ff2015-01-13 11:50:53 -08001174
shahshreyad0c80432014-12-04 16:56:05 -08001175 if ethType:
kelvin8ec71442015-01-15 16:57:00 -08001176 cmd += " --ethType " + str( ethType )
shahshreyad0c80432014-12-04 16:56:05 -08001177 if ethSrc:
kelvin8ec71442015-01-15 16:57:00 -08001178 cmd += " --ethSrc " + str( ethSrc )
1179 if ethDst:
1180 cmd += " --ethDst " + str( ethDst )
shahshreyad0c80432014-12-04 16:56:05 -08001181 if bandwidth:
kelvin8ec71442015-01-15 16:57:00 -08001182 cmd += " --bandwidth " + str( bandwidth )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001183 if lambdaAlloc:
shahshreyad0c80432014-12-04 16:56:05 -08001184 cmd += " --lambda "
1185 if ipProto:
kelvin8ec71442015-01-15 16:57:00 -08001186 cmd += " --ipProto " + str( ipProto )
shahshreyad0c80432014-12-04 16:56:05 -08001187 if ipSrc:
kelvin8ec71442015-01-15 16:57:00 -08001188 cmd += " --ipSrc " + str( ipSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001189 if ipDst:
kelvin8ec71442015-01-15 16:57:00 -08001190 cmd += " --ipDst " + str( ipDst )
shahshreyad0c80432014-12-04 16:56:05 -08001191 if tcpSrc:
kelvin8ec71442015-01-15 16:57:00 -08001192 cmd += " --tcpSrc " + str( tcpSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001193 if tcpDst:
kelvin8ec71442015-01-15 16:57:00 -08001194 cmd += " --tcpDst " + str( tcpDst )
shahshreyad0c80432014-12-04 16:56:05 -08001195 if setEthSrc:
kelvin8ec71442015-01-15 16:57:00 -08001196 cmd += " --setEthSrc " + str( setEthSrc )
shahshreyad0c80432014-12-04 16:56:05 -08001197 if setEthDst:
kelvin8ec71442015-01-15 16:57:00 -08001198 cmd += " --setEthDst " + str( setEthDst )
shahshreyad0c80432014-12-04 16:56:05 -08001199
kelvin8ec71442015-01-15 16:57:00 -08001200 # Check whether the user appended the port
1201 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001202 if "/" in ingressDevice1:
1203 cmd += " " + str( ingressDevice1 )
shahshreyad0c80432014-12-04 16:56:05 -08001204 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001205 if not portIngress1:
kelvin8ec71442015-01-15 16:57:00 -08001206 main.log.error( "You must specify " +
1207 "the ingress port1" )
1208 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001209 return main.FALSE
1210
kelvin8ec71442015-01-15 16:57:00 -08001211 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001212 str( ingressDevice1 ) + "/" +\
1213 str( portIngress1 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001214
kelvin-onlabd3b64892015-01-20 13:26:24 -08001215 if "/" in ingressDevice2:
1216 cmd += " " + str( ingressDevice2 )
shahshreyad0c80432014-12-04 16:56:05 -08001217 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001218 if not portIngress2:
kelvin8ec71442015-01-15 16:57:00 -08001219 main.log.error( "You must specify " +
1220 "the ingress port2" )
1221 # TODO: perhaps more meaningful return
shahshreyad0c80432014-12-04 16:56:05 -08001222 return main.FALSE
1223
kelvin8ec71442015-01-15 16:57:00 -08001224 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001225 str( ingressDevice2 ) + "/" +\
1226 str( portIngress2 ) + " "
shahshreyad0c80432014-12-04 16:56:05 -08001227
kelvin-onlabd3b64892015-01-20 13:26:24 -08001228 if "/" in egressDevice:
1229 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001230 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001231 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001232 main.log.error( "You must specify " +
1233 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001234 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001235
kelvin8ec71442015-01-15 16:57:00 -08001236 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001237 str( egressDevice ) + "/" +\
1238 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001239 print "cmd= ", cmd
kelvin-onlab898a6c62015-01-16 14:13:53 -08001240 handle = self.sendline( cmd )
1241 if re.search( "Error", handle ):
kelvin8ec71442015-01-15 16:57:00 -08001242 main.log.error( "Error in adding point-to-point intent" )
shahshreyad0c80432014-12-04 16:56:05 -08001243 return self.handle
1244 else:
1245 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001246 except TypeError:
1247 main.log.exception( self.name + ": Object not as expected" )
1248 return None
shahshreyad0c80432014-12-04 16:56:05 -08001249 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001250 main.log.error( self.name + ": EOF exception found" )
1251 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -08001252 main.cleanup()
1253 main.exit()
1254 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001255 main.log.exception( self.name + ": Uncaught exception!" )
shahshreyad0c80432014-12-04 16:56:05 -08001256 main.cleanup()
1257 main.exit()
1258
kelvin-onlabd3b64892015-01-20 13:26:24 -08001259 def removeIntent( self, intentId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001260 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001261 Remove intent for specified intent id
Jon Halle3f39ff2015-01-13 11:50:53 -08001262
1263 Returns:
1264 main.False on error and
1265 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08001266 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001267 try:
shahshreyac033d022015-02-20 16:10:19 -08001268 cmdStr = "remove-intent org.onosproject.cli " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001269 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001270 if re.search( "Error", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001271 main.log.error( "Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001272 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04001273 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001274 # TODO: Should this be main.TRUE
1275 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001276 except TypeError:
1277 main.log.exception( self.name + ": Object not as expected" )
1278 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001279 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001280 main.log.error( self.name + ": EOF exception found" )
1281 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001282 main.cleanup()
1283 main.exit()
1284 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001285 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001286 main.cleanup()
1287 main.exit()
1288
kelvin-onlabd3b64892015-01-20 13:26:24 -08001289 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08001290 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08001291 NOTE: This method should be used after installing application:
1292 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08001293 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001294 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08001295 Description:
1296 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08001297 """
pingping-lin8b306ac2014-11-17 18:13:51 -08001298 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001299 if jsonFormat:
1300 cmdStr = "routes -j"
1301 handleTmp = self.sendline( cmdStr )
1302 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1303 handle = ansiEscape.sub( '', handleTmp )
pingping-lin8b306ac2014-11-17 18:13:51 -08001304 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001305 cmdStr = "routes"
1306 handle = self.sendline( cmdStr )
pingping-lin8b306ac2014-11-17 18:13:51 -08001307 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001308 except TypeError:
1309 main.log.exception( self.name + ": Object not as expected" )
1310 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08001311 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001312 main.log.error( self.name + ": EOF exception found" )
1313 main.log.error( self.name + ": " + self.handle.before )
pingping-lin8b306ac2014-11-17 18:13:51 -08001314 main.cleanup()
1315 main.exit()
1316 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001317 main.log.exception( self.name + ": Uncaught exception!" )
pingping-lin8b306ac2014-11-17 18:13:51 -08001318 main.cleanup()
1319 main.exit()
1320
kelvin-onlabd3b64892015-01-20 13:26:24 -08001321 def intents( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001322 """
andrewonlab377693f2014-10-21 16:00:30 -04001323 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001324 * jsonFormat: enable output formatting in json
andrewonlabe6745342014-10-17 14:29:13 -04001325 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001326 Obtain intents currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001327 """
andrewonlabe6745342014-10-17 14:29:13 -04001328 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001329 if jsonFormat:
1330 cmdStr = "intents -j"
1331 handle = self.sendline( cmdStr )
1332 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1333 handle = ansiEscape.sub( '', handle )
kelvin8ec71442015-01-15 16:57:00 -08001334 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001335 cmdStr = "intents"
1336 handle = self.sendline( cmdStr )
andrewonlabe6745342014-10-17 14:29:13 -04001337 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001338 except TypeError:
1339 main.log.exception( self.name + ": Object not as expected" )
1340 return None
andrewonlabe6745342014-10-17 14:29:13 -04001341 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001342 main.log.error( self.name + ": EOF exception found" )
1343 main.log.error( self.name + ": " + self.handle.before )
andrewonlabe6745342014-10-17 14:29:13 -04001344 main.cleanup()
1345 main.exit()
1346 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001347 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabe6745342014-10-17 14:29:13 -04001348 main.cleanup()
1349 main.exit()
1350
kelvin-onlabd3b64892015-01-20 13:26:24 -08001351 def flows( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001352 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001353 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001354 * jsonFormat: enable output formatting in json
Shreya Shah0f01c812014-10-26 20:15:28 -04001355 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001356 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08001357 """
Shreya Shah0f01c812014-10-26 20:15:28 -04001358 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001359 if jsonFormat:
1360 cmdStr = "flows -j"
1361 handle = self.sendline( cmdStr )
1362 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1363 handle = ansiEscape.sub( '', handle )
Shreya Shah0f01c812014-10-26 20:15:28 -04001364 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001365 cmdStr = "flows"
1366 handle = self.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001367 if re.search( "Error\sexecuting\scommand:", handle ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001368 main.log.error( self.name + ".flows() response: " +
1369 str( handle ) )
Shreya Shah0f01c812014-10-26 20:15:28 -04001370 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001371 except TypeError:
1372 main.log.exception( self.name + ": Object not as expected" )
1373 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04001374 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001375 main.log.error( self.name + ": EOF exception found" )
1376 main.log.error( self.name + ": " + self.handle.before )
Shreya Shah0f01c812014-10-26 20:15:28 -04001377 main.cleanup()
1378 main.exit()
1379 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001380 main.log.exception( self.name + ": Uncaught exception!" )
Shreya Shah0f01c812014-10-26 20:15:28 -04001381 main.cleanup()
1382 main.exit()
1383
kelvin-onlabd3b64892015-01-20 13:26:24 -08001384 def pushTestIntents( self, dpidSrc, dpidDst, numIntents,
1385 numMult="", appId="", report=True ):
kelvin8ec71442015-01-15 16:57:00 -08001386 """
andrewonlab87852b02014-11-19 18:44:19 -05001387 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08001388 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05001389 a specific point-to-point intent definition
1390 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001391 * dpidSrc: specify source dpid
1392 * dpidDst: specify destination dpid
1393 * numIntents: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05001394 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001395 * numMult: number multiplier for multiplying
andrewonlabb66dfa12014-12-02 15:51:10 -05001396 the number of intents specified
kelvin-onlabd3b64892015-01-20 13:26:24 -08001397 * appId: specify the application id init to further
andrewonlabb66dfa12014-12-02 15:51:10 -05001398 modularize the intents
andrewonlab87852b02014-11-19 18:44:19 -05001399 * report: default True, returns latency information
kelvin8ec71442015-01-15 16:57:00 -08001400 """
andrewonlab87852b02014-11-19 18:44:19 -05001401 try:
kelvin8ec71442015-01-15 16:57:00 -08001402 cmd = "push-test-intents " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001403 str( dpidSrc ) + " " + str( dpidDst ) + " " +\
1404 str( numIntents )
1405 if numMult:
1406 cmd += " " + str( numMult )
1407 # If app id is specified, then numMult
kelvin8ec71442015-01-15 16:57:00 -08001408 # must exist because of the way this command
kelvin-onlabd3b64892015-01-20 13:26:24 -08001409 if appId:
1410 cmd += " " + str( appId )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001411 handle = self.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001412 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1413 handle = ansiEscape.sub( '', handle )
andrewonlab87852b02014-11-19 18:44:19 -05001414 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001415 latResult = []
kelvin8ec71442015-01-15 16:57:00 -08001416 main.log.info( handle )
1417 # Split result by newline
1418 newline = handle.split( "\r\r\n" )
1419 # Ignore the first object of list, which is empty
1420 newline = newline[ 1: ]
1421 # Some sloppy parsing method to get the latency
andrewonlabb66dfa12014-12-02 15:51:10 -05001422 for result in newline:
kelvin8ec71442015-01-15 16:57:00 -08001423 result = result.split( ": " )
1424 # Append the first result of second parse
kelvin-onlabd3b64892015-01-20 13:26:24 -08001425 latResult.append( result[ 1 ].split( " " )[ 0 ] )
1426 main.log.info( latResult )
1427 return latResult
andrewonlab87852b02014-11-19 18:44:19 -05001428 else:
1429 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -08001430 except TypeError:
1431 main.log.exception( self.name + ": Object not as expected" )
1432 return None
andrewonlab87852b02014-11-19 18:44:19 -05001433 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001434 main.log.error( self.name + ": EOF exception found" )
1435 main.log.error( self.name + ": " + self.handle.before )
andrewonlab87852b02014-11-19 18:44:19 -05001436 main.cleanup()
1437 main.exit()
1438 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001439 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab87852b02014-11-19 18:44:19 -05001440 main.cleanup()
1441 main.exit()
1442
kelvin-onlabd3b64892015-01-20 13:26:24 -08001443 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001444 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001445 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001446 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001447 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001448 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001449 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001450 if jsonFormat:
1451 cmdStr = "intents-events-metrics -j"
1452 handle = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001453 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001454 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1455 handle = ansiEscape.sub( '', handle )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001456 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001457 cmdStr = "intents-events-metrics"
1458 handle = self.sendline( cmdStr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001459 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001460 except TypeError:
1461 main.log.exception( self.name + ": Object not as expected" )
1462 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001463 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001464 main.log.error( self.name + ": EOF exception found" )
1465 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001466 main.cleanup()
1467 main.exit()
1468 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001469 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001470 main.cleanup()
1471 main.exit()
Shreya Shah0f01c812014-10-26 20:15:28 -04001472
kelvin-onlabd3b64892015-01-20 13:26:24 -08001473 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001474 """
1475 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04001476 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001477 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08001478 """
andrewonlab867212a2014-10-22 20:13:38 -04001479 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001480 if jsonFormat:
1481 cmdStr = "topology-events-metrics -j"
1482 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001483 # Some color thing that we want to escape
kelvin-onlabd3b64892015-01-20 13:26:24 -08001484 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1485 handle = ansiEscape.sub( '', handle )
andrewonlab867212a2014-10-22 20:13:38 -04001486 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001487 cmdStr = "topology-events-metrics"
1488 handle = self.sendline( cmdStr )
andrewonlab867212a2014-10-22 20:13:38 -04001489 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001490 except TypeError:
1491 main.log.exception( self.name + ": Object not as expected" )
1492 return None
andrewonlab867212a2014-10-22 20:13:38 -04001493 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001494 main.log.error( self.name + ": EOF exception found" )
1495 main.log.error( self.name + ": " + self.handle.before )
andrewonlab867212a2014-10-22 20:13:38 -04001496 main.cleanup()
1497 main.exit()
1498 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001499 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab867212a2014-10-22 20:13:38 -04001500 main.cleanup()
1501 main.exit()
1502
kelvin8ec71442015-01-15 16:57:00 -08001503 # Wrapper functions ****************
1504 # Wrapper functions use existing driver
1505 # functions and extends their use case.
1506 # For example, we may use the output of
1507 # a normal driver function, and parse it
1508 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04001509
kelvin-onlabd3b64892015-01-20 13:26:24 -08001510 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001511 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001512 Description:
1513 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08001514 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04001515 try:
kelvin8ec71442015-01-15 16:57:00 -08001516 # Obtain output of intents function
kelvin-onlabd3b64892015-01-20 13:26:24 -08001517 intentsStr = self.intents()
1518 allIntentList = []
1519 intentIdList = []
andrewonlab9a50dfe2014-10-17 17:22:31 -04001520
kelvin8ec71442015-01-15 16:57:00 -08001521 # Parse the intents output for ID's
kelvin-onlabd3b64892015-01-20 13:26:24 -08001522 intentsList = [ s.strip() for s in intentsStr.splitlines() ]
1523 for intents in intentsList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001524 if "onos>" in intents:
1525 continue
1526 elif "intents" in intents:
1527 continue
1528 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001529 lineList = intents.split( " " )
1530 allIntentList.append( lineList[ 0 ] )
kelvin8ec71442015-01-15 16:57:00 -08001531
kelvin-onlabd3b64892015-01-20 13:26:24 -08001532 allIntentList = allIntentList[ 1:-2 ]
andrewonlab9a50dfe2014-10-17 17:22:31 -04001533
kelvin-onlabd3b64892015-01-20 13:26:24 -08001534 for intents in allIntentList:
andrewonlab9a50dfe2014-10-17 17:22:31 -04001535 if not intents:
1536 continue
1537 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001538 intentIdList.append( intents )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001539
kelvin-onlabd3b64892015-01-20 13:26:24 -08001540 return intentIdList
andrewonlab9a50dfe2014-10-17 17:22:31 -04001541
Jon Halld4d4b372015-01-28 16:02:41 -08001542 except TypeError:
1543 main.log.exception( self.name + ": Object not as expected" )
1544 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04001545 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001546 main.log.error( self.name + ": EOF exception found" )
1547 main.log.error( self.name + ": " + self.handle.before )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001548 main.cleanup()
1549 main.exit()
1550 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001551 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab9a50dfe2014-10-17 17:22:31 -04001552 main.cleanup()
1553 main.exit()
1554
shahshreya353268a2015-02-25 17:03:41 -08001555
shahshreya580840d2015-02-26 10:44:04 -08001556 def FlowAddedCount( self, id ):
shahshreya353268a2015-02-25 17:03:41 -08001557 """
1558 Determine the number of flow rules for the given device id that are
1559 in the added state
1560 """
1561 try:
1562 cmdStr = "flows any " + id + " | grep 'state=ADDED' | wc -l"
1563 handle = self.sendline( cmdStr )
1564 return handle
1565 except pexpect.EOF:
1566 main.log.error( self.name + ": EOF exception found" )
1567 main.log.error( self.name + ": " + self.handle.before )
1568 main.cleanup()
1569 main.exit()
1570 except:
1571 main.log.exception( self.name + ": Uncaught exception!" )
1572 main.cleanup()
1573 main.exit()
1574
1575
kelvin-onlabd3b64892015-01-20 13:26:24 -08001576 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001577 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001578 Use 'devices' function to obtain list of all devices
1579 and parse the result to obtain a list of all device
1580 id's. Returns this list. Returns empty list if no
1581 devices exist
kelvin8ec71442015-01-15 16:57:00 -08001582 List is ordered sequentially
1583
andrewonlab3e15ead2014-10-15 14:21:34 -04001584 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08001585 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04001586 the ids. By obtaining the list of device ids on the fly,
1587 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08001588 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04001589 try:
kelvin8ec71442015-01-15 16:57:00 -08001590 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08001591 devicesStr = self.devices( jsonFormat=False )
1592 idList = []
kelvin8ec71442015-01-15 16:57:00 -08001593
kelvin-onlabd3b64892015-01-20 13:26:24 -08001594 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08001595 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001596 return idList
kelvin8ec71442015-01-15 16:57:00 -08001597
1598 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001599 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08001600 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08001601 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08001602 # Split list further into arguments before and after string
1603 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08001604 # append to idList
1605 for arg in tempList:
1606 idList.append( arg.split( "id=" )[ 1 ] )
1607 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04001608
Jon Halld4d4b372015-01-28 16:02:41 -08001609 except TypeError:
1610 main.log.exception( self.name + ": Object not as expected" )
1611 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04001612 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001613 main.log.error( self.name + ": EOF exception found" )
1614 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001615 main.cleanup()
1616 main.exit()
1617 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001618 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7e4d2d32014-10-15 13:23:21 -04001619 main.cleanup()
1620 main.exit()
1621
kelvin-onlabd3b64892015-01-20 13:26:24 -08001622 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08001623 """
andrewonlab7c211572014-10-15 16:45:20 -04001624 Uses 'nodes' function to obtain list of all nodes
1625 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08001626 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04001627 Returns:
1628 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08001629 """
andrewonlab7c211572014-10-15 16:45:20 -04001630 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001631 nodesStr = self.nodes()
1632 idList = []
andrewonlab7c211572014-10-15 16:45:20 -04001633
kelvin-onlabd3b64892015-01-20 13:26:24 -08001634 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08001635 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001636 return idList
andrewonlab7c211572014-10-15 16:45:20 -04001637
kelvin-onlabd3b64892015-01-20 13:26:24 -08001638 # Sample nodesStr output
kelvin8ec71442015-01-15 16:57:00 -08001639 # id=local, address=127.0.0.1:9876, state=ACTIVE *
andrewonlab7c211572014-10-15 16:45:20 -04001640
kelvin8ec71442015-01-15 16:57:00 -08001641 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08001642 nodesList = nodesStr.split( "," )
1643 tempList = [ node for node in nodesList if "id=" in node ]
1644 for arg in tempList:
1645 idList.append( arg.split( "id=" )[ 1 ] )
andrewonlab7c211572014-10-15 16:45:20 -04001646
kelvin-onlabd3b64892015-01-20 13:26:24 -08001647 return idList
kelvin8ec71442015-01-15 16:57:00 -08001648
Jon Halld4d4b372015-01-28 16:02:41 -08001649 except TypeError:
1650 main.log.exception( self.name + ": Object not as expected" )
1651 return None
andrewonlab7c211572014-10-15 16:45:20 -04001652 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001653 main.log.error( self.name + ": EOF exception found" )
1654 main.log.error( self.name + ": " + self.handle.before )
andrewonlab7c211572014-10-15 16:45:20 -04001655 main.cleanup()
1656 main.exit()
1657 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001658 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab7c211572014-10-15 16:45:20 -04001659 main.cleanup()
1660 main.exit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04001661
kelvin-onlabd3b64892015-01-20 13:26:24 -08001662 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08001663 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001664 Return the first device from the devices api whose 'id' contains 'dpid'
1665 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001666 """
Jon Halla91c4dc2014-10-22 12:57:04 -04001667 import json
1668 try:
kelvin8ec71442015-01-15 16:57:00 -08001669 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04001670 return None
1671 else:
kelvin8ec71442015-01-15 16:57:00 -08001672 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001673 rawDevices = self.devices()
1674 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08001675 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001676 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08001677 # print "%s in %s?" % ( dpid, device[ 'id' ] )
1678 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04001679 return device
1680 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001681 except TypeError:
1682 main.log.exception( self.name + ": Object not as expected" )
1683 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04001684 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001685 main.log.error( self.name + ": EOF exception found" )
1686 main.log.error( self.name + ": " + self.handle.before )
Jon Halla91c4dc2014-10-22 12:57:04 -04001687 main.cleanup()
1688 main.exit()
1689 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001690 main.log.exception( self.name + ": Uncaught exception!" )
Jon Halla91c4dc2014-10-22 12:57:04 -04001691 main.cleanup()
1692 main.exit()
1693
kelvin-onlabd3b64892015-01-20 13:26:24 -08001694 def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08001695 """
1696 Checks the number of swithes & links that ONOS sees against the
1697 supplied values. By default this will report to main.log, but the
Jon Hall42db6dc2014-10-24 19:03:48 -04001698 log level can be specifid.
kelvin8ec71442015-01-15 16:57:00 -08001699
Jon Hall42db6dc2014-10-24 19:03:48 -04001700 Params: ip = ip used for the onos cli
1701 numoswitch = expected number of switches
1702 numlink = expected number of links
kelvin-onlabd3b64892015-01-20 13:26:24 -08001703 logLevel = level to log to. Currently accepts
1704 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04001705
1706
kelvin-onlabd3b64892015-01-20 13:26:24 -08001707 logLevel can
Jon Hall42db6dc2014-10-24 19:03:48 -04001708
kelvin8ec71442015-01-15 16:57:00 -08001709 Returns: main.TRUE if the number of switchs and links are correct,
Jon Hall42db6dc2014-10-24 19:03:48 -04001710 main.FALSE if the numer of switches and links is incorrect,
1711 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08001712 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001713 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001714 topology = self.getTopology( ip )
Jon Hall42db6dc2014-10-24 19:03:48 -04001715 if topology == {}:
1716 return main.ERROR
1717 output = ""
kelvin8ec71442015-01-15 16:57:00 -08001718 # Is the number of switches is what we expected
1719 devices = topology.get( 'devices', False )
1720 links = topology.get( 'links', False )
Jon Hall42db6dc2014-10-24 19:03:48 -04001721 if devices == False or links == False:
1722 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08001723 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08001724 # Is the number of links is what we expected
kelvin-onlabd3b64892015-01-20 13:26:24 -08001725 linkCheck = ( int( links ) == int( numolink ) )
1726 if ( switchCheck and linkCheck ):
kelvin8ec71442015-01-15 16:57:00 -08001727 # We expected the correct numbers
Jon Hall42db6dc2014-10-24 19:03:48 -04001728 output = output + "The number of links and switches match "\
kelvin8ec71442015-01-15 16:57:00 -08001729 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001730 result = main.TRUE
1731 else:
1732 output = output + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001733 "The number of links and switches does not matc\
1734 h what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04001735 result = main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001736 output = output + "\n ONOS sees %i devices (%i expected) \
1737 and %i links (%i expected)" % (
1738 int( devices ), int( numoswitch ), int( links ),
1739 int( numolink ) )
1740 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08001741 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001742 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08001743 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04001744 else:
kelvin8ec71442015-01-15 16:57:00 -08001745 main.log.info( output )
1746 return result
Jon Halld4d4b372015-01-28 16:02:41 -08001747 except TypeError:
1748 main.log.exception( self.name + ": Object not as expected" )
1749 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001750 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001751 main.log.error( self.name + ": EOF exception found" )
1752 main.log.error( self.name + ": " + self.handle.before )
Jon Hall42db6dc2014-10-24 19:03:48 -04001753 main.cleanup()
1754 main.exit()
1755 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001756 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall42db6dc2014-10-24 19:03:48 -04001757 main.cleanup()
1758 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001759
kelvin-onlabd3b64892015-01-20 13:26:24 -08001760 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08001761 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001762 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08001763 deviceId must be the id of a device as seen in the onos devices command
1764 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04001765 role must be either master, standby, or none
1766
Jon Halle3f39ff2015-01-13 11:50:53 -08001767 Returns:
1768 main.TRUE or main.FALSE based on argument verification and
1769 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001770 """
Jon Hall1c9e8732014-10-27 19:29:27 -04001771 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001772 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04001773 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08001774 cmdStr = "device-role " +\
1775 str( deviceId ) + " " +\
1776 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001777 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001778 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001779 if re.search( "Error", handle ):
1780 # end color output to escape any colours
1781 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08001782 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001783 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08001784 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08001785 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04001786 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001787 main.log.error( "Invalid 'role' given to device_role(). " +
1788 "Value was '" + str(role) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04001789 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001790 except TypeError:
1791 main.log.exception( self.name + ": Object not as expected" )
1792 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04001793 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001794 main.log.error( self.name + ": EOF exception found" )
1795 main.log.error( self.name + ": " + self.handle.before )
Jon Hall1c9e8732014-10-27 19:29:27 -04001796 main.cleanup()
1797 main.exit()
1798 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001799 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall1c9e8732014-10-27 19:29:27 -04001800 main.cleanup()
1801 main.exit()
1802
kelvin-onlabd3b64892015-01-20 13:26:24 -08001803 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001804 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001805 Lists all clusters
Jon Hallffb386d2014-11-21 13:43:38 -08001806 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001807 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001808 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001809 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001810 if jsonFormat:
1811 cmdStr = "clusters -j"
1812 handle = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001813 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08001814 handle variable here contains some ANSI escape color code
1815 sequences at the end which are invisible in the print command
Jon Halle3f39ff2015-01-13 11:50:53 -08001816 output. To make that escape sequence visible, use repr()
kelvin-onlab898a6c62015-01-16 14:13:53 -08001817 function. The repr( handle ) output when printed shows the ANSI
1818 escape sequences. In json.loads( somestring ), this somestring
kelvin-onlabd3b64892015-01-20 13:26:24 -08001819 variable is actually repr( somestring ) and json.loads would
1820 fail with the escape sequence. So we take off that escape
1821 sequence using:
Jon Halle3f39ff2015-01-13 11:50:53 -08001822
kelvin-onlabd3b64892015-01-20 13:26:24 -08001823 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1824 handle1 = ansiEscape.sub( '', handle )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001825 """
kelvin-onlabd3b64892015-01-20 13:26:24 -08001826 ansiEscape = re.compile( r'\r\r\n\x1b[^m]*m' )
1827 handle1 = ansiEscape.sub( '', handle )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001828 return handle1
1829 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001830 cmdStr = "clusters"
1831 handle = self.sendline( cmdStr )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001832 return handle
Jon Halld4d4b372015-01-28 16:02:41 -08001833 except TypeError:
1834 main.log.exception( self.name + ": Object not as expected" )
1835 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08001836 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001837 main.log.error( self.name + ": EOF exception found" )
1838 main.log.error( self.name + ": " + self.handle.before )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001839 main.cleanup()
1840 main.exit()
1841 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001842 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall73cf9cc2014-11-20 22:28:38 -08001843 main.cleanup()
1844 main.exit()
1845
kelvin-onlabd3b64892015-01-20 13:26:24 -08001846 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001847 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001848 CLI command to get the current leader for the Election test application
1849 NOTE: Requires installation of the onos-app-election feature
1850 Returns: Node IP of the leader if one exists
1851 None if none exists
1852 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001853 """
Jon Hall94fd0472014-12-08 11:52:42 -08001854 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001855 cmdStr = "election-test-leader"
1856 response = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001857 # Leader
1858 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001859 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08001860 nodeSearch = re.search( leaderPattern, response )
1861 if nodeSearch:
1862 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08001863 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001864 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08001865 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08001866 # no leader
1867 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001868 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001869 nullSearch = re.search( nullPattern, response )
1870 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08001871 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001872 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08001873 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08001874 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001875 errorPattern = "Command\snot\sfound"
kelvin-onlab898a6c62015-01-16 14:13:53 -08001876 if re.search( errorPattern, response ):
1877 main.log.error( "Election app is not loaded on " + self.name )
Jon Halle3f39ff2015-01-13 11:50:53 -08001878 # TODO: Should this be main.ERROR?
Jon Hall669173b2014-12-17 11:36:30 -08001879 return main.FALSE
1880 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08001881 main.log.error( "Error in election_test_leader: " +
1882 "unexpected response" )
kelvin8ec71442015-01-15 16:57:00 -08001883 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001884 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001885 except TypeError:
1886 main.log.exception( self.name + ": Object not as expected" )
1887 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001888 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001889 main.log.error( self.name + ": EOF exception found" )
1890 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001891 main.cleanup()
1892 main.exit()
1893 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001894 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001895 main.cleanup()
1896 main.exit()
1897
kelvin-onlabd3b64892015-01-20 13:26:24 -08001898 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001899 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001900 CLI command to run for leadership of the Election test application.
1901 NOTE: Requires installation of the onos-app-election feature
1902 Returns: Main.TRUE on success
1903 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001904 """
Jon Hall94fd0472014-12-08 11:52:42 -08001905 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001906 cmdStr = "election-test-run"
1907 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001908 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001909 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001910 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001911 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08001912 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08001913 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001914 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001915 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001916 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001917 errorPattern = "Command\snot\sfound"
1918 if re.search( errorPattern, response ):
1919 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001920 return main.FALSE
1921 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001922 main.log.error( "Error in election_test_run: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001923 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001924 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001925 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001926 except TypeError:
1927 main.log.exception( self.name + ": Object not as expected" )
1928 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001929 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001930 main.log.error( self.name + ": EOF exception found" )
1931 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001932 main.cleanup()
1933 main.exit()
1934 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001935 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001936 main.cleanup()
1937 main.exit()
1938
kelvin-onlabd3b64892015-01-20 13:26:24 -08001939 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08001940 """
Jon Hall94fd0472014-12-08 11:52:42 -08001941 * CLI command to withdraw the local node from leadership election for
1942 * the Election test application.
1943 #NOTE: Requires installation of the onos-app-election feature
1944 Returns: Main.TRUE on success
1945 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08001946 """
Jon Hall94fd0472014-12-08 11:52:42 -08001947 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001948 cmdStr = "election-test-withdraw"
1949 response = self.sendline( cmdStr )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001950 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08001951 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08001952 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08001953 if re.search( successPattern, response ):
1954 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001955 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08001956 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08001957 # error
Jon Halle3f39ff2015-01-13 11:50:53 -08001958 errorPattern = "Command\snot\sfound"
1959 if re.search( errorPattern, response ):
1960 main.log.error( "Election app is not loaded on " + self.name )
Jon Hall669173b2014-12-17 11:36:30 -08001961 return main.FALSE
1962 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001963 main.log.error( "Error in election_test_withdraw: " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08001964 "unexpected response" )
Jon Halle3f39ff2015-01-13 11:50:53 -08001965 main.log.error( repr( response ) )
Jon Hall669173b2014-12-17 11:36:30 -08001966 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -08001967 except TypeError:
1968 main.log.exception( self.name + ": Object not as expected" )
1969 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08001970 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001971 main.log.error( self.name + ": EOF exception found" )
1972 main.log.error( self.name + ": " + self.handle.before )
Jon Hall94fd0472014-12-08 11:52:42 -08001973 main.cleanup()
1974 main.exit()
1975 except:
Jon Halld4d4b372015-01-28 16:02:41 -08001976 main.log.exception( self.name + ": Uncaught exception!" )
Jon Hall94fd0472014-12-08 11:52:42 -08001977 main.cleanup()
1978 main.exit()
Jon Hall1c9e8732014-10-27 19:29:27 -04001979
kelvin8ec71442015-01-15 16:57:00 -08001980 def getDevicePortsEnabledCount( self, dpid ):
1981 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001982 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08001983 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001984 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08001985 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001986 cmdStr = "onos:ports -e " + dpid + " | wc -l"
1987 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08001988 if re.search( "No such device", output ):
1989 main.log.error( "Error in getting ports" )
1990 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001991 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08001992 return output
Jon Halld4d4b372015-01-28 16:02:41 -08001993 except TypeError:
1994 main.log.exception( self.name + ": Object not as expected" )
1995 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001996 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001997 main.log.error( self.name + ": EOF exception found" )
1998 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001999 main.cleanup()
2000 main.exit()
2001 except:
Jon Halld4d4b372015-01-28 16:02:41 -08002002 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002003 main.cleanup()
2004 main.exit()
2005
kelvin8ec71442015-01-15 16:57:00 -08002006 def getDeviceLinksActiveCount( self, dpid ):
2007 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002008 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08002009 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002010 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08002011 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002012 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
2013 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002014 if re.search( "No such device", output ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002015 main.log.error( "Error in getting ports " )
2016 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002017 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002018 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002019 except TypeError:
2020 main.log.exception( self.name + ": Object not as expected" )
2021 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002022 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002023 main.log.error( self.name + ": EOF exception found" )
2024 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002025 main.cleanup()
2026 main.exit()
2027 except:
Jon Halld4d4b372015-01-28 16:02:41 -08002028 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002029 main.cleanup()
2030 main.exit()
2031
kelvin8ec71442015-01-15 16:57:00 -08002032 def getAllIntentIds( self ):
2033 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002034 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08002035 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002036 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002037 cmdStr = "onos:intents | grep id="
2038 output = self.sendline( cmdStr )
Jon Halle3f39ff2015-01-13 11:50:53 -08002039 if re.search( "Error", output ):
2040 main.log.error( "Error in getting ports" )
2041 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002042 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002043 return output
Jon Halld4d4b372015-01-28 16:02:41 -08002044 except TypeError:
2045 main.log.exception( self.name + ": Object not as expected" )
2046 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002047 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002048 main.log.error( self.name + ": EOF exception found" )
2049 main.log.error( self.name + ": " + self.handle.before )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002050 main.cleanup()
2051 main.exit()
2052 except:
Jon Halld4d4b372015-01-28 16:02:41 -08002053 main.log.exception( self.name + ": Uncaught exception!" )
2054 main.cleanup()
2055 main.exit()
2056
Jon Hall3f45d112015-02-24 16:42:56 -08002057 def intentSummary( self ):
Jon Halld4d4b372015-01-28 16:02:41 -08002058 """
Jon Hall3f45d112015-02-24 16:42:56 -08002059 Returns a dictonary containing the current intent states and the count
Jon Halld4d4b372015-01-28 16:02:41 -08002060 """
Jon Halld4d4b372015-01-28 16:02:41 -08002061 try:
Jon Hall3f45d112015-02-24 16:42:56 -08002062 intents = self.intents( )
2063 intentStates = []
2064 out = []
2065 for intent in json.loads( intents ):
2066 intentStates.append( intent.get( 'state', None ) )
2067 for i in set( intentStates ):
2068 out.append( (i, intentStates.count( i ) ) )
2069 return dict( out )
Jon Halld4d4b372015-01-28 16:02:41 -08002070 except TypeError:
2071 main.log.exception( self.name + ": Object not as expected" )
2072 return None
2073 except pexpect.EOF:
2074 main.log.error( self.name + ": EOF exception found" )
2075 main.log.error( self.name + ": " + self.handle.before )
2076 main.cleanup()
2077 main.exit()
2078 except:
2079 main.log.exception( self.name + ": Uncaught exception!" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08002080 main.cleanup()
2081 main.exit()