blob: 416ecf28877a1092d9fc44ac379b07c5c49b139a [file] [log] [blame]
andrewonlab95ce8322014-10-13 14:12:04 -04001#!/usr/bin/env python
2
kelvin8ec71442015-01-15 16:57:00 -08003"""
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07004OCT 13 2014
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005Copyright 2014 Open Networking Foundation (ONF)
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07006
7Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
8the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
9or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
10
11 TestON is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 2 of the License, or
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000014 (at your option) any later version.
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -070015
16 TestON is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with TestON. If not, see <http://www.gnu.org/licenses/>.
23"""
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000024
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -070025"""
andrewonlab95ce8322014-10-13 14:12:04 -040026This driver enters the onos> prompt to issue commands.
27
kelvin8ec71442015-01-15 16:57:00 -080028Please follow the coding style demonstrated by existing
andrewonlab95ce8322014-10-13 14:12:04 -040029functions and document properly.
30
31If you are a contributor to the driver, please
32list your email here for future contact:
33
34jhall@onlab.us
35andrew@onlab.us
Jon Halle8217482014-10-17 13:49:14 -040036shreya@onlab.us
Jeremy Ronquillo818bc7c2017-08-09 17:14:53 +000037jeremyr@opennetworking.org
kelvin8ec71442015-01-15 16:57:00 -080038"""
andrewonlab95ce8322014-10-13 14:12:04 -040039import pexpect
40import re
Jon Hall30b82fa2015-03-04 17:15:43 -080041import json
42import types
Jon Hallbd16b922015-03-26 17:53:15 -070043import time
kelvin-onlaba4074292015-07-09 15:19:49 -070044import os
andrewonlab95ce8322014-10-13 14:12:04 -040045from drivers.common.clidriver import CLI
You Wangdb8cd0a2016-05-26 15:19:45 -070046from core.graph import Graph
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -070047from cStringIO import StringIO
48from itertools import izip
andrewonlab95ce8322014-10-13 14:12:04 -040049
kelvin8ec71442015-01-15 16:57:00 -080050class OnosCliDriver( CLI ):
andrewonlab95ce8322014-10-13 14:12:04 -040051
kelvin8ec71442015-01-15 16:57:00 -080052 def __init__( self ):
53 """
54 Initialize client
55 """
Jon Hallefbd9792015-03-05 16:11:36 -080056 self.name = None
57 self.home = None
58 self.handle = None
Devin Limdc78e202017-06-09 18:30:07 -070059 self.karafUser = None
60 self.karafPass = None
You Wangdb8cd0a2016-05-26 15:19:45 -070061 self.graph = Graph()
Devin Limdc78e202017-06-09 18:30:07 -070062 super( OnosCliDriver, self ).__init__()
kelvin8ec71442015-01-15 16:57:00 -080063
Jeremy Ronquillo82705492017-10-18 14:19:55 -070064 def checkOptions( self, var, defaultVar ):
Devin Limdc78e202017-06-09 18:30:07 -070065 if var is None or var == "":
66 return defaultVar
67 return var
Jeremy Ronquillo82705492017-10-18 14:19:55 -070068
kelvin8ec71442015-01-15 16:57:00 -080069 def connect( self, **connectargs ):
70 """
andrewonlab95ce8322014-10-13 14:12:04 -040071 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080072 """
andrewonlab95ce8322014-10-13 14:12:04 -040073 try:
74 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080075 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us658ec012015-03-11 15:13:09 -070076 self.home = "~/onos"
andrewonlab95ce8322014-10-13 14:12:04 -040077 for key in self.options:
78 if key == "home":
Devin Limdc78e202017-06-09 18:30:07 -070079 self.home = self.options[ key ]
80 elif key == "karaf_username":
81 self.karafUser = self.options[ key ]
82 elif key == "karaf_password":
83 self.karafPass = self.options[ key ]
84
Jeremy Ronquillo82705492017-10-18 14:19:55 -070085 self.home = self.checkOptions( self.home, "~/onos" )
86 self.karafUser = self.checkOptions( self.karafUser, self.user_name )
87 self.karafPass = self.checkOptions( self.karafPass, self.pwd )
andrewonlab95ce8322014-10-13 14:12:04 -040088
kelvin-onlaba4074292015-07-09 15:19:49 -070089 for key in self.options:
90 if key == 'onosIp':
91 self.onosIp = self.options[ 'onosIp' ]
92 break
93
kelvin8ec71442015-01-15 16:57:00 -080094 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070095
96 try:
Jon Hallc6793552016-01-19 14:18:37 -080097 if os.getenv( str( self.ip_address ) ) is not None:
kelvin-onlaba4074292015-07-09 15:19:49 -070098 self.ip_address = os.getenv( str( self.ip_address ) )
99 else:
100 main.log.info( self.name +
101 ": Trying to connect to " +
102 self.ip_address )
103
104 except KeyError:
105 main.log.info( "Invalid host name," +
106 " connecting to local host instead" )
107 self.ip_address = 'localhost'
108 except Exception as inst:
109 main.log.error( "Uncaught exception: " + str( inst ) )
110
kelvin8ec71442015-01-15 16:57:00 -0800111 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -0800112 user_name=self.user_name,
113 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800114 port=self.port,
115 pwd=self.pwd,
116 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -0400117
kelvin8ec71442015-01-15 16:57:00 -0800118 self.handle.sendline( "cd " + self.home )
Devin Limdc78e202017-06-09 18:30:07 -0700119 self.handle.expect( self.prompt )
andrewonlab95ce8322014-10-13 14:12:04 -0400120 if self.handle:
121 return self.handle
kelvin8ec71442015-01-15 16:57:00 -0800122 else:
123 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -0400124 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -0800125 except TypeError:
126 main.log.exception( self.name + ": Object not as expected" )
127 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400128 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800129 main.log.error( self.name + ": EOF exception found" )
130 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700131 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800132 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800133 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700134 main.cleanAndExit()
andrewonlab95ce8322014-10-13 14:12:04 -0400135
kelvin8ec71442015-01-15 16:57:00 -0800136 def disconnect( self ):
137 """
andrewonlab95ce8322014-10-13 14:12:04 -0400138 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800139 """
Jon Halld61331b2015-02-17 16:35:47 -0800140 response = main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400141 try:
Jon Hall61282e32015-03-19 11:34:11 -0700142 if self.handle:
143 i = self.logout()
144 if i == main.TRUE:
145 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700146 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700147 self.handle.sendline( "exit" )
148 self.handle.expect( "closed" )
Jon Halld4d4b372015-01-28 16:02:41 -0800149 except TypeError:
150 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -0800151 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400152 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800153 main.log.error( self.name + ": EOF exception found" )
154 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700155 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700156 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700157 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800158 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800159 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400160 response = main.FALSE
161 return response
162
kelvin8ec71442015-01-15 16:57:00 -0800163 def logout( self ):
164 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500165 Sends 'logout' command to ONOS cli
Jon Hall61282e32015-03-19 11:34:11 -0700166 Returns main.TRUE if exited CLI and
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000167 main.FALSE on timeout (not guranteed you are disconnected)
Jon Hall61282e32015-03-19 11:34:11 -0700168 None on TypeError
169 Exits test on unknown error or pexpect exits unexpectedly
kelvin8ec71442015-01-15 16:57:00 -0800170 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500171 try:
Jon Hall61282e32015-03-19 11:34:11 -0700172 if self.handle:
173 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700174 i = self.handle.expect( [ "onos>", self.prompt, pexpect.TIMEOUT ],
Jon Hall61282e32015-03-19 11:34:11 -0700175 timeout=10 )
176 if i == 0: # In ONOS CLI
177 self.handle.sendline( "logout" )
Devin Limdc78e202017-06-09 18:30:07 -0700178 j = self.handle.expect( [ self.prompt,
Jon Hallbfe00002016-04-05 10:23:54 -0700179 "Command not found:",
180 pexpect.TIMEOUT ] )
181 if j == 0: # Successfully logged out
182 return main.TRUE
183 elif j == 1 or j == 2:
184 # ONOS didn't fully load, and logout command isn't working
185 # or the command timed out
186 self.handle.send( "\x04" ) # send ctrl-d
Jon Hall64ab3bd2016-05-13 11:29:44 -0700187 try:
Devin Limdc78e202017-06-09 18:30:07 -0700188 self.handle.expect( self.prompt )
Jon Hall64ab3bd2016-05-13 11:29:44 -0700189 except pexpect.TIMEOUT:
190 main.log.error( "ONOS did not respond to 'logout' or CTRL-d" )
Jon Hallbfe00002016-04-05 10:23:54 -0700191 return main.TRUE
Jon Halle0f0b342017-04-18 11:43:47 -0700192 else: # some other output
Jon Hallbfe00002016-04-05 10:23:54 -0700193 main.log.warn( "Unknown repsonse to logout command: '{}'",
194 repr( self.handle.before ) )
195 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -0700196 elif i == 1: # not in CLI
197 return main.TRUE
198 elif i == 3: # Timeout
199 return main.FALSE
200 else:
andrewonlab9627f432014-11-14 12:45:10 -0500201 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800202 except TypeError:
203 main.log.exception( self.name + ": Object not as expected" )
204 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500205 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800206 main.log.error( self.name + ": eof exception found" )
Jon Hall61282e32015-03-19 11:34:11 -0700207 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700208 main.cleanAndExit()
Jon Hall61282e32015-03-19 11:34:11 -0700209 except ValueError:
Jon Hall5aa168b2015-03-23 14:23:09 -0700210 main.log.error( self.name +
211 "ValueError exception in logout method" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800212 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800213 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700214 main.cleanAndExit()
andrewonlab38d2b4a2014-11-13 16:28:47 -0500215
kelvin-onlabd3b64892015-01-20 13:26:24 -0800216 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800217 """
andrewonlab95ce8322014-10-13 14:12:04 -0400218 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800219
andrewonlab95ce8322014-10-13 14:12:04 -0400220 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800221 """
andrewonlab95ce8322014-10-13 14:12:04 -0400222 try:
223 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800224 main.log.error( "Must define cellname" )
Devin Lim44075962017-08-11 10:56:37 -0700225 main.cleanAndExit()
andrewonlab95ce8322014-10-13 14:12:04 -0400226 else:
kelvin8ec71442015-01-15 16:57:00 -0800227 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800228 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800229 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400230 # and that this driver will have to change accordingly
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700231 self.handle.expect( str( cellname ) )
andrew@onlab.usc400b112015-01-21 15:33:19 -0800232 handleBefore = self.handle.before
233 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800234 # Get the rest of the handle
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700235 self.handle.sendline( "" )
236 self.handle.expect( self.prompt )
andrew@onlab.usc400b112015-01-21 15:33:19 -0800237 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400238
kelvin-onlabd3b64892015-01-20 13:26:24 -0800239 main.log.info( "Cell call returned: " + handleBefore +
240 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400241
242 return main.TRUE
243
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 )
Devin Lim44075962017-08-11 10:56:37 -0700250 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800251 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800252 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700253 main.cleanAndExit()
kelvin8ec71442015-01-15 16:57:00 -0800254
pingping-lin57a56ce2015-05-20 16:43:48 -0700255 def startOnosCli( self, ONOSIp, karafTimeout="",
Chiyu Chengef109502016-11-21 15:51:38 -0800256 commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ):
kelvin8ec71442015-01-15 16:57:00 -0800257 """
Jon Hallefbd9792015-03-05 16:11:36 -0800258 karafTimeout is an optional argument. karafTimeout value passed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800259 by user would be used to set the current karaf shell idle timeout.
260 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800261 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800262 Below is an example to start a session with 60 seconds idle timeout
263 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800264
Hari Krishna25d42f72015-01-05 15:08:28 -0800265 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800266 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800267
kelvin-onlabd3b64892015-01-20 13:26:24 -0800268 Note: karafTimeout is left as str so that this could be read
269 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800270 """
You Wangf69ab392016-01-26 16:34:38 -0800271 self.onosIp = ONOSIp
andrewonlab95ce8322014-10-13 14:12:04 -0400272 try:
Jon Hall67253832016-12-05 09:47:13 -0800273 # Check if we are already in the cli
kelvin8ec71442015-01-15 16:57:00 -0800274 self.handle.sendline( "" )
275 x = self.handle.expect( [
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700276 self.prompt, "onos>" ], commandlineTimeout )
andrewonlab48829f62014-11-17 13:49:01 -0500277 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800278 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500279 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400280
Jon Hall67253832016-12-05 09:47:13 -0800281 # Not in CLI so login
Chiyu Chengef109502016-11-21 15:51:38 -0800282 if waitForStart:
Jeremy Ronquilloec916a42018-02-02 13:05:57 -0800283 # Wait for onos start ( onos-wait-for-start ) and enter onos cli
284 startCliCommand = "onos-wait-for-start "
Chiyu Chengef109502016-11-21 15:51:38 -0800285 else:
286 startCliCommand = "onos "
287 self.handle.sendline( startCliCommand + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800288 i = self.handle.expect( [
289 "onos>",
pingping-lin57a56ce2015-05-20 16:43:48 -0700290 pexpect.TIMEOUT ], onosStartTimeout )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400291
292 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800293 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800294 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800295 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800296 "config:property-set -p org.apache.karaf.shell\
297 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800298 karafTimeout )
Devin Limdc78e202017-06-09 18:30:07 -0700299 self.handle.expect( self.prompt )
Chiyu Chengef109502016-11-21 15:51:38 -0800300 self.handle.sendline( startCliCommand + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800301 self.handle.expect( "onos>" )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400302 return main.TRUE
303 else:
kelvin8ec71442015-01-15 16:57:00 -0800304 # If failed, send ctrl+c to process and try again
305 main.log.info( "Starting CLI failed. Retrying..." )
306 self.handle.send( "\x03" )
Chiyu Chengef109502016-11-21 15:51:38 -0800307 self.handle.sendline( startCliCommand + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800308 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
309 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400310 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800311 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800312 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800313 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800314 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800315 "config:property-set -p org.apache.karaf.shell\
316 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800317 karafTimeout )
Devin Limdc78e202017-06-09 18:30:07 -0700318 self.handle.expect( self.prompt )
Chiyu Chengef109502016-11-21 15:51:38 -0800319 self.handle.sendline( startCliCommand + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800320 self.handle.expect( "onos>" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400321 return main.TRUE
322 else:
kelvin8ec71442015-01-15 16:57:00 -0800323 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800324 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400325 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400326
Jon Halld4d4b372015-01-28 16:02:41 -0800327 except TypeError:
328 main.log.exception( self.name + ": Object not as expected" )
329 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400330 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800331 main.log.error( self.name + ": EOF exception found" )
332 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700333 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800334 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800335 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700336 main.cleanAndExit()
andrewonlab95ce8322014-10-13 14:12:04 -0400337
suibin zhang116647a2016-05-06 16:30:09 -0700338 def startCellCli( self, karafTimeout="",
339 commandlineTimeout=10, onosStartTimeout=60 ):
340 """
341 Start CLI on onos ecll handle.
342
343 karafTimeout is an optional argument. karafTimeout value passed
344 by user would be used to set the current karaf shell idle timeout.
345 Note that when ever this property is modified the shell will exit and
346 the subsequent login would reflect new idle timeout.
347 Below is an example to start a session with 60 seconds idle timeout
348 ( input value is in milliseconds ):
349
350 tValue = "60000"
351
352 Note: karafTimeout is left as str so that this could be read
353 and passed to startOnosCli from PARAMS file as str.
354 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000355
suibin zhang116647a2016-05-06 16:30:09 -0700356 try:
357 self.handle.sendline( "" )
358 x = self.handle.expect( [
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700359 self.prompt, "onos>" ], commandlineTimeout )
suibin zhang116647a2016-05-06 16:30:09 -0700360
361 if x == 1:
362 main.log.info( "ONOS cli is already running" )
363 return main.TRUE
364
Jeremy Ronquilloec916a42018-02-02 13:05:57 -0800365 # Wait for onos start ( onos-wait-for-start ) and enter onos cli
suibin zhang116647a2016-05-06 16:30:09 -0700366 self.handle.sendline( "/opt/onos/bin/onos" )
367 i = self.handle.expect( [
368 "onos>",
369 pexpect.TIMEOUT ], onosStartTimeout )
370
371 if i == 0:
372 main.log.info( self.name + " CLI Started successfully" )
373 if karafTimeout:
374 self.handle.sendline(
375 "config:property-set -p org.apache.karaf.shell\
376 sshIdleTimeout " +
377 karafTimeout )
Devin Limdc78e202017-06-09 18:30:07 -0700378 self.handle.expect( self.prompt )
suibin zhang116647a2016-05-06 16:30:09 -0700379 self.handle.sendline( "/opt/onos/bin/onos" )
380 self.handle.expect( "onos>" )
381 return main.TRUE
382 else:
383 # If failed, send ctrl+c to process and try again
384 main.log.info( "Starting CLI failed. Retrying..." )
385 self.handle.send( "\x03" )
386 self.handle.sendline( "/opt/onos/bin/onos" )
387 i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ],
388 timeout=30 )
389 if i == 0:
390 main.log.info( self.name + " CLI Started " +
391 "successfully after retry attempt" )
392 if karafTimeout:
393 self.handle.sendline(
394 "config:property-set -p org.apache.karaf.shell\
395 sshIdleTimeout " +
396 karafTimeout )
Devin Limdc78e202017-06-09 18:30:07 -0700397 self.handle.expect( self.prompt )
suibin zhang116647a2016-05-06 16:30:09 -0700398 self.handle.sendline( "/opt/onos/bin/onos" )
399 self.handle.expect( "onos>" )
400 return main.TRUE
401 else:
402 main.log.error( "Connection to CLI " +
403 self.name + " timeout" )
404 return main.FALSE
405
406 except TypeError:
407 main.log.exception( self.name + ": Object not as expected" )
408 return None
409 except pexpect.EOF:
410 main.log.error( self.name + ": EOF exception found" )
411 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700412 main.cleanAndExit()
suibin zhang116647a2016-05-06 16:30:09 -0700413 except Exception:
414 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700415 main.cleanAndExit()
suibin zhang116647a2016-05-06 16:30:09 -0700416
Pratik Parab3b2ab5a2017-02-14 13:15:14 -0800417 def log( self, cmdStr, level="", noExit=False ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800418 """
419 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800420 returns main.TRUE on success
Jon Hallefbd9792015-03-05 16:11:36 -0800421 returns main.FALSE if Error occurred
YPZhangebf9eb52016-05-12 15:20:24 -0700422 if noExit is True, TestON will not exit, but clean up
kelvin-onlab338f5512015-02-06 10:53:16 -0800423 Available level: DEBUG, TRACE, INFO, WARN, ERROR
424 Level defaults to INFO
Pratik Parab3b2ab5a2017-02-14 13:15:14 -0800425 if cmdStr has spaces then put quotes in the passed string
kelvin-onlab9f541032015-02-04 16:19:53 -0800426 """
427 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800428 lvlStr = ""
429 if level:
430 lvlStr = "--level=" + level
431
kelvin-onlab338f5512015-02-06 10:53:16 -0800432 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -0700433 self.handle.expect( "log:log" )
kelvin-onlab9f541032015-02-04 16:19:53 -0800434 self.handle.expect( "onos>" )
kelvin-onlabfb521662015-02-27 09:52:40 -0800435
kelvin-onlab9f541032015-02-04 16:19:53 -0800436 response = self.handle.before
437 if re.search( "Error", response ):
438 return main.FALSE
439 return main.TRUE
Jon Hall80daded2015-05-27 16:07:00 -0700440 except pexpect.TIMEOUT:
441 main.log.exception( self.name + ": TIMEOUT exception found" )
YPZhangebf9eb52016-05-12 15:20:24 -0700442 if noExit:
443 main.cleanup()
444 return None
445 else:
Devin Lim44075962017-08-11 10:56:37 -0700446 main.cleanAndExit()
kelvin-onlab9f541032015-02-04 16:19:53 -0800447 except pexpect.EOF:
448 main.log.error( self.name + ": EOF exception found" )
449 main.log.error( self.name + ": " + self.handle.before )
YPZhangebf9eb52016-05-12 15:20:24 -0700450 if noExit:
451 main.cleanup()
452 return None
453 else:
Devin Lim44075962017-08-11 10:56:37 -0700454 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800455 except Exception:
kelvin-onlabfb521662015-02-27 09:52:40 -0800456 main.log.exception( self.name + ": Uncaught exception!" )
YPZhangebf9eb52016-05-12 15:20:24 -0700457 if noExit:
458 main.cleanup()
459 return None
460 else:
Devin Lim44075962017-08-11 10:56:37 -0700461 main.cleanAndExit()
andrewonlab95ce8322014-10-13 14:12:04 -0400462
Jon Hall0e240372018-05-02 11:21:57 -0700463 def clearBuffer( self, debug=False, timeout=10, noExit=False ):
kelvin8ec71442015-01-15 16:57:00 -0800464 """
Jon Hall0e240372018-05-02 11:21:57 -0700465 Test cli connection and clear any left over output in the buffer
466 Optional Arguments:
467 debug - Defaults to False. If True, will enable debug logging.
468 timeout - Defaults to 10. Amount of time in seconds for a command to return
469 before a timeout.
470 noExit - Defaults to False. If True, will not exit TestON in the event of a
kelvin8ec71442015-01-15 16:57:00 -0800471 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400472 try:
Jon Halla495f562016-05-16 18:03:26 -0700473 # Try to reconnect if disconnected from cli
474 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700475 i = self.handle.expect( [ "onos>", self.prompt, pexpect.TIMEOUT ] )
Jon Hall0e240372018-05-02 11:21:57 -0700476 response = self.handle.before
Jon Halla495f562016-05-16 18:03:26 -0700477 if i == 1:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700478 main.log.error( self.name + ": onos cli session closed. " )
Jon Halla495f562016-05-16 18:03:26 -0700479 if self.onosIp:
480 main.log.warn( "Trying to reconnect " + self.onosIp )
481 reconnectResult = self.startOnosCli( self.onosIp )
482 if reconnectResult:
483 main.log.info( self.name + ": onos cli session reconnected." )
484 else:
485 main.log.error( self.name + ": reconnection failed." )
YPZhang14a4aa92016-07-15 13:37:15 -0700486 if noExit:
487 return None
488 else:
Devin Lim44075962017-08-11 10:56:37 -0700489 main.cleanAndExit()
Jon Halla495f562016-05-16 18:03:26 -0700490 else:
Devin Lim44075962017-08-11 10:56:37 -0700491 main.cleanAndExit()
Jon Halla495f562016-05-16 18:03:26 -0700492 if i == 2:
Jon Hall7a6ebfd2017-03-13 10:58:58 -0700493 main.log.warn( "Timeout when testing cli responsiveness" )
494 main.log.debug( self.handle.before )
495 self.handle.send( "\x03" ) # Send ctrl-c to clear previous output
Jon Halla495f562016-05-16 18:03:26 -0700496 self.handle.expect( "onos>" )
497
Jon Hall0e240372018-05-02 11:21:57 -0700498 response += self.handle.before
Jon Hall14a03b52016-05-11 12:07:30 -0700499 if debug:
Jon Hall0e240372018-05-02 11:21:57 -0700500 main.log.debug( self.name + ": Raw output from sending ''" )
501 main.log.debug( self.name + ": " + repr( response ) )
502 except pexpect.TIMEOUT:
503 main.log.error( self.name + ": ONOS timeout" )
504 main.log.debug( self.handle.before )
You Wang141b43b2018-06-26 16:50:18 -0700505 self.handle.send( "\x03" )
506 self.handle.expect( "onos>" )
Jon Hall0e240372018-05-02 11:21:57 -0700507 return None
508 except pexpect.EOF:
509 main.log.error( self.name + ": EOF exception found" )
510 main.log.error( self.name + ": " + self.handle.before )
511 if noExit:
512 return None
513 else:
514 main.cleanAndExit()
515 except Exception:
516 main.log.exception( self.name + ": Uncaught exception!" )
517 if noExit:
518 return None
519 else:
520 main.cleanAndExit()
521
522 def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False ):
523 """
524 A wrapper around pexpect's sendline/expect. Will return all the output from a given command
525
526 Required Arguments:
527 cmdStr - String to send to the pexpect session
528
529 Optional Arguments:
530 showResponse - Defaults to False. If True will log the response.
531 debug - Defaults to False. If True, will enable debug logging.
532 timeout - Defaults to 10. Amount of time in seconds for a command to return
533 before a timeout.
534 noExit - Defaults to False. If True, will not exit TestON in the event of a
535 closed channel, but instead return None
536
537 Warning: There are no sanity checking to commands sent using this method.
538
539 """
540 try:
541 # Try to reconnect if disconnected from cli
542 self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit )
543 if debug:
544 # NOTE: This adds an average of .4 seconds per call
Jon Hall14a03b52016-05-11 12:07:30 -0700545 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
Jon Halle0f0b342017-04-18 11:43:47 -0700546 self.log( logStr, noExit=noExit )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800547 self.handle.sendline( cmdStr )
Jon Hall0e240372018-05-02 11:21:57 -0700548 i = self.handle.expect( "onos>", timeout )
Jon Hall63604932015-02-26 17:09:50 -0800549 response = self.handle.before
Jon Hall63604932015-02-26 17:09:50 -0800550 # TODO: do something with i
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000551 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
Jon Hallc6793552016-01-19 14:18:37 -0800552 + self.name + "." )
Jon Hallc6358dd2015-04-10 12:44:28 -0700553 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700554 main.log.debug( self.name + ": Raw output" )
555 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700556
557 # Remove ANSI color control strings from output
kelvin-onlabd3b64892015-01-20 13:26:24 -0800558 ansiEscape = re.compile( r'\x1b[^m]*m' )
Jon Hall63604932015-02-26 17:09:50 -0800559 response = ansiEscape.sub( '', response )
Jon Hallc6358dd2015-04-10 12:44:28 -0700560 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700561 main.log.debug( self.name + ": ansiEscape output" )
562 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700563
kelvin-onlabfb521662015-02-27 09:52:40 -0800564 # Remove extra return chars that get added
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000565 response = re.sub( r"\s\r", "", response )
Jon Hallc6358dd2015-04-10 12:44:28 -0700566 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700567 main.log.debug( self.name + ": Removed extra returns " +
568 "from output" )
569 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700570
571 # Strip excess whitespace
Jon Hall63604932015-02-26 17:09:50 -0800572 response = response.strip()
Jon Hallc6358dd2015-04-10 12:44:28 -0700573 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700574 main.log.debug( self.name + ": parsed and stripped output" )
575 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700576
Jon Hall63604932015-02-26 17:09:50 -0800577 # parse for just the output, remove the cmd from response
Jon Hallc6358dd2015-04-10 12:44:28 -0700578 output = response.split( cmdStr.strip(), 1 )
Jon Hall0e240372018-05-02 11:21:57 -0700579 if output:
580 if debug:
581 main.log.debug( self.name + ": split output" )
582 for r in output:
583 main.log.debug( self.name + ": " + repr( r ) )
584 output = output[ 1 ].strip()
GlennRC85870432015-11-23 11:45:51 -0800585 if showResponse:
GlennRCed771242016-01-13 17:02:47 -0800586 main.log.info( "Response from ONOS: {}".format( output ) )
Jon Hall0e240372018-05-02 11:21:57 -0700587 self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit )
GlennRC85870432015-11-23 11:45:51 -0800588 return output
GlennRCed771242016-01-13 17:02:47 -0800589 except pexpect.TIMEOUT:
Jon Hall0e240372018-05-02 11:21:57 -0700590 main.log.error( self.name + ": ONOS timeout" )
GlennRCed771242016-01-13 17:02:47 -0800591 if debug:
592 main.log.debug( self.handle.before )
You Wang141b43b2018-06-26 16:50:18 -0700593 self.handle.send( "\x03" )
594 self.handle.expect( "onos>" )
GlennRCed771242016-01-13 17:02:47 -0800595 return None
Jon Hallc6358dd2015-04-10 12:44:28 -0700596 except IndexError:
597 main.log.exception( self.name + ": Object not as expected" )
Jon Halla495f562016-05-16 18:03:26 -0700598 main.log.debug( "response: {}".format( repr( response ) ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700599 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800600 except TypeError:
601 main.log.exception( self.name + ": Object not as expected" )
602 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400603 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800604 main.log.error( self.name + ": EOF exception found" )
605 main.log.error( self.name + ": " + self.handle.before )
YPZhangebf9eb52016-05-12 15:20:24 -0700606 if noExit:
YPZhangebf9eb52016-05-12 15:20:24 -0700607 return None
608 else:
Devin Lim44075962017-08-11 10:56:37 -0700609 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800610 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800611 main.log.exception( self.name + ": Uncaught exception!" )
YPZhangebf9eb52016-05-12 15:20:24 -0700612 if noExit:
YPZhangebf9eb52016-05-12 15:20:24 -0700613 return None
614 else:
Devin Lim44075962017-08-11 10:56:37 -0700615 main.cleanAndExit()
andrewonlaba18f6bf2014-10-13 19:31:54 -0400616
kelvin8ec71442015-01-15 16:57:00 -0800617 # IMPORTANT NOTE:
618 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800619 # the cli command changing 'a:b' with 'aB'.
620 # Ex ) onos:topology > onosTopology
621 # onos:links > onosLinks
622 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800623
kelvin-onlabd3b64892015-01-20 13:26:24 -0800624 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800625 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400626 Adds a new cluster node by ID and address information.
627 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800628 * nodeId
629 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400630 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800631 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800632 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400633 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800634 cmdStr = "add-node " + str( nodeId ) + " " +\
635 str( ONOSIp ) + " " + str( tcpPort )
636 handle = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -0700637 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800638 assert "Command not found:" not in handle, handle
kelvin-onlab898a6c62015-01-16 14:13:53 -0800639 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -0700640 main.log.error( self.name + ": Error in adding node" )
kelvin8ec71442015-01-15 16:57:00 -0800641 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800642 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400643 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800644 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400645 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -0800646 except AssertionError:
647 main.log.exception( "" )
648 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800649 except TypeError:
650 main.log.exception( self.name + ": Object not as expected" )
651 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400652 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800653 main.log.error( self.name + ": EOF exception found" )
654 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700655 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800656 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800657 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700658 main.cleanAndExit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400659
kelvin-onlabd3b64892015-01-20 13:26:24 -0800660 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800661 """
andrewonlab86dc3082014-10-13 18:18:38 -0400662 Removes a cluster by ID
663 Issues command: 'remove-node [<node-id>]'
664 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800665 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800666 """
andrewonlab86dc3082014-10-13 18:18:38 -0400667 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400668
kelvin-onlabd3b64892015-01-20 13:26:24 -0800669 cmdStr = "remove-node " + str( nodeId )
Jon Hall08f61bc2015-04-13 16:00:30 -0700670 handle = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -0700671 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800672 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700673 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -0700674 main.log.error( self.name + ": Error in removing node" )
Jon Hallc6358dd2015-04-10 12:44:28 -0700675 main.log.error( handle )
676 return main.FALSE
677 else:
678 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -0800679 except AssertionError:
680 main.log.exception( "" )
681 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800682 except TypeError:
683 main.log.exception( self.name + ": Object not as expected" )
684 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400685 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800686 main.log.error( self.name + ": EOF exception found" )
687 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700688 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800689 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800690 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700691 main.cleanAndExit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400692
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700693 def nodes( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800694 """
andrewonlab7c211572014-10-15 16:45:20 -0400695 List the nodes currently visible
696 Issues command: 'nodes'
Jon Hall61282e32015-03-19 11:34:11 -0700697 Optional argument:
698 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800699 """
andrewonlab7c211572014-10-15 16:45:20 -0400700 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700701 cmdStr = "nodes"
Jon Hall61282e32015-03-19 11:34:11 -0700702 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700703 cmdStr += " -j"
704 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -0700705 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800706 assert "Command not found:" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -0700707 return output
Jon Hallc6793552016-01-19 14:18:37 -0800708 except AssertionError:
709 main.log.exception( "" )
710 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800711 except TypeError:
712 main.log.exception( self.name + ": Object not as expected" )
713 return None
andrewonlab7c211572014-10-15 16:45:20 -0400714 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800715 main.log.error( self.name + ": EOF exception found" )
716 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700717 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800718 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800719 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700720 main.cleanAndExit()
andrewonlab7c211572014-10-15 16:45:20 -0400721
kelvin8ec71442015-01-15 16:57:00 -0800722 def topology( self ):
723 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700724 Definition:
Jon Hall390696c2015-05-05 17:13:41 -0700725 Returns the output of topology command.
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700726 Return:
727 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -0800728 """
andrewonlab95ce8322014-10-13 14:12:04 -0400729 try:
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700730 cmdStr = "topology -j"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800731 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -0800732 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800733 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700734 main.log.info( cmdStr + " returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400735 return handle
Jon Hallc6793552016-01-19 14:18:37 -0800736 except AssertionError:
737 main.log.exception( "" )
Jon Halld4d4b372015-01-28 16:02:41 -0800738 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800739 except TypeError:
740 main.log.exception( self.name + ": Object not as expected" )
741 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400742 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800743 main.log.error( self.name + ": EOF exception found" )
744 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700745 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800746 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800747 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700748 main.cleanAndExit()
Jon Hallffb386d2014-11-21 13:43:38 -0800749
jenkins7ead5a82015-03-13 10:28:21 -0700750 def deviceRemove( self, deviceId ):
751 """
752 Removes particular device from storage
753
754 TODO: refactor this function
755 """
756 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700757 cmdStr = "device-remove " + str( deviceId )
758 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -0800759 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800760 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700761 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -0700762 main.log.error( self.name + ": Error in removing device" )
Jon Hallc6358dd2015-04-10 12:44:28 -0700763 main.log.error( handle )
764 return main.FALSE
765 else:
766 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -0800767 except AssertionError:
768 main.log.exception( "" )
769 return None
jenkins7ead5a82015-03-13 10:28:21 -0700770 except TypeError:
771 main.log.exception( self.name + ": Object not as expected" )
772 return None
773 except pexpect.EOF:
774 main.log.error( self.name + ": EOF exception found" )
775 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700776 main.cleanAndExit()
jenkins7ead5a82015-03-13 10:28:21 -0700777 except Exception:
778 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700779 main.cleanAndExit()
jenkins7ead5a82015-03-13 10:28:21 -0700780
kelvin-onlabd3b64892015-01-20 13:26:24 -0800781 def devices( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800782 """
Jon Hall7b02d952014-10-17 20:14:54 -0400783 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400784 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800785 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800786 """
andrewonlab86dc3082014-10-13 18:18:38 -0400787 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700788 cmdStr = "devices"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800789 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700790 cmdStr += " -j"
791 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -0800792 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800793 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700794 return handle
Jon Hallc6793552016-01-19 14:18:37 -0800795 except AssertionError:
796 main.log.exception( "" )
797 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800798 except TypeError:
799 main.log.exception( self.name + ": Object not as expected" )
800 return None
andrewonlab7c211572014-10-15 16:45:20 -0400801 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800802 main.log.error( self.name + ": EOF exception found" )
803 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700804 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800805 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800806 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700807 main.cleanAndExit()
andrewonlab7c211572014-10-15 16:45:20 -0400808
kelvin-onlabd3b64892015-01-20 13:26:24 -0800809 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800810 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800811 This balances the devices across all controllers
812 by issuing command: 'onos> onos:balance-masters'
813 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800814 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800815 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800816 cmdStr = "onos:balance-masters"
Jon Hallc6358dd2015-04-10 12:44:28 -0700817 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -0800818 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800819 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700820 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -0700821 main.log.error( self.name + ": Error in balancing masters" )
Jon Hallc6358dd2015-04-10 12:44:28 -0700822 main.log.error( handle )
823 return main.FALSE
824 else:
825 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -0800826 except AssertionError:
827 main.log.exception( "" )
828 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800829 except TypeError:
830 main.log.exception( self.name + ": Object not as expected" )
831 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800832 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800833 main.log.error( self.name + ": EOF exception found" )
834 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700835 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800836 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800837 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700838 main.cleanAndExit()
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800839
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000840 def checkMasters( self, jsonFormat=True ):
acsmars24950022015-07-30 18:00:43 -0700841 """
842 Returns the output of the masters command.
843 Optional argument:
844 * jsonFormat - boolean indicating if you want output in json
845 """
846 try:
847 cmdStr = "onos:masters"
848 if jsonFormat:
849 cmdStr += " -j"
850 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -0700851 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800852 assert "Command not found:" not in output, output
acsmars24950022015-07-30 18:00:43 -0700853 return output
Jon Hallc6793552016-01-19 14:18:37 -0800854 except AssertionError:
855 main.log.exception( "" )
856 return None
acsmars24950022015-07-30 18:00:43 -0700857 except TypeError:
858 main.log.exception( self.name + ": Object not as expected" )
859 return None
860 except pexpect.EOF:
861 main.log.error( self.name + ": EOF exception found" )
862 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700863 main.cleanAndExit()
acsmars24950022015-07-30 18:00:43 -0700864 except Exception:
865 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700866 main.cleanAndExit()
acsmars24950022015-07-30 18:00:43 -0700867
Jon Hallc6793552016-01-19 14:18:37 -0800868 def checkBalanceMasters( self, jsonFormat=True ):
acsmars24950022015-07-30 18:00:43 -0700869 """
870 Uses the master command to check that the devices' leadership
871 is evenly divided
872
873 Dependencies: checkMasters() and summary()
874
Jon Hall6509dbf2016-06-21 17:01:17 -0700875 Returns main.TRUE if the devices are balanced
876 Returns main.FALSE if the devices are unbalanced
acsmars24950022015-07-30 18:00:43 -0700877 Exits on Exception
878 Returns None on TypeError
879 """
880 try:
Jon Hallc6793552016-01-19 14:18:37 -0800881 summaryOutput = self.summary()
882 totalDevices = json.loads( summaryOutput )[ "devices" ]
883 except ( TypeError, ValueError ):
884 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) )
885 return None
886 try:
acsmars24950022015-07-30 18:00:43 -0700887 totalOwnedDevices = 0
Jon Hallc6793552016-01-19 14:18:37 -0800888 mastersOutput = self.checkMasters()
889 masters = json.loads( mastersOutput )
acsmars24950022015-07-30 18:00:43 -0700890 first = masters[ 0 ][ "size" ]
891 for master in masters:
892 totalOwnedDevices += master[ "size" ]
893 if master[ "size" ] > first + 1 or master[ "size" ] < first - 1:
894 main.log.error( "Mastership not balanced" )
895 main.log.info( "\n" + self.checkMasters( False ) )
896 return main.FALSE
Jon Halle0f0b342017-04-18 11:43:47 -0700897 main.log.info( "Mastership balanced between " +
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700898 str( len( masters ) ) + " masters" )
acsmars24950022015-07-30 18:00:43 -0700899 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -0800900 except ( TypeError, ValueError ):
901 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) )
acsmars24950022015-07-30 18:00:43 -0700902 return None
903 except pexpect.EOF:
904 main.log.error( self.name + ": EOF exception found" )
905 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700906 main.cleanAndExit()
acsmars24950022015-07-30 18:00:43 -0700907 except Exception:
908 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700909 main.cleanAndExit()
acsmars24950022015-07-30 18:00:43 -0700910
YPZhangfebf7302016-05-24 16:45:56 -0700911 def links( self, jsonFormat=True, timeout=30 ):
kelvin8ec71442015-01-15 16:57:00 -0800912 """
Jon Halle8217482014-10-17 13:49:14 -0400913 Lists all core links
914 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800915 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800916 """
Jon Halle8217482014-10-17 13:49:14 -0400917 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700918 cmdStr = "links"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800919 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700920 cmdStr += " -j"
YPZhangfebf7302016-05-24 16:45:56 -0700921 handle = self.sendline( cmdStr, timeout=timeout )
You Wangb5a55f72017-03-03 12:51:05 -0800922 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800923 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700924 return handle
Jon Hallc6793552016-01-19 14:18:37 -0800925 except AssertionError:
926 main.log.exception( "" )
927 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800928 except TypeError:
929 main.log.exception( self.name + ": Object not as expected" )
930 return None
Jon Halle8217482014-10-17 13:49:14 -0400931 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800932 main.log.error( self.name + ": EOF exception found" )
933 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700934 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800935 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800936 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700937 main.cleanAndExit()
Jon Halle8217482014-10-17 13:49:14 -0400938
kelvin-onlabd3b64892015-01-20 13:26:24 -0800939 def ports( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800940 """
Jon Halle8217482014-10-17 13:49:14 -0400941 Lists all ports
942 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800943 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800944 """
Jon Halle8217482014-10-17 13:49:14 -0400945 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700946 cmdStr = "ports"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800947 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700948 cmdStr += " -j"
949 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -0800950 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800951 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700952 return handle
Jon Hallc6793552016-01-19 14:18:37 -0800953 except AssertionError:
954 main.log.exception( "" )
955 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800956 except TypeError:
957 main.log.exception( self.name + ": Object not as expected" )
958 return None
Jon Halle8217482014-10-17 13:49:14 -0400959 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800960 main.log.error( self.name + ": EOF exception found" )
961 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700962 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800963 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800964 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700965 main.cleanAndExit()
Jon Halle8217482014-10-17 13:49:14 -0400966
kelvin-onlabd3b64892015-01-20 13:26:24 -0800967 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800968 """
Jon Hall983a1702014-10-28 18:44:22 -0400969 Lists all devices and the controllers with roles assigned to them
970 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800971 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800972 """
andrewonlab7c211572014-10-15 16:45:20 -0400973 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700974 cmdStr = "roles"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800975 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700976 cmdStr += " -j"
977 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -0800978 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800979 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700980 return handle
Jon Hallc6793552016-01-19 14:18:37 -0800981 except AssertionError:
982 main.log.exception( "" )
983 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800984 except TypeError:
985 main.log.exception( self.name + ": Object not as expected" )
986 return None
Jon Hall983a1702014-10-28 18:44:22 -0400987 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800988 main.log.error( self.name + ": EOF exception found" )
989 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700990 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800991 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800992 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700993 main.cleanAndExit()
Jon Hall983a1702014-10-28 18:44:22 -0400994
kelvin-onlabd3b64892015-01-20 13:26:24 -0800995 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -0800996 """
Jon Halle3f39ff2015-01-13 11:50:53 -0800997 Given the a string containing the json representation of the "roles"
998 cli command and a partial or whole device id, returns a json object
999 containing the roles output for the first device whose id contains
1000 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -04001001
1002 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -08001003 A dict of the role assignments for the given device or
1004 None if no match
kelvin8ec71442015-01-15 16:57:00 -08001005 """
Jon Hall983a1702014-10-28 18:44:22 -04001006 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001007 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -04001008 return None
1009 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001010 rawRoles = self.roles()
1011 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -08001012 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001013 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -08001014 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001015 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -04001016 return device
1017 return None
Jon Hallc6793552016-01-19 14:18:37 -08001018 except ( TypeError, ValueError ):
1019 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) )
Jon Halld4d4b372015-01-28 16:02:41 -08001020 return None
andrewonlab86dc3082014-10-13 18:18:38 -04001021 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001022 main.log.error( self.name + ": EOF exception found" )
1023 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001024 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001025 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001026 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001027 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08001028
kelvin-onlabd3b64892015-01-20 13:26:24 -08001029 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -08001030 """
Jon Hall94fd0472014-12-08 11:52:42 -08001031 Iterates through each device and checks if there is a master assigned
1032 Returns: main.TRUE if each device has a master
1033 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -08001034 """
Jon Hall94fd0472014-12-08 11:52:42 -08001035 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001036 rawRoles = self.roles()
1037 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -08001038 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001039 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -08001040 # print device
1041 if device[ 'master' ] == "none":
1042 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -08001043 return main.FALSE
1044 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08001045 except ( TypeError, ValueError ):
1046 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) )
Jon Halld4d4b372015-01-28 16:02:41 -08001047 return None
Jon Hall94fd0472014-12-08 11:52:42 -08001048 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001049 main.log.error( self.name + ": EOF exception found" )
1050 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001051 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001052 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001053 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001054 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08001055
kelvin-onlabd3b64892015-01-20 13:26:24 -08001056 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -08001057 """
andrewonlab3e15ead2014-10-15 14:21:34 -04001058 Returns string of paths, and the cost.
1059 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -08001060 """
andrewonlab3e15ead2014-10-15 14:21:34 -04001061 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001062 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
1063 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08001064 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001065 assert "Command not found:" not in handle, handle
Jon Halle3f39ff2015-01-13 11:50:53 -08001066 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001067 main.log.error( self.name + ": Error in getting paths" )
kelvin8ec71442015-01-15 16:57:00 -08001068 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -04001069 else:
kelvin8ec71442015-01-15 16:57:00 -08001070 path = handle.split( ";" )[ 0 ]
1071 cost = handle.split( ";" )[ 1 ]
1072 return ( path, cost )
Jon Hallc6793552016-01-19 14:18:37 -08001073 except AssertionError:
1074 main.log.exception( "" )
1075 return ( handle, "Error" )
Jon Halld4d4b372015-01-28 16:02:41 -08001076 except TypeError:
1077 main.log.exception( self.name + ": Object not as expected" )
1078 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -04001079 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001080 main.log.error( self.name + ": EOF exception found" )
1081 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001082 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001083 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001084 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001085 main.cleanAndExit()
Jon Hallffb386d2014-11-21 13:43:38 -08001086
kelvin-onlabd3b64892015-01-20 13:26:24 -08001087 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001088 """
Jon Hallffb386d2014-11-21 13:43:38 -08001089 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -04001090 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001091 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001092 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001093 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07001094 cmdStr = "hosts"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001095 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07001096 cmdStr += " -j"
1097 handle = self.sendline( cmdStr )
Jeremyd9e4eb12016-04-13 12:09:06 -07001098 if handle:
1099 assert "Command not found:" not in handle, handle
Jon Hallbaf53162015-12-17 17:04:34 -08001100 # TODO: Maybe make this less hardcoded
1101 # ConsistentMap Exceptions
1102 assert "org.onosproject.store.service" not in handle
1103 # Node not leader
1104 assert "java.lang.IllegalStateException" not in handle
Jon Hallc6358dd2015-04-10 12:44:28 -07001105 return handle
Jon Hallc6793552016-01-19 14:18:37 -08001106 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07001107 main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " +
Jeremy Songster6949cea2016-04-19 18:13:18 -07001108 "command: " + str( handle ) )
Jon Hallc6793552016-01-19 14:18:37 -08001109 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001110 except TypeError:
1111 main.log.exception( self.name + ": Object not as expected" )
1112 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001113 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001114 main.log.error( self.name + ": EOF exception found" )
1115 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001116 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001117 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001118 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001119 main.cleanAndExit()
Jon Hall42db6dc2014-10-24 19:03:48 -04001120
kelvin-onlabd3b64892015-01-20 13:26:24 -08001121 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -08001122 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001123 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -08001124
Jon Hallefbd9792015-03-05 16:11:36 -08001125 Note: mac must be a colon separated mac address, but could be a
Jon Halle3f39ff2015-01-13 11:50:53 -08001126 partial mac address
1127
Jon Hall42db6dc2014-10-24 19:03:48 -04001128 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001129 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001130 try:
kelvin8ec71442015-01-15 16:57:00 -08001131 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -04001132 return None
1133 else:
1134 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -08001135 rawHosts = self.hosts()
1136 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -08001137 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001138 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -08001139 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -08001140 if not host:
1141 pass
1142 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -04001143 return host
1144 return None
Jon Hallc6793552016-01-19 14:18:37 -08001145 except ( TypeError, ValueError ):
1146 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) )
Jon Halld4d4b372015-01-28 16:02:41 -08001147 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001148 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001149 main.log.error( self.name + ": EOF exception found" )
1150 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001151 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001152 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001153 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001154 main.cleanAndExit()
Jon Hall42db6dc2014-10-24 19:03:48 -04001155
kelvin-onlabd3b64892015-01-20 13:26:24 -08001156 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -08001157 """
1158 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -04001159 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -08001160
andrewonlab3f0a4af2014-10-17 12:25:14 -04001161 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001162 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -04001163 IMPORTANT:
1164 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -08001165 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -04001166 Furthermore, it assumes that value of VLAN is '-1'
1167 Description:
kelvin8ec71442015-01-15 16:57:00 -08001168 Converts mininet hosts ( h1, h2, h3... ) into
1169 ONOS format ( 00:00:00:00:00:01/-1 , ... )
1170 """
andrewonlab3f0a4af2014-10-17 12:25:14 -04001171 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001172 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -04001173
kelvin-onlabd3b64892015-01-20 13:26:24 -08001174 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -08001175 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001176 hostHex = hex( int( host ) ).zfill( 12 )
1177 hostHex = str( hostHex ).replace( 'x', '0' )
1178 i = iter( str( hostHex ) )
1179 hostHex = ":".join( a + b for a, b in zip( i, i ) )
1180 hostHex = hostHex + "/-1"
1181 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001182
kelvin-onlabd3b64892015-01-20 13:26:24 -08001183 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -04001184
Jon Halld4d4b372015-01-28 16:02:41 -08001185 except TypeError:
1186 main.log.exception( self.name + ": Object not as expected" )
1187 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -04001188 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001189 main.log.error( self.name + ": EOF exception found" )
1190 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001191 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001192 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001193 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001194 main.cleanAndExit()
andrewonlab3e15ead2014-10-15 14:21:34 -04001195
You Wangbc898b82018-05-03 16:22:34 -07001196 def verifyHostLocation( self, hostIp, location ):
1197 """
1198 Description:
1199 Verify the host given is discovered in all locations expected
1200 Required:
1201 hostIp: IP address of the host
1202 location: expected location(s) of the given host. ex. "of:0000000000000005/8"
1203 Could be a string or list
1204 Returns:
1205 main.TRUE if host is discovered on all locations provided
1206 main.FALSE otherwise
1207 """
1208 import json
1209 locations = [ location ] if isinstance( location, str ) else location
1210 assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) )
1211 try:
1212 hosts = self.hosts()
1213 hosts = json.loads( hosts )
1214 targetHost = None
1215 for host in hosts:
1216 if hostIp in host[ "ipAddresses" ]:
1217 targetHost = host
You Wangfd80ab42018-05-10 17:21:53 -07001218 assert targetHost, "Not able to find host with IP {}".format( hostIp )
You Wangbc898b82018-05-03 16:22:34 -07001219 result = main.TRUE
1220 locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ]
1221 for loc in locations:
1222 discovered = False
1223 for locDiscovered in locationsDiscovered:
You Wang547893e2018-05-08 13:34:59 -07001224 locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0]
1225 if loc == locToMatch:
You Wangbc898b82018-05-03 16:22:34 -07001226 main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) )
You Wang547893e2018-05-08 13:34:59 -07001227 discovered = True
You Wangbc898b82018-05-03 16:22:34 -07001228 break
1229 if discovered:
1230 locationsDiscovered.remove( locDiscovered )
1231 else:
1232 main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) )
1233 result = main.FALSE
1234 if locationsDiscovered:
1235 main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) )
1236 result = main.FALSE
1237 return result
1238 except KeyError:
1239 main.log.exception( self.name + ": host data not as expected: " + hosts )
1240 return None
1241 except pexpect.EOF:
1242 main.log.error( self.name + ": EOF exception found" )
1243 main.log.error( self.name + ": " + self.handle.before )
1244 main.cleanAndExit()
1245 except Exception:
1246 main.log.exception( self.name + ": Uncaught exception" )
1247 return None
1248
You Wang53dba1e2018-02-02 17:45:44 -08001249 def verifyHostIp( self, hostList=[], prefix="" ):
1250 """
1251 Description:
1252 Verify that all hosts have IP address assigned to them
1253 Optional:
1254 hostList: If specified, verifications only happen to the hosts
1255 in hostList
1256 prefix: at least one of the ip address assigned to the host
1257 needs to have the specified prefix
1258 Returns:
1259 main.TRUE if all hosts have specific IP address assigned;
1260 main.FALSE otherwise
1261 """
1262 import json
1263 try:
1264 hosts = self.hosts()
1265 hosts = json.loads( hosts )
1266 if not hostList:
1267 hostList = [ host[ "id" ] for host in hosts ]
1268 for host in hosts:
1269 hostId = host[ "id" ]
1270 if hostId not in hostList:
1271 continue
1272 ipList = host[ "ipAddresses" ]
1273 main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) )
1274 if not ipList:
1275 main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) )
1276 else:
1277 if not any( ip.startswith( str( prefix ) ) for ip in ipList ):
1278 main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) )
1279 else:
1280 main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) )
1281 hostList.remove( hostId )
1282 if hostList:
1283 main.log.warn( self.name + ": failed to verify IP on following hosts: " + str( hostList) )
1284 return main.FALSE
1285 else:
1286 return main.TRUE
1287 except KeyError:
1288 main.log.exception( self.name + ": host data not as expected: " + hosts )
1289 return None
1290 except pexpect.EOF:
1291 main.log.error( self.name + ": EOF exception found" )
1292 main.log.error( self.name + ": " + self.handle.before )
1293 main.cleanAndExit()
1294 except Exception:
1295 main.log.exception( self.name + ": Uncaught exception" )
1296 return None
1297
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07001298 def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ):
kelvin8ec71442015-01-15 16:57:00 -08001299 """
andrewonlabe6745342014-10-17 14:29:13 -04001300 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001301 * hostIdOne: ONOS host id for host1
1302 * hostIdTwo: ONOS host id for host2
Jeremy Songster832f9e92016-05-05 14:30:49 -07001303 Optional:
1304 * vlanId: specify a VLAN id for the intent
Jeremy Songsterff553672016-05-12 17:06:23 -07001305 * setVlan: specify a VLAN id treatment
Jeremy Songsterc032f162016-08-04 17:14:49 -07001306 * encap: specify an encapsulation type
andrewonlabe6745342014-10-17 14:29:13 -04001307 Description:
Jon Hallefbd9792015-03-05 16:11:36 -08001308 Adds a host-to-host intent ( bidirectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -05001309 specifying the two hosts.
kelvin-onlabfb521662015-02-27 09:52:40 -08001310 Returns:
1311 A string of the intent id or None on Error
kelvin8ec71442015-01-15 16:57:00 -08001312 """
andrewonlabe6745342014-10-17 14:29:13 -04001313 try:
Jeremy Songster832f9e92016-05-05 14:30:49 -07001314 cmdStr = "add-host-intent "
1315 if vlanId:
1316 cmdStr += "-v " + str( vlanId ) + " "
Jeremy Songsterff553672016-05-12 17:06:23 -07001317 if setVlan:
1318 cmdStr += "--setVlan " + str( vlanId ) + " "
Jeremy Songsterc032f162016-08-04 17:14:49 -07001319 if encap:
1320 cmdStr += "--encapsulation " + str( encap ) + " "
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07001321 if bandwidth:
1322 cmdStr += "-b " + str( bandwidth ) + " "
Jeremy Songster832f9e92016-05-05 14:30:49 -07001323 cmdStr += str( hostIdOne ) + " " + str( hostIdTwo )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001324 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08001325 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001326 assert "Command not found:" not in handle, handle
Hari Krishnaac4e1782015-01-26 12:09:12 -08001327 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001328 main.log.error( self.name + ": Error in adding Host intent" )
Jon Hall61282e32015-03-19 11:34:11 -07001329 main.log.debug( "Response from ONOS was: " + repr( handle ) )
kelvin-onlabfb521662015-02-27 09:52:40 -08001330 return None
Hari Krishnaac4e1782015-01-26 12:09:12 -08001331 else:
1332 main.log.info( "Host intent installed between " +
kelvin-onlabfb521662015-02-27 09:52:40 -08001333 str( hostIdOne ) + " and " + str( hostIdTwo ) )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001334 match = re.search( 'id=0x([\da-f]+),', handle )
kelvin-onlabfb521662015-02-27 09:52:40 -08001335 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001336 return match.group()[ 3:-1 ]
kelvin-onlabfb521662015-02-27 09:52:40 -08001337 else:
1338 main.log.error( "Error, intent ID not found" )
Jon Hall61282e32015-03-19 11:34:11 -07001339 main.log.debug( "Response from ONOS was: " +
1340 repr( handle ) )
kelvin-onlabfb521662015-02-27 09:52:40 -08001341 return None
Jon Hallc6793552016-01-19 14:18:37 -08001342 except AssertionError:
1343 main.log.exception( "" )
1344 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001345 except TypeError:
1346 main.log.exception( self.name + ": Object not as expected" )
1347 return None
andrewonlabe6745342014-10-17 14:29:13 -04001348 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001349 main.log.error( self.name + ": EOF exception found" )
1350 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001351 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001352 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001353 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001354 main.cleanAndExit()
andrewonlabe6745342014-10-17 14:29:13 -04001355
kelvin-onlabd3b64892015-01-20 13:26:24 -08001356 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -08001357 """
andrewonlab7b31d232014-10-24 13:31:47 -04001358 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001359 * ingressDevice: device id of ingress device
1360 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -04001361 Optional:
1362 TODO: Still needs to be implemented via dev side
kelvin-onlabfb521662015-02-27 09:52:40 -08001363 Description:
1364 Adds an optical intent by specifying an ingress and egress device
1365 Returns:
1366 A string of the intent id or None on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001367 """
andrewonlab7b31d232014-10-24 13:31:47 -04001368 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001369 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
1370 " " + str( egressDevice )
1371 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08001372 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001373 assert "Command not found:" not in handle, handle
kelvin-onlab898a6c62015-01-16 14:13:53 -08001374 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -08001375 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001376 main.log.error( self.name + ": Error in adding Optical intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001377 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001378 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001379 main.log.info( "Optical intent installed between " +
1380 str( ingressDevice ) + " and " +
1381 str( egressDevice ) )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001382 match = re.search( 'id=0x([\da-f]+),', handle )
kelvin-onlabfb521662015-02-27 09:52:40 -08001383 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001384 return match.group()[ 3:-1 ]
kelvin-onlabfb521662015-02-27 09:52:40 -08001385 else:
1386 main.log.error( "Error, intent ID not found" )
1387 return None
Jon Hallc6793552016-01-19 14:18:37 -08001388 except AssertionError:
1389 main.log.exception( "" )
1390 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001391 except TypeError:
1392 main.log.exception( self.name + ": Object not as expected" )
1393 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001394 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001395 main.log.error( self.name + ": EOF exception found" )
1396 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001397 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001398 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001399 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001400 main.cleanAndExit()
andrewonlab7b31d232014-10-24 13:31:47 -04001401
kelvin-onlabd3b64892015-01-20 13:26:24 -08001402 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001403 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001404 ingressDevice,
1405 egressDevice,
1406 portIngress="",
1407 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001408 ethType="",
1409 ethSrc="",
1410 ethDst="",
1411 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001412 lambdaAlloc=False,
alisonda157272016-12-22 01:13:21 -08001413 protected=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001414 ipProto="",
1415 ipSrc="",
1416 ipDst="",
1417 tcpSrc="",
Jeremy Songster832f9e92016-05-05 14:30:49 -07001418 tcpDst="",
Jeremy Songsterff553672016-05-12 17:06:23 -07001419 vlanId="",
Jeremy Songsterc032f162016-08-04 17:14:49 -07001420 setVlan="",
1421 encap="" ):
kelvin8ec71442015-01-15 16:57:00 -08001422 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001423 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001424 * ingressDevice: device id of ingress device
1425 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001426 Optional:
1427 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001428 * ethSrc: specify ethSrc ( i.e. src mac addr )
1429 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001430 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001431 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001432 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001433 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001434 * ipSrc: specify ip source address
1435 * ipDst: specify ip destination address
1436 * tcpSrc: specify tcp source port
1437 * tcpDst: specify tcp destination port
Jeremy Songster832f9e92016-05-05 14:30:49 -07001438 * vlanId: specify vlan ID
Jeremy Songsterff553672016-05-12 17:06:23 -07001439 * setVlan: specify a VLAN id treatment
Jeremy Songsterc032f162016-08-04 17:14:49 -07001440 * encap: specify an Encapsulation type to use
andrewonlab4dbb4d82014-10-17 18:22:31 -04001441 Description:
kelvin8ec71442015-01-15 16:57:00 -08001442 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001443 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001444 Returns:
1445 A string of the intent id or None on error
andrewonlab289e4b72014-10-21 21:24:18 -04001446
Jon Halle3f39ff2015-01-13 11:50:53 -08001447 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001448 options developers provide for point-to-point
1449 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001450 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001451 try:
Jeremy Songsterff553672016-05-12 17:06:23 -07001452 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001453
Jeremy Songsterff553672016-05-12 17:06:23 -07001454 if ethType:
1455 cmd += " --ethType " + str( ethType )
1456 if ethSrc:
1457 cmd += " --ethSrc " + str( ethSrc )
1458 if ethDst:
1459 cmd += " --ethDst " + str( ethDst )
1460 if bandwidth:
1461 cmd += " --bandwidth " + str( bandwidth )
1462 if lambdaAlloc:
1463 cmd += " --lambda "
1464 if ipProto:
1465 cmd += " --ipProto " + str( ipProto )
1466 if ipSrc:
1467 cmd += " --ipSrc " + str( ipSrc )
1468 if ipDst:
1469 cmd += " --ipDst " + str( ipDst )
1470 if tcpSrc:
1471 cmd += " --tcpSrc " + str( tcpSrc )
1472 if tcpDst:
1473 cmd += " --tcpDst " + str( tcpDst )
1474 if vlanId:
1475 cmd += " -v " + str( vlanId )
1476 if setVlan:
1477 cmd += " --setVlan " + str( setVlan )
Jeremy Songsterc032f162016-08-04 17:14:49 -07001478 if encap:
1479 cmd += " --encapsulation " + str( encap )
alisonda157272016-12-22 01:13:21 -08001480 if protected:
1481 cmd += " --protect "
andrewonlab289e4b72014-10-21 21:24:18 -04001482
kelvin8ec71442015-01-15 16:57:00 -08001483 # Check whether the user appended the port
1484 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001485 if "/" in ingressDevice:
1486 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001487 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001488 if not portIngress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001489 main.log.error( "You must specify the ingress port" )
kelvin8ec71442015-01-15 16:57:00 -08001490 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001491 # Would it make sense to throw an exception and exit
1492 # the test?
1493 return None
andrewonlab36af3822014-11-18 17:48:18 -05001494
kelvin8ec71442015-01-15 16:57:00 -08001495 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001496 str( ingressDevice ) + "/" +\
1497 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001498
kelvin-onlabd3b64892015-01-20 13:26:24 -08001499 if "/" in egressDevice:
1500 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001501 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001502 if not portEgress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001503 main.log.error( "You must specify the egress port" )
1504 return None
Jon Halle3f39ff2015-01-13 11:50:53 -08001505
kelvin8ec71442015-01-15 16:57:00 -08001506 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001507 str( egressDevice ) + "/" +\
1508 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001509
kelvin-onlab898a6c62015-01-16 14:13:53 -08001510 handle = self.sendline( cmd )
You Wangb5a55f72017-03-03 12:51:05 -08001511 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001512 assert "Command not found:" not in handle, handle
kelvin-onlabfb521662015-02-27 09:52:40 -08001513 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001514 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001515 main.log.error( self.name + ": Error in adding point-to-point intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001516 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001517 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001518 # TODO: print out all the options in this message?
1519 main.log.info( "Point-to-point intent installed between " +
1520 str( ingressDevice ) + " and " +
1521 str( egressDevice ) )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001522 match = re.search( 'id=0x([\da-f]+),', handle )
kelvin-onlabfb521662015-02-27 09:52:40 -08001523 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001524 return match.group()[ 3:-1 ]
kelvin-onlabfb521662015-02-27 09:52:40 -08001525 else:
1526 main.log.error( "Error, intent ID not found" )
1527 return None
Jon Hallc6793552016-01-19 14:18:37 -08001528 except AssertionError:
1529 main.log.exception( "" )
1530 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001531 except TypeError:
1532 main.log.exception( self.name + ": Object not as expected" )
1533 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001534 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001535 main.log.error( self.name + ": EOF exception found" )
1536 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001537 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001538 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001539 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001540 main.cleanAndExit()
andrewonlab4dbb4d82014-10-17 18:22:31 -04001541
kelvin-onlabd3b64892015-01-20 13:26:24 -08001542 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001543 self,
shahshreyac2f97072015-03-19 17:04:29 -07001544 ingressDeviceList,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001545 egressDevice,
shahshreyac2f97072015-03-19 17:04:29 -07001546 portIngressList=None,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001547 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001548 ethType="",
1549 ethSrc="",
1550 ethDst="",
1551 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001552 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001553 ipProto="",
1554 ipSrc="",
1555 ipDst="",
1556 tcpSrc="",
1557 tcpDst="",
1558 setEthSrc="",
Jeremy Songster832f9e92016-05-05 14:30:49 -07001559 setEthDst="",
Jeremy Songsterff553672016-05-12 17:06:23 -07001560 vlanId="",
Jeremy Songster9385d412016-06-02 17:57:36 -07001561 setVlan="",
Jeremy Songsterc032f162016-08-04 17:14:49 -07001562 partial=False,
1563 encap="" ):
kelvin8ec71442015-01-15 16:57:00 -08001564 """
shahshreyad0c80432014-12-04 16:56:05 -08001565 Note:
shahshreya70622b12015-03-19 17:19:00 -07001566 This function assumes the format of all ingress devices
Jon Hallbe379602015-03-24 13:39:32 -07001567 is same. That is, all ingress devices include port numbers
1568 with a "/" or all ingress devices could specify device
1569 ids and port numbers seperately.
shahshreyad0c80432014-12-04 16:56:05 -08001570 Required:
Jon Hallbe379602015-03-24 13:39:32 -07001571 * ingressDeviceList: List of device ids of ingress device
shahshreyac2f97072015-03-19 17:04:29 -07001572 ( Atleast 2 ingress devices required in the list )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001573 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001574 Optional:
1575 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001576 * ethSrc: specify ethSrc ( i.e. src mac addr )
1577 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001578 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001579 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001580 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001581 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001582 * ipSrc: specify ip source address
1583 * ipDst: specify ip destination address
1584 * tcpSrc: specify tcp source port
1585 * tcpDst: specify tcp destination port
1586 * setEthSrc: action to Rewrite Source MAC Address
1587 * setEthDst: action to Rewrite Destination MAC Address
Jeremy Songster832f9e92016-05-05 14:30:49 -07001588 * vlanId: specify vlan Id
Jeremy Songsterff553672016-05-12 17:06:23 -07001589 * setVlan: specify VLAN Id treatment
Jeremy Songsterc032f162016-08-04 17:14:49 -07001590 * encap: specify a type of encapsulation
shahshreyad0c80432014-12-04 16:56:05 -08001591 Description:
kelvin8ec71442015-01-15 16:57:00 -08001592 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001593 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001594 Returns:
1595 A string of the intent id or None on error
shahshreyad0c80432014-12-04 16:56:05 -08001596
Jon Halle3f39ff2015-01-13 11:50:53 -08001597 NOTE: This function may change depending on the
Jon Hallefbd9792015-03-05 16:11:36 -08001598 options developers provide for multipoint-to-singlepoint
shahshreyad0c80432014-12-04 16:56:05 -08001599 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001600 """
shahshreyad0c80432014-12-04 16:56:05 -08001601 try:
Jeremy Songsterff553672016-05-12 17:06:23 -07001602 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001603
Jeremy Songsterff553672016-05-12 17:06:23 -07001604 if ethType:
1605 cmd += " --ethType " + str( ethType )
1606 if ethSrc:
1607 cmd += " --ethSrc " + str( ethSrc )
1608 if ethDst:
1609 cmd += " --ethDst " + str( ethDst )
1610 if bandwidth:
1611 cmd += " --bandwidth " + str( bandwidth )
1612 if lambdaAlloc:
1613 cmd += " --lambda "
1614 if ipProto:
1615 cmd += " --ipProto " + str( ipProto )
1616 if ipSrc:
1617 cmd += " --ipSrc " + str( ipSrc )
1618 if ipDst:
1619 cmd += " --ipDst " + str( ipDst )
1620 if tcpSrc:
1621 cmd += " --tcpSrc " + str( tcpSrc )
1622 if tcpDst:
1623 cmd += " --tcpDst " + str( tcpDst )
1624 if setEthSrc:
1625 cmd += " --setEthSrc " + str( setEthSrc )
1626 if setEthDst:
1627 cmd += " --setEthDst " + str( setEthDst )
1628 if vlanId:
1629 cmd += " -v " + str( vlanId )
1630 if setVlan:
1631 cmd += " --setVlan " + str( setVlan )
Jeremy Songster9385d412016-06-02 17:57:36 -07001632 if partial:
1633 cmd += " --partial"
Jeremy Songsterc032f162016-08-04 17:14:49 -07001634 if encap:
1635 cmd += " --encapsulation " + str( encap )
shahshreyad0c80432014-12-04 16:56:05 -08001636
kelvin8ec71442015-01-15 16:57:00 -08001637 # Check whether the user appended the port
1638 # or provided it as an input
shahshreyac2f97072015-03-19 17:04:29 -07001639
1640 if portIngressList is None:
1641 for ingressDevice in ingressDeviceList:
1642 if "/" in ingressDevice:
1643 cmd += " " + str( ingressDevice )
1644 else:
1645 main.log.error( "You must specify " +
Jon Hallbe379602015-03-24 13:39:32 -07001646 "the ingress port" )
shahshreyac2f97072015-03-19 17:04:29 -07001647 # TODO: perhaps more meaningful return
1648 return main.FALSE
shahshreyad0c80432014-12-04 16:56:05 -08001649 else:
Jon Hall71ce4e72015-03-23 14:05:58 -07001650 if len( ingressDeviceList ) == len( portIngressList ):
Jon Hall08f61bc2015-04-13 16:00:30 -07001651 for ingressDevice, portIngress in zip( ingressDeviceList,
1652 portIngressList ):
shahshreya70622b12015-03-19 17:19:00 -07001653 cmd += " " + \
1654 str( ingressDevice ) + "/" +\
1655 str( portIngress ) + " "
kelvin-onlab38143812015-04-01 15:03:01 -07001656 else:
Jon Hall08f61bc2015-04-13 16:00:30 -07001657 main.log.error( "Device list and port list does not " +
1658 "have the same length" )
kelvin-onlab38143812015-04-01 15:03:01 -07001659 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001660 if "/" in egressDevice:
1661 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001662 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001663 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001664 main.log.error( "You must specify " +
1665 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001666 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001667
kelvin8ec71442015-01-15 16:57:00 -08001668 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001669 str( egressDevice ) + "/" +\
1670 str( portEgress )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001671 handle = self.sendline( cmd )
You Wangb5a55f72017-03-03 12:51:05 -08001672 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001673 assert "Command not found:" not in handle, handle
kelvin-onlabfb521662015-02-27 09:52:40 -08001674 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001675 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001676 main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " +
kelvin-onlabfb521662015-02-27 09:52:40 -08001677 "intent" )
1678 return None
shahshreyad0c80432014-12-04 16:56:05 -08001679 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001680 match = re.search( 'id=0x([\da-f]+),', handle )
kelvin-onlabb9408212015-04-01 13:34:04 -07001681 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001682 return match.group()[ 3:-1 ]
kelvin-onlabb9408212015-04-01 13:34:04 -07001683 else:
1684 main.log.error( "Error, intent ID not found" )
1685 return None
Jon Hallc6793552016-01-19 14:18:37 -08001686 except AssertionError:
1687 main.log.exception( "" )
1688 return None
kelvin-onlabb9408212015-04-01 13:34:04 -07001689 except TypeError:
1690 main.log.exception( self.name + ": Object not as expected" )
1691 return None
1692 except pexpect.EOF:
1693 main.log.error( self.name + ": EOF exception found" )
1694 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001695 main.cleanAndExit()
kelvin-onlabb9408212015-04-01 13:34:04 -07001696 except Exception:
1697 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001698 main.cleanAndExit()
kelvin-onlabb9408212015-04-01 13:34:04 -07001699
1700 def addSinglepointToMultipointIntent(
1701 self,
1702 ingressDevice,
1703 egressDeviceList,
1704 portIngress="",
1705 portEgressList=None,
1706 ethType="",
1707 ethSrc="",
1708 ethDst="",
1709 bandwidth="",
1710 lambdaAlloc=False,
1711 ipProto="",
1712 ipSrc="",
1713 ipDst="",
1714 tcpSrc="",
1715 tcpDst="",
1716 setEthSrc="",
Jeremy Songster832f9e92016-05-05 14:30:49 -07001717 setEthDst="",
Jeremy Songsterff553672016-05-12 17:06:23 -07001718 vlanId="",
Jeremy Songster9385d412016-06-02 17:57:36 -07001719 setVlan="",
Jeremy Songsterc032f162016-08-04 17:14:49 -07001720 partial=False,
1721 encap="" ):
kelvin-onlabb9408212015-04-01 13:34:04 -07001722 """
1723 Note:
1724 This function assumes the format of all egress devices
1725 is same. That is, all egress devices include port numbers
1726 with a "/" or all egress devices could specify device
1727 ids and port numbers seperately.
1728 Required:
1729 * EgressDeviceList: List of device ids of egress device
1730 ( Atleast 2 eress devices required in the list )
1731 * ingressDevice: device id of ingress device
1732 Optional:
1733 * ethType: specify ethType
1734 * ethSrc: specify ethSrc ( i.e. src mac addr )
1735 * ethDst: specify ethDst ( i.e. dst mac addr )
1736 * bandwidth: specify bandwidth capacity of link
1737 * lambdaAlloc: if True, intent will allocate lambda
1738 for the specified intent
1739 * ipProto: specify ip protocol
1740 * ipSrc: specify ip source address
1741 * ipDst: specify ip destination address
1742 * tcpSrc: specify tcp source port
1743 * tcpDst: specify tcp destination port
1744 * setEthSrc: action to Rewrite Source MAC Address
1745 * setEthDst: action to Rewrite Destination MAC Address
Jeremy Songster832f9e92016-05-05 14:30:49 -07001746 * vlanId: specify vlan Id
Jeremy Songsterff553672016-05-12 17:06:23 -07001747 * setVlan: specify VLAN ID treatment
Jeremy Songsterc032f162016-08-04 17:14:49 -07001748 * encap: specify an encapsulation type
kelvin-onlabb9408212015-04-01 13:34:04 -07001749 Description:
1750 Adds a singlepoint-to-multipoint intent ( uni-directional ) by
1751 specifying device id's and optional fields
1752 Returns:
1753 A string of the intent id or None on error
1754
1755 NOTE: This function may change depending on the
1756 options developers provide for singlepoint-to-multipoint
1757 intent via cli
1758 """
1759 try:
Jeremy Songsterff553672016-05-12 17:06:23 -07001760 cmd = "add-single-to-multi-intent"
kelvin-onlabb9408212015-04-01 13:34:04 -07001761
Jeremy Songsterff553672016-05-12 17:06:23 -07001762 if ethType:
1763 cmd += " --ethType " + str( ethType )
1764 if ethSrc:
1765 cmd += " --ethSrc " + str( ethSrc )
1766 if ethDst:
1767 cmd += " --ethDst " + str( ethDst )
1768 if bandwidth:
1769 cmd += " --bandwidth " + str( bandwidth )
1770 if lambdaAlloc:
1771 cmd += " --lambda "
1772 if ipProto:
1773 cmd += " --ipProto " + str( ipProto )
1774 if ipSrc:
1775 cmd += " --ipSrc " + str( ipSrc )
1776 if ipDst:
1777 cmd += " --ipDst " + str( ipDst )
1778 if tcpSrc:
1779 cmd += " --tcpSrc " + str( tcpSrc )
1780 if tcpDst:
1781 cmd += " --tcpDst " + str( tcpDst )
1782 if setEthSrc:
1783 cmd += " --setEthSrc " + str( setEthSrc )
1784 if setEthDst:
1785 cmd += " --setEthDst " + str( setEthDst )
1786 if vlanId:
1787 cmd += " -v " + str( vlanId )
1788 if setVlan:
1789 cmd += " --setVlan " + str( setVlan )
Jeremy Songster9385d412016-06-02 17:57:36 -07001790 if partial:
1791 cmd += " --partial"
Jeremy Songsterc032f162016-08-04 17:14:49 -07001792 if encap:
1793 cmd += " --encapsulation " + str( encap )
kelvin-onlabb9408212015-04-01 13:34:04 -07001794
1795 # Check whether the user appended the port
1796 # or provided it as an input
Jon Hall08f61bc2015-04-13 16:00:30 -07001797
kelvin-onlabb9408212015-04-01 13:34:04 -07001798 if "/" in ingressDevice:
1799 cmd += " " + str( ingressDevice )
1800 else:
1801 if not portIngress:
1802 main.log.error( "You must specify " +
1803 "the Ingress port" )
1804 return main.FALSE
1805
1806 cmd += " " +\
1807 str( ingressDevice ) + "/" +\
1808 str( portIngress )
1809
1810 if portEgressList is None:
1811 for egressDevice in egressDeviceList:
1812 if "/" in egressDevice:
1813 cmd += " " + str( egressDevice )
1814 else:
1815 main.log.error( "You must specify " +
1816 "the egress port" )
1817 # TODO: perhaps more meaningful return
1818 return main.FALSE
1819 else:
1820 if len( egressDeviceList ) == len( portEgressList ):
Jon Hall08f61bc2015-04-13 16:00:30 -07001821 for egressDevice, portEgress in zip( egressDeviceList,
1822 portEgressList ):
kelvin-onlabb9408212015-04-01 13:34:04 -07001823 cmd += " " + \
1824 str( egressDevice ) + "/" +\
1825 str( portEgress )
kelvin-onlab38143812015-04-01 15:03:01 -07001826 else:
Jon Hall08f61bc2015-04-13 16:00:30 -07001827 main.log.error( "Device list and port list does not " +
1828 "have the same length" )
kelvin-onlab38143812015-04-01 15:03:01 -07001829 return main.FALSE
kelvin-onlabb9408212015-04-01 13:34:04 -07001830 handle = self.sendline( cmd )
You Wangb5a55f72017-03-03 12:51:05 -08001831 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001832 assert "Command not found:" not in handle, handle
kelvin-onlabb9408212015-04-01 13:34:04 -07001833 # If error, return error message
1834 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001835 main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " +
kelvin-onlabb9408212015-04-01 13:34:04 -07001836 "intent" )
shahshreyac2f97072015-03-19 17:04:29 -07001837 return None
kelvin-onlabb9408212015-04-01 13:34:04 -07001838 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001839 match = re.search( 'id=0x([\da-f]+),', handle )
kelvin-onlabb9408212015-04-01 13:34:04 -07001840 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001841 return match.group()[ 3:-1 ]
kelvin-onlabb9408212015-04-01 13:34:04 -07001842 else:
1843 main.log.error( "Error, intent ID not found" )
1844 return None
Jon Hallc6793552016-01-19 14:18:37 -08001845 except AssertionError:
1846 main.log.exception( "" )
1847 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001848 except TypeError:
1849 main.log.exception( self.name + ": Object not as expected" )
1850 return None
shahshreyad0c80432014-12-04 16:56:05 -08001851 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001852 main.log.error( self.name + ": EOF exception found" )
1853 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001854 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001855 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001856 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001857 main.cleanAndExit()
shahshreyad0c80432014-12-04 16:56:05 -08001858
Hari Krishna9e232602015-04-13 17:29:08 -07001859 def addMplsIntent(
1860 self,
1861 ingressDevice,
1862 egressDevice,
Hari Krishna87a17f12015-04-13 17:42:23 -07001863 ingressPort="",
1864 egressPort="",
Hari Krishna9e232602015-04-13 17:29:08 -07001865 ethType="",
1866 ethSrc="",
1867 ethDst="",
1868 bandwidth="",
1869 lambdaAlloc=False,
1870 ipProto="",
1871 ipSrc="",
1872 ipDst="",
1873 tcpSrc="",
1874 tcpDst="",
Hari Krishna87a17f12015-04-13 17:42:23 -07001875 ingressLabel="",
Hari Krishnadfff6672015-04-13 17:53:27 -07001876 egressLabel="",
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001877 priority="" ):
Hari Krishna9e232602015-04-13 17:29:08 -07001878 """
1879 Required:
1880 * ingressDevice: device id of ingress device
1881 * egressDevice: device id of egress device
1882 Optional:
1883 * ethType: specify ethType
1884 * ethSrc: specify ethSrc ( i.e. src mac addr )
1885 * ethDst: specify ethDst ( i.e. dst mac addr )
1886 * bandwidth: specify bandwidth capacity of link
1887 * lambdaAlloc: if True, intent will allocate lambda
1888 for the specified intent
1889 * ipProto: specify ip protocol
1890 * ipSrc: specify ip source address
1891 * ipDst: specify ip destination address
1892 * tcpSrc: specify tcp source port
1893 * tcpDst: specify tcp destination port
1894 * ingressLabel: Ingress MPLS label
1895 * egressLabel: Egress MPLS label
1896 Description:
1897 Adds MPLS intent by
1898 specifying device id's and optional fields
1899 Returns:
1900 A string of the intent id or None on error
1901
1902 NOTE: This function may change depending on the
1903 options developers provide for MPLS
1904 intent via cli
1905 """
1906 try:
Jeremy Songsterff553672016-05-12 17:06:23 -07001907 cmd = "add-mpls-intent"
Hari Krishna9e232602015-04-13 17:29:08 -07001908
Jeremy Songsterff553672016-05-12 17:06:23 -07001909 if ethType:
1910 cmd += " --ethType " + str( ethType )
1911 if ethSrc:
1912 cmd += " --ethSrc " + str( ethSrc )
1913 if ethDst:
1914 cmd += " --ethDst " + str( ethDst )
1915 if bandwidth:
1916 cmd += " --bandwidth " + str( bandwidth )
1917 if lambdaAlloc:
1918 cmd += " --lambda "
1919 if ipProto:
1920 cmd += " --ipProto " + str( ipProto )
1921 if ipSrc:
1922 cmd += " --ipSrc " + str( ipSrc )
1923 if ipDst:
1924 cmd += " --ipDst " + str( ipDst )
1925 if tcpSrc:
1926 cmd += " --tcpSrc " + str( tcpSrc )
1927 if tcpDst:
1928 cmd += " --tcpDst " + str( tcpDst )
1929 if ingressLabel:
1930 cmd += " --ingressLabel " + str( ingressLabel )
1931 if egressLabel:
1932 cmd += " --egressLabel " + str( egressLabel )
1933 if priority:
1934 cmd += " --priority " + str( priority )
Hari Krishna9e232602015-04-13 17:29:08 -07001935
1936 # Check whether the user appended the port
1937 # or provided it as an input
1938 if "/" in ingressDevice:
1939 cmd += " " + str( ingressDevice )
1940 else:
Hari Krishna87a17f12015-04-13 17:42:23 -07001941 if not ingressPort:
Hari Krishna9e232602015-04-13 17:29:08 -07001942 main.log.error( "You must specify the ingress port" )
1943 return None
1944
1945 cmd += " " + \
1946 str( ingressDevice ) + "/" +\
Hari Krishna87a17f12015-04-13 17:42:23 -07001947 str( ingressPort ) + " "
Hari Krishna9e232602015-04-13 17:29:08 -07001948
1949 if "/" in egressDevice:
1950 cmd += " " + str( egressDevice )
1951 else:
Hari Krishna87a17f12015-04-13 17:42:23 -07001952 if not egressPort:
Hari Krishna9e232602015-04-13 17:29:08 -07001953 main.log.error( "You must specify the egress port" )
1954 return None
1955
1956 cmd += " " +\
1957 str( egressDevice ) + "/" +\
Hari Krishna87a17f12015-04-13 17:42:23 -07001958 str( egressPort )
Hari Krishna9e232602015-04-13 17:29:08 -07001959
1960 handle = self.sendline( cmd )
You Wangb5a55f72017-03-03 12:51:05 -08001961 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001962 assert "Command not found:" not in handle, handle
Hari Krishna9e232602015-04-13 17:29:08 -07001963 # If error, return error message
1964 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001965 main.log.error( self.name + ": Error in adding mpls intent" )
Hari Krishna9e232602015-04-13 17:29:08 -07001966 return None
1967 else:
1968 # TODO: print out all the options in this message?
1969 main.log.info( "MPLS intent installed between " +
1970 str( ingressDevice ) + " and " +
1971 str( egressDevice ) )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001972 match = re.search( 'id=0x([\da-f]+),', handle )
Hari Krishna9e232602015-04-13 17:29:08 -07001973 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001974 return match.group()[ 3:-1 ]
Hari Krishna9e232602015-04-13 17:29:08 -07001975 else:
1976 main.log.error( "Error, intent ID not found" )
1977 return None
Jon Hallc6793552016-01-19 14:18:37 -08001978 except AssertionError:
1979 main.log.exception( "" )
1980 return None
Hari Krishna9e232602015-04-13 17:29:08 -07001981 except TypeError:
1982 main.log.exception( self.name + ": Object not as expected" )
1983 return None
1984 except pexpect.EOF:
1985 main.log.error( self.name + ": EOF exception found" )
1986 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001987 main.cleanAndExit()
Hari Krishna9e232602015-04-13 17:29:08 -07001988 except Exception:
1989 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001990 main.cleanAndExit()
Hari Krishna9e232602015-04-13 17:29:08 -07001991
Jon Hallefbd9792015-03-05 16:11:36 -08001992 def removeIntent( self, intentId, app='org.onosproject.cli',
1993 purge=False, sync=False ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001994 """
shahshreya1c818fc2015-02-26 13:44:08 -08001995 Remove intent for specified application id and intent id
Jon Hall61282e32015-03-19 11:34:11 -07001996 Optional args:-
shahshreya1c818fc2015-02-26 13:44:08 -08001997 -s or --sync: Waits for the removal before returning
Jon Hall61282e32015-03-19 11:34:11 -07001998 -p or --purge: Purge the intent from the store after removal
1999
Jon Halle3f39ff2015-01-13 11:50:53 -08002000 Returns:
Jon Hall6509dbf2016-06-21 17:01:17 -07002001 main.FALSE on error and
Jon Halle3f39ff2015-01-13 11:50:53 -08002002 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08002003 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04002004 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002005 cmdStr = "remove-intent"
shahshreya1c818fc2015-02-26 13:44:08 -08002006 if purge:
2007 cmdStr += " -p"
2008 if sync:
2009 cmdStr += " -s"
2010
2011 cmdStr += " " + app + " " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002012 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002013 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002014 assert "Command not found:" not in handle, handle
Jon Halle3f39ff2015-01-13 11:50:53 -08002015 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07002016 main.log.error( self.name + ": Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002017 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04002018 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002019 # TODO: Should this be main.TRUE
2020 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002021 except AssertionError:
2022 main.log.exception( "" )
2023 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002024 except TypeError:
2025 main.log.exception( self.name + ": Object not as expected" )
2026 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04002027 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002028 main.log.error( self.name + ": EOF exception found" )
2029 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002030 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002031 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002032 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002033 main.cleanAndExit()
andrewonlab9a50dfe2014-10-17 17:22:31 -04002034
YPZhangfebf7302016-05-24 16:45:56 -07002035 def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ):
Jeremy42df2e72016-02-23 16:37:46 -08002036 """
2037 Description:
2038 Remove all the intents
2039 Optional args:-
2040 -s or --sync: Waits for the removal before returning
2041 -p or --purge: Purge the intent from the store after removal
2042 Returns:
2043 Returns main.TRUE if all intents are removed, otherwise returns
2044 main.FALSE; Returns None for exception
2045 """
2046 try:
2047 cmdStr = "remove-intent"
2048 if purge:
2049 cmdStr += " -p"
2050 if sync:
2051 cmdStr += " -s"
2052
2053 cmdStr += " " + app
YPZhangfebf7302016-05-24 16:45:56 -07002054 handle = self.sendline( cmdStr, timeout=timeout )
You Wangb5a55f72017-03-03 12:51:05 -08002055 assert handle is not None, "Error in sendline"
Jeremy42df2e72016-02-23 16:37:46 -08002056 assert "Command not found:" not in handle, handle
2057 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07002058 main.log.error( self.name + ": Error in removing intent" )
Jeremy42df2e72016-02-23 16:37:46 -08002059 return main.FALSE
2060 else:
2061 return main.TRUE
2062 except AssertionError:
2063 main.log.exception( "" )
2064 return None
2065 except TypeError:
2066 main.log.exception( self.name + ": Object not as expected" )
2067 return None
2068 except pexpect.EOF:
2069 main.log.error( self.name + ": EOF exception found" )
2070 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002071 main.cleanAndExit()
Jeremy42df2e72016-02-23 16:37:46 -08002072 except Exception:
2073 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002074 main.cleanAndExit()
Jeremy42df2e72016-02-23 16:37:46 -08002075
Hari Krishnaacabd5a2015-07-01 17:10:19 -07002076 def purgeWithdrawnIntents( self ):
Hari Krishna0ce0e152015-06-23 09:55:29 -07002077 """
2078 Purges all WITHDRAWN Intents
2079 """
2080 try:
2081 cmdStr = "purge-intents"
2082 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002083 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002084 assert "Command not found:" not in handle, handle
Hari Krishna0ce0e152015-06-23 09:55:29 -07002085 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07002086 main.log.error( self.name + ": Error in purging intents" )
Hari Krishna0ce0e152015-06-23 09:55:29 -07002087 return main.FALSE
2088 else:
2089 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08002090 except AssertionError:
2091 main.log.exception( "" )
2092 return None
Hari Krishna0ce0e152015-06-23 09:55:29 -07002093 except TypeError:
2094 main.log.exception( self.name + ": Object not as expected" )
2095 return None
2096 except pexpect.EOF:
2097 main.log.error( self.name + ": EOF exception found" )
2098 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002099 main.cleanAndExit()
Hari Krishna0ce0e152015-06-23 09:55:29 -07002100 except Exception:
2101 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002102 main.cleanAndExit()
Hari Krishna0ce0e152015-06-23 09:55:29 -07002103
Devin Lime6fe3c42017-10-18 16:28:40 -07002104 def wipeout( self ):
2105 """
2106 Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS.
2107 """
2108 try:
2109 cmdStr = "wipe-out please"
2110 handle = self.sendline( cmdStr, timeout=60 )
2111 assert handle is not None, "Error in sendline"
2112 assert "Command not found:" not in handle, handle
2113 return main.TRUE
2114 except AssertionError:
2115 main.log.exception( "" )
2116 return None
2117 except TypeError:
2118 main.log.exception( self.name + ": Object not as expected" )
2119 return None
2120 except pexpect.EOF:
2121 main.log.error( self.name + ": EOF exception found" )
2122 main.log.error( self.name + ": " + self.handle.before )
2123 main.cleanAndExit()
2124 except Exception:
2125 main.log.exception( self.name + ": Uncaught exception!" )
2126 main.cleanAndExit()
2127
kelvin-onlabd3b64892015-01-20 13:26:24 -08002128 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08002129 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08002130 NOTE: This method should be used after installing application:
2131 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08002132 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002133 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08002134 Description:
2135 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08002136 """
pingping-lin8b306ac2014-11-17 18:13:51 -08002137 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002138 cmdStr = "routes"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002139 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002140 cmdStr += " -j"
2141 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002142 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002143 assert "Command not found:" not in handle, handle
pingping-lin8b306ac2014-11-17 18:13:51 -08002144 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002145 except AssertionError:
2146 main.log.exception( "" )
2147 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002148 except TypeError:
2149 main.log.exception( self.name + ": Object not as expected" )
2150 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08002151 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002152 main.log.error( self.name + ": EOF exception found" )
2153 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002154 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002155 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002156 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002157 main.cleanAndExit()
pingping-lin8b306ac2014-11-17 18:13:51 -08002158
pingping-lin54b03372015-08-13 14:43:10 -07002159 def ipv4RouteNumber( self ):
2160 """
2161 NOTE: This method should be used after installing application:
2162 onos-app-sdnip
2163 Description:
2164 Obtain the total IPv4 routes number in the system
2165 """
2166 try:
Pratik Parab57963572017-05-09 11:37:54 -07002167 cmdStr = "routes -j"
pingping-lin54b03372015-08-13 14:43:10 -07002168 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002169 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002170 assert "Command not found:" not in handle, handle
pingping-lin54b03372015-08-13 14:43:10 -07002171 jsonResult = json.loads( handle )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002172 return len( jsonResult[ 'routes4' ] )
Jon Hallc6793552016-01-19 14:18:37 -08002173 except AssertionError:
2174 main.log.exception( "" )
2175 return None
2176 except ( TypeError, ValueError ):
2177 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) )
pingping-lin54b03372015-08-13 14:43:10 -07002178 return None
2179 except pexpect.EOF:
2180 main.log.error( self.name + ": EOF exception found" )
2181 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002182 main.cleanAndExit()
pingping-lin54b03372015-08-13 14:43:10 -07002183 except Exception:
2184 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002185 main.cleanAndExit()
pingping-lin54b03372015-08-13 14:43:10 -07002186
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002187 # =============Function to check Bandwidth allocation========
Jon Hall0e240372018-05-02 11:21:57 -07002188 def allocations( self, jsonFormat = True ):
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07002189 """
2190 Description:
2191 Obtain Bandwidth Allocation Information from ONOS cli.
2192 """
2193 try:
2194 cmdStr = "allocations"
2195 if jsonFormat:
2196 cmdStr += " -j"
Jon Hall0e240372018-05-02 11:21:57 -07002197 handle = self.sendline( cmdStr, timeout=300 )
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07002198 assert handle is not None, "Error in sendline"
2199 assert "Command not found:" not in handle, handle
2200 return handle
2201 except AssertionError:
2202 main.log.exception( "" )
2203 return None
2204 except ( TypeError, ValueError ):
2205 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) )
2206 return None
2207 except pexpect.EOF:
2208 main.log.error( self.name + ": EOF exception found" )
2209 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002210 main.cleanAndExit()
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07002211 except Exception:
2212 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002213 main.cleanAndExit()
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07002214
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002215 def intents( self, jsonFormat = True, summary = False, **intentargs ):
kelvin8ec71442015-01-15 16:57:00 -08002216 """
andrewonlabe6745342014-10-17 14:29:13 -04002217 Description:
Jon Hallff566d52016-01-15 14:45:36 -08002218 Obtain intents from the ONOS cli.
2219 Optional:
2220 * jsonFormat: Enable output formatting in json, default to True
2221 * summary: Whether only output the intent summary, defaults to False
2222 * type: Only output a certain type of intent. This options is valid
2223 only when jsonFormat is True and summary is True.
kelvin-onlab898a6c62015-01-16 14:13:53 -08002224 """
andrewonlabe6745342014-10-17 14:29:13 -04002225 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002226 cmdStr = "intents"
pingping-lin8244a3b2015-09-16 13:36:56 -07002227 if summary:
2228 cmdStr += " -s"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002229 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002230 cmdStr += " -j"
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07002231 handle = self.sendline( cmdStr, timeout=300 )
You Wangb5a55f72017-03-03 12:51:05 -08002232 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002233 assert "Command not found:" not in handle, handle
pingping-lin8244a3b2015-09-16 13:36:56 -07002234 args = utilities.parse_args( [ "TYPE" ], **intentargs )
acsmars5b5fbaf2015-09-18 10:38:20 -07002235 if "TYPE" in args.keys():
Jon Hallff566d52016-01-15 14:45:36 -08002236 intentType = args[ "TYPE" ]
acsmars5b5fbaf2015-09-18 10:38:20 -07002237 else:
Jon Hallff566d52016-01-15 14:45:36 -08002238 intentType = ""
2239 # IF we want the summary of a specific intent type
2240 if jsonFormat and summary and ( intentType != "" ):
pingping-lin8244a3b2015-09-16 13:36:56 -07002241 jsonResult = json.loads( handle )
Jon Hallff566d52016-01-15 14:45:36 -08002242 if intentType in jsonResult.keys():
2243 return jsonResult[ intentType ]
pingping-lin8244a3b2015-09-16 13:36:56 -07002244 else:
Jon Hallff566d52016-01-15 14:45:36 -08002245 main.log.error( "unknown TYPE, returning all types of intents" )
pingping-lin8244a3b2015-09-16 13:36:56 -07002246 return handle
2247 else:
2248 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002249 except AssertionError:
2250 main.log.exception( "" )
2251 return None
2252 except ( TypeError, ValueError ):
2253 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) )
pingping-lin54b03372015-08-13 14:43:10 -07002254 return None
2255 except pexpect.EOF:
2256 main.log.error( self.name + ": EOF exception found" )
2257 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002258 main.cleanAndExit()
pingping-lin54b03372015-08-13 14:43:10 -07002259 except Exception:
2260 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002261 main.cleanAndExit()
pingping-lin54b03372015-08-13 14:43:10 -07002262
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002263 def getIntentState( self, intentsId, intentsJson=None ):
kelvin-onlab54400a92015-02-26 18:05:51 -08002264 """
You Wangfdcbfc42016-05-16 12:16:53 -07002265 Description:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002266 Gets intent state. Accepts a single intent ID (string type) or a
You Wangfdcbfc42016-05-16 12:16:53 -07002267 list of intent IDs.
2268 Parameters:
2269 intentsId: intent ID, both string type and list type are acceptable
kelvin-onlab54400a92015-02-26 18:05:51 -08002270 intentsJson: parsed json object from the onos:intents api
You Wangfdcbfc42016-05-16 12:16:53 -07002271 Returns:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002272 Returns the state (string type) of the ID if a single intent ID is
You Wangfdcbfc42016-05-16 12:16:53 -07002273 accepted.
2274 Returns a list of dictionaries if a list of intent IDs is accepted,
2275 and each dictionary maps 'id' to the Intent ID and 'state' to
2276 corresponding intent state.
kelvin-onlab54400a92015-02-26 18:05:51 -08002277 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002278
kelvin-onlab54400a92015-02-26 18:05:51 -08002279 try:
2280 state = "State is Undefined"
2281 if not intentsJson:
Jon Hallc6793552016-01-19 14:18:37 -08002282 rawJson = self.intents()
kelvin-onlab54400a92015-02-26 18:05:51 -08002283 else:
Jon Hallc6793552016-01-19 14:18:37 -08002284 rawJson = intentsJson
2285 parsedIntentsJson = json.loads( rawJson )
Jon Hallefbd9792015-03-05 16:11:36 -08002286 if isinstance( intentsId, types.StringType ):
Jon Hallc6793552016-01-19 14:18:37 -08002287 for intent in parsedIntentsJson:
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002288 if intentsId == intent[ 'id' ]:
2289 state = intent[ 'state' ]
kelvin-onlab54400a92015-02-26 18:05:51 -08002290 return state
Jon Hallefbd9792015-03-05 16:11:36 -08002291 main.log.info( "Cannot find intent ID" + str( intentsId ) +
Jon Hall53158082017-05-18 11:17:00 -07002292 " in the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08002293 return state
Jon Hallefbd9792015-03-05 16:11:36 -08002294 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08002295 dictList = []
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002296 for i in xrange( len( intentsId ) ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08002297 stateDict = {}
Jon Hall53158082017-05-18 11:17:00 -07002298 for intent in parsedIntentsJson:
2299 if intentsId[ i ] == intent[ 'id' ]:
2300 stateDict[ 'state' ] = intent[ 'state' ]
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002301 stateDict[ 'id' ] = intentsId[ i ]
Jon Hallefbd9792015-03-05 16:11:36 -08002302 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08002303 break
Jon Hallefbd9792015-03-05 16:11:36 -08002304 if len( intentsId ) != len( dictList ):
Jon Hall53158082017-05-18 11:17:00 -07002305 main.log.warn( "Could not find all intents in ONOS output" )
2306 main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) )
kelvin-onlab07dbd012015-03-04 16:29:39 -08002307 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08002308 else:
Jon Hall53158082017-05-18 11:17:00 -07002309 main.log.info( "Invalid type for intentsId argument" )
kelvin-onlab54400a92015-02-26 18:05:51 -08002310 return None
Jon Hallc6793552016-01-19 14:18:37 -08002311 except ( TypeError, ValueError ):
2312 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) )
kelvin-onlab54400a92015-02-26 18:05:51 -08002313 return None
2314 except pexpect.EOF:
2315 main.log.error( self.name + ": EOF exception found" )
2316 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002317 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002318 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08002319 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002320 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07002321
Jon Hallf539eb92017-05-22 17:18:42 -07002322 def checkIntentState( self, intentsId, expectedState='INSTALLED' ):
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002323 """
2324 Description:
2325 Check intents state
2326 Required:
2327 intentsId - List of intents ID to be checked
2328 Optional:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002329 expectedState - Check the expected state(s) of each intents
kelvin-onlabf512e942015-06-08 19:42:59 -07002330 state in the list.
2331 *NOTE: You can pass in a list of expected state,
2332 Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ]
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002333 Return:
Jon Hall53158082017-05-18 11:17:00 -07002334 Returns main.TRUE only if all intent are the same as expected states,
2335 otherwise returns main.FALSE.
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002336 """
2337 try:
kelvin-onlabf512e942015-06-08 19:42:59 -07002338 returnValue = main.TRUE
Jon Hallf539eb92017-05-22 17:18:42 -07002339 # Generating a dictionary: intent id as a key and state as value
Devin Lim752dd7b2017-06-27 14:40:03 -07002340
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002341 # intentsDict = self.getIntentState( intentsId )
Devin Lim752dd7b2017-06-27 14:40:03 -07002342 intentsDict = []
2343 for intent in json.loads( self.intents() ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002344 if isinstance( intentsId, types.StringType ) \
2345 and intent.get( 'id' ) == intentsId:
2346 intentsDict.append( intent )
2347 elif isinstance( intentsId, types.ListType ) \
Devin Lim752dd7b2017-06-27 14:40:03 -07002348 and any( intent.get( 'id' ) == ids for ids in intentsId ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002349 intentsDict.append( intent )
Devin Lim752dd7b2017-06-27 14:40:03 -07002350
2351 if not intentsDict:
Jon Hallae04e622016-01-27 10:38:05 -08002352 main.log.info( self.name + ": There is something wrong " +
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002353 "getting intents state" )
2354 return main.FALSE
kelvin-onlabf512e942015-06-08 19:42:59 -07002355
2356 if isinstance( expectedState, types.StringType ):
2357 for intents in intentsDict:
2358 if intents.get( 'state' ) != expectedState:
kelvin-onlaba297c4d2015-06-01 13:53:55 -07002359 main.log.debug( self.name + " : Intent ID - " +
2360 intents.get( 'id' ) +
kelvin-onlabf512e942015-06-08 19:42:59 -07002361 " actual state = " +
2362 intents.get( 'state' )
2363 + " does not equal expected state = "
2364 + expectedState )
kelvin-onlaba297c4d2015-06-01 13:53:55 -07002365 returnValue = main.FALSE
kelvin-onlabf512e942015-06-08 19:42:59 -07002366 elif isinstance( expectedState, types.ListType ):
2367 for intents in intentsDict:
2368 if not any( state == intents.get( 'state' ) for state in
2369 expectedState ):
2370 main.log.debug( self.name + " : Intent ID - " +
2371 intents.get( 'id' ) +
2372 " actual state = " +
2373 intents.get( 'state' ) +
2374 " does not equal expected states = "
2375 + str( expectedState ) )
2376 returnValue = main.FALSE
2377
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002378 if returnValue == main.TRUE:
2379 main.log.info( self.name + ": All " +
2380 str( len( intentsDict ) ) +
kelvin-onlabf512e942015-06-08 19:42:59 -07002381 " intents are in " + str( expectedState ) +
2382 " state" )
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002383 return returnValue
2384 except TypeError:
2385 main.log.exception( self.name + ": Object not as expected" )
2386 return None
2387 except pexpect.EOF:
2388 main.log.error( self.name + ": EOF exception found" )
2389 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002390 main.cleanAndExit()
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002391 except Exception:
2392 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002393 main.cleanAndExit()
andrewonlabe6745342014-10-17 14:29:13 -04002394
Jon Hallf539eb92017-05-22 17:18:42 -07002395 def compareBandwidthAllocations( self, expectedAllocations ):
2396 """
2397 Description:
2398 Compare the allocated bandwidth with the given allocations
2399 Required:
2400 expectedAllocations - The expected ONOS output of the allocations command
2401 Return:
2402 Returns main.TRUE only if all intent are the same as expected states,
2403 otherwise returns main.FALSE.
2404 """
2405 # FIXME: Convert these string comparisons to object comparisons
2406 try:
2407 returnValue = main.TRUE
2408 bandwidthFailed = False
2409 rawAlloc = self.allocations()
2410 expectedFormat = StringIO( expectedAllocations )
2411 ONOSOutput = StringIO( rawAlloc )
2412 main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ),
2413 str( expectedFormat ) ) )
2414
2415 for actual, expected in izip( ONOSOutput, expectedFormat ):
2416 actual = actual.rstrip()
2417 expected = expected.rstrip()
2418 main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) )
2419 if actual != expected and 'allocated' in actual and 'allocated' in expected:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002420 marker1 = actual.find( 'allocated' )
2421 m1 = actual[ :marker1 ]
2422 marker2 = expected.find( 'allocated' )
2423 m2 = expected[ :marker2 ]
Jon Hallf539eb92017-05-22 17:18:42 -07002424 if m1 != m2:
2425 bandwidthFailed = True
2426 elif actual != expected and 'allocated' not in actual and 'allocated' not in expected:
2427 bandwidthFailed = True
2428 expectedFormat.close()
2429 ONOSOutput.close()
2430
2431 if bandwidthFailed:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002432 main.log.error( "Bandwidth not allocated correctly using Intents!!" )
Jon Hallf539eb92017-05-22 17:18:42 -07002433 returnValue = main.FALSE
2434 return returnValue
2435 except TypeError:
2436 main.log.exception( self.name + ": Object not as expected" )
2437 return None
2438 except pexpect.EOF:
2439 main.log.error( self.name + ": EOF exception found" )
2440 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002441 main.cleanAndExit()
Jon Hallf539eb92017-05-22 17:18:42 -07002442 except Exception:
2443 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002444 main.cleanAndExit()
Jon Hallf539eb92017-05-22 17:18:42 -07002445
You Wang66518af2016-05-16 15:32:59 -07002446 def compareIntent( self, intentDict ):
2447 """
2448 Description:
2449 Compare the intent ids and states provided in the argument with all intents in ONOS
2450 Return:
2451 Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE
2452 Arguments:
2453 intentDict: a dictionary which maps intent ids to intent states
2454 """
2455 try:
2456 intentsRaw = self.intents()
2457 intentsJson = json.loads( intentsRaw )
2458 intentDictONOS = {}
2459 for intent in intentsJson:
2460 intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ]
You Wang58d04452016-09-21 15:13:05 -07002461 returnValue = main.TRUE
You Wang66518af2016-05-16 15:32:59 -07002462 if len( intentDict ) != len( intentDictONOS ):
You Wang58d04452016-09-21 15:13:05 -07002463 main.log.warn( self.name + ": expected intent count does not match that in ONOS, " +
You Wang66518af2016-05-16 15:32:59 -07002464 str( len( intentDict ) ) + " expected and " +
2465 str( len( intentDictONOS ) ) + " actual" )
You Wang58d04452016-09-21 15:13:05 -07002466 returnValue = main.FALSE
You Wang66518af2016-05-16 15:32:59 -07002467 for intentID in intentDict.keys():
Jon Halle0f0b342017-04-18 11:43:47 -07002468 if intentID not in intentDictONOS.keys():
You Wang66518af2016-05-16 15:32:59 -07002469 main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" )
2470 returnValue = main.FALSE
You Wang58d04452016-09-21 15:13:05 -07002471 else:
2472 if intentDict[ intentID ] != intentDictONOS[ intentID ]:
2473 main.log.debug( self.name + ": intent ID - " + intentID +
2474 " expected state is " + intentDict[ intentID ] +
2475 " but actual state is " + intentDictONOS[ intentID ] )
2476 returnValue = main.FALSE
2477 intentDictONOS.pop( intentID )
2478 if len( intentDictONOS ) > 0:
2479 returnValue = main.FALSE
2480 for intentID in intentDictONOS.keys():
2481 main.log.debug( self.name + ": find extra intent in ONOS: intent ID " + intentID )
You Wang66518af2016-05-16 15:32:59 -07002482 if returnValue == main.TRUE:
2483 main.log.info( self.name + ": all intent IDs and states match that in ONOS" )
2484 return returnValue
You Wang1be9a512016-05-26 16:54:17 -07002485 except KeyError:
2486 main.log.exception( self.name + ": KeyError exception found" )
2487 return main.ERROR
You Wang66518af2016-05-16 15:32:59 -07002488 except ( TypeError, ValueError ):
2489 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) )
You Wang85560372016-05-18 10:44:33 -07002490 return main.ERROR
You Wang66518af2016-05-16 15:32:59 -07002491 except pexpect.EOF:
2492 main.log.error( self.name + ": EOF exception found" )
2493 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002494 main.cleanAndExit()
You Wang66518af2016-05-16 15:32:59 -07002495 except Exception:
2496 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002497 main.cleanAndExit()
You Wang66518af2016-05-16 15:32:59 -07002498
YPZhang14a4aa92016-07-15 13:37:15 -07002499 def checkIntentSummary( self, timeout=60, noExit=True ):
GlennRCed771242016-01-13 17:02:47 -08002500 """
2501 Description:
2502 Check the number of installed intents.
2503 Optional:
2504 timeout - the timeout for pexcept
YPZhang14a4aa92016-07-15 13:37:15 -07002505 noExit - If noExit, TestON will not exit if any except.
GlennRCed771242016-01-13 17:02:47 -08002506 Return:
2507 Returns main.TRUE only if the number of all installed intents are the same as total intents number
2508 , otherwise, returns main.FALSE.
2509 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002510
GlennRCed771242016-01-13 17:02:47 -08002511 try:
2512 cmd = "intents -s -j"
2513
2514 # Check response if something wrong
YPZhang14a4aa92016-07-15 13:37:15 -07002515 response = self.sendline( cmd, timeout=timeout, noExit=noExit )
Jon Halle0f0b342017-04-18 11:43:47 -07002516 if response is None:
YPZhang0584d432016-06-21 15:20:13 -07002517 return main.FALSE
GlennRCed771242016-01-13 17:02:47 -08002518 response = json.loads( response )
2519
2520 # get total and installed number, see if they are match
2521 allState = response.get( 'all' )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002522 if allState.get( 'total' ) == allState.get( 'installed' ):
Jon Halla478b852017-12-04 15:00:15 -08002523 main.log.info( 'Total Intents: {} Installed Intents: {}'.format(
2524 allState.get( 'total' ), allState.get( 'installed' ) ) )
GlennRCed771242016-01-13 17:02:47 -08002525 return main.TRUE
Jon Halla478b852017-12-04 15:00:15 -08002526 main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format(
2527 allState.get( 'total' ), allState.get( 'installed' ) ) )
GlennRCed771242016-01-13 17:02:47 -08002528 return main.FALSE
2529
Jon Hallc6793552016-01-19 14:18:37 -08002530 except ( TypeError, ValueError ):
2531 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) )
GlennRCed771242016-01-13 17:02:47 -08002532 return None
2533 except pexpect.EOF:
2534 main.log.error( self.name + ": EOF exception found" )
2535 main.log.error( self.name + ": " + self.handle.before )
YPZhang14a4aa92016-07-15 13:37:15 -07002536 if noExit:
2537 return main.FALSE
2538 else:
Devin Lim44075962017-08-11 10:56:37 -07002539 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07002540 except pexpect.TIMEOUT:
2541 main.log.error( self.name + ": ONOS timeout" )
2542 return None
GlennRCed771242016-01-13 17:02:47 -08002543 except Exception:
2544 main.log.exception( self.name + ": Uncaught exception!" )
YPZhang14a4aa92016-07-15 13:37:15 -07002545 if noExit:
2546 return main.FALSE
2547 else:
Devin Lim44075962017-08-11 10:56:37 -07002548 main.cleanAndExit()
GlennRCed771242016-01-13 17:02:47 -08002549
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07002550 def flows( self, state="any", jsonFormat=True, timeout=60, noExit=False, noCore=False, device=""):
kelvin8ec71442015-01-15 16:57:00 -08002551 """
Shreya Shah0f01c812014-10-26 20:15:28 -04002552 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002553 * jsonFormat: enable output formatting in json
Jeremy Songster306ed7a2016-07-19 10:59:07 -07002554 * noCore: suppress core flows
Shreya Shah0f01c812014-10-26 20:15:28 -04002555 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08002556 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08002557 """
Shreya Shah0f01c812014-10-26 20:15:28 -04002558 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002559 cmdStr = "flows"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002560 if jsonFormat:
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07002561 cmdStr += " -j"
Jeremy Songster306ed7a2016-07-19 10:59:07 -07002562 if noCore:
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07002563 cmdStr += " -n"
2564 cmdStr += " " + state
2565 cmdStr += " " + device
YPZhangebf9eb52016-05-12 15:20:24 -07002566 handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit )
You Wangb5a55f72017-03-03 12:51:05 -08002567 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002568 assert "Command not found:" not in handle, handle
2569 if re.search( "Error:", handle ):
2570 main.log.error( self.name + ": flows() response: " +
2571 str( handle ) )
2572 return handle
2573 except AssertionError:
2574 main.log.exception( "" )
GlennRCed771242016-01-13 17:02:47 -08002575 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002576 except TypeError:
2577 main.log.exception( self.name + ": Object not as expected" )
2578 return None
Jon Hallc6793552016-01-19 14:18:37 -08002579 except pexpect.TIMEOUT:
2580 main.log.error( self.name + ": ONOS timeout" )
2581 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04002582 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002583 main.log.error( self.name + ": EOF exception found" )
2584 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002585 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002586 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002587 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002588 main.cleanAndExit()
Shreya Shah0f01c812014-10-26 20:15:28 -04002589
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002590 def checkFlowCount( self, min=0, timeout=60 ):
Flavio Castroa1286fe2016-07-25 14:48:51 -07002591 count = self.getTotalFlowsNum( timeout=timeout )
Jon Halle0f0b342017-04-18 11:43:47 -07002592 count = int( count ) if count else 0
2593 return count if ( count > min ) else False
GlennRCed771242016-01-13 17:02:47 -08002594
Jon Halle0f0b342017-04-18 11:43:47 -07002595 def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ):
kelvin-onlab4df89f22015-04-13 18:10:23 -07002596 """
2597 Description:
GlennRCed771242016-01-13 17:02:47 -08002598 Check the if all the current flows are in ADDED state
Jon Hallc6793552016-01-19 14:18:37 -08002599 We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows,
2600 if the count of those states is 0, which means all current flows
2601 are in ADDED state, and return main.TRUE otherwise return main.FALSE
pingping-linbab7f8a2015-09-21 17:33:36 -07002602 Optional:
GlennRCed771242016-01-13 17:02:47 -08002603 * isPENDING: whether the PENDING_ADD is also a correct status
kelvin-onlab4df89f22015-04-13 18:10:23 -07002604 Return:
2605 returnValue - Returns main.TRUE only if all flows are in
Jon Hallc6793552016-01-19 14:18:37 -08002606 ADDED state or PENDING_ADD if the isPENDING
pingping-linbab7f8a2015-09-21 17:33:36 -07002607 parameter is set true, return main.FALSE otherwise.
kelvin-onlab4df89f22015-04-13 18:10:23 -07002608 """
2609 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002610 states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ]
GlennRCed771242016-01-13 17:02:47 -08002611 checkedStates = []
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002612 statesCount = [ 0, 0, 0, 0 ]
GlennRCed771242016-01-13 17:02:47 -08002613 for s in states:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002614 rawFlows = self.flows( state=s, timeout = timeout )
YPZhang240842b2016-05-17 12:00:50 -07002615 if rawFlows:
2616 # if we didn't get flows or flows function return None, we should return
2617 # main.Flase
2618 checkedStates.append( json.loads( rawFlows ) )
2619 else:
2620 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08002621 for i in range( len( states ) ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002622 for c in checkedStates[ i ]:
Jon Hallc6793552016-01-19 14:18:37 -08002623 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002624 statesCount[ i ] += int( c.get( "flowCount" ) )
Jon Hallc6793552016-01-19 14:18:37 -08002625 except TypeError:
2626 main.log.exception( "Json object not as expected" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002627 main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) )
kelvin-onlabf2ec6e02015-05-27 14:15:28 -07002628
GlennRCed771242016-01-13 17:02:47 -08002629 # We want to count PENDING_ADD if isPENDING is true
2630 if isPENDING:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002631 if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0:
GlennRCed771242016-01-13 17:02:47 -08002632 return main.FALSE
pingping-linbab7f8a2015-09-21 17:33:36 -07002633 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002634 if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0:
GlennRCed771242016-01-13 17:02:47 -08002635 return main.FALSE
GlennRCed771242016-01-13 17:02:47 -08002636 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08002637 except ( TypeError, ValueError ):
2638 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) )
kelvin-onlab4df89f22015-04-13 18:10:23 -07002639 return None
Jeremy Songster9385d412016-06-02 17:57:36 -07002640
YPZhang240842b2016-05-17 12:00:50 -07002641 except AssertionError:
2642 main.log.exception( "" )
2643 return None
Jon Halle0f0b342017-04-18 11:43:47 -07002644 except pexpect.TIMEOUT:
2645 main.log.error( self.name + ": ONOS timeout" )
2646 return None
kelvin-onlab4df89f22015-04-13 18:10:23 -07002647 except pexpect.EOF:
2648 main.log.error( self.name + ": EOF exception found" )
2649 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002650 main.cleanAndExit()
kelvin-onlab4df89f22015-04-13 18:10:23 -07002651 except Exception:
2652 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002653 main.cleanAndExit()
kelvin-onlab4df89f22015-04-13 18:10:23 -07002654
GlennRCed771242016-01-13 17:02:47 -08002655 def pushTestIntents( self, ingress, egress, batchSize, offset="",
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002656 options="", timeout=10, background = False, noExit=False, getResponse=False ):
kelvin8ec71442015-01-15 16:57:00 -08002657 """
andrewonlab87852b02014-11-19 18:44:19 -05002658 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08002659 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05002660 a specific point-to-point intent definition
2661 Required:
GlennRCed771242016-01-13 17:02:47 -08002662 * ingress: specify source dpid
2663 * egress: specify destination dpid
2664 * batchSize: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05002665 Optional:
GlennRCed771242016-01-13 17:02:47 -08002666 * offset: the keyOffset is where the next batch of intents
2667 will be installed
YPZhangb34b7e12016-06-14 14:28:19 -07002668 * noExit: If set to True, TestON will not exit if any error when issus command
2669 * getResponse: If set to True, function will return ONOS response.
2670
GlennRCed771242016-01-13 17:02:47 -08002671 Returns: If failed to push test intents, it will returen None,
2672 if successful, return true.
2673 Timeout expection will return None,
2674 TypeError will return false
2675 other expections will exit()
kelvin8ec71442015-01-15 16:57:00 -08002676 """
andrewonlab87852b02014-11-19 18:44:19 -05002677 try:
GlennRCed771242016-01-13 17:02:47 -08002678 if background:
2679 back = "&"
andrewonlab87852b02014-11-19 18:44:19 -05002680 else:
GlennRCed771242016-01-13 17:02:47 -08002681 back = ""
2682 cmd = "push-test-intents {} {} {} {} {} {}".format( options,
Jon Hallc6793552016-01-19 14:18:37 -08002683 ingress,
2684 egress,
2685 batchSize,
2686 offset,
2687 back )
YPZhangebf9eb52016-05-12 15:20:24 -07002688 response = self.sendline( cmd, timeout=timeout, noExit=noExit )
You Wangb5a55f72017-03-03 12:51:05 -08002689 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002690 assert "Command not found:" not in response, response
GlennRCed771242016-01-13 17:02:47 -08002691 main.log.info( response )
YPZhangb34b7e12016-06-14 14:28:19 -07002692 if getResponse:
2693 return response
2694
GlennRCed771242016-01-13 17:02:47 -08002695 # TODO: We should handle if there is failure in installation
2696 return main.TRUE
2697
Jon Hallc6793552016-01-19 14:18:37 -08002698 except AssertionError:
2699 main.log.exception( "" )
2700 return None
GlennRCed771242016-01-13 17:02:47 -08002701 except pexpect.TIMEOUT:
2702 main.log.error( self.name + ": ONOS timeout" )
Jon Halld4d4b372015-01-28 16:02:41 -08002703 return None
andrewonlab87852b02014-11-19 18:44:19 -05002704 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002705 main.log.error( self.name + ": EOF exception found" )
2706 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002707 main.cleanAndExit()
GlennRCed771242016-01-13 17:02:47 -08002708 except TypeError:
2709 main.log.exception( self.name + ": Object not as expected" )
Jon Hallc6793552016-01-19 14:18:37 -08002710 return None
Jon Hallfebb1c72015-03-05 13:30:09 -08002711 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002712 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002713 main.cleanAndExit()
andrewonlab87852b02014-11-19 18:44:19 -05002714
YPZhangebf9eb52016-05-12 15:20:24 -07002715 def getTotalFlowsNum( self, timeout=60, noExit=False ):
YPZhangb5d3f832016-01-23 22:54:26 -08002716 """
2717 Description:
YPZhangf6f14a02016-01-28 15:17:31 -08002718 Get the number of ADDED flows.
YPZhangb5d3f832016-01-23 22:54:26 -08002719 Return:
YPZhangf6f14a02016-01-28 15:17:31 -08002720 The number of ADDED flows
YPZhang14a4aa92016-07-15 13:37:15 -07002721 Or return None if any exceptions
YPZhangb5d3f832016-01-23 22:54:26 -08002722 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002723
YPZhangb5d3f832016-01-23 22:54:26 -08002724 try:
YPZhange3109a72016-02-02 11:25:37 -08002725 # get total added flows number
YPZhang14a4aa92016-07-15 13:37:15 -07002726 cmd = "flows -c added"
2727 rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit )
2728 if rawFlows:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002729 rawFlows = rawFlows.split( "\n" )
YPZhange3109a72016-02-02 11:25:37 -08002730 totalFlows = 0
YPZhang14a4aa92016-07-15 13:37:15 -07002731 for l in rawFlows:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002732 totalFlows += int( l.split( "Count=" )[ 1 ] )
YPZhang14a4aa92016-07-15 13:37:15 -07002733 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002734 main.log.error( "Response not as expected!" )
YPZhang14a4aa92016-07-15 13:37:15 -07002735 return None
2736 return totalFlows
YPZhange3109a72016-02-02 11:25:37 -08002737
You Wangd3cb2ce2016-05-16 14:01:24 -07002738 except ( TypeError, ValueError ):
YPZhang14a4aa92016-07-15 13:37:15 -07002739 main.log.exception( "{}: Object not as expected!".format( self.name ) )
YPZhangb5d3f832016-01-23 22:54:26 -08002740 return None
2741 except pexpect.EOF:
2742 main.log.error( self.name + ": EOF exception found" )
2743 main.log.error( self.name + ": " + self.handle.before )
YPZhang14a4aa92016-07-15 13:37:15 -07002744 if not noExit:
Devin Lim44075962017-08-11 10:56:37 -07002745 main.cleanAndExit()
YPZhang14a4aa92016-07-15 13:37:15 -07002746 return None
Jon Halle0f0b342017-04-18 11:43:47 -07002747 except pexpect.TIMEOUT:
2748 main.log.error( self.name + ": ONOS timeout" )
2749 return None
YPZhangb5d3f832016-01-23 22:54:26 -08002750 except Exception:
2751 main.log.exception( self.name + ": Uncaught exception!" )
YPZhang14a4aa92016-07-15 13:37:15 -07002752 if not noExit:
Devin Lim44075962017-08-11 10:56:37 -07002753 main.cleanAndExit()
YPZhang14a4aa92016-07-15 13:37:15 -07002754 return None
YPZhangb5d3f832016-01-23 22:54:26 -08002755
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002756 def getTotalIntentsNum( self, timeout=60, noExit = False ):
YPZhangb5d3f832016-01-23 22:54:26 -08002757 """
2758 Description:
2759 Get the total number of intents, include every states.
YPZhang14a4aa92016-07-15 13:37:15 -07002760 Optional:
2761 noExit - If noExit, TestON will not exit if any except.
YPZhangb5d3f832016-01-23 22:54:26 -08002762 Return:
2763 The number of intents
2764 """
2765 try:
2766 cmd = "summary -j"
YPZhang14a4aa92016-07-15 13:37:15 -07002767 response = self.sendline( cmd, timeout=timeout, noExit=noExit )
Jon Halle0f0b342017-04-18 11:43:47 -07002768 if response is None:
2769 return -1
YPZhangb5d3f832016-01-23 22:54:26 -08002770 response = json.loads( response )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002771 return int( response.get( "intents" ) )
You Wangd3cb2ce2016-05-16 14:01:24 -07002772 except ( TypeError, ValueError ):
2773 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) )
YPZhangb5d3f832016-01-23 22:54:26 -08002774 return None
2775 except pexpect.EOF:
2776 main.log.error( self.name + ": EOF exception found" )
2777 main.log.error( self.name + ": " + self.handle.before )
YPZhang14a4aa92016-07-15 13:37:15 -07002778 if noExit:
2779 return -1
2780 else:
Devin Lim44075962017-08-11 10:56:37 -07002781 main.cleanAndExit()
YPZhangb5d3f832016-01-23 22:54:26 -08002782 except Exception:
2783 main.log.exception( self.name + ": Uncaught exception!" )
YPZhang14a4aa92016-07-15 13:37:15 -07002784 if noExit:
2785 return -1
2786 else:
Devin Lim44075962017-08-11 10:56:37 -07002787 main.cleanAndExit()
YPZhangb5d3f832016-01-23 22:54:26 -08002788
kelvin-onlabd3b64892015-01-20 13:26:24 -08002789 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08002790 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002791 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002792 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002793 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08002794 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002795 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002796 cmdStr = "intents-events-metrics"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002797 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002798 cmdStr += " -j"
2799 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002800 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002801 assert "Command not found:" not in handle, handle
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002802 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002803 except AssertionError:
2804 main.log.exception( "" )
2805 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002806 except TypeError:
2807 main.log.exception( self.name + ": Object not as expected" )
2808 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002809 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002810 main.log.error( self.name + ": EOF exception found" )
2811 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002812 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002813 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002814 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002815 main.cleanAndExit()
Shreya Shah0f01c812014-10-26 20:15:28 -04002816
kelvin-onlabd3b64892015-01-20 13:26:24 -08002817 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08002818 """
2819 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04002820 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002821 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08002822 """
andrewonlab867212a2014-10-22 20:13:38 -04002823 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002824 cmdStr = "topology-events-metrics"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002825 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002826 cmdStr += " -j"
2827 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002828 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002829 assert "Command not found:" not in handle, handle
jenkins7ead5a82015-03-13 10:28:21 -07002830 if handle:
2831 return handle
Jon Hallc6358dd2015-04-10 12:44:28 -07002832 elif jsonFormat:
Jon Hallbe379602015-03-24 13:39:32 -07002833 # Return empty json
jenkins7ead5a82015-03-13 10:28:21 -07002834 return '{}'
Jon Hallc6358dd2015-04-10 12:44:28 -07002835 else:
2836 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002837 except AssertionError:
2838 main.log.exception( "" )
2839 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002840 except TypeError:
2841 main.log.exception( self.name + ": Object not as expected" )
2842 return None
andrewonlab867212a2014-10-22 20:13:38 -04002843 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002844 main.log.error( self.name + ": EOF exception found" )
2845 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002846 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002847 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002848 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002849 main.cleanAndExit()
andrewonlab867212a2014-10-22 20:13:38 -04002850
kelvin8ec71442015-01-15 16:57:00 -08002851 # Wrapper functions ****************
2852 # Wrapper functions use existing driver
2853 # functions and extends their use case.
2854 # For example, we may use the output of
2855 # a normal driver function, and parse it
2856 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04002857
kelvin-onlabd3b64892015-01-20 13:26:24 -08002858 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08002859 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04002860 Description:
2861 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08002862 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04002863 try:
kelvin8ec71442015-01-15 16:57:00 -08002864 # Obtain output of intents function
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002865 intentsStr = self.intents( jsonFormat=True )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07002866 if intentsStr is None:
2867 raise TypeError
Jon Hall6021e062017-01-30 11:10:06 -08002868 # Convert to a dictionary
2869 intents = json.loads( intentsStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002870 intentIdList = []
Jon Hall6021e062017-01-30 11:10:06 -08002871 for intent in intents:
2872 intentIdList.append( intent[ 'id' ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002873 return intentIdList
Jon Halld4d4b372015-01-28 16:02:41 -08002874 except TypeError:
2875 main.log.exception( self.name + ": Object not as expected" )
2876 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04002877 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002878 main.log.error( self.name + ": EOF exception found" )
2879 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002880 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002881 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002882 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002883 main.cleanAndExit()
andrewonlab9a50dfe2014-10-17 17:22:31 -04002884
You Wang3c276252016-09-21 15:21:36 -07002885 def flowAddedCount( self, deviceId, core=False ):
Jon Hall30b82fa2015-03-04 17:15:43 -08002886 """
2887 Determine the number of flow rules for the given device id that are
2888 in the added state
You Wang3c276252016-09-21 15:21:36 -07002889 Params:
2890 core: if True, only return the number of core flows added
Jon Hall30b82fa2015-03-04 17:15:43 -08002891 """
2892 try:
You Wang3c276252016-09-21 15:21:36 -07002893 if core:
2894 cmdStr = "flows any " + str( deviceId ) + " | " +\
2895 "grep 'state=ADDED' | grep org.onosproject.core | wc -l"
2896 else:
2897 cmdStr = "flows any " + str( deviceId ) + " | " +\
2898 "grep 'state=ADDED' | wc -l"
Jon Hall30b82fa2015-03-04 17:15:43 -08002899 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002900 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002901 assert "Command not found:" not in handle, handle
Jon Hall30b82fa2015-03-04 17:15:43 -08002902 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002903 except AssertionError:
2904 main.log.exception( "" )
2905 return None
Jon Hall30b82fa2015-03-04 17:15:43 -08002906 except pexpect.EOF:
2907 main.log.error( self.name + ": EOF exception found" )
2908 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002909 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002910 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -08002911 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002912 main.cleanAndExit()
andrewonlab95ce8322014-10-13 14:12:04 -04002913
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08002914 def groupAddedCount( self, deviceId, core=False ):
2915 """
2916 Determine the number of group rules for the given device id that are
2917 in the added state
2918 Params:
2919 core: if True, only return the number of core groups added
2920 """
2921 try:
2922 if core:
2923 cmdStr = "groups any " + str( deviceId ) + " | " +\
2924 "grep 'state=ADDED' | grep org.onosproject.core | wc -l"
2925 else:
2926 cmdStr = "groups any " + str( deviceId ) + " | " +\
2927 "grep 'state=ADDED' | wc -l"
2928 handle = self.sendline( cmdStr )
2929 assert handle is not None, "Error in sendline"
2930 assert "Command not found:" not in handle, handle
2931 return handle
2932 except AssertionError:
2933 main.log.exception( "" )
2934 return None
2935 except pexpect.EOF:
2936 main.log.error( self.name + ": EOF exception found" )
2937 main.log.error( self.name + ": " + self.handle.before )
2938 main.cleanAndExit()
2939 except Exception:
2940 main.log.exception( self.name + ": Uncaught exception!" )
2941 main.cleanAndExit()
2942
Andreas Pantelopoulos2eae3242018-03-06 13:47:20 -08002943 def addStaticRoute( self, subnet, intf):
2944 """
2945 Adds a static route to onos.
2946 Params:
2947 subnet: The subnet reaching through this route
2948 intf: The interface this route is reachable through
2949 """
2950 try:
2951 cmdStr = "route-add " + subnet + " " + intf
2952 handle = self.sendline( cmdStr )
2953 assert handle is not None, "Error in sendline"
2954 assert "Command not found:" not in handle, handle
2955 return handle
2956 except AssertionError:
2957 main.log.exception( "" )
2958 return None
2959 except pexpect.EOF:
2960 main.log.error( self.name + ": EOF exception found" )
2961 main.log.error( self.name + ": " + self.handle.before )
2962 main.cleanAndExit()
2963 except Exception:
2964 main.log.exception( self.name + ": Uncaught exception!" )
2965 main.cleanAndExit()
2966
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08002967 def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0):
2968 """
2969 Description:
2970 Check whether the number of groups for the given device id that
2971 are in ADDED state is bigger than minGroupCount.
2972 Required:
2973 * deviceId: device id to check the number of added group rules
2974 Optional:
2975 * minGroupCount: the number of groups to compare
2976 * core: if True, only check the number of core groups added
2977 * comparison: if 0, compare with greater than minFlowCount
2978 * if 1, compare with equal to minFlowCount
2979 Return:
2980 Returns the number of groups if it is bigger than minGroupCount,
2981 returns main.FALSE otherwise.
2982 """
2983 count = self.groupAddedCount( deviceId, core )
2984 count = int( count ) if count else 0
Jon Hall9677ed32018-04-24 11:16:23 -07002985 main.log.debug( "found {} groups".format( count ) )
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08002986 return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE
2987
You Wangc02f3be2018-05-18 12:14:23 -07002988 def getGroups( self, deviceId, groupType="any" ):
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07002989 """
2990 Retrieve groups from a specific device.
You Wangc02f3be2018-05-18 12:14:23 -07002991 deviceId: Id of the device from which we retrieve groups
2992 groupType: Type of group
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07002993 """
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07002994 try:
You Wangc02f3be2018-05-18 12:14:23 -07002995 groupCmd = "groups -t {0} any {1}".format( groupType, deviceId )
2996 handle = self.sendline( groupCmd )
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07002997 assert handle is not None, "Error in sendline"
2998 assert "Command not found:" not in handle, handle
2999 return handle
3000 except AssertionError:
3001 main.log.exception( "" )
3002 return None
3003 except TypeError:
3004 main.log.exception( self.name + ": Object not as expected" )
3005 return None
3006 except pexpect.EOF:
3007 main.log.error( self.name + ": EOF exception found" )
3008 main.log.error( self.name + ": " + self.handle.before )
3009 main.cleanAndExit()
3010 except Exception:
3011 main.log.exception( self.name + ": Uncaught exception!" )
3012 main.cleanAndExit()
3013
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08003014 def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0):
Jonghwan Hyuncf2345c2018-02-26 11:07:54 -08003015 """
3016 Description:
3017 Check whether the number of flow rules for the given device id that
3018 are in ADDED state is bigger than minFlowCount.
3019 Required:
3020 * deviceId: device id to check the number of added flow rules
3021 Optional:
3022 * minFlowCount: the number of flow rules to compare
3023 * core: if True, only check the number of core flows added
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08003024 * comparison: if 0, compare with greater than minFlowCount
3025 * if 1, compare with equal to minFlowCount
Jonghwan Hyuncf2345c2018-02-26 11:07:54 -08003026 Return:
3027 Returns the number of flow rules if it is bigger than minFlowCount,
3028 returns main.FALSE otherwise.
3029 """
3030 count = self.flowAddedCount( deviceId, core )
3031 count = int( count ) if count else 0
Jon Hall9677ed32018-04-24 11:16:23 -07003032 main.log.debug( "found {} flows".format( count ) )
Andreas Pantelopoulos2eae3242018-03-06 13:47:20 -08003033 return count if ((count > expectedFlowCount) if (comparison == 0) else (count == expectedFlowCount)) else main.FALSE
Jonghwan Hyuncf2345c2018-02-26 11:07:54 -08003034
kelvin-onlabd3b64892015-01-20 13:26:24 -08003035 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08003036 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04003037 Use 'devices' function to obtain list of all devices
3038 and parse the result to obtain a list of all device
3039 id's. Returns this list. Returns empty list if no
3040 devices exist
kelvin8ec71442015-01-15 16:57:00 -08003041 List is ordered sequentially
3042
andrewonlab3e15ead2014-10-15 14:21:34 -04003043 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08003044 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04003045 the ids. By obtaining the list of device ids on the fly,
3046 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08003047 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04003048 try:
kelvin8ec71442015-01-15 16:57:00 -08003049 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08003050 devicesStr = self.devices( jsonFormat=False )
3051 idList = []
kelvin8ec71442015-01-15 16:57:00 -08003052
kelvin-onlabd3b64892015-01-20 13:26:24 -08003053 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08003054 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003055 return idList
kelvin8ec71442015-01-15 16:57:00 -08003056
3057 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08003058 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08003059 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08003060 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08003061 # Split list further into arguments before and after string
3062 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08003063 # append to idList
3064 for arg in tempList:
3065 idList.append( arg.split( "id=" )[ 1 ] )
3066 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04003067
Jon Halld4d4b372015-01-28 16:02:41 -08003068 except TypeError:
3069 main.log.exception( self.name + ": Object not as expected" )
3070 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04003071 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003072 main.log.error( self.name + ": EOF exception found" )
3073 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003074 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003075 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003076 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003077 main.cleanAndExit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04003078
kelvin-onlabd3b64892015-01-20 13:26:24 -08003079 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08003080 """
andrewonlab7c211572014-10-15 16:45:20 -04003081 Uses 'nodes' function to obtain list of all nodes
3082 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08003083 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04003084 Returns:
3085 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08003086 """
andrewonlab7c211572014-10-15 16:45:20 -04003087 try:
Jon Hall5aa168b2015-03-23 14:23:09 -07003088 nodesStr = self.nodes( jsonFormat=True )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003089 idList = []
Jon Hall5aa168b2015-03-23 14:23:09 -07003090 # Sample nodesStr output
Jon Hallbd182782016-03-28 16:42:22 -07003091 # id=local, address=127.0.0.1:9876, state=READY *
kelvin-onlabd3b64892015-01-20 13:26:24 -08003092 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08003093 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003094 return idList
Jon Hall5aa168b2015-03-23 14:23:09 -07003095 nodesJson = json.loads( nodesStr )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003096 idList = [ node.get( 'id' ) for node in nodesJson ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08003097 return idList
Jon Hallc6793552016-01-19 14:18:37 -08003098 except ( TypeError, ValueError ):
3099 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) )
Jon Halld4d4b372015-01-28 16:02:41 -08003100 return None
andrewonlab7c211572014-10-15 16:45:20 -04003101 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003102 main.log.error( self.name + ": EOF exception found" )
3103 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003104 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003105 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003106 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003107 main.cleanAndExit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04003108
kelvin-onlabd3b64892015-01-20 13:26:24 -08003109 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08003110 """
Jon Halla91c4dc2014-10-22 12:57:04 -04003111 Return the first device from the devices api whose 'id' contains 'dpid'
3112 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08003113 """
Jon Halla91c4dc2014-10-22 12:57:04 -04003114 try:
kelvin8ec71442015-01-15 16:57:00 -08003115 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04003116 return None
3117 else:
kelvin8ec71442015-01-15 16:57:00 -08003118 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003119 rawDevices = self.devices()
3120 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08003121 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08003122 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08003123 # print "%s in %s?" % ( dpid, device[ 'id' ] )
3124 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04003125 return device
3126 return None
Jon Hallc6793552016-01-19 14:18:37 -08003127 except ( TypeError, ValueError ):
3128 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) )
Jon Halld4d4b372015-01-28 16:02:41 -08003129 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04003130 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003131 main.log.error( self.name + ": EOF exception found" )
3132 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003133 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003134 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003135 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003136 main.cleanAndExit()
Jon Halla91c4dc2014-10-22 12:57:04 -04003137
You Wang24139872016-05-03 11:48:47 -07003138 def getTopology( self, topologyOutput ):
3139 """
3140 Definition:
3141 Loads a json topology output
3142 Return:
3143 topology = current ONOS topology
3144 """
3145 import json
3146 try:
3147 # either onos:topology or 'topology' will work in CLI
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003148 topology = json.loads( topologyOutput )
Jeremy Songsterbc2d8ac2016-05-04 11:25:42 -07003149 main.log.debug( topology )
You Wang24139872016-05-03 11:48:47 -07003150 return topology
You Wangd3cb2ce2016-05-16 14:01:24 -07003151 except ( TypeError, ValueError ):
3152 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) )
3153 return None
You Wang24139872016-05-03 11:48:47 -07003154 except pexpect.EOF:
3155 main.log.error( self.name + ": EOF exception found" )
3156 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003157 main.cleanAndExit()
You Wang24139872016-05-03 11:48:47 -07003158 except Exception:
3159 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003160 main.cleanAndExit()
You Wang24139872016-05-03 11:48:47 -07003161
Pier6a0c4de2018-03-18 16:01:30 -07003162 def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08003163 """
Jon Hallefbd9792015-03-05 16:11:36 -08003164 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08003165 supplied values. By default this will report to main.log, but the
You Wang24139872016-05-03 11:48:47 -07003166 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08003167
Flavio Castro82ee2f62016-06-07 15:04:12 -07003168 Params: numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08003169 numolink = expected number of links
Flavio Castro82ee2f62016-06-07 15:04:12 -07003170 numoctrl = expected number of controllers
You Wang24139872016-05-03 11:48:47 -07003171 logLevel = level to log to.
3172 Currently accepts 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04003173
Jon Hallefbd9792015-03-05 16:11:36 -08003174 Returns: main.TRUE if the number of switches and links are correct,
3175 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04003176 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08003177 """
Flavio Castro82ee2f62016-06-07 15:04:12 -07003178 import json
Jon Hall42db6dc2014-10-24 19:03:48 -04003179 try:
You Wang13310252016-07-31 10:56:14 -07003180 summary = self.summary()
3181 summary = json.loads( summary )
Flavio Castrof5b3f872016-06-23 17:52:31 -07003182 except ( TypeError, ValueError ):
3183 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) )
3184 return main.ERROR
3185 try:
3186 topology = self.getTopology( self.topology() )
Jon Halle0f0b342017-04-18 11:43:47 -07003187 if topology == {} or topology is None or summary == {} or summary is None:
Jon Hall42db6dc2014-10-24 19:03:48 -04003188 return main.ERROR
3189 output = ""
kelvin8ec71442015-01-15 16:57:00 -08003190 # Is the number of switches is what we expected
3191 devices = topology.get( 'devices', False )
3192 links = topology.get( 'links', False )
Flavio Castro82ee2f62016-06-07 15:04:12 -07003193 nodes = summary.get( 'nodes', False )
3194 if devices is False or links is False or nodes is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04003195 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08003196 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08003197 # Is the number of links is what we expected
Pier6a0c4de2018-03-18 16:01:30 -07003198 linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1
Flavio Castro82ee2f62016-06-07 15:04:12 -07003199 nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1
3200 if switchCheck and linkCheck and nodeCheck:
kelvin8ec71442015-01-15 16:57:00 -08003201 # We expected the correct numbers
You Wang24139872016-05-03 11:48:47 -07003202 output = output + "The number of links and switches match "\
3203 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04003204 result = main.TRUE
3205 else:
You Wang24139872016-05-03 11:48:47 -07003206 output = output + \
3207 "The number of links and switches does not match " + \
3208 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04003209 result = main.FALSE
You Wang24139872016-05-03 11:48:47 -07003210 output = output + "\n ONOS sees %i devices" % int( devices )
3211 output = output + " (%i expected) " % int( numoswitch )
Pier6a0c4de2018-03-18 16:01:30 -07003212 if int( numolink ) > 0:
3213 output = output + "and %i links " % int( links )
3214 output = output + "(%i expected)" % int( numolink )
YPZhangd7e4b6e2016-06-17 16:07:55 -07003215 if int( numoctrl ) > 0:
Flavio Castro82ee2f62016-06-07 15:04:12 -07003216 output = output + "and %i controllers " % int( nodes )
3217 output = output + "(%i expected)" % int( numoctrl )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003218 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08003219 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003220 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08003221 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04003222 else:
You Wang24139872016-05-03 11:48:47 -07003223 main.log.info( output )
kelvin8ec71442015-01-15 16:57:00 -08003224 return result
Jon Hall42db6dc2014-10-24 19:03:48 -04003225 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003226 main.log.error( self.name + ": EOF exception found" )
3227 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003228 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003229 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003230 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003231 main.cleanAndExit()
Jon Hall1c9e8732014-10-27 19:29:27 -04003232
kelvin-onlabd3b64892015-01-20 13:26:24 -08003233 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08003234 """
Jon Hall1c9e8732014-10-27 19:29:27 -04003235 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08003236 deviceId must be the id of a device as seen in the onos devices command
3237 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04003238 role must be either master, standby, or none
3239
Jon Halle3f39ff2015-01-13 11:50:53 -08003240 Returns:
3241 main.TRUE or main.FALSE based on argument verification and
3242 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08003243 """
Jon Hall1c9e8732014-10-27 19:29:27 -04003244 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08003245 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04003246 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08003247 cmdStr = "device-role " +\
3248 str( deviceId ) + " " +\
3249 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08003250 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003251 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003252 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003253 assert "Command not found:" not in handle, handle
kelvin-onlab898a6c62015-01-16 14:13:53 -08003254 if re.search( "Error", handle ):
3255 # end color output to escape any colours
3256 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08003257 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08003258 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08003259 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08003260 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04003261 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08003262 main.log.error( "Invalid 'role' given to device_role(). " +
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003263 "Value was '" + str( role ) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04003264 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08003265 except AssertionError:
3266 main.log.exception( "" )
3267 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003268 except TypeError:
3269 main.log.exception( self.name + ": Object not as expected" )
3270 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04003271 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003272 main.log.error( self.name + ": EOF exception found" )
3273 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003274 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003275 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003276 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003277 main.cleanAndExit()
Jon Hall1c9e8732014-10-27 19:29:27 -04003278
kelvin-onlabd3b64892015-01-20 13:26:24 -08003279 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08003280 """
Jon Hall0dd09952018-04-19 09:59:11 -07003281 Lists all topology clusters
Jon Hallffb386d2014-11-21 13:43:38 -08003282 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08003283 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08003284 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08003285 try:
Jon Hall0dd09952018-04-19 09:59:11 -07003286 cmdStr = "topo-clusters"
kelvin-onlabd3b64892015-01-20 13:26:24 -08003287 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07003288 cmdStr += " -j"
3289 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003290 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003291 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -07003292 return handle
Jon Hallc6793552016-01-19 14:18:37 -08003293 except AssertionError:
3294 main.log.exception( "" )
3295 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003296 except TypeError:
3297 main.log.exception( self.name + ": Object not as expected" )
3298 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08003299 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003300 main.log.error( self.name + ": EOF exception found" )
3301 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003302 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003303 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003304 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003305 main.cleanAndExit()
Jon Hall73cf9cc2014-11-20 22:28:38 -08003306
kelvin-onlabd3b64892015-01-20 13:26:24 -08003307 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08003308 """
Jon Halle3f39ff2015-01-13 11:50:53 -08003309 CLI command to get the current leader for the Election test application
3310 NOTE: Requires installation of the onos-app-election feature
3311 Returns: Node IP of the leader if one exists
3312 None if none exists
3313 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08003314 """
Jon Hall94fd0472014-12-08 11:52:42 -08003315 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08003316 cmdStr = "election-test-leader"
3317 response = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003318 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003319 assert "Command not found:" not in response, response
Jon Halle3f39ff2015-01-13 11:50:53 -08003320 # Leader
3321 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08003322 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08003323 nodeSearch = re.search( leaderPattern, response )
3324 if nodeSearch:
3325 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08003326 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08003327 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08003328 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08003329 # no leader
3330 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08003331 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08003332 nullSearch = re.search( nullPattern, response )
3333 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08003334 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08003335 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08003336 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08003337 # error
Jon Hall0e240372018-05-02 11:21:57 -07003338 main.log.error( self.name + ": Error in electionTestLeader on " + self.name +
Jon Hall97cf84a2016-06-20 13:35:58 -07003339 ": " + "unexpected response" )
3340 main.log.error( repr( response ) )
3341 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08003342 except AssertionError:
3343 main.log.exception( "" )
3344 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003345 except TypeError:
3346 main.log.exception( self.name + ": Object not as expected" )
3347 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08003348 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003349 main.log.error( self.name + ": EOF exception found" )
3350 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003351 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003352 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003353 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003354 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08003355
kelvin-onlabd3b64892015-01-20 13:26:24 -08003356 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08003357 """
Jon Halle3f39ff2015-01-13 11:50:53 -08003358 CLI command to run for leadership of the Election test application.
3359 NOTE: Requires installation of the onos-app-election feature
3360 Returns: Main.TRUE on success
3361 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08003362 """
Jon Hall94fd0472014-12-08 11:52:42 -08003363 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08003364 cmdStr = "election-test-run"
3365 response = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003366 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003367 assert "Command not found:" not in response, response
kelvin-onlab898a6c62015-01-16 14:13:53 -08003368 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08003369 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08003370 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08003371 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08003372 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08003373 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08003374 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08003375 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08003376 # error
Jon Hall0e240372018-05-02 11:21:57 -07003377 main.log.error( self.name + ": Error in electionTestRun on " + self.name +
Jon Hall97cf84a2016-06-20 13:35:58 -07003378 ": " + "unexpected response" )
3379 main.log.error( repr( response ) )
3380 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08003381 except AssertionError:
3382 main.log.exception( "" )
3383 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003384 except TypeError:
3385 main.log.exception( self.name + ": Object not as expected" )
3386 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08003387 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003388 main.log.error( self.name + ": EOF exception found" )
3389 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003390 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003391 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003392 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003393 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08003394
kelvin-onlabd3b64892015-01-20 13:26:24 -08003395 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08003396 """
Jon Hall94fd0472014-12-08 11:52:42 -08003397 * CLI command to withdraw the local node from leadership election for
3398 * the Election test application.
3399 #NOTE: Requires installation of the onos-app-election feature
3400 Returns: Main.TRUE on success
3401 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08003402 """
Jon Hall94fd0472014-12-08 11:52:42 -08003403 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08003404 cmdStr = "election-test-withdraw"
3405 response = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003406 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003407 assert "Command not found:" not in response, response
kelvin-onlab898a6c62015-01-16 14:13:53 -08003408 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08003409 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08003410 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08003411 if re.search( successPattern, response ):
3412 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08003413 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08003414 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08003415 # error
Jon Hall0e240372018-05-02 11:21:57 -07003416 main.log.error( self.name + ": Error in electionTestWithdraw on " +
Jon Hall97cf84a2016-06-20 13:35:58 -07003417 self.name + ": " + "unexpected response" )
3418 main.log.error( repr( response ) )
3419 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08003420 except AssertionError:
3421 main.log.exception( "" )
3422 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003423 except TypeError:
3424 main.log.exception( self.name + ": Object not as expected" )
3425 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08003426 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003427 main.log.error( self.name + ": EOF exception found" )
3428 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003429 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003430 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003431 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003432 main.cleanAndExit()
Jon Hall1c9e8732014-10-27 19:29:27 -04003433
kelvin8ec71442015-01-15 16:57:00 -08003434 def getDevicePortsEnabledCount( self, dpid ):
3435 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003436 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08003437 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003438 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08003439 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003440 cmdStr = "onos:ports -e " + dpid + " | wc -l"
3441 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003442 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003443 assert "Command not found:" not in output, output
Jon Halle3f39ff2015-01-13 11:50:53 -08003444 if re.search( "No such device", output ):
Jon Hall0e240372018-05-02 11:21:57 -07003445 main.log.error( self.name + ": Error in getting ports" )
Jon Halle3f39ff2015-01-13 11:50:53 -08003446 return ( output, "Error" )
Jon Halla495f562016-05-16 18:03:26 -07003447 return output
Jon Hallc6793552016-01-19 14:18:37 -08003448 except AssertionError:
3449 main.log.exception( "" )
3450 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003451 except TypeError:
3452 main.log.exception( self.name + ": Object not as expected" )
3453 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003454 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003455 main.log.error( self.name + ": EOF exception found" )
3456 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003457 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003458 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003459 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003460 main.cleanAndExit()
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003461
kelvin8ec71442015-01-15 16:57:00 -08003462 def getDeviceLinksActiveCount( self, dpid ):
3463 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003464 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08003465 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003466 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08003467 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003468 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
3469 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003470 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003471 assert "Command not found:" not in output, output
Jon Halle3f39ff2015-01-13 11:50:53 -08003472 if re.search( "No such device", output ):
Jon Hall0e240372018-05-02 11:21:57 -07003473 main.log.error( self.name + ": Error in getting ports " )
kelvin-onlab898a6c62015-01-16 14:13:53 -08003474 return ( output, "Error " )
Jon Halla495f562016-05-16 18:03:26 -07003475 return output
Jon Hallc6793552016-01-19 14:18:37 -08003476 except AssertionError:
3477 main.log.exception( "" )
3478 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003479 except TypeError:
3480 main.log.exception( self.name + ": Object not as expected" )
3481 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003482 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003483 main.log.error( self.name + ": EOF exception found" )
3484 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003485 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003486 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003487 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003488 main.cleanAndExit()
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003489
kelvin8ec71442015-01-15 16:57:00 -08003490 def getAllIntentIds( self ):
3491 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003492 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08003493 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003494 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08003495 cmdStr = "onos:intents | grep id="
3496 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003497 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003498 assert "Command not found:" not in output, output
Jon Halle3f39ff2015-01-13 11:50:53 -08003499 if re.search( "Error", output ):
Jon Hall0e240372018-05-02 11:21:57 -07003500 main.log.error( self.name + ": Error in getting ports" )
Jon Halle3f39ff2015-01-13 11:50:53 -08003501 return ( output, "Error" )
Jon Halla495f562016-05-16 18:03:26 -07003502 return output
Jon Hallc6793552016-01-19 14:18:37 -08003503 except AssertionError:
3504 main.log.exception( "" )
3505 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003506 except TypeError:
3507 main.log.exception( self.name + ": Object not as expected" )
3508 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003509 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003510 main.log.error( self.name + ": EOF exception found" )
3511 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003512 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003513 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003514 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003515 main.cleanAndExit()
Jon Halld4d4b372015-01-28 16:02:41 -08003516
Jon Hall73509952015-02-24 16:42:56 -08003517 def intentSummary( self ):
3518 """
Jon Hallefbd9792015-03-05 16:11:36 -08003519 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08003520 """
3521 try:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00003522 intents = self.intents( )
Jon Hall08f61bc2015-04-13 16:00:30 -07003523 states = []
Jon Hall5aa168b2015-03-23 14:23:09 -07003524 for intent in json.loads( intents ):
Jon Hall08f61bc2015-04-13 16:00:30 -07003525 states.append( intent.get( 'state', None ) )
3526 out = [ ( i, states.count( i ) ) for i in set( states ) ]
Jon Hall63604932015-02-26 17:09:50 -08003527 main.log.info( dict( out ) )
Jon Hall73509952015-02-24 16:42:56 -08003528 return dict( out )
Jon Hallc6793552016-01-19 14:18:37 -08003529 except ( TypeError, ValueError ):
3530 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) )
Jon Hall73509952015-02-24 16:42:56 -08003531 return None
3532 except pexpect.EOF:
3533 main.log.error( self.name + ": EOF exception found" )
3534 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003535 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003536 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08003537 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003538 main.cleanAndExit()
Jon Hall63604932015-02-26 17:09:50 -08003539
Jon Hall61282e32015-03-19 11:34:11 -07003540 def leaders( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08003541 """
3542 Returns the output of the leaders command.
Jon Hall61282e32015-03-19 11:34:11 -07003543 Optional argument:
3544 * jsonFormat - boolean indicating if you want output in json
Jon Hall63604932015-02-26 17:09:50 -08003545 """
Jon Hall63604932015-02-26 17:09:50 -08003546 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07003547 cmdStr = "onos:leaders"
Jon Hall61282e32015-03-19 11:34:11 -07003548 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07003549 cmdStr += " -j"
3550 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003551 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003552 assert "Command not found:" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -07003553 return output
Jon Hallc6793552016-01-19 14:18:37 -08003554 except AssertionError:
3555 main.log.exception( "" )
3556 return None
Jon Hall63604932015-02-26 17:09:50 -08003557 except TypeError:
3558 main.log.exception( self.name + ": Object not as expected" )
3559 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003560 except pexpect.EOF:
3561 main.log.error( self.name + ": EOF exception found" )
3562 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003563 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003564 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08003565 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003566 main.cleanAndExit()
Jon Hall63604932015-02-26 17:09:50 -08003567
acsmarsa4a4d1e2015-07-10 16:01:24 -07003568 def leaderCandidates( self, jsonFormat=True ):
3569 """
3570 Returns the output of the leaders -c command.
3571 Optional argument:
3572 * jsonFormat - boolean indicating if you want output in json
3573 """
3574 try:
3575 cmdStr = "onos:leaders -c"
3576 if jsonFormat:
3577 cmdStr += " -j"
3578 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003579 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003580 assert "Command not found:" not in output, output
acsmarsa4a4d1e2015-07-10 16:01:24 -07003581 return output
Jon Hallc6793552016-01-19 14:18:37 -08003582 except AssertionError:
3583 main.log.exception( "" )
3584 return None
acsmarsa4a4d1e2015-07-10 16:01:24 -07003585 except TypeError:
3586 main.log.exception( self.name + ": Object not as expected" )
3587 return None
3588 except pexpect.EOF:
3589 main.log.error( self.name + ": EOF exception found" )
3590 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003591 main.cleanAndExit()
acsmarsa4a4d1e2015-07-10 16:01:24 -07003592 except Exception:
3593 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003594 main.cleanAndExit()
acsmarsa4a4d1e2015-07-10 16:01:24 -07003595
Jon Hallc6793552016-01-19 14:18:37 -08003596 def specificLeaderCandidate( self, topic ):
acsmarsa4a4d1e2015-07-10 16:01:24 -07003597 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00003598 Returns a list in format [leader,candidate1,candidate2,...] for a given
acsmarsa4a4d1e2015-07-10 16:01:24 -07003599 topic parameter and an empty list if the topic doesn't exist
3600 If no leader is elected leader in the returned list will be "none"
3601 Returns None if there is a type error processing the json object
3602 """
3603 try:
Jon Hall6e709752016-02-01 13:38:46 -08003604 cmdStr = "onos:leaders -j"
Jon Hallc6793552016-01-19 14:18:37 -08003605 rawOutput = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003606 assert rawOutput is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003607 assert "Command not found:" not in rawOutput, rawOutput
3608 output = json.loads( rawOutput )
acsmarsa4a4d1e2015-07-10 16:01:24 -07003609 results = []
3610 for dict in output:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003611 if dict[ "topic" ] == topic:
3612 leader = dict[ "leader" ]
3613 candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] )
Jon Hallc6793552016-01-19 14:18:37 -08003614 results.append( leader )
3615 results.extend( candidates )
acsmarsa4a4d1e2015-07-10 16:01:24 -07003616 return results
Jon Hallc6793552016-01-19 14:18:37 -08003617 except AssertionError:
3618 main.log.exception( "" )
3619 return None
3620 except ( TypeError, ValueError ):
3621 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) )
acsmarsa4a4d1e2015-07-10 16:01:24 -07003622 return None
3623 except pexpect.EOF:
3624 main.log.error( self.name + ": EOF exception found" )
3625 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003626 main.cleanAndExit()
acsmarsa4a4d1e2015-07-10 16:01:24 -07003627 except Exception:
3628 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003629 main.cleanAndExit()
acsmarsa4a4d1e2015-07-10 16:01:24 -07003630
Jon Hall61282e32015-03-19 11:34:11 -07003631 def pendingMap( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08003632 """
3633 Returns the output of the intent Pending map.
3634 """
Jon Hall63604932015-02-26 17:09:50 -08003635 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07003636 cmdStr = "onos:intents -p"
Jon Hall61282e32015-03-19 11:34:11 -07003637 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07003638 cmdStr += " -j"
3639 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003640 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003641 assert "Command not found:" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -07003642 return output
Jon Hallc6793552016-01-19 14:18:37 -08003643 except AssertionError:
3644 main.log.exception( "" )
3645 return None
Jon Hall63604932015-02-26 17:09:50 -08003646 except TypeError:
3647 main.log.exception( self.name + ": Object not as expected" )
3648 return None
3649 except pexpect.EOF:
3650 main.log.error( self.name + ": EOF exception found" )
3651 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003652 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003653 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08003654 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003655 main.cleanAndExit()
Jon Hall63604932015-02-26 17:09:50 -08003656
Jon Hall2c8959e2016-12-16 12:17:34 -08003657 def partitions( self, candidates=False, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08003658 """
3659 Returns the output of the raft partitions command for ONOS.
3660 """
Jon Hall61282e32015-03-19 11:34:11 -07003661 # Sample JSON
3662 # {
3663 # "leader": "tcp://10.128.30.11:7238",
3664 # "members": [
3665 # "tcp://10.128.30.11:7238",
3666 # "tcp://10.128.30.17:7238",
3667 # "tcp://10.128.30.13:7238",
3668 # ],
3669 # "name": "p1",
3670 # "term": 3
3671 # },
Jon Hall63604932015-02-26 17:09:50 -08003672 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07003673 cmdStr = "onos:partitions"
Jon Hall2c8959e2016-12-16 12:17:34 -08003674 if candidates:
3675 cmdStr += " -c"
Jon Hall61282e32015-03-19 11:34:11 -07003676 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07003677 cmdStr += " -j"
3678 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003679 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003680 assert "Command not found:" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -07003681 return output
Jon Hallc6793552016-01-19 14:18:37 -08003682 except AssertionError:
3683 main.log.exception( "" )
3684 return None
Jon Hall63604932015-02-26 17:09:50 -08003685 except TypeError:
3686 main.log.exception( self.name + ": Object not as expected" )
3687 return None
3688 except pexpect.EOF:
3689 main.log.error( self.name + ": EOF exception found" )
3690 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003691 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003692 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08003693 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003694 main.cleanAndExit()
Jon Hall63604932015-02-26 17:09:50 -08003695
Jon Halle9f909e2016-09-23 10:43:12 -07003696 def apps( self, summary=False, active=False, jsonFormat=True ):
Jon Hallbe379602015-03-24 13:39:32 -07003697 """
3698 Returns the output of the apps command for ONOS. This command lists
3699 information about installed ONOS applications
3700 """
3701 # Sample JSON object
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00003702 # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0",
Jon Hallbe379602015-03-24 13:39:32 -07003703 # "description":"ONOS OpenFlow protocol southbound providers",
3704 # "origin":"ON.Lab","permissions":"[]","featuresRepo":"",
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00003705 # "features":"[onos-openflow]","state":"ACTIVE"}]
Jon Hallbe379602015-03-24 13:39:32 -07003706 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07003707 cmdStr = "onos:apps"
Jon Halle9f909e2016-09-23 10:43:12 -07003708 if summary:
3709 cmdStr += " -s"
3710 if active:
3711 cmdStr += " -a"
Jon Hallbe379602015-03-24 13:39:32 -07003712 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07003713 cmdStr += " -j"
3714 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003715 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003716 assert "Command not found:" not in output, output
3717 assert "Error executing command" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -07003718 return output
Jon Hallbe379602015-03-24 13:39:32 -07003719 # FIXME: look at specific exceptions/Errors
3720 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07003721 main.log.exception( self.name + ": Error in processing onos:app command." )
Jon Hallbe379602015-03-24 13:39:32 -07003722 return None
3723 except TypeError:
3724 main.log.exception( self.name + ": Object not as expected" )
3725 return None
3726 except pexpect.EOF:
3727 main.log.error( self.name + ": EOF exception found" )
3728 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003729 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003730 except Exception:
Jon Hallbe379602015-03-24 13:39:32 -07003731 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003732 main.cleanAndExit()
Jon Hallbe379602015-03-24 13:39:32 -07003733
You Wangcdc51fe2018-08-12 17:14:56 -07003734 def appStatus( self, appName ):
Jon Hall146f1522015-03-24 15:33:24 -07003735 """
3736 Uses the onos:apps cli command to return the status of an application.
3737 Returns:
3738 "ACTIVE" - If app is installed and activated
3739 "INSTALLED" - If app is installed and deactivated
3740 "UNINSTALLED" - If app is not installed
3741 None - on error
3742 """
Jon Hall146f1522015-03-24 15:33:24 -07003743 try:
3744 if not isinstance( appName, types.StringType ):
3745 main.log.error( self.name + ".appStatus(): appName must be" +
3746 " a string" )
3747 return None
3748 output = self.apps( jsonFormat=True )
3749 appsJson = json.loads( output )
3750 state = None
3751 for app in appsJson:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003752 if appName == app.get( 'name' ):
3753 state = app.get( 'state' )
Jon Hall146f1522015-03-24 15:33:24 -07003754 break
3755 if state == "ACTIVE" or state == "INSTALLED":
3756 return state
3757 elif state is None:
You Wang0d9f2c02018-08-10 14:56:32 -07003758 main.log.warn( "{} app not found".format( appName ) )
Jon Hall146f1522015-03-24 15:33:24 -07003759 return "UNINSTALLED"
3760 elif state:
3761 main.log.error( "Unexpected state from 'onos:apps': " +
3762 str( state ) )
3763 return state
Jon Hallc6793552016-01-19 14:18:37 -08003764 except ( TypeError, ValueError ):
3765 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) )
Jon Hall146f1522015-03-24 15:33:24 -07003766 return None
3767 except pexpect.EOF:
3768 main.log.error( self.name + ": EOF exception found" )
3769 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003770 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003771 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003772 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003773 main.cleanAndExit()
Jon Hall146f1522015-03-24 15:33:24 -07003774
Jon Hallbe379602015-03-24 13:39:32 -07003775 def app( self, appName, option ):
3776 """
3777 Interacts with the app command for ONOS. This command manages
3778 application inventory.
3779 """
Jon Hallbe379602015-03-24 13:39:32 -07003780 try:
Jon Hallbd16b922015-03-26 17:53:15 -07003781 # Validate argument types
3782 valid = True
3783 if not isinstance( appName, types.StringType ):
3784 main.log.error( self.name + ".app(): appName must be a " +
3785 "string" )
3786 valid = False
3787 if not isinstance( option, types.StringType ):
3788 main.log.error( self.name + ".app(): option must be a string" )
3789 valid = False
3790 if not valid:
3791 return main.FALSE
3792 # Validate Option
3793 option = option.lower()
3794 # NOTE: Install may become a valid option
3795 if option == "activate":
3796 pass
3797 elif option == "deactivate":
3798 pass
3799 elif option == "uninstall":
3800 pass
3801 else:
3802 # Invalid option
3803 main.log.error( "The ONOS app command argument only takes " +
3804 "the values: (activate|deactivate|uninstall)" +
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003805 "; was given '" + option + "'" )
Jon Hallbd16b922015-03-26 17:53:15 -07003806 return main.FALSE
Jon Hall146f1522015-03-24 15:33:24 -07003807 cmdStr = "onos:app " + option + " " + appName
Jon Hallbe379602015-03-24 13:39:32 -07003808 output = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003809 assert output is not None, "Error in sendline"
3810 assert "Command not found:" not in output, output
Jon Hallbe379602015-03-24 13:39:32 -07003811 if "Error executing command" in output:
Jon Hall0e240372018-05-02 11:21:57 -07003812 main.log.error( self.name + ": Error in processing onos:app command: " +
Jon Hallbe379602015-03-24 13:39:32 -07003813 str( output ) )
Jon Hall146f1522015-03-24 15:33:24 -07003814 return main.FALSE
Jon Hallbe379602015-03-24 13:39:32 -07003815 elif "No such application" in output:
3816 main.log.error( "The application '" + appName +
3817 "' is not installed in ONOS" )
Jon Hall146f1522015-03-24 15:33:24 -07003818 return main.FALSE
3819 elif "Command not found:" in output:
Jon Hall0e240372018-05-02 11:21:57 -07003820 main.log.error( self.name + ": Error in processing onos:app command: " +
Jon Hall146f1522015-03-24 15:33:24 -07003821 str( output ) )
3822 return main.FALSE
Jon Hallbd16b922015-03-26 17:53:15 -07003823 elif "Unsupported command:" in output:
3824 main.log.error( "Incorrect command given to 'app': " +
3825 str( output ) )
Jon Hallbe379602015-03-24 13:39:32 -07003826 # NOTE: we may need to add more checks here
Jon Hallbd16b922015-03-26 17:53:15 -07003827 # else: Command was successful
Jon Hall08f61bc2015-04-13 16:00:30 -07003828 # main.log.debug( "app response: " + repr( output ) )
Jon Hallbe379602015-03-24 13:39:32 -07003829 return main.TRUE
You Wangb5a55f72017-03-03 12:51:05 -08003830 except AssertionError:
3831 main.log.exception( self.name + ": AssertionError exception found" )
3832 return main.ERROR
Jon Hallbe379602015-03-24 13:39:32 -07003833 except TypeError:
3834 main.log.exception( self.name + ": Object not as expected" )
3835 return main.ERROR
3836 except pexpect.EOF:
3837 main.log.error( self.name + ": EOF exception found" )
3838 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003839 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003840 except Exception:
Jon Hallbe379602015-03-24 13:39:32 -07003841 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003842 main.cleanAndExit()
Jon Hall146f1522015-03-24 15:33:24 -07003843
Jon Hallbd16b922015-03-26 17:53:15 -07003844 def activateApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07003845 """
3846 Activate an app that is already installed in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07003847 appName is the hierarchical app name, not the feature name
3848 If check is True, method will check the status of the app after the
3849 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07003850 Returns main.TRUE if the command was successfully sent
3851 main.FALSE if the cli responded with an error or given
3852 incorrect input
3853 """
3854 try:
3855 if not isinstance( appName, types.StringType ):
3856 main.log.error( self.name + ".activateApp(): appName must be" +
3857 " a string" )
3858 return main.FALSE
3859 status = self.appStatus( appName )
3860 if status == "INSTALLED":
3861 response = self.app( appName, "activate" )
Jon Hallbd16b922015-03-26 17:53:15 -07003862 if check and response == main.TRUE:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003863 for i in range( 10 ): # try 10 times then give up
Jon Hallbd16b922015-03-26 17:53:15 -07003864 status = self.appStatus( appName )
3865 if status == "ACTIVE":
3866 return main.TRUE
3867 else:
Jon Hall050e1bd2015-03-30 13:33:02 -07003868 main.log.debug( "The state of application " +
3869 appName + " is " + status )
Jon Hallbd16b922015-03-26 17:53:15 -07003870 time.sleep( 1 )
3871 return main.FALSE
3872 else: # not 'check' or command didn't succeed
3873 return response
Jon Hall146f1522015-03-24 15:33:24 -07003874 elif status == "ACTIVE":
3875 return main.TRUE
3876 elif status == "UNINSTALLED":
3877 main.log.error( self.name + ": Tried to activate the " +
3878 "application '" + appName + "' which is not " +
3879 "installed." )
3880 else:
3881 main.log.error( "Unexpected return value from appStatus: " +
3882 str( status ) )
3883 return main.ERROR
3884 except TypeError:
3885 main.log.exception( self.name + ": Object not as expected" )
3886 return main.ERROR
3887 except pexpect.EOF:
3888 main.log.error( self.name + ": EOF exception found" )
3889 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003890 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003891 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003892 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003893 main.cleanAndExit()
Jon Hall146f1522015-03-24 15:33:24 -07003894
Jon Hallbd16b922015-03-26 17:53:15 -07003895 def deactivateApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07003896 """
3897 Deactivate an app that is already activated in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07003898 appName is the hierarchical app name, not the feature name
3899 If check is True, method will check the status of the app after the
3900 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07003901 Returns main.TRUE if the command was successfully sent
3902 main.FALSE if the cli responded with an error or given
3903 incorrect input
3904 """
3905 try:
3906 if not isinstance( appName, types.StringType ):
3907 main.log.error( self.name + ".deactivateApp(): appName must " +
3908 "be a string" )
3909 return main.FALSE
3910 status = self.appStatus( appName )
3911 if status == "INSTALLED":
3912 return main.TRUE
3913 elif status == "ACTIVE":
3914 response = self.app( appName, "deactivate" )
Jon Hallbd16b922015-03-26 17:53:15 -07003915 if check and response == main.TRUE:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003916 for i in range( 10 ): # try 10 times then give up
Jon Hallbd16b922015-03-26 17:53:15 -07003917 status = self.appStatus( appName )
3918 if status == "INSTALLED":
3919 return main.TRUE
3920 else:
3921 time.sleep( 1 )
3922 return main.FALSE
3923 else: # not check or command didn't succeed
3924 return response
Jon Hall146f1522015-03-24 15:33:24 -07003925 elif status == "UNINSTALLED":
3926 main.log.warn( self.name + ": Tried to deactivate the " +
3927 "application '" + appName + "' which is not " +
3928 "installed." )
3929 return main.TRUE
3930 else:
3931 main.log.error( "Unexpected return value from appStatus: " +
3932 str( status ) )
3933 return main.ERROR
3934 except TypeError:
3935 main.log.exception( self.name + ": Object not as expected" )
3936 return main.ERROR
3937 except pexpect.EOF:
3938 main.log.error( self.name + ": EOF exception found" )
3939 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003940 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003941 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003942 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003943 main.cleanAndExit()
Jon Hall146f1522015-03-24 15:33:24 -07003944
Jon Hallbd16b922015-03-26 17:53:15 -07003945 def uninstallApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07003946 """
3947 Uninstall an app that is already installed in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07003948 appName is the hierarchical app name, not the feature name
3949 If check is True, method will check the status of the app after the
3950 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07003951 Returns main.TRUE if the command was successfully sent
3952 main.FALSE if the cli responded with an error or given
3953 incorrect input
3954 """
3955 # TODO: check with Thomas about the state machine for apps
3956 try:
3957 if not isinstance( appName, types.StringType ):
3958 main.log.error( self.name + ".uninstallApp(): appName must " +
3959 "be a string" )
3960 return main.FALSE
3961 status = self.appStatus( appName )
3962 if status == "INSTALLED":
3963 response = self.app( appName, "uninstall" )
Jon Hallbd16b922015-03-26 17:53:15 -07003964 if check and response == main.TRUE:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003965 for i in range( 10 ): # try 10 times then give up
Jon Hallbd16b922015-03-26 17:53:15 -07003966 status = self.appStatus( appName )
3967 if status == "UNINSTALLED":
3968 return main.TRUE
3969 else:
3970 time.sleep( 1 )
3971 return main.FALSE
3972 else: # not check or command didn't succeed
3973 return response
Jon Hall146f1522015-03-24 15:33:24 -07003974 elif status == "ACTIVE":
3975 main.log.warn( self.name + ": Tried to uninstall the " +
3976 "application '" + appName + "' which is " +
3977 "currently active." )
3978 response = self.app( appName, "uninstall" )
Jon Hallbd16b922015-03-26 17:53:15 -07003979 if check and response == main.TRUE:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003980 for i in range( 10 ): # try 10 times then give up
Jon Hallbd16b922015-03-26 17:53:15 -07003981 status = self.appStatus( appName )
3982 if status == "UNINSTALLED":
3983 return main.TRUE
3984 else:
3985 time.sleep( 1 )
3986 return main.FALSE
3987 else: # not check or command didn't succeed
3988 return response
Jon Hall146f1522015-03-24 15:33:24 -07003989 elif status == "UNINSTALLED":
3990 return main.TRUE
3991 else:
3992 main.log.error( "Unexpected return value from appStatus: " +
3993 str( status ) )
3994 return main.ERROR
3995 except TypeError:
3996 main.log.exception( self.name + ": Object not as expected" )
3997 return main.ERROR
3998 except pexpect.EOF:
3999 main.log.error( self.name + ": EOF exception found" )
4000 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004001 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07004002 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07004003 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004004 main.cleanAndExit()
Jon Hallbd16b922015-03-26 17:53:15 -07004005
4006 def appIDs( self, jsonFormat=True ):
4007 """
4008 Show the mappings between app id and app names given by the 'app-ids'
4009 cli command
4010 """
4011 try:
4012 cmdStr = "app-ids"
4013 if jsonFormat:
4014 cmdStr += " -j"
Jon Hallc6358dd2015-04-10 12:44:28 -07004015 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07004016 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004017 assert "Command not found:" not in output, output
4018 assert "Error executing command" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -07004019 return output
Jon Hallbd16b922015-03-26 17:53:15 -07004020 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004021 main.log.exception( self.name + ": Error in processing onos:app-ids command." )
Jon Hallbd16b922015-03-26 17:53:15 -07004022 return None
4023 except TypeError:
4024 main.log.exception( self.name + ": Object not as expected" )
4025 return None
4026 except pexpect.EOF:
4027 main.log.error( self.name + ": EOF exception found" )
4028 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004029 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07004030 except Exception:
Jon Hallbd16b922015-03-26 17:53:15 -07004031 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004032 main.cleanAndExit()
Jon Hallbd16b922015-03-26 17:53:15 -07004033
4034 def appToIDCheck( self ):
4035 """
4036 This method will check that each application's ID listed in 'apps' is
4037 the same as the ID listed in 'app-ids'. The check will also check that
4038 there are no duplicate IDs issued. Note that an app ID should be
4039 a globaly unique numerical identifier for app/app-like features. Once
4040 an ID is registered, the ID is never freed up so that if an app is
4041 reinstalled it will have the same ID.
4042
4043 Returns: main.TRUE if the check passes and
4044 main.FALSE if the check fails or
4045 main.ERROR if there is some error in processing the test
4046 """
4047 try:
Jon Hall0e240372018-05-02 11:21:57 -07004048 # Grab IDs
Jon Hallc6793552016-01-19 14:18:37 -08004049 rawJson = self.appIDs( jsonFormat=True )
4050 if rawJson:
4051 ids = json.loads( rawJson )
Jon Hall390696c2015-05-05 17:13:41 -07004052 else:
Jon Hall0e240372018-05-02 11:21:57 -07004053 main.log.error( "app-ids returned nothing: " + repr( rawJson ) )
4054 return main.FALSE
4055
4056 # Grab Apps
Jon Hallc6793552016-01-19 14:18:37 -08004057 rawJson = self.apps( jsonFormat=True )
4058 if rawJson:
4059 apps = json.loads( rawJson )
Jon Hall390696c2015-05-05 17:13:41 -07004060 else:
Jon Hallc6793552016-01-19 14:18:37 -08004061 main.log.error( "apps returned nothing:" + repr( rawJson ) )
Jon Hall390696c2015-05-05 17:13:41 -07004062 return main.FALSE
Jon Hall0e240372018-05-02 11:21:57 -07004063
Jon Hallbd16b922015-03-26 17:53:15 -07004064 result = main.TRUE
4065 for app in apps:
4066 appID = app.get( 'id' )
4067 if appID is None:
4068 main.log.error( "Error parsing app: " + str( app ) )
4069 result = main.FALSE
4070 appName = app.get( 'name' )
4071 if appName is None:
4072 main.log.error( "Error parsing app: " + str( app ) )
4073 result = main.FALSE
4074 # get the entry in ids that has the same appID
Jon Hall390696c2015-05-05 17:13:41 -07004075 current = filter( lambda item: item[ 'id' ] == appID, ids )
Jon Hallbd16b922015-03-26 17:53:15 -07004076 if not current: # if ids doesn't have this id
4077 result = main.FALSE
4078 main.log.error( "'app-ids' does not have the ID for " +
4079 str( appName ) + " that apps does." )
Jon Hallb9d381e2018-02-05 12:02:10 -08004080 main.log.debug( "apps command returned: " + str( app ) +
4081 "; app-ids has: " + str( ids ) )
Jon Hallbd16b922015-03-26 17:53:15 -07004082 elif len( current ) > 1:
4083 # there is more than one app with this ID
4084 result = main.FALSE
4085 # We will log this later in the method
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004086 elif not current[ 0 ][ 'name' ] == appName:
4087 currentName = current[ 0 ][ 'name' ]
Jon Hallbd16b922015-03-26 17:53:15 -07004088 result = main.FALSE
4089 main.log.error( "'app-ids' has " + str( currentName ) +
4090 " registered under id:" + str( appID ) +
4091 " but 'apps' has " + str( appName ) )
4092 else:
4093 pass # id and name match!
Jon Hall0e240372018-05-02 11:21:57 -07004094
Jon Hallbd16b922015-03-26 17:53:15 -07004095 # now make sure that app-ids has no duplicates
4096 idsList = []
4097 namesList = []
4098 for item in ids:
4099 idsList.append( item[ 'id' ] )
4100 namesList.append( item[ 'name' ] )
4101 if len( idsList ) != len( set( idsList ) ) or\
4102 len( namesList ) != len( set( namesList ) ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004103 main.log.error( "'app-ids' has some duplicate entries: \n"
4104 + json.dumps( ids,
4105 sort_keys=True,
4106 indent=4,
4107 separators=( ',', ': ' ) ) )
4108 result = main.FALSE
Jon Hallbd16b922015-03-26 17:53:15 -07004109 return result
Jon Hallc6793552016-01-19 14:18:37 -08004110 except ( TypeError, ValueError ):
4111 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) )
Jon Hallbd16b922015-03-26 17:53:15 -07004112 return main.ERROR
4113 except pexpect.EOF:
4114 main.log.error( self.name + ": EOF exception found" )
4115 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004116 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07004117 except Exception:
Jon Hallbd16b922015-03-26 17:53:15 -07004118 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004119 main.cleanAndExit()
Jon Hallbd16b922015-03-26 17:53:15 -07004120
Jon Hallfb760a02015-04-13 15:35:03 -07004121 def getCfg( self, component=None, propName=None, short=False,
4122 jsonFormat=True ):
4123 """
4124 Get configuration settings from onos cli
4125 Optional arguments:
4126 component - Optionally only list configurations for a specific
4127 component. If None, all components with configurations
4128 are displayed. Case Sensitive string.
4129 propName - If component is specified, propName option will show
4130 only this specific configuration from that component.
4131 Case Sensitive string.
4132 jsonFormat - Returns output as json. Note that this will override
4133 the short option
4134 short - Short, less verbose, version of configurations.
4135 This is overridden by the json option
4136 returns:
4137 Output from cli as a string or None on error
4138 """
4139 try:
4140 baseStr = "cfg"
4141 cmdStr = " get"
4142 componentStr = ""
4143 if component:
4144 componentStr += " " + component
4145 if propName:
4146 componentStr += " " + propName
4147 if jsonFormat:
4148 baseStr += " -j"
4149 elif short:
4150 baseStr += " -s"
4151 output = self.sendline( baseStr + cmdStr + componentStr )
Jon Halla495f562016-05-16 18:03:26 -07004152 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004153 assert "Command not found:" not in output, output
4154 assert "Error executing command" not in output, output
Jon Hallfb760a02015-04-13 15:35:03 -07004155 return output
4156 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004157 main.log.exception( self.name + ": Error in processing 'cfg get' command." )
Jon Hallfb760a02015-04-13 15:35:03 -07004158 return None
4159 except TypeError:
4160 main.log.exception( self.name + ": Object not as expected" )
4161 return None
4162 except pexpect.EOF:
4163 main.log.error( self.name + ": EOF exception found" )
4164 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004165 main.cleanAndExit()
Jon Hallfb760a02015-04-13 15:35:03 -07004166 except Exception:
4167 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004168 main.cleanAndExit()
Jon Hallfb760a02015-04-13 15:35:03 -07004169
4170 def setCfg( self, component, propName, value=None, check=True ):
4171 """
4172 Set/Unset configuration settings from ONOS cli
Jon Hall390696c2015-05-05 17:13:41 -07004173 Required arguments:
Jon Hallfb760a02015-04-13 15:35:03 -07004174 component - The case sensitive name of the component whose
4175 property is to be set
4176 propName - The case sensitive name of the property to be set/unset
Jon Hall390696c2015-05-05 17:13:41 -07004177 Optional arguments:
Jon Hallfb760a02015-04-13 15:35:03 -07004178 value - The value to set the property to. If None, will unset the
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004179 property and revert it to it's default value(if applicable)
Jon Hallfb760a02015-04-13 15:35:03 -07004180 check - Boolean, Check whether the option was successfully set this
4181 only applies when a value is given.
4182 returns:
4183 main.TRUE on success or main.FALSE on failure. If check is False,
4184 will return main.TRUE unless there is an error
4185 """
4186 try:
4187 baseStr = "cfg"
4188 cmdStr = " set " + str( component ) + " " + str( propName )
4189 if value is not None:
4190 cmdStr += " " + str( value )
4191 output = self.sendline( baseStr + cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07004192 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004193 assert "Command not found:" not in output, output
4194 assert "Error executing command" not in output, output
Jon Hallfb760a02015-04-13 15:35:03 -07004195 if value and check:
4196 results = self.getCfg( component=str( component ),
4197 propName=str( propName ),
4198 jsonFormat=True )
4199 # Check if current value is what we just set
4200 try:
4201 jsonOutput = json.loads( results )
4202 current = jsonOutput[ 'value' ]
Jon Hallc6793552016-01-19 14:18:37 -08004203 except ( TypeError, ValueError ):
Jon Hallfb760a02015-04-13 15:35:03 -07004204 main.log.exception( "Error parsing cfg output" )
4205 main.log.error( "output:" + repr( results ) )
4206 return main.FALSE
4207 if current == str( value ):
4208 return main.TRUE
4209 return main.FALSE
4210 return main.TRUE
4211 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004212 main.log.exception( self.name + ": Error in processing 'cfg set' command." )
Jon Hallfb760a02015-04-13 15:35:03 -07004213 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08004214 except ( TypeError, ValueError ):
4215 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) )
Jon Hallfb760a02015-04-13 15:35:03 -07004216 return main.FALSE
4217 except pexpect.EOF:
4218 main.log.error( self.name + ": EOF exception found" )
4219 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004220 main.cleanAndExit()
Jon Hallfb760a02015-04-13 15:35:03 -07004221 except Exception:
4222 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004223 main.cleanAndExit()
Jon Hallfb760a02015-04-13 15:35:03 -07004224
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004225 def distPrimitivesSend( self, cmd ):
4226 """
4227 Function to handle sending cli commands for the distributed primitives test app
4228
4229 This command will catch some exceptions and retry the command on some
4230 specific store exceptions.
4231
4232 Required arguments:
4233 cmd - The command to send to the cli
4234 returns:
4235 string containing the cli output
4236 None on Error
4237 """
4238 try:
4239 output = self.sendline( cmd )
4240 try:
4241 assert output is not None, "Error in sendline"
4242 # TODO: Maybe make this less hardcoded
4243 # ConsistentMap Exceptions
4244 assert "org.onosproject.store.service" not in output
4245 # Node not leader
4246 assert "java.lang.IllegalStateException" not in output
4247 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004248 main.log.error( self.name + ": Error in processing '" + cmd + "' " +
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004249 "command: " + str( output ) )
4250 retryTime = 30 # Conservative time, given by Madan
4251 main.log.info( "Waiting " + str( retryTime ) +
4252 "seconds before retrying." )
4253 time.sleep( retryTime ) # Due to change in mastership
4254 output = self.sendline( cmd )
4255 assert output is not None, "Error in sendline"
4256 assert "Command not found:" not in output, output
4257 assert "Error executing command" not in output, output
4258 main.log.info( self.name + ": " + output )
4259 return output
4260 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004261 main.log.exception( self.name + ": Error in processing '" + cmd + "' command." )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004262 return None
4263 except TypeError:
4264 main.log.exception( self.name + ": Object not as expected" )
4265 return None
4266 except pexpect.EOF:
4267 main.log.error( self.name + ": EOF exception found" )
4268 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004269 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004270 except Exception:
4271 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004272 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004273
Jon Hall390696c2015-05-05 17:13:41 -07004274 def setTestAdd( self, setName, values ):
4275 """
4276 CLI command to add elements to a distributed set.
4277 Arguments:
4278 setName - The name of the set to add to.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004279 values - The value(s) to add to the set, space seperated.
Jon Hall390696c2015-05-05 17:13:41 -07004280 Example usages:
4281 setTestAdd( "set1", "a b c" )
4282 setTestAdd( "set2", "1" )
4283 returns:
4284 main.TRUE on success OR
4285 main.FALSE if elements were already in the set OR
4286 main.ERROR on error
4287 """
4288 try:
4289 cmdStr = "set-test-add " + str( setName ) + " " + str( values )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004290 output = self.distPrimitivesSend( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07004291 positiveMatch = "\[(.*)\] was added to the set " + str( setName )
4292 negativeMatch = "\[(.*)\] was already in set " + str( setName )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004293 if re.search( positiveMatch, output ):
Jon Hall390696c2015-05-05 17:13:41 -07004294 return main.TRUE
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004295 elif re.search( negativeMatch, output ):
Jon Hall390696c2015-05-05 17:13:41 -07004296 return main.FALSE
4297 else:
4298 main.log.error( self.name + ": setTestAdd did not" +
4299 " match expected output" )
Jon Hall390696c2015-05-05 17:13:41 -07004300 main.log.debug( self.name + " actual: " + repr( output ) )
4301 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004302 except TypeError:
4303 main.log.exception( self.name + ": Object not as expected" )
4304 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004305 except Exception:
4306 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004307 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004308
4309 def setTestRemove( self, setName, values, clear=False, retain=False ):
4310 """
4311 CLI command to remove elements from a distributed set.
4312 Required arguments:
4313 setName - The name of the set to remove from.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004314 values - The value(s) to remove from the set, space seperated.
Jon Hall390696c2015-05-05 17:13:41 -07004315 Optional arguments:
4316 clear - Clear all elements from the set
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004317 retain - Retain only the given values. (intersection of the
4318 original set and the given set)
Jon Hall390696c2015-05-05 17:13:41 -07004319 returns:
4320 main.TRUE on success OR
4321 main.FALSE if the set was not changed OR
4322 main.ERROR on error
4323 """
4324 try:
4325 cmdStr = "set-test-remove "
4326 if clear:
4327 cmdStr += "-c " + str( setName )
4328 elif retain:
4329 cmdStr += "-r " + str( setName ) + " " + str( values )
4330 else:
4331 cmdStr += str( setName ) + " " + str( values )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004332 output = self.distPrimitivesSend( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07004333 if clear:
4334 pattern = "Set " + str( setName ) + " cleared"
4335 if re.search( pattern, output ):
4336 return main.TRUE
4337 elif retain:
4338 positivePattern = str( setName ) + " was pruned to contain " +\
4339 "only elements of set \[(.*)\]"
4340 negativePattern = str( setName ) + " was not changed by " +\
4341 "retaining only elements of the set " +\
4342 "\[(.*)\]"
4343 if re.search( positivePattern, output ):
4344 return main.TRUE
4345 elif re.search( negativePattern, output ):
4346 return main.FALSE
4347 else:
4348 positivePattern = "\[(.*)\] was removed from the set " +\
4349 str( setName )
4350 if ( len( values.split() ) == 1 ):
4351 negativePattern = "\[(.*)\] was not in set " +\
4352 str( setName )
4353 else:
4354 negativePattern = "No element of \[(.*)\] was in set " +\
4355 str( setName )
4356 if re.search( positivePattern, output ):
4357 return main.TRUE
4358 elif re.search( negativePattern, output ):
4359 return main.FALSE
4360 main.log.error( self.name + ": setTestRemove did not" +
4361 " match expected output" )
4362 main.log.debug( self.name + " expected: " + pattern )
4363 main.log.debug( self.name + " actual: " + repr( output ) )
4364 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004365 except TypeError:
4366 main.log.exception( self.name + ": Object not as expected" )
4367 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004368 except Exception:
4369 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004370 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004371
4372 def setTestGet( self, setName, values="" ):
4373 """
4374 CLI command to get the elements in a distributed set.
4375 Required arguments:
4376 setName - The name of the set to remove from.
4377 Optional arguments:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004378 values - The value(s) to check if in the set, space seperated.
Jon Hall390696c2015-05-05 17:13:41 -07004379 returns:
4380 main.ERROR on error OR
4381 A list of elements in the set if no optional arguments are
4382 supplied OR
4383 A tuple containing the list then:
4384 main.FALSE if the given values are not in the set OR
4385 main.TRUE if the given values are in the set OR
4386 """
4387 try:
4388 values = str( values ).strip()
4389 setName = str( setName ).strip()
4390 length = len( values.split() )
4391 containsCheck = None
4392 # Patterns to match
4393 setPattern = "\[(.*)\]"
Jon Hall67253832016-12-05 09:47:13 -08004394 pattern = "Items in set " + setName + ":\r\n" + setPattern
Jon Hall390696c2015-05-05 17:13:41 -07004395 containsTrue = "Set " + setName + " contains the value " + values
4396 containsFalse = "Set " + setName + " did not contain the value " +\
4397 values
4398 containsAllTrue = "Set " + setName + " contains the the subset " +\
4399 setPattern
4400 containsAllFalse = "Set " + setName + " did not contain the the" +\
4401 " subset " + setPattern
4402
4403 cmdStr = "set-test-get "
4404 cmdStr += setName + " " + values
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004405 output = self.distPrimitivesSend( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07004406 if length == 0:
4407 match = re.search( pattern, output )
4408 else: # if given values
4409 if length == 1: # Contains output
Jon Hall54b994f2016-12-05 10:48:59 -08004410 patternTrue = pattern + "\r\n" + containsTrue
4411 patternFalse = pattern + "\r\n" + containsFalse
Jon Hall390696c2015-05-05 17:13:41 -07004412 else: # ContainsAll output
Jon Hall54b994f2016-12-05 10:48:59 -08004413 patternTrue = pattern + "\r\n" + containsAllTrue
4414 patternFalse = pattern + "\r\n" + containsAllFalse
Jon Hall390696c2015-05-05 17:13:41 -07004415 matchTrue = re.search( patternTrue, output )
4416 matchFalse = re.search( patternFalse, output )
4417 if matchTrue:
4418 containsCheck = main.TRUE
4419 match = matchTrue
4420 elif matchFalse:
4421 containsCheck = main.FALSE
4422 match = matchFalse
4423 else:
Jon Halle0f0b342017-04-18 11:43:47 -07004424 main.log.error( self.name + " setTestGet did not match " +
Jon Hall390696c2015-05-05 17:13:41 -07004425 "expected output" )
4426 main.log.debug( self.name + " expected: " + pattern )
4427 main.log.debug( self.name + " actual: " + repr( output ) )
4428 match = None
4429 if match:
4430 setMatch = match.group( 1 )
4431 if setMatch == '':
4432 setList = []
4433 else:
4434 setList = setMatch.split( ", " )
4435 if length > 0:
4436 return ( setList, containsCheck )
4437 else:
4438 return setList
4439 else: # no match
4440 main.log.error( self.name + ": setTestGet did not" +
4441 " match expected output" )
4442 main.log.debug( self.name + " expected: " + pattern )
4443 main.log.debug( self.name + " actual: " + repr( output ) )
4444 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004445 except TypeError:
4446 main.log.exception( self.name + ": Object not as expected" )
4447 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004448 except Exception:
4449 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004450 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004451
4452 def setTestSize( self, setName ):
4453 """
4454 CLI command to get the elements in a distributed set.
4455 Required arguments:
4456 setName - The name of the set to remove from.
4457 returns:
Jon Hallfeff3082015-05-19 10:23:26 -07004458 The integer value of the size returned or
Jon Hall390696c2015-05-05 17:13:41 -07004459 None on error
4460 """
4461 try:
4462 # TODO: Should this check against the number of elements returned
4463 # and then return true/false based on that?
4464 setName = str( setName ).strip()
4465 # Patterns to match
4466 setPattern = "\[(.*)\]"
Jon Hall67253832016-12-05 09:47:13 -08004467 pattern = "There are (\d+) items in set " + setName + ":\r\n" +\
Jon Hall390696c2015-05-05 17:13:41 -07004468 setPattern
4469 cmdStr = "set-test-get -s "
4470 cmdStr += setName
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004471 output = self.distPrimitivesSend( cmdStr )
Jon Hall0e240372018-05-02 11:21:57 -07004472 if output:
4473 match = re.search( pattern, output )
4474 if match:
4475 setSize = int( match.group( 1 ) )
4476 setMatch = match.group( 2 )
4477 if len( setMatch.split() ) == setSize:
4478 main.log.info( "The size returned by " + self.name +
4479 " matches the number of elements in " +
4480 "the returned set" )
4481 else:
4482 main.log.error( "The size returned by " + self.name +
4483 " does not match the number of " +
4484 "elements in the returned set." )
4485 return setSize
Jon Hall390696c2015-05-05 17:13:41 -07004486 else: # no match
4487 main.log.error( self.name + ": setTestGet did not" +
4488 " match expected output" )
4489 main.log.debug( self.name + " expected: " + pattern )
4490 main.log.debug( self.name + " actual: " + repr( output ) )
4491 return None
Jon Hall390696c2015-05-05 17:13:41 -07004492 except TypeError:
4493 main.log.exception( self.name + ": Object not as expected" )
4494 return None
Jon Hall390696c2015-05-05 17:13:41 -07004495 except Exception:
4496 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004497 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004498
Jon Hall80daded2015-05-27 16:07:00 -07004499 def counters( self, jsonFormat=True ):
Jon Hall390696c2015-05-05 17:13:41 -07004500 """
4501 Command to list the various counters in the system.
4502 returns:
Jon Hall80daded2015-05-27 16:07:00 -07004503 if jsonFormat, a string of the json object returned by the cli
4504 command
4505 if not jsonFormat, the normal string output of the cli command
Jon Hall390696c2015-05-05 17:13:41 -07004506 None on error
4507 """
Jon Hall390696c2015-05-05 17:13:41 -07004508 try:
Jon Hall390696c2015-05-05 17:13:41 -07004509 cmdStr = "counters"
Jon Hall80daded2015-05-27 16:07:00 -07004510 if jsonFormat:
4511 cmdStr += " -j"
Jon Hall390696c2015-05-05 17:13:41 -07004512 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07004513 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004514 assert "Command not found:" not in output, output
4515 assert "Error executing command" not in output, output
Jon Hall390696c2015-05-05 17:13:41 -07004516 main.log.info( self.name + ": " + output )
Jon Hall80daded2015-05-27 16:07:00 -07004517 return output
Jon Hall390696c2015-05-05 17:13:41 -07004518 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004519 main.log.exception( self.name + ": Error in processing 'counters' command." )
Jon Hall80daded2015-05-27 16:07:00 -07004520 return None
Jon Hall390696c2015-05-05 17:13:41 -07004521 except TypeError:
4522 main.log.exception( self.name + ": Object not as expected" )
Jon Hall80daded2015-05-27 16:07:00 -07004523 return None
Jon Hall390696c2015-05-05 17:13:41 -07004524 except pexpect.EOF:
4525 main.log.error( self.name + ": EOF exception found" )
4526 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004527 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004528 except Exception:
4529 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004530 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004531
Jon Hall935db192016-04-19 00:22:04 -07004532 def counterTestAddAndGet( self, counter, delta=1 ):
Jon Hall390696c2015-05-05 17:13:41 -07004533 """
Jon Halle1a3b752015-07-22 13:02:46 -07004534 CLI command to add a delta to then get a distributed counter.
Jon Hall390696c2015-05-05 17:13:41 -07004535 Required arguments:
4536 counter - The name of the counter to increment.
4537 Optional arguments:
Jon Halle1a3b752015-07-22 13:02:46 -07004538 delta - The long to add to the counter
Jon Hall390696c2015-05-05 17:13:41 -07004539 returns:
4540 integer value of the counter or
4541 None on Error
4542 """
4543 try:
4544 counter = str( counter )
Jon Halle1a3b752015-07-22 13:02:46 -07004545 delta = int( delta )
Jon Hall390696c2015-05-05 17:13:41 -07004546 cmdStr = "counter-test-increment "
Jon Hall390696c2015-05-05 17:13:41 -07004547 cmdStr += counter
Jon Halle1a3b752015-07-22 13:02:46 -07004548 if delta != 1:
4549 cmdStr += " " + str( delta )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004550 output = self.distPrimitivesSend( cmdStr )
Jon Halle1a3b752015-07-22 13:02:46 -07004551 pattern = counter + " was updated to (-?\d+)"
Jon Hall390696c2015-05-05 17:13:41 -07004552 match = re.search( pattern, output )
4553 if match:
4554 return int( match.group( 1 ) )
4555 else:
Jon Halle1a3b752015-07-22 13:02:46 -07004556 main.log.error( self.name + ": counterTestAddAndGet did not" +
Jon Hall390696c2015-05-05 17:13:41 -07004557 " match expected output." )
4558 main.log.debug( self.name + " expected: " + pattern )
4559 main.log.debug( self.name + " actual: " + repr( output ) )
4560 return None
Jon Hall390696c2015-05-05 17:13:41 -07004561 except TypeError:
4562 main.log.exception( self.name + ": Object not as expected" )
4563 return None
Jon Hall390696c2015-05-05 17:13:41 -07004564 except Exception:
4565 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004566 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004567
Jon Hall935db192016-04-19 00:22:04 -07004568 def counterTestGetAndAdd( self, counter, delta=1 ):
Jon Halle1a3b752015-07-22 13:02:46 -07004569 """
4570 CLI command to get a distributed counter then add a delta to it.
4571 Required arguments:
4572 counter - The name of the counter to increment.
4573 Optional arguments:
4574 delta - The long to add to the counter
Jon Halle1a3b752015-07-22 13:02:46 -07004575 returns:
4576 integer value of the counter or
4577 None on Error
4578 """
4579 try:
4580 counter = str( counter )
4581 delta = int( delta )
4582 cmdStr = "counter-test-increment -g "
Jon Halle1a3b752015-07-22 13:02:46 -07004583 cmdStr += counter
4584 if delta != 1:
4585 cmdStr += " " + str( delta )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004586 output = self.distPrimitivesSend( cmdStr )
Jon Halle1a3b752015-07-22 13:02:46 -07004587 pattern = counter + " was updated to (-?\d+)"
4588 match = re.search( pattern, output )
4589 if match:
4590 return int( match.group( 1 ) )
4591 else:
4592 main.log.error( self.name + ": counterTestGetAndAdd did not" +
4593 " match expected output." )
4594 main.log.debug( self.name + " expected: " + pattern )
4595 main.log.debug( self.name + " actual: " + repr( output ) )
4596 return None
Jon Halle1a3b752015-07-22 13:02:46 -07004597 except TypeError:
4598 main.log.exception( self.name + ": Object not as expected" )
4599 return None
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004600 except Exception:
4601 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004602 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004603
4604 def valueTestGet( self, valueName ):
4605 """
4606 CLI command to get the value of an atomic value.
4607 Required arguments:
4608 valueName - The name of the value to get.
4609 returns:
4610 string value of the value or
4611 None on Error
4612 """
4613 try:
4614 valueName = str( valueName )
4615 cmdStr = "value-test "
4616 operation = "get"
4617 cmdStr = "value-test {} {}".format( valueName,
4618 operation )
4619 output = self.distPrimitivesSend( cmdStr )
4620 pattern = "(\w+)"
4621 match = re.search( pattern, output )
4622 if match:
4623 return match.group( 1 )
4624 else:
4625 main.log.error( self.name + ": valueTestGet did not" +
4626 " match expected output." )
4627 main.log.debug( self.name + " expected: " + pattern )
4628 main.log.debug( self.name + " actual: " + repr( output ) )
4629 return None
4630 except TypeError:
4631 main.log.exception( self.name + ": Object not as expected" )
4632 return None
4633 except Exception:
4634 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004635 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004636
4637 def valueTestSet( self, valueName, newValue ):
4638 """
4639 CLI command to set the value of an atomic value.
4640 Required arguments:
4641 valueName - The name of the value to set.
4642 newValue - The value to assign to the given value.
4643 returns:
4644 main.TRUE on success or
4645 main.ERROR on Error
4646 """
4647 try:
4648 valueName = str( valueName )
4649 newValue = str( newValue )
4650 operation = "set"
4651 cmdStr = "value-test {} {} {}".format( valueName,
4652 operation,
4653 newValue )
4654 output = self.distPrimitivesSend( cmdStr )
4655 if output is not None:
4656 return main.TRUE
4657 else:
4658 return main.ERROR
4659 except TypeError:
4660 main.log.exception( self.name + ": Object not as expected" )
4661 return main.ERROR
4662 except Exception:
4663 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004664 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004665
4666 def valueTestCompareAndSet( self, valueName, oldValue, newValue ):
4667 """
4668 CLI command to compareAndSet the value of an atomic value.
4669 Required arguments:
4670 valueName - The name of the value.
4671 oldValue - Compare the current value of the atomic value to this
4672 newValue - If the value equals oldValue, set the value to newValue
4673 returns:
4674 main.TRUE on success or
4675 main.FALSE on failure or
4676 main.ERROR on Error
4677 """
4678 try:
4679 valueName = str( valueName )
4680 oldValue = str( oldValue )
4681 newValue = str( newValue )
4682 operation = "compareAndSet"
4683 cmdStr = "value-test {} {} {} {}".format( valueName,
4684 operation,
4685 oldValue,
4686 newValue )
4687 output = self.distPrimitivesSend( cmdStr )
4688 pattern = "(\w+)"
4689 match = re.search( pattern, output )
4690 if match:
4691 result = match.group( 1 )
4692 if result == "true":
4693 return main.TRUE
4694 elif result == "false":
4695 return main.FALSE
4696 else:
4697 main.log.error( self.name + ": valueTestCompareAndSet did not" +
4698 " match expected output." )
4699 main.log.debug( self.name + " expected: " + pattern )
4700 main.log.debug( self.name + " actual: " + repr( output ) )
4701 return main.ERROR
4702 except TypeError:
4703 main.log.exception( self.name + ": Object not as expected" )
4704 return main.ERROR
4705 except Exception:
4706 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004707 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004708
4709 def valueTestGetAndSet( self, valueName, newValue ):
4710 """
4711 CLI command to getAndSet the value of an atomic value.
4712 Required arguments:
4713 valueName - The name of the value to get.
4714 newValue - The value to assign to the given value
4715 returns:
4716 string value of the value or
4717 None on Error
4718 """
4719 try:
4720 valueName = str( valueName )
4721 cmdStr = "value-test "
4722 operation = "getAndSet"
4723 cmdStr += valueName + " " + operation
4724 cmdStr = "value-test {} {} {}".format( valueName,
4725 operation,
4726 newValue )
4727 output = self.distPrimitivesSend( cmdStr )
4728 pattern = "(\w+)"
4729 match = re.search( pattern, output )
4730 if match:
4731 return match.group( 1 )
4732 else:
4733 main.log.error( self.name + ": valueTestGetAndSet did not" +
4734 " match expected output." )
4735 main.log.debug( self.name + " expected: " + pattern )
4736 main.log.debug( self.name + " actual: " + repr( output ) )
4737 return None
4738 except TypeError:
4739 main.log.exception( self.name + ": Object not as expected" )
4740 return None
4741 except Exception:
4742 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004743 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004744
4745 def valueTestDestroy( self, valueName ):
4746 """
4747 CLI command to destroy an atomic value.
4748 Required arguments:
4749 valueName - The name of the value to destroy.
4750 returns:
4751 main.TRUE on success or
4752 main.ERROR on Error
4753 """
4754 try:
4755 valueName = str( valueName )
4756 cmdStr = "value-test "
4757 operation = "destroy"
4758 cmdStr += valueName + " " + operation
4759 output = self.distPrimitivesSend( cmdStr )
4760 if output is not None:
4761 return main.TRUE
4762 else:
4763 return main.ERROR
4764 except TypeError:
4765 main.log.exception( self.name + ": Object not as expected" )
4766 return main.ERROR
Jon Halle1a3b752015-07-22 13:02:46 -07004767 except Exception:
4768 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004769 main.cleanAndExit()
Jon Halle1a3b752015-07-22 13:02:46 -07004770
YPZhangfebf7302016-05-24 16:45:56 -07004771 def summary( self, jsonFormat=True, timeout=30 ):
kelvin-onlaba297c4d2015-06-01 13:53:55 -07004772 """
4773 Description: Execute summary command in onos
4774 Returns: json object ( summary -j ), returns main.FALSE if there is
4775 no output
4776
4777 """
4778 try:
4779 cmdStr = "summary"
4780 if jsonFormat:
4781 cmdStr += " -j"
YPZhangfebf7302016-05-24 16:45:56 -07004782 handle = self.sendline( cmdStr, timeout=timeout )
Jon Halla495f562016-05-16 18:03:26 -07004783 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004784 assert "Command not found:" not in handle, handle
Jon Hall6e709752016-02-01 13:38:46 -08004785 assert "Error:" not in handle, handle
Devin Lima7cfdbd2017-09-29 15:02:22 -07004786 assert "Error executing" not in handle, handle
kelvin-onlaba297c4d2015-06-01 13:53:55 -07004787 if not handle:
4788 main.log.error( self.name + ": There is no output in " +
4789 "summary command" )
4790 return main.FALSE
4791 return handle
Jon Hallc6793552016-01-19 14:18:37 -08004792 except AssertionError:
Jon Hall6e709752016-02-01 13:38:46 -08004793 main.log.exception( "{} Error in summary output:".format( self.name ) )
Jon Hallc6793552016-01-19 14:18:37 -08004794 return None
kelvin-onlaba297c4d2015-06-01 13:53:55 -07004795 except TypeError:
4796 main.log.exception( self.name + ": Object not as expected" )
4797 return None
4798 except pexpect.EOF:
4799 main.log.error( self.name + ": EOF exception found" )
4800 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004801 main.cleanAndExit()
kelvin-onlaba297c4d2015-06-01 13:53:55 -07004802 except Exception:
4803 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004804 main.cleanAndExit()
Jon Hall2a5002c2015-08-21 16:49:11 -07004805
Jon Hall935db192016-04-19 00:22:04 -07004806 def transactionalMapGet( self, keyName ):
Jon Hall2a5002c2015-08-21 16:49:11 -07004807 """
4808 CLI command to get the value of a key in a consistent map using
4809 transactions. This a test function and can only get keys from the
4810 test map hard coded into the cli command
4811 Required arguments:
4812 keyName - The name of the key to get
Jon Hall2a5002c2015-08-21 16:49:11 -07004813 returns:
4814 The string value of the key or
4815 None on Error
4816 """
4817 try:
4818 keyName = str( keyName )
4819 cmdStr = "transactional-map-test-get "
Jon Hall2a5002c2015-08-21 16:49:11 -07004820 cmdStr += keyName
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004821 output = self.distPrimitivesSend( cmdStr )
Jon Hall2a5002c2015-08-21 16:49:11 -07004822 pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found."
4823 if "Key " + keyName + " not found." in output:
Jon Hall9bfadd22016-05-11 14:48:07 -07004824 main.log.warn( output )
Jon Hall2a5002c2015-08-21 16:49:11 -07004825 return None
4826 else:
4827 match = re.search( pattern, output )
4828 if match:
4829 return match.groupdict()[ 'value' ]
4830 else:
4831 main.log.error( self.name + ": transactionlMapGet did not" +
4832 " match expected output." )
4833 main.log.debug( self.name + " expected: " + pattern )
4834 main.log.debug( self.name + " actual: " + repr( output ) )
4835 return None
4836 except TypeError:
4837 main.log.exception( self.name + ": Object not as expected" )
4838 return None
Jon Hall2a5002c2015-08-21 16:49:11 -07004839 except Exception:
4840 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004841 main.cleanAndExit()
Jon Hall2a5002c2015-08-21 16:49:11 -07004842
Jon Hall935db192016-04-19 00:22:04 -07004843 def transactionalMapPut( self, numKeys, value ):
Jon Hall2a5002c2015-08-21 16:49:11 -07004844 """
4845 CLI command to put a value into 'numKeys' number of keys in a
4846 consistent map using transactions. This a test function and can only
4847 put into keys named 'Key#' of the test map hard coded into the cli command
4848 Required arguments:
4849 numKeys - Number of keys to add the value to
4850 value - The string value to put into the keys
Jon Hall2a5002c2015-08-21 16:49:11 -07004851 returns:
4852 A dictionary whose keys are the name of the keys put into the map
4853 and the values of the keys are dictionaries whose key-values are
4854 'value': value put into map and optionaly
4855 'oldValue': Previous value in the key or
4856 None on Error
4857
4858 Example output
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004859 { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'},
4860 'Key2': {'value': 'Testing'} }
Jon Hall2a5002c2015-08-21 16:49:11 -07004861 """
4862 try:
4863 numKeys = str( numKeys )
4864 value = str( value )
4865 cmdStr = "transactional-map-test-put "
Jon Hall2a5002c2015-08-21 16:49:11 -07004866 cmdStr += numKeys + " " + value
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004867 output = self.distPrimitivesSend( cmdStr )
Jon Hall2a5002c2015-08-21 16:49:11 -07004868 newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.'
4869 updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\."
4870 results = {}
4871 for line in output.splitlines():
4872 new = re.search( newPattern, line )
4873 updated = re.search( updatedPattern, line )
4874 if new:
4875 results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] }
4876 elif updated:
4877 results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ],
Jon Hallc6793552016-01-19 14:18:37 -08004878 'oldValue': updated.groupdict()[ 'oldValue' ] }
Jon Hall2a5002c2015-08-21 16:49:11 -07004879 else:
4880 main.log.error( self.name + ": transactionlMapGet did not" +
4881 " match expected output." )
Jon Hallc6793552016-01-19 14:18:37 -08004882 main.log.debug( "{} expected: {!r} or {!r}".format( self.name,
4883 newPattern,
4884 updatedPattern ) )
Jon Hall2a5002c2015-08-21 16:49:11 -07004885 main.log.debug( self.name + " actual: " + repr( output ) )
4886 return results
Jon Hall0e240372018-05-02 11:21:57 -07004887 except ( TypeError, AttributeError ):
Jon Hall2a5002c2015-08-21 16:49:11 -07004888 main.log.exception( self.name + ": Object not as expected" )
4889 return None
Jon Hall2a5002c2015-08-21 16:49:11 -07004890 except Exception:
4891 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004892 main.cleanAndExit()
Jon Hallc6793552016-01-19 14:18:37 -08004893
acsmarsdaea66c2015-09-03 11:44:06 -07004894 def maps( self, jsonFormat=True ):
4895 """
4896 Description: Returns result of onos:maps
4897 Optional:
4898 * jsonFormat: enable json formatting of output
4899 """
4900 try:
4901 cmdStr = "maps"
4902 if jsonFormat:
4903 cmdStr += " -j"
4904 handle = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07004905 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004906 assert "Command not found:" not in handle, handle
acsmarsdaea66c2015-09-03 11:44:06 -07004907 return handle
Jon Hallc6793552016-01-19 14:18:37 -08004908 except AssertionError:
4909 main.log.exception( "" )
4910 return None
acsmarsdaea66c2015-09-03 11:44:06 -07004911 except TypeError:
4912 main.log.exception( self.name + ": Object not as expected" )
4913 return None
4914 except pexpect.EOF:
4915 main.log.error( self.name + ": EOF exception found" )
4916 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004917 main.cleanAndExit()
acsmarsdaea66c2015-09-03 11:44:06 -07004918 except Exception:
4919 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004920 main.cleanAndExit()
GlennRC050596c2015-11-18 17:06:41 -08004921
4922 def getSwController( self, uri, jsonFormat=True ):
4923 """
4924 Descrition: Gets the controller information from the device
4925 """
4926 try:
4927 cmd = "device-controllers "
4928 if jsonFormat:
4929 cmd += "-j "
4930 response = self.sendline( cmd + uri )
Jon Halla495f562016-05-16 18:03:26 -07004931 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004932 assert "Command not found:" not in response, response
GlennRC050596c2015-11-18 17:06:41 -08004933 return response
Jon Hallc6793552016-01-19 14:18:37 -08004934 except AssertionError:
4935 main.log.exception( "" )
4936 return None
GlennRC050596c2015-11-18 17:06:41 -08004937 except TypeError:
4938 main.log.exception( self.name + ": Object not as expected" )
4939 return None
4940 except pexpect.EOF:
4941 main.log.error( self.name + ": EOF exception found" )
4942 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004943 main.cleanAndExit()
GlennRC050596c2015-11-18 17:06:41 -08004944 except Exception:
4945 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004946 main.cleanAndExit()
GlennRC050596c2015-11-18 17:06:41 -08004947
4948 def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ):
4949 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004950 Descrition: sets the controller(s) for the specified device
GlennRC050596c2015-11-18 17:06:41 -08004951
4952 Parameters:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004953 Required: uri - String: The uri of the device(switch).
GlennRC050596c2015-11-18 17:06:41 -08004954 ip - String or List: The ip address of the controller.
4955 This parameter can be formed in a couple of different ways.
4956 VALID:
4957 10.0.0.1 - just the ip address
4958 tcp:10.0.0.1 - the protocol and the ip address
4959 tcp:10.0.0.1:6653 - the protocol and port can be specified,
4960 so that you can add controllers with different
4961 protocols and ports
4962 INVALID:
4963 10.0.0.1:6653 - this is not supported by ONOS
4964
4965 Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given
4966 port - The port number.
4967 jsonFormat - If set ONOS will output in json NOTE: This is currently not supported
4968
4969 Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE
4970 """
4971 try:
4972 cmd = "device-setcontrollers"
4973
4974 if jsonFormat:
4975 cmd += " -j"
4976 cmd += " " + uri
4977 if isinstance( ip, str ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004978 ip = [ ip ]
GlennRC050596c2015-11-18 17:06:41 -08004979 for item in ip:
4980 if ":" in item:
4981 sitem = item.split( ":" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004982 if len( sitem ) == 3:
GlennRC050596c2015-11-18 17:06:41 -08004983 cmd += " " + item
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004984 elif "." in sitem[ 1 ]:
4985 cmd += " {}:{}".format( item, port )
GlennRC050596c2015-11-18 17:06:41 -08004986 else:
4987 main.log.error( "Malformed entry: " + item )
4988 raise TypeError
4989 else:
4990 cmd += " {}:{}:{}".format( proto, item, port )
GlennRC050596c2015-11-18 17:06:41 -08004991 response = self.sendline( cmd )
Jon Halla495f562016-05-16 18:03:26 -07004992 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004993 assert "Command not found:" not in response, response
GlennRC050596c2015-11-18 17:06:41 -08004994 if "Error" in response:
4995 main.log.error( response )
4996 return main.FALSE
GlennRC050596c2015-11-18 17:06:41 -08004997 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08004998 except AssertionError:
4999 main.log.exception( "" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005000 return main.FALSE
GlennRC050596c2015-11-18 17:06:41 -08005001 except TypeError:
5002 main.log.exception( self.name + ": Object not as expected" )
5003 return main.FALSE
5004 except pexpect.EOF:
5005 main.log.error( self.name + ": EOF exception found" )
5006 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005007 main.cleanAndExit()
GlennRC050596c2015-11-18 17:06:41 -08005008 except Exception:
5009 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005010 main.cleanAndExit()
GlennRC20fc6522015-12-23 23:26:57 -08005011
5012 def removeDevice( self, device ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005013 '''
GlennRC20fc6522015-12-23 23:26:57 -08005014 Description:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005015 Remove a device from ONOS by passing the uri of the device(s).
GlennRC20fc6522015-12-23 23:26:57 -08005016 Parameters:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005017 device - (str or list) the id or uri of the device ex. "of:0000000000000001"
GlennRC20fc6522015-12-23 23:26:57 -08005018 Returns:
5019 Returns main.FALSE if an exception is thrown or an error is present
5020 in the response. Otherwise, returns main.TRUE.
5021 NOTE:
5022 If a host cannot be removed, then this function will return main.FALSE
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005023 '''
GlennRC20fc6522015-12-23 23:26:57 -08005024 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005025 if isinstance( device, str ):
You Wang823f5022016-08-18 15:24:41 -07005026 deviceStr = device
5027 device = []
5028 device.append( deviceStr )
GlennRC20fc6522015-12-23 23:26:57 -08005029
5030 for d in device:
5031 time.sleep( 1 )
5032 response = self.sendline( "device-remove {}".format( d ) )
Jon Halla495f562016-05-16 18:03:26 -07005033 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08005034 assert "Command not found:" not in response, response
GlennRC20fc6522015-12-23 23:26:57 -08005035 if "Error" in response:
5036 main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) )
5037 return main.FALSE
GlennRC20fc6522015-12-23 23:26:57 -08005038 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08005039 except AssertionError:
5040 main.log.exception( "" )
5041 return main.FALSE
GlennRC20fc6522015-12-23 23:26:57 -08005042 except TypeError:
5043 main.log.exception( self.name + ": Object not as expected" )
5044 return main.FALSE
5045 except pexpect.EOF:
5046 main.log.error( self.name + ": EOF exception found" )
5047 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005048 main.cleanAndExit()
GlennRC20fc6522015-12-23 23:26:57 -08005049 except Exception:
5050 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005051 main.cleanAndExit()
GlennRC20fc6522015-12-23 23:26:57 -08005052
5053 def removeHost( self, host ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005054 '''
GlennRC20fc6522015-12-23 23:26:57 -08005055 Description:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005056 Remove a host from ONOS by passing the id of the host(s)
GlennRC20fc6522015-12-23 23:26:57 -08005057 Parameters:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005058 hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01"
GlennRC20fc6522015-12-23 23:26:57 -08005059 Returns:
5060 Returns main.FALSE if an exception is thrown or an error is present
5061 in the response. Otherwise, returns main.TRUE.
5062 NOTE:
5063 If a host cannot be removed, then this function will return main.FALSE
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005064 '''
GlennRC20fc6522015-12-23 23:26:57 -08005065 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005066 if isinstance( host, str ):
GlennRC20fc6522015-12-23 23:26:57 -08005067 host = list( host )
5068
5069 for h in host:
5070 time.sleep( 1 )
5071 response = self.sendline( "host-remove {}".format( h ) )
Jon Halla495f562016-05-16 18:03:26 -07005072 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08005073 assert "Command not found:" not in response, response
GlennRC20fc6522015-12-23 23:26:57 -08005074 if "Error" in response:
5075 main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) )
5076 return main.FALSE
GlennRC20fc6522015-12-23 23:26:57 -08005077 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08005078 except AssertionError:
5079 main.log.exception( "" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005080 return main.FALSE
GlennRC20fc6522015-12-23 23:26:57 -08005081 except TypeError:
5082 main.log.exception( self.name + ": Object not as expected" )
5083 return main.FALSE
5084 except pexpect.EOF:
5085 main.log.error( self.name + ": EOF exception found" )
5086 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005087 main.cleanAndExit()
GlennRC20fc6522015-12-23 23:26:57 -08005088 except Exception:
5089 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005090 main.cleanAndExit()
GlennRCed771242016-01-13 17:02:47 -08005091
YPZhangfebf7302016-05-24 16:45:56 -07005092 def link( self, begin, end, state, timeout=30, showResponse=True ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005093 '''
GlennRCed771242016-01-13 17:02:47 -08005094 Description:
5095 Bring link down or up in the null-provider.
5096 params:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005097 begin - (string) One end of a device or switch.
5098 end - (string) the other end of the device or switch
GlennRCed771242016-01-13 17:02:47 -08005099 returns:
5100 main.TRUE if no exceptions were thrown and no Errors are
5101 present in the resoponse. Otherwise, returns main.FALSE
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005102 '''
GlennRCed771242016-01-13 17:02:47 -08005103 try:
Jon Halle0f0b342017-04-18 11:43:47 -07005104 cmd = "null-link null:{} null:{} {}".format( begin, end, state )
YPZhangfebf7302016-05-24 16:45:56 -07005105 response = self.sendline( cmd, showResponse=showResponse, timeout=timeout )
Jon Halla495f562016-05-16 18:03:26 -07005106 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08005107 assert "Command not found:" not in response, response
GlennRCed771242016-01-13 17:02:47 -08005108 if "Error" in response or "Failure" in response:
5109 main.log.error( response )
5110 return main.FALSE
GlennRCed771242016-01-13 17:02:47 -08005111 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08005112 except AssertionError:
5113 main.log.exception( "" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005114 return main.FALSE
GlennRCed771242016-01-13 17:02:47 -08005115 except TypeError:
5116 main.log.exception( self.name + ": Object not as expected" )
5117 return main.FALSE
5118 except pexpect.EOF:
5119 main.log.error( self.name + ": EOF exception found" )
5120 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005121 main.cleanAndExit()
GlennRCed771242016-01-13 17:02:47 -08005122 except Exception:
5123 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005124 main.cleanAndExit()
GlennRCed771242016-01-13 17:02:47 -08005125
Jon Hall2c8959e2016-12-16 12:17:34 -08005126 def portstate( self, dpid, port, state ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005127 '''
Flavio Castro82ee2f62016-06-07 15:04:12 -07005128 Description:
5129 Changes the state of port in an OF switch by means of the
5130 PORTSTATUS OF messages.
5131 params:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005132 dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102'
5133 port - (string) target port in the device. Ex: '2'
5134 state - (string) target state (enable or disable)
Flavio Castro82ee2f62016-06-07 15:04:12 -07005135 returns:
5136 main.TRUE if no exceptions were thrown and no Errors are
5137 present in the resoponse. Otherwise, returns main.FALSE
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005138 '''
Flavio Castro82ee2f62016-06-07 15:04:12 -07005139 try:
Jon Hall2c8959e2016-12-16 12:17:34 -08005140 state = state.lower()
5141 assert state == 'enable' or state == 'disable', "Unknown state"
Jon Halle0f0b342017-04-18 11:43:47 -07005142 cmd = "portstate {} {} {}".format( dpid, port, state )
Flavio Castro82ee2f62016-06-07 15:04:12 -07005143 response = self.sendline( cmd, showResponse=True )
5144 assert response is not None, "Error in sendline"
5145 assert "Command not found:" not in response, response
5146 if "Error" in response or "Failure" in response:
5147 main.log.error( response )
5148 return main.FALSE
5149 return main.TRUE
5150 except AssertionError:
5151 main.log.exception( "" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005152 return main.FALSE
Flavio Castro82ee2f62016-06-07 15:04:12 -07005153 except TypeError:
5154 main.log.exception( self.name + ": Object not as expected" )
5155 return main.FALSE
5156 except pexpect.EOF:
5157 main.log.error( self.name + ": EOF exception found" )
5158 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005159 main.cleanAndExit()
Flavio Castro82ee2f62016-06-07 15:04:12 -07005160 except Exception:
5161 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005162 main.cleanAndExit()
Flavio Castro82ee2f62016-06-07 15:04:12 -07005163
5164 def logSet( self, level="INFO", app="org.onosproject" ):
5165 """
5166 Set the logging level to lvl for a specific app
5167 returns main.TRUE on success
5168 returns main.FALSE if Error occurred
5169 if noExit is True, TestON will not exit, but clean up
5170 Available level: DEBUG, TRACE, INFO, WARN, ERROR
5171 Level defaults to INFO
5172 """
5173 try:
Jon Halle0f0b342017-04-18 11:43:47 -07005174 self.handle.sendline( "log:set %s %s" % ( level, app ) )
Flavio Castro82ee2f62016-06-07 15:04:12 -07005175 self.handle.expect( "onos>" )
5176
5177 response = self.handle.before
5178 if re.search( "Error", response ):
5179 return main.FALSE
5180 return main.TRUE
5181 except pexpect.TIMEOUT:
5182 main.log.exception( self.name + ": TIMEOUT exception found" )
Devin Lim44075962017-08-11 10:56:37 -07005183 main.cleanAndExit()
Flavio Castro82ee2f62016-06-07 15:04:12 -07005184 except pexpect.EOF:
5185 main.log.error( self.name + ": EOF exception found" )
5186 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005187 main.cleanAndExit()
Flavio Castro82ee2f62016-06-07 15:04:12 -07005188 except Exception:
5189 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005190 main.cleanAndExit()
You Wangdb8cd0a2016-05-26 15:19:45 -07005191
5192 def getGraphDict( self, timeout=60, includeHost=False ):
5193 """
5194 Return a dictionary which describes the latest network topology data as a
5195 graph.
5196 An example of the dictionary:
5197 { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... },
5198 vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } }
5199 Each vertex should at least have an 'edges' attribute which describes the
5200 adjacency information. The value of 'edges' attribute is also represented by
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005201 a dictionary, which maps each edge (identified by the neighbor vertex) to a
You Wangdb8cd0a2016-05-26 15:19:45 -07005202 list of attributes.
5203 An example of the edges dictionary:
5204 'edges': { vertex2: { 'port': ..., 'weight': ... },
5205 vertex3: { 'port': ..., 'weight': ... } }
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005206 If includeHost == True, all hosts (and host-switch links) will be included
You Wangdb8cd0a2016-05-26 15:19:45 -07005207 in topology data.
5208 """
5209 graphDict = {}
5210 try:
5211 links = self.links()
5212 links = json.loads( links )
5213 devices = self.devices()
5214 devices = json.loads( devices )
5215 idToDevice = {}
5216 for device in devices:
5217 idToDevice[ device[ 'id' ] ] = device
5218 if includeHost:
5219 hosts = self.hosts()
5220 # FIXME: support 'includeHost' argument
5221 for link in links:
5222 nodeA = link[ 'src' ][ 'device' ]
5223 nodeB = link[ 'dst' ][ 'device' ]
5224 assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ]
Jon Halle0f0b342017-04-18 11:43:47 -07005225 if nodeA not in graphDict.keys():
5226 graphDict[ nodeA ] = { 'edges': {},
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005227 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ],
Jon Halle0f0b342017-04-18 11:43:47 -07005228 'type': idToDevice[ nodeA ][ 'type' ],
5229 'available': idToDevice[ nodeA ][ 'available' ],
5230 'role': idToDevice[ nodeA ][ 'role' ],
5231 'mfr': idToDevice[ nodeA ][ 'mfr' ],
5232 'hw': idToDevice[ nodeA ][ 'hw' ],
5233 'sw': idToDevice[ nodeA ][ 'sw' ],
5234 'serial': idToDevice[ nodeA ][ 'serial' ],
5235 'chassisId': idToDevice[ nodeA ][ 'chassisId' ],
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005236 'annotations': idToDevice[ nodeA ][ 'annotations' ]}
You Wangdb8cd0a2016-05-26 15:19:45 -07005237 else:
5238 # Assert nodeB is not connected to any current links of nodeA
5239 assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys()
Jon Halle0f0b342017-04-18 11:43:47 -07005240 graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ],
5241 'type': link[ 'type' ],
5242 'state': link[ 'state' ] }
You Wangdb8cd0a2016-05-26 15:19:45 -07005243 return graphDict
5244 except ( TypeError, ValueError ):
5245 main.log.exception( self.name + ": Object not as expected" )
5246 return None
5247 except KeyError:
5248 main.log.exception( self.name + ": KeyError exception found" )
5249 return None
5250 except AssertionError:
5251 main.log.exception( self.name + ": AssertionError exception found" )
5252 return None
5253 except pexpect.EOF:
5254 main.log.error( self.name + ": EOF exception found" )
5255 main.log.error( self.name + ": " + self.handle.before )
5256 return None
5257 except Exception:
5258 main.log.exception( self.name + ": Uncaught exception!" )
5259 return None
YPZhangcbc2a062016-07-11 10:55:44 -07005260
5261 def getIntentPerfSummary( self ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005262 '''
YPZhangcbc2a062016-07-11 10:55:44 -07005263 Send command to check intent-perf summary
5264 Returns: dictionary for intent-perf summary
5265 if something wrong, function will return None
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005266 '''
YPZhangcbc2a062016-07-11 10:55:44 -07005267 cmd = "intent-perf -s"
5268 respDic = {}
5269 resp = self.sendline( cmd )
You Wangb5a55f72017-03-03 12:51:05 -08005270 assert resp is not None, "Error in sendline"
5271 assert "Command not found:" not in resp, resp
YPZhangcbc2a062016-07-11 10:55:44 -07005272 try:
5273 # Generate the dictionary to return
5274 for l in resp.split( "\n" ):
5275 # Delete any white space in line
5276 temp = re.sub( r'\s+', '', l )
5277 temp = temp.split( ":" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005278 respDic[ temp[ 0 ] ] = temp[ 1 ]
YPZhangcbc2a062016-07-11 10:55:44 -07005279
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005280 except ( TypeError, ValueError ):
YPZhangcbc2a062016-07-11 10:55:44 -07005281 main.log.exception( self.name + ": Object not as expected" )
5282 return None
5283 except KeyError:
5284 main.log.exception( self.name + ": KeyError exception found" )
5285 return None
5286 except AssertionError:
5287 main.log.exception( self.name + ": AssertionError exception found" )
5288 return None
5289 except pexpect.EOF:
5290 main.log.error( self.name + ": EOF exception found" )
5291 main.log.error( self.name + ": " + self.handle.before )
5292 return None
5293 except Exception:
5294 main.log.exception( self.name + ": Uncaught exception!" )
5295 return None
5296 return respDic
5297
Chiyu Chengec63bde2016-11-17 18:11:36 -08005298 def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ):
chengchiyu08303a02016-09-08 17:40:26 -07005299 """
5300 Searches the latest ONOS log file for the given search term and
5301 return a list that contains all the lines that have the search term.
YPZhangcbc2a062016-07-11 10:55:44 -07005302
chengchiyu08303a02016-09-08 17:40:26 -07005303 Arguments:
Chiyu Chengec63bde2016-11-17 18:11:36 -08005304 searchTerm:
5305 The string to grep from the ONOS log.
5306 startLine:
5307 The term that decides which line is the start to search the searchTerm in
5308 the karaf log. For now, startTerm only works in 'first' mode.
5309 logNum:
5310 In some extreme cases, one karaf log is not big enough to contain all the
5311 information.Because of this, search mutiply logs is necessary to capture
5312 the right result. logNum is the number of karaf logs that we need to search
5313 the searchTerm.
chengchiyu08303a02016-09-08 17:40:26 -07005314 mode:
5315 all: return all the strings that contain the search term
5316 last: return the last string that contains the search term
5317 first: return the first string that contains the search term
Chiyu Chengec63bde2016-11-17 18:11:36 -08005318 num: return the number of times that the searchTerm appears in the log
5319 total: return how many lines in karaf log
chengchiyu08303a02016-09-08 17:40:26 -07005320 """
5321 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005322 assert isinstance( searchTerm, str )
Jon Halle0f0b342017-04-18 11:43:47 -07005323 # Build the log paths string
Chiyu Chengec63bde2016-11-17 18:11:36 -08005324 logPath = '/opt/onos/log/karaf.log.'
5325 logPaths = '/opt/onos/log/karaf.log'
5326 for i in range( 1, logNum ):
5327 logPaths = logPath + str( i ) + " " + logPaths
5328 cmd = "cat " + logPaths
You Wang6d301d42017-04-21 10:49:33 -07005329 if startLine:
Jon Halla478b852017-12-04 15:00:15 -08005330 # 100000000 is just a extreme large number to make sure this function can
5331 # grep all the lines after startLine
You Wang6d301d42017-04-21 10:49:33 -07005332 cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'"
Chiyu Chengec63bde2016-11-17 18:11:36 -08005333 if mode == 'all':
5334 cmd = cmd + " | grep \'" + searchTerm + "\'"
You Wang6d301d42017-04-21 10:49:33 -07005335 elif mode == 'last':
Chiyu Chengec63bde2016-11-17 18:11:36 -08005336 cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1"
You Wang6d301d42017-04-21 10:49:33 -07005337 elif mode == 'first':
5338 cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1"
5339 elif mode == 'num':
Chiyu Chengec63bde2016-11-17 18:11:36 -08005340 cmd = cmd + " | grep -c \'" + searchTerm + "\'"
You Wang118ba582017-01-02 17:14:43 -08005341 num = self.sendline( cmd )
Chiyu Chengb8c2c842016-10-05 12:40:49 -07005342 return num
You Wang6d301d42017-04-21 10:49:33 -07005343 elif mode == 'total':
Chiyu Chengec63bde2016-11-17 18:11:36 -08005344 totalLines = self.sendline( "cat /opt/onos/log/karaf.log | wc -l" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005345 return int( totalLines )
You Wang6d301d42017-04-21 10:49:33 -07005346 else:
5347 main.log.error( self.name + " unsupported mode" )
5348 return main.ERROR
chengchiyu08303a02016-09-08 17:40:26 -07005349 before = self.sendline( cmd )
5350 before = before.splitlines()
5351 # make sure the returned list only contains the search term
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005352 returnLines = [ line for line in before if searchTerm in line ]
chengchiyu08303a02016-09-08 17:40:26 -07005353 return returnLines
5354 except AssertionError:
5355 main.log.error( self.name + " searchTerm is not string type" )
5356 return None
5357 except pexpect.EOF:
5358 main.log.error( self.name + ": EOF exception found" )
5359 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005360 main.cleanAndExit()
chengchiyu08303a02016-09-08 17:40:26 -07005361 except pexpect.TIMEOUT:
5362 main.log.error( self.name + ": TIMEOUT exception found" )
5363 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005364 main.cleanAndExit()
chengchiyu08303a02016-09-08 17:40:26 -07005365 except Exception:
5366 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005367 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005368
5369 def vplsShow( self, jsonFormat=True ):
5370 """
5371 Description: Returns result of onos:vpls show, which should list the
5372 configured VPLS networks and the assigned interfaces.
5373 Optional:
5374 * jsonFormat: enable json formatting of output
5375 Returns:
5376 The output of the command or None on error.
5377 """
5378 try:
5379 cmdStr = "vpls show"
5380 if jsonFormat:
5381 raise NotImplementedError
5382 cmdStr += " -j"
5383 handle = self.sendline( cmdStr )
5384 assert handle is not None, "Error in sendline"
5385 assert "Command not found:" not in handle, handle
5386 return handle
5387 except AssertionError:
5388 main.log.exception( "" )
5389 return None
5390 except TypeError:
5391 main.log.exception( self.name + ": Object not as expected" )
5392 return None
5393 except pexpect.EOF:
5394 main.log.error( self.name + ": EOF exception found" )
5395 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005396 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005397 except NotImplementedError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005398 main.log.exception( self.name + ": Json output not supported" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005399 return None
5400 except Exception:
5401 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005402 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005403
5404 def parseVplsShow( self ):
5405 """
5406 Parse the cli output of 'vpls show' into json output. This is required
5407 as there is currently no json output available.
5408 """
5409 try:
5410 output = []
5411 raw = self.vplsShow( jsonFormat=False )
5412 namePat = "VPLS name: (?P<name>\w+)"
5413 interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]"
5414 encapPat = "Encapsulation: (?P<encap>\w+)"
5415 pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] )
5416 mIter = re.finditer( pattern, raw )
5417 for match in mIter:
5418 item = {}
5419 item[ 'name' ] = match.group( 'name' )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005420 ifaces = match.group( 'interfaces' ).split( ', ' )
Jon Hall2c8959e2016-12-16 12:17:34 -08005421 if ifaces == [ "" ]:
5422 ifaces = []
5423 item[ 'interfaces' ] = ifaces
5424 encap = match.group( 'encap' )
5425 if encap != 'NONE':
5426 item[ 'encapsulation' ] = encap.lower()
5427 output.append( item )
5428 return output
5429 except Exception:
5430 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005431 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005432
5433 def vplsList( self, jsonFormat=True ):
5434 """
5435 Description: Returns result of onos:vpls list, which should list the
5436 configured VPLS networks.
5437 Optional:
5438 * jsonFormat: enable json formatting of output
5439 """
5440 try:
5441 cmdStr = "vpls list"
5442 if jsonFormat:
5443 raise NotImplementedError
5444 cmdStr += " -j"
5445 handle = self.sendline( cmdStr )
5446 assert handle is not None, "Error in sendline"
5447 assert "Command not found:" not in handle, handle
5448 return handle
5449 except AssertionError:
5450 main.log.exception( "" )
5451 return None
5452 except TypeError:
5453 main.log.exception( self.name + ": Object not as expected" )
5454 return None
5455 except pexpect.EOF:
5456 main.log.error( self.name + ": EOF exception found" )
5457 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005458 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005459 except NotImplementedError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005460 main.log.exception( self.name + ": Json output not supported" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005461 return None
5462 except Exception:
5463 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005464 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005465
5466 def vplsCreate( self, network ):
5467 """
5468 CLI command to create a new VPLS network.
5469 Required arguments:
5470 network - String name of the network to create.
5471 returns:
5472 main.TRUE on success and main.FALSE on failure
5473 """
5474 try:
5475 network = str( network )
5476 cmdStr = "vpls create "
5477 cmdStr += network
5478 output = self.sendline( cmdStr )
5479 assert output is not None, "Error in sendline"
5480 assert "Command not found:" not in output, output
5481 assert "Error executing command" not in output, output
5482 assert "VPLS already exists:" not in output, output
5483 return main.TRUE
5484 except AssertionError:
5485 main.log.exception( "" )
5486 return main.FALSE
5487 except TypeError:
5488 main.log.exception( self.name + ": Object not as expected" )
5489 return main.FALSE
5490 except pexpect.EOF:
5491 main.log.error( self.name + ": EOF exception found" )
5492 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005493 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005494 except Exception:
5495 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005496 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005497
5498 def vplsDelete( self, network ):
5499 """
5500 CLI command to delete a VPLS network.
5501 Required arguments:
5502 network - Name of the network to delete.
5503 returns:
5504 main.TRUE on success and main.FALSE on failure
5505 """
5506 try:
5507 network = str( network )
5508 cmdStr = "vpls delete "
5509 cmdStr += network
5510 output = self.sendline( cmdStr )
5511 assert output is not None, "Error in sendline"
5512 assert "Command not found:" not in output, output
5513 assert "Error executing command" not in output, output
5514 assert " not found" not in output, output
Jon Hallcf97cf12017-06-06 09:37:51 -07005515 assert "still updating" not in output, output
Jon Hall2c8959e2016-12-16 12:17:34 -08005516 return main.TRUE
5517 except AssertionError:
5518 main.log.exception( "" )
5519 return main.FALSE
5520 except TypeError:
5521 main.log.exception( self.name + ": Object not as expected" )
5522 return main.FALSE
5523 except pexpect.EOF:
5524 main.log.error( self.name + ": EOF exception found" )
5525 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005526 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005527 except Exception:
5528 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005529 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005530
5531 def vplsAddIface( self, network, iface ):
5532 """
5533 CLI command to add an interface to a VPLS network.
5534 Required arguments:
5535 network - Name of the network to add the interface to.
5536 iface - The ONOS name for an interface.
5537 returns:
5538 main.TRUE on success and main.FALSE on failure
5539 """
5540 try:
5541 network = str( network )
5542 iface = str( iface )
5543 cmdStr = "vpls add-if "
5544 cmdStr += network + " " + iface
5545 output = self.sendline( cmdStr )
5546 assert output is not None, "Error in sendline"
5547 assert "Command not found:" not in output, output
5548 assert "Error executing command" not in output, output
5549 assert "already associated to network" not in output, output
5550 assert "Interface cannot be added." not in output, output
Jon Hallcf97cf12017-06-06 09:37:51 -07005551 assert "still updating" not in output, output
Jon Hall2c8959e2016-12-16 12:17:34 -08005552 return main.TRUE
5553 except AssertionError:
5554 main.log.exception( "" )
5555 return main.FALSE
5556 except TypeError:
5557 main.log.exception( self.name + ": Object not as expected" )
5558 return main.FALSE
5559 except pexpect.EOF:
5560 main.log.error( self.name + ": EOF exception found" )
5561 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005562 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005563 except Exception:
5564 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005565 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005566
5567 def vplsRemIface( self, network, iface ):
5568 """
5569 CLI command to remove an interface from a VPLS network.
5570 Required arguments:
5571 network - Name of the network to remove the interface from.
5572 iface - Name of the interface to remove.
5573 returns:
5574 main.TRUE on success and main.FALSE on failure
5575 """
5576 try:
5577 iface = str( iface )
5578 cmdStr = "vpls rem-if "
5579 cmdStr += network + " " + iface
5580 output = self.sendline( cmdStr )
5581 assert output is not None, "Error in sendline"
5582 assert "Command not found:" not in output, output
5583 assert "Error executing command" not in output, output
5584 assert "is not configured" not in output, output
Jon Hallcf97cf12017-06-06 09:37:51 -07005585 assert "still updating" not in output, output
Jon Hall2c8959e2016-12-16 12:17:34 -08005586 return main.TRUE
5587 except AssertionError:
5588 main.log.exception( "" )
5589 return main.FALSE
5590 except TypeError:
5591 main.log.exception( self.name + ": Object not as expected" )
5592 return main.FALSE
5593 except pexpect.EOF:
5594 main.log.error( self.name + ": EOF exception found" )
5595 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005596 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005597 except Exception:
5598 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005599 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005600
5601 def vplsClean( self ):
5602 """
5603 Description: Clears the VPLS app configuration.
5604 Returns: main.TRUE on success and main.FALSE on failure
5605 """
5606 try:
5607 cmdStr = "vpls clean"
5608 handle = self.sendline( cmdStr )
5609 assert handle is not None, "Error in sendline"
5610 assert "Command not found:" not in handle, handle
Jon Hallcf97cf12017-06-06 09:37:51 -07005611 assert "still updating" not in handle, handle
Jon Hall2c8959e2016-12-16 12:17:34 -08005612 return handle
5613 except AssertionError:
5614 main.log.exception( "" )
5615 return main.FALSE
5616 except TypeError:
5617 main.log.exception( self.name + ": Object not as expected" )
5618 return main.FALSE
5619 except pexpect.EOF:
5620 main.log.error( self.name + ": EOF exception found" )
5621 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005622 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005623 except Exception:
5624 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005625 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005626
5627 def vplsSetEncap( self, network, encapType ):
5628 """
5629 CLI command to add an interface to a VPLS network.
5630 Required arguments:
5631 network - Name of the network to create.
5632 encapType - Type of encapsulation.
5633 returns:
5634 main.TRUE on success and main.FALSE on failure
5635 """
5636 try:
5637 network = str( network )
5638 encapType = str( encapType ).upper()
5639 assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type"
5640 cmdStr = "vpls set-encap "
5641 cmdStr += network + " " + encapType
5642 output = self.sendline( cmdStr )
5643 assert output is not None, "Error in sendline"
5644 assert "Command not found:" not in output, output
5645 assert "Error executing command" not in output, output
5646 assert "already associated to network" not in output, output
5647 assert "Encapsulation type " not in output, output
Jon Hallcf97cf12017-06-06 09:37:51 -07005648 assert "still updating" not in output, output
Jon Hall2c8959e2016-12-16 12:17:34 -08005649 return main.TRUE
5650 except AssertionError:
5651 main.log.exception( "" )
5652 return main.FALSE
5653 except TypeError:
5654 main.log.exception( self.name + ": Object not as expected" )
5655 return main.FALSE
5656 except pexpect.EOF:
5657 main.log.error( self.name + ": EOF exception found" )
5658 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005659 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005660 except Exception:
5661 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005662 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005663
5664 def interfaces( self, jsonFormat=True ):
5665 """
5666 Description: Returns result of interfaces command.
5667 Optional:
5668 * jsonFormat: enable json formatting of output
5669 Returns:
5670 The output of the command or None on error.
5671 """
5672 try:
5673 cmdStr = "interfaces"
5674 if jsonFormat:
Jon Halle0f0b342017-04-18 11:43:47 -07005675 raise NotImplementedError
Jon Hall2c8959e2016-12-16 12:17:34 -08005676 cmdStr += " -j"
5677 handle = self.sendline( cmdStr )
5678 assert handle is not None, "Error in sendline"
5679 assert "Command not found:" not in handle, handle
5680 return handle
5681 except AssertionError:
5682 main.log.exception( "" )
5683 return None
5684 except TypeError:
5685 main.log.exception( self.name + ": Object not as expected" )
5686 return None
5687 except pexpect.EOF:
5688 main.log.error( self.name + ": EOF exception found" )
5689 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005690 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005691 except NotImplementedError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005692 main.log.exception( self.name + ": Json output not supported" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005693 return None
5694 except Exception:
5695 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005696 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08005697
5698 def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005699 '''
Chiyu Chengec63bde2016-11-17 18:11:36 -08005700 Get the timestamp of searchTerm from karaf log.
5701
5702 Arguments:
5703 splitTerm_before and splitTerm_after:
5704
5705 The terms that split the string that contains the timeStamp of
5706 searchTerm. For example, if that string is "xxxxxxxcreationTime =
5707 1419510501xxxxxx", then the splitTerm_before is "CreationTime = "
5708 and the splitTerm_after is "x"
5709
5710 others:
Jon Halle0f0b342017-04-18 11:43:47 -07005711 Please look at the "logsearch" Function in onosclidriver.py
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005712 '''
Chiyu Chengec63bde2016-11-17 18:11:36 -08005713 if logNum < 0:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005714 main.log.error( "Get wrong log number ")
Chiyu Chengec63bde2016-11-17 18:11:36 -08005715 return main.ERROR
5716 lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005717 if len( lines ) == 0:
Chiyu Chengec63bde2016-11-17 18:11:36 -08005718 main.log.warn( "Captured timestamp string is empty" )
5719 return main.ERROR
5720 lines = lines[ 0 ]
5721 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005722 assert isinstance( lines, str )
Chiyu Chengec63bde2016-11-17 18:11:36 -08005723 # get the target value
5724 line = lines.split( splitTerm_before )
5725 key = line[ 1 ].split( splitTerm_after )
5726 return int( key[ 0 ] )
5727 except IndexError:
5728 main.log.warn( "Index Error!" )
5729 return main.ERROR
5730 except AssertionError:
5731 main.log.warn( "Search Term Not Found " )
5732 return main.ERROR
Jon Halle0f0b342017-04-18 11:43:47 -07005733
5734 def workQueueAdd( self, queueName, value ):
5735 """
5736 CLI command to add a string to the specified Work Queue.
5737 This function uses the distributed primitives test app, which
5738 gives some cli access to distributed primitives for testing
5739 purposes only.
5740
5741 Required arguments:
5742 queueName - The name of the queue to add to
5743 value - The value to add to the queue
5744 returns:
5745 main.TRUE on success, main.FALSE on failure and
5746 main.ERROR on error.
5747 """
5748 try:
5749 queueName = str( queueName )
5750 value = str( value )
5751 prefix = "work-queue-test"
5752 operation = "add"
5753 cmdStr = " ".join( [ prefix, queueName, operation, value ] )
5754 output = self.distPrimitivesSend( cmdStr )
5755 if "Invalid operation name" in output:
5756 main.log.warn( output )
5757 return main.ERROR
5758 elif "Done" in output:
5759 return main.TRUE
5760 except TypeError:
5761 main.log.exception( self.name + ": Object not as expected" )
5762 return main.ERROR
5763 except Exception:
5764 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005765 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07005766
5767 def workQueueAddMultiple( self, queueName, value1, value2 ):
5768 """
5769 CLI command to add two strings to the specified Work Queue.
5770 This function uses the distributed primitives test app, which
5771 gives some cli access to distributed primitives for testing
5772 purposes only.
5773
5774 Required arguments:
5775 queueName - The name of the queue to add to
5776 value1 - The first value to add to the queue
5777 value2 - The second value to add to the queue
5778 returns:
5779 main.TRUE on success, main.FALSE on failure and
5780 main.ERROR on error.
5781 """
5782 try:
5783 queueName = str( queueName )
5784 value1 = str( value1 )
5785 value2 = str( value2 )
5786 prefix = "work-queue-test"
5787 operation = "addMultiple"
5788 cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] )
5789 output = self.distPrimitivesSend( cmdStr )
5790 if "Invalid operation name" in output:
5791 main.log.warn( output )
5792 return main.ERROR
5793 elif "Done" in output:
5794 return main.TRUE
5795 except TypeError:
5796 main.log.exception( self.name + ": Object not as expected" )
5797 return main.ERROR
5798 except Exception:
5799 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005800 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07005801
5802 def workQueueTakeAndComplete( self, queueName, number=1 ):
5803 """
5804 CLI command to take a value from the specified Work Queue and compelte it.
5805 This function uses the distributed primitives test app, which
5806 gives some cli access to distributed primitives for testing
5807 purposes only.
5808
5809 Required arguments:
5810 queueName - The name of the queue to add to
5811 number - The number of items to take and complete
5812 returns:
5813 main.TRUE on success, main.FALSE on failure and
5814 main.ERROR on error.
5815 """
5816 try:
5817 queueName = str( queueName )
5818 number = str( int( number ) )
5819 prefix = "work-queue-test"
5820 operation = "takeAndComplete"
5821 cmdStr = " ".join( [ prefix, queueName, operation, number ] )
5822 output = self.distPrimitivesSend( cmdStr )
5823 if "Invalid operation name" in output:
5824 main.log.warn( output )
5825 return main.ERROR
5826 elif "Done" in output:
5827 return main.TRUE
5828 except TypeError:
5829 main.log.exception( self.name + ": Object not as expected" )
5830 return main.ERROR
5831 except Exception:
5832 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005833 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07005834
5835 def workQueueDestroy( self, queueName ):
5836 """
5837 CLI command to destroy the specified Work Queue.
5838 This function uses the distributed primitives test app, which
5839 gives some cli access to distributed primitives for testing
5840 purposes only.
5841
5842 Required arguments:
5843 queueName - The name of the queue to add to
5844 returns:
5845 main.TRUE on success, main.FALSE on failure and
5846 main.ERROR on error.
5847 """
5848 try:
5849 queueName = str( queueName )
5850 prefix = "work-queue-test"
5851 operation = "destroy"
5852 cmdStr = " ".join( [ prefix, queueName, operation ] )
5853 output = self.distPrimitivesSend( cmdStr )
5854 if "Invalid operation name" in output:
5855 main.log.warn( output )
5856 return main.ERROR
5857 return main.TRUE
5858 except TypeError:
5859 main.log.exception( self.name + ": Object not as expected" )
5860 return main.ERROR
5861 except Exception:
5862 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005863 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07005864
5865 def workQueueTotalPending( self, queueName ):
5866 """
5867 CLI command to get the Total Pending items of the specified Work Queue.
5868 This function uses the distributed primitives test app, which
5869 gives some cli access to distributed primitives for testing
5870 purposes only.
5871
5872 Required arguments:
5873 queueName - The name of the queue to add to
5874 returns:
5875 The number of Pending items in the specified work queue or
5876 None on error
5877 """
5878 try:
5879 queueName = str( queueName )
5880 prefix = "work-queue-test"
5881 operation = "totalPending"
5882 cmdStr = " ".join( [ prefix, queueName, operation ] )
5883 output = self.distPrimitivesSend( cmdStr )
5884 pattern = r'\d+'
5885 if "Invalid operation name" in output:
5886 main.log.warn( output )
5887 return None
5888 else:
5889 match = re.search( pattern, output )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005890 return match.group( 0 )
Jon Halle0f0b342017-04-18 11:43:47 -07005891 except ( AttributeError, TypeError ):
5892 main.log.exception( self.name + ": Object not as expected; " + str( output ) )
5893 return None
5894 except Exception:
5895 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005896 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07005897
5898 def workQueueTotalCompleted( self, queueName ):
5899 """
5900 CLI command to get the Total Completed items of the specified Work Queue.
5901 This function uses the distributed primitives test app, which
5902 gives some cli access to distributed primitives for testing
5903 purposes only.
5904
5905 Required arguments:
5906 queueName - The name of the queue to add to
5907 returns:
5908 The number of complete items in the specified work queue or
5909 None on error
5910 """
5911 try:
5912 queueName = str( queueName )
5913 prefix = "work-queue-test"
5914 operation = "totalCompleted"
5915 cmdStr = " ".join( [ prefix, queueName, operation ] )
5916 output = self.distPrimitivesSend( cmdStr )
5917 pattern = r'\d+'
5918 if "Invalid operation name" in output:
5919 main.log.warn( output )
5920 return None
5921 else:
5922 match = re.search( pattern, output )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005923 return match.group( 0 )
Jon Halle0f0b342017-04-18 11:43:47 -07005924 except ( AttributeError, TypeError ):
5925 main.log.exception( self.name + ": Object not as expected; " + str( output ) )
5926 return None
5927 except Exception:
5928 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005929 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07005930
5931 def workQueueTotalInProgress( self, queueName ):
5932 """
5933 CLI command to get the Total In Progress items of the specified Work Queue.
5934 This function uses the distributed primitives test app, which
5935 gives some cli access to distributed primitives for testing
5936 purposes only.
5937
5938 Required arguments:
5939 queueName - The name of the queue to add to
5940 returns:
5941 The number of In Progress items in the specified work queue or
5942 None on error
5943 """
5944 try:
5945 queueName = str( queueName )
5946 prefix = "work-queue-test"
5947 operation = "totalInProgress"
5948 cmdStr = " ".join( [ prefix, queueName, operation ] )
5949 output = self.distPrimitivesSend( cmdStr )
5950 pattern = r'\d+'
5951 if "Invalid operation name" in output:
5952 main.log.warn( output )
5953 return None
5954 else:
5955 match = re.search( pattern, output )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005956 return match.group( 0 )
Jon Halle0f0b342017-04-18 11:43:47 -07005957 except ( AttributeError, TypeError ):
5958 main.log.exception( self.name + ": Object not as expected; " + str( output ) )
5959 return None
5960 except Exception:
5961 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005962 main.cleanAndExit()
Jeremy Ronquillo818bc7c2017-08-09 17:14:53 +00005963
5964 def events( self, args='-a' ):
5965 """
5966 Description: Returns events -a command output
5967 Optional:
5968 add other arguments
5969 """
5970 try:
5971 cmdStr = "events"
5972 if args:
5973 cmdStr += " " + args
5974 handle = self.sendline( cmdStr )
5975 assert handle is not None, "Error in sendline"
5976 assert "Command not found:" not in handle, handle
5977 return handle
5978 except AssertionError:
5979 main.log.exception( "" )
5980 return None
5981 except TypeError:
5982 main.log.exception( self.name + ": Object not as expected" )
5983 return None
5984 except pexpect.EOF:
5985 main.log.error( self.name + ": EOF exception found" )
5986 main.log.error( self.name + ": " + self.handle.before )
5987 main.cleanAndExit()
5988 except Exception:
5989 main.log.exception( self.name + ": Uncaught exception!" )
5990 main.cleanAndExit()
5991
5992 def getMaster( self, deviceID ):
5993 """
5994 Description: Obtains current master using "roles" command for a specific deviceID
5995 """
5996 try:
5997 return str( self.getRole( deviceID )[ 'master' ] )
5998 except AssertionError:
5999 main.log.exception( "" )
6000 return None
6001 except TypeError:
6002 main.log.exception( self.name + ": Object not as expected" )
6003 return None
6004 except pexpect.EOF:
6005 main.log.error( self.name + ": EOF exception found" )
6006 main.log.error( self.name + ": " + self.handle.before )
6007 main.cleanAndExit()
6008 except Exception:
6009 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lime6fe3c42017-10-18 16:28:40 -07006010 main.cleanAndExit()
Jon Halla478b852017-12-04 15:00:15 -08006011
6012 def issu( self ):
6013 """
6014 Short summary of In-Service Software Upgrade status
6015
6016 Returns the output of the cli command or None on Error
6017 """
6018 try:
6019 cmdStr = "issu"
6020 handle = self.sendline( cmdStr )
6021 assert handle is not None, "Error in sendline"
6022 assert "Command not found:" not in handle, handle
6023 assert "Unsupported command:" not in handle, handle
6024 return handle
6025 except AssertionError:
6026 main.log.exception( "" )
6027 return None
6028 except TypeError:
6029 main.log.exception( self.name + ": Object not as expected" )
6030 return None
6031 except pexpect.EOF:
6032 main.log.error( self.name + ": EOF exception found" )
6033 main.log.error( self.name + ": " + self.handle.before )
6034 main.cleanAndExit()
6035 except Exception:
6036 main.log.exception( self.name + ": Uncaught exception!" )
6037 main.cleanAndExit()
6038
6039 def issuInit( self ):
6040 """
6041 Initiates an In-Service Software Upgrade
6042
6043 Returns main.TRUE on success, main.ERROR on error, else main.FALSE
6044 """
6045 try:
6046 cmdStr = "issu init"
6047 handle = self.sendline( cmdStr )
6048 assert handle is not None, "Error in sendline"
6049 assert "Command not found:" not in handle, handle
6050 assert "Unsupported command:" not in handle, handle
6051 if "Initialized" in handle:
6052 return main.TRUE
6053 else:
6054 return main.FALSE
6055 except AssertionError:
6056 main.log.exception( "" )
6057 return main.ERROR
6058 except TypeError:
6059 main.log.exception( self.name + ": Object not as expected" )
6060 return main.ERROR
6061 except pexpect.EOF:
6062 main.log.error( self.name + ": EOF exception found" )
6063 main.log.error( self.name + ": " + self.handle.before )
6064 main.cleanAndExit()
6065 except Exception:
6066 main.log.exception( self.name + ": Uncaught exception!" )
6067 main.cleanAndExit()
6068
6069 def issuUpgrade( self ):
6070 """
6071 Transitions stores to upgraded nodes
6072
6073 Returns main.TRUE on success, main.ERROR on error, else main.FALSE
6074 """
6075 try:
6076 cmdStr = "issu upgrade"
6077 handle = self.sendline( cmdStr )
6078 assert handle is not None, "Error in sendline"
6079 assert "Command not found:" not in handle, handle
6080 assert "Unsupported command:" not in handle, handle
6081 if "Upgraded" in handle:
6082 return main.TRUE
6083 else:
6084 return main.FALSE
6085 except AssertionError:
6086 main.log.exception( "" )
6087 return main.ERROR
6088 except TypeError:
6089 main.log.exception( self.name + ": Object not as expected" )
6090 return main.ERROR
6091 except pexpect.EOF:
6092 main.log.error( self.name + ": EOF exception found" )
6093 main.log.error( self.name + ": " + self.handle.before )
6094 main.cleanAndExit()
6095 except Exception:
6096 main.log.exception( self.name + ": Uncaught exception!" )
6097 main.cleanAndExit()
6098
6099 def issuCommit( self ):
6100 """
6101 Finalizes an In-Service Software Upgrade
6102
6103 Returns main.TRUE on success, main.ERROR on error, else main.FALSE
6104 """
6105 try:
6106 cmdStr = "issu commit"
6107 handle = self.sendline( cmdStr )
6108 assert handle is not None, "Error in sendline"
6109 assert "Command not found:" not in handle, handle
6110 assert "Unsupported command:" not in handle, handle
6111 # TODO: Check the version returned by this command
6112 if "Committed version" in handle:
6113 return main.TRUE
6114 else:
6115 return main.FALSE
6116 except AssertionError:
6117 main.log.exception( "" )
6118 return main.ERROR
6119 except TypeError:
6120 main.log.exception( self.name + ": Object not as expected" )
6121 return main.ERROR
6122 except pexpect.EOF:
6123 main.log.error( self.name + ": EOF exception found" )
6124 main.log.error( self.name + ": " + self.handle.before )
6125 main.cleanAndExit()
6126 except Exception:
6127 main.log.exception( self.name + ": Uncaught exception!" )
6128 main.cleanAndExit()
6129
6130 def issuRollback( self ):
6131 """
6132 Rolls back an In-Service Software Upgrade
6133
6134 Returns main.TRUE on success, main.ERROR on error, else main.FALSE
6135 """
6136 try:
6137 cmdStr = "issu rollback"
6138 handle = self.sendline( cmdStr )
6139 assert handle is not None, "Error in sendline"
6140 assert "Command not found:" not in handle, handle
6141 assert "Unsupported command:" not in handle, handle
6142 # TODO: Check the version returned by this command
6143 if "Rolled back to version" in handle:
6144 return main.TRUE
6145 else:
6146 return main.FALSE
6147 except AssertionError:
6148 main.log.exception( "" )
6149 return main.ERROR
6150 except TypeError:
6151 main.log.exception( self.name + ": Object not as expected" )
6152 return main.ERROR
6153 except pexpect.EOF:
6154 main.log.error( self.name + ": EOF exception found" )
6155 main.log.error( self.name + ": " + self.handle.before )
6156 main.cleanAndExit()
6157 except Exception:
6158 main.log.exception( self.name + ": Uncaught exception!" )
6159 main.cleanAndExit()
6160
6161 def issuReset( self ):
6162 """
6163 Resets the In-Service Software Upgrade status after a rollback
6164
6165 Returns main.TRUE on success, main.ERROR on error, else main.FALSE
6166 """
6167 try:
6168 cmdStr = "issu reset"
6169 handle = self.sendline( cmdStr )
6170 assert handle is not None, "Error in sendline"
6171 assert "Command not found:" not in handle, handle
6172 assert "Unsupported command:" not in handle, handle
6173 # TODO: Check the version returned by this command
6174 if "Reset version" in handle:
6175 return main.TRUE
6176 else:
6177 return main.FALSE
6178 except AssertionError:
6179 main.log.exception( "" )
6180 return main.ERROR
6181 except TypeError:
6182 main.log.exception( self.name + ": Object not as expected" )
6183 return main.ERROR
6184 except pexpect.EOF:
6185 main.log.error( self.name + ": EOF exception found" )
6186 main.log.error( self.name + ": " + self.handle.before )
6187 main.cleanAndExit()
6188 except Exception:
6189 main.log.exception( self.name + ": Uncaught exception!" )
6190 main.cleanAndExit()
6191
6192 def issuStatus( self ):
6193 """
6194 Status of an In-Service Software Upgrade
6195
6196 Returns the output of the cli command or None on Error
6197 """
6198 try:
6199 cmdStr = "issu status"
6200 handle = self.sendline( cmdStr )
6201 assert handle is not None, "Error in sendline"
6202 assert "Command not found:" not in handle, handle
6203 assert "Unsupported command:" not in handle, handle
6204 return handle
6205 except AssertionError:
6206 main.log.exception( "" )
6207 return None
6208 except TypeError:
6209 main.log.exception( self.name + ": Object not as expected" )
6210 return None
6211 except pexpect.EOF:
6212 main.log.error( self.name + ": EOF exception found" )
6213 main.log.error( self.name + ": " + self.handle.before )
6214 main.cleanAndExit()
6215 except Exception:
6216 main.log.exception( self.name + ": Uncaught exception!" )
6217 main.cleanAndExit()
6218
6219 def issuVersion( self ):
6220 """
6221 Get the version of an In-Service Software Upgrade
6222
6223 Returns the output of the cli command or None on Error
6224 """
6225 try:
6226 cmdStr = "issu version"
6227 handle = self.sendline( cmdStr )
6228 assert handle is not None, "Error in sendline"
6229 assert "Command not found:" not in handle, handle
6230 assert "Unsupported command:" not in handle, handle
6231 return handle
6232 except AssertionError:
6233 main.log.exception( "" )
6234 return None
6235 except TypeError:
6236 main.log.exception( self.name + ": Object not as expected" )
6237 return None
6238 except pexpect.EOF:
6239 main.log.error( self.name + ": EOF exception found" )
6240 main.log.error( self.name + ": " + self.handle.before )
6241 main.cleanAndExit()
6242 except Exception:
6243 main.log.exception( self.name + ": Uncaught exception!" )
6244 main.cleanAndExit()
You Wange24d6272018-03-27 21:18:50 -07006245
6246 def mcastJoin( self, sIP, groupIP, sPort, dPorts ):
6247 """
6248 Create a multicast route by calling 'mcast-join' command
6249 sIP: source IP of the multicast route
6250 groupIP: group IP of the multicast route
6251 sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route
6252 dPorts: a list of destination ports of the multicast route
6253 Returns main.TRUE if mcast route is added; Otherwise main.FALSE
6254 """
6255 try:
6256 cmdStr = "mcast-join"
6257 cmdStr += " " + str( sIP )
6258 cmdStr += " " + str( groupIP )
6259 cmdStr += " " + str( sPort )
6260 assert isinstance( dPorts, list )
6261 for dPort in dPorts:
6262 cmdStr += " " + str( dPort )
6263 handle = self.sendline( cmdStr )
6264 assert handle is not None, "Error in sendline"
6265 assert "Command not found:" not in handle, handle
6266 assert "Unsupported command:" not in handle, handle
6267 assert "Error executing command" not in handle, handle
6268 if "Added the mcast route" in handle:
6269 return main.TRUE
6270 else:
6271 return main.FALSE
6272 except AssertionError:
6273 main.log.exception( "" )
6274 return None
6275 except TypeError:
6276 main.log.exception( self.name + ": Object not as expected" )
6277 return None
6278 except pexpect.EOF:
6279 main.log.error( self.name + ": EOF exception found" )
6280 main.log.error( self.name + ": " + self.handle.before )
6281 main.cleanAndExit()
6282 except Exception:
6283 main.log.exception( self.name + ": Uncaught exception!" )
6284 main.cleanAndExit()
6285
6286 def mcastDelete( self, sIP, groupIP, dPorts ):
6287 """
6288 Delete a multicast route by calling 'mcast-delete' command
6289 sIP: source IP of the multicast route
6290 groupIP: group IP of the multicast route
6291 dPorts: a list of destination ports of the multicast route
6292 Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE
6293 """
6294 try:
6295 cmdStr = "mcast-delete"
6296 cmdStr += " " + str( sIP )
6297 cmdStr += " " + str( groupIP )
6298 assert isinstance( dPorts, list )
6299 for dPort in dPorts:
6300 cmdStr += " " + str( dPort )
6301 handle = self.sendline( cmdStr )
6302 assert handle is not None, "Error in sendline"
6303 assert "Command not found:" not in handle, handle
6304 assert "Unsupported command:" not in handle, handle
6305 assert "Error executing command" not in handle, handle
6306 if "Updated the mcast route" in handle:
6307 return main.TRUE
6308 else:
6309 return main.FALSE
6310 except AssertionError:
6311 main.log.exception( "" )
6312 return None
6313 except TypeError:
6314 main.log.exception( self.name + ": Object not as expected" )
6315 return None
6316 except pexpect.EOF:
6317 main.log.error( self.name + ": EOF exception found" )
6318 main.log.error( self.name + ": " + self.handle.before )
6319 main.cleanAndExit()
6320 except Exception:
6321 main.log.exception( self.name + ": Uncaught exception!" )
6322 main.cleanAndExit()
6323
6324 def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ):
6325 """
6326 Create a multicast route by calling 'mcast-host-join' command
6327 sAddr: we can provide * for ASM or a specific address for SSM
6328 gAddr: specifies multicast group address
You Wang547893e2018-05-08 13:34:59 -07006329 srcs: a list of HostId of the sources e.g. ["00:AA:00:00:00:01/None"]
You Wange24d6272018-03-27 21:18:50 -07006330 sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"]
6331 Returns main.TRUE if mcast route is added; Otherwise main.FALSE
6332 """
6333 try:
6334 cmdStr = "mcast-host-join"
6335 cmdStr += " -sAddr " + str( sAddr )
6336 cmdStr += " -gAddr " + str( gAddr )
6337 assert isinstance( srcs, list )
6338 for src in srcs:
6339 cmdStr += " -srcs " + str( src )
6340 assert isinstance( sinks, list )
6341 for sink in sinks:
6342 cmdStr += " -sinks " + str( sink )
6343 handle = self.sendline( cmdStr )
6344 assert handle is not None, "Error in sendline"
6345 assert "Command not found:" not in handle, handle
6346 assert "Unsupported command:" not in handle, handle
6347 assert "Error executing command" not in handle, handle
6348 if "Added the mcast route" in handle:
6349 return main.TRUE
6350 else:
6351 return main.FALSE
6352 except AssertionError:
6353 main.log.exception( "" )
6354 return None
6355 except TypeError:
6356 main.log.exception( self.name + ": Object not as expected" )
6357 return None
6358 except pexpect.EOF:
6359 main.log.error( self.name + ": EOF exception found" )
6360 main.log.error( self.name + ": " + self.handle.before )
6361 main.cleanAndExit()
6362 except Exception:
6363 main.log.exception( self.name + ": Uncaught exception!" )
6364 main.cleanAndExit()
6365
6366 def mcastHostDelete( self, sAddr, gAddr, host=None ):
6367 """
6368 Delete multicast sink(s) by calling 'mcast-host-delete' command
6369 sAddr: we can provide * for ASM or a specific address for SSM
6370 gAddr: specifies multicast group address
You Wangc02d8352018-04-17 16:42:10 -07006371 host: HostId of the sink e.g. "00:AA:00:00:01:05/40",
You Wange24d6272018-03-27 21:18:50 -07006372 will delete the route if not specified
6373 Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE
6374 """
6375 try:
6376 cmdStr = "mcast-host-delete"
6377 cmdStr += " -sAddr " + str( sAddr )
6378 cmdStr += " -gAddr " + str( gAddr )
6379 if host:
6380 cmdStr += " -h " + str( host )
6381 handle = self.sendline( cmdStr )
6382 assert handle is not None, "Error in sendline"
6383 assert "Command not found:" not in handle, handle
6384 assert "Unsupported command:" not in handle, handle
6385 assert "Error executing command" not in handle, handle
6386 if "Updated the mcast route" in handle:
6387 return main.TRUE
6388 elif "Deleted the mcast route" in handle:
6389 return main.TRUE
6390 else:
6391 return main.FALSE
6392 except AssertionError:
6393 main.log.exception( "" )
6394 return None
6395 except TypeError:
6396 main.log.exception( self.name + ": Object not as expected" )
6397 return None
6398 except pexpect.EOF:
6399 main.log.error( self.name + ": EOF exception found" )
6400 main.log.error( self.name + ": " + self.handle.before )
6401 main.cleanAndExit()
6402 except Exception:
6403 main.log.exception( self.name + ": Uncaught exception!" )
6404 main.cleanAndExit()
6405
You Wang547893e2018-05-08 13:34:59 -07006406 def mcastSinkDelete( self, sAddr, gAddr, sink=None ):
6407 """
6408 Delete multicast sink(s) by calling 'mcast-sink-delete' command
6409 sAddr: we can provide * for ASM or a specific address for SSM
6410 gAddr: specifies multicast group address
6411 host: HostId of the sink e.g. "00:AA:00:00:01:05/40",
6412 will delete the route if not specified
6413 Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE
6414 """
6415 try:
6416 cmdStr = "mcast-sink-delete"
6417 cmdStr += " -sAddr " + str( sAddr )
6418 cmdStr += " -gAddr " + str( gAddr )
6419 if sink:
6420 cmdStr += " -s " + str( sink )
6421 handle = self.sendline( cmdStr )
6422 assert handle is not None, "Error in sendline"
6423 assert "Command not found:" not in handle, handle
6424 assert "Unsupported command:" not in handle, handle
6425 assert "Error executing command" not in handle, handle
6426 if "Updated the mcast route" in handle:
6427 return main.TRUE
6428 elif "Deleted the mcast route" in handle:
6429 return main.TRUE
6430 else:
6431 return main.FALSE
6432 except AssertionError:
6433 main.log.exception( "" )
6434 return None
6435 except TypeError:
6436 main.log.exception( self.name + ": Object not as expected" )
6437 return None
6438 except pexpect.EOF:
6439 main.log.error( self.name + ": EOF exception found" )
6440 main.log.error( self.name + ": " + self.handle.before )
6441 main.cleanAndExit()
6442 except Exception:
6443 main.log.exception( self.name + ": Uncaught exception!" )
6444 main.cleanAndExit()
6445
You Wange24d6272018-03-27 21:18:50 -07006446 def mcastSourceDelete( self, sAddr, gAddr, srcs=None ):
6447 """
6448 Delete multicast src(s) by calling 'mcast-source-delete' command
6449 sAddr: we can provide * for ASM or a specific address for SSM
6450 gAddr: specifies multicast group address
You Wang547893e2018-05-08 13:34:59 -07006451 srcs: a list of host IDs of the sources e.g. ["00:AA:00:00:01:05/40"],
You Wange24d6272018-03-27 21:18:50 -07006452 will delete the route if not specified
6453 Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE
6454 """
6455 try:
6456 cmdStr = "mcast-source-delete"
6457 cmdStr += " -sAddr " + str( sAddr )
6458 cmdStr += " -gAddr " + str( gAddr )
6459 if srcs:
6460 assert isinstance( srcs, list )
6461 for src in srcs:
6462 cmdStr += " -src " + str( src )
6463 handle = self.sendline( cmdStr )
6464 assert handle is not None, "Error in sendline"
6465 assert "Command not found:" not in handle, handle
6466 assert "Unsupported command:" not in handle, handle
6467 assert "Error executing command" not in handle, handle
6468 if "Updated the mcast route" in handle:
6469 return main.TRUE
6470 elif "Deleted the mcast route" in handle:
6471 return main.TRUE
6472 else:
6473 return main.FALSE
6474 except AssertionError:
6475 main.log.exception( "" )
6476 return None
6477 except TypeError:
6478 main.log.exception( self.name + ": Object not as expected" )
6479 return None
6480 except pexpect.EOF:
6481 main.log.error( self.name + ": EOF exception found" )
6482 main.log.error( self.name + ": " + self.handle.before )
6483 main.cleanAndExit()
6484 except Exception:
6485 main.log.exception( self.name + ": Uncaught exception!" )
6486 main.cleanAndExit()
You Wang5da39c82018-04-26 22:55:08 -07006487
6488 def netcfg( self, jsonFormat=True, args="" ):
6489 """
6490 Run netcfg cli command with given args
6491 """
6492 try:
6493 cmdStr = "netcfg"
6494 if jsonFormat:
6495 cmdStr = cmdStr + " -j"
6496 if args:
6497 cmdStr = cmdStr + " " + str( args )
6498 handle = self.sendline( cmdStr )
6499 assert handle is not None, "Error in sendline"
6500 assert "Command not found:" not in handle, handle
6501 assert "Unsupported command:" not in handle, handle
6502 assert "Error executing command" not in handle, handle
6503 return handle
6504 except AssertionError:
6505 main.log.exception( "" )
6506 return None
6507 except TypeError:
6508 main.log.exception( self.name + ": Object not as expected" )
6509 return None
6510 except pexpect.EOF:
6511 main.log.error( self.name + ": EOF exception found" )
6512 main.log.error( self.name + ": " + self.handle.before )
6513 main.cleanAndExit()
6514 except Exception:
6515 main.log.exception( self.name + ": Uncaught exception!" )
6516 main.cleanAndExit()
6517
You Wang0fa76e72018-05-18 11:33:25 -07006518 def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ):
You Wang5da39c82018-04-26 22:55:08 -07006519 """
You Wang54b1d672018-06-11 16:44:13 -07006520 Compose and return a list of t3-troubleshoot cli commands for given source and destination addresses
You Wang5da39c82018-04-26 22:55:08 -07006521 Options:
6522 sAddr: IP address of the source host
6523 dAddr: IP address of the destination host
You Wang0fa76e72018-05-18 11:33:25 -07006524 ipv6: True if hosts are IPv6
6525 verbose: return verbose t3 output if True
6526 simple: compose command for t3-troubleshoot-simple if True
You Wang5da39c82018-04-26 22:55:08 -07006527 """
6528 try:
6529 # Collect information of both hosts from onos
6530 hosts = self.hosts()
6531 hosts = json.loads( hosts )
6532 sHost = None
6533 dHost = None
6534 for host in hosts:
6535 if sAddr in host[ "ipAddresses" ]:
6536 sHost = host
6537 elif dAddr in host[ "ipAddresses" ]:
6538 dHost = host
6539 if sHost and dHost:
6540 break
6541 assert sHost, "Not able to find host with IP {}".format( sAddr )
You Wang54b1d672018-06-11 16:44:13 -07006542 cmdList = []
You Wang5d9527b2018-05-29 17:08:54 -07006543 if simple:
6544 assert dHost, "Not able to find host with IP {}".format( dAddr )
You Wang54b1d672018-06-11 16:44:13 -07006545 cmdStr = "t3-troubleshoot-simple"
6546 if verbose:
6547 cmdStr += " -vv"
6548 if ipv6:
6549 cmdStr += " -et ipv6"
You Wang0fa76e72018-05-18 11:33:25 -07006550 cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] )
You Wang54b1d672018-06-11 16:44:13 -07006551 cmdList.append( cmdStr )
You Wang0fa76e72018-05-18 11:33:25 -07006552 else:
You Wang54b1d672018-06-11 16:44:13 -07006553 for location in sHost[ "locations" ]:
6554 cmdStr = "t3-troubleshoot"
6555 if verbose:
6556 cmdStr += " -vv"
6557 if ipv6:
6558 cmdStr += " -et ipv6"
6559 cmdStr += " -s " + str( sAddr )
6560 cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] )
6561 cmdStr += " -sm " + str( sHost[ "mac" ] )
6562 if sHost[ "vlan" ] != "None":
6563 cmdStr += " -vid " + sHost[ "vlan" ]
6564 cmdStr += " -d " + str( dAddr )
6565 netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) )
6566 netcfg = json.loads( netcfg )
6567 assert netcfg, "Failed to get netcfg"
6568 cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] )
6569 cmdList.append( cmdStr )
6570 return cmdList
You Wang5da39c82018-04-26 22:55:08 -07006571 except AssertionError:
6572 main.log.exception( "" )
6573 return None
6574 except ( KeyError, TypeError ):
6575 main.log.exception( self.name + ": Object not as expected" )
6576 return None
6577 except Exception:
6578 main.log.exception( self.name + ": Uncaught exception!" )
6579 main.cleanAndExit()
6580
6581 def t3( self, sAddr, dAddr, ipv6=False ):
6582 """
You Wang54b1d672018-06-11 16:44:13 -07006583 Run t3-troubleshoot cli commands for all posible routes given source and destination addresses
You Wang5da39c82018-04-26 22:55:08 -07006584 Options:
6585 sAddr: IP address of the source host
6586 dAddr: IP address of the destination host
6587 """
6588 try:
You Wang54b1d672018-06-11 16:44:13 -07006589 cmdList = self.composeT3Command( sAddr, dAddr, ipv6 )
6590 assert cmdList is not None, "composeT3Command returned None"
6591 t3Output = ""
6592 for cmdStr in cmdList:
6593 handle = self.sendline( cmdStr )
6594 assert handle is not None, "Error in sendline"
6595 assert "Command not found:" not in handle, handle
6596 assert "Unsupported command:" not in handle, handle
6597 assert "Error executing command" not in handle, handle
6598 assert "Tracing packet" in handle
6599 t3Output += handle
6600 return t3Output
You Wang5da39c82018-04-26 22:55:08 -07006601 except AssertionError:
6602 main.log.exception( "" )
6603 return None
6604 except pexpect.EOF:
6605 main.log.error( self.name + ": EOF exception found" )
6606 main.log.error( self.name + ": " + self.handle.before )
6607 main.cleanAndExit()
6608 except Exception:
6609 main.log.exception( self.name + ": Uncaught exception!" )
6610 main.cleanAndExit()