blob: 6f0fb5d2eb709c43a2ca19e319488208ec8c0cb3 [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
Jon Hall6c9e2da2018-11-06 12:01:23 -080061 self.karafPrompt = "sdn@root >" # FIXME: make configurable
You Wangdb8cd0a2016-05-26 15:19:45 -070062 self.graph = Graph()
Devin Limdc78e202017-06-09 18:30:07 -070063 super( OnosCliDriver, self ).__init__()
kelvin8ec71442015-01-15 16:57:00 -080064
Jeremy Ronquillo82705492017-10-18 14:19:55 -070065 def checkOptions( self, var, defaultVar ):
Devin Limdc78e202017-06-09 18:30:07 -070066 if var is None or var == "":
67 return defaultVar
68 return var
Jeremy Ronquillo82705492017-10-18 14:19:55 -070069
kelvin8ec71442015-01-15 16:57:00 -080070 def connect( self, **connectargs ):
71 """
andrewonlab95ce8322014-10-13 14:12:04 -040072 Creates ssh handle for ONOS cli.
kelvin8ec71442015-01-15 16:57:00 -080073 """
andrewonlab95ce8322014-10-13 14:12:04 -040074 try:
75 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080076 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us658ec012015-03-11 15:13:09 -070077 self.home = "~/onos"
andrewonlab95ce8322014-10-13 14:12:04 -040078 for key in self.options:
79 if key == "home":
Devin Limdc78e202017-06-09 18:30:07 -070080 self.home = self.options[ key ]
81 elif key == "karaf_username":
82 self.karafUser = self.options[ key ]
83 elif key == "karaf_password":
84 self.karafPass = self.options[ key ]
85
Jeremy Ronquillo82705492017-10-18 14:19:55 -070086 self.home = self.checkOptions( self.home, "~/onos" )
87 self.karafUser = self.checkOptions( self.karafUser, self.user_name )
88 self.karafPass = self.checkOptions( self.karafPass, self.pwd )
andrewonlab95ce8322014-10-13 14:12:04 -040089
kelvin-onlaba4074292015-07-09 15:19:49 -070090 for key in self.options:
91 if key == 'onosIp':
92 self.onosIp = self.options[ 'onosIp' ]
93 break
94
kelvin8ec71442015-01-15 16:57:00 -080095 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070096
97 try:
Jon Hallc6793552016-01-19 14:18:37 -080098 if os.getenv( str( self.ip_address ) ) is not None:
kelvin-onlaba4074292015-07-09 15:19:49 -070099 self.ip_address = os.getenv( str( self.ip_address ) )
100 else:
101 main.log.info( self.name +
102 ": Trying to connect to " +
103 self.ip_address )
104
105 except KeyError:
106 main.log.info( "Invalid host name," +
107 " connecting to local host instead" )
108 self.ip_address = 'localhost'
109 except Exception as inst:
110 main.log.error( "Uncaught exception: " + str( inst ) )
111
kelvin8ec71442015-01-15 16:57:00 -0800112 self.handle = super( OnosCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -0800113 user_name=self.user_name,
114 ip_address=self.ip_address,
kelvin-onlab898a6c62015-01-16 14:13:53 -0800115 port=self.port,
116 pwd=self.pwd,
117 home=self.home )
andrewonlab95ce8322014-10-13 14:12:04 -0400118
kelvin8ec71442015-01-15 16:57:00 -0800119 self.handle.sendline( "cd " + self.home )
Devin Limdc78e202017-06-09 18:30:07 -0700120 self.handle.expect( self.prompt )
andrewonlab95ce8322014-10-13 14:12:04 -0400121 if self.handle:
122 return self.handle
kelvin8ec71442015-01-15 16:57:00 -0800123 else:
124 main.log.info( "NO ONOS HANDLE" )
andrewonlab95ce8322014-10-13 14:12:04 -0400125 return main.FALSE
Jon Halld4d4b372015-01-28 16:02:41 -0800126 except TypeError:
127 main.log.exception( self.name + ": Object not as expected" )
128 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400129 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800130 main.log.error( self.name + ": EOF exception found" )
131 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700132 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800133 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800134 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700135 main.cleanAndExit()
andrewonlab95ce8322014-10-13 14:12:04 -0400136
kelvin8ec71442015-01-15 16:57:00 -0800137 def disconnect( self ):
138 """
andrewonlab95ce8322014-10-13 14:12:04 -0400139 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800140 """
Jon Halld61331b2015-02-17 16:35:47 -0800141 response = main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400142 try:
Jon Hall61282e32015-03-19 11:34:11 -0700143 if self.handle:
144 i = self.logout()
145 if i == main.TRUE:
146 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700147 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700148 self.handle.sendline( "exit" )
149 self.handle.expect( "closed" )
Jon Halld4d4b372015-01-28 16:02:41 -0800150 except TypeError:
151 main.log.exception( self.name + ": Object not as expected" )
Jon Halld61331b2015-02-17 16:35:47 -0800152 response = main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400153 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800154 main.log.error( self.name + ": EOF exception found" )
155 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700156 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700157 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700158 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800159 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800160 main.log.exception( self.name + ": Connection failed to the host" )
andrewonlab95ce8322014-10-13 14:12:04 -0400161 response = main.FALSE
162 return response
163
kelvin8ec71442015-01-15 16:57:00 -0800164 def logout( self ):
165 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500166 Sends 'logout' command to ONOS cli
Jon Hall61282e32015-03-19 11:34:11 -0700167 Returns main.TRUE if exited CLI and
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000168 main.FALSE on timeout (not guranteed you are disconnected)
Jon Hall61282e32015-03-19 11:34:11 -0700169 None on TypeError
170 Exits test on unknown error or pexpect exits unexpectedly
kelvin8ec71442015-01-15 16:57:00 -0800171 """
andrewonlab38d2b4a2014-11-13 16:28:47 -0500172 try:
Jon Hall61282e32015-03-19 11:34:11 -0700173 if self.handle:
174 self.handle.sendline( "" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800175 i = self.handle.expect( [ self.karafPrompt, self.prompt, pexpect.TIMEOUT ],
Jon Hall61282e32015-03-19 11:34:11 -0700176 timeout=10 )
177 if i == 0: # In ONOS CLI
178 self.handle.sendline( "logout" )
Devin Limdc78e202017-06-09 18:30:07 -0700179 j = self.handle.expect( [ self.prompt,
Jon Hallbfe00002016-04-05 10:23:54 -0700180 "Command not found:",
181 pexpect.TIMEOUT ] )
182 if j == 0: # Successfully logged out
183 return main.TRUE
184 elif j == 1 or j == 2:
185 # ONOS didn't fully load, and logout command isn't working
186 # or the command timed out
187 self.handle.send( "\x04" ) # send ctrl-d
Jon Hall64ab3bd2016-05-13 11:29:44 -0700188 try:
Devin Limdc78e202017-06-09 18:30:07 -0700189 self.handle.expect( self.prompt )
Jon Hall64ab3bd2016-05-13 11:29:44 -0700190 except pexpect.TIMEOUT:
191 main.log.error( "ONOS did not respond to 'logout' or CTRL-d" )
Jon Hallbfe00002016-04-05 10:23:54 -0700192 return main.TRUE
Jon Halle0f0b342017-04-18 11:43:47 -0700193 else: # some other output
Jon Hallbfe00002016-04-05 10:23:54 -0700194 main.log.warn( "Unknown repsonse to logout command: '{}'",
195 repr( self.handle.before ) )
196 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -0700197 elif i == 1: # not in CLI
198 return main.TRUE
199 elif i == 3: # Timeout
200 return main.FALSE
201 else:
andrewonlab9627f432014-11-14 12:45:10 -0500202 return main.TRUE
Jon Halld4d4b372015-01-28 16:02:41 -0800203 except TypeError:
204 main.log.exception( self.name + ": Object not as expected" )
205 return None
andrewonlab38d2b4a2014-11-13 16:28:47 -0500206 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800207 main.log.error( self.name + ": eof exception found" )
Jon Hall61282e32015-03-19 11:34:11 -0700208 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700209 main.cleanAndExit()
Jon Hall61282e32015-03-19 11:34:11 -0700210 except ValueError:
Jon Hall5aa168b2015-03-23 14:23:09 -0700211 main.log.error( self.name +
212 "ValueError exception in logout method" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800213 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800214 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700215 main.cleanAndExit()
andrewonlab38d2b4a2014-11-13 16:28:47 -0500216
kelvin-onlabd3b64892015-01-20 13:26:24 -0800217 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800218 """
andrewonlab95ce8322014-10-13 14:12:04 -0400219 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800220
andrewonlab95ce8322014-10-13 14:12:04 -0400221 Before issuing any cli commands, set the environment variable first.
kelvin8ec71442015-01-15 16:57:00 -0800222 """
andrewonlab95ce8322014-10-13 14:12:04 -0400223 try:
224 if not cellname:
kelvin8ec71442015-01-15 16:57:00 -0800225 main.log.error( "Must define cellname" )
Devin Lim44075962017-08-11 10:56:37 -0700226 main.cleanAndExit()
andrewonlab95ce8322014-10-13 14:12:04 -0400227 else:
kelvin8ec71442015-01-15 16:57:00 -0800228 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800229 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800230 # Note that this variable name is subject to change
andrewonlab95ce8322014-10-13 14:12:04 -0400231 # and that this driver will have to change accordingly
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700232 self.handle.expect( str( cellname ) )
andrew@onlab.usc400b112015-01-21 15:33:19 -0800233 handleBefore = self.handle.before
234 handleAfter = self.handle.after
kelvin8ec71442015-01-15 16:57:00 -0800235 # Get the rest of the handle
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700236 self.handle.sendline( "" )
237 self.handle.expect( self.prompt )
andrew@onlab.usc400b112015-01-21 15:33:19 -0800238 handleMore = self.handle.before
andrewonlab95ce8322014-10-13 14:12:04 -0400239
kelvin-onlabd3b64892015-01-20 13:26:24 -0800240 main.log.info( "Cell call returned: " + handleBefore +
241 handleAfter + handleMore )
andrewonlab95ce8322014-10-13 14:12:04 -0400242
243 return main.TRUE
244
Jon Halld4d4b372015-01-28 16:02:41 -0800245 except TypeError:
246 main.log.exception( self.name + ": Object not as expected" )
247 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400248 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800249 main.log.error( self.name + ": eof exception found" )
250 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700251 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800252 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800253 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700254 main.cleanAndExit()
kelvin8ec71442015-01-15 16:57:00 -0800255
pingping-lin57a56ce2015-05-20 16:43:48 -0700256 def startOnosCli( self, ONOSIp, karafTimeout="",
Chiyu Chengef109502016-11-21 15:51:38 -0800257 commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ):
kelvin8ec71442015-01-15 16:57:00 -0800258 """
Jon Hallefbd9792015-03-05 16:11:36 -0800259 karafTimeout is an optional argument. karafTimeout value passed
kelvin-onlabd3b64892015-01-20 13:26:24 -0800260 by user would be used to set the current karaf shell idle timeout.
261 Note that when ever this property is modified the shell will exit and
Hari Krishnad7b9c202015-01-05 10:38:14 -0800262 the subsequent login would reflect new idle timeout.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800263 Below is an example to start a session with 60 seconds idle timeout
264 ( input value is in milliseconds ):
kelvin8ec71442015-01-15 16:57:00 -0800265
Hari Krishna25d42f72015-01-05 15:08:28 -0800266 tValue = "60000"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800267 main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue )
kelvin8ec71442015-01-15 16:57:00 -0800268
kelvin-onlabd3b64892015-01-20 13:26:24 -0800269 Note: karafTimeout is left as str so that this could be read
270 and passed to startOnosCli from PARAMS file as str.
kelvin8ec71442015-01-15 16:57:00 -0800271 """
You Wangf69ab392016-01-26 16:34:38 -0800272 self.onosIp = ONOSIp
andrewonlab95ce8322014-10-13 14:12:04 -0400273 try:
Jon Hall67253832016-12-05 09:47:13 -0800274 # Check if we are already in the cli
kelvin8ec71442015-01-15 16:57:00 -0800275 self.handle.sendline( "" )
276 x = self.handle.expect( [
Jon Hall6c9e2da2018-11-06 12:01:23 -0800277 self.prompt, self.karafPrompt ], commandlineTimeout )
andrewonlab48829f62014-11-17 13:49:01 -0500278 if x == 1:
kelvin8ec71442015-01-15 16:57:00 -0800279 main.log.info( "ONOS cli is already running" )
andrewonlab48829f62014-11-17 13:49:01 -0500280 return main.TRUE
andrewonlab95ce8322014-10-13 14:12:04 -0400281
Jon Hall67253832016-12-05 09:47:13 -0800282 # Not in CLI so login
Chiyu Chengef109502016-11-21 15:51:38 -0800283 if waitForStart:
Jeremy Ronquilloec916a42018-02-02 13:05:57 -0800284 # Wait for onos start ( onos-wait-for-start ) and enter onos cli
285 startCliCommand = "onos-wait-for-start "
Chiyu Chengef109502016-11-21 15:51:38 -0800286 else:
287 startCliCommand = "onos "
288 self.handle.sendline( startCliCommand + str( ONOSIp ) )
kelvin8ec71442015-01-15 16:57:00 -0800289 i = self.handle.expect( [
Jon Hall6c9e2da2018-11-06 12:01:23 -0800290 self.karafPrompt,
pingping-lin57a56ce2015-05-20 16:43:48 -0700291 pexpect.TIMEOUT ], onosStartTimeout )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400292
293 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800294 main.log.info( str( ONOSIp ) + " CLI Started successfully" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800295 if karafTimeout: # FIXME: This doesn't look right
kelvin8ec71442015-01-15 16:57:00 -0800296 self.handle.sendline(
Hari Krishnaac4e1782015-01-26 12:09:12 -0800297 "config:property-set -p org.apache.karaf.shell\
298 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800299 karafTimeout )
Devin Limdc78e202017-06-09 18:30:07 -0700300 self.handle.expect( self.prompt )
Chiyu Chengef109502016-11-21 15:51:38 -0800301 self.handle.sendline( startCliCommand + str( ONOSIp ) )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800302 self.handle.expect( self.karafPrompt )
303 main.log.debug( self.handle.before )
andrewonlab2a7ea9b2014-10-24 12:21:05 -0400304 return main.TRUE
305 else:
kelvin8ec71442015-01-15 16:57:00 -0800306 # If failed, send ctrl+c to process and try again
307 main.log.info( "Starting CLI failed. Retrying..." )
308 self.handle.send( "\x03" )
Chiyu Chengef109502016-11-21 15:51:38 -0800309 self.handle.sendline( startCliCommand + str( ONOSIp ) )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800310 i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ],
kelvin8ec71442015-01-15 16:57:00 -0800311 timeout=30 )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400312 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800313 main.log.info( str( ONOSIp ) + " CLI Started " +
kelvin8ec71442015-01-15 16:57:00 -0800314 "successfully after retry attempt" )
Hari Krishnae36ef212015-01-04 14:09:13 -0800315 if karafTimeout:
kelvin8ec71442015-01-15 16:57:00 -0800316 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800317 "config:property-set -p org.apache.karaf.shell\
318 sshIdleTimeout " +
kelvin8ec71442015-01-15 16:57:00 -0800319 karafTimeout )
Devin Limdc78e202017-06-09 18:30:07 -0700320 self.handle.expect( self.prompt )
Chiyu Chengef109502016-11-21 15:51:38 -0800321 self.handle.sendline( startCliCommand + str( ONOSIp ) )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800322 self.handle.expect( self.karafPrompt )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400323 return main.TRUE
324 else:
kelvin8ec71442015-01-15 16:57:00 -0800325 main.log.error( "Connection to CLI " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800326 str( ONOSIp ) + " timeout" )
andrewonlab3a7c3c72014-10-24 17:21:03 -0400327 return main.FALSE
andrewonlab95ce8322014-10-13 14:12:04 -0400328
Jon Halld4d4b372015-01-28 16:02:41 -0800329 except TypeError:
330 main.log.exception( self.name + ": Object not as expected" )
331 return None
andrewonlab95ce8322014-10-13 14:12:04 -0400332 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800333 main.log.error( self.name + ": EOF exception found" )
334 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700335 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800336 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800337 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700338 main.cleanAndExit()
andrewonlab95ce8322014-10-13 14:12:04 -0400339
suibin zhang116647a2016-05-06 16:30:09 -0700340 def startCellCli( self, karafTimeout="",
341 commandlineTimeout=10, onosStartTimeout=60 ):
342 """
343 Start CLI on onos ecll handle.
344
345 karafTimeout is an optional argument. karafTimeout value passed
346 by user would be used to set the current karaf shell idle timeout.
347 Note that when ever this property is modified the shell will exit and
348 the subsequent login would reflect new idle timeout.
349 Below is an example to start a session with 60 seconds idle timeout
350 ( input value is in milliseconds ):
351
352 tValue = "60000"
353
354 Note: karafTimeout is left as str so that this could be read
355 and passed to startOnosCli from PARAMS file as str.
356 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000357
suibin zhang116647a2016-05-06 16:30:09 -0700358 try:
359 self.handle.sendline( "" )
360 x = self.handle.expect( [
Jon Hall6c9e2da2018-11-06 12:01:23 -0800361 self.prompt, self.karafPrompt ], commandlineTimeout )
suibin zhang116647a2016-05-06 16:30:09 -0700362
363 if x == 1:
364 main.log.info( "ONOS cli is already running" )
365 return main.TRUE
366
Jeremy Ronquilloec916a42018-02-02 13:05:57 -0800367 # Wait for onos start ( onos-wait-for-start ) and enter onos cli
suibin zhang116647a2016-05-06 16:30:09 -0700368 self.handle.sendline( "/opt/onos/bin/onos" )
369 i = self.handle.expect( [
Jon Hall6c9e2da2018-11-06 12:01:23 -0800370 self.karafPrompt,
suibin zhang116647a2016-05-06 16:30:09 -0700371 pexpect.TIMEOUT ], onosStartTimeout )
372
373 if i == 0:
374 main.log.info( self.name + " CLI Started successfully" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800375 if karafTimeout: # FIXME: This doesn't look right
suibin zhang116647a2016-05-06 16:30:09 -0700376 self.handle.sendline(
377 "config:property-set -p org.apache.karaf.shell\
378 sshIdleTimeout " +
379 karafTimeout )
Devin Limdc78e202017-06-09 18:30:07 -0700380 self.handle.expect( self.prompt )
suibin zhang116647a2016-05-06 16:30:09 -0700381 self.handle.sendline( "/opt/onos/bin/onos" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800382 self.handle.expect( self.karafPrompt )
383 main.log.debug( self.handle.before )
suibin zhang116647a2016-05-06 16:30:09 -0700384 return main.TRUE
385 else:
386 # If failed, send ctrl+c to process and try again
387 main.log.info( "Starting CLI failed. Retrying..." )
388 self.handle.send( "\x03" )
389 self.handle.sendline( "/opt/onos/bin/onos" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800390 i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ],
suibin zhang116647a2016-05-06 16:30:09 -0700391 timeout=30 )
392 if i == 0:
393 main.log.info( self.name + " CLI Started " +
394 "successfully after retry attempt" )
395 if karafTimeout:
396 self.handle.sendline(
397 "config:property-set -p org.apache.karaf.shell\
398 sshIdleTimeout " +
399 karafTimeout )
Devin Limdc78e202017-06-09 18:30:07 -0700400 self.handle.expect( self.prompt )
suibin zhang116647a2016-05-06 16:30:09 -0700401 self.handle.sendline( "/opt/onos/bin/onos" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800402 self.handle.expect( self.karafPrompt )
suibin zhang116647a2016-05-06 16:30:09 -0700403 return main.TRUE
404 else:
405 main.log.error( "Connection to CLI " +
406 self.name + " timeout" )
407 return main.FALSE
408
409 except TypeError:
410 main.log.exception( self.name + ": Object not as expected" )
411 return None
412 except pexpect.EOF:
413 main.log.error( self.name + ": EOF exception found" )
414 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700415 main.cleanAndExit()
suibin zhang116647a2016-05-06 16:30:09 -0700416 except Exception:
417 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700418 main.cleanAndExit()
suibin zhang116647a2016-05-06 16:30:09 -0700419
Pratik Parab3b2ab5a2017-02-14 13:15:14 -0800420 def log( self, cmdStr, level="", noExit=False ):
kelvin-onlab9f541032015-02-04 16:19:53 -0800421 """
422 log the commands in the onos CLI.
kelvin-onlab338f5512015-02-06 10:53:16 -0800423 returns main.TRUE on success
Jon Hallefbd9792015-03-05 16:11:36 -0800424 returns main.FALSE if Error occurred
YPZhangebf9eb52016-05-12 15:20:24 -0700425 if noExit is True, TestON will not exit, but clean up
kelvin-onlab338f5512015-02-06 10:53:16 -0800426 Available level: DEBUG, TRACE, INFO, WARN, ERROR
427 Level defaults to INFO
Pratik Parab3b2ab5a2017-02-14 13:15:14 -0800428 if cmdStr has spaces then put quotes in the passed string
kelvin-onlab9f541032015-02-04 16:19:53 -0800429 """
430 try:
kelvin-onlab338f5512015-02-06 10:53:16 -0800431 lvlStr = ""
432 if level:
433 lvlStr = "--level=" + level
434
kelvin-onlab338f5512015-02-06 10:53:16 -0800435 self.handle.sendline( "log:log " + lvlStr + " " + cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -0700436 self.handle.expect( "log:log" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800437 self.handle.expect( self.karafPrompt )
kelvin-onlabfb521662015-02-27 09:52:40 -0800438
kelvin-onlab9f541032015-02-04 16:19:53 -0800439 response = self.handle.before
440 if re.search( "Error", response ):
441 return main.FALSE
442 return main.TRUE
Jon Hall80daded2015-05-27 16:07:00 -0700443 except pexpect.TIMEOUT:
444 main.log.exception( self.name + ": TIMEOUT exception found" )
YPZhangebf9eb52016-05-12 15:20:24 -0700445 if noExit:
446 main.cleanup()
447 return None
448 else:
Devin Lim44075962017-08-11 10:56:37 -0700449 main.cleanAndExit()
kelvin-onlab9f541032015-02-04 16:19:53 -0800450 except pexpect.EOF:
451 main.log.error( self.name + ": EOF exception found" )
452 main.log.error( self.name + ": " + self.handle.before )
YPZhangebf9eb52016-05-12 15:20:24 -0700453 if noExit:
454 main.cleanup()
455 return None
456 else:
Devin Lim44075962017-08-11 10:56:37 -0700457 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800458 except Exception:
kelvin-onlabfb521662015-02-27 09:52:40 -0800459 main.log.exception( self.name + ": Uncaught exception!" )
YPZhangebf9eb52016-05-12 15:20:24 -0700460 if noExit:
461 main.cleanup()
462 return None
463 else:
Devin Lim44075962017-08-11 10:56:37 -0700464 main.cleanAndExit()
andrewonlab95ce8322014-10-13 14:12:04 -0400465
Jon Hall0e240372018-05-02 11:21:57 -0700466 def clearBuffer( self, debug=False, timeout=10, noExit=False ):
kelvin8ec71442015-01-15 16:57:00 -0800467 """
Jon Hall0e240372018-05-02 11:21:57 -0700468 Test cli connection and clear any left over output in the buffer
469 Optional Arguments:
470 debug - Defaults to False. If True, will enable debug logging.
471 timeout - Defaults to 10. Amount of time in seconds for a command to return
472 before a timeout.
473 noExit - Defaults to False. If True, will not exit TestON in the event of a
kelvin8ec71442015-01-15 16:57:00 -0800474 """
andrewonlaba18f6bf2014-10-13 19:31:54 -0400475 try:
Jon Halla495f562016-05-16 18:03:26 -0700476 # Try to reconnect if disconnected from cli
477 self.handle.sendline( "" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800478 i = self.handle.expect( [ self.karafPrompt, self.prompt, pexpect.TIMEOUT ] )
Jon Hall0e240372018-05-02 11:21:57 -0700479 response = self.handle.before
Jon Halla495f562016-05-16 18:03:26 -0700480 if i == 1:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700481 main.log.error( self.name + ": onos cli session closed. " )
Jon Halla495f562016-05-16 18:03:26 -0700482 if self.onosIp:
483 main.log.warn( "Trying to reconnect " + self.onosIp )
484 reconnectResult = self.startOnosCli( self.onosIp )
485 if reconnectResult:
486 main.log.info( self.name + ": onos cli session reconnected." )
487 else:
488 main.log.error( self.name + ": reconnection failed." )
YPZhang14a4aa92016-07-15 13:37:15 -0700489 if noExit:
490 return None
491 else:
Devin Lim44075962017-08-11 10:56:37 -0700492 main.cleanAndExit()
Jon Halla495f562016-05-16 18:03:26 -0700493 else:
Devin Lim44075962017-08-11 10:56:37 -0700494 main.cleanAndExit()
Jon Halla495f562016-05-16 18:03:26 -0700495 if i == 2:
Jon Hall7a6ebfd2017-03-13 10:58:58 -0700496 main.log.warn( "Timeout when testing cli responsiveness" )
497 main.log.debug( self.handle.before )
498 self.handle.send( "\x03" ) # Send ctrl-c to clear previous output
Jon Hall6c9e2da2018-11-06 12:01:23 -0800499 self.handle.expect( self.karafPrompt )
Jon Halla495f562016-05-16 18:03:26 -0700500
Jon Hall0e240372018-05-02 11:21:57 -0700501 response += self.handle.before
Jon Hall14a03b52016-05-11 12:07:30 -0700502 if debug:
Jon Hall0e240372018-05-02 11:21:57 -0700503 main.log.debug( self.name + ": Raw output from sending ''" )
504 main.log.debug( self.name + ": " + repr( response ) )
505 except pexpect.TIMEOUT:
506 main.log.error( self.name + ": ONOS timeout" )
507 main.log.debug( self.handle.before )
You Wang141b43b2018-06-26 16:50:18 -0700508 self.handle.send( "\x03" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800509 self.handle.expect( self.karafPrompt )
Jon Hall0e240372018-05-02 11:21:57 -0700510 return None
511 except pexpect.EOF:
512 main.log.error( self.name + ": EOF exception found" )
513 main.log.error( self.name + ": " + self.handle.before )
514 if noExit:
515 return None
516 else:
517 main.cleanAndExit()
518 except Exception:
519 main.log.exception( self.name + ": Uncaught exception!" )
520 if noExit:
521 return None
522 else:
523 main.cleanAndExit()
524
Jon Hall22e94ce2019-01-15 14:52:17 -0800525 def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True, expectJson=False ):
Jon Hall0e240372018-05-02 11:21:57 -0700526 """
527 A wrapper around pexpect's sendline/expect. Will return all the output from a given command
528
529 Required Arguments:
530 cmdStr - String to send to the pexpect session
531
532 Optional Arguments:
533 showResponse - Defaults to False. If True will log the response.
534 debug - Defaults to False. If True, will enable debug logging.
535 timeout - Defaults to 10. Amount of time in seconds for a command to return
536 before a timeout.
537 noExit - Defaults to False. If True, will not exit TestON in the event of a
538 closed channel, but instead return None
Jon Hall6c9e2da2018-11-06 12:01:23 -0800539 relaxedRegex - Defaults to True. If there is a pipe in the command send, will only try to match the last part of the piped command.
Jon Hall0e240372018-05-02 11:21:57 -0700540
541 Warning: There are no sanity checking to commands sent using this method.
542
543 """
544 try:
545 # Try to reconnect if disconnected from cli
546 self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit )
547 if debug:
548 # NOTE: This adds an average of .4 seconds per call
Jon Hall14a03b52016-05-11 12:07:30 -0700549 logStr = "\"Sending CLI command: '" + cmdStr + "'\""
Jon Halle0f0b342017-04-18 11:43:47 -0700550 self.log( logStr, noExit=noExit )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800551 self.handle.sendline( cmdStr )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800552 self.handle.expect( self.karafPrompt, timeout )
Jon Hall63604932015-02-26 17:09:50 -0800553 response = self.handle.before
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000554 main.log.info( "Command '" + str( cmdStr ) + "' sent to "
Jon Hallc6793552016-01-19 14:18:37 -0800555 + self.name + "." )
Jon Hallc6358dd2015-04-10 12:44:28 -0700556 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700557 main.log.debug( self.name + ": Raw output" )
558 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700559
Jon Hall6c9e2da2018-11-06 12:01:23 -0800560 # Remove control codes from karaf 4.2.1
Jon Hallcf31d0f2018-12-13 11:18:48 -0800561 karafEscape = re.compile( r"('(0|1)~\'|\r\r\r\n\x1b\[A\x1b\[79C(x|\s)?|\x1b(>|=)|\x1b\[90m~)" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800562 response = karafEscape.sub( '', response )
563 if debug:
564 main.log.debug( self.name + ": karafEscape output" )
565 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700566 # Remove ANSI color control strings from output
Jon Hall6c9e2da2018-11-06 12:01:23 -0800567 ansiEscape = re.compile( r'((\x9b|\x1b\[)[0-?]*[ -/]*[@-~])' )
Jon Hall63604932015-02-26 17:09:50 -0800568 response = ansiEscape.sub( '', response )
Jon Hallc6358dd2015-04-10 12:44:28 -0700569 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700570 main.log.debug( self.name + ": ansiEscape output" )
571 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700572
Jon Hall6c9e2da2018-11-06 12:01:23 -0800573 # Remove ANSI color control strings from output
Jon Hallcf31d0f2018-12-13 11:18:48 -0800574 # NOTE: karaf is sometimes adding a single character then two
575 # backspaces and sometimes adding 2 characters with 2 backspaces??
Jon Hall6c9e2da2018-11-06 12:01:23 -0800576 backspaceEscape = re.compile( r'((..\x08\x08)|(.|\s)\x08)' )
577 response = backspaceEscape.sub( '', response )
578 if debug:
579 main.log.debug( self.name + ": backspaceEscape output" )
580 main.log.debug( self.name + ": " + repr( response ) )
581
kelvin-onlabfb521662015-02-27 09:52:40 -0800582 # Remove extra return chars that get added
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000583 response = re.sub( r"\s\r", "", response )
Jon Hallc6358dd2015-04-10 12:44:28 -0700584 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700585 main.log.debug( self.name + ": Removed extra returns " +
586 "from output" )
587 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700588
589 # Strip excess whitespace
Jon Hall63604932015-02-26 17:09:50 -0800590 response = response.strip()
Jon Hallc6358dd2015-04-10 12:44:28 -0700591 if debug:
Jon Hall390696c2015-05-05 17:13:41 -0700592 main.log.debug( self.name + ": parsed and stripped output" )
593 main.log.debug( self.name + ": " + repr( response ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700594
Jon Hall63604932015-02-26 17:09:50 -0800595 # parse for just the output, remove the cmd from response
Jon Hallce0d70b2018-12-11 11:01:32 -0800596 cmdPattern = cmdStr.strip()
597 main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) +
598 "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800599 if relaxedRegex:
Jon Hallce0d70b2018-12-11 11:01:32 -0800600 cmdPattern = cmdPattern.split( '|' )[ -1 ].strip()
601 main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) +
602 "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800603 # This was added because karaf 4.2 is stripping some characters from the command echo
Jon Hallce0d70b2018-12-11 11:01:32 -0800604 output = response.split( cmdPattern, 1 )
Jon Hall22e94ce2019-01-15 14:52:17 -0800605 if expectJson:
606 main.log.warn( "Relaxed Regex: Searching for a json string amongst the output" )
607 jsonPattern = r'\{.*\}'
608 match = re.search( jsonPattern, output[ 0 ] )
609 if match:
610 output = [ '' , match.group( 0 ) ] # We expect a list with the second element to be the output
Jon Hall39e3ffe2018-12-05 11:40:29 -0800611 if len( output ) < 2:
612 main.log.warn( "Relaxing regex match to last 5 characters of the sent command" )
Jon Hallce0d70b2018-12-11 11:01:32 -0800613 cmdPattern = cmdPattern[ -5: ]
614 main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) +
615 "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) )
616 output = response.split( cmdPattern, 1 )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800617 else:
Jon Hallce0d70b2018-12-11 11:01:32 -0800618 output = response.split( cmdPattern, 1 )
619 if len( output ) < 2: # TODO: Should we do this without the relaxedRegex flag?
Jon Hall8c9dd1c2018-11-14 15:40:39 -0800620 main.log.warn( "Relaxing regex match to last 5 characters of the sent command" )
Jon Hallce0d70b2018-12-11 11:01:32 -0800621 output = response.split( cmdPattern[ -5: ], 1 )
Jon Hall0e240372018-05-02 11:21:57 -0700622 if output:
623 if debug:
624 main.log.debug( self.name + ": split output" )
625 for r in output:
626 main.log.debug( self.name + ": " + repr( r ) )
Jon Hallce0d70b2018-12-11 11:01:32 -0800627 if len( output ) == 1:
628 main.log.error( "Could not remove sent command echo from output" )
629 return output
Jon Hall0e240372018-05-02 11:21:57 -0700630 output = output[ 1 ].strip()
GlennRC85870432015-11-23 11:45:51 -0800631 if showResponse:
GlennRCed771242016-01-13 17:02:47 -0800632 main.log.info( "Response from ONOS: {}".format( output ) )
Jon Hall0e240372018-05-02 11:21:57 -0700633 self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit )
GlennRC85870432015-11-23 11:45:51 -0800634 return output
GlennRCed771242016-01-13 17:02:47 -0800635 except pexpect.TIMEOUT:
Jon Hall0e240372018-05-02 11:21:57 -0700636 main.log.error( self.name + ": ONOS timeout" )
GlennRCed771242016-01-13 17:02:47 -0800637 if debug:
638 main.log.debug( self.handle.before )
You Wang141b43b2018-06-26 16:50:18 -0700639 self.handle.send( "\x03" )
Jon Hall6c9e2da2018-11-06 12:01:23 -0800640 self.handle.expect( self.karafPrompt )
GlennRCed771242016-01-13 17:02:47 -0800641 return None
Jon Hallc6358dd2015-04-10 12:44:28 -0700642 except IndexError:
643 main.log.exception( self.name + ": Object not as expected" )
Jon Halla495f562016-05-16 18:03:26 -0700644 main.log.debug( "response: {}".format( repr( response ) ) )
Jon Hallc6358dd2015-04-10 12:44:28 -0700645 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800646 except TypeError:
647 main.log.exception( self.name + ": Object not as expected" )
648 return None
andrewonlaba18f6bf2014-10-13 19:31:54 -0400649 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800650 main.log.error( self.name + ": EOF exception found" )
651 main.log.error( self.name + ": " + self.handle.before )
YPZhangebf9eb52016-05-12 15:20:24 -0700652 if noExit:
YPZhangebf9eb52016-05-12 15:20:24 -0700653 return None
654 else:
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!" )
YPZhangebf9eb52016-05-12 15:20:24 -0700658 if noExit:
YPZhangebf9eb52016-05-12 15:20:24 -0700659 return None
660 else:
Devin Lim44075962017-08-11 10:56:37 -0700661 main.cleanAndExit()
andrewonlaba18f6bf2014-10-13 19:31:54 -0400662
Jon Halld5a94fb2018-11-13 14:32:23 -0800663 def lineCount( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ):
664 """
665 A wrapper around sendline(). Will return the number of lines returned or None on error
666
667 Required Arguments:
668 cmdStr - String to send to the pexpect session
669
670 Optional Arguments:
671 showResponse - Defaults to False. If True will log the response.
672 debug - Defaults to False. If True, will enable debug logging.
673 timeout - Defaults to 10. Amount of time in seconds for a command to return
674 before a timeout.
675 noExit - Defaults to False. If True, will not exit TestON in the event of a
676 closed channel, but instead return None
677 relaxedRegex - Defaults to True. If there is a pipe in the command send, will only try to match the last part of the piped command.
678
679 Warning: There are no sanity checking to commands sent using this method.
680
681 """
682 try:
683 numLines = self.sendline( cmdStr, showResponse, debug, timeout, noExit, relaxedRegex )
Jon Hall8c9dd1c2018-11-14 15:40:39 -0800684 parsed = re.search( "(\d+)\s+(\d+)", numLines )
685 if not parsed:
686 main.log.error( "Warning, output of karaf's wc may have changed" )
687 return None
688 return parsed.group( 1 )
Jon Halld5a94fb2018-11-13 14:32:23 -0800689 except IndexError:
690 main.log.exception( self.name + ": Object not as expected" )
Jon Hallce0d70b2018-12-11 11:01:32 -0800691 main.log.debug( "response: {}".format( repr( numLines ) ) )
Jon Halld5a94fb2018-11-13 14:32:23 -0800692 return None
693 except TypeError:
694 main.log.exception( self.name + ": Object not as expected" )
695 return None
696 except Exception:
697 main.log.exception( self.name + ": Uncaught exception!" )
698 if noExit:
699 return None
700 else:
701 main.cleanAndExit()
702
kelvin8ec71442015-01-15 16:57:00 -0800703 # IMPORTANT NOTE:
704 # For all cli commands, naming convention should match
kelvin-onlabd3b64892015-01-20 13:26:24 -0800705 # the cli command changing 'a:b' with 'aB'.
706 # Ex ) onos:topology > onosTopology
707 # onos:links > onosLinks
708 # feature:list > featureList
Jon Halle3f39ff2015-01-13 11:50:53 -0800709
kelvin-onlabd3b64892015-01-20 13:26:24 -0800710 def addNode( self, nodeId, ONOSIp, tcpPort="" ):
kelvin8ec71442015-01-15 16:57:00 -0800711 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400712 Adds a new cluster node by ID and address information.
713 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800714 * nodeId
715 * ONOSIp
andrewonlabc2d05aa2014-10-13 16:51:10 -0400716 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800717 * tcpPort
kelvin8ec71442015-01-15 16:57:00 -0800718 """
andrewonlabc2d05aa2014-10-13 16:51:10 -0400719 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800720 cmdStr = "add-node " + str( nodeId ) + " " +\
721 str( ONOSIp ) + " " + str( tcpPort )
722 handle = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -0700723 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800724 assert "Command not found:" not in handle, handle
kelvin-onlab898a6c62015-01-16 14:13:53 -0800725 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -0700726 main.log.error( self.name + ": Error in adding node" )
kelvin8ec71442015-01-15 16:57:00 -0800727 main.log.error( handle )
Jon Halle3f39ff2015-01-13 11:50:53 -0800728 return main.FALSE
andrewonlabc2d05aa2014-10-13 16:51:10 -0400729 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800730 main.log.info( "Node " + str( ONOSIp ) + " added" )
andrewonlabc2d05aa2014-10-13 16:51:10 -0400731 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -0800732 except AssertionError:
733 main.log.exception( "" )
734 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800735 except TypeError:
736 main.log.exception( self.name + ": Object not as expected" )
737 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400738 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800739 main.log.error( self.name + ": EOF exception found" )
740 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700741 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800742 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800743 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700744 main.cleanAndExit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400745
kelvin-onlabd3b64892015-01-20 13:26:24 -0800746 def removeNode( self, nodeId ):
kelvin8ec71442015-01-15 16:57:00 -0800747 """
andrewonlab86dc3082014-10-13 18:18:38 -0400748 Removes a cluster by ID
749 Issues command: 'remove-node [<node-id>]'
750 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800751 * nodeId
kelvin8ec71442015-01-15 16:57:00 -0800752 """
andrewonlab86dc3082014-10-13 18:18:38 -0400753 try:
andrewonlab86dc3082014-10-13 18:18:38 -0400754
kelvin-onlabd3b64892015-01-20 13:26:24 -0800755 cmdStr = "remove-node " + str( nodeId )
Jon Hall08f61bc2015-04-13 16:00:30 -0700756 handle = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -0700757 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800758 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700759 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -0700760 main.log.error( self.name + ": Error in removing node" )
Jon Hallc6358dd2015-04-10 12:44:28 -0700761 main.log.error( handle )
762 return main.FALSE
763 else:
764 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -0800765 except AssertionError:
766 main.log.exception( "" )
767 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800768 except TypeError:
769 main.log.exception( self.name + ": Object not as expected" )
770 return None
andrewonlab86dc3082014-10-13 18:18:38 -0400771 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800772 main.log.error( self.name + ": EOF exception found" )
773 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700774 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800775 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800776 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700777 main.cleanAndExit()
andrewonlabc2d05aa2014-10-13 16:51:10 -0400778
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700779 def nodes( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -0800780 """
andrewonlab7c211572014-10-15 16:45:20 -0400781 List the nodes currently visible
782 Issues command: 'nodes'
Jon Hall61282e32015-03-19 11:34:11 -0700783 Optional argument:
784 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800785 """
andrewonlab7c211572014-10-15 16:45:20 -0400786 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700787 cmdStr = "nodes"
Jon Hall61282e32015-03-19 11:34:11 -0700788 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700789 cmdStr += " -j"
790 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -0700791 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800792 assert "Command not found:" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -0700793 return output
Jon Hallc6793552016-01-19 14:18:37 -0800794 except AssertionError:
795 main.log.exception( "" )
796 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800797 except TypeError:
798 main.log.exception( self.name + ": Object not as expected" )
799 return None
andrewonlab7c211572014-10-15 16:45:20 -0400800 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800801 main.log.error( self.name + ": EOF exception found" )
802 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700803 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800804 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800805 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700806 main.cleanAndExit()
andrewonlab7c211572014-10-15 16:45:20 -0400807
kelvin8ec71442015-01-15 16:57:00 -0800808 def topology( self ):
809 """
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700810 Definition:
Jon Hall390696c2015-05-05 17:13:41 -0700811 Returns the output of topology command.
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700812 Return:
813 topology = current ONOS topology
kelvin8ec71442015-01-15 16:57:00 -0800814 """
andrewonlab95ce8322014-10-13 14:12:04 -0400815 try:
Hari Krishnaef1bd4e2015-03-12 16:55:30 -0700816 cmdStr = "topology -j"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800817 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 main.log.info( cmdStr + " returned: " + str( handle ) )
andrewonlab95ce8322014-10-13 14:12:04 -0400821 return handle
Jon Hallc6793552016-01-19 14:18:37 -0800822 except AssertionError:
823 main.log.exception( "" )
Jon Halld4d4b372015-01-28 16:02:41 -0800824 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800825 except TypeError:
826 main.log.exception( self.name + ": Object not as expected" )
827 return None
andrewonlabc2d05aa2014-10-13 16:51:10 -0400828 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800829 main.log.error( self.name + ": EOF exception found" )
830 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700831 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800832 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800833 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700834 main.cleanAndExit()
Jon Hallffb386d2014-11-21 13:43:38 -0800835
jenkins7ead5a82015-03-13 10:28:21 -0700836 def deviceRemove( self, deviceId ):
837 """
838 Removes particular device from storage
839
840 TODO: refactor this function
841 """
842 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700843 cmdStr = "device-remove " + str( deviceId )
844 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -0800845 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800846 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700847 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -0700848 main.log.error( self.name + ": Error in removing device" )
Jon Hallc6358dd2015-04-10 12:44:28 -0700849 main.log.error( handle )
850 return main.FALSE
851 else:
852 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -0800853 except AssertionError:
854 main.log.exception( "" )
855 return None
jenkins7ead5a82015-03-13 10:28:21 -0700856 except TypeError:
857 main.log.exception( self.name + ": Object not as expected" )
858 return None
859 except pexpect.EOF:
860 main.log.error( self.name + ": EOF exception found" )
861 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700862 main.cleanAndExit()
jenkins7ead5a82015-03-13 10:28:21 -0700863 except Exception:
864 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700865 main.cleanAndExit()
jenkins7ead5a82015-03-13 10:28:21 -0700866
You Wang3b9689a2018-08-30 12:24:00 -0700867 def devices( self, jsonFormat=True, timeout=30 ):
kelvin8ec71442015-01-15 16:57:00 -0800868 """
Jon Hall7b02d952014-10-17 20:14:54 -0400869 Lists all infrastructure devices or switches
andrewonlab86dc3082014-10-13 18:18:38 -0400870 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800871 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -0800872 """
andrewonlab86dc3082014-10-13 18:18:38 -0400873 try:
Jon Hallc6358dd2015-04-10 12:44:28 -0700874 cmdStr = "devices"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800875 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -0700876 cmdStr += " -j"
You Wang3b9689a2018-08-30 12:24:00 -0700877 handle = self.sendline( cmdStr, timeout=timeout )
You Wangb5a55f72017-03-03 12:51:05 -0800878 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800879 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700880 return handle
Jon Hallc6793552016-01-19 14:18:37 -0800881 except AssertionError:
882 main.log.exception( "" )
883 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800884 except TypeError:
885 main.log.exception( self.name + ": Object not as expected" )
886 return None
andrewonlab7c211572014-10-15 16:45:20 -0400887 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800888 main.log.error( self.name + ": EOF exception found" )
889 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700890 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800891 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800892 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700893 main.cleanAndExit()
andrewonlab7c211572014-10-15 16:45:20 -0400894
kelvin-onlabd3b64892015-01-20 13:26:24 -0800895 def balanceMasters( self ):
kelvin8ec71442015-01-15 16:57:00 -0800896 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800897 This balances the devices across all controllers
898 by issuing command: 'onos> onos:balance-masters'
899 If required this could be extended to return devices balanced output.
kelvin8ec71442015-01-15 16:57:00 -0800900 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800901 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800902 cmdStr = "onos:balance-masters"
Jon Hallc6358dd2015-04-10 12:44:28 -0700903 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -0800904 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800905 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -0700906 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -0700907 main.log.error( self.name + ": Error in balancing masters" )
Jon Hallc6358dd2015-04-10 12:44:28 -0700908 main.log.error( handle )
909 return main.FALSE
910 else:
911 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -0800912 except AssertionError:
913 main.log.exception( "" )
914 return None
Jon Halld4d4b372015-01-28 16:02:41 -0800915 except TypeError:
916 main.log.exception( self.name + ": Object not as expected" )
917 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800918 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800919 main.log.error( self.name + ": EOF exception found" )
920 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700921 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800922 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -0800923 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700924 main.cleanAndExit()
Hari Krishnaa43d4e92014-12-19 13:22:40 -0800925
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000926 def checkMasters( self, jsonFormat=True ):
acsmars24950022015-07-30 18:00:43 -0700927 """
928 Returns the output of the masters command.
929 Optional argument:
930 * jsonFormat - boolean indicating if you want output in json
931 """
932 try:
933 cmdStr = "onos:masters"
934 if jsonFormat:
935 cmdStr += " -j"
936 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -0700937 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -0800938 assert "Command not found:" not in output, output
acsmars24950022015-07-30 18:00:43 -0700939 return output
Jon Hallc6793552016-01-19 14:18:37 -0800940 except AssertionError:
941 main.log.exception( "" )
942 return None
acsmars24950022015-07-30 18:00:43 -0700943 except TypeError:
944 main.log.exception( self.name + ": Object not as expected" )
945 return None
946 except pexpect.EOF:
947 main.log.error( self.name + ": EOF exception found" )
948 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700949 main.cleanAndExit()
acsmars24950022015-07-30 18:00:43 -0700950 except Exception:
951 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700952 main.cleanAndExit()
acsmars24950022015-07-30 18:00:43 -0700953
Jon Hallc6793552016-01-19 14:18:37 -0800954 def checkBalanceMasters( self, jsonFormat=True ):
acsmars24950022015-07-30 18:00:43 -0700955 """
956 Uses the master command to check that the devices' leadership
957 is evenly divided
958
959 Dependencies: checkMasters() and summary()
960
Jon Hall6509dbf2016-06-21 17:01:17 -0700961 Returns main.TRUE if the devices are balanced
962 Returns main.FALSE if the devices are unbalanced
acsmars24950022015-07-30 18:00:43 -0700963 Exits on Exception
964 Returns None on TypeError
965 """
966 try:
Jon Hallc6793552016-01-19 14:18:37 -0800967 summaryOutput = self.summary()
968 totalDevices = json.loads( summaryOutput )[ "devices" ]
969 except ( TypeError, ValueError ):
970 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) )
971 return None
972 try:
acsmars24950022015-07-30 18:00:43 -0700973 totalOwnedDevices = 0
Jon Hallc6793552016-01-19 14:18:37 -0800974 mastersOutput = self.checkMasters()
975 masters = json.loads( mastersOutput )
acsmars24950022015-07-30 18:00:43 -0700976 first = masters[ 0 ][ "size" ]
977 for master in masters:
978 totalOwnedDevices += master[ "size" ]
979 if master[ "size" ] > first + 1 or master[ "size" ] < first - 1:
980 main.log.error( "Mastership not balanced" )
981 main.log.info( "\n" + self.checkMasters( False ) )
982 return main.FALSE
Jon Halle0f0b342017-04-18 11:43:47 -0700983 main.log.info( "Mastership balanced between " +
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700984 str( len( masters ) ) + " masters" )
acsmars24950022015-07-30 18:00:43 -0700985 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -0800986 except ( TypeError, ValueError ):
987 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) )
acsmars24950022015-07-30 18:00:43 -0700988 return None
989 except pexpect.EOF:
990 main.log.error( self.name + ": EOF exception found" )
991 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700992 main.cleanAndExit()
acsmars24950022015-07-30 18:00:43 -0700993 except Exception:
994 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700995 main.cleanAndExit()
acsmars24950022015-07-30 18:00:43 -0700996
YPZhangfebf7302016-05-24 16:45:56 -0700997 def links( self, jsonFormat=True, timeout=30 ):
kelvin8ec71442015-01-15 16:57:00 -0800998 """
Jon Halle8217482014-10-17 13:49:14 -0400999 Lists all core links
1000 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001001 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001002 """
Jon Halle8217482014-10-17 13:49:14 -04001003 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07001004 cmdStr = "links"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001005 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07001006 cmdStr += " -j"
YPZhangfebf7302016-05-24 16:45:56 -07001007 handle = self.sendline( cmdStr, timeout=timeout )
You Wangb5a55f72017-03-03 12:51:05 -08001008 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001009 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -07001010 return handle
Jon Hallc6793552016-01-19 14:18:37 -08001011 except AssertionError:
1012 main.log.exception( "" )
1013 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001014 except TypeError:
1015 main.log.exception( self.name + ": Object not as expected" )
1016 return None
Jon Halle8217482014-10-17 13:49:14 -04001017 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001018 main.log.error( self.name + ": EOF exception found" )
1019 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001020 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001021 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001022 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001023 main.cleanAndExit()
Jon Halle8217482014-10-17 13:49:14 -04001024
You Wang3b9689a2018-08-30 12:24:00 -07001025 def ports( self, jsonFormat=True, timeout=30 ):
kelvin8ec71442015-01-15 16:57:00 -08001026 """
Jon Halle8217482014-10-17 13:49:14 -04001027 Lists all ports
1028 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001029 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001030 """
Jon Halle8217482014-10-17 13:49:14 -04001031 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07001032 cmdStr = "ports"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001033 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07001034 cmdStr += " -j"
You Wang3b9689a2018-08-30 12:24:00 -07001035 handle = self.sendline( cmdStr, timeout=timeout )
You Wangb5a55f72017-03-03 12:51:05 -08001036 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001037 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -07001038 return handle
Jon Hallc6793552016-01-19 14:18:37 -08001039 except AssertionError:
1040 main.log.exception( "" )
1041 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001042 except TypeError:
1043 main.log.exception( self.name + ": Object not as expected" )
1044 return None
Jon Halle8217482014-10-17 13:49:14 -04001045 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001046 main.log.error( self.name + ": EOF exception found" )
1047 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001048 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001049 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001050 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001051 main.cleanAndExit()
Jon Halle8217482014-10-17 13:49:14 -04001052
kelvin-onlabd3b64892015-01-20 13:26:24 -08001053 def roles( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001054 """
Jon Hall983a1702014-10-28 18:44:22 -04001055 Lists all devices and the controllers with roles assigned to them
1056 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001057 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001058 """
andrewonlab7c211572014-10-15 16:45:20 -04001059 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07001060 cmdStr = "roles"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001061 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07001062 cmdStr += " -j"
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 Hallc6358dd2015-04-10 12:44:28 -07001066 return handle
Jon Hallc6793552016-01-19 14:18:37 -08001067 except AssertionError:
1068 main.log.exception( "" )
1069 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001070 except TypeError:
1071 main.log.exception( self.name + ": Object not as expected" )
1072 return None
Jon Hall983a1702014-10-28 18:44:22 -04001073 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001074 main.log.error( self.name + ": EOF exception found" )
1075 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001076 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001077 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001078 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001079 main.cleanAndExit()
Jon Hall983a1702014-10-28 18:44:22 -04001080
kelvin-onlabd3b64892015-01-20 13:26:24 -08001081 def getRole( self, deviceId ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08001082 """
Jon Halle3f39ff2015-01-13 11:50:53 -08001083 Given the a string containing the json representation of the "roles"
1084 cli command and a partial or whole device id, returns a json object
1085 containing the roles output for the first device whose id contains
1086 "device_id"
Jon Hall983a1702014-10-28 18:44:22 -04001087
1088 Returns:
Jon Halle3f39ff2015-01-13 11:50:53 -08001089 A dict of the role assignments for the given device or
1090 None if no match
kelvin8ec71442015-01-15 16:57:00 -08001091 """
Jon Hall983a1702014-10-28 18:44:22 -04001092 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001093 if deviceId is None:
Jon Hall983a1702014-10-28 18:44:22 -04001094 return None
1095 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001096 rawRoles = self.roles()
1097 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -08001098 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001099 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -08001100 # print device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001101 if str( deviceId ) in device[ 'id' ]:
Jon Hall983a1702014-10-28 18:44:22 -04001102 return device
1103 return None
Jon Hallc6793552016-01-19 14:18:37 -08001104 except ( TypeError, ValueError ):
1105 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) )
Jon Halld4d4b372015-01-28 16:02:41 -08001106 return None
andrewonlab86dc3082014-10-13 18:18:38 -04001107 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001108 main.log.error( self.name + ": EOF exception found" )
1109 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001110 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001111 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001112 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001113 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08001114
kelvin-onlabd3b64892015-01-20 13:26:24 -08001115 def rolesNotNull( self ):
kelvin8ec71442015-01-15 16:57:00 -08001116 """
Jon Hall94fd0472014-12-08 11:52:42 -08001117 Iterates through each device and checks if there is a master assigned
1118 Returns: main.TRUE if each device has a master
1119 main.FALSE any device has no master
kelvin8ec71442015-01-15 16:57:00 -08001120 """
Jon Hall94fd0472014-12-08 11:52:42 -08001121 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001122 rawRoles = self.roles()
1123 rolesJson = json.loads( rawRoles )
kelvin8ec71442015-01-15 16:57:00 -08001124 # search json for the device with id then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001125 for device in rolesJson:
kelvin8ec71442015-01-15 16:57:00 -08001126 # print device
1127 if device[ 'master' ] == "none":
1128 main.log.warn( "Device has no master: " + str( device ) )
Jon Hall94fd0472014-12-08 11:52:42 -08001129 return main.FALSE
1130 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08001131 except ( TypeError, ValueError ):
1132 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) )
Jon Halld4d4b372015-01-28 16:02:41 -08001133 return None
Jon Hall94fd0472014-12-08 11:52:42 -08001134 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001135 main.log.error( self.name + ": EOF exception found" )
1136 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001137 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001138 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001139 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001140 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08001141
kelvin-onlabd3b64892015-01-20 13:26:24 -08001142 def paths( self, srcId, dstId ):
kelvin8ec71442015-01-15 16:57:00 -08001143 """
andrewonlab3e15ead2014-10-15 14:21:34 -04001144 Returns string of paths, and the cost.
1145 Issues command: onos:paths <src> <dst>
kelvin8ec71442015-01-15 16:57:00 -08001146 """
andrewonlab3e15ead2014-10-15 14:21:34 -04001147 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001148 cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId )
1149 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08001150 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001151 assert "Command not found:" not in handle, handle
Jon Halle3f39ff2015-01-13 11:50:53 -08001152 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001153 main.log.error( self.name + ": Error in getting paths" )
kelvin8ec71442015-01-15 16:57:00 -08001154 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -04001155 else:
kelvin8ec71442015-01-15 16:57:00 -08001156 path = handle.split( ";" )[ 0 ]
1157 cost = handle.split( ";" )[ 1 ]
1158 return ( path, cost )
Jon Hallc6793552016-01-19 14:18:37 -08001159 except AssertionError:
1160 main.log.exception( "" )
1161 return ( handle, "Error" )
Jon Halld4d4b372015-01-28 16:02:41 -08001162 except TypeError:
1163 main.log.exception( self.name + ": Object not as expected" )
1164 return ( handle, "Error" )
andrewonlab3e15ead2014-10-15 14:21:34 -04001165 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001166 main.log.error( self.name + ": EOF exception found" )
1167 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001168 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001169 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001170 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001171 main.cleanAndExit()
Jon Hallffb386d2014-11-21 13:43:38 -08001172
kelvin-onlabd3b64892015-01-20 13:26:24 -08001173 def hosts( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08001174 """
Jon Hallffb386d2014-11-21 13:43:38 -08001175 Lists all discovered hosts
Jon Hall42db6dc2014-10-24 19:03:48 -04001176 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001177 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08001178 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001179 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07001180 cmdStr = "hosts"
kelvin-onlabd3b64892015-01-20 13:26:24 -08001181 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07001182 cmdStr += " -j"
1183 handle = self.sendline( cmdStr )
Jeremyd9e4eb12016-04-13 12:09:06 -07001184 if handle:
1185 assert "Command not found:" not in handle, handle
Jon Hallbaf53162015-12-17 17:04:34 -08001186 # TODO: Maybe make this less hardcoded
1187 # ConsistentMap Exceptions
1188 assert "org.onosproject.store.service" not in handle
1189 # Node not leader
1190 assert "java.lang.IllegalStateException" not in handle
Jon Hallc6358dd2015-04-10 12:44:28 -07001191 return handle
Jon Hallc6793552016-01-19 14:18:37 -08001192 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07001193 main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " +
Jeremy Songster6949cea2016-04-19 18:13:18 -07001194 "command: " + str( handle ) )
Jon Hallc6793552016-01-19 14:18:37 -08001195 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001196 except TypeError:
1197 main.log.exception( self.name + ": Object not as expected" )
1198 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001199 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001200 main.log.error( self.name + ": EOF exception found" )
1201 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001202 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001203 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001204 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001205 main.cleanAndExit()
Jon Hall42db6dc2014-10-24 19:03:48 -04001206
kelvin-onlabd3b64892015-01-20 13:26:24 -08001207 def getHost( self, mac ):
kelvin8ec71442015-01-15 16:57:00 -08001208 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001209 Return the first host from the hosts api whose 'id' contains 'mac'
Jon Halle3f39ff2015-01-13 11:50:53 -08001210
Jon Hallefbd9792015-03-05 16:11:36 -08001211 Note: mac must be a colon separated mac address, but could be a
Jon Halle3f39ff2015-01-13 11:50:53 -08001212 partial mac address
1213
Jon Hall42db6dc2014-10-24 19:03:48 -04001214 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08001215 """
Jon Hall42db6dc2014-10-24 19:03:48 -04001216 try:
kelvin8ec71442015-01-15 16:57:00 -08001217 if mac is None:
Jon Hall42db6dc2014-10-24 19:03:48 -04001218 return None
1219 else:
1220 mac = mac
kelvin-onlabd3b64892015-01-20 13:26:24 -08001221 rawHosts = self.hosts()
1222 hostsJson = json.loads( rawHosts )
kelvin8ec71442015-01-15 16:57:00 -08001223 # search json for the host with mac then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08001224 for host in hostsJson:
kelvin8ec71442015-01-15 16:57:00 -08001225 # print "%s in %s?" % ( mac, host[ 'id' ] )
Jon Halld4d4b372015-01-28 16:02:41 -08001226 if not host:
1227 pass
1228 elif mac in host[ 'id' ]:
Jon Hall42db6dc2014-10-24 19:03:48 -04001229 return host
1230 return None
Jon Hallc6793552016-01-19 14:18:37 -08001231 except ( TypeError, ValueError ):
1232 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) )
Jon Halld4d4b372015-01-28 16:02:41 -08001233 return None
Jon Hall42db6dc2014-10-24 19:03:48 -04001234 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001235 main.log.error( self.name + ": EOF exception found" )
1236 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001237 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001238 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001239 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001240 main.cleanAndExit()
Jon Hall42db6dc2014-10-24 19:03:48 -04001241
kelvin-onlabd3b64892015-01-20 13:26:24 -08001242 def getHostsId( self, hostList ):
kelvin8ec71442015-01-15 16:57:00 -08001243 """
1244 Obtain list of hosts
andrewonlab3f0a4af2014-10-17 12:25:14 -04001245 Issues command: 'onos> hosts'
kelvin8ec71442015-01-15 16:57:00 -08001246
andrewonlab3f0a4af2014-10-17 12:25:14 -04001247 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001248 * hostList: List of hosts obtained by Mininet
andrewonlab3f0a4af2014-10-17 12:25:14 -04001249 IMPORTANT:
1250 This function assumes that you started your
kelvin8ec71442015-01-15 16:57:00 -08001251 topology with the option '--mac'.
andrewonlab3f0a4af2014-10-17 12:25:14 -04001252 Furthermore, it assumes that value of VLAN is '-1'
1253 Description:
kelvin8ec71442015-01-15 16:57:00 -08001254 Converts mininet hosts ( h1, h2, h3... ) into
1255 ONOS format ( 00:00:00:00:00:01/-1 , ... )
1256 """
andrewonlab3f0a4af2014-10-17 12:25:14 -04001257 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001258 onosHostList = []
andrewonlab3f0a4af2014-10-17 12:25:14 -04001259
kelvin-onlabd3b64892015-01-20 13:26:24 -08001260 for host in hostList:
kelvin8ec71442015-01-15 16:57:00 -08001261 host = host.replace( "h", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001262 hostHex = hex( int( host ) ).zfill( 12 )
1263 hostHex = str( hostHex ).replace( 'x', '0' )
1264 i = iter( str( hostHex ) )
1265 hostHex = ":".join( a + b for a, b in zip( i, i ) )
1266 hostHex = hostHex + "/-1"
1267 onosHostList.append( hostHex )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001268
kelvin-onlabd3b64892015-01-20 13:26:24 -08001269 return onosHostList
andrewonlab3f0a4af2014-10-17 12:25:14 -04001270
Jon Halld4d4b372015-01-28 16:02:41 -08001271 except TypeError:
1272 main.log.exception( self.name + ": Object not as expected" )
1273 return None
andrewonlab3f0a4af2014-10-17 12:25:14 -04001274 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001275 main.log.error( self.name + ": EOF exception found" )
1276 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001277 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001278 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001279 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001280 main.cleanAndExit()
andrewonlab3e15ead2014-10-15 14:21:34 -04001281
You Wangbc898b82018-05-03 16:22:34 -07001282 def verifyHostLocation( self, hostIp, location ):
1283 """
1284 Description:
1285 Verify the host given is discovered in all locations expected
1286 Required:
1287 hostIp: IP address of the host
1288 location: expected location(s) of the given host. ex. "of:0000000000000005/8"
1289 Could be a string or list
1290 Returns:
1291 main.TRUE if host is discovered on all locations provided
1292 main.FALSE otherwise
1293 """
1294 import json
1295 locations = [ location ] if isinstance( location, str ) else location
1296 assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) )
1297 try:
1298 hosts = self.hosts()
1299 hosts = json.loads( hosts )
1300 targetHost = None
1301 for host in hosts:
1302 if hostIp in host[ "ipAddresses" ]:
1303 targetHost = host
You Wangfd80ab42018-05-10 17:21:53 -07001304 assert targetHost, "Not able to find host with IP {}".format( hostIp )
You Wangbc898b82018-05-03 16:22:34 -07001305 result = main.TRUE
1306 locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ]
1307 for loc in locations:
1308 discovered = False
1309 for locDiscovered in locationsDiscovered:
You Wang547893e2018-05-08 13:34:59 -07001310 locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0]
1311 if loc == locToMatch:
You Wangbc898b82018-05-03 16:22:34 -07001312 main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) )
You Wang547893e2018-05-08 13:34:59 -07001313 discovered = True
You Wangbc898b82018-05-03 16:22:34 -07001314 break
1315 if discovered:
1316 locationsDiscovered.remove( locDiscovered )
1317 else:
1318 main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) )
1319 result = main.FALSE
1320 if locationsDiscovered:
1321 main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) )
1322 result = main.FALSE
1323 return result
1324 except KeyError:
1325 main.log.exception( self.name + ": host data not as expected: " + hosts )
1326 return None
1327 except pexpect.EOF:
1328 main.log.error( self.name + ": EOF exception found" )
1329 main.log.error( self.name + ": " + self.handle.before )
1330 main.cleanAndExit()
1331 except Exception:
1332 main.log.exception( self.name + ": Uncaught exception" )
1333 return None
1334
You Wang53dba1e2018-02-02 17:45:44 -08001335 def verifyHostIp( self, hostList=[], prefix="" ):
1336 """
1337 Description:
1338 Verify that all hosts have IP address assigned to them
1339 Optional:
1340 hostList: If specified, verifications only happen to the hosts
1341 in hostList
1342 prefix: at least one of the ip address assigned to the host
1343 needs to have the specified prefix
1344 Returns:
1345 main.TRUE if all hosts have specific IP address assigned;
1346 main.FALSE otherwise
1347 """
1348 import json
1349 try:
1350 hosts = self.hosts()
1351 hosts = json.loads( hosts )
1352 if not hostList:
1353 hostList = [ host[ "id" ] for host in hosts ]
1354 for host in hosts:
1355 hostId = host[ "id" ]
1356 if hostId not in hostList:
1357 continue
1358 ipList = host[ "ipAddresses" ]
1359 main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) )
1360 if not ipList:
1361 main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) )
1362 else:
1363 if not any( ip.startswith( str( prefix ) ) for ip in ipList ):
1364 main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) )
1365 else:
1366 main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) )
1367 hostList.remove( hostId )
1368 if hostList:
1369 main.log.warn( self.name + ": failed to verify IP on following hosts: " + str( hostList) )
1370 return main.FALSE
1371 else:
1372 return main.TRUE
1373 except KeyError:
1374 main.log.exception( self.name + ": host data not as expected: " + hosts )
1375 return None
1376 except pexpect.EOF:
1377 main.log.error( self.name + ": EOF exception found" )
1378 main.log.error( self.name + ": " + self.handle.before )
1379 main.cleanAndExit()
1380 except Exception:
1381 main.log.exception( self.name + ": Uncaught exception" )
1382 return None
1383
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07001384 def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ):
kelvin8ec71442015-01-15 16:57:00 -08001385 """
andrewonlabe6745342014-10-17 14:29:13 -04001386 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001387 * hostIdOne: ONOS host id for host1
1388 * hostIdTwo: ONOS host id for host2
Jeremy Songster832f9e92016-05-05 14:30:49 -07001389 Optional:
1390 * vlanId: specify a VLAN id for the intent
Jeremy Songsterff553672016-05-12 17:06:23 -07001391 * setVlan: specify a VLAN id treatment
Jeremy Songsterc032f162016-08-04 17:14:49 -07001392 * encap: specify an encapsulation type
andrewonlabe6745342014-10-17 14:29:13 -04001393 Description:
Jon Hallefbd9792015-03-05 16:11:36 -08001394 Adds a host-to-host intent ( bidirectional ) by
Jon Hallb1290e82014-11-18 16:17:48 -05001395 specifying the two hosts.
kelvin-onlabfb521662015-02-27 09:52:40 -08001396 Returns:
1397 A string of the intent id or None on Error
kelvin8ec71442015-01-15 16:57:00 -08001398 """
andrewonlabe6745342014-10-17 14:29:13 -04001399 try:
Jeremy Songster832f9e92016-05-05 14:30:49 -07001400 cmdStr = "add-host-intent "
1401 if vlanId:
1402 cmdStr += "-v " + str( vlanId ) + " "
Jeremy Songsterff553672016-05-12 17:06:23 -07001403 if setVlan:
1404 cmdStr += "--setVlan " + str( vlanId ) + " "
Jeremy Songsterc032f162016-08-04 17:14:49 -07001405 if encap:
1406 cmdStr += "--encapsulation " + str( encap ) + " "
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07001407 if bandwidth:
1408 cmdStr += "-b " + str( bandwidth ) + " "
Jeremy Songster832f9e92016-05-05 14:30:49 -07001409 cmdStr += str( hostIdOne ) + " " + str( hostIdTwo )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001410 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08001411 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001412 assert "Command not found:" not in handle, handle
Hari Krishnaac4e1782015-01-26 12:09:12 -08001413 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001414 main.log.error( self.name + ": Error in adding Host intent" )
Jon Hall61282e32015-03-19 11:34:11 -07001415 main.log.debug( "Response from ONOS was: " + repr( handle ) )
kelvin-onlabfb521662015-02-27 09:52:40 -08001416 return None
Hari Krishnaac4e1782015-01-26 12:09:12 -08001417 else:
1418 main.log.info( "Host intent installed between " +
kelvin-onlabfb521662015-02-27 09:52:40 -08001419 str( hostIdOne ) + " and " + str( hostIdTwo ) )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001420 match = re.search( 'id=0x([\da-f]+),', handle )
kelvin-onlabfb521662015-02-27 09:52:40 -08001421 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001422 return match.group()[ 3:-1 ]
kelvin-onlabfb521662015-02-27 09:52:40 -08001423 else:
1424 main.log.error( "Error, intent ID not found" )
Jon Hall61282e32015-03-19 11:34:11 -07001425 main.log.debug( "Response from ONOS was: " +
1426 repr( handle ) )
kelvin-onlabfb521662015-02-27 09:52:40 -08001427 return None
Jon Hallc6793552016-01-19 14:18:37 -08001428 except AssertionError:
1429 main.log.exception( "" )
1430 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001431 except TypeError:
1432 main.log.exception( self.name + ": Object not as expected" )
1433 return None
andrewonlabe6745342014-10-17 14:29:13 -04001434 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001435 main.log.error( self.name + ": EOF exception found" )
1436 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001437 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001438 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001439 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001440 main.cleanAndExit()
andrewonlabe6745342014-10-17 14:29:13 -04001441
kelvin-onlabd3b64892015-01-20 13:26:24 -08001442 def addOpticalIntent( self, ingressDevice, egressDevice ):
kelvin8ec71442015-01-15 16:57:00 -08001443 """
andrewonlab7b31d232014-10-24 13:31:47 -04001444 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001445 * ingressDevice: device id of ingress device
1446 * egressDevice: device id of egress device
andrewonlab7b31d232014-10-24 13:31:47 -04001447 Optional:
1448 TODO: Still needs to be implemented via dev side
kelvin-onlabfb521662015-02-27 09:52:40 -08001449 Description:
1450 Adds an optical intent by specifying an ingress and egress device
1451 Returns:
1452 A string of the intent id or None on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08001453 """
andrewonlab7b31d232014-10-24 13:31:47 -04001454 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001455 cmdStr = "add-optical-intent " + str( ingressDevice ) +\
1456 " " + str( egressDevice )
1457 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08001458 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001459 assert "Command not found:" not in handle, handle
kelvin-onlab898a6c62015-01-16 14:13:53 -08001460 # If error, return error message
Jon Halle3f39ff2015-01-13 11:50:53 -08001461 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001462 main.log.error( self.name + ": Error in adding Optical intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001463 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001464 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001465 main.log.info( "Optical intent installed between " +
1466 str( ingressDevice ) + " and " +
1467 str( egressDevice ) )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001468 match = re.search( 'id=0x([\da-f]+),', handle )
kelvin-onlabfb521662015-02-27 09:52:40 -08001469 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001470 return match.group()[ 3:-1 ]
kelvin-onlabfb521662015-02-27 09:52:40 -08001471 else:
1472 main.log.error( "Error, intent ID not found" )
1473 return None
Jon Hallc6793552016-01-19 14:18:37 -08001474 except AssertionError:
1475 main.log.exception( "" )
1476 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001477 except TypeError:
1478 main.log.exception( self.name + ": Object not as expected" )
1479 return None
andrewonlab7b31d232014-10-24 13:31:47 -04001480 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001481 main.log.error( self.name + ": EOF exception found" )
1482 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001483 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001484 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001485 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001486 main.cleanAndExit()
andrewonlab7b31d232014-10-24 13:31:47 -04001487
kelvin-onlabd3b64892015-01-20 13:26:24 -08001488 def addPointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001489 self,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001490 ingressDevice,
1491 egressDevice,
1492 portIngress="",
1493 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001494 ethType="",
1495 ethSrc="",
1496 ethDst="",
1497 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001498 lambdaAlloc=False,
alisonda157272016-12-22 01:13:21 -08001499 protected=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001500 ipProto="",
1501 ipSrc="",
1502 ipDst="",
1503 tcpSrc="",
Jeremy Songster832f9e92016-05-05 14:30:49 -07001504 tcpDst="",
Jeremy Songsterff553672016-05-12 17:06:23 -07001505 vlanId="",
Jeremy Songsterc032f162016-08-04 17:14:49 -07001506 setVlan="",
1507 encap="" ):
kelvin8ec71442015-01-15 16:57:00 -08001508 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001509 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001510 * ingressDevice: device id of ingress device
1511 * egressDevice: device id of egress device
andrewonlab289e4b72014-10-21 21:24:18 -04001512 Optional:
1513 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001514 * ethSrc: specify ethSrc ( i.e. src mac addr )
1515 * ethDst: specify ethDst ( i.e. dst mac addr )
andrewonlab0dbb6ec2014-11-06 13:46:55 -05001516 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001517 * lambdaAlloc: if True, intent will allocate lambda
andrewonlab40ccd8b2014-11-06 16:23:34 -05001518 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001519 * ipProto: specify ip protocol
andrewonlabf77e0cb2014-11-11 17:17:59 -05001520 * ipSrc: specify ip source address
1521 * ipDst: specify ip destination address
1522 * tcpSrc: specify tcp source port
1523 * tcpDst: specify tcp destination port
Jeremy Songster832f9e92016-05-05 14:30:49 -07001524 * vlanId: specify vlan ID
Jeremy Songsterff553672016-05-12 17:06:23 -07001525 * setVlan: specify a VLAN id treatment
Jeremy Songsterc032f162016-08-04 17:14:49 -07001526 * encap: specify an Encapsulation type to use
andrewonlab4dbb4d82014-10-17 18:22:31 -04001527 Description:
kelvin8ec71442015-01-15 16:57:00 -08001528 Adds a point-to-point intent ( uni-directional ) by
andrewonlab289e4b72014-10-21 21:24:18 -04001529 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001530 Returns:
1531 A string of the intent id or None on error
andrewonlab289e4b72014-10-21 21:24:18 -04001532
Jon Halle3f39ff2015-01-13 11:50:53 -08001533 NOTE: This function may change depending on the
andrewonlab4dbb4d82014-10-17 18:22:31 -04001534 options developers provide for point-to-point
1535 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001536 """
andrewonlab4dbb4d82014-10-17 18:22:31 -04001537 try:
Jeremy Songsterff553672016-05-12 17:06:23 -07001538 cmd = "add-point-intent"
andrewonlab36af3822014-11-18 17:48:18 -05001539
Jeremy Songsterff553672016-05-12 17:06:23 -07001540 if ethType:
1541 cmd += " --ethType " + str( ethType )
1542 if ethSrc:
1543 cmd += " --ethSrc " + str( ethSrc )
1544 if ethDst:
1545 cmd += " --ethDst " + str( ethDst )
1546 if bandwidth:
1547 cmd += " --bandwidth " + str( bandwidth )
1548 if lambdaAlloc:
1549 cmd += " --lambda "
1550 if ipProto:
1551 cmd += " --ipProto " + str( ipProto )
1552 if ipSrc:
1553 cmd += " --ipSrc " + str( ipSrc )
1554 if ipDst:
1555 cmd += " --ipDst " + str( ipDst )
1556 if tcpSrc:
1557 cmd += " --tcpSrc " + str( tcpSrc )
1558 if tcpDst:
1559 cmd += " --tcpDst " + str( tcpDst )
1560 if vlanId:
1561 cmd += " -v " + str( vlanId )
1562 if setVlan:
1563 cmd += " --setVlan " + str( setVlan )
Jeremy Songsterc032f162016-08-04 17:14:49 -07001564 if encap:
1565 cmd += " --encapsulation " + str( encap )
alisonda157272016-12-22 01:13:21 -08001566 if protected:
1567 cmd += " --protect "
andrewonlab289e4b72014-10-21 21:24:18 -04001568
kelvin8ec71442015-01-15 16:57:00 -08001569 # Check whether the user appended the port
1570 # or provided it as an input
kelvin-onlabd3b64892015-01-20 13:26:24 -08001571 if "/" in ingressDevice:
1572 cmd += " " + str( ingressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001573 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001574 if not portIngress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001575 main.log.error( "You must specify the ingress port" )
kelvin8ec71442015-01-15 16:57:00 -08001576 # TODO: perhaps more meaningful return
kelvin-onlabfb521662015-02-27 09:52:40 -08001577 # Would it make sense to throw an exception and exit
1578 # the test?
1579 return None
andrewonlab36af3822014-11-18 17:48:18 -05001580
kelvin8ec71442015-01-15 16:57:00 -08001581 cmd += " " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -08001582 str( ingressDevice ) + "/" +\
1583 str( portIngress ) + " "
andrewonlab36af3822014-11-18 17:48:18 -05001584
kelvin-onlabd3b64892015-01-20 13:26:24 -08001585 if "/" in egressDevice:
1586 cmd += " " + str( egressDevice )
andrewonlab36af3822014-11-18 17:48:18 -05001587 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001588 if not portEgress:
kelvin-onlabfb521662015-02-27 09:52:40 -08001589 main.log.error( "You must specify the egress port" )
1590 return None
Jon Halle3f39ff2015-01-13 11:50:53 -08001591
kelvin8ec71442015-01-15 16:57:00 -08001592 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001593 str( egressDevice ) + "/" +\
1594 str( portEgress )
kelvin8ec71442015-01-15 16:57:00 -08001595
kelvin-onlab898a6c62015-01-16 14:13:53 -08001596 handle = self.sendline( cmd )
You Wangb5a55f72017-03-03 12:51:05 -08001597 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001598 assert "Command not found:" not in handle, handle
kelvin-onlabfb521662015-02-27 09:52:40 -08001599 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001600 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001601 main.log.error( self.name + ": Error in adding point-to-point intent" )
kelvin-onlabfb521662015-02-27 09:52:40 -08001602 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001603 else:
kelvin-onlabfb521662015-02-27 09:52:40 -08001604 # TODO: print out all the options in this message?
1605 main.log.info( "Point-to-point intent installed between " +
1606 str( ingressDevice ) + " and " +
1607 str( egressDevice ) )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001608 match = re.search( 'id=0x([\da-f]+),', handle )
kelvin-onlabfb521662015-02-27 09:52:40 -08001609 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001610 return match.group()[ 3:-1 ]
kelvin-onlabfb521662015-02-27 09:52:40 -08001611 else:
1612 main.log.error( "Error, intent ID not found" )
1613 return None
Jon Hallc6793552016-01-19 14:18:37 -08001614 except AssertionError:
1615 main.log.exception( "" )
1616 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001617 except TypeError:
1618 main.log.exception( self.name + ": Object not as expected" )
1619 return None
andrewonlab4dbb4d82014-10-17 18:22:31 -04001620 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001621 main.log.error( self.name + ": EOF exception found" )
1622 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001623 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001624 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001625 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001626 main.cleanAndExit()
andrewonlab4dbb4d82014-10-17 18:22:31 -04001627
kelvin-onlabd3b64892015-01-20 13:26:24 -08001628 def addMultipointToSinglepointIntent(
kelvin-onlab898a6c62015-01-16 14:13:53 -08001629 self,
shahshreyac2f97072015-03-19 17:04:29 -07001630 ingressDeviceList,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001631 egressDevice,
shahshreyac2f97072015-03-19 17:04:29 -07001632 portIngressList=None,
kelvin-onlabd3b64892015-01-20 13:26:24 -08001633 portEgress="",
kelvin-onlab898a6c62015-01-16 14:13:53 -08001634 ethType="",
1635 ethSrc="",
1636 ethDst="",
1637 bandwidth="",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001638 lambdaAlloc=False,
kelvin-onlab898a6c62015-01-16 14:13:53 -08001639 ipProto="",
1640 ipSrc="",
1641 ipDst="",
1642 tcpSrc="",
1643 tcpDst="",
1644 setEthSrc="",
Jeremy Songster832f9e92016-05-05 14:30:49 -07001645 setEthDst="",
Jeremy Songsterff553672016-05-12 17:06:23 -07001646 vlanId="",
Jeremy Songster9385d412016-06-02 17:57:36 -07001647 setVlan="",
Jeremy Songsterc032f162016-08-04 17:14:49 -07001648 partial=False,
1649 encap="" ):
kelvin8ec71442015-01-15 16:57:00 -08001650 """
shahshreyad0c80432014-12-04 16:56:05 -08001651 Note:
shahshreya70622b12015-03-19 17:19:00 -07001652 This function assumes the format of all ingress devices
Jon Hallbe379602015-03-24 13:39:32 -07001653 is same. That is, all ingress devices include port numbers
1654 with a "/" or all ingress devices could specify device
1655 ids and port numbers seperately.
shahshreyad0c80432014-12-04 16:56:05 -08001656 Required:
Jon Hallbe379602015-03-24 13:39:32 -07001657 * ingressDeviceList: List of device ids of ingress device
shahshreyac2f97072015-03-19 17:04:29 -07001658 ( Atleast 2 ingress devices required in the list )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001659 * egressDevice: device id of egress device
shahshreyad0c80432014-12-04 16:56:05 -08001660 Optional:
1661 * ethType: specify ethType
kelvin8ec71442015-01-15 16:57:00 -08001662 * ethSrc: specify ethSrc ( i.e. src mac addr )
1663 * ethDst: specify ethDst ( i.e. dst mac addr )
shahshreyad0c80432014-12-04 16:56:05 -08001664 * bandwidth: specify bandwidth capacity of link
kelvin-onlabd3b64892015-01-20 13:26:24 -08001665 * lambdaAlloc: if True, intent will allocate lambda
shahshreyad0c80432014-12-04 16:56:05 -08001666 for the specified intent
Jon Halle3f39ff2015-01-13 11:50:53 -08001667 * ipProto: specify ip protocol
shahshreyad0c80432014-12-04 16:56:05 -08001668 * ipSrc: specify ip source address
1669 * ipDst: specify ip destination address
1670 * tcpSrc: specify tcp source port
1671 * tcpDst: specify tcp destination port
1672 * setEthSrc: action to Rewrite Source MAC Address
1673 * setEthDst: action to Rewrite Destination MAC Address
Jeremy Songster832f9e92016-05-05 14:30:49 -07001674 * vlanId: specify vlan Id
Jeremy Songsterff553672016-05-12 17:06:23 -07001675 * setVlan: specify VLAN Id treatment
Jeremy Songsterc032f162016-08-04 17:14:49 -07001676 * encap: specify a type of encapsulation
shahshreyad0c80432014-12-04 16:56:05 -08001677 Description:
kelvin8ec71442015-01-15 16:57:00 -08001678 Adds a multipoint-to-singlepoint intent ( uni-directional ) by
shahshreyad0c80432014-12-04 16:56:05 -08001679 specifying device id's and optional fields
kelvin-onlabfb521662015-02-27 09:52:40 -08001680 Returns:
1681 A string of the intent id or None on error
shahshreyad0c80432014-12-04 16:56:05 -08001682
Jon Halle3f39ff2015-01-13 11:50:53 -08001683 NOTE: This function may change depending on the
Jon Hallefbd9792015-03-05 16:11:36 -08001684 options developers provide for multipoint-to-singlepoint
shahshreyad0c80432014-12-04 16:56:05 -08001685 intent via cli
kelvin8ec71442015-01-15 16:57:00 -08001686 """
shahshreyad0c80432014-12-04 16:56:05 -08001687 try:
Jeremy Songsterff553672016-05-12 17:06:23 -07001688 cmd = "add-multi-to-single-intent"
shahshreyad0c80432014-12-04 16:56:05 -08001689
Jeremy Songsterff553672016-05-12 17:06:23 -07001690 if ethType:
1691 cmd += " --ethType " + str( ethType )
1692 if ethSrc:
1693 cmd += " --ethSrc " + str( ethSrc )
1694 if ethDst:
1695 cmd += " --ethDst " + str( ethDst )
1696 if bandwidth:
1697 cmd += " --bandwidth " + str( bandwidth )
1698 if lambdaAlloc:
1699 cmd += " --lambda "
1700 if ipProto:
1701 cmd += " --ipProto " + str( ipProto )
1702 if ipSrc:
1703 cmd += " --ipSrc " + str( ipSrc )
1704 if ipDst:
1705 cmd += " --ipDst " + str( ipDst )
1706 if tcpSrc:
1707 cmd += " --tcpSrc " + str( tcpSrc )
1708 if tcpDst:
1709 cmd += " --tcpDst " + str( tcpDst )
1710 if setEthSrc:
1711 cmd += " --setEthSrc " + str( setEthSrc )
1712 if setEthDst:
1713 cmd += " --setEthDst " + str( setEthDst )
1714 if vlanId:
1715 cmd += " -v " + str( vlanId )
1716 if setVlan:
1717 cmd += " --setVlan " + str( setVlan )
Jeremy Songster9385d412016-06-02 17:57:36 -07001718 if partial:
1719 cmd += " --partial"
Jeremy Songsterc032f162016-08-04 17:14:49 -07001720 if encap:
1721 cmd += " --encapsulation " + str( encap )
shahshreyad0c80432014-12-04 16:56:05 -08001722
kelvin8ec71442015-01-15 16:57:00 -08001723 # Check whether the user appended the port
1724 # or provided it as an input
shahshreyac2f97072015-03-19 17:04:29 -07001725
1726 if portIngressList is None:
1727 for ingressDevice in ingressDeviceList:
1728 if "/" in ingressDevice:
1729 cmd += " " + str( ingressDevice )
1730 else:
1731 main.log.error( "You must specify " +
Jon Hallbe379602015-03-24 13:39:32 -07001732 "the ingress port" )
shahshreyac2f97072015-03-19 17:04:29 -07001733 # TODO: perhaps more meaningful return
1734 return main.FALSE
shahshreyad0c80432014-12-04 16:56:05 -08001735 else:
Jon Hall71ce4e72015-03-23 14:05:58 -07001736 if len( ingressDeviceList ) == len( portIngressList ):
Jon Hall08f61bc2015-04-13 16:00:30 -07001737 for ingressDevice, portIngress in zip( ingressDeviceList,
1738 portIngressList ):
shahshreya70622b12015-03-19 17:19:00 -07001739 cmd += " " + \
1740 str( ingressDevice ) + "/" +\
1741 str( portIngress ) + " "
kelvin-onlab38143812015-04-01 15:03:01 -07001742 else:
Jon Hall08f61bc2015-04-13 16:00:30 -07001743 main.log.error( "Device list and port list does not " +
1744 "have the same length" )
kelvin-onlab38143812015-04-01 15:03:01 -07001745 return main.FALSE
kelvin-onlabd3b64892015-01-20 13:26:24 -08001746 if "/" in egressDevice:
1747 cmd += " " + str( egressDevice )
shahshreyad0c80432014-12-04 16:56:05 -08001748 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001749 if not portEgress:
kelvin8ec71442015-01-15 16:57:00 -08001750 main.log.error( "You must specify " +
1751 "the egress port" )
shahshreyad0c80432014-12-04 16:56:05 -08001752 return main.FALSE
Jon Halle3f39ff2015-01-13 11:50:53 -08001753
kelvin8ec71442015-01-15 16:57:00 -08001754 cmd += " " +\
kelvin-onlabd3b64892015-01-20 13:26:24 -08001755 str( egressDevice ) + "/" +\
1756 str( portEgress )
kelvin-onlab898a6c62015-01-16 14:13:53 -08001757 handle = self.sendline( cmd )
You Wangb5a55f72017-03-03 12:51:05 -08001758 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001759 assert "Command not found:" not in handle, handle
kelvin-onlabfb521662015-02-27 09:52:40 -08001760 # If error, return error message
kelvin-onlab898a6c62015-01-16 14:13:53 -08001761 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001762 main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " +
kelvin-onlabfb521662015-02-27 09:52:40 -08001763 "intent" )
1764 return None
shahshreyad0c80432014-12-04 16:56:05 -08001765 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001766 match = re.search( 'id=0x([\da-f]+),', handle )
kelvin-onlabb9408212015-04-01 13:34:04 -07001767 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001768 return match.group()[ 3:-1 ]
kelvin-onlabb9408212015-04-01 13:34:04 -07001769 else:
1770 main.log.error( "Error, intent ID not found" )
1771 return None
Jon Hallc6793552016-01-19 14:18:37 -08001772 except AssertionError:
1773 main.log.exception( "" )
1774 return None
kelvin-onlabb9408212015-04-01 13:34:04 -07001775 except TypeError:
1776 main.log.exception( self.name + ": Object not as expected" )
1777 return None
1778 except pexpect.EOF:
1779 main.log.error( self.name + ": EOF exception found" )
1780 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001781 main.cleanAndExit()
kelvin-onlabb9408212015-04-01 13:34:04 -07001782 except Exception:
1783 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001784 main.cleanAndExit()
kelvin-onlabb9408212015-04-01 13:34:04 -07001785
1786 def addSinglepointToMultipointIntent(
1787 self,
1788 ingressDevice,
1789 egressDeviceList,
1790 portIngress="",
1791 portEgressList=None,
1792 ethType="",
1793 ethSrc="",
1794 ethDst="",
1795 bandwidth="",
1796 lambdaAlloc=False,
1797 ipProto="",
1798 ipSrc="",
1799 ipDst="",
1800 tcpSrc="",
1801 tcpDst="",
1802 setEthSrc="",
Jeremy Songster832f9e92016-05-05 14:30:49 -07001803 setEthDst="",
Jeremy Songsterff553672016-05-12 17:06:23 -07001804 vlanId="",
Jeremy Songster9385d412016-06-02 17:57:36 -07001805 setVlan="",
Jeremy Songsterc032f162016-08-04 17:14:49 -07001806 partial=False,
1807 encap="" ):
kelvin-onlabb9408212015-04-01 13:34:04 -07001808 """
1809 Note:
1810 This function assumes the format of all egress devices
1811 is same. That is, all egress devices include port numbers
1812 with a "/" or all egress devices could specify device
1813 ids and port numbers seperately.
1814 Required:
1815 * EgressDeviceList: List of device ids of egress device
1816 ( Atleast 2 eress devices required in the list )
1817 * ingressDevice: device id of ingress device
1818 Optional:
1819 * ethType: specify ethType
1820 * ethSrc: specify ethSrc ( i.e. src mac addr )
1821 * ethDst: specify ethDst ( i.e. dst mac addr )
1822 * bandwidth: specify bandwidth capacity of link
1823 * lambdaAlloc: if True, intent will allocate lambda
1824 for the specified intent
1825 * ipProto: specify ip protocol
1826 * ipSrc: specify ip source address
1827 * ipDst: specify ip destination address
1828 * tcpSrc: specify tcp source port
1829 * tcpDst: specify tcp destination port
1830 * setEthSrc: action to Rewrite Source MAC Address
1831 * setEthDst: action to Rewrite Destination MAC Address
Jeremy Songster832f9e92016-05-05 14:30:49 -07001832 * vlanId: specify vlan Id
Jeremy Songsterff553672016-05-12 17:06:23 -07001833 * setVlan: specify VLAN ID treatment
Jeremy Songsterc032f162016-08-04 17:14:49 -07001834 * encap: specify an encapsulation type
kelvin-onlabb9408212015-04-01 13:34:04 -07001835 Description:
1836 Adds a singlepoint-to-multipoint intent ( uni-directional ) by
1837 specifying device id's and optional fields
1838 Returns:
1839 A string of the intent id or None on error
1840
1841 NOTE: This function may change depending on the
1842 options developers provide for singlepoint-to-multipoint
1843 intent via cli
1844 """
1845 try:
Jeremy Songsterff553672016-05-12 17:06:23 -07001846 cmd = "add-single-to-multi-intent"
kelvin-onlabb9408212015-04-01 13:34:04 -07001847
Jeremy Songsterff553672016-05-12 17:06:23 -07001848 if ethType:
1849 cmd += " --ethType " + str( ethType )
1850 if ethSrc:
1851 cmd += " --ethSrc " + str( ethSrc )
1852 if ethDst:
1853 cmd += " --ethDst " + str( ethDst )
1854 if bandwidth:
1855 cmd += " --bandwidth " + str( bandwidth )
1856 if lambdaAlloc:
1857 cmd += " --lambda "
1858 if ipProto:
1859 cmd += " --ipProto " + str( ipProto )
1860 if ipSrc:
1861 cmd += " --ipSrc " + str( ipSrc )
1862 if ipDst:
1863 cmd += " --ipDst " + str( ipDst )
1864 if tcpSrc:
1865 cmd += " --tcpSrc " + str( tcpSrc )
1866 if tcpDst:
1867 cmd += " --tcpDst " + str( tcpDst )
1868 if setEthSrc:
1869 cmd += " --setEthSrc " + str( setEthSrc )
1870 if setEthDst:
1871 cmd += " --setEthDst " + str( setEthDst )
1872 if vlanId:
1873 cmd += " -v " + str( vlanId )
1874 if setVlan:
1875 cmd += " --setVlan " + str( setVlan )
Jeremy Songster9385d412016-06-02 17:57:36 -07001876 if partial:
1877 cmd += " --partial"
Jeremy Songsterc032f162016-08-04 17:14:49 -07001878 if encap:
1879 cmd += " --encapsulation " + str( encap )
kelvin-onlabb9408212015-04-01 13:34:04 -07001880
1881 # Check whether the user appended the port
1882 # or provided it as an input
Jon Hall08f61bc2015-04-13 16:00:30 -07001883
kelvin-onlabb9408212015-04-01 13:34:04 -07001884 if "/" in ingressDevice:
1885 cmd += " " + str( ingressDevice )
1886 else:
1887 if not portIngress:
1888 main.log.error( "You must specify " +
1889 "the Ingress port" )
1890 return main.FALSE
1891
1892 cmd += " " +\
1893 str( ingressDevice ) + "/" +\
1894 str( portIngress )
1895
1896 if portEgressList is None:
1897 for egressDevice in egressDeviceList:
1898 if "/" in egressDevice:
1899 cmd += " " + str( egressDevice )
1900 else:
1901 main.log.error( "You must specify " +
1902 "the egress port" )
1903 # TODO: perhaps more meaningful return
1904 return main.FALSE
1905 else:
1906 if len( egressDeviceList ) == len( portEgressList ):
Jon Hall08f61bc2015-04-13 16:00:30 -07001907 for egressDevice, portEgress in zip( egressDeviceList,
1908 portEgressList ):
kelvin-onlabb9408212015-04-01 13:34:04 -07001909 cmd += " " + \
1910 str( egressDevice ) + "/" +\
1911 str( portEgress )
kelvin-onlab38143812015-04-01 15:03:01 -07001912 else:
Jon Hall08f61bc2015-04-13 16:00:30 -07001913 main.log.error( "Device list and port list does not " +
1914 "have the same length" )
kelvin-onlab38143812015-04-01 15:03:01 -07001915 return main.FALSE
kelvin-onlabb9408212015-04-01 13:34:04 -07001916 handle = self.sendline( cmd )
You Wangb5a55f72017-03-03 12:51:05 -08001917 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08001918 assert "Command not found:" not in handle, handle
kelvin-onlabb9408212015-04-01 13:34:04 -07001919 # If error, return error message
1920 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07001921 main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " +
kelvin-onlabb9408212015-04-01 13:34:04 -07001922 "intent" )
shahshreyac2f97072015-03-19 17:04:29 -07001923 return None
kelvin-onlabb9408212015-04-01 13:34:04 -07001924 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001925 match = re.search( 'id=0x([\da-f]+),', handle )
kelvin-onlabb9408212015-04-01 13:34:04 -07001926 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001927 return match.group()[ 3:-1 ]
kelvin-onlabb9408212015-04-01 13:34:04 -07001928 else:
1929 main.log.error( "Error, intent ID not found" )
1930 return None
Jon Hallc6793552016-01-19 14:18:37 -08001931 except AssertionError:
1932 main.log.exception( "" )
1933 return None
Jon Halld4d4b372015-01-28 16:02:41 -08001934 except TypeError:
1935 main.log.exception( self.name + ": Object not as expected" )
1936 return None
shahshreyad0c80432014-12-04 16:56:05 -08001937 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001938 main.log.error( self.name + ": EOF exception found" )
1939 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001940 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001941 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08001942 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001943 main.cleanAndExit()
shahshreyad0c80432014-12-04 16:56:05 -08001944
Hari Krishna9e232602015-04-13 17:29:08 -07001945 def addMplsIntent(
1946 self,
1947 ingressDevice,
1948 egressDevice,
Hari Krishna87a17f12015-04-13 17:42:23 -07001949 ingressPort="",
1950 egressPort="",
Hari Krishna9e232602015-04-13 17:29:08 -07001951 ethType="",
1952 ethSrc="",
1953 ethDst="",
1954 bandwidth="",
1955 lambdaAlloc=False,
1956 ipProto="",
1957 ipSrc="",
1958 ipDst="",
1959 tcpSrc="",
1960 tcpDst="",
Hari Krishna87a17f12015-04-13 17:42:23 -07001961 ingressLabel="",
Hari Krishnadfff6672015-04-13 17:53:27 -07001962 egressLabel="",
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001963 priority="" ):
Hari Krishna9e232602015-04-13 17:29:08 -07001964 """
1965 Required:
1966 * ingressDevice: device id of ingress device
1967 * egressDevice: device id of egress device
1968 Optional:
1969 * ethType: specify ethType
1970 * ethSrc: specify ethSrc ( i.e. src mac addr )
1971 * ethDst: specify ethDst ( i.e. dst mac addr )
1972 * bandwidth: specify bandwidth capacity of link
1973 * lambdaAlloc: if True, intent will allocate lambda
1974 for the specified intent
1975 * ipProto: specify ip protocol
1976 * ipSrc: specify ip source address
1977 * ipDst: specify ip destination address
1978 * tcpSrc: specify tcp source port
1979 * tcpDst: specify tcp destination port
1980 * ingressLabel: Ingress MPLS label
1981 * egressLabel: Egress MPLS label
1982 Description:
1983 Adds MPLS intent by
1984 specifying device id's and optional fields
1985 Returns:
1986 A string of the intent id or None on error
1987
1988 NOTE: This function may change depending on the
1989 options developers provide for MPLS
1990 intent via cli
1991 """
1992 try:
Jeremy Songsterff553672016-05-12 17:06:23 -07001993 cmd = "add-mpls-intent"
Hari Krishna9e232602015-04-13 17:29:08 -07001994
Jeremy Songsterff553672016-05-12 17:06:23 -07001995 if ethType:
1996 cmd += " --ethType " + str( ethType )
1997 if ethSrc:
1998 cmd += " --ethSrc " + str( ethSrc )
1999 if ethDst:
2000 cmd += " --ethDst " + str( ethDst )
2001 if bandwidth:
2002 cmd += " --bandwidth " + str( bandwidth )
2003 if lambdaAlloc:
2004 cmd += " --lambda "
2005 if ipProto:
2006 cmd += " --ipProto " + str( ipProto )
2007 if ipSrc:
2008 cmd += " --ipSrc " + str( ipSrc )
2009 if ipDst:
2010 cmd += " --ipDst " + str( ipDst )
2011 if tcpSrc:
2012 cmd += " --tcpSrc " + str( tcpSrc )
2013 if tcpDst:
2014 cmd += " --tcpDst " + str( tcpDst )
2015 if ingressLabel:
2016 cmd += " --ingressLabel " + str( ingressLabel )
2017 if egressLabel:
2018 cmd += " --egressLabel " + str( egressLabel )
2019 if priority:
2020 cmd += " --priority " + str( priority )
Hari Krishna9e232602015-04-13 17:29:08 -07002021
2022 # Check whether the user appended the port
2023 # or provided it as an input
2024 if "/" in ingressDevice:
2025 cmd += " " + str( ingressDevice )
2026 else:
Hari Krishna87a17f12015-04-13 17:42:23 -07002027 if not ingressPort:
Hari Krishna9e232602015-04-13 17:29:08 -07002028 main.log.error( "You must specify the ingress port" )
2029 return None
2030
2031 cmd += " " + \
2032 str( ingressDevice ) + "/" +\
Hari Krishna87a17f12015-04-13 17:42:23 -07002033 str( ingressPort ) + " "
Hari Krishna9e232602015-04-13 17:29:08 -07002034
2035 if "/" in egressDevice:
2036 cmd += " " + str( egressDevice )
2037 else:
Hari Krishna87a17f12015-04-13 17:42:23 -07002038 if not egressPort:
Hari Krishna9e232602015-04-13 17:29:08 -07002039 main.log.error( "You must specify the egress port" )
2040 return None
2041
2042 cmd += " " +\
2043 str( egressDevice ) + "/" +\
Hari Krishna87a17f12015-04-13 17:42:23 -07002044 str( egressPort )
Hari Krishna9e232602015-04-13 17:29:08 -07002045
2046 handle = self.sendline( cmd )
You Wangb5a55f72017-03-03 12:51:05 -08002047 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002048 assert "Command not found:" not in handle, handle
Hari Krishna9e232602015-04-13 17:29:08 -07002049 # If error, return error message
2050 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07002051 main.log.error( self.name + ": Error in adding mpls intent" )
Hari Krishna9e232602015-04-13 17:29:08 -07002052 return None
2053 else:
2054 # TODO: print out all the options in this message?
2055 main.log.info( "MPLS intent installed between " +
2056 str( ingressDevice ) + " and " +
2057 str( egressDevice ) )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002058 match = re.search( 'id=0x([\da-f]+),', handle )
Hari Krishna9e232602015-04-13 17:29:08 -07002059 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002060 return match.group()[ 3:-1 ]
Hari Krishna9e232602015-04-13 17:29:08 -07002061 else:
2062 main.log.error( "Error, intent ID not found" )
2063 return None
Jon Hallc6793552016-01-19 14:18:37 -08002064 except AssertionError:
2065 main.log.exception( "" )
2066 return None
Hari Krishna9e232602015-04-13 17:29:08 -07002067 except TypeError:
2068 main.log.exception( self.name + ": Object not as expected" )
2069 return None
2070 except pexpect.EOF:
2071 main.log.error( self.name + ": EOF exception found" )
2072 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002073 main.cleanAndExit()
Hari Krishna9e232602015-04-13 17:29:08 -07002074 except Exception:
2075 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002076 main.cleanAndExit()
Hari Krishna9e232602015-04-13 17:29:08 -07002077
Jon Hallefbd9792015-03-05 16:11:36 -08002078 def removeIntent( self, intentId, app='org.onosproject.cli',
2079 purge=False, sync=False ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08002080 """
shahshreya1c818fc2015-02-26 13:44:08 -08002081 Remove intent for specified application id and intent id
Jon Hall61282e32015-03-19 11:34:11 -07002082 Optional args:-
shahshreya1c818fc2015-02-26 13:44:08 -08002083 -s or --sync: Waits for the removal before returning
Jon Hall61282e32015-03-19 11:34:11 -07002084 -p or --purge: Purge the intent from the store after removal
2085
Jon Halle3f39ff2015-01-13 11:50:53 -08002086 Returns:
Jon Hall6509dbf2016-06-21 17:01:17 -07002087 main.FALSE on error and
Jon Halle3f39ff2015-01-13 11:50:53 -08002088 cli output otherwise
kelvin-onlab898a6c62015-01-16 14:13:53 -08002089 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04002090 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002091 cmdStr = "remove-intent"
shahshreya1c818fc2015-02-26 13:44:08 -08002092 if purge:
2093 cmdStr += " -p"
2094 if sync:
2095 cmdStr += " -s"
2096
2097 cmdStr += " " + app + " " + str( intentId )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002098 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002099 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002100 assert "Command not found:" not in handle, handle
Jon Halle3f39ff2015-01-13 11:50:53 -08002101 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07002102 main.log.error( self.name + ": Error in removing intent" )
Jon Halle3f39ff2015-01-13 11:50:53 -08002103 return main.FALSE
andrewonlab9a50dfe2014-10-17 17:22:31 -04002104 else:
Jon Halle3f39ff2015-01-13 11:50:53 -08002105 # TODO: Should this be main.TRUE
2106 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002107 except AssertionError:
2108 main.log.exception( "" )
2109 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002110 except TypeError:
2111 main.log.exception( self.name + ": Object not as expected" )
2112 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04002113 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002114 main.log.error( self.name + ": EOF exception found" )
2115 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002116 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002117 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002118 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002119 main.cleanAndExit()
andrewonlab9a50dfe2014-10-17 17:22:31 -04002120
YPZhangfebf7302016-05-24 16:45:56 -07002121 def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ):
Jeremy42df2e72016-02-23 16:37:46 -08002122 """
2123 Description:
2124 Remove all the intents
2125 Optional args:-
2126 -s or --sync: Waits for the removal before returning
2127 -p or --purge: Purge the intent from the store after removal
2128 Returns:
2129 Returns main.TRUE if all intents are removed, otherwise returns
2130 main.FALSE; Returns None for exception
2131 """
2132 try:
2133 cmdStr = "remove-intent"
2134 if purge:
2135 cmdStr += " -p"
2136 if sync:
2137 cmdStr += " -s"
2138
2139 cmdStr += " " + app
YPZhangfebf7302016-05-24 16:45:56 -07002140 handle = self.sendline( cmdStr, timeout=timeout )
You Wangb5a55f72017-03-03 12:51:05 -08002141 assert handle is not None, "Error in sendline"
Jeremy42df2e72016-02-23 16:37:46 -08002142 assert "Command not found:" not in handle, handle
2143 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07002144 main.log.error( self.name + ": Error in removing intent" )
Jeremy42df2e72016-02-23 16:37:46 -08002145 return main.FALSE
2146 else:
2147 return main.TRUE
2148 except AssertionError:
2149 main.log.exception( "" )
2150 return None
2151 except TypeError:
2152 main.log.exception( self.name + ": Object not as expected" )
2153 return None
2154 except pexpect.EOF:
2155 main.log.error( self.name + ": EOF exception found" )
2156 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002157 main.cleanAndExit()
Jeremy42df2e72016-02-23 16:37:46 -08002158 except Exception:
2159 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002160 main.cleanAndExit()
Jeremy42df2e72016-02-23 16:37:46 -08002161
Hari Krishnaacabd5a2015-07-01 17:10:19 -07002162 def purgeWithdrawnIntents( self ):
Hari Krishna0ce0e152015-06-23 09:55:29 -07002163 """
2164 Purges all WITHDRAWN Intents
2165 """
2166 try:
2167 cmdStr = "purge-intents"
2168 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
Hari Krishna0ce0e152015-06-23 09:55:29 -07002171 if re.search( "Error", handle ):
Jon Hall0e240372018-05-02 11:21:57 -07002172 main.log.error( self.name + ": Error in purging intents" )
Hari Krishna0ce0e152015-06-23 09:55:29 -07002173 return main.FALSE
2174 else:
2175 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08002176 except AssertionError:
2177 main.log.exception( "" )
2178 return None
Hari Krishna0ce0e152015-06-23 09:55:29 -07002179 except TypeError:
2180 main.log.exception( self.name + ": Object not as expected" )
2181 return None
2182 except pexpect.EOF:
2183 main.log.error( self.name + ": EOF exception found" )
2184 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002185 main.cleanAndExit()
Hari Krishna0ce0e152015-06-23 09:55:29 -07002186 except Exception:
2187 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002188 main.cleanAndExit()
Hari Krishna0ce0e152015-06-23 09:55:29 -07002189
Devin Lime6fe3c42017-10-18 16:28:40 -07002190 def wipeout( self ):
2191 """
2192 Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS.
2193 """
2194 try:
2195 cmdStr = "wipe-out please"
2196 handle = self.sendline( cmdStr, timeout=60 )
2197 assert handle is not None, "Error in sendline"
2198 assert "Command not found:" not in handle, handle
2199 return main.TRUE
2200 except AssertionError:
2201 main.log.exception( "" )
2202 return None
2203 except TypeError:
2204 main.log.exception( self.name + ": Object not as expected" )
2205 return None
2206 except pexpect.EOF:
2207 main.log.error( self.name + ": EOF exception found" )
2208 main.log.error( self.name + ": " + self.handle.before )
2209 main.cleanAndExit()
2210 except Exception:
2211 main.log.exception( self.name + ": Uncaught exception!" )
2212 main.cleanAndExit()
2213
kelvin-onlabd3b64892015-01-20 13:26:24 -08002214 def routes( self, jsonFormat=False ):
kelvin8ec71442015-01-15 16:57:00 -08002215 """
kelvin-onlab898a6c62015-01-16 14:13:53 -08002216 NOTE: This method should be used after installing application:
2217 onos-app-sdnip
pingping-lin8b306ac2014-11-17 18:13:51 -08002218 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002219 * jsonFormat: enable output formatting in json
pingping-lin8b306ac2014-11-17 18:13:51 -08002220 Description:
2221 Obtain all routes in the system
kelvin8ec71442015-01-15 16:57:00 -08002222 """
pingping-lin8b306ac2014-11-17 18:13:51 -08002223 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002224 cmdStr = "routes"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002225 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002226 cmdStr += " -j"
2227 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002228 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002229 assert "Command not found:" not in handle, handle
pingping-lin8b306ac2014-11-17 18:13:51 -08002230 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002231 except AssertionError:
2232 main.log.exception( "" )
2233 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002234 except TypeError:
2235 main.log.exception( self.name + ": Object not as expected" )
2236 return None
pingping-lin8b306ac2014-11-17 18:13:51 -08002237 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002238 main.log.error( self.name + ": EOF exception found" )
2239 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002240 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002241 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002242 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002243 main.cleanAndExit()
pingping-lin8b306ac2014-11-17 18:13:51 -08002244
pingping-lin54b03372015-08-13 14:43:10 -07002245 def ipv4RouteNumber( self ):
2246 """
2247 NOTE: This method should be used after installing application:
2248 onos-app-sdnip
2249 Description:
2250 Obtain the total IPv4 routes number in the system
2251 """
2252 try:
Pratik Parab57963572017-05-09 11:37:54 -07002253 cmdStr = "routes -j"
pingping-lin54b03372015-08-13 14:43:10 -07002254 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002255 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002256 assert "Command not found:" not in handle, handle
pingping-lin54b03372015-08-13 14:43:10 -07002257 jsonResult = json.loads( handle )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002258 return len( jsonResult[ 'routes4' ] )
Jon Hallc6793552016-01-19 14:18:37 -08002259 except AssertionError:
2260 main.log.exception( "" )
2261 return None
2262 except ( TypeError, ValueError ):
2263 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) )
pingping-lin54b03372015-08-13 14:43:10 -07002264 return None
2265 except pexpect.EOF:
2266 main.log.error( self.name + ": EOF exception found" )
2267 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002268 main.cleanAndExit()
pingping-lin54b03372015-08-13 14:43:10 -07002269 except Exception:
2270 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002271 main.cleanAndExit()
pingping-lin54b03372015-08-13 14:43:10 -07002272
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002273 # =============Function to check Bandwidth allocation========
Jon Hall0e240372018-05-02 11:21:57 -07002274 def allocations( self, jsonFormat = True ):
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07002275 """
2276 Description:
2277 Obtain Bandwidth Allocation Information from ONOS cli.
2278 """
2279 try:
2280 cmdStr = "allocations"
2281 if jsonFormat:
2282 cmdStr += " -j"
Jon Hall0e240372018-05-02 11:21:57 -07002283 handle = self.sendline( cmdStr, timeout=300 )
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07002284 assert handle is not None, "Error in sendline"
2285 assert "Command not found:" not in handle, handle
2286 return handle
2287 except AssertionError:
2288 main.log.exception( "" )
2289 return None
2290 except ( TypeError, ValueError ):
2291 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) )
2292 return None
2293 except pexpect.EOF:
2294 main.log.error( self.name + ": EOF exception found" )
2295 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002296 main.cleanAndExit()
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07002297 except Exception:
2298 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002299 main.cleanAndExit()
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07002300
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002301 def intents( self, jsonFormat = True, summary = False, **intentargs ):
kelvin8ec71442015-01-15 16:57:00 -08002302 """
andrewonlabe6745342014-10-17 14:29:13 -04002303 Description:
Jon Hallff566d52016-01-15 14:45:36 -08002304 Obtain intents from the ONOS cli.
2305 Optional:
2306 * jsonFormat: Enable output formatting in json, default to True
2307 * summary: Whether only output the intent summary, defaults to False
2308 * type: Only output a certain type of intent. This options is valid
2309 only when jsonFormat is True and summary is True.
kelvin-onlab898a6c62015-01-16 14:13:53 -08002310 """
andrewonlabe6745342014-10-17 14:29:13 -04002311 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002312 cmdStr = "intents"
pingping-lin8244a3b2015-09-16 13:36:56 -07002313 if summary:
2314 cmdStr += " -s"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002315 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002316 cmdStr += " -j"
Shreya Chowdhary6fbb96c2017-05-02 16:20:19 -07002317 handle = self.sendline( cmdStr, timeout=300 )
You Wangb5a55f72017-03-03 12:51:05 -08002318 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002319 assert "Command not found:" not in handle, handle
pingping-lin8244a3b2015-09-16 13:36:56 -07002320 args = utilities.parse_args( [ "TYPE" ], **intentargs )
acsmars5b5fbaf2015-09-18 10:38:20 -07002321 if "TYPE" in args.keys():
Jon Hallff566d52016-01-15 14:45:36 -08002322 intentType = args[ "TYPE" ]
acsmars5b5fbaf2015-09-18 10:38:20 -07002323 else:
Jon Hallff566d52016-01-15 14:45:36 -08002324 intentType = ""
2325 # IF we want the summary of a specific intent type
2326 if jsonFormat and summary and ( intentType != "" ):
pingping-lin8244a3b2015-09-16 13:36:56 -07002327 jsonResult = json.loads( handle )
Jon Hallff566d52016-01-15 14:45:36 -08002328 if intentType in jsonResult.keys():
2329 return jsonResult[ intentType ]
pingping-lin8244a3b2015-09-16 13:36:56 -07002330 else:
Jon Hallff566d52016-01-15 14:45:36 -08002331 main.log.error( "unknown TYPE, returning all types of intents" )
pingping-lin8244a3b2015-09-16 13:36:56 -07002332 return handle
2333 else:
2334 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002335 except AssertionError:
2336 main.log.exception( "" )
2337 return None
2338 except ( TypeError, ValueError ):
2339 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) )
pingping-lin54b03372015-08-13 14:43:10 -07002340 return None
2341 except pexpect.EOF:
2342 main.log.error( self.name + ": EOF exception found" )
2343 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002344 main.cleanAndExit()
pingping-lin54b03372015-08-13 14:43:10 -07002345 except Exception:
2346 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002347 main.cleanAndExit()
pingping-lin54b03372015-08-13 14:43:10 -07002348
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002349 def getIntentState( self, intentsId, intentsJson=None ):
kelvin-onlab54400a92015-02-26 18:05:51 -08002350 """
You Wangfdcbfc42016-05-16 12:16:53 -07002351 Description:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002352 Gets intent state. Accepts a single intent ID (string type) or a
You Wangfdcbfc42016-05-16 12:16:53 -07002353 list of intent IDs.
2354 Parameters:
2355 intentsId: intent ID, both string type and list type are acceptable
kelvin-onlab54400a92015-02-26 18:05:51 -08002356 intentsJson: parsed json object from the onos:intents api
You Wangfdcbfc42016-05-16 12:16:53 -07002357 Returns:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002358 Returns the state (string type) of the ID if a single intent ID is
You Wangfdcbfc42016-05-16 12:16:53 -07002359 accepted.
2360 Returns a list of dictionaries if a list of intent IDs is accepted,
2361 and each dictionary maps 'id' to the Intent ID and 'state' to
2362 corresponding intent state.
kelvin-onlab54400a92015-02-26 18:05:51 -08002363 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002364
kelvin-onlab54400a92015-02-26 18:05:51 -08002365 try:
2366 state = "State is Undefined"
2367 if not intentsJson:
Jon Hallc6793552016-01-19 14:18:37 -08002368 rawJson = self.intents()
kelvin-onlab54400a92015-02-26 18:05:51 -08002369 else:
Jon Hallc6793552016-01-19 14:18:37 -08002370 rawJson = intentsJson
2371 parsedIntentsJson = json.loads( rawJson )
Jon Hallefbd9792015-03-05 16:11:36 -08002372 if isinstance( intentsId, types.StringType ):
Jon Hallc6793552016-01-19 14:18:37 -08002373 for intent in parsedIntentsJson:
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002374 if intentsId == intent[ 'id' ]:
2375 state = intent[ 'state' ]
kelvin-onlab54400a92015-02-26 18:05:51 -08002376 return state
Jon Hallefbd9792015-03-05 16:11:36 -08002377 main.log.info( "Cannot find intent ID" + str( intentsId ) +
Jon Hall53158082017-05-18 11:17:00 -07002378 " in the list" )
kelvin-onlab54400a92015-02-26 18:05:51 -08002379 return state
Jon Hallefbd9792015-03-05 16:11:36 -08002380 elif isinstance( intentsId, types.ListType ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08002381 dictList = []
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002382 for i in xrange( len( intentsId ) ):
kelvin-onlab07dbd012015-03-04 16:29:39 -08002383 stateDict = {}
Jon Hall53158082017-05-18 11:17:00 -07002384 for intent in parsedIntentsJson:
2385 if intentsId[ i ] == intent[ 'id' ]:
2386 stateDict[ 'state' ] = intent[ 'state' ]
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002387 stateDict[ 'id' ] = intentsId[ i ]
Jon Hallefbd9792015-03-05 16:11:36 -08002388 dictList.append( stateDict )
kelvin-onlab54400a92015-02-26 18:05:51 -08002389 break
Jon Hallefbd9792015-03-05 16:11:36 -08002390 if len( intentsId ) != len( dictList ):
Jon Hall53158082017-05-18 11:17:00 -07002391 main.log.warn( "Could not find all intents in ONOS output" )
2392 main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) )
kelvin-onlab07dbd012015-03-04 16:29:39 -08002393 return dictList
kelvin-onlab54400a92015-02-26 18:05:51 -08002394 else:
Jon Hall53158082017-05-18 11:17:00 -07002395 main.log.info( "Invalid type for intentsId argument" )
kelvin-onlab54400a92015-02-26 18:05:51 -08002396 return None
Jon Hallc6793552016-01-19 14:18:37 -08002397 except ( TypeError, ValueError ):
2398 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) )
kelvin-onlab54400a92015-02-26 18:05:51 -08002399 return None
2400 except pexpect.EOF:
2401 main.log.error( self.name + ": EOF exception found" )
2402 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002403 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002404 except Exception:
kelvin-onlab54400a92015-02-26 18:05:51 -08002405 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002406 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07002407
Jon Hallf539eb92017-05-22 17:18:42 -07002408 def checkIntentState( self, intentsId, expectedState='INSTALLED' ):
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002409 """
2410 Description:
2411 Check intents state
2412 Required:
2413 intentsId - List of intents ID to be checked
2414 Optional:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002415 expectedState - Check the expected state(s) of each intents
kelvin-onlabf512e942015-06-08 19:42:59 -07002416 state in the list.
2417 *NOTE: You can pass in a list of expected state,
2418 Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ]
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002419 Return:
Jon Hall53158082017-05-18 11:17:00 -07002420 Returns main.TRUE only if all intent are the same as expected states,
2421 otherwise returns main.FALSE.
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002422 """
2423 try:
kelvin-onlabf512e942015-06-08 19:42:59 -07002424 returnValue = main.TRUE
Jon Hallf539eb92017-05-22 17:18:42 -07002425 # Generating a dictionary: intent id as a key and state as value
Devin Lim752dd7b2017-06-27 14:40:03 -07002426
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002427 # intentsDict = self.getIntentState( intentsId )
Devin Lim752dd7b2017-06-27 14:40:03 -07002428 intentsDict = []
2429 for intent in json.loads( self.intents() ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002430 if isinstance( intentsId, types.StringType ) \
2431 and intent.get( 'id' ) == intentsId:
2432 intentsDict.append( intent )
2433 elif isinstance( intentsId, types.ListType ) \
Devin Lim752dd7b2017-06-27 14:40:03 -07002434 and any( intent.get( 'id' ) == ids for ids in intentsId ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002435 intentsDict.append( intent )
Devin Lim752dd7b2017-06-27 14:40:03 -07002436
2437 if not intentsDict:
Jon Hallae04e622016-01-27 10:38:05 -08002438 main.log.info( self.name + ": There is something wrong " +
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002439 "getting intents state" )
2440 return main.FALSE
kelvin-onlabf512e942015-06-08 19:42:59 -07002441
2442 if isinstance( expectedState, types.StringType ):
2443 for intents in intentsDict:
2444 if intents.get( 'state' ) != expectedState:
kelvin-onlaba297c4d2015-06-01 13:53:55 -07002445 main.log.debug( self.name + " : Intent ID - " +
2446 intents.get( 'id' ) +
kelvin-onlabf512e942015-06-08 19:42:59 -07002447 " actual state = " +
2448 intents.get( 'state' )
2449 + " does not equal expected state = "
2450 + expectedState )
kelvin-onlaba297c4d2015-06-01 13:53:55 -07002451 returnValue = main.FALSE
kelvin-onlabf512e942015-06-08 19:42:59 -07002452 elif isinstance( expectedState, types.ListType ):
2453 for intents in intentsDict:
2454 if not any( state == intents.get( 'state' ) for state in
2455 expectedState ):
2456 main.log.debug( self.name + " : Intent ID - " +
2457 intents.get( 'id' ) +
2458 " actual state = " +
2459 intents.get( 'state' ) +
2460 " does not equal expected states = "
2461 + str( expectedState ) )
2462 returnValue = main.FALSE
2463
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002464 if returnValue == main.TRUE:
2465 main.log.info( self.name + ": All " +
2466 str( len( intentsDict ) ) +
kelvin-onlabf512e942015-06-08 19:42:59 -07002467 " intents are in " + str( expectedState ) +
2468 " state" )
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002469 return returnValue
2470 except TypeError:
2471 main.log.exception( self.name + ": Object not as expected" )
2472 return None
2473 except pexpect.EOF:
2474 main.log.error( self.name + ": EOF exception found" )
2475 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002476 main.cleanAndExit()
kelvin-onlabc2dcd3f2015-04-09 16:40:02 -07002477 except Exception:
2478 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002479 main.cleanAndExit()
andrewonlabe6745342014-10-17 14:29:13 -04002480
Jon Hallf539eb92017-05-22 17:18:42 -07002481 def compareBandwidthAllocations( self, expectedAllocations ):
2482 """
2483 Description:
2484 Compare the allocated bandwidth with the given allocations
2485 Required:
2486 expectedAllocations - The expected ONOS output of the allocations command
2487 Return:
2488 Returns main.TRUE only if all intent are the same as expected states,
2489 otherwise returns main.FALSE.
2490 """
2491 # FIXME: Convert these string comparisons to object comparisons
2492 try:
2493 returnValue = main.TRUE
2494 bandwidthFailed = False
2495 rawAlloc = self.allocations()
2496 expectedFormat = StringIO( expectedAllocations )
2497 ONOSOutput = StringIO( rawAlloc )
2498 main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ),
2499 str( expectedFormat ) ) )
2500
2501 for actual, expected in izip( ONOSOutput, expectedFormat ):
2502 actual = actual.rstrip()
2503 expected = expected.rstrip()
2504 main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) )
2505 if actual != expected and 'allocated' in actual and 'allocated' in expected:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002506 marker1 = actual.find( 'allocated' )
2507 m1 = actual[ :marker1 ]
2508 marker2 = expected.find( 'allocated' )
2509 m2 = expected[ :marker2 ]
Jon Hallf539eb92017-05-22 17:18:42 -07002510 if m1 != m2:
2511 bandwidthFailed = True
2512 elif actual != expected and 'allocated' not in actual and 'allocated' not in expected:
2513 bandwidthFailed = True
2514 expectedFormat.close()
2515 ONOSOutput.close()
2516
2517 if bandwidthFailed:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002518 main.log.error( "Bandwidth not allocated correctly using Intents!!" )
Jon Hallf539eb92017-05-22 17:18:42 -07002519 returnValue = main.FALSE
2520 return returnValue
2521 except TypeError:
2522 main.log.exception( self.name + ": Object not as expected" )
2523 return None
2524 except pexpect.EOF:
2525 main.log.error( self.name + ": EOF exception found" )
2526 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002527 main.cleanAndExit()
Jon Hallf539eb92017-05-22 17:18:42 -07002528 except Exception:
2529 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002530 main.cleanAndExit()
Jon Hallf539eb92017-05-22 17:18:42 -07002531
You Wang66518af2016-05-16 15:32:59 -07002532 def compareIntent( self, intentDict ):
2533 """
2534 Description:
2535 Compare the intent ids and states provided in the argument with all intents in ONOS
2536 Return:
2537 Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE
2538 Arguments:
2539 intentDict: a dictionary which maps intent ids to intent states
2540 """
2541 try:
2542 intentsRaw = self.intents()
2543 intentsJson = json.loads( intentsRaw )
2544 intentDictONOS = {}
2545 for intent in intentsJson:
2546 intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ]
You Wang58d04452016-09-21 15:13:05 -07002547 returnValue = main.TRUE
You Wang66518af2016-05-16 15:32:59 -07002548 if len( intentDict ) != len( intentDictONOS ):
You Wang58d04452016-09-21 15:13:05 -07002549 main.log.warn( self.name + ": expected intent count does not match that in ONOS, " +
You Wang66518af2016-05-16 15:32:59 -07002550 str( len( intentDict ) ) + " expected and " +
2551 str( len( intentDictONOS ) ) + " actual" )
You Wang58d04452016-09-21 15:13:05 -07002552 returnValue = main.FALSE
You Wang66518af2016-05-16 15:32:59 -07002553 for intentID in intentDict.keys():
Jon Halle0f0b342017-04-18 11:43:47 -07002554 if intentID not in intentDictONOS.keys():
You Wang66518af2016-05-16 15:32:59 -07002555 main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" )
2556 returnValue = main.FALSE
You Wang58d04452016-09-21 15:13:05 -07002557 else:
2558 if intentDict[ intentID ] != intentDictONOS[ intentID ]:
2559 main.log.debug( self.name + ": intent ID - " + intentID +
2560 " expected state is " + intentDict[ intentID ] +
2561 " but actual state is " + intentDictONOS[ intentID ] )
2562 returnValue = main.FALSE
2563 intentDictONOS.pop( intentID )
2564 if len( intentDictONOS ) > 0:
2565 returnValue = main.FALSE
2566 for intentID in intentDictONOS.keys():
2567 main.log.debug( self.name + ": find extra intent in ONOS: intent ID " + intentID )
You Wang66518af2016-05-16 15:32:59 -07002568 if returnValue == main.TRUE:
2569 main.log.info( self.name + ": all intent IDs and states match that in ONOS" )
2570 return returnValue
You Wang1be9a512016-05-26 16:54:17 -07002571 except KeyError:
2572 main.log.exception( self.name + ": KeyError exception found" )
2573 return main.ERROR
You Wang66518af2016-05-16 15:32:59 -07002574 except ( TypeError, ValueError ):
2575 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) )
You Wang85560372016-05-18 10:44:33 -07002576 return main.ERROR
You Wang66518af2016-05-16 15:32:59 -07002577 except pexpect.EOF:
2578 main.log.error( self.name + ": EOF exception found" )
2579 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002580 main.cleanAndExit()
You Wang66518af2016-05-16 15:32:59 -07002581 except Exception:
2582 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002583 main.cleanAndExit()
You Wang66518af2016-05-16 15:32:59 -07002584
YPZhang14a4aa92016-07-15 13:37:15 -07002585 def checkIntentSummary( self, timeout=60, noExit=True ):
GlennRCed771242016-01-13 17:02:47 -08002586 """
2587 Description:
2588 Check the number of installed intents.
2589 Optional:
2590 timeout - the timeout for pexcept
YPZhang14a4aa92016-07-15 13:37:15 -07002591 noExit - If noExit, TestON will not exit if any except.
GlennRCed771242016-01-13 17:02:47 -08002592 Return:
2593 Returns main.TRUE only if the number of all installed intents are the same as total intents number
2594 , otherwise, returns main.FALSE.
2595 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002596
GlennRCed771242016-01-13 17:02:47 -08002597 try:
2598 cmd = "intents -s -j"
2599
2600 # Check response if something wrong
YPZhang14a4aa92016-07-15 13:37:15 -07002601 response = self.sendline( cmd, timeout=timeout, noExit=noExit )
Jon Halle0f0b342017-04-18 11:43:47 -07002602 if response is None:
YPZhang0584d432016-06-21 15:20:13 -07002603 return main.FALSE
GlennRCed771242016-01-13 17:02:47 -08002604 response = json.loads( response )
2605
2606 # get total and installed number, see if they are match
2607 allState = response.get( 'all' )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002608 if allState.get( 'total' ) == allState.get( 'installed' ):
Jon Halla478b852017-12-04 15:00:15 -08002609 main.log.info( 'Total Intents: {} Installed Intents: {}'.format(
2610 allState.get( 'total' ), allState.get( 'installed' ) ) )
GlennRCed771242016-01-13 17:02:47 -08002611 return main.TRUE
Jon Halla478b852017-12-04 15:00:15 -08002612 main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format(
2613 allState.get( 'total' ), allState.get( 'installed' ) ) )
GlennRCed771242016-01-13 17:02:47 -08002614 return main.FALSE
2615
Jon Hallc6793552016-01-19 14:18:37 -08002616 except ( TypeError, ValueError ):
2617 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) )
GlennRCed771242016-01-13 17:02:47 -08002618 return None
2619 except pexpect.EOF:
2620 main.log.error( self.name + ": EOF exception found" )
2621 main.log.error( self.name + ": " + self.handle.before )
YPZhang14a4aa92016-07-15 13:37:15 -07002622 if noExit:
2623 return main.FALSE
2624 else:
Devin Lim44075962017-08-11 10:56:37 -07002625 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07002626 except pexpect.TIMEOUT:
2627 main.log.error( self.name + ": ONOS timeout" )
2628 return None
GlennRCed771242016-01-13 17:02:47 -08002629 except Exception:
2630 main.log.exception( self.name + ": Uncaught exception!" )
YPZhang14a4aa92016-07-15 13:37:15 -07002631 if noExit:
2632 return main.FALSE
2633 else:
Devin Lim44075962017-08-11 10:56:37 -07002634 main.cleanAndExit()
GlennRCed771242016-01-13 17:02:47 -08002635
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07002636 def flows( self, state="any", jsonFormat=True, timeout=60, noExit=False, noCore=False, device=""):
kelvin8ec71442015-01-15 16:57:00 -08002637 """
Shreya Shah0f01c812014-10-26 20:15:28 -04002638 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002639 * jsonFormat: enable output formatting in json
Jeremy Songster306ed7a2016-07-19 10:59:07 -07002640 * noCore: suppress core flows
Shreya Shah0f01c812014-10-26 20:15:28 -04002641 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08002642 Obtain flows currently installed
kelvin-onlab898a6c62015-01-16 14:13:53 -08002643 """
Shreya Shah0f01c812014-10-26 20:15:28 -04002644 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002645 cmdStr = "flows"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002646 if jsonFormat:
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07002647 cmdStr += " -j"
Jeremy Songster306ed7a2016-07-19 10:59:07 -07002648 if noCore:
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07002649 cmdStr += " -n"
2650 cmdStr += " " + state
2651 cmdStr += " " + device
YPZhangebf9eb52016-05-12 15:20:24 -07002652 handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit )
You Wangb5a55f72017-03-03 12:51:05 -08002653 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002654 assert "Command not found:" not in handle, handle
2655 if re.search( "Error:", handle ):
2656 main.log.error( self.name + ": flows() response: " +
2657 str( handle ) )
2658 return handle
2659 except AssertionError:
2660 main.log.exception( "" )
GlennRCed771242016-01-13 17:02:47 -08002661 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002662 except TypeError:
2663 main.log.exception( self.name + ": Object not as expected" )
2664 return None
Jon Hallc6793552016-01-19 14:18:37 -08002665 except pexpect.TIMEOUT:
2666 main.log.error( self.name + ": ONOS timeout" )
2667 return None
Shreya Shah0f01c812014-10-26 20:15:28 -04002668 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002669 main.log.error( self.name + ": EOF exception found" )
2670 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002671 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002672 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002673 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002674 main.cleanAndExit()
Shreya Shah0f01c812014-10-26 20:15:28 -04002675
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002676 def checkFlowCount( self, min=0, timeout=60 ):
Flavio Castroa1286fe2016-07-25 14:48:51 -07002677 count = self.getTotalFlowsNum( timeout=timeout )
Jon Halle0f0b342017-04-18 11:43:47 -07002678 count = int( count ) if count else 0
2679 return count if ( count > min ) else False
GlennRCed771242016-01-13 17:02:47 -08002680
Jon Halle0f0b342017-04-18 11:43:47 -07002681 def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ):
kelvin-onlab4df89f22015-04-13 18:10:23 -07002682 """
2683 Description:
GlennRCed771242016-01-13 17:02:47 -08002684 Check the if all the current flows are in ADDED state
Jon Hallc6793552016-01-19 14:18:37 -08002685 We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows,
2686 if the count of those states is 0, which means all current flows
2687 are in ADDED state, and return main.TRUE otherwise return main.FALSE
pingping-linbab7f8a2015-09-21 17:33:36 -07002688 Optional:
GlennRCed771242016-01-13 17:02:47 -08002689 * isPENDING: whether the PENDING_ADD is also a correct status
kelvin-onlab4df89f22015-04-13 18:10:23 -07002690 Return:
2691 returnValue - Returns main.TRUE only if all flows are in
Jon Hallc6793552016-01-19 14:18:37 -08002692 ADDED state or PENDING_ADD if the isPENDING
pingping-linbab7f8a2015-09-21 17:33:36 -07002693 parameter is set true, return main.FALSE otherwise.
kelvin-onlab4df89f22015-04-13 18:10:23 -07002694 """
2695 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002696 states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ]
GlennRCed771242016-01-13 17:02:47 -08002697 checkedStates = []
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002698 statesCount = [ 0, 0, 0, 0 ]
GlennRCed771242016-01-13 17:02:47 -08002699 for s in states:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002700 rawFlows = self.flows( state=s, timeout = timeout )
YPZhang240842b2016-05-17 12:00:50 -07002701 if rawFlows:
2702 # if we didn't get flows or flows function return None, we should return
2703 # main.Flase
2704 checkedStates.append( json.loads( rawFlows ) )
2705 else:
2706 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08002707 for i in range( len( states ) ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002708 for c in checkedStates[ i ]:
Jon Hallc6793552016-01-19 14:18:37 -08002709 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002710 statesCount[ i ] += int( c.get( "flowCount" ) )
Jon Hallc6793552016-01-19 14:18:37 -08002711 except TypeError:
2712 main.log.exception( "Json object not as expected" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002713 main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) )
kelvin-onlabf2ec6e02015-05-27 14:15:28 -07002714
GlennRCed771242016-01-13 17:02:47 -08002715 # We want to count PENDING_ADD if isPENDING is true
2716 if isPENDING:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002717 if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0:
GlennRCed771242016-01-13 17:02:47 -08002718 return main.FALSE
pingping-linbab7f8a2015-09-21 17:33:36 -07002719 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002720 if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0:
GlennRCed771242016-01-13 17:02:47 -08002721 return main.FALSE
GlennRCed771242016-01-13 17:02:47 -08002722 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08002723 except ( TypeError, ValueError ):
2724 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) )
kelvin-onlab4df89f22015-04-13 18:10:23 -07002725 return None
Jeremy Songster9385d412016-06-02 17:57:36 -07002726
YPZhang240842b2016-05-17 12:00:50 -07002727 except AssertionError:
2728 main.log.exception( "" )
2729 return None
Jon Halle0f0b342017-04-18 11:43:47 -07002730 except pexpect.TIMEOUT:
2731 main.log.error( self.name + ": ONOS timeout" )
2732 return None
kelvin-onlab4df89f22015-04-13 18:10:23 -07002733 except pexpect.EOF:
2734 main.log.error( self.name + ": EOF exception found" )
2735 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002736 main.cleanAndExit()
kelvin-onlab4df89f22015-04-13 18:10:23 -07002737 except Exception:
2738 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002739 main.cleanAndExit()
kelvin-onlab4df89f22015-04-13 18:10:23 -07002740
GlennRCed771242016-01-13 17:02:47 -08002741 def pushTestIntents( self, ingress, egress, batchSize, offset="",
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002742 options="", timeout=10, background = False, noExit=False, getResponse=False ):
kelvin8ec71442015-01-15 16:57:00 -08002743 """
andrewonlab87852b02014-11-19 18:44:19 -05002744 Description:
Jon Halle3f39ff2015-01-13 11:50:53 -08002745 Push a number of intents in a batch format to
andrewonlab87852b02014-11-19 18:44:19 -05002746 a specific point-to-point intent definition
2747 Required:
GlennRCed771242016-01-13 17:02:47 -08002748 * ingress: specify source dpid
2749 * egress: specify destination dpid
2750 * batchSize: specify number of intents to push
andrewonlab87852b02014-11-19 18:44:19 -05002751 Optional:
GlennRCed771242016-01-13 17:02:47 -08002752 * offset: the keyOffset is where the next batch of intents
2753 will be installed
YPZhangb34b7e12016-06-14 14:28:19 -07002754 * noExit: If set to True, TestON will not exit if any error when issus command
2755 * getResponse: If set to True, function will return ONOS response.
2756
GlennRCed771242016-01-13 17:02:47 -08002757 Returns: If failed to push test intents, it will returen None,
2758 if successful, return true.
2759 Timeout expection will return None,
2760 TypeError will return false
2761 other expections will exit()
kelvin8ec71442015-01-15 16:57:00 -08002762 """
andrewonlab87852b02014-11-19 18:44:19 -05002763 try:
GlennRCed771242016-01-13 17:02:47 -08002764 if background:
2765 back = "&"
andrewonlab87852b02014-11-19 18:44:19 -05002766 else:
GlennRCed771242016-01-13 17:02:47 -08002767 back = ""
2768 cmd = "push-test-intents {} {} {} {} {} {}".format( options,
Jon Hallc6793552016-01-19 14:18:37 -08002769 ingress,
2770 egress,
2771 batchSize,
2772 offset,
2773 back )
YPZhangebf9eb52016-05-12 15:20:24 -07002774 response = self.sendline( cmd, timeout=timeout, noExit=noExit )
You Wangb5a55f72017-03-03 12:51:05 -08002775 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002776 assert "Command not found:" not in response, response
GlennRCed771242016-01-13 17:02:47 -08002777 main.log.info( response )
YPZhangb34b7e12016-06-14 14:28:19 -07002778 if getResponse:
2779 return response
2780
GlennRCed771242016-01-13 17:02:47 -08002781 # TODO: We should handle if there is failure in installation
2782 return main.TRUE
2783
Jon Hallc6793552016-01-19 14:18:37 -08002784 except AssertionError:
2785 main.log.exception( "" )
2786 return None
GlennRCed771242016-01-13 17:02:47 -08002787 except pexpect.TIMEOUT:
2788 main.log.error( self.name + ": ONOS timeout" )
Jon Halld4d4b372015-01-28 16:02:41 -08002789 return None
andrewonlab87852b02014-11-19 18:44:19 -05002790 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002791 main.log.error( self.name + ": EOF exception found" )
2792 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002793 main.cleanAndExit()
GlennRCed771242016-01-13 17:02:47 -08002794 except TypeError:
2795 main.log.exception( self.name + ": Object not as expected" )
Jon Hallc6793552016-01-19 14:18:37 -08002796 return None
Jon Hallfebb1c72015-03-05 13:30:09 -08002797 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002798 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002799 main.cleanAndExit()
andrewonlab87852b02014-11-19 18:44:19 -05002800
YPZhangebf9eb52016-05-12 15:20:24 -07002801 def getTotalFlowsNum( self, timeout=60, noExit=False ):
YPZhangb5d3f832016-01-23 22:54:26 -08002802 """
2803 Description:
YPZhangf6f14a02016-01-28 15:17:31 -08002804 Get the number of ADDED flows.
YPZhangb5d3f832016-01-23 22:54:26 -08002805 Return:
YPZhangf6f14a02016-01-28 15:17:31 -08002806 The number of ADDED flows
YPZhang14a4aa92016-07-15 13:37:15 -07002807 Or return None if any exceptions
YPZhangb5d3f832016-01-23 22:54:26 -08002808 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002809
YPZhangb5d3f832016-01-23 22:54:26 -08002810 try:
YPZhange3109a72016-02-02 11:25:37 -08002811 # get total added flows number
YPZhang14a4aa92016-07-15 13:37:15 -07002812 cmd = "flows -c added"
2813 rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit )
2814 if rawFlows:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002815 rawFlows = rawFlows.split( "\n" )
YPZhange3109a72016-02-02 11:25:37 -08002816 totalFlows = 0
YPZhang14a4aa92016-07-15 13:37:15 -07002817 for l in rawFlows:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002818 totalFlows += int( l.split( "Count=" )[ 1 ] )
YPZhang14a4aa92016-07-15 13:37:15 -07002819 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002820 main.log.error( "Response not as expected!" )
YPZhang14a4aa92016-07-15 13:37:15 -07002821 return None
2822 return totalFlows
YPZhange3109a72016-02-02 11:25:37 -08002823
You Wangd3097f72018-12-12 11:56:03 -08002824 except IndexError:
2825 main.log.exception( "{}: Object not as expected!".format( self.name ) )
2826 main.log.debug( "rawFlows: {}".format( rawFlows ) )
2827 return None
You Wangd3cb2ce2016-05-16 14:01:24 -07002828 except ( TypeError, ValueError ):
YPZhang14a4aa92016-07-15 13:37:15 -07002829 main.log.exception( "{}: Object not as expected!".format( self.name ) )
YPZhangb5d3f832016-01-23 22:54:26 -08002830 return None
2831 except pexpect.EOF:
2832 main.log.error( self.name + ": EOF exception found" )
2833 main.log.error( self.name + ": " + self.handle.before )
YPZhang14a4aa92016-07-15 13:37:15 -07002834 if not noExit:
Devin Lim44075962017-08-11 10:56:37 -07002835 main.cleanAndExit()
YPZhang14a4aa92016-07-15 13:37:15 -07002836 return None
Jon Halle0f0b342017-04-18 11:43:47 -07002837 except pexpect.TIMEOUT:
2838 main.log.error( self.name + ": ONOS timeout" )
2839 return None
YPZhangb5d3f832016-01-23 22:54:26 -08002840 except Exception:
2841 main.log.exception( self.name + ": Uncaught exception!" )
YPZhang14a4aa92016-07-15 13:37:15 -07002842 if not noExit:
Devin Lim44075962017-08-11 10:56:37 -07002843 main.cleanAndExit()
YPZhang14a4aa92016-07-15 13:37:15 -07002844 return None
YPZhangb5d3f832016-01-23 22:54:26 -08002845
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002846 def getTotalIntentsNum( self, timeout=60, noExit = False ):
YPZhangb5d3f832016-01-23 22:54:26 -08002847 """
2848 Description:
2849 Get the total number of intents, include every states.
YPZhang14a4aa92016-07-15 13:37:15 -07002850 Optional:
2851 noExit - If noExit, TestON will not exit if any except.
YPZhangb5d3f832016-01-23 22:54:26 -08002852 Return:
2853 The number of intents
2854 """
2855 try:
2856 cmd = "summary -j"
YPZhang14a4aa92016-07-15 13:37:15 -07002857 response = self.sendline( cmd, timeout=timeout, noExit=noExit )
Jon Halle0f0b342017-04-18 11:43:47 -07002858 if response is None:
2859 return -1
YPZhangb5d3f832016-01-23 22:54:26 -08002860 response = json.loads( response )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002861 return int( response.get( "intents" ) )
You Wangd3cb2ce2016-05-16 14:01:24 -07002862 except ( TypeError, ValueError ):
2863 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) )
YPZhangb5d3f832016-01-23 22:54:26 -08002864 return None
2865 except pexpect.EOF:
2866 main.log.error( self.name + ": EOF exception found" )
2867 main.log.error( self.name + ": " + self.handle.before )
YPZhang14a4aa92016-07-15 13:37:15 -07002868 if noExit:
2869 return -1
2870 else:
Devin Lim44075962017-08-11 10:56:37 -07002871 main.cleanAndExit()
YPZhangb5d3f832016-01-23 22:54:26 -08002872 except Exception:
2873 main.log.exception( self.name + ": Uncaught exception!" )
YPZhang14a4aa92016-07-15 13:37:15 -07002874 if noExit:
2875 return -1
2876 else:
Devin Lim44075962017-08-11 10:56:37 -07002877 main.cleanAndExit()
YPZhangb5d3f832016-01-23 22:54:26 -08002878
kelvin-onlabd3b64892015-01-20 13:26:24 -08002879 def intentsEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08002880 """
Jon Halle3f39ff2015-01-13 11:50:53 -08002881 Description:Returns topology metrics
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002882 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002883 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08002884 """
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002885 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002886 cmdStr = "intents-events-metrics"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002887 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002888 cmdStr += " -j"
2889 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002890 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002891 assert "Command not found:" not in handle, handle
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002892 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002893 except AssertionError:
2894 main.log.exception( "" )
2895 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002896 except TypeError:
2897 main.log.exception( self.name + ": Object not as expected" )
2898 return None
andrewonlab0dbb6ec2014-11-06 13:46:55 -05002899 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002900 main.log.error( self.name + ": EOF exception found" )
2901 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002902 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002903 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002904 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002905 main.cleanAndExit()
Shreya Shah0f01c812014-10-26 20:15:28 -04002906
kelvin-onlabd3b64892015-01-20 13:26:24 -08002907 def topologyEventsMetrics( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08002908 """
2909 Description:Returns topology metrics
andrewonlab867212a2014-10-22 20:13:38 -04002910 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08002911 * jsonFormat: enable json formatting of output
kelvin8ec71442015-01-15 16:57:00 -08002912 """
andrewonlab867212a2014-10-22 20:13:38 -04002913 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07002914 cmdStr = "topology-events-metrics"
kelvin-onlabd3b64892015-01-20 13:26:24 -08002915 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07002916 cmdStr += " -j"
2917 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002918 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002919 assert "Command not found:" not in handle, handle
jenkins7ead5a82015-03-13 10:28:21 -07002920 if handle:
2921 return handle
Jon Hallc6358dd2015-04-10 12:44:28 -07002922 elif jsonFormat:
Jon Hallbe379602015-03-24 13:39:32 -07002923 # Return empty json
jenkins7ead5a82015-03-13 10:28:21 -07002924 return '{}'
Jon Hallc6358dd2015-04-10 12:44:28 -07002925 else:
2926 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002927 except AssertionError:
2928 main.log.exception( "" )
2929 return None
Jon Halld4d4b372015-01-28 16:02:41 -08002930 except TypeError:
2931 main.log.exception( self.name + ": Object not as expected" )
2932 return None
andrewonlab867212a2014-10-22 20:13:38 -04002933 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002934 main.log.error( self.name + ": EOF exception found" )
2935 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002936 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002937 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002938 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002939 main.cleanAndExit()
andrewonlab867212a2014-10-22 20:13:38 -04002940
kelvin8ec71442015-01-15 16:57:00 -08002941 # Wrapper functions ****************
2942 # Wrapper functions use existing driver
2943 # functions and extends their use case.
2944 # For example, we may use the output of
2945 # a normal driver function, and parse it
2946 # using a wrapper function
andrewonlabc2d05aa2014-10-13 16:51:10 -04002947
kelvin-onlabd3b64892015-01-20 13:26:24 -08002948 def getAllIntentsId( self ):
kelvin8ec71442015-01-15 16:57:00 -08002949 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04002950 Description:
2951 Obtain all intent id's in a list
kelvin8ec71442015-01-15 16:57:00 -08002952 """
andrewonlab9a50dfe2014-10-17 17:22:31 -04002953 try:
kelvin8ec71442015-01-15 16:57:00 -08002954 # Obtain output of intents function
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002955 intentsStr = self.intents( jsonFormat=True )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07002956 if intentsStr is None:
2957 raise TypeError
Jon Hall6021e062017-01-30 11:10:06 -08002958 # Convert to a dictionary
2959 intents = json.loads( intentsStr )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002960 intentIdList = []
Jon Hall6021e062017-01-30 11:10:06 -08002961 for intent in intents:
2962 intentIdList.append( intent[ 'id' ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -08002963 return intentIdList
Jon Halld4d4b372015-01-28 16:02:41 -08002964 except TypeError:
2965 main.log.exception( self.name + ": Object not as expected" )
2966 return None
andrewonlab9a50dfe2014-10-17 17:22:31 -04002967 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08002968 main.log.error( self.name + ": EOF exception found" )
2969 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002970 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08002971 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08002972 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002973 main.cleanAndExit()
andrewonlab9a50dfe2014-10-17 17:22:31 -04002974
You Wang3c276252016-09-21 15:21:36 -07002975 def flowAddedCount( self, deviceId, core=False ):
Jon Hall30b82fa2015-03-04 17:15:43 -08002976 """
2977 Determine the number of flow rules for the given device id that are
2978 in the added state
You Wang3c276252016-09-21 15:21:36 -07002979 Params:
2980 core: if True, only return the number of core flows added
Jon Hall30b82fa2015-03-04 17:15:43 -08002981 """
2982 try:
You Wang3c276252016-09-21 15:21:36 -07002983 if core:
2984 cmdStr = "flows any " + str( deviceId ) + " | " +\
2985 "grep 'state=ADDED' | grep org.onosproject.core | wc -l"
2986 else:
2987 cmdStr = "flows any " + str( deviceId ) + " | " +\
2988 "grep 'state=ADDED' | wc -l"
Jon Halld5a94fb2018-11-13 14:32:23 -08002989 handle = self.lineCount( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08002990 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08002991 assert "Command not found:" not in handle, handle
Jon Hall30b82fa2015-03-04 17:15:43 -08002992 return handle
Jon Hallc6793552016-01-19 14:18:37 -08002993 except AssertionError:
2994 main.log.exception( "" )
2995 return None
Jon Hall30b82fa2015-03-04 17:15:43 -08002996 except pexpect.EOF:
2997 main.log.error( self.name + ": EOF exception found" )
2998 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002999 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003000 except Exception:
Jon Hall30b82fa2015-03-04 17:15:43 -08003001 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003002 main.cleanAndExit()
andrewonlab95ce8322014-10-13 14:12:04 -04003003
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08003004 def groupAddedCount( self, deviceId, core=False ):
3005 """
3006 Determine the number of group rules for the given device id that are
3007 in the added state
3008 Params:
3009 core: if True, only return the number of core groups added
3010 """
3011 try:
3012 if core:
3013 cmdStr = "groups any " + str( deviceId ) + " | " +\
3014 "grep 'state=ADDED' | grep org.onosproject.core | wc -l"
3015 else:
3016 cmdStr = "groups any " + str( deviceId ) + " | " +\
3017 "grep 'state=ADDED' | wc -l"
Jon Halld5a94fb2018-11-13 14:32:23 -08003018 handle = self.lineCount( cmdStr )
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08003019 assert handle is not None, "Error in sendline"
3020 assert "Command not found:" not in handle, handle
3021 return handle
3022 except AssertionError:
3023 main.log.exception( "" )
3024 return None
3025 except pexpect.EOF:
3026 main.log.error( self.name + ": EOF exception found" )
3027 main.log.error( self.name + ": " + self.handle.before )
3028 main.cleanAndExit()
3029 except Exception:
3030 main.log.exception( self.name + ": Uncaught exception!" )
3031 main.cleanAndExit()
3032
Andreas Pantelopoulos2eae3242018-03-06 13:47:20 -08003033 def addStaticRoute( self, subnet, intf):
3034 """
3035 Adds a static route to onos.
3036 Params:
3037 subnet: The subnet reaching through this route
3038 intf: The interface this route is reachable through
3039 """
3040 try:
3041 cmdStr = "route-add " + subnet + " " + intf
3042 handle = self.sendline( cmdStr )
3043 assert handle is not None, "Error in sendline"
3044 assert "Command not found:" not in handle, handle
3045 return handle
3046 except AssertionError:
3047 main.log.exception( "" )
3048 return None
3049 except pexpect.EOF:
3050 main.log.error( self.name + ": EOF exception found" )
3051 main.log.error( self.name + ": " + self.handle.before )
3052 main.cleanAndExit()
3053 except Exception:
3054 main.log.exception( self.name + ": Uncaught exception!" )
3055 main.cleanAndExit()
3056
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08003057 def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0):
3058 """
3059 Description:
3060 Check whether the number of groups for the given device id that
3061 are in ADDED state is bigger than minGroupCount.
3062 Required:
3063 * deviceId: device id to check the number of added group rules
3064 Optional:
3065 * minGroupCount: the number of groups to compare
3066 * core: if True, only check the number of core groups added
3067 * comparison: if 0, compare with greater than minFlowCount
3068 * if 1, compare with equal to minFlowCount
3069 Return:
3070 Returns the number of groups if it is bigger than minGroupCount,
3071 returns main.FALSE otherwise.
3072 """
3073 count = self.groupAddedCount( deviceId, core )
3074 count = int( count ) if count else 0
Jon Hall9677ed32018-04-24 11:16:23 -07003075 main.log.debug( "found {} groups".format( count ) )
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08003076 return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE
3077
You Wangc02f3be2018-05-18 12:14:23 -07003078 def getGroups( self, deviceId, groupType="any" ):
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07003079 """
3080 Retrieve groups from a specific device.
You Wangc02f3be2018-05-18 12:14:23 -07003081 deviceId: Id of the device from which we retrieve groups
3082 groupType: Type of group
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07003083 """
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07003084 try:
You Wangc02f3be2018-05-18 12:14:23 -07003085 groupCmd = "groups -t {0} any {1}".format( groupType, deviceId )
3086 handle = self.sendline( groupCmd )
Andreas Pantelopoulosdf5061f2018-05-15 11:46:59 -07003087 assert handle is not None, "Error in sendline"
3088 assert "Command not found:" not in handle, handle
3089 return handle
3090 except AssertionError:
3091 main.log.exception( "" )
3092 return None
3093 except TypeError:
3094 main.log.exception( self.name + ": Object not as expected" )
3095 return None
3096 except pexpect.EOF:
3097 main.log.error( self.name + ": EOF exception found" )
3098 main.log.error( self.name + ": " + self.handle.before )
3099 main.cleanAndExit()
3100 except Exception:
3101 main.log.exception( self.name + ": Uncaught exception!" )
3102 main.cleanAndExit()
3103
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08003104 def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0):
Jonghwan Hyuncf2345c2018-02-26 11:07:54 -08003105 """
3106 Description:
3107 Check whether the number of flow rules for the given device id that
3108 are in ADDED state is bigger than minFlowCount.
3109 Required:
3110 * deviceId: device id to check the number of added flow rules
3111 Optional:
3112 * minFlowCount: the number of flow rules to compare
3113 * core: if True, only check the number of core flows added
Andreas Pantelopoulos9173d442018-03-01 17:07:37 -08003114 * comparison: if 0, compare with greater than minFlowCount
3115 * if 1, compare with equal to minFlowCount
Jonghwan Hyuncf2345c2018-02-26 11:07:54 -08003116 Return:
3117 Returns the number of flow rules if it is bigger than minFlowCount,
3118 returns main.FALSE otherwise.
3119 """
3120 count = self.flowAddedCount( deviceId, core )
3121 count = int( count ) if count else 0
Jon Hall9677ed32018-04-24 11:16:23 -07003122 main.log.debug( "found {} flows".format( count ) )
Andreas Pantelopoulos2eae3242018-03-06 13:47:20 -08003123 return count if ((count > expectedFlowCount) if (comparison == 0) else (count == expectedFlowCount)) else main.FALSE
Jonghwan Hyuncf2345c2018-02-26 11:07:54 -08003124
kelvin-onlabd3b64892015-01-20 13:26:24 -08003125 def getAllDevicesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08003126 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04003127 Use 'devices' function to obtain list of all devices
3128 and parse the result to obtain a list of all device
3129 id's. Returns this list. Returns empty list if no
3130 devices exist
kelvin8ec71442015-01-15 16:57:00 -08003131 List is ordered sequentially
3132
andrewonlab3e15ead2014-10-15 14:21:34 -04003133 This function may be useful if you are not sure of the
kelvin8ec71442015-01-15 16:57:00 -08003134 device id, and wish to execute other commands using
andrewonlab3e15ead2014-10-15 14:21:34 -04003135 the ids. By obtaining the list of device ids on the fly,
3136 you can iterate through the list to get mastership, etc.
kelvin8ec71442015-01-15 16:57:00 -08003137 """
andrewonlab7e4d2d32014-10-15 13:23:21 -04003138 try:
kelvin8ec71442015-01-15 16:57:00 -08003139 # Call devices and store result string
kelvin-onlabd3b64892015-01-20 13:26:24 -08003140 devicesStr = self.devices( jsonFormat=False )
3141 idList = []
kelvin8ec71442015-01-15 16:57:00 -08003142
kelvin-onlabd3b64892015-01-20 13:26:24 -08003143 if not devicesStr:
kelvin8ec71442015-01-15 16:57:00 -08003144 main.log.info( "There are no devices to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003145 return idList
kelvin8ec71442015-01-15 16:57:00 -08003146
3147 # Split the string into list by comma
kelvin-onlabd3b64892015-01-20 13:26:24 -08003148 deviceList = devicesStr.split( "," )
kelvin8ec71442015-01-15 16:57:00 -08003149 # Get temporary list of all arguments with string 'id='
kelvin-onlabd3b64892015-01-20 13:26:24 -08003150 tempList = [ dev for dev in deviceList if "id=" in dev ]
kelvin8ec71442015-01-15 16:57:00 -08003151 # Split list further into arguments before and after string
3152 # 'id='. Get the latter portion ( the actual device id ) and
kelvin-onlabd3b64892015-01-20 13:26:24 -08003153 # append to idList
3154 for arg in tempList:
3155 idList.append( arg.split( "id=" )[ 1 ] )
3156 return idList
andrewonlab7e4d2d32014-10-15 13:23:21 -04003157
Jon Halld4d4b372015-01-28 16:02:41 -08003158 except TypeError:
3159 main.log.exception( self.name + ": Object not as expected" )
3160 return None
andrewonlab7e4d2d32014-10-15 13:23:21 -04003161 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003162 main.log.error( self.name + ": EOF exception found" )
3163 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003164 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003165 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003166 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003167 main.cleanAndExit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04003168
kelvin-onlabd3b64892015-01-20 13:26:24 -08003169 def getAllNodesId( self ):
kelvin8ec71442015-01-15 16:57:00 -08003170 """
andrewonlab7c211572014-10-15 16:45:20 -04003171 Uses 'nodes' function to obtain list of all nodes
3172 and parse the result of nodes to obtain just the
kelvin8ec71442015-01-15 16:57:00 -08003173 node id's.
andrewonlab7c211572014-10-15 16:45:20 -04003174 Returns:
3175 list of node id's
kelvin8ec71442015-01-15 16:57:00 -08003176 """
andrewonlab7c211572014-10-15 16:45:20 -04003177 try:
Jon Hall5aa168b2015-03-23 14:23:09 -07003178 nodesStr = self.nodes( jsonFormat=True )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003179 idList = []
Jon Hall5aa168b2015-03-23 14:23:09 -07003180 # Sample nodesStr output
Jon Hallbd182782016-03-28 16:42:22 -07003181 # id=local, address=127.0.0.1:9876, state=READY *
kelvin-onlabd3b64892015-01-20 13:26:24 -08003182 if not nodesStr:
kelvin8ec71442015-01-15 16:57:00 -08003183 main.log.info( "There are no nodes to get id from" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003184 return idList
Jon Hall5aa168b2015-03-23 14:23:09 -07003185 nodesJson = json.loads( nodesStr )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003186 idList = [ node.get( 'id' ) for node in nodesJson ]
kelvin-onlabd3b64892015-01-20 13:26:24 -08003187 return idList
Jon Hallc6793552016-01-19 14:18:37 -08003188 except ( TypeError, ValueError ):
3189 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) )
Jon Halld4d4b372015-01-28 16:02:41 -08003190 return None
andrewonlab7c211572014-10-15 16:45:20 -04003191 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003192 main.log.error( self.name + ": EOF exception found" )
3193 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003194 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003195 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003196 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003197 main.cleanAndExit()
andrewonlab7e4d2d32014-10-15 13:23:21 -04003198
kelvin-onlabd3b64892015-01-20 13:26:24 -08003199 def getDevice( self, dpid=None ):
kelvin8ec71442015-01-15 16:57:00 -08003200 """
Jon Halla91c4dc2014-10-22 12:57:04 -04003201 Return the first device from the devices api whose 'id' contains 'dpid'
3202 Return None if there is no match
kelvin8ec71442015-01-15 16:57:00 -08003203 """
Jon Halla91c4dc2014-10-22 12:57:04 -04003204 try:
kelvin8ec71442015-01-15 16:57:00 -08003205 if dpid is None:
Jon Halla91c4dc2014-10-22 12:57:04 -04003206 return None
3207 else:
kelvin8ec71442015-01-15 16:57:00 -08003208 dpid = dpid.replace( ':', '' )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003209 rawDevices = self.devices()
3210 devicesJson = json.loads( rawDevices )
kelvin8ec71442015-01-15 16:57:00 -08003211 # search json for the device with dpid then return the device
kelvin-onlabd3b64892015-01-20 13:26:24 -08003212 for device in devicesJson:
kelvin8ec71442015-01-15 16:57:00 -08003213 # print "%s in %s?" % ( dpid, device[ 'id' ] )
3214 if dpid in device[ 'id' ]:
Jon Halla91c4dc2014-10-22 12:57:04 -04003215 return device
3216 return None
Jon Hallc6793552016-01-19 14:18:37 -08003217 except ( TypeError, ValueError ):
3218 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) )
Jon Halld4d4b372015-01-28 16:02:41 -08003219 return None
Jon Halla91c4dc2014-10-22 12:57:04 -04003220 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003221 main.log.error( self.name + ": EOF exception found" )
3222 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003223 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003224 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003225 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003226 main.cleanAndExit()
Jon Halla91c4dc2014-10-22 12:57:04 -04003227
You Wang24139872016-05-03 11:48:47 -07003228 def getTopology( self, topologyOutput ):
3229 """
3230 Definition:
3231 Loads a json topology output
3232 Return:
3233 topology = current ONOS topology
3234 """
3235 import json
3236 try:
3237 # either onos:topology or 'topology' will work in CLI
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003238 topology = json.loads( topologyOutput )
Jeremy Songsterbc2d8ac2016-05-04 11:25:42 -07003239 main.log.debug( topology )
You Wang24139872016-05-03 11:48:47 -07003240 return topology
You Wangd3cb2ce2016-05-16 14:01:24 -07003241 except ( TypeError, ValueError ):
3242 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) )
3243 return None
You Wang24139872016-05-03 11:48:47 -07003244 except pexpect.EOF:
3245 main.log.error( self.name + ": EOF exception found" )
3246 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003247 main.cleanAndExit()
You Wang24139872016-05-03 11:48:47 -07003248 except Exception:
3249 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003250 main.cleanAndExit()
You Wang24139872016-05-03 11:48:47 -07003251
Pier6a0c4de2018-03-18 16:01:30 -07003252 def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, logLevel="info" ):
kelvin8ec71442015-01-15 16:57:00 -08003253 """
Jon Hallefbd9792015-03-05 16:11:36 -08003254 Checks the number of switches & links that ONOS sees against the
kelvin8ec71442015-01-15 16:57:00 -08003255 supplied values. By default this will report to main.log, but the
You Wang24139872016-05-03 11:48:47 -07003256 log level can be specific.
kelvin8ec71442015-01-15 16:57:00 -08003257
Flavio Castro82ee2f62016-06-07 15:04:12 -07003258 Params: numoswitch = expected number of switches
Jon Hallefbd9792015-03-05 16:11:36 -08003259 numolink = expected number of links
Flavio Castro82ee2f62016-06-07 15:04:12 -07003260 numoctrl = expected number of controllers
You Wang24139872016-05-03 11:48:47 -07003261 logLevel = level to log to.
3262 Currently accepts 'info', 'warn' and 'report'
Jon Hall42db6dc2014-10-24 19:03:48 -04003263
Jon Hallefbd9792015-03-05 16:11:36 -08003264 Returns: main.TRUE if the number of switches and links are correct,
3265 main.FALSE if the number of switches and links is incorrect,
Jon Hall42db6dc2014-10-24 19:03:48 -04003266 and main.ERROR otherwise
kelvin8ec71442015-01-15 16:57:00 -08003267 """
Flavio Castro82ee2f62016-06-07 15:04:12 -07003268 import json
Jon Hall42db6dc2014-10-24 19:03:48 -04003269 try:
You Wang13310252016-07-31 10:56:14 -07003270 summary = self.summary()
3271 summary = json.loads( summary )
Flavio Castrof5b3f872016-06-23 17:52:31 -07003272 except ( TypeError, ValueError ):
3273 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) )
3274 return main.ERROR
3275 try:
3276 topology = self.getTopology( self.topology() )
Jon Halle0f0b342017-04-18 11:43:47 -07003277 if topology == {} or topology is None or summary == {} or summary is None:
Jon Hall42db6dc2014-10-24 19:03:48 -04003278 return main.ERROR
3279 output = ""
kelvin8ec71442015-01-15 16:57:00 -08003280 # Is the number of switches is what we expected
3281 devices = topology.get( 'devices', False )
3282 links = topology.get( 'links', False )
Flavio Castro82ee2f62016-06-07 15:04:12 -07003283 nodes = summary.get( 'nodes', False )
3284 if devices is False or links is False or nodes is False:
Jon Hall42db6dc2014-10-24 19:03:48 -04003285 return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -08003286 switchCheck = ( int( devices ) == int( numoswitch ) )
kelvin8ec71442015-01-15 16:57:00 -08003287 # Is the number of links is what we expected
Pier6a0c4de2018-03-18 16:01:30 -07003288 linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1
Flavio Castro82ee2f62016-06-07 15:04:12 -07003289 nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1
3290 if switchCheck and linkCheck and nodeCheck:
kelvin8ec71442015-01-15 16:57:00 -08003291 # We expected the correct numbers
You Wang24139872016-05-03 11:48:47 -07003292 output = output + "The number of links and switches match "\
3293 + "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04003294 result = main.TRUE
3295 else:
You Wang24139872016-05-03 11:48:47 -07003296 output = output + \
3297 "The number of links and switches does not match " + \
3298 "what was expected"
Jon Hall42db6dc2014-10-24 19:03:48 -04003299 result = main.FALSE
You Wang24139872016-05-03 11:48:47 -07003300 output = output + "\n ONOS sees %i devices" % int( devices )
3301 output = output + " (%i expected) " % int( numoswitch )
Pier6a0c4de2018-03-18 16:01:30 -07003302 if int( numolink ) > 0:
3303 output = output + "and %i links " % int( links )
3304 output = output + "(%i expected)" % int( numolink )
YPZhangd7e4b6e2016-06-17 16:07:55 -07003305 if int( numoctrl ) > 0:
Flavio Castro82ee2f62016-06-07 15:04:12 -07003306 output = output + "and %i controllers " % int( nodes )
3307 output = output + "(%i expected)" % int( numoctrl )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003308 if logLevel == "report":
kelvin8ec71442015-01-15 16:57:00 -08003309 main.log.report( output )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003310 elif logLevel == "warn":
kelvin8ec71442015-01-15 16:57:00 -08003311 main.log.warn( output )
Jon Hall42db6dc2014-10-24 19:03:48 -04003312 else:
You Wang24139872016-05-03 11:48:47 -07003313 main.log.info( output )
kelvin8ec71442015-01-15 16:57:00 -08003314 return result
Jon Hall42db6dc2014-10-24 19:03:48 -04003315 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003316 main.log.error( self.name + ": EOF exception found" )
3317 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003318 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003319 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003320 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003321 main.cleanAndExit()
Jon Hall1c9e8732014-10-27 19:29:27 -04003322
kelvin-onlabd3b64892015-01-20 13:26:24 -08003323 def deviceRole( self, deviceId, onosNode, role="master" ):
kelvin8ec71442015-01-15 16:57:00 -08003324 """
Jon Hall1c9e8732014-10-27 19:29:27 -04003325 Calls the device-role cli command.
kelvin-onlabd3b64892015-01-20 13:26:24 -08003326 deviceId must be the id of a device as seen in the onos devices command
3327 onosNode is the ip of one of the onos nodes in the cluster
Jon Hall1c9e8732014-10-27 19:29:27 -04003328 role must be either master, standby, or none
3329
Jon Halle3f39ff2015-01-13 11:50:53 -08003330 Returns:
3331 main.TRUE or main.FALSE based on argument verification and
3332 main.ERROR if command returns and error
kelvin-onlab898a6c62015-01-16 14:13:53 -08003333 """
Jon Hall1c9e8732014-10-27 19:29:27 -04003334 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08003335 if role.lower() == "master" or role.lower() == "standby" or\
Jon Hall1c9e8732014-10-27 19:29:27 -04003336 role.lower() == "none":
kelvin-onlabd3b64892015-01-20 13:26:24 -08003337 cmdStr = "device-role " +\
3338 str( deviceId ) + " " +\
3339 str( onosNode ) + " " +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08003340 str( role )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003341 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003342 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003343 assert "Command not found:" not in handle, handle
kelvin-onlab898a6c62015-01-16 14:13:53 -08003344 if re.search( "Error", handle ):
3345 # end color output to escape any colours
3346 # from the cli
kelvin8ec71442015-01-15 16:57:00 -08003347 main.log.error( self.name + ": " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08003348 handle + '\033[0m' )
kelvin8ec71442015-01-15 16:57:00 -08003349 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -08003350 return main.TRUE
Jon Hall1c9e8732014-10-27 19:29:27 -04003351 else:
kelvin-onlab898a6c62015-01-16 14:13:53 -08003352 main.log.error( "Invalid 'role' given to device_role(). " +
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003353 "Value was '" + str( role ) + "'." )
Jon Hall1c9e8732014-10-27 19:29:27 -04003354 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08003355 except AssertionError:
3356 main.log.exception( "" )
3357 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003358 except TypeError:
3359 main.log.exception( self.name + ": Object not as expected" )
3360 return None
Jon Hall1c9e8732014-10-27 19:29:27 -04003361 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003362 main.log.error( self.name + ": EOF exception found" )
3363 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003364 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003365 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003366 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003367 main.cleanAndExit()
Jon Hall1c9e8732014-10-27 19:29:27 -04003368
kelvin-onlabd3b64892015-01-20 13:26:24 -08003369 def clusters( self, jsonFormat=True ):
kelvin8ec71442015-01-15 16:57:00 -08003370 """
Jon Hall0dd09952018-04-19 09:59:11 -07003371 Lists all topology clusters
Jon Hallffb386d2014-11-21 13:43:38 -08003372 Optional argument:
kelvin-onlabd3b64892015-01-20 13:26:24 -08003373 * jsonFormat - boolean indicating if you want output in json
kelvin8ec71442015-01-15 16:57:00 -08003374 """
Jon Hall73cf9cc2014-11-20 22:28:38 -08003375 try:
Jon Hall0dd09952018-04-19 09:59:11 -07003376 cmdStr = "topo-clusters"
kelvin-onlabd3b64892015-01-20 13:26:24 -08003377 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07003378 cmdStr += " -j"
3379 handle = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003380 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003381 assert "Command not found:" not in handle, handle
Jon Hallc6358dd2015-04-10 12:44:28 -07003382 return handle
Jon Hallc6793552016-01-19 14:18:37 -08003383 except AssertionError:
3384 main.log.exception( "" )
3385 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003386 except TypeError:
3387 main.log.exception( self.name + ": Object not as expected" )
3388 return None
Jon Hall73cf9cc2014-11-20 22:28:38 -08003389 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003390 main.log.error( self.name + ": EOF exception found" )
3391 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003392 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003393 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003394 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003395 main.cleanAndExit()
Jon Hall73cf9cc2014-11-20 22:28:38 -08003396
kelvin-onlabd3b64892015-01-20 13:26:24 -08003397 def electionTestLeader( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08003398 """
Jon Halle3f39ff2015-01-13 11:50:53 -08003399 CLI command to get the current leader for the Election test application
3400 NOTE: Requires installation of the onos-app-election feature
3401 Returns: Node IP of the leader if one exists
3402 None if none exists
3403 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08003404 """
Jon Hall94fd0472014-12-08 11:52:42 -08003405 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08003406 cmdStr = "election-test-leader"
3407 response = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003408 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003409 assert "Command not found:" not in response, response
Jon Halle3f39ff2015-01-13 11:50:53 -08003410 # Leader
3411 leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08003412 "app\sis\s(?P<node>.+)\."
kelvin-onlabd3b64892015-01-20 13:26:24 -08003413 nodeSearch = re.search( leaderPattern, response )
3414 if nodeSearch:
3415 node = nodeSearch.group( 'node' )
Jon Halle3f39ff2015-01-13 11:50:53 -08003416 main.log.info( "Election-test-leader on " + str( self.name ) +
kelvin-onlab898a6c62015-01-16 14:13:53 -08003417 " found " + node + " as the leader" )
Jon Hall94fd0472014-12-08 11:52:42 -08003418 return node
Jon Halle3f39ff2015-01-13 11:50:53 -08003419 # no leader
3420 nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08003421 "the\sElection\sapp"
kelvin-onlabd3b64892015-01-20 13:26:24 -08003422 nullSearch = re.search( nullPattern, response )
3423 if nullSearch:
Jon Halle3f39ff2015-01-13 11:50:53 -08003424 main.log.info( "Election-test-leader found no leader on " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08003425 self.name )
Jon Hall94fd0472014-12-08 11:52:42 -08003426 return None
kelvin-onlab898a6c62015-01-16 14:13:53 -08003427 # error
Jon Hall0e240372018-05-02 11:21:57 -07003428 main.log.error( self.name + ": Error in electionTestLeader on " + self.name +
Jon Hall97cf84a2016-06-20 13:35:58 -07003429 ": " + "unexpected response" )
3430 main.log.error( repr( response ) )
3431 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08003432 except AssertionError:
3433 main.log.exception( "" )
3434 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003435 except TypeError:
3436 main.log.exception( self.name + ": Object not as expected" )
3437 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08003438 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003439 main.log.error( self.name + ": EOF exception found" )
3440 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003441 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003442 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003443 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003444 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08003445
kelvin-onlabd3b64892015-01-20 13:26:24 -08003446 def electionTestRun( self ):
kelvin-onlab898a6c62015-01-16 14:13:53 -08003447 """
Jon Halle3f39ff2015-01-13 11:50:53 -08003448 CLI command to run for leadership of the Election test application.
3449 NOTE: Requires installation of the onos-app-election feature
3450 Returns: Main.TRUE on success
3451 Main.FALSE on error
kelvin-onlab898a6c62015-01-16 14:13:53 -08003452 """
Jon Hall94fd0472014-12-08 11:52:42 -08003453 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08003454 cmdStr = "election-test-run"
3455 response = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003456 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003457 assert "Command not found:" not in response, response
kelvin-onlab898a6c62015-01-16 14:13:53 -08003458 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08003459 successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08003460 "Election\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08003461 search = re.search( successPattern, response )
Jon Hall94fd0472014-12-08 11:52:42 -08003462 if search:
Jon Halle3f39ff2015-01-13 11:50:53 -08003463 main.log.info( self.name + " entering leadership elections " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08003464 "for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08003465 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08003466 # error
Jon Hall0e240372018-05-02 11:21:57 -07003467 main.log.error( self.name + ": Error in electionTestRun on " + self.name +
Jon Hall97cf84a2016-06-20 13:35:58 -07003468 ": " + "unexpected response" )
3469 main.log.error( repr( response ) )
3470 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08003471 except AssertionError:
3472 main.log.exception( "" )
3473 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003474 except TypeError:
3475 main.log.exception( self.name + ": Object not as expected" )
3476 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08003477 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003478 main.log.error( self.name + ": EOF exception found" )
3479 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003480 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003481 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003482 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003483 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08003484
kelvin-onlabd3b64892015-01-20 13:26:24 -08003485 def electionTestWithdraw( self ):
kelvin8ec71442015-01-15 16:57:00 -08003486 """
Jon Hall94fd0472014-12-08 11:52:42 -08003487 * CLI command to withdraw the local node from leadership election for
3488 * the Election test application.
3489 #NOTE: Requires installation of the onos-app-election feature
3490 Returns: Main.TRUE on success
3491 Main.FALSE on error
kelvin8ec71442015-01-15 16:57:00 -08003492 """
Jon Hall94fd0472014-12-08 11:52:42 -08003493 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08003494 cmdStr = "election-test-withdraw"
3495 response = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003496 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003497 assert "Command not found:" not in response, response
kelvin-onlab898a6c62015-01-16 14:13:53 -08003498 # success
Jon Halle3f39ff2015-01-13 11:50:53 -08003499 successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\
kelvin-onlab898a6c62015-01-16 14:13:53 -08003500 "\sthe\sElection\sapp."
Jon Halle3f39ff2015-01-13 11:50:53 -08003501 if re.search( successPattern, response ):
3502 main.log.info( self.name + " withdrawing from leadership " +
kelvin-onlab898a6c62015-01-16 14:13:53 -08003503 "elections for the Election app." )
Jon Hall94fd0472014-12-08 11:52:42 -08003504 return main.TRUE
kelvin-onlab898a6c62015-01-16 14:13:53 -08003505 # error
Jon Hall0e240372018-05-02 11:21:57 -07003506 main.log.error( self.name + ": Error in electionTestWithdraw on " +
Jon Hall97cf84a2016-06-20 13:35:58 -07003507 self.name + ": " + "unexpected response" )
3508 main.log.error( repr( response ) )
3509 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08003510 except AssertionError:
3511 main.log.exception( "" )
3512 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003513 except TypeError:
3514 main.log.exception( self.name + ": Object not as expected" )
3515 return main.FALSE
Jon Hall94fd0472014-12-08 11:52:42 -08003516 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003517 main.log.error( self.name + ": EOF exception found" )
3518 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003519 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003520 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003521 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003522 main.cleanAndExit()
Jon Hall1c9e8732014-10-27 19:29:27 -04003523
kelvin8ec71442015-01-15 16:57:00 -08003524 def getDevicePortsEnabledCount( self, dpid ):
3525 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003526 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08003527 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003528 try:
Jon Halle3f39ff2015-01-13 11:50:53 -08003529 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003530 cmdStr = "onos:ports -e " + dpid + " | wc -l"
Jon Halld5a94fb2018-11-13 14:32:23 -08003531 output = self.lineCount( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003532 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003533 assert "Command not found:" not in output, output
Jon Halle3f39ff2015-01-13 11:50:53 -08003534 if re.search( "No such device", output ):
Jon Hall0e240372018-05-02 11:21:57 -07003535 main.log.error( self.name + ": Error in getting ports" )
Jon Halle3f39ff2015-01-13 11:50:53 -08003536 return ( output, "Error" )
Jon Halla495f562016-05-16 18:03:26 -07003537 return output
Jon Hallc6793552016-01-19 14:18:37 -08003538 except AssertionError:
3539 main.log.exception( "" )
3540 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003541 except TypeError:
3542 main.log.exception( self.name + ": Object not as expected" )
3543 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003544 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003545 main.log.error( self.name + ": EOF exception found" )
3546 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003547 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003548 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003549 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003550 main.cleanAndExit()
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003551
kelvin8ec71442015-01-15 16:57:00 -08003552 def getDeviceLinksActiveCount( self, dpid ):
3553 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003554 Get the count of all enabled ports on a particular device/switch
kelvin8ec71442015-01-15 16:57:00 -08003555 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003556 try:
kelvin-onlab898a6c62015-01-16 14:13:53 -08003557 dpid = str( dpid )
kelvin-onlabd3b64892015-01-20 13:26:24 -08003558 cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l"
Jon Halld5a94fb2018-11-13 14:32:23 -08003559 output = self.lineCount( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003560 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003561 assert "Command not found:" not in output, output
Jon Halle3f39ff2015-01-13 11:50:53 -08003562 if re.search( "No such device", output ):
Jon Hall0e240372018-05-02 11:21:57 -07003563 main.log.error( self.name + ": Error in getting ports " )
kelvin-onlab898a6c62015-01-16 14:13:53 -08003564 return ( output, "Error " )
Jon Halla495f562016-05-16 18:03:26 -07003565 return output
Jon Hallc6793552016-01-19 14:18:37 -08003566 except AssertionError:
3567 main.log.exception( "" )
3568 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003569 except TypeError:
3570 main.log.exception( self.name + ": Object not as expected" )
3571 return ( output, "Error " )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003572 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003573 main.log.error( self.name + ": EOF exception found" )
3574 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003575 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003576 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003577 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003578 main.cleanAndExit()
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003579
kelvin8ec71442015-01-15 16:57:00 -08003580 def getAllIntentIds( self ):
3581 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003582 Return a list of all Intent IDs
kelvin8ec71442015-01-15 16:57:00 -08003583 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003584 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08003585 cmdStr = "onos:intents | grep id="
3586 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003587 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003588 assert "Command not found:" not in output, output
Jon Halle3f39ff2015-01-13 11:50:53 -08003589 if re.search( "Error", output ):
Jon Hall0e240372018-05-02 11:21:57 -07003590 main.log.error( self.name + ": Error in getting ports" )
Jon Halle3f39ff2015-01-13 11:50:53 -08003591 return ( output, "Error" )
Jon Halla495f562016-05-16 18:03:26 -07003592 return output
Jon Hallc6793552016-01-19 14:18:37 -08003593 except AssertionError:
3594 main.log.exception( "" )
3595 return None
Jon Halld4d4b372015-01-28 16:02:41 -08003596 except TypeError:
3597 main.log.exception( self.name + ": Object not as expected" )
3598 return ( output, "Error" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003599 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08003600 main.log.error( self.name + ": EOF exception found" )
3601 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003602 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003603 except Exception:
Jon Halld4d4b372015-01-28 16:02:41 -08003604 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003605 main.cleanAndExit()
Jon Halld4d4b372015-01-28 16:02:41 -08003606
Jon Hall73509952015-02-24 16:42:56 -08003607 def intentSummary( self ):
3608 """
Jon Hallefbd9792015-03-05 16:11:36 -08003609 Returns a dictionary containing the current intent states and the count
Jon Hall73509952015-02-24 16:42:56 -08003610 """
3611 try:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00003612 intents = self.intents( )
Jon Hall08f61bc2015-04-13 16:00:30 -07003613 states = []
Jon Hall5aa168b2015-03-23 14:23:09 -07003614 for intent in json.loads( intents ):
Jon Hall08f61bc2015-04-13 16:00:30 -07003615 states.append( intent.get( 'state', None ) )
3616 out = [ ( i, states.count( i ) ) for i in set( states ) ]
Jon Hall63604932015-02-26 17:09:50 -08003617 main.log.info( dict( out ) )
Jon Hall73509952015-02-24 16:42:56 -08003618 return dict( out )
Jon Hallc6793552016-01-19 14:18:37 -08003619 except ( TypeError, ValueError ):
3620 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) )
Jon Hall73509952015-02-24 16:42:56 -08003621 return None
3622 except pexpect.EOF:
3623 main.log.error( self.name + ": EOF exception found" )
3624 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003625 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08003626 except Exception:
Jon Hall73509952015-02-24 16:42:56 -08003627 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003628 main.cleanAndExit()
Jon Hall63604932015-02-26 17:09:50 -08003629
Jon Hall61282e32015-03-19 11:34:11 -07003630 def leaders( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08003631 """
3632 Returns the output of the leaders command.
Jon Hall61282e32015-03-19 11:34:11 -07003633 Optional argument:
3634 * jsonFormat - boolean indicating if you want output in json
Jon Hall63604932015-02-26 17:09:50 -08003635 """
Jon Hall63604932015-02-26 17:09:50 -08003636 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07003637 cmdStr = "onos:leaders"
Jon Hall61282e32015-03-19 11:34:11 -07003638 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07003639 cmdStr += " -j"
3640 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003641 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003642 assert "Command not found:" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -07003643 return output
Jon Hallc6793552016-01-19 14:18:37 -08003644 except AssertionError:
3645 main.log.exception( "" )
3646 return None
Jon Hall63604932015-02-26 17:09:50 -08003647 except TypeError:
3648 main.log.exception( self.name + ": Object not as expected" )
3649 return None
Hari Krishnaa43d4e92014-12-19 13:22:40 -08003650 except pexpect.EOF:
3651 main.log.error( self.name + ": EOF exception found" )
3652 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003653 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003654 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08003655 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003656 main.cleanAndExit()
Jon Hall63604932015-02-26 17:09:50 -08003657
acsmarsa4a4d1e2015-07-10 16:01:24 -07003658 def leaderCandidates( self, jsonFormat=True ):
3659 """
3660 Returns the output of the leaders -c command.
3661 Optional argument:
3662 * jsonFormat - boolean indicating if you want output in json
3663 """
3664 try:
3665 cmdStr = "onos:leaders -c"
3666 if jsonFormat:
3667 cmdStr += " -j"
3668 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003669 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003670 assert "Command not found:" not in output, output
acsmarsa4a4d1e2015-07-10 16:01:24 -07003671 return output
Jon Hallc6793552016-01-19 14:18:37 -08003672 except AssertionError:
3673 main.log.exception( "" )
3674 return None
acsmarsa4a4d1e2015-07-10 16:01:24 -07003675 except TypeError:
3676 main.log.exception( self.name + ": Object not as expected" )
3677 return None
3678 except pexpect.EOF:
3679 main.log.error( self.name + ": EOF exception found" )
3680 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003681 main.cleanAndExit()
acsmarsa4a4d1e2015-07-10 16:01:24 -07003682 except Exception:
3683 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003684 main.cleanAndExit()
acsmarsa4a4d1e2015-07-10 16:01:24 -07003685
Jon Hallc6793552016-01-19 14:18:37 -08003686 def specificLeaderCandidate( self, topic ):
acsmarsa4a4d1e2015-07-10 16:01:24 -07003687 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00003688 Returns a list in format [leader,candidate1,candidate2,...] for a given
acsmarsa4a4d1e2015-07-10 16:01:24 -07003689 topic parameter and an empty list if the topic doesn't exist
3690 If no leader is elected leader in the returned list will be "none"
3691 Returns None if there is a type error processing the json object
3692 """
3693 try:
Jon Hall6e709752016-02-01 13:38:46 -08003694 cmdStr = "onos:leaders -j"
Jon Hallc6793552016-01-19 14:18:37 -08003695 rawOutput = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003696 assert rawOutput is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003697 assert "Command not found:" not in rawOutput, rawOutput
3698 output = json.loads( rawOutput )
acsmarsa4a4d1e2015-07-10 16:01:24 -07003699 results = []
3700 for dict in output:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003701 if dict[ "topic" ] == topic:
3702 leader = dict[ "leader" ]
3703 candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] )
Jon Hallc6793552016-01-19 14:18:37 -08003704 results.append( leader )
3705 results.extend( candidates )
acsmarsa4a4d1e2015-07-10 16:01:24 -07003706 return results
Jon Hallc6793552016-01-19 14:18:37 -08003707 except AssertionError:
3708 main.log.exception( "" )
3709 return None
3710 except ( TypeError, ValueError ):
3711 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) )
acsmarsa4a4d1e2015-07-10 16:01:24 -07003712 return None
3713 except pexpect.EOF:
3714 main.log.error( self.name + ": EOF exception found" )
3715 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003716 main.cleanAndExit()
acsmarsa4a4d1e2015-07-10 16:01:24 -07003717 except Exception:
3718 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003719 main.cleanAndExit()
acsmarsa4a4d1e2015-07-10 16:01:24 -07003720
Jon Hall61282e32015-03-19 11:34:11 -07003721 def pendingMap( self, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08003722 """
3723 Returns the output of the intent Pending map.
3724 """
Jon Hall63604932015-02-26 17:09:50 -08003725 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07003726 cmdStr = "onos:intents -p"
Jon Hall61282e32015-03-19 11:34:11 -07003727 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07003728 cmdStr += " -j"
3729 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003730 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003731 assert "Command not found:" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -07003732 return output
Jon Hallc6793552016-01-19 14:18:37 -08003733 except AssertionError:
3734 main.log.exception( "" )
3735 return None
Jon Hall63604932015-02-26 17:09:50 -08003736 except TypeError:
3737 main.log.exception( self.name + ": Object not as expected" )
3738 return None
3739 except pexpect.EOF:
3740 main.log.error( self.name + ": EOF exception found" )
3741 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003742 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003743 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08003744 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003745 main.cleanAndExit()
Jon Hall63604932015-02-26 17:09:50 -08003746
Jon Hall2c8959e2016-12-16 12:17:34 -08003747 def partitions( self, candidates=False, jsonFormat=True ):
Jon Hall63604932015-02-26 17:09:50 -08003748 """
3749 Returns the output of the raft partitions command for ONOS.
3750 """
Jon Hall61282e32015-03-19 11:34:11 -07003751 # Sample JSON
3752 # {
3753 # "leader": "tcp://10.128.30.11:7238",
3754 # "members": [
3755 # "tcp://10.128.30.11:7238",
3756 # "tcp://10.128.30.17:7238",
3757 # "tcp://10.128.30.13:7238",
3758 # ],
3759 # "name": "p1",
3760 # "term": 3
3761 # },
Jon Hall63604932015-02-26 17:09:50 -08003762 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07003763 cmdStr = "onos:partitions"
Jon Hall2c8959e2016-12-16 12:17:34 -08003764 if candidates:
3765 cmdStr += " -c"
Jon Hall61282e32015-03-19 11:34:11 -07003766 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07003767 cmdStr += " -j"
3768 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003769 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003770 assert "Command not found:" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -07003771 return output
Jon Hallc6793552016-01-19 14:18:37 -08003772 except AssertionError:
3773 main.log.exception( "" )
3774 return None
Jon Hall63604932015-02-26 17:09:50 -08003775 except TypeError:
3776 main.log.exception( self.name + ": Object not as expected" )
3777 return None
3778 except pexpect.EOF:
3779 main.log.error( self.name + ": EOF exception found" )
3780 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003781 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003782 except Exception:
Jon Hall63604932015-02-26 17:09:50 -08003783 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003784 main.cleanAndExit()
Jon Hall63604932015-02-26 17:09:50 -08003785
Jon Halle9f909e2016-09-23 10:43:12 -07003786 def apps( self, summary=False, active=False, jsonFormat=True ):
Jon Hallbe379602015-03-24 13:39:32 -07003787 """
3788 Returns the output of the apps command for ONOS. This command lists
3789 information about installed ONOS applications
3790 """
3791 # Sample JSON object
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00003792 # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0",
Jon Hallbe379602015-03-24 13:39:32 -07003793 # "description":"ONOS OpenFlow protocol southbound providers",
3794 # "origin":"ON.Lab","permissions":"[]","featuresRepo":"",
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00003795 # "features":"[onos-openflow]","state":"ACTIVE"}]
Jon Hallbe379602015-03-24 13:39:32 -07003796 try:
Jon Hallc6358dd2015-04-10 12:44:28 -07003797 cmdStr = "onos:apps"
Jon Halle9f909e2016-09-23 10:43:12 -07003798 if summary:
3799 cmdStr += " -s"
3800 if active:
3801 cmdStr += " -a"
Jon Hallbe379602015-03-24 13:39:32 -07003802 if jsonFormat:
Jon Hallc6358dd2015-04-10 12:44:28 -07003803 cmdStr += " -j"
3804 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07003805 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08003806 assert "Command not found:" not in output, output
3807 assert "Error executing command" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -07003808 return output
Jon Hallbe379602015-03-24 13:39:32 -07003809 # FIXME: look at specific exceptions/Errors
3810 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07003811 main.log.exception( self.name + ": Error in processing onos:app command." )
Jon Hallbe379602015-03-24 13:39:32 -07003812 return None
3813 except TypeError:
3814 main.log.exception( self.name + ": Object not as expected" )
3815 return None
3816 except pexpect.EOF:
3817 main.log.error( self.name + ": EOF exception found" )
3818 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003819 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003820 except Exception:
Jon Hallbe379602015-03-24 13:39:32 -07003821 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003822 main.cleanAndExit()
Jon Hallbe379602015-03-24 13:39:32 -07003823
You Wangcdc51fe2018-08-12 17:14:56 -07003824 def appStatus( self, appName ):
Jon Hall146f1522015-03-24 15:33:24 -07003825 """
3826 Uses the onos:apps cli command to return the status of an application.
3827 Returns:
3828 "ACTIVE" - If app is installed and activated
3829 "INSTALLED" - If app is installed and deactivated
3830 "UNINSTALLED" - If app is not installed
3831 None - on error
3832 """
Jon Hall146f1522015-03-24 15:33:24 -07003833 try:
3834 if not isinstance( appName, types.StringType ):
3835 main.log.error( self.name + ".appStatus(): appName must be" +
3836 " a string" )
3837 return None
3838 output = self.apps( jsonFormat=True )
3839 appsJson = json.loads( output )
3840 state = None
3841 for app in appsJson:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003842 if appName == app.get( 'name' ):
3843 state = app.get( 'state' )
Jon Hall146f1522015-03-24 15:33:24 -07003844 break
3845 if state == "ACTIVE" or state == "INSTALLED":
3846 return state
3847 elif state is None:
You Wang0d9f2c02018-08-10 14:56:32 -07003848 main.log.warn( "{} app not found".format( appName ) )
Jon Hall146f1522015-03-24 15:33:24 -07003849 return "UNINSTALLED"
3850 elif state:
3851 main.log.error( "Unexpected state from 'onos:apps': " +
3852 str( state ) )
3853 return state
Jon Hallc6793552016-01-19 14:18:37 -08003854 except ( TypeError, ValueError ):
3855 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) )
Jon Hall146f1522015-03-24 15:33:24 -07003856 return None
3857 except pexpect.EOF:
3858 main.log.error( self.name + ": EOF exception found" )
3859 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003860 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003861 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003862 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003863 main.cleanAndExit()
Jon Hall146f1522015-03-24 15:33:24 -07003864
Jon Hallbe379602015-03-24 13:39:32 -07003865 def app( self, appName, option ):
3866 """
3867 Interacts with the app command for ONOS. This command manages
3868 application inventory.
3869 """
Jon Hallbe379602015-03-24 13:39:32 -07003870 try:
Jon Hallbd16b922015-03-26 17:53:15 -07003871 # Validate argument types
3872 valid = True
3873 if not isinstance( appName, types.StringType ):
3874 main.log.error( self.name + ".app(): appName must be a " +
3875 "string" )
3876 valid = False
3877 if not isinstance( option, types.StringType ):
3878 main.log.error( self.name + ".app(): option must be a string" )
3879 valid = False
3880 if not valid:
3881 return main.FALSE
3882 # Validate Option
3883 option = option.lower()
3884 # NOTE: Install may become a valid option
3885 if option == "activate":
3886 pass
3887 elif option == "deactivate":
3888 pass
3889 elif option == "uninstall":
3890 pass
3891 else:
3892 # Invalid option
3893 main.log.error( "The ONOS app command argument only takes " +
3894 "the values: (activate|deactivate|uninstall)" +
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003895 "; was given '" + option + "'" )
Jon Hallbd16b922015-03-26 17:53:15 -07003896 return main.FALSE
Jon Hall146f1522015-03-24 15:33:24 -07003897 cmdStr = "onos:app " + option + " " + appName
Jon Hallbe379602015-03-24 13:39:32 -07003898 output = self.sendline( cmdStr )
You Wangb5a55f72017-03-03 12:51:05 -08003899 assert output is not None, "Error in sendline"
3900 assert "Command not found:" not in output, output
Jon Hallbe379602015-03-24 13:39:32 -07003901 if "Error executing command" in output:
Jon Hall0e240372018-05-02 11:21:57 -07003902 main.log.error( self.name + ": Error in processing onos:app command: " +
Jon Hallbe379602015-03-24 13:39:32 -07003903 str( output ) )
Jon Hall146f1522015-03-24 15:33:24 -07003904 return main.FALSE
Jon Hallbe379602015-03-24 13:39:32 -07003905 elif "No such application" in output:
3906 main.log.error( "The application '" + appName +
3907 "' is not installed in ONOS" )
Jon Hall146f1522015-03-24 15:33:24 -07003908 return main.FALSE
3909 elif "Command not found:" in output:
Jon Hall0e240372018-05-02 11:21:57 -07003910 main.log.error( self.name + ": Error in processing onos:app command: " +
Jon Hall146f1522015-03-24 15:33:24 -07003911 str( output ) )
3912 return main.FALSE
Jon Hallbd16b922015-03-26 17:53:15 -07003913 elif "Unsupported command:" in output:
3914 main.log.error( "Incorrect command given to 'app': " +
3915 str( output ) )
Jon Hallbe379602015-03-24 13:39:32 -07003916 # NOTE: we may need to add more checks here
Jon Hallbd16b922015-03-26 17:53:15 -07003917 # else: Command was successful
Jon Hall08f61bc2015-04-13 16:00:30 -07003918 # main.log.debug( "app response: " + repr( output ) )
Jon Hallbe379602015-03-24 13:39:32 -07003919 return main.TRUE
You Wangb5a55f72017-03-03 12:51:05 -08003920 except AssertionError:
3921 main.log.exception( self.name + ": AssertionError exception found" )
3922 return main.ERROR
Jon Hallbe379602015-03-24 13:39:32 -07003923 except TypeError:
3924 main.log.exception( self.name + ": Object not as expected" )
3925 return main.ERROR
3926 except pexpect.EOF:
3927 main.log.error( self.name + ": EOF exception found" )
3928 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003929 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003930 except Exception:
Jon Hallbe379602015-03-24 13:39:32 -07003931 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003932 main.cleanAndExit()
Jon Hall146f1522015-03-24 15:33:24 -07003933
Jon Hallbd16b922015-03-26 17:53:15 -07003934 def activateApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07003935 """
3936 Activate an app that is already installed in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07003937 appName is the hierarchical app name, not the feature name
3938 If check is True, method will check the status of the app after the
3939 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07003940 Returns main.TRUE if the command was successfully sent
3941 main.FALSE if the cli responded with an error or given
3942 incorrect input
3943 """
3944 try:
3945 if not isinstance( appName, types.StringType ):
3946 main.log.error( self.name + ".activateApp(): appName must be" +
3947 " a string" )
3948 return main.FALSE
3949 status = self.appStatus( appName )
3950 if status == "INSTALLED":
3951 response = self.app( appName, "activate" )
Jon Hallbd16b922015-03-26 17:53:15 -07003952 if check and response == main.TRUE:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07003953 for i in range( 10 ): # try 10 times then give up
Jon Hallbd16b922015-03-26 17:53:15 -07003954 status = self.appStatus( appName )
3955 if status == "ACTIVE":
3956 return main.TRUE
3957 else:
Jon Hall050e1bd2015-03-30 13:33:02 -07003958 main.log.debug( "The state of application " +
3959 appName + " is " + status )
Jon Hallbd16b922015-03-26 17:53:15 -07003960 time.sleep( 1 )
3961 return main.FALSE
3962 else: # not 'check' or command didn't succeed
3963 return response
Jon Hall146f1522015-03-24 15:33:24 -07003964 elif status == "ACTIVE":
3965 return main.TRUE
3966 elif status == "UNINSTALLED":
3967 main.log.error( self.name + ": Tried to activate the " +
3968 "application '" + appName + "' which is not " +
3969 "installed." )
3970 else:
3971 main.log.error( "Unexpected return value from appStatus: " +
3972 str( status ) )
3973 return main.ERROR
3974 except TypeError:
3975 main.log.exception( self.name + ": Object not as expected" )
3976 return main.ERROR
3977 except pexpect.EOF:
3978 main.log.error( self.name + ": EOF exception found" )
3979 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07003980 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07003981 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07003982 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07003983 main.cleanAndExit()
Jon Hall146f1522015-03-24 15:33:24 -07003984
Jon Hallbd16b922015-03-26 17:53:15 -07003985 def deactivateApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07003986 """
3987 Deactivate an app that is already activated in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07003988 appName is the hierarchical app name, not the feature name
3989 If check is True, method will check the status of the app after the
3990 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07003991 Returns main.TRUE if the command was successfully sent
3992 main.FALSE if the cli responded with an error or given
3993 incorrect input
3994 """
3995 try:
3996 if not isinstance( appName, types.StringType ):
3997 main.log.error( self.name + ".deactivateApp(): appName must " +
3998 "be a string" )
3999 return main.FALSE
4000 status = self.appStatus( appName )
4001 if status == "INSTALLED":
4002 return main.TRUE
4003 elif status == "ACTIVE":
4004 response = self.app( appName, "deactivate" )
Jon Hallbd16b922015-03-26 17:53:15 -07004005 if check and response == main.TRUE:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004006 for i in range( 10 ): # try 10 times then give up
Jon Hallbd16b922015-03-26 17:53:15 -07004007 status = self.appStatus( appName )
4008 if status == "INSTALLED":
4009 return main.TRUE
4010 else:
4011 time.sleep( 1 )
4012 return main.FALSE
4013 else: # not check or command didn't succeed
4014 return response
Jon Hall146f1522015-03-24 15:33:24 -07004015 elif status == "UNINSTALLED":
4016 main.log.warn( self.name + ": Tried to deactivate the " +
4017 "application '" + appName + "' which is not " +
4018 "installed." )
4019 return main.TRUE
4020 else:
4021 main.log.error( "Unexpected return value from appStatus: " +
4022 str( status ) )
4023 return main.ERROR
4024 except TypeError:
4025 main.log.exception( self.name + ": Object not as expected" )
4026 return main.ERROR
4027 except pexpect.EOF:
4028 main.log.error( self.name + ": EOF exception found" )
4029 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004030 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07004031 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07004032 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004033 main.cleanAndExit()
Jon Hall146f1522015-03-24 15:33:24 -07004034
Jon Hallbd16b922015-03-26 17:53:15 -07004035 def uninstallApp( self, appName, check=True ):
Jon Hall146f1522015-03-24 15:33:24 -07004036 """
4037 Uninstall an app that is already installed in ONOS
Jon Hallbd16b922015-03-26 17:53:15 -07004038 appName is the hierarchical app name, not the feature name
4039 If check is True, method will check the status of the app after the
4040 command is issued
Jon Hall146f1522015-03-24 15:33:24 -07004041 Returns main.TRUE if the command was successfully sent
4042 main.FALSE if the cli responded with an error or given
4043 incorrect input
4044 """
4045 # TODO: check with Thomas about the state machine for apps
4046 try:
4047 if not isinstance( appName, types.StringType ):
4048 main.log.error( self.name + ".uninstallApp(): appName must " +
4049 "be a string" )
4050 return main.FALSE
4051 status = self.appStatus( appName )
4052 if status == "INSTALLED":
4053 response = self.app( appName, "uninstall" )
Jon Hallbd16b922015-03-26 17:53:15 -07004054 if check and response == main.TRUE:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004055 for i in range( 10 ): # try 10 times then give up
Jon Hallbd16b922015-03-26 17:53:15 -07004056 status = self.appStatus( appName )
4057 if status == "UNINSTALLED":
4058 return main.TRUE
4059 else:
4060 time.sleep( 1 )
4061 return main.FALSE
4062 else: # not check or command didn't succeed
4063 return response
Jon Hall146f1522015-03-24 15:33:24 -07004064 elif status == "ACTIVE":
4065 main.log.warn( self.name + ": Tried to uninstall the " +
4066 "application '" + appName + "' which is " +
4067 "currently active." )
4068 response = self.app( appName, "uninstall" )
Jon Hallbd16b922015-03-26 17:53:15 -07004069 if check and response == main.TRUE:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004070 for i in range( 10 ): # try 10 times then give up
Jon Hallbd16b922015-03-26 17:53:15 -07004071 status = self.appStatus( appName )
4072 if status == "UNINSTALLED":
4073 return main.TRUE
4074 else:
4075 time.sleep( 1 )
4076 return main.FALSE
4077 else: # not check or command didn't succeed
4078 return response
Jon Hall146f1522015-03-24 15:33:24 -07004079 elif status == "UNINSTALLED":
4080 return main.TRUE
4081 else:
4082 main.log.error( "Unexpected return value from appStatus: " +
4083 str( status ) )
4084 return main.ERROR
4085 except TypeError:
4086 main.log.exception( self.name + ": Object not as expected" )
4087 return main.ERROR
4088 except pexpect.EOF:
4089 main.log.error( self.name + ": EOF exception found" )
4090 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004091 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07004092 except Exception:
Jon Hall146f1522015-03-24 15:33:24 -07004093 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004094 main.cleanAndExit()
Jon Hallbd16b922015-03-26 17:53:15 -07004095
4096 def appIDs( self, jsonFormat=True ):
4097 """
4098 Show the mappings between app id and app names given by the 'app-ids'
4099 cli command
4100 """
4101 try:
4102 cmdStr = "app-ids"
4103 if jsonFormat:
4104 cmdStr += " -j"
Jon Hallc6358dd2015-04-10 12:44:28 -07004105 output = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07004106 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004107 assert "Command not found:" not in output, output
4108 assert "Error executing command" not in output, output
Jon Hallc6358dd2015-04-10 12:44:28 -07004109 return output
Jon Hallbd16b922015-03-26 17:53:15 -07004110 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004111 main.log.exception( self.name + ": Error in processing onos:app-ids command." )
Jon Hallbd16b922015-03-26 17:53:15 -07004112 return None
4113 except TypeError:
4114 main.log.exception( self.name + ": Object not as expected" )
4115 return None
4116 except pexpect.EOF:
4117 main.log.error( self.name + ": EOF exception found" )
4118 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004119 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07004120 except Exception:
Jon Hallbd16b922015-03-26 17:53:15 -07004121 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004122 main.cleanAndExit()
Jon Hallbd16b922015-03-26 17:53:15 -07004123
4124 def appToIDCheck( self ):
4125 """
4126 This method will check that each application's ID listed in 'apps' is
4127 the same as the ID listed in 'app-ids'. The check will also check that
4128 there are no duplicate IDs issued. Note that an app ID should be
4129 a globaly unique numerical identifier for app/app-like features. Once
4130 an ID is registered, the ID is never freed up so that if an app is
4131 reinstalled it will have the same ID.
4132
4133 Returns: main.TRUE if the check passes and
4134 main.FALSE if the check fails or
4135 main.ERROR if there is some error in processing the test
4136 """
4137 try:
Jon Hall0e240372018-05-02 11:21:57 -07004138 # Grab IDs
Jon Hallc6793552016-01-19 14:18:37 -08004139 rawJson = self.appIDs( jsonFormat=True )
4140 if rawJson:
4141 ids = json.loads( rawJson )
Jon Hall390696c2015-05-05 17:13:41 -07004142 else:
Jon Hall0e240372018-05-02 11:21:57 -07004143 main.log.error( "app-ids returned nothing: " + repr( rawJson ) )
4144 return main.FALSE
4145
4146 # Grab Apps
Jon Hallc6793552016-01-19 14:18:37 -08004147 rawJson = self.apps( jsonFormat=True )
4148 if rawJson:
4149 apps = json.loads( rawJson )
Jon Hall390696c2015-05-05 17:13:41 -07004150 else:
Jon Hallc6793552016-01-19 14:18:37 -08004151 main.log.error( "apps returned nothing:" + repr( rawJson ) )
Jon Hall390696c2015-05-05 17:13:41 -07004152 return main.FALSE
Jon Hall0e240372018-05-02 11:21:57 -07004153
Jon Hallbd16b922015-03-26 17:53:15 -07004154 result = main.TRUE
4155 for app in apps:
4156 appID = app.get( 'id' )
4157 if appID is None:
4158 main.log.error( "Error parsing app: " + str( app ) )
4159 result = main.FALSE
4160 appName = app.get( 'name' )
4161 if appName is None:
4162 main.log.error( "Error parsing app: " + str( app ) )
4163 result = main.FALSE
4164 # get the entry in ids that has the same appID
Jon Hall390696c2015-05-05 17:13:41 -07004165 current = filter( lambda item: item[ 'id' ] == appID, ids )
Jon Hallbd16b922015-03-26 17:53:15 -07004166 if not current: # if ids doesn't have this id
4167 result = main.FALSE
4168 main.log.error( "'app-ids' does not have the ID for " +
4169 str( appName ) + " that apps does." )
Jon Hallb9d381e2018-02-05 12:02:10 -08004170 main.log.debug( "apps command returned: " + str( app ) +
4171 "; app-ids has: " + str( ids ) )
Jon Hallbd16b922015-03-26 17:53:15 -07004172 elif len( current ) > 1:
4173 # there is more than one app with this ID
4174 result = main.FALSE
4175 # We will log this later in the method
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004176 elif not current[ 0 ][ 'name' ] == appName:
4177 currentName = current[ 0 ][ 'name' ]
Jon Hallbd16b922015-03-26 17:53:15 -07004178 result = main.FALSE
4179 main.log.error( "'app-ids' has " + str( currentName ) +
4180 " registered under id:" + str( appID ) +
4181 " but 'apps' has " + str( appName ) )
4182 else:
4183 pass # id and name match!
Jon Hall0e240372018-05-02 11:21:57 -07004184
Jon Hallbd16b922015-03-26 17:53:15 -07004185 # now make sure that app-ids has no duplicates
4186 idsList = []
4187 namesList = []
4188 for item in ids:
4189 idsList.append( item[ 'id' ] )
4190 namesList.append( item[ 'name' ] )
4191 if len( idsList ) != len( set( idsList ) ) or\
4192 len( namesList ) != len( set( namesList ) ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004193 main.log.error( "'app-ids' has some duplicate entries: \n"
4194 + json.dumps( ids,
4195 sort_keys=True,
4196 indent=4,
4197 separators=( ',', ': ' ) ) )
4198 result = main.FALSE
Jon Hallbd16b922015-03-26 17:53:15 -07004199 return result
Jon Hallc6793552016-01-19 14:18:37 -08004200 except ( TypeError, ValueError ):
4201 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) )
Jon Hallbd16b922015-03-26 17:53:15 -07004202 return main.ERROR
4203 except pexpect.EOF:
4204 main.log.error( self.name + ": EOF exception found" )
4205 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004206 main.cleanAndExit()
Jon Hall77ba41c2015-04-06 10:25:40 -07004207 except Exception:
Jon Hallbd16b922015-03-26 17:53:15 -07004208 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004209 main.cleanAndExit()
Jon Hallbd16b922015-03-26 17:53:15 -07004210
Jon Hallfb760a02015-04-13 15:35:03 -07004211 def getCfg( self, component=None, propName=None, short=False,
4212 jsonFormat=True ):
4213 """
4214 Get configuration settings from onos cli
4215 Optional arguments:
4216 component - Optionally only list configurations for a specific
4217 component. If None, all components with configurations
4218 are displayed. Case Sensitive string.
4219 propName - If component is specified, propName option will show
4220 only this specific configuration from that component.
4221 Case Sensitive string.
4222 jsonFormat - Returns output as json. Note that this will override
4223 the short option
4224 short - Short, less verbose, version of configurations.
4225 This is overridden by the json option
4226 returns:
4227 Output from cli as a string or None on error
4228 """
4229 try:
4230 baseStr = "cfg"
4231 cmdStr = " get"
4232 componentStr = ""
4233 if component:
4234 componentStr += " " + component
4235 if propName:
4236 componentStr += " " + propName
4237 if jsonFormat:
4238 baseStr += " -j"
Jon Hall22e94ce2019-01-15 14:52:17 -08004239 expectJson = True
Jon Hallfb760a02015-04-13 15:35:03 -07004240 elif short:
4241 baseStr += " -s"
Jon Hall22e94ce2019-01-15 14:52:17 -08004242 expectJson = False
4243 output = self.sendline( baseStr + cmdStr + componentStr, expectJson=expectJson )
Jon Halla495f562016-05-16 18:03:26 -07004244 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004245 assert "Command not found:" not in output, output
4246 assert "Error executing command" not in output, output
Jon Hallfb760a02015-04-13 15:35:03 -07004247 return output
4248 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004249 main.log.exception( self.name + ": Error in processing 'cfg get' command." )
Jon Hallfb760a02015-04-13 15:35:03 -07004250 return None
4251 except TypeError:
4252 main.log.exception( self.name + ": Object not as expected" )
4253 return None
4254 except pexpect.EOF:
4255 main.log.error( self.name + ": EOF exception found" )
4256 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004257 main.cleanAndExit()
Jon Hallfb760a02015-04-13 15:35:03 -07004258 except Exception:
4259 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004260 main.cleanAndExit()
Jon Hallfb760a02015-04-13 15:35:03 -07004261
4262 def setCfg( self, component, propName, value=None, check=True ):
4263 """
4264 Set/Unset configuration settings from ONOS cli
Jon Hall390696c2015-05-05 17:13:41 -07004265 Required arguments:
Jon Hallfb760a02015-04-13 15:35:03 -07004266 component - The case sensitive name of the component whose
4267 property is to be set
4268 propName - The case sensitive name of the property to be set/unset
Jon Hall390696c2015-05-05 17:13:41 -07004269 Optional arguments:
Jon Hallfb760a02015-04-13 15:35:03 -07004270 value - The value to set the property to. If None, will unset the
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004271 property and revert it to it's default value(if applicable)
Jon Hallfb760a02015-04-13 15:35:03 -07004272 check - Boolean, Check whether the option was successfully set this
4273 only applies when a value is given.
4274 returns:
4275 main.TRUE on success or main.FALSE on failure. If check is False,
4276 will return main.TRUE unless there is an error
4277 """
4278 try:
4279 baseStr = "cfg"
4280 cmdStr = " set " + str( component ) + " " + str( propName )
4281 if value is not None:
4282 cmdStr += " " + str( value )
4283 output = self.sendline( baseStr + cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07004284 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004285 assert "Command not found:" not in output, output
4286 assert "Error executing command" not in output, output
Jon Hallfb760a02015-04-13 15:35:03 -07004287 if value and check:
4288 results = self.getCfg( component=str( component ),
4289 propName=str( propName ),
4290 jsonFormat=True )
4291 # Check if current value is what we just set
4292 try:
4293 jsonOutput = json.loads( results )
4294 current = jsonOutput[ 'value' ]
Jon Hallc6793552016-01-19 14:18:37 -08004295 except ( TypeError, ValueError ):
Jon Hallfb760a02015-04-13 15:35:03 -07004296 main.log.exception( "Error parsing cfg output" )
4297 main.log.error( "output:" + repr( results ) )
4298 return main.FALSE
4299 if current == str( value ):
4300 return main.TRUE
4301 return main.FALSE
4302 return main.TRUE
4303 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004304 main.log.exception( self.name + ": Error in processing 'cfg set' command." )
Jon Hallfb760a02015-04-13 15:35:03 -07004305 return main.FALSE
Jon Hallc6793552016-01-19 14:18:37 -08004306 except ( TypeError, ValueError ):
4307 main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) )
Jon Hallfb760a02015-04-13 15:35:03 -07004308 return main.FALSE
4309 except pexpect.EOF:
4310 main.log.error( self.name + ": EOF exception found" )
4311 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004312 main.cleanAndExit()
Jon Hallfb760a02015-04-13 15:35:03 -07004313 except Exception:
4314 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004315 main.cleanAndExit()
Jon Hallfb760a02015-04-13 15:35:03 -07004316
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004317 def distPrimitivesSend( self, cmd ):
4318 """
4319 Function to handle sending cli commands for the distributed primitives test app
4320
4321 This command will catch some exceptions and retry the command on some
4322 specific store exceptions.
4323
4324 Required arguments:
4325 cmd - The command to send to the cli
4326 returns:
4327 string containing the cli output
4328 None on Error
4329 """
4330 try:
4331 output = self.sendline( cmd )
4332 try:
4333 assert output is not None, "Error in sendline"
4334 # TODO: Maybe make this less hardcoded
4335 # ConsistentMap Exceptions
4336 assert "org.onosproject.store.service" not in output
4337 # Node not leader
4338 assert "java.lang.IllegalStateException" not in output
4339 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004340 main.log.error( self.name + ": Error in processing '" + cmd + "' " +
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004341 "command: " + str( output ) )
4342 retryTime = 30 # Conservative time, given by Madan
4343 main.log.info( "Waiting " + str( retryTime ) +
4344 "seconds before retrying." )
4345 time.sleep( retryTime ) # Due to change in mastership
4346 output = self.sendline( cmd )
4347 assert output is not None, "Error in sendline"
4348 assert "Command not found:" not in output, output
4349 assert "Error executing command" not in output, output
4350 main.log.info( self.name + ": " + output )
4351 return output
4352 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004353 main.log.exception( self.name + ": Error in processing '" + cmd + "' command." )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004354 return None
4355 except TypeError:
4356 main.log.exception( self.name + ": Object not as expected" )
4357 return None
4358 except pexpect.EOF:
4359 main.log.error( self.name + ": EOF exception found" )
4360 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004361 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004362 except Exception:
4363 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004364 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004365
Jon Hall390696c2015-05-05 17:13:41 -07004366 def setTestAdd( self, setName, values ):
4367 """
4368 CLI command to add elements to a distributed set.
4369 Arguments:
4370 setName - The name of the set to add to.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004371 values - The value(s) to add to the set, space seperated.
Jon Hall390696c2015-05-05 17:13:41 -07004372 Example usages:
4373 setTestAdd( "set1", "a b c" )
4374 setTestAdd( "set2", "1" )
4375 returns:
4376 main.TRUE on success OR
4377 main.FALSE if elements were already in the set OR
4378 main.ERROR on error
4379 """
4380 try:
4381 cmdStr = "set-test-add " + str( setName ) + " " + str( values )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004382 output = self.distPrimitivesSend( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07004383 positiveMatch = "\[(.*)\] was added to the set " + str( setName )
4384 negativeMatch = "\[(.*)\] was already in set " + str( setName )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004385 if re.search( positiveMatch, output ):
Jon Hall390696c2015-05-05 17:13:41 -07004386 return main.TRUE
Jeremy Ronquillo82705492017-10-18 14:19:55 -07004387 elif re.search( negativeMatch, output ):
Jon Hall390696c2015-05-05 17:13:41 -07004388 return main.FALSE
4389 else:
4390 main.log.error( self.name + ": setTestAdd did not" +
4391 " match expected output" )
Jon Hall390696c2015-05-05 17:13:41 -07004392 main.log.debug( self.name + " actual: " + repr( output ) )
4393 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004394 except TypeError:
4395 main.log.exception( self.name + ": Object not as expected" )
4396 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004397 except Exception:
4398 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004399 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004400
4401 def setTestRemove( self, setName, values, clear=False, retain=False ):
4402 """
4403 CLI command to remove elements from a distributed set.
4404 Required arguments:
4405 setName - The name of the set to remove from.
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004406 values - The value(s) to remove from the set, space seperated.
Jon Hall390696c2015-05-05 17:13:41 -07004407 Optional arguments:
4408 clear - Clear all elements from the set
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004409 retain - Retain only the given values. (intersection of the
4410 original set and the given set)
Jon Hall390696c2015-05-05 17:13:41 -07004411 returns:
4412 main.TRUE on success OR
4413 main.FALSE if the set was not changed OR
4414 main.ERROR on error
4415 """
4416 try:
4417 cmdStr = "set-test-remove "
4418 if clear:
4419 cmdStr += "-c " + str( setName )
4420 elif retain:
4421 cmdStr += "-r " + str( setName ) + " " + str( values )
4422 else:
4423 cmdStr += str( setName ) + " " + str( values )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004424 output = self.distPrimitivesSend( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07004425 if clear:
4426 pattern = "Set " + str( setName ) + " cleared"
4427 if re.search( pattern, output ):
4428 return main.TRUE
4429 elif retain:
4430 positivePattern = str( setName ) + " was pruned to contain " +\
4431 "only elements of set \[(.*)\]"
4432 negativePattern = str( setName ) + " was not changed by " +\
4433 "retaining only elements of the set " +\
4434 "\[(.*)\]"
4435 if re.search( positivePattern, output ):
4436 return main.TRUE
4437 elif re.search( negativePattern, output ):
4438 return main.FALSE
4439 else:
4440 positivePattern = "\[(.*)\] was removed from the set " +\
4441 str( setName )
4442 if ( len( values.split() ) == 1 ):
4443 negativePattern = "\[(.*)\] was not in set " +\
4444 str( setName )
4445 else:
4446 negativePattern = "No element of \[(.*)\] was in set " +\
4447 str( setName )
4448 if re.search( positivePattern, output ):
4449 return main.TRUE
4450 elif re.search( negativePattern, output ):
4451 return main.FALSE
4452 main.log.error( self.name + ": setTestRemove did not" +
4453 " match expected output" )
4454 main.log.debug( self.name + " expected: " + pattern )
4455 main.log.debug( self.name + " actual: " + repr( output ) )
4456 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004457 except TypeError:
4458 main.log.exception( self.name + ": Object not as expected" )
4459 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004460 except Exception:
4461 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004462 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004463
4464 def setTestGet( self, setName, values="" ):
4465 """
4466 CLI command to get the elements in a distributed set.
4467 Required arguments:
4468 setName - The name of the set to remove from.
4469 Optional arguments:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004470 values - The value(s) to check if in the set, space seperated.
Jon Hall390696c2015-05-05 17:13:41 -07004471 returns:
4472 main.ERROR on error OR
4473 A list of elements in the set if no optional arguments are
4474 supplied OR
4475 A tuple containing the list then:
4476 main.FALSE if the given values are not in the set OR
4477 main.TRUE if the given values are in the set OR
4478 """
4479 try:
4480 values = str( values ).strip()
4481 setName = str( setName ).strip()
4482 length = len( values.split() )
4483 containsCheck = None
4484 # Patterns to match
4485 setPattern = "\[(.*)\]"
Jon Hall67253832016-12-05 09:47:13 -08004486 pattern = "Items in set " + setName + ":\r\n" + setPattern
Jon Hall390696c2015-05-05 17:13:41 -07004487 containsTrue = "Set " + setName + " contains the value " + values
4488 containsFalse = "Set " + setName + " did not contain the value " +\
4489 values
4490 containsAllTrue = "Set " + setName + " contains the the subset " +\
4491 setPattern
4492 containsAllFalse = "Set " + setName + " did not contain the the" +\
4493 " subset " + setPattern
4494
4495 cmdStr = "set-test-get "
4496 cmdStr += setName + " " + values
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004497 output = self.distPrimitivesSend( cmdStr )
Jon Hall390696c2015-05-05 17:13:41 -07004498 if length == 0:
4499 match = re.search( pattern, output )
4500 else: # if given values
4501 if length == 1: # Contains output
Jon Hall54b994f2016-12-05 10:48:59 -08004502 patternTrue = pattern + "\r\n" + containsTrue
4503 patternFalse = pattern + "\r\n" + containsFalse
Jon Hall390696c2015-05-05 17:13:41 -07004504 else: # ContainsAll output
Jon Hall54b994f2016-12-05 10:48:59 -08004505 patternTrue = pattern + "\r\n" + containsAllTrue
4506 patternFalse = pattern + "\r\n" + containsAllFalse
Jon Hall390696c2015-05-05 17:13:41 -07004507 matchTrue = re.search( patternTrue, output )
4508 matchFalse = re.search( patternFalse, output )
4509 if matchTrue:
4510 containsCheck = main.TRUE
4511 match = matchTrue
4512 elif matchFalse:
4513 containsCheck = main.FALSE
4514 match = matchFalse
4515 else:
Jon Halle0f0b342017-04-18 11:43:47 -07004516 main.log.error( self.name + " setTestGet did not match " +
Jon Hall390696c2015-05-05 17:13:41 -07004517 "expected output" )
4518 main.log.debug( self.name + " expected: " + pattern )
4519 main.log.debug( self.name + " actual: " + repr( output ) )
4520 match = None
4521 if match:
4522 setMatch = match.group( 1 )
4523 if setMatch == '':
4524 setList = []
4525 else:
4526 setList = setMatch.split( ", " )
4527 if length > 0:
4528 return ( setList, containsCheck )
4529 else:
4530 return setList
4531 else: # no match
4532 main.log.error( self.name + ": setTestGet did not" +
4533 " match expected output" )
4534 main.log.debug( self.name + " expected: " + pattern )
4535 main.log.debug( self.name + " actual: " + repr( output ) )
4536 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004537 except TypeError:
4538 main.log.exception( self.name + ": Object not as expected" )
4539 return main.ERROR
Jon Hall390696c2015-05-05 17:13:41 -07004540 except Exception:
4541 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004542 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004543
4544 def setTestSize( self, setName ):
4545 """
4546 CLI command to get the elements in a distributed set.
4547 Required arguments:
4548 setName - The name of the set to remove from.
4549 returns:
Jon Hallfeff3082015-05-19 10:23:26 -07004550 The integer value of the size returned or
Jon Hall390696c2015-05-05 17:13:41 -07004551 None on error
4552 """
4553 try:
4554 # TODO: Should this check against the number of elements returned
4555 # and then return true/false based on that?
4556 setName = str( setName ).strip()
4557 # Patterns to match
4558 setPattern = "\[(.*)\]"
Jon Hall67253832016-12-05 09:47:13 -08004559 pattern = "There are (\d+) items in set " + setName + ":\r\n" +\
Jon Hall390696c2015-05-05 17:13:41 -07004560 setPattern
4561 cmdStr = "set-test-get -s "
4562 cmdStr += setName
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004563 output = self.distPrimitivesSend( cmdStr )
Jon Hall0e240372018-05-02 11:21:57 -07004564 if output:
4565 match = re.search( pattern, output )
4566 if match:
4567 setSize = int( match.group( 1 ) )
4568 setMatch = match.group( 2 )
4569 if len( setMatch.split() ) == setSize:
4570 main.log.info( "The size returned by " + self.name +
4571 " matches the number of elements in " +
4572 "the returned set" )
4573 else:
4574 main.log.error( "The size returned by " + self.name +
4575 " does not match the number of " +
4576 "elements in the returned set." )
4577 return setSize
Jon Hall390696c2015-05-05 17:13:41 -07004578 else: # no match
4579 main.log.error( self.name + ": setTestGet did not" +
4580 " match expected output" )
4581 main.log.debug( self.name + " expected: " + pattern )
4582 main.log.debug( self.name + " actual: " + repr( output ) )
4583 return None
Jon Hall390696c2015-05-05 17:13:41 -07004584 except TypeError:
4585 main.log.exception( self.name + ": Object not as expected" )
4586 return None
Jon Hall390696c2015-05-05 17:13:41 -07004587 except Exception:
4588 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004589 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004590
Jon Hall80daded2015-05-27 16:07:00 -07004591 def counters( self, jsonFormat=True ):
Jon Hall390696c2015-05-05 17:13:41 -07004592 """
4593 Command to list the various counters in the system.
4594 returns:
Jon Hall80daded2015-05-27 16:07:00 -07004595 if jsonFormat, a string of the json object returned by the cli
4596 command
4597 if not jsonFormat, the normal string output of the cli command
Jon Hall390696c2015-05-05 17:13:41 -07004598 None on error
4599 """
Jon Hall390696c2015-05-05 17:13:41 -07004600 try:
Jon Hall390696c2015-05-05 17:13:41 -07004601 cmdStr = "counters"
Jon Hall80daded2015-05-27 16:07:00 -07004602 if jsonFormat:
4603 cmdStr += " -j"
Jon Hall390696c2015-05-05 17:13:41 -07004604 output = self.sendline( cmdStr )
Jon Hall22e94ce2019-01-15 14:52:17 -08004605 main.log.debug( self.name + ": Counters unparsed: " + output )
4606 output = output.split( "\r\n" )[ -1 ]
4607 main.log.debug( self.name + ": Counters parsed: " + output )
Jon Halla495f562016-05-16 18:03:26 -07004608 assert output is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004609 assert "Command not found:" not in output, output
4610 assert "Error executing command" not in output, output
Jon Hall390696c2015-05-05 17:13:41 -07004611 main.log.info( self.name + ": " + output )
Jon Hall80daded2015-05-27 16:07:00 -07004612 return output
Jon Hall390696c2015-05-05 17:13:41 -07004613 except AssertionError:
Jon Hall0e240372018-05-02 11:21:57 -07004614 main.log.exception( self.name + ": Error in processing 'counters' command." )
Jon Hall80daded2015-05-27 16:07:00 -07004615 return None
Jon Hall390696c2015-05-05 17:13:41 -07004616 except TypeError:
4617 main.log.exception( self.name + ": Object not as expected" )
Jon Hall80daded2015-05-27 16:07:00 -07004618 return None
Jon Hall390696c2015-05-05 17:13:41 -07004619 except pexpect.EOF:
4620 main.log.error( self.name + ": EOF exception found" )
4621 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004622 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004623 except Exception:
4624 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004625 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004626
Jon Hall935db192016-04-19 00:22:04 -07004627 def counterTestAddAndGet( self, counter, delta=1 ):
Jon Hall390696c2015-05-05 17:13:41 -07004628 """
Jon Halle1a3b752015-07-22 13:02:46 -07004629 CLI command to add a delta to then get a distributed counter.
Jon Hall390696c2015-05-05 17:13:41 -07004630 Required arguments:
4631 counter - The name of the counter to increment.
4632 Optional arguments:
Jon Halle1a3b752015-07-22 13:02:46 -07004633 delta - The long to add to the counter
Jon Hall390696c2015-05-05 17:13:41 -07004634 returns:
4635 integer value of the counter or
4636 None on Error
4637 """
4638 try:
4639 counter = str( counter )
Jon Halle1a3b752015-07-22 13:02:46 -07004640 delta = int( delta )
Jon Hall390696c2015-05-05 17:13:41 -07004641 cmdStr = "counter-test-increment "
Jon Hall390696c2015-05-05 17:13:41 -07004642 cmdStr += counter
Jon Halle1a3b752015-07-22 13:02:46 -07004643 if delta != 1:
4644 cmdStr += " " + str( delta )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004645 output = self.distPrimitivesSend( cmdStr )
Jon Halle1a3b752015-07-22 13:02:46 -07004646 pattern = counter + " was updated to (-?\d+)"
Jon Hall390696c2015-05-05 17:13:41 -07004647 match = re.search( pattern, output )
4648 if match:
4649 return int( match.group( 1 ) )
4650 else:
Jon Halle1a3b752015-07-22 13:02:46 -07004651 main.log.error( self.name + ": counterTestAddAndGet did not" +
Jon Hall390696c2015-05-05 17:13:41 -07004652 " match expected output." )
4653 main.log.debug( self.name + " expected: " + pattern )
4654 main.log.debug( self.name + " actual: " + repr( output ) )
4655 return None
Jon Hall390696c2015-05-05 17:13:41 -07004656 except TypeError:
4657 main.log.exception( self.name + ": Object not as expected" )
4658 return None
Jon Hall390696c2015-05-05 17:13:41 -07004659 except Exception:
4660 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004661 main.cleanAndExit()
Jon Hall390696c2015-05-05 17:13:41 -07004662
Jon Hall935db192016-04-19 00:22:04 -07004663 def counterTestGetAndAdd( self, counter, delta=1 ):
Jon Halle1a3b752015-07-22 13:02:46 -07004664 """
4665 CLI command to get a distributed counter then add a delta to it.
4666 Required arguments:
4667 counter - The name of the counter to increment.
4668 Optional arguments:
4669 delta - The long to add to the counter
Jon Halle1a3b752015-07-22 13:02:46 -07004670 returns:
4671 integer value of the counter or
4672 None on Error
4673 """
4674 try:
4675 counter = str( counter )
4676 delta = int( delta )
4677 cmdStr = "counter-test-increment -g "
Jon Halle1a3b752015-07-22 13:02:46 -07004678 cmdStr += counter
4679 if delta != 1:
4680 cmdStr += " " + str( delta )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004681 output = self.distPrimitivesSend( cmdStr )
Jon Halle1a3b752015-07-22 13:02:46 -07004682 pattern = counter + " was updated to (-?\d+)"
4683 match = re.search( pattern, output )
4684 if match:
4685 return int( match.group( 1 ) )
4686 else:
4687 main.log.error( self.name + ": counterTestGetAndAdd did not" +
4688 " match expected output." )
4689 main.log.debug( self.name + " expected: " + pattern )
4690 main.log.debug( self.name + " actual: " + repr( output ) )
4691 return None
Jon Halle1a3b752015-07-22 13:02:46 -07004692 except TypeError:
4693 main.log.exception( self.name + ": Object not as expected" )
4694 return None
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004695 except Exception:
4696 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004697 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004698
4699 def valueTestGet( self, valueName ):
4700 """
4701 CLI command to get the value of an atomic value.
4702 Required arguments:
4703 valueName - The name of the value to get.
4704 returns:
4705 string value of the value or
4706 None on Error
4707 """
4708 try:
4709 valueName = str( valueName )
4710 cmdStr = "value-test "
4711 operation = "get"
4712 cmdStr = "value-test {} {}".format( valueName,
4713 operation )
4714 output = self.distPrimitivesSend( cmdStr )
Jon Hall22e94ce2019-01-15 14:52:17 -08004715 main.log.debug( self.name + ": value test unparsed: " + output )
4716 output = output.split( "\r\n" )[ -1 ]
4717 main.log.debug( self.name + ": value test parsed: " + output )
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004718 pattern = "(\w+)"
4719 match = re.search( pattern, output )
4720 if match:
4721 return match.group( 1 )
4722 else:
4723 main.log.error( self.name + ": valueTestGet did not" +
4724 " match expected output." )
4725 main.log.debug( self.name + " expected: " + pattern )
4726 main.log.debug( self.name + " actual: " + repr( output ) )
4727 return None
4728 except TypeError:
4729 main.log.exception( self.name + ": Object not as expected" )
4730 return None
4731 except Exception:
4732 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004733 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004734
4735 def valueTestSet( self, valueName, newValue ):
4736 """
4737 CLI command to set the value of an atomic value.
4738 Required arguments:
4739 valueName - The name of the value to set.
4740 newValue - The value to assign to the given value.
4741 returns:
4742 main.TRUE on success or
4743 main.ERROR on Error
4744 """
4745 try:
4746 valueName = str( valueName )
4747 newValue = str( newValue )
4748 operation = "set"
4749 cmdStr = "value-test {} {} {}".format( valueName,
4750 operation,
4751 newValue )
4752 output = self.distPrimitivesSend( cmdStr )
4753 if output is not None:
4754 return main.TRUE
4755 else:
4756 return main.ERROR
4757 except TypeError:
4758 main.log.exception( self.name + ": Object not as expected" )
4759 return main.ERROR
4760 except Exception:
4761 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004762 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004763
4764 def valueTestCompareAndSet( self, valueName, oldValue, newValue ):
4765 """
4766 CLI command to compareAndSet the value of an atomic value.
4767 Required arguments:
4768 valueName - The name of the value.
4769 oldValue - Compare the current value of the atomic value to this
4770 newValue - If the value equals oldValue, set the value to newValue
4771 returns:
4772 main.TRUE on success or
4773 main.FALSE on failure or
4774 main.ERROR on Error
4775 """
4776 try:
4777 valueName = str( valueName )
4778 oldValue = str( oldValue )
4779 newValue = str( newValue )
4780 operation = "compareAndSet"
4781 cmdStr = "value-test {} {} {} {}".format( valueName,
4782 operation,
4783 oldValue,
4784 newValue )
4785 output = self.distPrimitivesSend( cmdStr )
4786 pattern = "(\w+)"
4787 match = re.search( pattern, output )
4788 if match:
4789 result = match.group( 1 )
4790 if result == "true":
4791 return main.TRUE
4792 elif result == "false":
4793 return main.FALSE
4794 else:
4795 main.log.error( self.name + ": valueTestCompareAndSet did not" +
4796 " match expected output." )
4797 main.log.debug( self.name + " expected: " + pattern )
4798 main.log.debug( self.name + " actual: " + repr( output ) )
4799 return main.ERROR
4800 except TypeError:
4801 main.log.exception( self.name + ": Object not as expected" )
4802 return main.ERROR
4803 except Exception:
4804 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004805 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004806
4807 def valueTestGetAndSet( self, valueName, newValue ):
4808 """
4809 CLI command to getAndSet the value of an atomic value.
4810 Required arguments:
4811 valueName - The name of the value to get.
4812 newValue - The value to assign to the given value
4813 returns:
4814 string value of the value or
4815 None on Error
4816 """
4817 try:
4818 valueName = str( valueName )
4819 cmdStr = "value-test "
4820 operation = "getAndSet"
4821 cmdStr += valueName + " " + operation
4822 cmdStr = "value-test {} {} {}".format( valueName,
4823 operation,
4824 newValue )
4825 output = self.distPrimitivesSend( cmdStr )
4826 pattern = "(\w+)"
4827 match = re.search( pattern, output )
4828 if match:
4829 return match.group( 1 )
4830 else:
4831 main.log.error( self.name + ": valueTestGetAndSet 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
4839 except Exception:
4840 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004841 main.cleanAndExit()
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004842
4843 def valueTestDestroy( self, valueName ):
4844 """
4845 CLI command to destroy an atomic value.
4846 Required arguments:
4847 valueName - The name of the value to destroy.
4848 returns:
4849 main.TRUE on success or
4850 main.ERROR on Error
4851 """
4852 try:
4853 valueName = str( valueName )
4854 cmdStr = "value-test "
4855 operation = "destroy"
4856 cmdStr += valueName + " " + operation
4857 output = self.distPrimitivesSend( cmdStr )
4858 if output is not None:
4859 return main.TRUE
4860 else:
4861 return main.ERROR
4862 except TypeError:
4863 main.log.exception( self.name + ": Object not as expected" )
4864 return main.ERROR
Jon Halle1a3b752015-07-22 13:02:46 -07004865 except Exception:
4866 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004867 main.cleanAndExit()
Jon Halle1a3b752015-07-22 13:02:46 -07004868
YPZhangfebf7302016-05-24 16:45:56 -07004869 def summary( self, jsonFormat=True, timeout=30 ):
kelvin-onlaba297c4d2015-06-01 13:53:55 -07004870 """
4871 Description: Execute summary command in onos
4872 Returns: json object ( summary -j ), returns main.FALSE if there is
4873 no output
4874
4875 """
4876 try:
4877 cmdStr = "summary"
4878 if jsonFormat:
4879 cmdStr += " -j"
YPZhangfebf7302016-05-24 16:45:56 -07004880 handle = self.sendline( cmdStr, timeout=timeout )
Jon Halla495f562016-05-16 18:03:26 -07004881 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08004882 assert "Command not found:" not in handle, handle
Jon Hall6e709752016-02-01 13:38:46 -08004883 assert "Error:" not in handle, handle
Devin Lima7cfdbd2017-09-29 15:02:22 -07004884 assert "Error executing" not in handle, handle
kelvin-onlaba297c4d2015-06-01 13:53:55 -07004885 if not handle:
4886 main.log.error( self.name + ": There is no output in " +
4887 "summary command" )
4888 return main.FALSE
4889 return handle
Jon Hallc6793552016-01-19 14:18:37 -08004890 except AssertionError:
Jon Hall6e709752016-02-01 13:38:46 -08004891 main.log.exception( "{} Error in summary output:".format( self.name ) )
Jon Hallc6793552016-01-19 14:18:37 -08004892 return None
kelvin-onlaba297c4d2015-06-01 13:53:55 -07004893 except TypeError:
4894 main.log.exception( self.name + ": Object not as expected" )
4895 return None
4896 except pexpect.EOF:
4897 main.log.error( self.name + ": EOF exception found" )
4898 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07004899 main.cleanAndExit()
kelvin-onlaba297c4d2015-06-01 13:53:55 -07004900 except Exception:
4901 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004902 main.cleanAndExit()
Jon Hall2a5002c2015-08-21 16:49:11 -07004903
Jon Hall935db192016-04-19 00:22:04 -07004904 def transactionalMapGet( self, keyName ):
Jon Hall2a5002c2015-08-21 16:49:11 -07004905 """
4906 CLI command to get the value of a key in a consistent map using
4907 transactions. This a test function and can only get keys from the
4908 test map hard coded into the cli command
4909 Required arguments:
4910 keyName - The name of the key to get
Jon Hall2a5002c2015-08-21 16:49:11 -07004911 returns:
4912 The string value of the key or
4913 None on Error
4914 """
4915 try:
4916 keyName = str( keyName )
4917 cmdStr = "transactional-map-test-get "
Jon Hall2a5002c2015-08-21 16:49:11 -07004918 cmdStr += keyName
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004919 output = self.distPrimitivesSend( cmdStr )
Jon Hall2a5002c2015-08-21 16:49:11 -07004920 pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found."
4921 if "Key " + keyName + " not found." in output:
Jon Hall9bfadd22016-05-11 14:48:07 -07004922 main.log.warn( output )
Jon Hall2a5002c2015-08-21 16:49:11 -07004923 return None
4924 else:
4925 match = re.search( pattern, output )
4926 if match:
4927 return match.groupdict()[ 'value' ]
4928 else:
4929 main.log.error( self.name + ": transactionlMapGet did not" +
4930 " match expected output." )
4931 main.log.debug( self.name + " expected: " + pattern )
4932 main.log.debug( self.name + " actual: " + repr( output ) )
4933 return None
4934 except TypeError:
4935 main.log.exception( self.name + ": Object not as expected" )
4936 return None
Jon Hall2a5002c2015-08-21 16:49:11 -07004937 except Exception:
4938 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004939 main.cleanAndExit()
Jon Hall2a5002c2015-08-21 16:49:11 -07004940
Jon Hall935db192016-04-19 00:22:04 -07004941 def transactionalMapPut( self, numKeys, value ):
Jon Hall2a5002c2015-08-21 16:49:11 -07004942 """
4943 CLI command to put a value into 'numKeys' number of keys in a
4944 consistent map using transactions. This a test function and can only
4945 put into keys named 'Key#' of the test map hard coded into the cli command
4946 Required arguments:
4947 numKeys - Number of keys to add the value to
4948 value - The string value to put into the keys
Jon Hall2a5002c2015-08-21 16:49:11 -07004949 returns:
4950 A dictionary whose keys are the name of the keys put into the map
4951 and the values of the keys are dictionaries whose key-values are
4952 'value': value put into map and optionaly
4953 'oldValue': Previous value in the key or
4954 None on Error
4955
4956 Example output
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004957 { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'},
4958 'Key2': {'value': 'Testing'} }
Jon Hall2a5002c2015-08-21 16:49:11 -07004959 """
4960 try:
4961 numKeys = str( numKeys )
4962 value = str( value )
4963 cmdStr = "transactional-map-test-put "
Jon Hall2a5002c2015-08-21 16:49:11 -07004964 cmdStr += numKeys + " " + value
Jon Hall7a6ebfd2017-03-13 10:58:58 -07004965 output = self.distPrimitivesSend( cmdStr )
Jon Hall2a5002c2015-08-21 16:49:11 -07004966 newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.'
4967 updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\."
4968 results = {}
4969 for line in output.splitlines():
4970 new = re.search( newPattern, line )
4971 updated = re.search( updatedPattern, line )
4972 if new:
4973 results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] }
4974 elif updated:
4975 results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ],
Jon Hallc6793552016-01-19 14:18:37 -08004976 'oldValue': updated.groupdict()[ 'oldValue' ] }
Jon Hall2a5002c2015-08-21 16:49:11 -07004977 else:
4978 main.log.error( self.name + ": transactionlMapGet did not" +
4979 " match expected output." )
Jon Hallc6793552016-01-19 14:18:37 -08004980 main.log.debug( "{} expected: {!r} or {!r}".format( self.name,
4981 newPattern,
4982 updatedPattern ) )
Jon Hall2a5002c2015-08-21 16:49:11 -07004983 main.log.debug( self.name + " actual: " + repr( output ) )
4984 return results
Jon Hall0e240372018-05-02 11:21:57 -07004985 except ( TypeError, AttributeError ):
Jon Hall2a5002c2015-08-21 16:49:11 -07004986 main.log.exception( self.name + ": Object not as expected" )
4987 return None
Jon Hall2a5002c2015-08-21 16:49:11 -07004988 except Exception:
4989 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07004990 main.cleanAndExit()
Jon Hallc6793552016-01-19 14:18:37 -08004991
acsmarsdaea66c2015-09-03 11:44:06 -07004992 def maps( self, jsonFormat=True ):
4993 """
4994 Description: Returns result of onos:maps
4995 Optional:
4996 * jsonFormat: enable json formatting of output
4997 """
4998 try:
4999 cmdStr = "maps"
5000 if jsonFormat:
5001 cmdStr += " -j"
5002 handle = self.sendline( cmdStr )
Jon Halla495f562016-05-16 18:03:26 -07005003 assert handle is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08005004 assert "Command not found:" not in handle, handle
acsmarsdaea66c2015-09-03 11:44:06 -07005005 return handle
Jon Hallc6793552016-01-19 14:18:37 -08005006 except AssertionError:
5007 main.log.exception( "" )
5008 return None
acsmarsdaea66c2015-09-03 11:44:06 -07005009 except TypeError:
5010 main.log.exception( self.name + ": Object not as expected" )
5011 return None
5012 except pexpect.EOF:
5013 main.log.error( self.name + ": EOF exception found" )
5014 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005015 main.cleanAndExit()
acsmarsdaea66c2015-09-03 11:44:06 -07005016 except Exception:
5017 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005018 main.cleanAndExit()
GlennRC050596c2015-11-18 17:06:41 -08005019
5020 def getSwController( self, uri, jsonFormat=True ):
5021 """
5022 Descrition: Gets the controller information from the device
5023 """
5024 try:
5025 cmd = "device-controllers "
5026 if jsonFormat:
5027 cmd += "-j "
5028 response = self.sendline( cmd + uri )
Jon Halla495f562016-05-16 18:03:26 -07005029 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08005030 assert "Command not found:" not in response, response
GlennRC050596c2015-11-18 17:06:41 -08005031 return response
Jon Hallc6793552016-01-19 14:18:37 -08005032 except AssertionError:
5033 main.log.exception( "" )
5034 return None
GlennRC050596c2015-11-18 17:06:41 -08005035 except TypeError:
5036 main.log.exception( self.name + ": Object not as expected" )
5037 return None
5038 except pexpect.EOF:
5039 main.log.error( self.name + ": EOF exception found" )
5040 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005041 main.cleanAndExit()
GlennRC050596c2015-11-18 17:06:41 -08005042 except Exception:
5043 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005044 main.cleanAndExit()
GlennRC050596c2015-11-18 17:06:41 -08005045
5046 def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ):
5047 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005048 Descrition: sets the controller(s) for the specified device
GlennRC050596c2015-11-18 17:06:41 -08005049
5050 Parameters:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005051 Required: uri - String: The uri of the device(switch).
GlennRC050596c2015-11-18 17:06:41 -08005052 ip - String or List: The ip address of the controller.
5053 This parameter can be formed in a couple of different ways.
5054 VALID:
5055 10.0.0.1 - just the ip address
5056 tcp:10.0.0.1 - the protocol and the ip address
5057 tcp:10.0.0.1:6653 - the protocol and port can be specified,
5058 so that you can add controllers with different
5059 protocols and ports
5060 INVALID:
5061 10.0.0.1:6653 - this is not supported by ONOS
5062
5063 Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given
5064 port - The port number.
5065 jsonFormat - If set ONOS will output in json NOTE: This is currently not supported
5066
5067 Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE
5068 """
5069 try:
5070 cmd = "device-setcontrollers"
5071
5072 if jsonFormat:
5073 cmd += " -j"
5074 cmd += " " + uri
5075 if isinstance( ip, str ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005076 ip = [ ip ]
GlennRC050596c2015-11-18 17:06:41 -08005077 for item in ip:
5078 if ":" in item:
5079 sitem = item.split( ":" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005080 if len( sitem ) == 3:
GlennRC050596c2015-11-18 17:06:41 -08005081 cmd += " " + item
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005082 elif "." in sitem[ 1 ]:
5083 cmd += " {}:{}".format( item, port )
GlennRC050596c2015-11-18 17:06:41 -08005084 else:
5085 main.log.error( "Malformed entry: " + item )
5086 raise TypeError
5087 else:
5088 cmd += " {}:{}:{}".format( proto, item, port )
GlennRC050596c2015-11-18 17:06:41 -08005089 response = self.sendline( cmd )
Jon Halla495f562016-05-16 18:03:26 -07005090 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08005091 assert "Command not found:" not in response, response
GlennRC050596c2015-11-18 17:06:41 -08005092 if "Error" in response:
5093 main.log.error( response )
5094 return main.FALSE
GlennRC050596c2015-11-18 17:06:41 -08005095 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08005096 except AssertionError:
5097 main.log.exception( "" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005098 return main.FALSE
GlennRC050596c2015-11-18 17:06:41 -08005099 except TypeError:
5100 main.log.exception( self.name + ": Object not as expected" )
5101 return main.FALSE
5102 except pexpect.EOF:
5103 main.log.error( self.name + ": EOF exception found" )
5104 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005105 main.cleanAndExit()
GlennRC050596c2015-11-18 17:06:41 -08005106 except Exception:
5107 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005108 main.cleanAndExit()
GlennRC20fc6522015-12-23 23:26:57 -08005109
5110 def removeDevice( self, device ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005111 '''
GlennRC20fc6522015-12-23 23:26:57 -08005112 Description:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005113 Remove a device from ONOS by passing the uri of the device(s).
GlennRC20fc6522015-12-23 23:26:57 -08005114 Parameters:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005115 device - (str or list) the id or uri of the device ex. "of:0000000000000001"
GlennRC20fc6522015-12-23 23:26:57 -08005116 Returns:
5117 Returns main.FALSE if an exception is thrown or an error is present
5118 in the response. Otherwise, returns main.TRUE.
5119 NOTE:
5120 If a host cannot be removed, then this function will return main.FALSE
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005121 '''
GlennRC20fc6522015-12-23 23:26:57 -08005122 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005123 if isinstance( device, str ):
You Wang823f5022016-08-18 15:24:41 -07005124 deviceStr = device
5125 device = []
5126 device.append( deviceStr )
GlennRC20fc6522015-12-23 23:26:57 -08005127
5128 for d in device:
5129 time.sleep( 1 )
5130 response = self.sendline( "device-remove {}".format( d ) )
Jon Halla495f562016-05-16 18:03:26 -07005131 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08005132 assert "Command not found:" not in response, response
GlennRC20fc6522015-12-23 23:26:57 -08005133 if "Error" in response:
5134 main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) )
5135 return main.FALSE
GlennRC20fc6522015-12-23 23:26:57 -08005136 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08005137 except AssertionError:
5138 main.log.exception( "" )
5139 return main.FALSE
GlennRC20fc6522015-12-23 23:26:57 -08005140 except TypeError:
5141 main.log.exception( self.name + ": Object not as expected" )
5142 return main.FALSE
5143 except pexpect.EOF:
5144 main.log.error( self.name + ": EOF exception found" )
5145 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005146 main.cleanAndExit()
GlennRC20fc6522015-12-23 23:26:57 -08005147 except Exception:
5148 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005149 main.cleanAndExit()
GlennRC20fc6522015-12-23 23:26:57 -08005150
5151 def removeHost( self, host ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005152 '''
GlennRC20fc6522015-12-23 23:26:57 -08005153 Description:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005154 Remove a host from ONOS by passing the id of the host(s)
GlennRC20fc6522015-12-23 23:26:57 -08005155 Parameters:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005156 hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01"
GlennRC20fc6522015-12-23 23:26:57 -08005157 Returns:
5158 Returns main.FALSE if an exception is thrown or an error is present
5159 in the response. Otherwise, returns main.TRUE.
5160 NOTE:
5161 If a host cannot be removed, then this function will return main.FALSE
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005162 '''
GlennRC20fc6522015-12-23 23:26:57 -08005163 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005164 if isinstance( host, str ):
GlennRC20fc6522015-12-23 23:26:57 -08005165 host = list( host )
5166
5167 for h in host:
5168 time.sleep( 1 )
5169 response = self.sendline( "host-remove {}".format( h ) )
Jon Halla495f562016-05-16 18:03:26 -07005170 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08005171 assert "Command not found:" not in response, response
GlennRC20fc6522015-12-23 23:26:57 -08005172 if "Error" in response:
5173 main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) )
5174 return main.FALSE
GlennRC20fc6522015-12-23 23:26:57 -08005175 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08005176 except AssertionError:
5177 main.log.exception( "" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005178 return main.FALSE
GlennRC20fc6522015-12-23 23:26:57 -08005179 except TypeError:
5180 main.log.exception( self.name + ": Object not as expected" )
5181 return main.FALSE
5182 except pexpect.EOF:
5183 main.log.error( self.name + ": EOF exception found" )
5184 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005185 main.cleanAndExit()
GlennRC20fc6522015-12-23 23:26:57 -08005186 except Exception:
5187 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005188 main.cleanAndExit()
GlennRCed771242016-01-13 17:02:47 -08005189
YPZhangfebf7302016-05-24 16:45:56 -07005190 def link( self, begin, end, state, timeout=30, showResponse=True ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005191 '''
GlennRCed771242016-01-13 17:02:47 -08005192 Description:
5193 Bring link down or up in the null-provider.
5194 params:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005195 begin - (string) One end of a device or switch.
5196 end - (string) the other end of the device or switch
GlennRCed771242016-01-13 17:02:47 -08005197 returns:
5198 main.TRUE if no exceptions were thrown and no Errors are
5199 present in the resoponse. Otherwise, returns main.FALSE
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005200 '''
GlennRCed771242016-01-13 17:02:47 -08005201 try:
Jon Halle0f0b342017-04-18 11:43:47 -07005202 cmd = "null-link null:{} null:{} {}".format( begin, end, state )
YPZhangfebf7302016-05-24 16:45:56 -07005203 response = self.sendline( cmd, showResponse=showResponse, timeout=timeout )
Jon Halla495f562016-05-16 18:03:26 -07005204 assert response is not None, "Error in sendline"
Jon Hallc6793552016-01-19 14:18:37 -08005205 assert "Command not found:" not in response, response
GlennRCed771242016-01-13 17:02:47 -08005206 if "Error" in response or "Failure" in response:
5207 main.log.error( response )
5208 return main.FALSE
GlennRCed771242016-01-13 17:02:47 -08005209 return main.TRUE
Jon Hallc6793552016-01-19 14:18:37 -08005210 except AssertionError:
5211 main.log.exception( "" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005212 return main.FALSE
GlennRCed771242016-01-13 17:02:47 -08005213 except TypeError:
5214 main.log.exception( self.name + ": Object not as expected" )
5215 return main.FALSE
5216 except pexpect.EOF:
5217 main.log.error( self.name + ": EOF exception found" )
5218 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005219 main.cleanAndExit()
GlennRCed771242016-01-13 17:02:47 -08005220 except Exception:
5221 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005222 main.cleanAndExit()
GlennRCed771242016-01-13 17:02:47 -08005223
Jon Hall2c8959e2016-12-16 12:17:34 -08005224 def portstate( self, dpid, port, state ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005225 '''
Flavio Castro82ee2f62016-06-07 15:04:12 -07005226 Description:
5227 Changes the state of port in an OF switch by means of the
5228 PORTSTATUS OF messages.
5229 params:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005230 dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102'
5231 port - (string) target port in the device. Ex: '2'
5232 state - (string) target state (enable or disable)
Flavio Castro82ee2f62016-06-07 15:04:12 -07005233 returns:
5234 main.TRUE if no exceptions were thrown and no Errors are
5235 present in the resoponse. Otherwise, returns main.FALSE
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005236 '''
Flavio Castro82ee2f62016-06-07 15:04:12 -07005237 try:
Jon Hall2c8959e2016-12-16 12:17:34 -08005238 state = state.lower()
5239 assert state == 'enable' or state == 'disable', "Unknown state"
Jon Halle0f0b342017-04-18 11:43:47 -07005240 cmd = "portstate {} {} {}".format( dpid, port, state )
Flavio Castro82ee2f62016-06-07 15:04:12 -07005241 response = self.sendline( cmd, showResponse=True )
5242 assert response is not None, "Error in sendline"
5243 assert "Command not found:" not in response, response
5244 if "Error" in response or "Failure" in response:
5245 main.log.error( response )
5246 return main.FALSE
5247 return main.TRUE
5248 except AssertionError:
5249 main.log.exception( "" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005250 return main.FALSE
Flavio Castro82ee2f62016-06-07 15:04:12 -07005251 except TypeError:
5252 main.log.exception( self.name + ": Object not as expected" )
5253 return main.FALSE
5254 except pexpect.EOF:
5255 main.log.error( self.name + ": EOF exception found" )
5256 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005257 main.cleanAndExit()
Flavio Castro82ee2f62016-06-07 15:04:12 -07005258 except Exception:
5259 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005260 main.cleanAndExit()
Flavio Castro82ee2f62016-06-07 15:04:12 -07005261
5262 def logSet( self, level="INFO", app="org.onosproject" ):
5263 """
5264 Set the logging level to lvl for a specific app
5265 returns main.TRUE on success
5266 returns main.FALSE if Error occurred
5267 if noExit is True, TestON will not exit, but clean up
5268 Available level: DEBUG, TRACE, INFO, WARN, ERROR
5269 Level defaults to INFO
5270 """
5271 try:
Jon Halle0f0b342017-04-18 11:43:47 -07005272 self.handle.sendline( "log:set %s %s" % ( level, app ) )
Jon Hall6c9e2da2018-11-06 12:01:23 -08005273 self.handle.expect( self.karafPrompt )
Flavio Castro82ee2f62016-06-07 15:04:12 -07005274
5275 response = self.handle.before
5276 if re.search( "Error", response ):
5277 return main.FALSE
5278 return main.TRUE
5279 except pexpect.TIMEOUT:
5280 main.log.exception( self.name + ": TIMEOUT exception found" )
Devin Lim44075962017-08-11 10:56:37 -07005281 main.cleanAndExit()
Flavio Castro82ee2f62016-06-07 15:04:12 -07005282 except pexpect.EOF:
5283 main.log.error( self.name + ": EOF exception found" )
5284 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005285 main.cleanAndExit()
Flavio Castro82ee2f62016-06-07 15:04:12 -07005286 except Exception:
5287 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005288 main.cleanAndExit()
You Wangdb8cd0a2016-05-26 15:19:45 -07005289
5290 def getGraphDict( self, timeout=60, includeHost=False ):
5291 """
5292 Return a dictionary which describes the latest network topology data as a
5293 graph.
5294 An example of the dictionary:
5295 { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... },
5296 vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } }
5297 Each vertex should at least have an 'edges' attribute which describes the
5298 adjacency information. The value of 'edges' attribute is also represented by
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005299 a dictionary, which maps each edge (identified by the neighbor vertex) to a
You Wangdb8cd0a2016-05-26 15:19:45 -07005300 list of attributes.
5301 An example of the edges dictionary:
5302 'edges': { vertex2: { 'port': ..., 'weight': ... },
5303 vertex3: { 'port': ..., 'weight': ... } }
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005304 If includeHost == True, all hosts (and host-switch links) will be included
You Wangdb8cd0a2016-05-26 15:19:45 -07005305 in topology data.
5306 """
5307 graphDict = {}
5308 try:
5309 links = self.links()
5310 links = json.loads( links )
5311 devices = self.devices()
5312 devices = json.loads( devices )
5313 idToDevice = {}
5314 for device in devices:
5315 idToDevice[ device[ 'id' ] ] = device
5316 if includeHost:
5317 hosts = self.hosts()
5318 # FIXME: support 'includeHost' argument
5319 for link in links:
5320 nodeA = link[ 'src' ][ 'device' ]
5321 nodeB = link[ 'dst' ][ 'device' ]
5322 assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ]
Jon Halle0f0b342017-04-18 11:43:47 -07005323 if nodeA not in graphDict.keys():
5324 graphDict[ nodeA ] = { 'edges': {},
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005325 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ],
Jon Halle0f0b342017-04-18 11:43:47 -07005326 'type': idToDevice[ nodeA ][ 'type' ],
5327 'available': idToDevice[ nodeA ][ 'available' ],
5328 'role': idToDevice[ nodeA ][ 'role' ],
5329 'mfr': idToDevice[ nodeA ][ 'mfr' ],
5330 'hw': idToDevice[ nodeA ][ 'hw' ],
5331 'sw': idToDevice[ nodeA ][ 'sw' ],
5332 'serial': idToDevice[ nodeA ][ 'serial' ],
5333 'chassisId': idToDevice[ nodeA ][ 'chassisId' ],
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005334 'annotations': idToDevice[ nodeA ][ 'annotations' ]}
You Wangdb8cd0a2016-05-26 15:19:45 -07005335 else:
5336 # Assert nodeB is not connected to any current links of nodeA
5337 assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys()
Jon Halle0f0b342017-04-18 11:43:47 -07005338 graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ],
5339 'type': link[ 'type' ],
5340 'state': link[ 'state' ] }
You Wangdb8cd0a2016-05-26 15:19:45 -07005341 return graphDict
5342 except ( TypeError, ValueError ):
5343 main.log.exception( self.name + ": Object not as expected" )
5344 return None
5345 except KeyError:
5346 main.log.exception( self.name + ": KeyError exception found" )
5347 return None
5348 except AssertionError:
5349 main.log.exception( self.name + ": AssertionError exception found" )
5350 return None
5351 except pexpect.EOF:
5352 main.log.error( self.name + ": EOF exception found" )
5353 main.log.error( self.name + ": " + self.handle.before )
5354 return None
5355 except Exception:
5356 main.log.exception( self.name + ": Uncaught exception!" )
5357 return None
YPZhangcbc2a062016-07-11 10:55:44 -07005358
5359 def getIntentPerfSummary( self ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005360 '''
YPZhangcbc2a062016-07-11 10:55:44 -07005361 Send command to check intent-perf summary
5362 Returns: dictionary for intent-perf summary
5363 if something wrong, function will return None
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005364 '''
YPZhangcbc2a062016-07-11 10:55:44 -07005365 cmd = "intent-perf -s"
5366 respDic = {}
5367 resp = self.sendline( cmd )
You Wangb5a55f72017-03-03 12:51:05 -08005368 assert resp is not None, "Error in sendline"
5369 assert "Command not found:" not in resp, resp
YPZhangcbc2a062016-07-11 10:55:44 -07005370 try:
5371 # Generate the dictionary to return
5372 for l in resp.split( "\n" ):
5373 # Delete any white space in line
5374 temp = re.sub( r'\s+', '', l )
5375 temp = temp.split( ":" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005376 respDic[ temp[ 0 ] ] = temp[ 1 ]
YPZhangcbc2a062016-07-11 10:55:44 -07005377
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005378 except ( TypeError, ValueError ):
YPZhangcbc2a062016-07-11 10:55:44 -07005379 main.log.exception( self.name + ": Object not as expected" )
5380 return None
5381 except KeyError:
5382 main.log.exception( self.name + ": KeyError exception found" )
5383 return None
5384 except AssertionError:
5385 main.log.exception( self.name + ": AssertionError exception found" )
5386 return None
5387 except pexpect.EOF:
5388 main.log.error( self.name + ": EOF exception found" )
5389 main.log.error( self.name + ": " + self.handle.before )
5390 return None
5391 except Exception:
5392 main.log.exception( self.name + ": Uncaught exception!" )
5393 return None
5394 return respDic
5395
Chiyu Chengec63bde2016-11-17 18:11:36 -08005396 def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ):
chengchiyu08303a02016-09-08 17:40:26 -07005397 """
5398 Searches the latest ONOS log file for the given search term and
5399 return a list that contains all the lines that have the search term.
YPZhangcbc2a062016-07-11 10:55:44 -07005400
chengchiyu08303a02016-09-08 17:40:26 -07005401 Arguments:
Chiyu Chengec63bde2016-11-17 18:11:36 -08005402 searchTerm:
5403 The string to grep from the ONOS log.
5404 startLine:
5405 The term that decides which line is the start to search the searchTerm in
5406 the karaf log. For now, startTerm only works in 'first' mode.
5407 logNum:
5408 In some extreme cases, one karaf log is not big enough to contain all the
5409 information.Because of this, search mutiply logs is necessary to capture
5410 the right result. logNum is the number of karaf logs that we need to search
5411 the searchTerm.
chengchiyu08303a02016-09-08 17:40:26 -07005412 mode:
5413 all: return all the strings that contain the search term
5414 last: return the last string that contains the search term
5415 first: return the first string that contains the search term
Chiyu Chengec63bde2016-11-17 18:11:36 -08005416 num: return the number of times that the searchTerm appears in the log
5417 total: return how many lines in karaf log
chengchiyu08303a02016-09-08 17:40:26 -07005418 """
5419 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005420 assert isinstance( searchTerm, str )
Jon Halle0f0b342017-04-18 11:43:47 -07005421 # Build the log paths string
Chiyu Chengec63bde2016-11-17 18:11:36 -08005422 logPath = '/opt/onos/log/karaf.log.'
5423 logPaths = '/opt/onos/log/karaf.log'
5424 for i in range( 1, logNum ):
5425 logPaths = logPath + str( i ) + " " + logPaths
5426 cmd = "cat " + logPaths
You Wang6d301d42017-04-21 10:49:33 -07005427 if startLine:
Jon Halla478b852017-12-04 15:00:15 -08005428 # 100000000 is just a extreme large number to make sure this function can
5429 # grep all the lines after startLine
You Wang6d301d42017-04-21 10:49:33 -07005430 cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'"
Chiyu Chengec63bde2016-11-17 18:11:36 -08005431 if mode == 'all':
5432 cmd = cmd + " | grep \'" + searchTerm + "\'"
You Wang6d301d42017-04-21 10:49:33 -07005433 elif mode == 'last':
Chiyu Chengec63bde2016-11-17 18:11:36 -08005434 cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1"
You Wang6d301d42017-04-21 10:49:33 -07005435 elif mode == 'first':
5436 cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1"
5437 elif mode == 'num':
You Wangd91a70f2019-01-03 15:28:10 -08005438 cmd = cmd + " | grep \'" + searchTerm + "\' | wc -l"
5439 num = self.lineCount( cmd )
Chiyu Chengb8c2c842016-10-05 12:40:49 -07005440 return num
You Wang6d301d42017-04-21 10:49:33 -07005441 elif mode == 'total':
Jon Halld5a94fb2018-11-13 14:32:23 -08005442 totalLines = self.lineCount( "cat /opt/onos/log/karaf.log | wc -l" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005443 return int( totalLines )
You Wang6d301d42017-04-21 10:49:33 -07005444 else:
5445 main.log.error( self.name + " unsupported mode" )
5446 return main.ERROR
chengchiyu08303a02016-09-08 17:40:26 -07005447 before = self.sendline( cmd )
5448 before = before.splitlines()
5449 # make sure the returned list only contains the search term
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005450 returnLines = [ line for line in before if searchTerm in line ]
chengchiyu08303a02016-09-08 17:40:26 -07005451 return returnLines
5452 except AssertionError:
5453 main.log.error( self.name + " searchTerm is not string type" )
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()
chengchiyu08303a02016-09-08 17:40:26 -07005459 except pexpect.TIMEOUT:
5460 main.log.error( self.name + ": TIMEOUT exception found" )
5461 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005462 main.cleanAndExit()
chengchiyu08303a02016-09-08 17:40:26 -07005463 except Exception:
5464 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005465 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005466
5467 def vplsShow( self, jsonFormat=True ):
5468 """
5469 Description: Returns result of onos:vpls show, which should list the
5470 configured VPLS networks and the assigned interfaces.
5471 Optional:
5472 * jsonFormat: enable json formatting of output
5473 Returns:
5474 The output of the command or None on error.
5475 """
5476 try:
5477 cmdStr = "vpls show"
5478 if jsonFormat:
5479 raise NotImplementedError
5480 cmdStr += " -j"
5481 handle = self.sendline( cmdStr )
5482 assert handle is not None, "Error in sendline"
5483 assert "Command not found:" not in handle, handle
5484 return handle
5485 except AssertionError:
5486 main.log.exception( "" )
5487 return None
5488 except TypeError:
5489 main.log.exception( self.name + ": Object not as expected" )
5490 return None
5491 except pexpect.EOF:
5492 main.log.error( self.name + ": EOF exception found" )
5493 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005494 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005495 except NotImplementedError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005496 main.log.exception( self.name + ": Json output not supported" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005497 return None
5498 except Exception:
5499 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005500 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005501
5502 def parseVplsShow( self ):
5503 """
5504 Parse the cli output of 'vpls show' into json output. This is required
5505 as there is currently no json output available.
5506 """
5507 try:
5508 output = []
5509 raw = self.vplsShow( jsonFormat=False )
5510 namePat = "VPLS name: (?P<name>\w+)"
5511 interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]"
5512 encapPat = "Encapsulation: (?P<encap>\w+)"
5513 pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] )
5514 mIter = re.finditer( pattern, raw )
5515 for match in mIter:
5516 item = {}
5517 item[ 'name' ] = match.group( 'name' )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005518 ifaces = match.group( 'interfaces' ).split( ', ' )
Jon Hall2c8959e2016-12-16 12:17:34 -08005519 if ifaces == [ "" ]:
5520 ifaces = []
5521 item[ 'interfaces' ] = ifaces
5522 encap = match.group( 'encap' )
5523 if encap != 'NONE':
5524 item[ 'encapsulation' ] = encap.lower()
5525 output.append( item )
5526 return output
5527 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 vplsList( self, jsonFormat=True ):
5532 """
5533 Description: Returns result of onos:vpls list, which should list the
5534 configured VPLS networks.
5535 Optional:
5536 * jsonFormat: enable json formatting of output
5537 """
5538 try:
5539 cmdStr = "vpls list"
5540 if jsonFormat:
5541 raise NotImplementedError
5542 cmdStr += " -j"
5543 handle = self.sendline( cmdStr )
5544 assert handle is not None, "Error in sendline"
5545 assert "Command not found:" not in handle, handle
5546 return handle
5547 except AssertionError:
5548 main.log.exception( "" )
5549 return None
5550 except TypeError:
5551 main.log.exception( self.name + ": Object not as expected" )
5552 return None
5553 except pexpect.EOF:
5554 main.log.error( self.name + ": EOF exception found" )
5555 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005556 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005557 except NotImplementedError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005558 main.log.exception( self.name + ": Json output not supported" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005559 return None
5560 except Exception:
5561 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005562 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005563
5564 def vplsCreate( self, network ):
5565 """
5566 CLI command to create a new VPLS network.
5567 Required arguments:
5568 network - String name of the network to create.
5569 returns:
5570 main.TRUE on success and main.FALSE on failure
5571 """
5572 try:
5573 network = str( network )
5574 cmdStr = "vpls create "
5575 cmdStr += network
5576 output = self.sendline( cmdStr )
5577 assert output is not None, "Error in sendline"
5578 assert "Command not found:" not in output, output
5579 assert "Error executing command" not in output, output
5580 assert "VPLS already exists:" not in output, output
5581 return main.TRUE
5582 except AssertionError:
5583 main.log.exception( "" )
5584 return main.FALSE
5585 except TypeError:
5586 main.log.exception( self.name + ": Object not as expected" )
5587 return main.FALSE
5588 except pexpect.EOF:
5589 main.log.error( self.name + ": EOF exception found" )
5590 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005591 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005592 except Exception:
5593 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005594 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005595
5596 def vplsDelete( self, network ):
5597 """
5598 CLI command to delete a VPLS network.
5599 Required arguments:
5600 network - Name of the network to delete.
5601 returns:
5602 main.TRUE on success and main.FALSE on failure
5603 """
5604 try:
5605 network = str( network )
5606 cmdStr = "vpls delete "
5607 cmdStr += network
5608 output = self.sendline( cmdStr )
5609 assert output is not None, "Error in sendline"
5610 assert "Command not found:" not in output, output
5611 assert "Error executing command" not in output, output
5612 assert " not found" not in output, output
Jon Hallcf97cf12017-06-06 09:37:51 -07005613 assert "still updating" not in output, output
Jon Hall2c8959e2016-12-16 12:17:34 -08005614 return main.TRUE
5615 except AssertionError:
5616 main.log.exception( "" )
5617 return main.FALSE
5618 except TypeError:
5619 main.log.exception( self.name + ": Object not as expected" )
5620 return main.FALSE
5621 except pexpect.EOF:
5622 main.log.error( self.name + ": EOF exception found" )
5623 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005624 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005625 except Exception:
5626 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005627 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005628
5629 def vplsAddIface( self, network, iface ):
5630 """
5631 CLI command to add an interface to a VPLS network.
5632 Required arguments:
5633 network - Name of the network to add the interface to.
5634 iface - The ONOS name for an interface.
5635 returns:
5636 main.TRUE on success and main.FALSE on failure
5637 """
5638 try:
5639 network = str( network )
5640 iface = str( iface )
5641 cmdStr = "vpls add-if "
5642 cmdStr += network + " " + iface
5643 output = self.sendline( cmdStr )
5644 assert output is not None, "Error in sendline"
5645 assert "Command not found:" not in output, output
5646 assert "Error executing command" not in output, output
5647 assert "already associated to network" not in output, output
5648 assert "Interface cannot be added." not in output, output
Jon Hallcf97cf12017-06-06 09:37:51 -07005649 assert "still updating" not in output, output
Jon Hall2c8959e2016-12-16 12:17:34 -08005650 return main.TRUE
5651 except AssertionError:
5652 main.log.exception( "" )
5653 return main.FALSE
5654 except TypeError:
5655 main.log.exception( self.name + ": Object not as expected" )
5656 return main.FALSE
5657 except pexpect.EOF:
5658 main.log.error( self.name + ": EOF exception found" )
5659 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005660 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005661 except Exception:
5662 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005663 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005664
5665 def vplsRemIface( self, network, iface ):
5666 """
5667 CLI command to remove an interface from a VPLS network.
5668 Required arguments:
5669 network - Name of the network to remove the interface from.
5670 iface - Name of the interface to remove.
5671 returns:
5672 main.TRUE on success and main.FALSE on failure
5673 """
5674 try:
5675 iface = str( iface )
5676 cmdStr = "vpls rem-if "
5677 cmdStr += network + " " + iface
5678 output = self.sendline( cmdStr )
5679 assert output is not None, "Error in sendline"
5680 assert "Command not found:" not in output, output
5681 assert "Error executing command" not in output, output
5682 assert "is not configured" not in output, output
Jon Hallcf97cf12017-06-06 09:37:51 -07005683 assert "still updating" not in output, output
Jon Hall2c8959e2016-12-16 12:17:34 -08005684 return main.TRUE
5685 except AssertionError:
5686 main.log.exception( "" )
5687 return main.FALSE
5688 except TypeError:
5689 main.log.exception( self.name + ": Object not as expected" )
5690 return main.FALSE
5691 except pexpect.EOF:
5692 main.log.error( self.name + ": EOF exception found" )
5693 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005694 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005695 except Exception:
5696 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005697 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005698
5699 def vplsClean( self ):
5700 """
5701 Description: Clears the VPLS app configuration.
5702 Returns: main.TRUE on success and main.FALSE on failure
5703 """
5704 try:
5705 cmdStr = "vpls clean"
5706 handle = self.sendline( cmdStr )
5707 assert handle is not None, "Error in sendline"
5708 assert "Command not found:" not in handle, handle
Jon Hallcf97cf12017-06-06 09:37:51 -07005709 assert "still updating" not in handle, handle
Jon Hall2c8959e2016-12-16 12:17:34 -08005710 return handle
5711 except AssertionError:
5712 main.log.exception( "" )
5713 return main.FALSE
5714 except TypeError:
5715 main.log.exception( self.name + ": Object not as expected" )
5716 return main.FALSE
5717 except pexpect.EOF:
5718 main.log.error( self.name + ": EOF exception found" )
5719 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005720 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005721 except Exception:
5722 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005723 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005724
5725 def vplsSetEncap( self, network, encapType ):
5726 """
5727 CLI command to add an interface to a VPLS network.
5728 Required arguments:
5729 network - Name of the network to create.
5730 encapType - Type of encapsulation.
5731 returns:
5732 main.TRUE on success and main.FALSE on failure
5733 """
5734 try:
5735 network = str( network )
5736 encapType = str( encapType ).upper()
5737 assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type"
5738 cmdStr = "vpls set-encap "
5739 cmdStr += network + " " + encapType
5740 output = self.sendline( cmdStr )
5741 assert output is not None, "Error in sendline"
5742 assert "Command not found:" not in output, output
5743 assert "Error executing command" not in output, output
5744 assert "already associated to network" not in output, output
5745 assert "Encapsulation type " not in output, output
Jon Hallcf97cf12017-06-06 09:37:51 -07005746 assert "still updating" not in output, output
Jon Hall2c8959e2016-12-16 12:17:34 -08005747 return main.TRUE
5748 except AssertionError:
5749 main.log.exception( "" )
5750 return main.FALSE
5751 except TypeError:
5752 main.log.exception( self.name + ": Object not as expected" )
5753 return main.FALSE
5754 except pexpect.EOF:
5755 main.log.error( self.name + ": EOF exception found" )
5756 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005757 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005758 except Exception:
5759 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005760 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005761
5762 def interfaces( self, jsonFormat=True ):
5763 """
5764 Description: Returns result of interfaces command.
5765 Optional:
5766 * jsonFormat: enable json formatting of output
5767 Returns:
5768 The output of the command or None on error.
5769 """
5770 try:
5771 cmdStr = "interfaces"
5772 if jsonFormat:
Jon Halle0f0b342017-04-18 11:43:47 -07005773 raise NotImplementedError
Jon Hall2c8959e2016-12-16 12:17:34 -08005774 cmdStr += " -j"
5775 handle = self.sendline( cmdStr )
5776 assert handle is not None, "Error in sendline"
5777 assert "Command not found:" not in handle, handle
5778 return handle
5779 except AssertionError:
5780 main.log.exception( "" )
5781 return None
5782 except TypeError:
5783 main.log.exception( self.name + ": Object not as expected" )
5784 return None
5785 except pexpect.EOF:
5786 main.log.error( self.name + ": EOF exception found" )
5787 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07005788 main.cleanAndExit()
Jon Hall2c8959e2016-12-16 12:17:34 -08005789 except NotImplementedError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005790 main.log.exception( self.name + ": Json output not supported" )
Jon Hall2c8959e2016-12-16 12:17:34 -08005791 return None
5792 except Exception:
5793 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005794 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08005795
5796 def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005797 '''
Chiyu Chengec63bde2016-11-17 18:11:36 -08005798 Get the timestamp of searchTerm from karaf log.
5799
5800 Arguments:
5801 splitTerm_before and splitTerm_after:
5802
5803 The terms that split the string that contains the timeStamp of
5804 searchTerm. For example, if that string is "xxxxxxxcreationTime =
5805 1419510501xxxxxx", then the splitTerm_before is "CreationTime = "
5806 and the splitTerm_after is "x"
5807
5808 others:
Jon Halle0f0b342017-04-18 11:43:47 -07005809 Please look at the "logsearch" Function in onosclidriver.py
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00005810 '''
Chiyu Chengec63bde2016-11-17 18:11:36 -08005811 if logNum < 0:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005812 main.log.error( "Get wrong log number ")
Chiyu Chengec63bde2016-11-17 18:11:36 -08005813 return main.ERROR
5814 lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005815 if len( lines ) == 0:
Chiyu Chengec63bde2016-11-17 18:11:36 -08005816 main.log.warn( "Captured timestamp string is empty" )
5817 return main.ERROR
5818 lines = lines[ 0 ]
5819 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005820 assert isinstance( lines, str )
Chiyu Chengec63bde2016-11-17 18:11:36 -08005821 # get the target value
5822 line = lines.split( splitTerm_before )
5823 key = line[ 1 ].split( splitTerm_after )
5824 return int( key[ 0 ] )
5825 except IndexError:
5826 main.log.warn( "Index Error!" )
5827 return main.ERROR
5828 except AssertionError:
5829 main.log.warn( "Search Term Not Found " )
5830 return main.ERROR
Jon Halle0f0b342017-04-18 11:43:47 -07005831
5832 def workQueueAdd( self, queueName, value ):
5833 """
5834 CLI command to add a string to the specified Work Queue.
5835 This function uses the distributed primitives test app, which
5836 gives some cli access to distributed primitives for testing
5837 purposes only.
5838
5839 Required arguments:
5840 queueName - The name of the queue to add to
5841 value - The value to add to the queue
5842 returns:
5843 main.TRUE on success, main.FALSE on failure and
5844 main.ERROR on error.
5845 """
5846 try:
5847 queueName = str( queueName )
5848 value = str( value )
5849 prefix = "work-queue-test"
5850 operation = "add"
5851 cmdStr = " ".join( [ prefix, queueName, operation, value ] )
5852 output = self.distPrimitivesSend( cmdStr )
5853 if "Invalid operation name" in output:
5854 main.log.warn( output )
5855 return main.ERROR
5856 elif "Done" in output:
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 workQueueAddMultiple( self, queueName, value1, value2 ):
5866 """
5867 CLI command to add two strings to 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 value1 - The first value to add to the queue
5875 value2 - The second value to add to the queue
5876 returns:
5877 main.TRUE on success, main.FALSE on failure and
5878 main.ERROR on error.
5879 """
5880 try:
5881 queueName = str( queueName )
5882 value1 = str( value1 )
5883 value2 = str( value2 )
5884 prefix = "work-queue-test"
5885 operation = "addMultiple"
5886 cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] )
5887 output = self.distPrimitivesSend( cmdStr )
5888 if "Invalid operation name" in output:
5889 main.log.warn( output )
5890 return main.ERROR
5891 elif "Done" in output:
5892 return main.TRUE
5893 except TypeError:
5894 main.log.exception( self.name + ": Object not as expected" )
5895 return main.ERROR
5896 except Exception:
5897 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005898 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07005899
5900 def workQueueTakeAndComplete( self, queueName, number=1 ):
5901 """
5902 CLI command to take a value from the specified Work Queue and compelte it.
5903 This function uses the distributed primitives test app, which
5904 gives some cli access to distributed primitives for testing
5905 purposes only.
5906
5907 Required arguments:
5908 queueName - The name of the queue to add to
5909 number - The number of items to take and complete
5910 returns:
5911 main.TRUE on success, main.FALSE on failure and
5912 main.ERROR on error.
5913 """
5914 try:
5915 queueName = str( queueName )
5916 number = str( int( number ) )
5917 prefix = "work-queue-test"
5918 operation = "takeAndComplete"
5919 cmdStr = " ".join( [ prefix, queueName, operation, number ] )
5920 output = self.distPrimitivesSend( cmdStr )
5921 if "Invalid operation name" in output:
5922 main.log.warn( output )
5923 return main.ERROR
5924 elif "Done" in output:
5925 return main.TRUE
5926 except TypeError:
5927 main.log.exception( self.name + ": Object not as expected" )
5928 return main.ERROR
5929 except Exception:
5930 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005931 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07005932
5933 def workQueueDestroy( self, queueName ):
5934 """
5935 CLI command to destroy the specified Work Queue.
5936 This function uses the distributed primitives test app, which
5937 gives some cli access to distributed primitives for testing
5938 purposes only.
5939
5940 Required arguments:
5941 queueName - The name of the queue to add to
5942 returns:
5943 main.TRUE on success, main.FALSE on failure and
5944 main.ERROR on error.
5945 """
5946 try:
5947 queueName = str( queueName )
5948 prefix = "work-queue-test"
5949 operation = "destroy"
5950 cmdStr = " ".join( [ prefix, queueName, operation ] )
5951 output = self.distPrimitivesSend( cmdStr )
5952 if "Invalid operation name" in output:
5953 main.log.warn( output )
5954 return main.ERROR
5955 return main.TRUE
5956 except TypeError:
5957 main.log.exception( self.name + ": Object not as expected" )
5958 return main.ERROR
5959 except Exception:
5960 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005961 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07005962
5963 def workQueueTotalPending( self, queueName ):
5964 """
5965 CLI command to get the Total Pending items of the specified Work Queue.
5966 This function uses the distributed primitives test app, which
5967 gives some cli access to distributed primitives for testing
5968 purposes only.
5969
5970 Required arguments:
5971 queueName - The name of the queue to add to
5972 returns:
5973 The number of Pending items in the specified work queue or
5974 None on error
5975 """
5976 try:
5977 queueName = str( queueName )
5978 prefix = "work-queue-test"
5979 operation = "totalPending"
5980 cmdStr = " ".join( [ prefix, queueName, operation ] )
5981 output = self.distPrimitivesSend( cmdStr )
Jon Hall22e94ce2019-01-15 14:52:17 -08005982 main.log.debug( self.name + ": work queue unparsed: " + output )
5983 output = output.split( "\r\n" )[ -1 ]
5984 main.log.debug( self.name + ": work queue parsed: " + output )
Jon Halle0f0b342017-04-18 11:43:47 -07005985 pattern = r'\d+'
5986 if "Invalid operation name" in output:
5987 main.log.warn( output )
5988 return None
5989 else:
5990 match = re.search( pattern, output )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07005991 return match.group( 0 )
Jon Halle0f0b342017-04-18 11:43:47 -07005992 except ( AttributeError, TypeError ):
5993 main.log.exception( self.name + ": Object not as expected; " + str( output ) )
5994 return None
5995 except Exception:
5996 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07005997 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07005998
5999 def workQueueTotalCompleted( self, queueName ):
6000 """
6001 CLI command to get the Total Completed items of the specified Work Queue.
6002 This function uses the distributed primitives test app, which
6003 gives some cli access to distributed primitives for testing
6004 purposes only.
6005
6006 Required arguments:
6007 queueName - The name of the queue to add to
6008 returns:
6009 The number of complete items in the specified work queue or
6010 None on error
6011 """
6012 try:
6013 queueName = str( queueName )
6014 prefix = "work-queue-test"
6015 operation = "totalCompleted"
6016 cmdStr = " ".join( [ prefix, queueName, operation ] )
6017 output = self.distPrimitivesSend( cmdStr )
Jon Hall22e94ce2019-01-15 14:52:17 -08006018 main.log.debug( self.name + ": work queue unparsed: " + output )
6019 output = output.split( "\r\n" )[ -1 ]
6020 main.log.debug( self.name + ": work queue parsed: " + output )
Jon Halle0f0b342017-04-18 11:43:47 -07006021 pattern = r'\d+'
6022 if "Invalid operation name" in output:
6023 main.log.warn( output )
6024 return None
6025 else:
6026 match = re.search( pattern, output )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07006027 return match.group( 0 )
Jon Halle0f0b342017-04-18 11:43:47 -07006028 except ( AttributeError, TypeError ):
6029 main.log.exception( self.name + ": Object not as expected; " + str( output ) )
6030 return None
6031 except Exception:
6032 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07006033 main.cleanAndExit()
Jon Halle0f0b342017-04-18 11:43:47 -07006034
6035 def workQueueTotalInProgress( self, queueName ):
6036 """
6037 CLI command to get the Total In Progress items of the specified Work Queue.
6038 This function uses the distributed primitives test app, which
6039 gives some cli access to distributed primitives for testing
6040 purposes only.
6041
6042 Required arguments:
6043 queueName - The name of the queue to add to
6044 returns:
6045 The number of In Progress items in the specified work queue or
6046 None on error
6047 """
6048 try:
6049 queueName = str( queueName )
6050 prefix = "work-queue-test"
6051 operation = "totalInProgress"
6052 cmdStr = " ".join( [ prefix, queueName, operation ] )
6053 output = self.distPrimitivesSend( cmdStr )
Jon Hall22e94ce2019-01-15 14:52:17 -08006054 main.log.debug( self.name + ": work queue unparsed: " + output )
6055 output = output.split( "\r\n" )[ -1 ]
6056 main.log.debug( self.name + ": work queue parsed: " + output )
Jon Halle0f0b342017-04-18 11:43:47 -07006057 pattern = r'\d+'
6058 if "Invalid operation name" in output:
6059 main.log.warn( output )
6060 return None
6061 else:
6062 match = re.search( pattern, output )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07006063 return match.group( 0 )
Jon Halle0f0b342017-04-18 11:43:47 -07006064 except ( AttributeError, TypeError ):
6065 main.log.exception( self.name + ": Object not as expected; " + str( output ) )
6066 return None
6067 except Exception:
6068 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07006069 main.cleanAndExit()
Jeremy Ronquillo818bc7c2017-08-09 17:14:53 +00006070
6071 def events( self, args='-a' ):
6072 """
6073 Description: Returns events -a command output
6074 Optional:
6075 add other arguments
6076 """
6077 try:
6078 cmdStr = "events"
6079 if args:
6080 cmdStr += " " + args
6081 handle = self.sendline( cmdStr )
6082 assert handle is not None, "Error in sendline"
6083 assert "Command not found:" not in handle, handle
6084 return handle
6085 except AssertionError:
6086 main.log.exception( "" )
6087 return None
6088 except TypeError:
6089 main.log.exception( self.name + ": Object not as expected" )
6090 return None
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 getMaster( self, deviceID ):
6100 """
6101 Description: Obtains current master using "roles" command for a specific deviceID
6102 """
6103 try:
6104 return str( self.getRole( deviceID )[ 'master' ] )
6105 except AssertionError:
6106 main.log.exception( "" )
6107 return None
6108 except TypeError:
6109 main.log.exception( self.name + ": Object not as expected" )
6110 return None
6111 except pexpect.EOF:
6112 main.log.error( self.name + ": EOF exception found" )
6113 main.log.error( self.name + ": " + self.handle.before )
6114 main.cleanAndExit()
6115 except Exception:
6116 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lime6fe3c42017-10-18 16:28:40 -07006117 main.cleanAndExit()
Jon Halla478b852017-12-04 15:00:15 -08006118
6119 def issu( self ):
6120 """
6121 Short summary of In-Service Software Upgrade status
6122
6123 Returns the output of the cli command or None on Error
6124 """
6125 try:
6126 cmdStr = "issu"
6127 handle = self.sendline( cmdStr )
6128 assert handle is not None, "Error in sendline"
6129 assert "Command not found:" not in handle, handle
6130 assert "Unsupported command:" not in handle, handle
6131 return handle
6132 except AssertionError:
6133 main.log.exception( "" )
6134 return None
6135 except TypeError:
6136 main.log.exception( self.name + ": Object not as expected" )
6137 return None
6138 except pexpect.EOF:
6139 main.log.error( self.name + ": EOF exception found" )
6140 main.log.error( self.name + ": " + self.handle.before )
6141 main.cleanAndExit()
6142 except Exception:
6143 main.log.exception( self.name + ": Uncaught exception!" )
6144 main.cleanAndExit()
6145
6146 def issuInit( self ):
6147 """
6148 Initiates an In-Service Software Upgrade
6149
6150 Returns main.TRUE on success, main.ERROR on error, else main.FALSE
6151 """
6152 try:
6153 cmdStr = "issu init"
6154 handle = self.sendline( cmdStr )
6155 assert handle is not None, "Error in sendline"
6156 assert "Command not found:" not in handle, handle
6157 assert "Unsupported command:" not in handle, handle
6158 if "Initialized" in handle:
6159 return main.TRUE
6160 else:
6161 return main.FALSE
6162 except AssertionError:
6163 main.log.exception( "" )
6164 return main.ERROR
6165 except TypeError:
6166 main.log.exception( self.name + ": Object not as expected" )
6167 return main.ERROR
6168 except pexpect.EOF:
6169 main.log.error( self.name + ": EOF exception found" )
6170 main.log.error( self.name + ": " + self.handle.before )
6171 main.cleanAndExit()
6172 except Exception:
6173 main.log.exception( self.name + ": Uncaught exception!" )
6174 main.cleanAndExit()
6175
6176 def issuUpgrade( self ):
6177 """
6178 Transitions stores to upgraded nodes
6179
6180 Returns main.TRUE on success, main.ERROR on error, else main.FALSE
6181 """
6182 try:
6183 cmdStr = "issu upgrade"
6184 handle = self.sendline( cmdStr )
6185 assert handle is not None, "Error in sendline"
6186 assert "Command not found:" not in handle, handle
6187 assert "Unsupported command:" not in handle, handle
6188 if "Upgraded" in handle:
6189 return main.TRUE
6190 else:
6191 return main.FALSE
6192 except AssertionError:
6193 main.log.exception( "" )
6194 return main.ERROR
6195 except TypeError:
6196 main.log.exception( self.name + ": Object not as expected" )
6197 return main.ERROR
6198 except pexpect.EOF:
6199 main.log.error( self.name + ": EOF exception found" )
6200 main.log.error( self.name + ": " + self.handle.before )
6201 main.cleanAndExit()
6202 except Exception:
6203 main.log.exception( self.name + ": Uncaught exception!" )
6204 main.cleanAndExit()
6205
6206 def issuCommit( self ):
6207 """
6208 Finalizes an In-Service Software Upgrade
6209
6210 Returns main.TRUE on success, main.ERROR on error, else main.FALSE
6211 """
6212 try:
6213 cmdStr = "issu commit"
6214 handle = self.sendline( cmdStr )
6215 assert handle is not None, "Error in sendline"
6216 assert "Command not found:" not in handle, handle
6217 assert "Unsupported command:" not in handle, handle
6218 # TODO: Check the version returned by this command
6219 if "Committed version" in handle:
6220 return main.TRUE
6221 else:
6222 return main.FALSE
6223 except AssertionError:
6224 main.log.exception( "" )
6225 return main.ERROR
6226 except TypeError:
6227 main.log.exception( self.name + ": Object not as expected" )
6228 return main.ERROR
6229 except pexpect.EOF:
6230 main.log.error( self.name + ": EOF exception found" )
6231 main.log.error( self.name + ": " + self.handle.before )
6232 main.cleanAndExit()
6233 except Exception:
6234 main.log.exception( self.name + ": Uncaught exception!" )
6235 main.cleanAndExit()
6236
6237 def issuRollback( self ):
6238 """
6239 Rolls back an In-Service Software Upgrade
6240
6241 Returns main.TRUE on success, main.ERROR on error, else main.FALSE
6242 """
6243 try:
6244 cmdStr = "issu rollback"
6245 handle = self.sendline( cmdStr )
6246 assert handle is not None, "Error in sendline"
6247 assert "Command not found:" not in handle, handle
6248 assert "Unsupported command:" not in handle, handle
6249 # TODO: Check the version returned by this command
6250 if "Rolled back to version" in handle:
6251 return main.TRUE
6252 else:
6253 return main.FALSE
6254 except AssertionError:
6255 main.log.exception( "" )
6256 return main.ERROR
6257 except TypeError:
6258 main.log.exception( self.name + ": Object not as expected" )
6259 return main.ERROR
6260 except pexpect.EOF:
6261 main.log.error( self.name + ": EOF exception found" )
6262 main.log.error( self.name + ": " + self.handle.before )
6263 main.cleanAndExit()
6264 except Exception:
6265 main.log.exception( self.name + ": Uncaught exception!" )
6266 main.cleanAndExit()
6267
6268 def issuReset( self ):
6269 """
6270 Resets the In-Service Software Upgrade status after a rollback
6271
6272 Returns main.TRUE on success, main.ERROR on error, else main.FALSE
6273 """
6274 try:
6275 cmdStr = "issu reset"
6276 handle = self.sendline( cmdStr )
6277 assert handle is not None, "Error in sendline"
6278 assert "Command not found:" not in handle, handle
6279 assert "Unsupported command:" not in handle, handle
6280 # TODO: Check the version returned by this command
6281 if "Reset version" in handle:
6282 return main.TRUE
6283 else:
6284 return main.FALSE
6285 except AssertionError:
6286 main.log.exception( "" )
6287 return main.ERROR
6288 except TypeError:
6289 main.log.exception( self.name + ": Object not as expected" )
6290 return main.ERROR
6291 except pexpect.EOF:
6292 main.log.error( self.name + ": EOF exception found" )
6293 main.log.error( self.name + ": " + self.handle.before )
6294 main.cleanAndExit()
6295 except Exception:
6296 main.log.exception( self.name + ": Uncaught exception!" )
6297 main.cleanAndExit()
6298
6299 def issuStatus( self ):
6300 """
6301 Status of an In-Service Software Upgrade
6302
6303 Returns the output of the cli command or None on Error
6304 """
6305 try:
6306 cmdStr = "issu status"
6307 handle = self.sendline( cmdStr )
6308 assert handle is not None, "Error in sendline"
6309 assert "Command not found:" not in handle, handle
6310 assert "Unsupported command:" not in handle, handle
6311 return handle
6312 except AssertionError:
6313 main.log.exception( "" )
6314 return None
6315 except TypeError:
6316 main.log.exception( self.name + ": Object not as expected" )
6317 return None
6318 except pexpect.EOF:
6319 main.log.error( self.name + ": EOF exception found" )
6320 main.log.error( self.name + ": " + self.handle.before )
6321 main.cleanAndExit()
6322 except Exception:
6323 main.log.exception( self.name + ": Uncaught exception!" )
6324 main.cleanAndExit()
6325
6326 def issuVersion( self ):
6327 """
6328 Get the version of an In-Service Software Upgrade
6329
6330 Returns the output of the cli command or None on Error
6331 """
6332 try:
6333 cmdStr = "issu version"
6334 handle = self.sendline( cmdStr )
6335 assert handle is not None, "Error in sendline"
6336 assert "Command not found:" not in handle, handle
6337 assert "Unsupported command:" not in handle, handle
6338 return handle
6339 except AssertionError:
6340 main.log.exception( "" )
6341 return None
6342 except TypeError:
6343 main.log.exception( self.name + ": Object not as expected" )
6344 return None
6345 except pexpect.EOF:
6346 main.log.error( self.name + ": EOF exception found" )
6347 main.log.error( self.name + ": " + self.handle.before )
6348 main.cleanAndExit()
6349 except Exception:
6350 main.log.exception( self.name + ": Uncaught exception!" )
6351 main.cleanAndExit()
You Wange24d6272018-03-27 21:18:50 -07006352
6353 def mcastJoin( self, sIP, groupIP, sPort, dPorts ):
6354 """
6355 Create a multicast route by calling 'mcast-join' command
6356 sIP: source IP of the multicast route
6357 groupIP: group IP of the multicast route
6358 sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route
6359 dPorts: a list of destination ports of the multicast route
6360 Returns main.TRUE if mcast route is added; Otherwise main.FALSE
6361 """
6362 try:
6363 cmdStr = "mcast-join"
6364 cmdStr += " " + str( sIP )
6365 cmdStr += " " + str( groupIP )
6366 cmdStr += " " + str( sPort )
6367 assert isinstance( dPorts, list )
6368 for dPort in dPorts:
6369 cmdStr += " " + str( dPort )
6370 handle = self.sendline( cmdStr )
6371 assert handle is not None, "Error in sendline"
6372 assert "Command not found:" not in handle, handle
6373 assert "Unsupported command:" not in handle, handle
6374 assert "Error executing command" not in handle, handle
6375 if "Added the mcast route" in handle:
6376 return main.TRUE
6377 else:
6378 return main.FALSE
6379 except AssertionError:
6380 main.log.exception( "" )
6381 return None
6382 except TypeError:
6383 main.log.exception( self.name + ": Object not as expected" )
6384 return None
6385 except pexpect.EOF:
6386 main.log.error( self.name + ": EOF exception found" )
6387 main.log.error( self.name + ": " + self.handle.before )
6388 main.cleanAndExit()
6389 except Exception:
6390 main.log.exception( self.name + ": Uncaught exception!" )
6391 main.cleanAndExit()
6392
6393 def mcastDelete( self, sIP, groupIP, dPorts ):
6394 """
6395 Delete a multicast route by calling 'mcast-delete' command
6396 sIP: source IP of the multicast route
6397 groupIP: group IP of the multicast route
6398 dPorts: a list of destination ports of the multicast route
6399 Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE
6400 """
6401 try:
6402 cmdStr = "mcast-delete"
6403 cmdStr += " " + str( sIP )
6404 cmdStr += " " + str( groupIP )
6405 assert isinstance( dPorts, list )
6406 for dPort in dPorts:
6407 cmdStr += " " + str( dPort )
6408 handle = self.sendline( cmdStr )
6409 assert handle is not None, "Error in sendline"
6410 assert "Command not found:" not in handle, handle
6411 assert "Unsupported command:" not in handle, handle
6412 assert "Error executing command" not in handle, handle
6413 if "Updated the mcast route" in handle:
6414 return main.TRUE
6415 else:
6416 return main.FALSE
6417 except AssertionError:
6418 main.log.exception( "" )
6419 return None
6420 except TypeError:
6421 main.log.exception( self.name + ": Object not as expected" )
6422 return None
6423 except pexpect.EOF:
6424 main.log.error( self.name + ": EOF exception found" )
6425 main.log.error( self.name + ": " + self.handle.before )
6426 main.cleanAndExit()
6427 except Exception:
6428 main.log.exception( self.name + ": Uncaught exception!" )
6429 main.cleanAndExit()
6430
6431 def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ):
6432 """
6433 Create a multicast route by calling 'mcast-host-join' command
6434 sAddr: we can provide * for ASM or a specific address for SSM
6435 gAddr: specifies multicast group address
You Wang547893e2018-05-08 13:34:59 -07006436 srcs: a list of HostId of the sources e.g. ["00:AA:00:00:00:01/None"]
You Wange24d6272018-03-27 21:18:50 -07006437 sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"]
6438 Returns main.TRUE if mcast route is added; Otherwise main.FALSE
6439 """
6440 try:
6441 cmdStr = "mcast-host-join"
6442 cmdStr += " -sAddr " + str( sAddr )
6443 cmdStr += " -gAddr " + str( gAddr )
6444 assert isinstance( srcs, list )
6445 for src in srcs:
6446 cmdStr += " -srcs " + str( src )
6447 assert isinstance( sinks, list )
6448 for sink in sinks:
6449 cmdStr += " -sinks " + str( sink )
6450 handle = self.sendline( cmdStr )
6451 assert handle is not None, "Error in sendline"
6452 assert "Command not found:" not in handle, handle
6453 assert "Unsupported command:" not in handle, handle
6454 assert "Error executing command" not in handle, handle
6455 if "Added the mcast route" in handle:
6456 return main.TRUE
6457 else:
6458 return main.FALSE
6459 except AssertionError:
6460 main.log.exception( "" )
6461 return None
6462 except TypeError:
6463 main.log.exception( self.name + ": Object not as expected" )
6464 return None
6465 except pexpect.EOF:
6466 main.log.error( self.name + ": EOF exception found" )
6467 main.log.error( self.name + ": " + self.handle.before )
6468 main.cleanAndExit()
6469 except Exception:
6470 main.log.exception( self.name + ": Uncaught exception!" )
6471 main.cleanAndExit()
6472
6473 def mcastHostDelete( self, sAddr, gAddr, host=None ):
6474 """
6475 Delete multicast sink(s) by calling 'mcast-host-delete' command
6476 sAddr: we can provide * for ASM or a specific address for SSM
6477 gAddr: specifies multicast group address
You Wangc02d8352018-04-17 16:42:10 -07006478 host: HostId of the sink e.g. "00:AA:00:00:01:05/40",
You Wange24d6272018-03-27 21:18:50 -07006479 will delete the route if not specified
6480 Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE
6481 """
6482 try:
6483 cmdStr = "mcast-host-delete"
6484 cmdStr += " -sAddr " + str( sAddr )
6485 cmdStr += " -gAddr " + str( gAddr )
6486 if host:
6487 cmdStr += " -h " + str( host )
6488 handle = self.sendline( cmdStr )
6489 assert handle is not None, "Error in sendline"
6490 assert "Command not found:" not in handle, handle
6491 assert "Unsupported command:" not in handle, handle
6492 assert "Error executing command" not in handle, handle
6493 if "Updated the mcast route" in handle:
6494 return main.TRUE
6495 elif "Deleted the mcast route" in handle:
6496 return main.TRUE
6497 else:
6498 return main.FALSE
6499 except AssertionError:
6500 main.log.exception( "" )
6501 return None
6502 except TypeError:
6503 main.log.exception( self.name + ": Object not as expected" )
6504 return None
6505 except pexpect.EOF:
6506 main.log.error( self.name + ": EOF exception found" )
6507 main.log.error( self.name + ": " + self.handle.before )
6508 main.cleanAndExit()
6509 except Exception:
6510 main.log.exception( self.name + ": Uncaught exception!" )
6511 main.cleanAndExit()
6512
You Wang547893e2018-05-08 13:34:59 -07006513 def mcastSinkDelete( self, sAddr, gAddr, sink=None ):
6514 """
6515 Delete multicast sink(s) by calling 'mcast-sink-delete' command
6516 sAddr: we can provide * for ASM or a specific address for SSM
6517 gAddr: specifies multicast group address
6518 host: HostId of the sink e.g. "00:AA:00:00:01:05/40",
6519 will delete the route if not specified
6520 Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE
6521 """
6522 try:
6523 cmdStr = "mcast-sink-delete"
6524 cmdStr += " -sAddr " + str( sAddr )
6525 cmdStr += " -gAddr " + str( gAddr )
6526 if sink:
6527 cmdStr += " -s " + str( sink )
6528 handle = self.sendline( cmdStr )
6529 assert handle is not None, "Error in sendline"
6530 assert "Command not found:" not in handle, handle
6531 assert "Unsupported command:" not in handle, handle
6532 assert "Error executing command" not in handle, handle
6533 if "Updated the mcast route" in handle:
6534 return main.TRUE
6535 elif "Deleted the mcast route" in handle:
6536 return main.TRUE
6537 else:
6538 return main.FALSE
6539 except AssertionError:
6540 main.log.exception( "" )
6541 return None
6542 except TypeError:
6543 main.log.exception( self.name + ": Object not as expected" )
6544 return None
6545 except pexpect.EOF:
6546 main.log.error( self.name + ": EOF exception found" )
6547 main.log.error( self.name + ": " + self.handle.before )
6548 main.cleanAndExit()
6549 except Exception:
6550 main.log.exception( self.name + ": Uncaught exception!" )
6551 main.cleanAndExit()
6552
You Wange24d6272018-03-27 21:18:50 -07006553 def mcastSourceDelete( self, sAddr, gAddr, srcs=None ):
6554 """
6555 Delete multicast src(s) by calling 'mcast-source-delete' command
6556 sAddr: we can provide * for ASM or a specific address for SSM
6557 gAddr: specifies multicast group address
You Wang547893e2018-05-08 13:34:59 -07006558 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 -07006559 will delete the route if not specified
6560 Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE
6561 """
6562 try:
6563 cmdStr = "mcast-source-delete"
6564 cmdStr += " -sAddr " + str( sAddr )
6565 cmdStr += " -gAddr " + str( gAddr )
6566 if srcs:
6567 assert isinstance( srcs, list )
6568 for src in srcs:
6569 cmdStr += " -src " + str( src )
6570 handle = self.sendline( cmdStr )
6571 assert handle is not None, "Error in sendline"
6572 assert "Command not found:" not in handle, handle
6573 assert "Unsupported command:" not in handle, handle
6574 assert "Error executing command" not in handle, handle
6575 if "Updated the mcast route" in handle:
6576 return main.TRUE
6577 elif "Deleted the mcast route" in handle:
6578 return main.TRUE
6579 else:
6580 return main.FALSE
6581 except AssertionError:
6582 main.log.exception( "" )
6583 return None
6584 except TypeError:
6585 main.log.exception( self.name + ": Object not as expected" )
6586 return None
6587 except pexpect.EOF:
6588 main.log.error( self.name + ": EOF exception found" )
6589 main.log.error( self.name + ": " + self.handle.before )
6590 main.cleanAndExit()
6591 except Exception:
6592 main.log.exception( self.name + ": Uncaught exception!" )
6593 main.cleanAndExit()
You Wang5da39c82018-04-26 22:55:08 -07006594
6595 def netcfg( self, jsonFormat=True, args="" ):
6596 """
6597 Run netcfg cli command with given args
6598 """
6599 try:
6600 cmdStr = "netcfg"
6601 if jsonFormat:
6602 cmdStr = cmdStr + " -j"
6603 if args:
6604 cmdStr = cmdStr + " " + str( args )
6605 handle = self.sendline( cmdStr )
6606 assert handle is not None, "Error in sendline"
6607 assert "Command not found:" not in handle, handle
6608 assert "Unsupported command:" not in handle, handle
6609 assert "Error executing command" not in handle, handle
6610 return handle
6611 except AssertionError:
6612 main.log.exception( "" )
6613 return None
6614 except TypeError:
6615 main.log.exception( self.name + ": Object not as expected" )
6616 return None
6617 except pexpect.EOF:
6618 main.log.error( self.name + ": EOF exception found" )
6619 main.log.error( self.name + ": " + self.handle.before )
6620 main.cleanAndExit()
6621 except Exception:
6622 main.log.exception( self.name + ": Uncaught exception!" )
6623 main.cleanAndExit()
6624
You Wang0fa76e72018-05-18 11:33:25 -07006625 def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ):
You Wang5da39c82018-04-26 22:55:08 -07006626 """
You Wang54b1d672018-06-11 16:44:13 -07006627 Compose and return a list of t3-troubleshoot cli commands for given source and destination addresses
You Wang5da39c82018-04-26 22:55:08 -07006628 Options:
6629 sAddr: IP address of the source host
6630 dAddr: IP address of the destination host
You Wang0fa76e72018-05-18 11:33:25 -07006631 ipv6: True if hosts are IPv6
6632 verbose: return verbose t3 output if True
6633 simple: compose command for t3-troubleshoot-simple if True
You Wang5da39c82018-04-26 22:55:08 -07006634 """
6635 try:
6636 # Collect information of both hosts from onos
6637 hosts = self.hosts()
6638 hosts = json.loads( hosts )
6639 sHost = None
6640 dHost = None
6641 for host in hosts:
6642 if sAddr in host[ "ipAddresses" ]:
6643 sHost = host
6644 elif dAddr in host[ "ipAddresses" ]:
6645 dHost = host
6646 if sHost and dHost:
6647 break
6648 assert sHost, "Not able to find host with IP {}".format( sAddr )
You Wang54b1d672018-06-11 16:44:13 -07006649 cmdList = []
You Wang5d9527b2018-05-29 17:08:54 -07006650 if simple:
6651 assert dHost, "Not able to find host with IP {}".format( dAddr )
You Wang54b1d672018-06-11 16:44:13 -07006652 cmdStr = "t3-troubleshoot-simple"
6653 if verbose:
6654 cmdStr += " -vv"
6655 if ipv6:
6656 cmdStr += " -et ipv6"
You Wang0fa76e72018-05-18 11:33:25 -07006657 cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] )
You Wang54b1d672018-06-11 16:44:13 -07006658 cmdList.append( cmdStr )
You Wang0fa76e72018-05-18 11:33:25 -07006659 else:
You Wang54b1d672018-06-11 16:44:13 -07006660 for location in sHost[ "locations" ]:
6661 cmdStr = "t3-troubleshoot"
6662 if verbose:
6663 cmdStr += " -vv"
6664 if ipv6:
6665 cmdStr += " -et ipv6"
6666 cmdStr += " -s " + str( sAddr )
6667 cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] )
6668 cmdStr += " -sm " + str( sHost[ "mac" ] )
6669 if sHost[ "vlan" ] != "None":
6670 cmdStr += " -vid " + sHost[ "vlan" ]
6671 cmdStr += " -d " + str( dAddr )
6672 netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) )
6673 netcfg = json.loads( netcfg )
6674 assert netcfg, "Failed to get netcfg"
6675 cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] )
6676 cmdList.append( cmdStr )
6677 return cmdList
You Wang5da39c82018-04-26 22:55:08 -07006678 except AssertionError:
6679 main.log.exception( "" )
6680 return None
6681 except ( KeyError, TypeError ):
6682 main.log.exception( self.name + ": Object not as expected" )
6683 return None
6684 except Exception:
6685 main.log.exception( self.name + ": Uncaught exception!" )
6686 main.cleanAndExit()
6687
6688 def t3( self, sAddr, dAddr, ipv6=False ):
6689 """
You Wang54b1d672018-06-11 16:44:13 -07006690 Run t3-troubleshoot cli commands for all posible routes given source and destination addresses
You Wang5da39c82018-04-26 22:55:08 -07006691 Options:
6692 sAddr: IP address of the source host
6693 dAddr: IP address of the destination host
6694 """
6695 try:
You Wang54b1d672018-06-11 16:44:13 -07006696 cmdList = self.composeT3Command( sAddr, dAddr, ipv6 )
6697 assert cmdList is not None, "composeT3Command returned None"
6698 t3Output = ""
6699 for cmdStr in cmdList:
6700 handle = self.sendline( cmdStr )
6701 assert handle is not None, "Error in sendline"
6702 assert "Command not found:" not in handle, handle
6703 assert "Unsupported command:" not in handle, handle
6704 assert "Error executing command" not in handle, handle
6705 assert "Tracing packet" in handle
6706 t3Output += handle
6707 return t3Output
You Wang5da39c82018-04-26 22:55:08 -07006708 except AssertionError:
6709 main.log.exception( "" )
6710 return None
6711 except pexpect.EOF:
6712 main.log.error( self.name + ": EOF exception found" )
6713 main.log.error( self.name + ": " + self.handle.before )
6714 main.cleanAndExit()
6715 except Exception:
6716 main.log.exception( self.name + ": Uncaught exception!" )
6717 main.cleanAndExit()