Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Created on 07-08-2015 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4 | Copyright 2015 Open Networking Foundation (ONF) |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 5 | |
| 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 9 | |
| 10 | TestON is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 2 of the License, or |
| 13 | ( at your option ) any later version. |
| 14 | |
| 15 | TestON is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | """ |
| 24 | import json |
| 25 | import os |
| 26 | import requests |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 27 | import types |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 28 | import sys |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 29 | import time |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 30 | |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 31 | from drivers.common.api.controllerdriver import Controller |
| 32 | |
| 33 | |
| 34 | class OnosRestDriver( Controller ): |
| 35 | |
| 36 | def __init__( self ): |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 37 | self.pwd = None |
| 38 | self.user_name = "user" |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 39 | super( OnosRestDriver, self ).__init__() |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 40 | self.ip_address = "localhost" |
| 41 | self.port = "8080" |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 42 | self.wrapped = sys.modules[ __name__ ] |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 43 | |
| 44 | def connect( self, **connectargs ): |
| 45 | try: |
| 46 | for key in connectargs: |
| 47 | vars( self )[ key ] = connectargs[ key ] |
| 48 | self.name = self.options[ 'name' ] |
| 49 | except Exception as e: |
| 50 | main.log.exception( e ) |
| 51 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 52 | if os.getenv( str( self.ip_address ) ) is not None: |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 53 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 54 | else: |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 55 | main.log.info( self.name + ": ip set to " + self.ip_address ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 56 | except KeyError: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 57 | main.log.info( self.name + ": Invalid host name," + |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 58 | "defaulting to 'localhost' instead" ) |
| 59 | self.ip_address = 'localhost' |
| 60 | except Exception as inst: |
| 61 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 62 | |
Jon Hall | 6040bcf | 2017-08-14 11:15:41 -0700 | [diff] [blame] | 63 | return super( OnosRestDriver, self ).connect() |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 64 | |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 65 | def pprint( self, jsonObject ): |
| 66 | """ |
| 67 | Pretty Prints a json object |
| 68 | |
| 69 | arguments: |
| 70 | jsonObject - a parsed json object |
| 71 | returns: |
| 72 | A formatted string for printing or None on error |
| 73 | """ |
| 74 | try: |
| 75 | if isinstance( jsonObject, str ): |
| 76 | jsonObject = json.loads( jsonObject ) |
| 77 | return json.dumps( jsonObject, sort_keys=True, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 78 | indent=4, separators=( ',', ': ' ) ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 79 | except ( TypeError, ValueError ): |
| 80 | main.log.exception( "Error parsing jsonObject" ) |
| 81 | return None |
| 82 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 83 | def send( self, url, ip = "DEFAULT", port = "DEFAULT", base="/onos/v1", method="GET", |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 84 | query=None, data=None, debug=False ): |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 85 | """ |
| 86 | Arguments: |
| 87 | str ip: ONOS IP Address |
| 88 | str port: ONOS REST Port |
| 89 | str url: ONOS REST url path. |
| 90 | NOTE that this is is only the relative path. IE "/devices" |
| 91 | str base: The base url for the given REST api. Applications could |
| 92 | potentially have their own base url |
| 93 | str method: HTTP method type |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 94 | dict query: Dictionary to be sent in the query string for |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 95 | the request |
| 96 | dict data: Dictionary to be sent in the body of the request |
| 97 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 98 | # TODO: Authentication - simple http (user,pass) tuple |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 99 | # TODO: should we maybe just pass kwargs straight to response? |
| 100 | # TODO: Do we need to allow for other protocols besides http? |
| 101 | # ANSWER: Not yet, but potentially https with certificates |
suibin zhang | d5b6fe4 | 2016-05-12 08:48:58 -0700 | [diff] [blame] | 102 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 103 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 104 | ip = self.ip_address |
suibin zhang | d5b6fe4 | 2016-05-12 08:48:58 -0700 | [diff] [blame] | 105 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 106 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 107 | "from topo file" ) |
| 108 | port = self.port |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 109 | |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 110 | try: |
| 111 | path = "http://" + str( ip ) + ":" + str( port ) + base + url |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 112 | if self.user_name and self.pwd: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 113 | main.log.info( self.name + ": user/passwd is: " + self.user_name + "/" + self.pwd ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 114 | auth = ( self.user_name, self.pwd ) |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 115 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 116 | auth = None |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 117 | main.log.info( self.name + ": Sending request " + path + " using " + |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 118 | method.upper() + " method." ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 119 | if debug: |
| 120 | main.log.debug( self.name + ": request data: " + str( data ) ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 121 | response = requests.request( method.upper(), |
| 122 | path, |
| 123 | params=query, |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 124 | data=data, |
| 125 | auth=auth ) |
| 126 | if debug: |
| 127 | main.log.debug( response ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 128 | return ( response.status_code, response.text.encode( 'utf8' ) ) |
You Wang | 7880b37 | 2019-02-27 16:50:47 -0800 | [diff] [blame] | 129 | except requests.ConnectionError: |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 130 | main.log.exception( "Error sending request." ) |
You Wang | 7880b37 | 2019-02-27 16:50:47 -0800 | [diff] [blame] | 131 | return ( None, None ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 132 | except Exception: |
| 133 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 134 | main.cleanAndExit() |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 135 | |
| 136 | def intents( self, ip="DEFAULT", port="DEFAULT" ): |
| 137 | """ |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 138 | Description: |
| 139 | Gets a list of dictionary of all intents in the system |
| 140 | Returns: |
| 141 | A list of dictionary of intents in string type to match the cli |
| 142 | version for now; Returns main.FALSE if error on request; |
| 143 | Returns None for exception |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 144 | """ |
| 145 | try: |
| 146 | output = None |
| 147 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 148 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 149 | ip = self.ip_address |
| 150 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 151 | main.log.warn( self.name + ": No port given, reverting to port " + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 152 | "from topo file" ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 153 | port = self.port |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 154 | response = self.send( url="/intents", ip = ip, port = port ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 155 | if response: |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 156 | if 200 <= response[ 0 ] <= 299: |
| 157 | output = response[ 1 ] |
| 158 | a = json.loads( output ).get( 'intents' ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 159 | assert a is not None, "Error parsing json object" |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 160 | b = json.dumps( a ) |
| 161 | return b |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 162 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 163 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 164 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 165 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 166 | except ( AttributeError, AssertionError, TypeError ): |
| 167 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 168 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 169 | except Exception: |
| 170 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 171 | main.cleanAndExit() |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 172 | |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 173 | def intent( self, intentId, appId="org.onosproject.cli", |
| 174 | ip="DEFAULT", port="DEFAULT" ): |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 175 | """ |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 176 | Description: |
| 177 | Get the specific intent information of the given application ID and |
| 178 | intent ID |
| 179 | Required: |
| 180 | str intentId - Intent id in hexadecimal form |
| 181 | Optional: |
| 182 | str appId - application id of intent |
| 183 | Returns: |
| 184 | Returns an information dictionary of the given intent; |
| 185 | Returns main.FALSE if error on requests; Returns None for exception |
| 186 | NOTE: |
| 187 | The GET /intents REST api command accepts application id but the |
| 188 | api will get updated to accept application name instead |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 189 | """ |
| 190 | try: |
| 191 | output = None |
| 192 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 193 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 194 | ip = self.ip_address |
| 195 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 196 | main.log.warn( self.name + ": No port given, reverting to port " + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 197 | "from topo file" ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 198 | port = self.port |
| 199 | # NOTE: REST url requires the intent id to be in decimal form |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 200 | query = "/" + str( appId ) + "/" + str( intentId ) |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 201 | response = self.send( url="/intents" + query, ip = ip, port = port ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 202 | if response: |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 203 | if 200 <= response[ 0 ] <= 299: |
| 204 | output = response[ 1 ] |
| 205 | a = json.loads( output ) |
| 206 | return a |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 207 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 208 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 209 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 210 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 211 | except ( AttributeError, TypeError ): |
| 212 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | fc91588 | 2015-07-14 13:33:17 -0700 | [diff] [blame] | 213 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 214 | except Exception: |
| 215 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 216 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 217 | |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 218 | def apps( self, ip="DEFAULT", port="DEFAULT" ): |
| 219 | """ |
| 220 | Description: |
| 221 | Returns all the current application installed in the system |
| 222 | Returns: |
| 223 | List of dictionary of installed application; Returns main.FALSE for |
| 224 | error on request; Returns None for exception |
| 225 | """ |
| 226 | try: |
| 227 | output = None |
| 228 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 229 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 230 | ip = self.ip_address |
| 231 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 232 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 233 | "from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 234 | port = self.port |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 235 | response = self.send( url="/applications", ip = ip, port = port ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 236 | if response: |
| 237 | if 200 <= response[ 0 ] <= 299: |
| 238 | output = response[ 1 ] |
| 239 | a = json.loads( output ).get( 'applications' ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 240 | assert a is not None, "Error parsing json object" |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 241 | b = json.dumps( a ) |
| 242 | return b |
| 243 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 244 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 245 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 246 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 247 | except ( AttributeError, AssertionError, TypeError ): |
| 248 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 249 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 250 | except Exception: |
| 251 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 252 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 253 | |
| 254 | def activateApp( self, appName, ip="DEFAULT", port="DEFAULT", check=True ): |
| 255 | """ |
| 256 | Decription: |
| 257 | Activate an app that is already installed in ONOS |
| 258 | Optional: |
| 259 | bool check - If check is True, method will check the status |
| 260 | of the app after the command is issued |
| 261 | Returns: |
| 262 | Returns main.TRUE if the command was successfully or main.FALSE |
| 263 | if the REST responded with an error or given incorrect input; |
| 264 | Returns None for exception |
| 265 | |
| 266 | """ |
| 267 | try: |
| 268 | output = None |
| 269 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 270 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 271 | ip = self.ip_address |
| 272 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 273 | main.log.warn( self.name + ": No port given, reverting to port " + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 274 | "from topo file" ) |
| 275 | port = self.port |
| 276 | query = "/" + str( appName ) + "/active" |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 277 | retry = 0 |
| 278 | retCode = main.TRUE |
| 279 | while retry < 50: |
| 280 | if retry > 0: |
| 281 | main.log.warn( self.name + ": Retrying " + query + " for the " + str( retry ) + " time" ) |
| 282 | |
| 283 | retry += 1 |
| 284 | response = self.send( method="POST", |
| 285 | debug=True, |
| 286 | url="/applications" + query, |
| 287 | ip = ip, port = port ) |
| 288 | if response: |
| 289 | output = response[ 1 ] |
| 290 | if 200 <= response[ 0 ] <= 299: |
| 291 | if check: |
| 292 | app = json.loads( output ) |
| 293 | if app.get( 'state' ) == 'ACTIVE': |
| 294 | main.log.info( self.name + ": " + appName + |
| 295 | " application" + |
| 296 | " is in ACTIVE state" ) |
| 297 | appHealth = self.getAppHealth( appName=appName, ip=ip, port=port ) |
| 298 | if "ready" == json.loads( appHealth[1] ).get( 'message' ): |
| 299 | return main.TRUE |
| 300 | else: |
| 301 | return main.FALSE |
| 302 | else: |
| 303 | main.log.error( self.name + ": " + appName + |
| 304 | " application" + " is in " + |
| 305 | app.get( 'state' ) + " state" ) |
| 306 | retCode = main.FALSE |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 307 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 308 | main.log.warn( self.name + ": Skipping " + appName + |
| 309 | "application check" ) |
| 310 | return main.TRUE |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 311 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 312 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 313 | ( response[ 0 ], response[ 1 ] ) ) |
| 314 | retCode = main.FALSE |
| 315 | time.sleep( 30 ) |
| 316 | return retCode |
| 317 | except ( ValueError ): |
| 318 | main.log.exception( self.name + ": Error parsing json" ) |
| 319 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 320 | except ( AttributeError, TypeError ): |
| 321 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 322 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 323 | except Exception: |
| 324 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 325 | main.log.debug( self.name + ": " + response ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 326 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 327 | |
| 328 | def deactivateApp( self, appName, ip="DEFAULT", port="DEFAULT", |
| 329 | check=True ): |
| 330 | """ |
| 331 | Required: |
| 332 | Deactivate an app that is already activated in ONOS |
| 333 | Optional: |
| 334 | bool check - If check is True, method will check the status of the |
| 335 | app after the command is issued |
| 336 | Returns: |
| 337 | Returns main.TRUE if the command was successfully sent |
| 338 | main.FALSE if the REST responded with an error or given |
| 339 | incorrect input; Returns None for exception |
| 340 | """ |
| 341 | try: |
| 342 | output = None |
| 343 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 344 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 345 | ip = self.ip_address |
| 346 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 347 | main.log.warn( self.name + ": No port given, reverting to port " + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 348 | "from topo file" ) |
| 349 | port = self.port |
| 350 | query = "/" + str( appName ) + "/active" |
Devin Lim | cde503f | 2017-09-11 17:23:30 -0700 | [diff] [blame] | 351 | self.send( method="DELETE", |
| 352 | url="/applications" + query, |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 353 | ip = ip, port = port ) |
Devin Lim | cde503f | 2017-09-11 17:23:30 -0700 | [diff] [blame] | 354 | response = self.getApp( appName, ip, port ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 355 | if response: |
| 356 | output = response[ 1 ] |
Jon Hall | 6040bcf | 2017-08-14 11:15:41 -0700 | [diff] [blame] | 357 | app = {} if output == "" else json.loads( output ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 358 | if 200 <= response[ 0 ] <= 299: |
| 359 | if check: |
| 360 | if app.get( 'state' ) == 'INSTALLED': |
| 361 | main.log.info( self.name + ": " + appName + |
| 362 | " application" + |
| 363 | " is in INSTALLED state" ) |
| 364 | return main.TRUE |
| 365 | else: |
| 366 | main.log.error( self.name + ": " + appName + |
| 367 | " application" + " is in " + |
| 368 | app.get( 'state' ) + " state" ) |
| 369 | return main.FALSE |
| 370 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 371 | main.log.warn( self.name + ": Skipping " + appName + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 372 | "application check" ) |
| 373 | return main.TRUE |
| 374 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 375 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 376 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 377 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 378 | except ( AttributeError, TypeError ): |
| 379 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 380 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 381 | except Exception: |
| 382 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 383 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 384 | |
Devin Lim | cde503f | 2017-09-11 17:23:30 -0700 | [diff] [blame] | 385 | def getApp( self, appName, ip="DEFAULT", |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 386 | port="DEFAULT" ): |
| 387 | """ |
| 388 | Decription: |
| 389 | Gets the informaion of the given application |
| 390 | Required: |
| 391 | str name - Name of onos application |
| 392 | Returns: |
| 393 | Returns a dictionary of information ONOS application in string type; |
| 394 | Returns main.FALSE if error on requests; Returns None for exception |
| 395 | """ |
| 396 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 397 | response = None |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 398 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 399 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 400 | ip = self.ip_address |
| 401 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 402 | main.log.warn( self.name + ": No port given, reverting to port " + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 403 | "from topo file" ) |
| 404 | port = self.port |
Devin Lim | cde503f | 2017-09-11 17:23:30 -0700 | [diff] [blame] | 405 | query = "/" + str( appName ) |
suibin zhang | d5b6fe4 | 2016-05-12 08:48:58 -0700 | [diff] [blame] | 406 | response = self.send( url="/applications" + query, |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 407 | ip = ip, port = port ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 408 | if response: |
| 409 | if 200 <= response[ 0 ] <= 299: |
Devin Lim | cde503f | 2017-09-11 17:23:30 -0700 | [diff] [blame] | 410 | return response |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 411 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 412 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 413 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 414 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 415 | except ( AttributeError, TypeError ): |
| 416 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 417 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 418 | except Exception: |
| 419 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 420 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 421 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 422 | def getAppHealth( self, appName, ip="DEFAULT", |
| 423 | port="DEFAULT" ): |
| 424 | """ |
| 425 | Decription: |
| 426 | Gets the health of the given application |
| 427 | Required: |
| 428 | str name - Name of onos application |
| 429 | Returns: |
| 430 | Returns a dictionary of information ONOS application in string type; |
| 431 | Returns main.FALSE if error on requests; Returns None for exception |
| 432 | """ |
| 433 | try: |
| 434 | response = None |
| 435 | if ip == "DEFAULT": |
| 436 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
| 437 | ip = self.ip_address |
| 438 | if port == "DEFAULT": |
| 439 | main.log.warn( self.name + ": No port given, reverting to port " + |
| 440 | "from topo file" ) |
| 441 | port = self.port |
| 442 | response = self.send( url="/applications/%s/health" % str( appName ), |
| 443 | ip = ip, port = port ) |
| 444 | if response: |
| 445 | if 200 <= response[ 0 ] <= 299: |
| 446 | return response |
| 447 | else: |
| 448 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 449 | ( response[ 0 ], response[ 1 ] ) ) |
| 450 | return main.FALSE |
| 451 | except ( AttributeError, TypeError ): |
| 452 | main.log.exception( self.name + ": Object not as expected" ) |
| 453 | return None |
| 454 | except Exception: |
| 455 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 456 | main.cleanAndExit() |
| 457 | |
| 458 | def getAllAppHealth( self, retries=1, wait=30 , ip="DEFAULT", |
| 459 | port="DEFAULT" ): |
| 460 | """ |
| 461 | Description: |
| 462 | Gets the health of all activated apps |
| 463 | Required: |
| 464 | Optional: |
| 465 | retries - The number of tries to return before returning |
| 466 | wait - Time to wait in between retries |
| 467 | """ |
| 468 | try: |
| 469 | responses = main.TRUE |
| 470 | if ip == "DEFAULT": |
| 471 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
| 472 | ip = self.ip_address |
| 473 | if port == "DEFAULT": |
| 474 | main.log.warn( self.name + ": No port given, reverting to port " + |
| 475 | "from topo file" ) |
| 476 | port = self.port |
| 477 | apps = self.apps() |
| 478 | for app in json.loads(apps): |
| 479 | appName = app.get( "name" ) |
| 480 | response = self.getAppHealth( appName=appName, ip=ip, port=port ) |
| 481 | responses = main.TRUE and "ready" == json.loads( response[1] ).get( "message" ) |
| 482 | return responses |
| 483 | except ( AttributeError, TypeError ): |
| 484 | main.log.exception( self.name + ": Object not as expected" ) |
| 485 | return None |
| 486 | except Exception: |
| 487 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 488 | main.cleanAndExit() |
| 489 | |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 490 | def addHostIntent( self, hostIdOne, hostIdTwo, appId='org.onosproject.cli', |
Jeremy Songster | ae2dd45 | 2016-05-17 16:44:35 -0700 | [diff] [blame] | 491 | ip="DEFAULT", port="DEFAULT", vlanId="" ): |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 492 | """ |
| 493 | Description: |
| 494 | Adds a host-to-host intent ( bidirectional ) by |
| 495 | specifying the two hosts. |
| 496 | Required: |
| 497 | * hostIdOne: ONOS host id for host1 |
| 498 | * hostIdTwo: ONOS host id for host2 |
| 499 | Optional: |
| 500 | str appId - Application name of intent identifier |
| 501 | Returns: |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 502 | Returns main.TRUE for successful requests; Returns main.FALSE if |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 503 | error on requests; Returns None for exceptions |
| 504 | """ |
| 505 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 506 | intentJson = { "two": str( hostIdTwo ), |
| 507 | "selector": { "criteria": [] }, "priority": 7, |
| 508 | "treatment": { "deferred": [], "instructions": [] }, |
| 509 | "appId": appId, "one": str( hostIdOne ), |
| 510 | "type": "HostToHostIntent", |
| 511 | "constraints": [ { "type": "LinkTypeConstraint", |
| 512 | "types": [ "OPTICAL" ], |
| 513 | "inclusive": 'false' } ] } |
Jeremy Songster | ae2dd45 | 2016-05-17 16:44:35 -0700 | [diff] [blame] | 514 | if vlanId: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 515 | intentJson[ 'selector' ][ 'criteria' ].append( { "type": "VLAN_VID", |
| 516 | "vlanId": vlanId } ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 517 | response = None |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 518 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 519 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 520 | ip = self.ip_address |
| 521 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 522 | main.log.warn( self.name + ": No port given, reverting to port " + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 523 | "from topo file" ) |
| 524 | port = self.port |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 525 | response = self.send( method="POST", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 526 | url="/intents", ip = ip, port = port, |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 527 | data=json.dumps( intentJson ) ) |
| 528 | if response: |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 529 | if "201" in str( response[ 0 ] ): |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 530 | main.log.info( self.name + ": Successfully POST host" + |
| 531 | " intent between host: " + hostIdOne + |
| 532 | " and host: " + hostIdTwo ) |
| 533 | return main.TRUE |
| 534 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 535 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 536 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 537 | return main.FALSE |
| 538 | |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 539 | except ( AttributeError, TypeError ): |
| 540 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 541 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 542 | except Exception: |
| 543 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 544 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 545 | |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 546 | def addPointIntent( self, |
| 547 | ingressDevice, |
| 548 | egressDevice, |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 549 | appId='org.onosproject.cli', |
| 550 | ingressPort="", |
| 551 | egressPort="", |
| 552 | ethType="", |
| 553 | ethSrc="", |
| 554 | ethDst="", |
| 555 | bandwidth="", |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 556 | protected=False, |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 557 | lambdaAlloc=False, |
| 558 | ipProto="", |
| 559 | ipSrc="", |
| 560 | ipDst="", |
| 561 | tcpSrc="", |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 562 | tcpDst="", |
| 563 | ip="DEFAULT", |
Jeremy Songster | ae2dd45 | 2016-05-17 16:44:35 -0700 | [diff] [blame] | 564 | port="DEFAULT", |
| 565 | vlanId="" ): |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 566 | """ |
| 567 | Description: |
| 568 | Adds a point-to-point intent ( uni-directional ) by |
| 569 | specifying device id's and optional fields |
| 570 | Required: |
| 571 | * ingressDevice: device id of ingress device |
| 572 | * egressDevice: device id of egress device |
| 573 | Optional: |
| 574 | * ethType: specify ethType |
| 575 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 576 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 577 | * bandwidth: specify bandwidth capacity of link (TODO) |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 578 | * lambdaAlloc: if True, intent will allocate lambda |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 579 | for the specified intent (TODO) |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 580 | * ipProto: specify ip protocol |
| 581 | * ipSrc: specify ip source address with mask eg. ip#/24 |
| 582 | * ipDst: specify ip destination address eg. ip#/24 |
| 583 | * tcpSrc: specify tcp source port |
| 584 | * tcpDst: specify tcp destination port |
| 585 | Returns: |
| 586 | Returns main.TRUE for successful requests; Returns main.FALSE if |
| 587 | no ingress|egress port found and if error on requests; |
| 588 | Returns None for exceptions |
| 589 | NOTE: |
| 590 | The ip and port option are for the requests input's ip and port |
| 591 | of the ONOS node |
| 592 | """ |
| 593 | try: |
| 594 | if "/" in ingressDevice: |
| 595 | if not ingressPort: |
| 596 | ingressPort = ingressDevice.split( "/" )[ 1 ] |
| 597 | ingressDevice = ingressDevice.split( "/" )[ 0 ] |
| 598 | else: |
| 599 | if not ingressPort: |
| 600 | main.log.debug( self.name + ": Ingress port not specified" ) |
| 601 | return main.FALSE |
| 602 | |
| 603 | if "/" in egressDevice: |
| 604 | if not egressPort: |
| 605 | egressPort = egressDevice.split( "/" )[ 1 ] |
| 606 | egressDevice = egressDevice.split( "/" )[ 0 ] |
| 607 | else: |
| 608 | if not egressPort: |
| 609 | main.log.debug( self.name + ": Egress port not specified" ) |
| 610 | return main.FALSE |
| 611 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 612 | intentJson = { "ingressPoint": { "device": ingressDevice, |
| 613 | "port": ingressPort }, |
| 614 | "selector": { "criteria": [] }, |
| 615 | "priority": 55, |
| 616 | "treatment": { "deferred": [], |
| 617 | "instructions": [] }, |
| 618 | "egressPoint": { "device": egressDevice, |
| 619 | "port": egressPort }, |
| 620 | "appId": appId, |
| 621 | "type": "PointToPointIntent", |
| 622 | "constraints": [ { "type": "LinkTypeConstraint", |
| 623 | "types": [ "OPTICAL" ], |
| 624 | "inclusive": "false" } ] } |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 625 | |
| 626 | if ethType == "IPV4": |
| 627 | intentJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 628 | "type": "ETH_TYPE", |
| 629 | "ethType": 2048 } ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 630 | elif ethType: |
| 631 | intentJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 632 | "type": "ETH_TYPE", |
| 633 | "ethType": ethType } ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 634 | |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 635 | if ethSrc: |
| 636 | intentJson[ 'selector' ][ 'criteria' ].append( |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 637 | { "type": "ETH_SRC", |
| 638 | "mac": ethSrc } ) |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 639 | if ethDst: |
| 640 | intentJson[ 'selector' ][ 'criteria' ].append( |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 641 | { "type": "ETH_DST", |
| 642 | "mac": ethDst } ) |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 643 | if ipSrc: |
| 644 | intentJson[ 'selector' ][ 'criteria' ].append( |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 645 | { "type": "IPV4_SRC", |
| 646 | "ip": ipSrc } ) |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 647 | if ipDst: |
| 648 | intentJson[ 'selector' ][ 'criteria' ].append( |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 649 | { "type": "IPV4_DST", |
| 650 | "ip": ipDst } ) |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 651 | if tcpSrc: |
| 652 | intentJson[ 'selector' ][ 'criteria' ].append( |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 653 | { "type": "TCP_SRC", |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 654 | "tcpPort": tcpSrc } ) |
| 655 | if tcpDst: |
| 656 | intentJson[ 'selector' ][ 'criteria' ].append( |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 657 | { "type": "TCP_DST", |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 658 | "tcpPort": tcpDst } ) |
| 659 | if ipProto: |
| 660 | intentJson[ 'selector' ][ 'criteria' ].append( |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 661 | { "type": "IP_PROTO", |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 662 | "protocol": ipProto } ) |
Jeremy Songster | ae2dd45 | 2016-05-17 16:44:35 -0700 | [diff] [blame] | 663 | if vlanId: |
| 664 | intentJson[ 'selector' ][ 'criteria' ].append( |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 665 | { "type": "VLAN_VID", |
Jeremy Songster | ae2dd45 | 2016-05-17 16:44:35 -0700 | [diff] [blame] | 666 | "vlanId": vlanId } ) |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 667 | |
| 668 | # TODO: Bandwidth and Lambda will be implemented if needed |
| 669 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 670 | response = None |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 671 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 672 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 673 | ip = self.ip_address |
| 674 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 675 | main.log.warn( self.name + ": No port given, reverting to port " + |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 676 | "from topo file" ) |
| 677 | port = self.port |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 678 | response = self.send( method="POST", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 679 | url="/intents", ip = ip, port = port, |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 680 | data=json.dumps( intentJson ) ) |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 681 | |
| 682 | main.log.debug( intentJson ) |
| 683 | |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 684 | if response: |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 685 | if "201" in str( response[ 0 ] ): |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 686 | main.log.info( self.name + ": Successfully POST point" + |
| 687 | " intent between ingress: " + ingressDevice + |
| 688 | " and egress: " + egressDevice + " devices" ) |
| 689 | return main.TRUE |
| 690 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 691 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 692 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 693 | return main.FALSE |
| 694 | |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 695 | except ( AttributeError, TypeError ): |
| 696 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | b50074f | 2015-07-27 16:18:32 -0700 | [diff] [blame] | 697 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 698 | except Exception: |
| 699 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 700 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 701 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 702 | def addSinglepointToMultipointIntent( self, |
| 703 | ingressDevice, |
| 704 | egressDeviceList, |
| 705 | portEgressList, |
| 706 | appId='org.onosproject.cli', |
| 707 | portIngress="", |
| 708 | ethType="", |
| 709 | ethSrc="", |
| 710 | ethDst="", |
| 711 | bandwidth="", |
| 712 | lambdaAlloc=False, |
| 713 | ipProto="", |
| 714 | ipSrc="", |
| 715 | ipDst="", |
| 716 | tcpSrc="", |
| 717 | tcpDst="", |
| 718 | partial=False, |
| 719 | ip="DEFAULT", |
| 720 | port="DEFAULT", |
| 721 | vlanId="" ): |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 722 | """ |
| 723 | Description: |
| 724 | Adds a point-to-multi point intent ( uni-directional ) by |
| 725 | specifying device id's and optional fields |
| 726 | Required: |
| 727 | * ingressDevice: device id of ingress device |
| 728 | * egressDevice: device id of egress device |
| 729 | * portEgressList: a list of port id of egress device |
| 730 | |
| 731 | Optional: |
| 732 | * portIngress: port id of ingress device |
| 733 | * ethType: specify ethType |
| 734 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 735 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 736 | * bandwidth: specify bandwidth capacity of link (TODO) |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 737 | * lambdaAlloc: if True, intent will allocate lambda |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 738 | for the specified intent (TODO) |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 739 | * ipProto: specify ip protocol |
| 740 | * ipSrc: specify ip source address with mask eg. ip#/24 |
| 741 | * ipDst: specify ip destination address eg. ip#/24 |
| 742 | * tcpSrc: specify tcp source port |
| 743 | * tcpDst: specify tcp destination port |
| 744 | Returns: |
| 745 | Returns main.TRUE for successful requests; Returns main.FALSE if |
| 746 | no ingress|egress port found and if error on requests; |
| 747 | Returns None for exceptions |
| 748 | NOTE: |
| 749 | The ip and port option are for the requests input's ip and port |
| 750 | of the ONOS node |
| 751 | """ |
| 752 | try: |
| 753 | |
| 754 | if "/" in ingressDevice: |
| 755 | if not portIngress: |
| 756 | ingressPort = ingressDevice.split( "/" )[ 1 ] |
| 757 | ingressDevice = ingressDevice.split( "/" )[ 0 ] |
| 758 | else: |
| 759 | if not portIngress: |
| 760 | main.log.debug( self.name + ": Ingress port not specified" ) |
| 761 | return main.FALSE |
| 762 | index = 0 |
| 763 | for egressDevice in egressDeviceList: |
| 764 | if "/" in egressDevice: |
| 765 | portEgressList.append( egressDevice.split( "/" )[ 1 ] ) |
| 766 | egressDeviceList[ index ] = egressDevice.split( "/" )[ 0 ] |
| 767 | else: |
| 768 | if not portEgressList: |
| 769 | main.log.debug( self.name + ": Egress port not specified" ) |
| 770 | return main.FALSE |
| 771 | index = index + 1 |
| 772 | |
| 773 | intentJson = { "ingressPoint": { "device": ingressDevice, |
| 774 | "port": ingressPort }, |
| 775 | "selector": { "criteria": [] }, |
| 776 | "priority": 55, |
| 777 | "treatment": { "deferred": [], |
| 778 | "instructions": [] }, |
| 779 | "egressPoint": { "connectPoints": [] }, |
| 780 | "appId": appId, |
| 781 | "type": "SinglePointToMultiPointIntent", |
| 782 | "constraints": [ { "type": "LinkTypeConstraint", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 783 | "types": [ "OPTICAL" ], |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 784 | "inclusive": "false" } ] } |
| 785 | |
| 786 | index = 0 |
| 787 | for ep in portEgressList: |
| 788 | intentJson[ 'egressPoint' ][ 'connectPoints' ].append( |
| 789 | { "device": egressDeviceList[ index ], |
| 790 | "port": ep } ) |
| 791 | index += 1 |
| 792 | |
| 793 | if ethType == "IPV4": |
| 794 | intentJson[ 'selector' ][ 'criteria' ].append( |
| 795 | { "type": "ETH_TYPE", |
| 796 | "ethType": 2048 } ) |
| 797 | elif ethType: |
| 798 | intentJson[ 'selector' ][ 'criteria' ].append( |
| 799 | { "type": "ETH_TYPE", |
| 800 | "ethType": ethType } ) |
| 801 | |
| 802 | if ethSrc: |
| 803 | intentJson[ 'selector' ][ 'criteria' ].append( |
| 804 | { "type": "ETH_SRC", |
| 805 | "mac": ethSrc } ) |
| 806 | |
| 807 | if ethDst: |
| 808 | for dst in ethDst: |
| 809 | if dst: |
| 810 | intentJson[ 'selector' ][ 'criteria' ].append( |
| 811 | { "type": "ETH_DST", |
| 812 | "mac": dst } ) |
| 813 | if tcpSrc: |
| 814 | intentJson[ 'selector' ][ 'criteria' ].append( |
| 815 | { "type": "TCP_SRC", |
| 816 | "tcpPort": tcpSrc } ) |
| 817 | if tcpDst: |
| 818 | intentJson[ 'selector' ][ 'criteria' ].append( |
| 819 | { "type": "TCP_DST", |
| 820 | "tcpPort": tcpDst } ) |
| 821 | if ipProto: |
| 822 | intentJson[ 'selector' ][ 'criteria' ].append( |
| 823 | { "type": "IP_PROTO", |
| 824 | "protocol": ipProto } ) |
| 825 | if vlanId: |
| 826 | intentJson[ 'selector' ][ 'criteria' ].append( |
| 827 | { "type": "VLAN_VID", |
| 828 | "vlanId": vlanId } ) |
| 829 | |
| 830 | # TODO: Bandwidth and Lambda will be implemented if needed |
| 831 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 832 | response = None |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 833 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 834 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 835 | ip = self.ip_address |
| 836 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 837 | main.log.warn( self.name + ": No port given, reverting to port " + |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 838 | "from topo file" ) |
| 839 | port = self.port |
| 840 | response = self.send( method="POST", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 841 | url="/intents", ip=ip, port=port, |
| 842 | data=json.dumps( intentJson ) ) |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 843 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 844 | main.log.debug( intentJson ) |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 845 | |
| 846 | if response: |
| 847 | if "201" in str( response[ 0 ] ): |
| 848 | main.log.info( self.name + ": Successfully POST point" + |
| 849 | " intent between ingress: " + ingressDevice + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 850 | " and egress: " + str( egressDeviceList ) + " devices" ) |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 851 | return main.TRUE |
| 852 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 853 | main.log.error( "Error with REST request, response was: %s: %s" % ( response[ 0 ], response[ 1 ] ) ) |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 854 | return main.FALSE |
| 855 | else: |
| 856 | main.log.error( "REST request has no response." ) |
| 857 | return main.FALSE |
| 858 | |
| 859 | except ( AttributeError, TypeError ): |
| 860 | main.log.exception( self.name + ": Object not as expected" ) |
| 861 | return None |
| 862 | except Exception: |
| 863 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 864 | main.cleanAndExit() |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 865 | |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 866 | def removeIntent( self, intentId, appId='org.onosproject.cli', |
| 867 | ip="DEFAULT", port="DEFAULT" ): |
| 868 | """ |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 869 | Remove intent for specified application id and intent id; |
| 870 | Returns None for exception |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 871 | """ |
| 872 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 873 | response = None |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 874 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 875 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 876 | ip = self.ip_address |
| 877 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 878 | main.log.warn( self.name + ": No port given, reverting to port " + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 879 | "from topo file" ) |
| 880 | port = self.port |
| 881 | # NOTE: REST url requires the intent id to be in decimal form |
| 882 | query = "/" + str( appId ) + "/" + str( int( intentId, 16 ) ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 883 | response = self.send( method="DELETE", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 884 | url="/intents" + query, ip = ip, port = port ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 885 | if response: |
| 886 | if 200 <= response[ 0 ] <= 299: |
| 887 | return main.TRUE |
| 888 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 889 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 890 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 891 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 892 | except ( AttributeError, TypeError ): |
| 893 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 894 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 895 | except Exception: |
| 896 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 897 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 898 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 899 | def getIntentsId( self, ip="DEFAULT", port="DEFAULT" ): |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 900 | """ |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 901 | Description: |
| 902 | Gets all intents ID using intents function |
| 903 | Returns: |
| 904 | List of intents ID if found any intents; Returns main.FALSE for other exceptions |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 905 | """ |
| 906 | try: |
| 907 | intentIdList = [] |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 908 | intentsJson = json.loads( self.intents( ip=ip, port=port ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 909 | for intent in intentsJson: |
| 910 | intentIdList.append( intent.get( 'id' ) ) |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 911 | if not intentIdList: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 912 | main.log.debug( self.name + ": Cannot find any intents" ) |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 913 | return main.FALSE |
| 914 | else: |
| 915 | return intentIdList |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 916 | except ( AttributeError, TypeError ): |
| 917 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 918 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 919 | except Exception: |
| 920 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 921 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 922 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 923 | def removeAllIntents( self, intentIdList ='ALL', appId='org.onosproject.cli', |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 924 | ip="DEFAULT", port="DEFAULT", delay=5 ): |
| 925 | """ |
| 926 | Description: |
| 927 | Remove all the intents |
| 928 | Returns: |
| 929 | Returns main.TRUE if all intents are removed, otherwise returns |
| 930 | main.FALSE; Returns None for exception |
| 931 | """ |
| 932 | try: |
| 933 | results = [] |
| 934 | if intentIdList == 'ALL': |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 935 | # intentIdList = self.getIntentsId( ip=ip, port=port ) |
| 936 | intentIdList = self.getIntentsId() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 937 | |
| 938 | main.log.info( self.name + ": Removing intents " + |
| 939 | str( intentIdList ) ) |
| 940 | |
| 941 | if isinstance( intentIdList, types.ListType ): |
| 942 | for intent in intentIdList: |
| 943 | results.append( self.removeIntent( intentId=intent, |
| 944 | appId=appId, |
| 945 | ip=ip, |
| 946 | port=port ) ) |
| 947 | # Check for remaining intents |
| 948 | # NOTE: Noticing some delay on Deleting the intents so i put |
| 949 | # this time out |
| 950 | import time |
| 951 | time.sleep( delay ) |
| 952 | intentRemain = len( json.loads( self.intents() ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 953 | if all( result == main.TRUE for result in results ) and \ |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 954 | intentRemain == 0: |
| 955 | main.log.info( self.name + ": All intents are removed " ) |
| 956 | return main.TRUE |
| 957 | else: |
| 958 | main.log.error( self.name + ": Did not removed all intents," |
| 959 | + " there are " + str( intentRemain ) |
| 960 | + " intents remaining" ) |
| 961 | return main.FALSE |
| 962 | else: |
| 963 | main.log.debug( self.name + ": There is no intents ID list" ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 964 | except ( AttributeError, TypeError ): |
| 965 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 966 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 967 | except Exception: |
| 968 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 969 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 970 | |
| 971 | def hosts( self, ip="DEFAULT", port="DEFAULT" ): |
| 972 | """ |
| 973 | Description: |
| 974 | Get a list of dictionary of all discovered hosts |
| 975 | Returns: |
| 976 | Returns a list of dictionary of information of the hosts currently |
| 977 | discovered by ONOS; Returns main.FALSE if error on requests; |
| 978 | Returns None for exception |
| 979 | """ |
| 980 | try: |
| 981 | output = None |
| 982 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 983 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 984 | ip = self.ip_address |
| 985 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 986 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 987 | "from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 988 | port = self.port |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 989 | response = self.send( url="/hosts", ip = ip, port = port ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 990 | if response: |
| 991 | if 200 <= response[ 0 ] <= 299: |
| 992 | output = response[ 1 ] |
| 993 | a = json.loads( output ).get( 'hosts' ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 994 | assert a is not None, "Error parsing json object" |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 995 | b = json.dumps( a ) |
| 996 | return b |
| 997 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 998 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 999 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1000 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1001 | except ( AttributeError, AssertionError, TypeError ): |
| 1002 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1003 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1004 | except Exception: |
| 1005 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1006 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1007 | |
| 1008 | def getHost( self, mac, vlan="-1", ip="DEFAULT", port="DEFAULT" ): |
| 1009 | """ |
| 1010 | Description: |
| 1011 | Gets the information from the given host |
| 1012 | Required: |
| 1013 | str mac - MAC address of the host |
| 1014 | Optional: |
| 1015 | str vlan - VLAN tag of the host, defaults to -1 |
| 1016 | Returns: |
| 1017 | Return the host id from the hosts/mac/vlan output in REST api |
| 1018 | whose 'id' contains mac/vlan; Returns None for exception; |
| 1019 | Returns main.FALSE if error on requests |
| 1020 | |
| 1021 | NOTE: |
| 1022 | Not sure what this function should do, any suggestion? |
| 1023 | """ |
| 1024 | try: |
| 1025 | output = None |
| 1026 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1027 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1028 | ip = self.ip_address |
| 1029 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1030 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 1031 | "from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1032 | port = self.port |
| 1033 | query = "/" + mac + "/" + vlan |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1034 | response = self.send( url="/hosts" + query, ip = ip, port = port ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1035 | if response: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1036 | # NOTE: What if the person wants other values? would it be better |
| 1037 | # to have a function that gets a key and return a value instead? |
| 1038 | # This function requires mac and vlan and returns an ID which |
| 1039 | # makes this current function useless |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1040 | if 200 <= response[ 0 ] <= 299: |
| 1041 | output = response[ 1 ] |
| 1042 | hostId = json.loads( output ).get( 'id' ) |
| 1043 | return hostId |
| 1044 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1045 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1046 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1047 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1048 | except ( AttributeError, TypeError ): |
| 1049 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1050 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1051 | except Exception: |
| 1052 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1053 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1054 | |
| 1055 | def topology( self, ip="DEFAULT", port="DEFAULT" ): |
| 1056 | """ |
| 1057 | Description: |
| 1058 | Gets the overview of network topology |
| 1059 | Returns: |
| 1060 | Returns a dictionary containing information about network topology; |
| 1061 | Returns None for exception |
| 1062 | """ |
| 1063 | try: |
| 1064 | output = None |
| 1065 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1066 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1067 | ip = self.ip_address |
| 1068 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1069 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 1070 | "from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1071 | port = self.port |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1072 | response = self.send( url="/topology", ip = ip, port = port ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1073 | if response: |
| 1074 | if 200 <= response[ 0 ] <= 299: |
| 1075 | output = response[ 1 ] |
| 1076 | a = json.loads( output ) |
| 1077 | b = json.dumps( a ) |
| 1078 | return b |
| 1079 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1080 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1081 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1082 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1083 | except ( AttributeError, TypeError ): |
| 1084 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1085 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1086 | except Exception: |
| 1087 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1088 | main.cleanAndExit() |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1089 | |
| 1090 | def devices( self, ip="DEFAULT", port="DEFAULT" ): |
| 1091 | """ |
| 1092 | Description: |
| 1093 | Get the devices discovered by ONOS is json string format |
| 1094 | Returns: |
| 1095 | a json string of the devices currently discovered by ONOS OR |
| 1096 | main.FALSE if there is an error in the request OR |
| 1097 | Returns None for exception |
| 1098 | """ |
| 1099 | try: |
| 1100 | output = None |
| 1101 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1102 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1103 | ip = self.ip_address |
| 1104 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1105 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1106 | "from topo file" ) |
| 1107 | port = self.port |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1108 | response = self.send( url="/devices", ip = ip, port = port ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1109 | if response: |
| 1110 | if 200 <= response[ 0 ] <= 299: |
| 1111 | output = response[ 1 ] |
| 1112 | a = json.loads( output ).get( 'devices' ) |
| 1113 | assert a is not None, "Error parsing json object" |
| 1114 | b = json.dumps( a ) |
| 1115 | return b |
| 1116 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1117 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1118 | ( response[ 0 ], response[ 1 ] ) ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1119 | return main.FALSE |
| 1120 | except ( AttributeError, AssertionError, TypeError ): |
| 1121 | main.log.exception( self.name + ": Object not as expected" ) |
| 1122 | return None |
| 1123 | except Exception: |
| 1124 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1125 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1126 | |
| 1127 | def getIntentState( self, intentsId, intentsJson=None, |
| 1128 | ip="DEFAULT", port="DEFAULT" ): |
| 1129 | """ |
| 1130 | Description: |
| 1131 | Get intent state. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1132 | Accepts a single intent ID (string type) or a list of intent IDs. |
| 1133 | Returns the state(string type) of the id if a single intent ID is |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1134 | accepted. |
| 1135 | Required: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1136 | intentId: intent ID (string type) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1137 | intentsJson: parsed json object from the onos:intents api |
| 1138 | Returns: |
| 1139 | Returns a dictionary with intent IDs as the key and its |
| 1140 | corresponding states as the values; Returns None for invalid IDs or |
| 1141 | Type error and any exceptions |
| 1142 | NOTE: |
| 1143 | An intent's state consist of INSTALLED,WITHDRAWN etc. |
| 1144 | """ |
| 1145 | try: |
| 1146 | state = "State is Undefined" |
| 1147 | if not intentsJson: |
| 1148 | intentsJsonTemp = json.loads( self.intents() ) |
| 1149 | else: |
| 1150 | intentsJsonTemp = json.loads( intentsJson ) |
| 1151 | if isinstance( intentsId, types.StringType ): |
| 1152 | for intent in intentsJsonTemp: |
| 1153 | if intentsId == intent[ 'id' ]: |
| 1154 | state = intent[ 'state' ] |
| 1155 | return state |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1156 | main.log.info( self.name + ": Cannot find intent ID" + str( intentsId ) + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1157 | " on the list" ) |
| 1158 | return state |
| 1159 | elif isinstance( intentsId, types.ListType ): |
| 1160 | dictList = [] |
| 1161 | for i in xrange( len( intentsId ) ): |
| 1162 | stateDict = {} |
| 1163 | for intents in intentsJsonTemp: |
| 1164 | if intentsId[ i ] == intents[ 'id' ]: |
| 1165 | stateDict[ 'state' ] = intents[ 'state' ] |
| 1166 | stateDict[ 'id' ] = intentsId[ i ] |
| 1167 | dictList.append( stateDict ) |
| 1168 | break |
| 1169 | if len( intentsId ) != len( dictList ): |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1170 | main.log.info( self.name + ": Cannot find some of the intent ID state" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1171 | return dictList |
| 1172 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1173 | main.log.info( self.name + ": Invalid intents ID entry" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1174 | return None |
| 1175 | |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1176 | except ( AttributeError, TypeError ): |
| 1177 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1178 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1179 | except Exception: |
| 1180 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1181 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1182 | |
| 1183 | def checkIntentState( self, intentsId="ALL", expectedState='INSTALLED', |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1184 | ip="DEFAULT", port="DEFAULT" ): |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1185 | """ |
| 1186 | Description: |
| 1187 | Check intents state based on expected state which defaults to |
| 1188 | INSTALLED state |
| 1189 | Required: |
| 1190 | intentsId - List of intents ID to be checked |
| 1191 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1192 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1193 | state in the list. |
| 1194 | *NOTE: You can pass in a list of expected state, |
| 1195 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
| 1196 | Return: |
| 1197 | Returns main.TRUE only if all intent are the same as expected states |
| 1198 | , otherwise, returns main.FALSE; Returns None for general exception |
| 1199 | """ |
| 1200 | try: |
| 1201 | # Generating a dictionary: intent id as a key and state as value |
| 1202 | returnValue = main.TRUE |
| 1203 | if intentsId == "ALL": |
| 1204 | intentsId = self.getIntentsId( ip=ip, port=port ) |
| 1205 | intentsDict = self.getIntentState( intentsId, ip=ip, port=port ) |
| 1206 | |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1207 | if len( intentsId ) != len( intentsDict ): |
| 1208 | main.log.error( self.name + ": There is something wrong " + |
| 1209 | "getting intents state" ) |
| 1210 | return main.FALSE |
| 1211 | |
| 1212 | if isinstance( expectedState, types.StringType ): |
| 1213 | for intents in intentsDict: |
| 1214 | if intents.get( 'state' ) != expectedState: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1215 | main.log.debug( self.name + ": Intent ID - " + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1216 | intents.get( 'id' ) + |
| 1217 | " actual state = " + |
| 1218 | intents.get( 'state' ) |
| 1219 | + " does not equal expected state = " |
| 1220 | + expectedState ) |
| 1221 | returnValue = main.FALSE |
| 1222 | |
| 1223 | elif isinstance( expectedState, types.ListType ): |
| 1224 | for intents in intentsDict: |
| 1225 | if not any( state == intents.get( 'state' ) for state in |
| 1226 | expectedState ): |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1227 | main.log.debug( self.name + ": Intent ID - " + |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1228 | intents.get( 'id' ) + |
| 1229 | " actual state = " + |
| 1230 | intents.get( 'state' ) + |
| 1231 | " does not equal expected states = " |
| 1232 | + str( expectedState ) ) |
| 1233 | returnValue = main.FALSE |
| 1234 | |
| 1235 | if returnValue == main.TRUE: |
| 1236 | main.log.info( self.name + ": All " + |
| 1237 | str( len( intentsDict ) ) + |
| 1238 | " intents are in " + str( expectedState ) + |
| 1239 | " state" ) |
| 1240 | return returnValue |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1241 | except ( AttributeError, TypeError ): |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1242 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1243 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1244 | except Exception: |
| 1245 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1246 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1247 | |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 1248 | def flows( self, ip="DEFAULT", port="DEFAULT", subjectClass=None, subjectKey=None ): |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1249 | """ |
| 1250 | Description: |
| 1251 | Get flows currently added to the system |
| 1252 | NOTE: |
| 1253 | The flows -j cli command has completely different format than |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1254 | the REST output |
| 1255 | |
| 1256 | Returns None for exception |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1257 | """ |
| 1258 | try: |
| 1259 | output = None |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 1260 | url = "/flows" |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1261 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1262 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1263 | ip = self.ip_address |
| 1264 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1265 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 1266 | "from topo file" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1267 | port = self.port |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 1268 | if subjectKey and not subjectClass: |
| 1269 | main.log.warning( "Subject Key provided without Subject Class. Ignoring Subject Key" ) |
| 1270 | if subjectClass: |
| 1271 | url += "/" + subjectClass |
| 1272 | if subjectKey: |
| 1273 | url += "/" + subjectKey |
| 1274 | response = self.send( url=url, ip=ip, port=port ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1275 | if response: |
| 1276 | if 200 <= response[ 0 ] <= 299: |
| 1277 | output = response[ 1 ] |
| 1278 | a = json.loads( output ).get( 'flows' ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1279 | assert a is not None, "Error parsing json object" |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1280 | b = json.dumps( a ) |
| 1281 | return b |
| 1282 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1283 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1284 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1285 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1286 | except ( AttributeError, AssertionError, TypeError ): |
| 1287 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1288 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1289 | except Exception: |
| 1290 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1291 | main.cleanAndExit() |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1292 | |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1293 | def getFlows( self, deviceId, flowId=None, ip="DEFAULT", port="DEFAULT" ): |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1294 | """ |
| 1295 | Description: |
| 1296 | Gets all the flows of the device or get a specific flow in the |
| 1297 | device by giving its flow ID |
| 1298 | Required: |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1299 | str deviceId - device/switch Id |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1300 | Optional: |
| 1301 | int/hex flowId - ID of the flow |
| 1302 | """ |
| 1303 | try: |
| 1304 | output = None |
| 1305 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1306 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1307 | ip = self.ip_address |
| 1308 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1309 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jon Hall | f723488 | 2015-08-28 13:16:31 -0700 | [diff] [blame] | 1310 | "from topo file" ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1311 | port = self.port |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1312 | url = "/flows/" + deviceId |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1313 | if flowId: |
| 1314 | url += "/" + str( int( flowId ) ) |
| 1315 | print url |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1316 | response = self.send( url=url, ip = ip, port = port ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1317 | if response: |
| 1318 | if 200 <= response[ 0 ] <= 299: |
| 1319 | output = response[ 1 ] |
| 1320 | a = json.loads( output ).get( 'flows' ) |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1321 | assert a is not None, "Error parsing json object" |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1322 | b = json.dumps( a ) |
| 1323 | return b |
| 1324 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1325 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1326 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1327 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1328 | except ( AttributeError, AssertionError, TypeError ): |
| 1329 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1330 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1331 | except Exception: |
| 1332 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1333 | main.cleanAndExit() |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1334 | |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1335 | def sendFlow( self, deviceId, flowJson, ip="DEFAULT", port="DEFAULT", debug=False ): |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1336 | """ |
| 1337 | Description: |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1338 | Sends a single flow to the specified device. This function exists |
| 1339 | so you can bypass the addFLow driver and send your own custom flow. |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1340 | Required: |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1341 | * The flow in json |
| 1342 | * the device id to add the flow to |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1343 | Returns: |
| 1344 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 1345 | if error on requests; |
| 1346 | Returns None for exceptions |
| 1347 | NOTE: |
| 1348 | The ip and port option are for the requests input's ip and port |
| 1349 | of the ONOS node |
| 1350 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1351 | |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1352 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1353 | if debug: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1354 | main.log.debug( self.name + ": Adding flow: " + self.pprint( flowJson ) ) |
| 1355 | response = None |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1356 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1357 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1358 | ip = self.ip_address |
| 1359 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1360 | main.log.warn( self.name + ": No port given, reverting to port " + |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1361 | "from topo file" ) |
| 1362 | port = self.port |
| 1363 | url = "/flows/" + deviceId |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 1364 | response = self.send( method="POST", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1365 | url=url, ip = ip, port = port, |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1366 | data=json.dumps( flowJson ) ) |
| 1367 | if response: |
Ming Yan Shu | ab2f7f5 | 2016-08-03 15:21:24 -0700 | [diff] [blame] | 1368 | if "201" in str( response[ 0 ] ): |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1369 | main.log.info( self.name + ": Successfully POST flow" + |
| 1370 | "in device: " + str( deviceId ) ) |
| 1371 | return main.TRUE |
| 1372 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1373 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1374 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1375 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1376 | except NotImplementedError as e: |
| 1377 | raise e # Inform the caller |
| 1378 | except ( AttributeError, TypeError ): |
| 1379 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1380 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1381 | except Exception: |
| 1382 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1383 | main.cleanAndExit() |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1384 | |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1385 | def addFlow( self, |
| 1386 | deviceId, |
| 1387 | appId=0, |
| 1388 | ingressPort="", |
| 1389 | egressPort="", |
| 1390 | ethType="", |
| 1391 | ethSrc="", |
| 1392 | ethDst="", |
| 1393 | vlan="", |
| 1394 | ipProto="", |
| 1395 | ipSrc=(), |
| 1396 | ipDst=(), |
| 1397 | tcpSrc="", |
| 1398 | tcpDst="", |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 1399 | udpDst="", |
| 1400 | udpSrc="", |
| 1401 | mpls="", |
alison | e14d7b0 | 2016-07-06 10:31:51 -0700 | [diff] [blame] | 1402 | priority=100, |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 1403 | groupId="", |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1404 | ip="DEFAULT", |
| 1405 | port="DEFAULT", |
| 1406 | debug=False ): |
| 1407 | """ |
| 1408 | Description: |
| 1409 | Creates a single flow in the specified device |
| 1410 | Required: |
| 1411 | * deviceId: id of the device |
| 1412 | Optional: |
| 1413 | * ingressPort: port ingress device |
| 1414 | * egressPort: port of egress device |
| 1415 | * ethType: specify ethType |
| 1416 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1417 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1418 | * ipProto: specify ip protocol |
| 1419 | * ipSrc: specify ip source address with mask eg. ip#/24 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1420 | as a tuple (type, ip#) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1421 | * ipDst: specify ip destination address eg. ip#/24 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1422 | as a tuple (type, ip#) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1423 | * tcpSrc: specify tcp source port |
| 1424 | * tcpDst: specify tcp destination port |
| 1425 | Returns: |
| 1426 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 1427 | if error on requests; |
| 1428 | Returns None for exceptions |
| 1429 | NOTE: |
| 1430 | The ip and port option are for the requests input's ip and port |
| 1431 | of the ONOS node |
| 1432 | """ |
| 1433 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1434 | flowJson = { "priority": priority, |
| 1435 | "isPermanent": "true", |
| 1436 | "timeout": 0, |
| 1437 | "deviceId": deviceId, |
| 1438 | "treatment": { "instructions": [] }, |
| 1439 | "selector": { "criteria": [] }} |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1440 | if appId: |
| 1441 | flowJson[ "appId" ] = appId |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 1442 | |
| 1443 | if groupId: |
| 1444 | flowJson[ 'treatment' ][ 'instructions' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1445 | "type": "GROUP", |
| 1446 | "groupId": groupId } ) |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 1447 | |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1448 | if egressPort: |
| 1449 | flowJson[ 'treatment' ][ 'instructions' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1450 | "type": "OUTPUT", |
| 1451 | "port": egressPort } ) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1452 | if ingressPort: |
| 1453 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1454 | "type": "IN_PORT", |
| 1455 | "port": ingressPort } ) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1456 | if ethType: |
| 1457 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1458 | "type": "ETH_TYPE", |
| 1459 | "ethType": ethType } ) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1460 | if ethSrc: |
| 1461 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1462 | "type": "ETH_SRC", |
| 1463 | "mac": ethSrc } ) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1464 | if ethDst: |
| 1465 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1466 | "type": "ETH_DST", |
| 1467 | "mac": ethDst } ) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1468 | if vlan: |
| 1469 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1470 | "type": "VLAN_VID", |
| 1471 | "vlanId": vlan } ) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 1472 | if mpls: |
| 1473 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1474 | "type": "MPLS_LABEL", |
| 1475 | "label": mpls } ) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1476 | if ipSrc: |
| 1477 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1478 | "type": ipSrc[ 0 ], |
| 1479 | "ip": ipSrc[ 1 ] } ) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1480 | if ipDst: |
| 1481 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1482 | "type": ipDst[ 0 ], |
| 1483 | "ip": ipDst[ 1 ] } ) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1484 | if tcpSrc: |
| 1485 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1486 | "type": "TCP_SRC", |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1487 | "tcpPort": tcpSrc } ) |
| 1488 | if tcpDst: |
| 1489 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1490 | "type": "TCP_DST", |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1491 | "tcpPort": tcpDst } ) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 1492 | if udpSrc: |
| 1493 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1494 | "type": "UDP_SRC", |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 1495 | "udpPort": udpSrc } ) |
| 1496 | if udpDst: |
| 1497 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1498 | "type": "UDP_DST", |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 1499 | "udpPort": udpDst } ) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1500 | if ipProto: |
| 1501 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1502 | "type": "IP_PROTO", |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1503 | "protocol": ipProto } ) |
| 1504 | |
Jeremy Ronquillo | 29260cd | 2017-10-13 13:30:54 -0700 | [diff] [blame] | 1505 | return self.sendFlow( deviceId=deviceId, flowJson=flowJson, debug=debug, ip=ip, port=port ) |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1506 | |
| 1507 | except ( AttributeError, TypeError ): |
| 1508 | main.log.exception( self.name + ": Object not as expected" ) |
| 1509 | return None |
| 1510 | except Exception: |
| 1511 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1512 | main.cleanAndExit() |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 1513 | |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1514 | def removeFlow( self, deviceId, flowId, |
| 1515 | ip="DEFAULT", port="DEFAULT" ): |
| 1516 | """ |
| 1517 | Description: |
| 1518 | Remove specific device flow |
| 1519 | Required: |
| 1520 | str deviceId - id of the device |
| 1521 | str flowId - id of the flow |
| 1522 | Return: |
| 1523 | Returns main.TRUE if successfully deletes flows, otherwise |
| 1524 | Returns main.FALSE, Returns None on error |
| 1525 | """ |
| 1526 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1527 | response = None |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1528 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1529 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1530 | ip = self.ip_address |
| 1531 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1532 | main.log.warn( self.name + ": No port given, reverting to port " + |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1533 | "from topo file" ) |
| 1534 | port = self.port |
| 1535 | # NOTE: REST url requires the intent id to be in decimal form |
| 1536 | query = "/" + str( deviceId ) + "/" + str( int( flowId ) ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 1537 | response = self.send( method="DELETE", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1538 | url="/flows" + query, ip = ip, port = port ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1539 | if response: |
| 1540 | if 200 <= response[ 0 ] <= 299: |
| 1541 | return main.TRUE |
| 1542 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1543 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1544 | ( response[ 0 ], response[ 1 ] ) ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1545 | return main.FALSE |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1546 | except ( AttributeError, TypeError ): |
| 1547 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1548 | return None |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1549 | except Exception: |
| 1550 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1551 | main.cleanAndExit() |
kelvin-onlab | 9b42b0a | 2015-08-05 14:43:58 -0700 | [diff] [blame] | 1552 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1553 | def checkFlowsState( self , ip="DEFAULT", port="DEFAULT" ): |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1554 | """ |
| 1555 | Description: |
| 1556 | Check if all the current flows are in ADDED state |
| 1557 | Return: |
| 1558 | returnValue - Returns main.TRUE only if all flows are in |
| 1559 | return main.FALSE otherwise; |
| 1560 | Returns None for exception |
| 1561 | """ |
| 1562 | try: |
| 1563 | tempFlows = json.loads( self.flows( ip=ip, port=port ) ) |
| 1564 | returnValue = main.TRUE |
| 1565 | for flow in tempFlows: |
| 1566 | if flow.get( 'state' ) != 'ADDED': |
| 1567 | main.log.info( self.name + ": flow Id: " + |
| 1568 | str( flow.get( 'groupId' ) ) + |
| 1569 | " | state:" + |
| 1570 | str( flow.get( 'state' ) ) ) |
| 1571 | returnValue = main.FALSE |
| 1572 | return returnValue |
Jon Hall | e401b09 | 2015-09-23 13:34:24 -0700 | [diff] [blame] | 1573 | except ( AttributeError, TypeError ): |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1574 | main.log.exception( self.name + ": Object not as expected" ) |
kelvin-onlab | 03eb88d | 2015-07-22 10:29:02 -0700 | [diff] [blame] | 1575 | return None |
| 1576 | except Exception: |
| 1577 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1578 | main.cleanAndExit() |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1579 | |
| 1580 | def getNetCfg( self, ip="DEFAULT", port="DEFAULT", |
| 1581 | subjectClass=None, subjectKey=None, configKey=None ): |
| 1582 | """ |
| 1583 | Description: |
| 1584 | Get a json object with the ONOS network configurations |
| 1585 | Returns: |
| 1586 | A json object containing the network configuration in |
| 1587 | ONOS; Returns main.FALSE if error on requests; |
| 1588 | Returns None for exception |
| 1589 | """ |
| 1590 | try: |
| 1591 | output = None |
| 1592 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1593 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1594 | ip = self.ip_address |
| 1595 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1596 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1597 | "from topo file" ) |
| 1598 | port = self.port |
| 1599 | url = "/network/configuration" |
| 1600 | if subjectClass: |
| 1601 | url += "/" + subjectClass |
| 1602 | if subjectKey: |
| 1603 | url += "/" + subjectKey |
| 1604 | if configKey: |
| 1605 | url += "/" + configKey |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1606 | response = self.send( url=url, ip = ip, port = port ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1607 | if response: |
| 1608 | if 200 <= response[ 0 ] <= 299: |
| 1609 | output = response[ 1 ] |
| 1610 | a = json.loads( output ) |
| 1611 | b = json.dumps( a ) |
| 1612 | return b |
| 1613 | elif response[ 0 ] == 404: |
| 1614 | main.log.error( "Requested configuration doesn't exist: " + |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1615 | ( response[ 0 ], response[ 1 ] ) ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1616 | return {} |
| 1617 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1618 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1619 | ( response[ 0 ], response[ 1 ] ) ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1620 | return main.FALSE |
| 1621 | except ( AttributeError, TypeError ): |
| 1622 | main.log.exception( self.name + ": Object not as expected" ) |
| 1623 | return None |
| 1624 | except Exception: |
| 1625 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1626 | main.cleanAndExit() |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1627 | |
| 1628 | def setNetCfg( self, cfgJson, ip="DEFAULT", port="DEFAULT", |
| 1629 | subjectClass=None, subjectKey=None, configKey=None ): |
| 1630 | """ |
| 1631 | Description: |
| 1632 | Set a json object with the ONOS network configurations |
| 1633 | Returns: |
| 1634 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 1635 | if error on requests; |
| 1636 | Returns None for exceptions |
| 1637 | |
| 1638 | """ |
| 1639 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1640 | response = None |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1641 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1642 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1643 | ip = self.ip_address |
| 1644 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1645 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1646 | "from topo file" ) |
| 1647 | port = self.port |
| 1648 | url = "/network/configuration" |
| 1649 | if subjectClass: |
| 1650 | url += "/" + subjectClass |
| 1651 | if subjectKey: |
| 1652 | url += "/" + subjectKey |
| 1653 | if configKey: |
| 1654 | url += "/" + configKey |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 1655 | response = self.send( method="POST", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1656 | url=url, ip = ip, port = port, |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1657 | data=json.dumps( cfgJson ) ) |
| 1658 | if response: |
| 1659 | if 200 <= response[ 0 ] <= 299: |
| 1660 | main.log.info( self.name + ": Successfully POST cfg" ) |
| 1661 | return main.TRUE |
| 1662 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1663 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1664 | ( response[ 0 ], response[ 1 ] ) ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1665 | return main.FALSE |
| 1666 | except ( AttributeError, TypeError ): |
| 1667 | main.log.exception( self.name + ": Object not as expected" ) |
| 1668 | return None |
| 1669 | except Exception: |
| 1670 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1671 | main.cleanAndExit() |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1672 | |
| 1673 | def removeNetCfg( self, ip="DEFAULT", port="DEFAULT", |
| 1674 | subjectClass=None, subjectKey=None, configKey=None ): |
| 1675 | """ |
| 1676 | Description: |
| 1677 | Remove a json object from the ONOS network configurations |
| 1678 | Returns: |
| 1679 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 1680 | if error on requests; |
| 1681 | Returns None for exceptions |
| 1682 | |
| 1683 | """ |
| 1684 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1685 | response = None |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1686 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1687 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1688 | ip = self.ip_address |
| 1689 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1690 | main.log.warn( self.name + ": No port given, reverting to port " + |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1691 | "from topo file" ) |
| 1692 | port = self.port |
| 1693 | url = "/network/configuration" |
| 1694 | if subjectClass: |
| 1695 | url += "/" + subjectClass |
| 1696 | if subjectKey: |
| 1697 | url += "/" + subjectKey |
| 1698 | if configKey: |
| 1699 | url += "/" + configKey |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 1700 | response = self.send( method="DELETE", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1701 | url=url, ip = ip, port = port ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1702 | if response: |
| 1703 | if 200 <= response[ 0 ] <= 299: |
| 1704 | main.log.info( self.name + ": Successfully delete cfg" ) |
| 1705 | return main.TRUE |
| 1706 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 1707 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1708 | ( response[ 0 ], response[ 1 ] ) ) |
Jon Hall | 66e001c | 2015-11-12 09:45:10 -0800 | [diff] [blame] | 1709 | return main.FALSE |
| 1710 | except ( AttributeError, TypeError ): |
| 1711 | main.log.exception( self.name + ": Object not as expected" ) |
| 1712 | return None |
| 1713 | except Exception: |
| 1714 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1715 | main.cleanAndExit() |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1716 | |
Jon Hall | 10e2ab8 | 2020-09-15 17:14:54 -0700 | [diff] [blame] | 1717 | def getXconnect( self, ip="DEFAULT", port="DEFAULT" ): |
| 1718 | """ |
| 1719 | Description: |
| 1720 | Get xconnects |
| 1721 | Returns: |
| 1722 | Return xconnects json object |
| 1723 | Returns None for exceptions |
| 1724 | |
| 1725 | """ |
| 1726 | try: |
| 1727 | base = "/onos/segmentrouting" |
| 1728 | response = None |
| 1729 | if ip == "DEFAULT": |
| 1730 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
| 1731 | ip = self.ip_address |
| 1732 | if port == "DEFAULT": |
| 1733 | main.log.warn( self.name + ": No port given, reverting to port " + |
| 1734 | "from topo file" ) |
| 1735 | port = self.port |
| 1736 | url = "/xconnect" |
| 1737 | response = self.send( method="GET", |
| 1738 | base=base, |
| 1739 | url=url, ip = ip, port = port ) |
| 1740 | if response: |
| 1741 | if 200 <= response[ 0 ] <= 299: |
| 1742 | main.log.info( self.name + ": Successfully POST cfg" ) |
| 1743 | return main.TRUE |
| 1744 | else: |
| 1745 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1746 | ( response[ 0 ], response[ 1 ] ) ) |
| 1747 | return main.FALSE |
| 1748 | except ( AttributeError, TypeError ): |
| 1749 | main.log.exception( self.name + ": Object not as expected" ) |
| 1750 | return None |
| 1751 | except Exception: |
| 1752 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1753 | main.cleanAndExit() |
| 1754 | |
| 1755 | def setXconnect( self, deviceId, vlanId, port1, port2, ip="DEFAULT", port="DEFAULT" ): |
| 1756 | """ |
| 1757 | Description: |
| 1758 | Set xconnects |
| 1759 | Returns: |
| 1760 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 1761 | if error on requests; |
| 1762 | Returns None for exceptions |
| 1763 | |
| 1764 | """ |
| 1765 | try: |
| 1766 | cfgJson = json.loads( '{"deviceId": "%s", "vlanId": "%s", "endpoints":[%s,%s]}' % |
| 1767 | ( deviceId, vlanId, port1, port2 ) ) |
| 1768 | response = self.setXconnectJson( cfgJson, ip=ip, port=port ) |
| 1769 | except ( AttributeError, TypeError ): |
| 1770 | main.log.exception( self.name + ": Object not as expected" ) |
| 1771 | return None |
| 1772 | except Exception: |
| 1773 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1774 | main.cleanAndExit() |
| 1775 | |
| 1776 | def setXconnectJson( self, cfgJson, ip="DEFAULT", port="DEFAULT" ): |
| 1777 | """ |
| 1778 | Description: |
| 1779 | Set xconnects |
| 1780 | Returns: |
| 1781 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 1782 | if error on requests; |
| 1783 | Returns None for exceptions |
| 1784 | |
| 1785 | """ |
| 1786 | try: |
| 1787 | base = "/onos/segmentrouting" |
| 1788 | response = None |
| 1789 | if ip == "DEFAULT": |
| 1790 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
| 1791 | ip = self.ip_address |
| 1792 | if port == "DEFAULT": |
| 1793 | main.log.warn( self.name + ": No port given, reverting to port " + |
| 1794 | "from topo file" ) |
| 1795 | port = self.port |
| 1796 | url = "/xconnect" |
| 1797 | response = self.send( method="POST", |
| 1798 | base=base, |
| 1799 | url=url, ip = ip, port = port, |
| 1800 | data=json.dumps( cfgJson ) ) |
| 1801 | if response: |
| 1802 | if 200 <= response[ 0 ] <= 299: |
| 1803 | main.log.info( self.name + ": Successfully POST cfg" ) |
| 1804 | return main.TRUE |
| 1805 | else: |
| 1806 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1807 | ( response[ 0 ], response[ 1 ] ) ) |
| 1808 | return main.FALSE |
| 1809 | except ( AttributeError, TypeError ): |
| 1810 | main.log.exception( self.name + ": Object not as expected" ) |
| 1811 | return None |
| 1812 | except Exception: |
| 1813 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1814 | main.cleanAndExit() |
| 1815 | |
| 1816 | def deleteXconnect( self, cfgJson, ip="DEFAULT", port="DEFAULT" ): |
| 1817 | """ |
| 1818 | Description: |
| 1819 | Remove xconnects |
| 1820 | Returns: |
| 1821 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 1822 | if error on requests; |
| 1823 | Returns None for exceptions |
| 1824 | |
| 1825 | """ |
| 1826 | try: |
| 1827 | base = "/onos/segmentrouting" |
| 1828 | response = None |
| 1829 | if ip == "DEFAULT": |
| 1830 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
| 1831 | ip = self.ip_address |
| 1832 | if port == "DEFAULT": |
| 1833 | main.log.warn( self.name + ": No port given, reverting to port " + |
| 1834 | "from topo file" ) |
| 1835 | port = self.port |
| 1836 | url = "/xconnect" |
| 1837 | response = self.send( method="DELETE", |
| 1838 | base=base, |
| 1839 | url=url, ip = ip, port = port, |
| 1840 | data=json.dumps( cfgJson ) ) |
| 1841 | if response: |
| 1842 | if 200 <= response[ 0 ] <= 299: |
| 1843 | main.log.info( self.name + ": Successfully POST cfg" ) |
| 1844 | return main.TRUE |
| 1845 | else: |
| 1846 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 1847 | ( response[ 0 ], response[ 1 ] ) ) |
| 1848 | return main.FALSE |
| 1849 | except ( AttributeError, TypeError ): |
| 1850 | main.log.exception( self.name + ": Object not as expected" ) |
| 1851 | return None |
| 1852 | except Exception: |
| 1853 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1854 | main.cleanAndExit() |
| 1855 | |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1856 | def createFlowBatch( self, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1857 | numSw = 1, |
| 1858 | swIndex = 1, |
| 1859 | batchSize = 1, |
| 1860 | batchIndex = 1, |
| 1861 | deviceIdpreFix = "of:", |
| 1862 | appId=0, |
| 1863 | deviceID="", |
| 1864 | ingressPort="", |
| 1865 | egressPort="", |
| 1866 | ethType="", |
| 1867 | ethSrc="", |
| 1868 | ethDst="", |
| 1869 | vlan="", |
| 1870 | ipProto="", |
| 1871 | ipSrc=(), |
| 1872 | ipDst=(), |
| 1873 | tcpSrc="", |
| 1874 | tcpDst="", |
| 1875 | udpDst="", |
| 1876 | udpSrc="", |
| 1877 | mpls="", |
| 1878 | ip="DEFAULT", |
| 1879 | port="DEFAULT", |
| 1880 | debug=False ): |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1881 | """ |
| 1882 | Description: |
| 1883 | Creates batches of MAC-rule flows for POST. |
| 1884 | Predefined MAC: 2 MS Hex digit for iterating devices |
| 1885 | Next 5 Hex digit for iterating batch numbers |
| 1886 | Next 5 Hex digit for iterating flows within a batch |
| 1887 | Required: |
| 1888 | * deviceId: id of the device |
| 1889 | Optional: |
| 1890 | * ingressPort: port ingress device |
| 1891 | * egressPort: port of egress device |
| 1892 | * ethType: specify ethType |
| 1893 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1894 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1895 | * ipProto: specify ip protocol |
| 1896 | * ipSrc: specify ip source address with mask eg. ip#/24 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1897 | as a tuple (type, ip#) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1898 | * ipDst: specify ip destination address eg. ip#/24 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 1899 | as a tuple (type, ip#) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1900 | * tcpSrc: specify tcp source port |
| 1901 | * tcpDst: specify tcp destination port |
| 1902 | Returns: |
| 1903 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 1904 | if error on requests; |
| 1905 | Returns None for exceptions |
| 1906 | NOTE: |
| 1907 | The ip and port option are for the requests input's ip and port |
| 1908 | of the ONOS node |
| 1909 | """ |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1910 | # from pprint import pprint |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1911 | |
| 1912 | flowJsonList = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1913 | flowJsonBatch = { "flows": flowJsonList } |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1914 | dev = swIndex |
| 1915 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1916 | for fl in range( 1, batchSize + 1 ): |
| 1917 | flowJson = { "priority": 100, |
| 1918 | "deviceId": "", |
| 1919 | "isPermanent": "true", |
| 1920 | "timeout": 0, |
| 1921 | "treatment": { "instructions": [] }, |
| 1922 | "selector": { "criteria": [] }} |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1923 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1924 | # main.log.info("fl: " + str(fl)) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1925 | if dev <= numSw: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1926 | deviceId = deviceIdpreFix + "{0:0{1}x}".format( dev, 16 ) |
| 1927 | # print deviceId |
| 1928 | flowJson[ 'deviceId' ] = deviceId |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1929 | dev += 1 |
| 1930 | else: |
| 1931 | dev = 1 |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1932 | deviceId = deviceIdpreFix + "{0:0{1}x}".format( dev, 16 ) |
| 1933 | # print deviceId |
| 1934 | flowJson[ 'deviceId' ] = deviceId |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1935 | dev += 1 |
| 1936 | |
| 1937 | # ethSrc starts with "0"; ethDst starts with "1" |
| 1938 | # 2 Hex digit of device number; 5 digits of batch index number; 5 digits of batch size |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1939 | ethS = "%02X" % int( "0" + "{0:0{1}b}".format( dev, 7 ), 2 ) + \ |
| 1940 | "{0:0{1}x}".format( batchIndex, 5 ) + "{0:0{1}x}".format( fl, 5 ) |
| 1941 | ethSrc = ':'.join( ethS[ i: i+2 ] for i in range( 0, len( ethS ), 2 ) ) |
| 1942 | ethD = "%02X" % int( "1" + "{0:0{1}b}".format( dev, 7 ), 2 ) + \ |
| 1943 | "{0:0{1}x}".format( batchIndex, 5 ) + "{0:0{1}x}".format( fl, 5 ) |
| 1944 | ethDst = ':'.join( ethD[ i: i+2 ] for i in range( 0, len( ethD ), 2 ) ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1945 | |
| 1946 | if appId: |
| 1947 | flowJson[ "appId" ] = appId |
| 1948 | |
| 1949 | if egressPort: |
| 1950 | flowJson[ 'treatment' ][ 'instructions' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1951 | "type": "OUTPUT", |
| 1952 | "port": egressPort } ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1953 | if ingressPort: |
| 1954 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1955 | "type": "IN_PORT", |
| 1956 | "port": ingressPort } ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1957 | if ethType: |
| 1958 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1959 | "type": "ETH_TYPE", |
| 1960 | "ethType": ethType } ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1961 | if ethSrc: |
| 1962 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1963 | "type": "ETH_SRC", |
| 1964 | "mac": ethSrc } ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1965 | if ethDst: |
| 1966 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1967 | "type": "ETH_DST", |
| 1968 | "mac": ethDst } ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1969 | if vlan: |
| 1970 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1971 | "type": "VLAN_VID", |
| 1972 | "vlanId": vlan } ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1973 | if mpls: |
| 1974 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1975 | "type": "MPLS_LABEL", |
| 1976 | "label": mpls } ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1977 | if ipSrc: |
| 1978 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1979 | "type": ipSrc[ 0 ], |
| 1980 | "ip": ipSrc[ 1 ] } ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1981 | if ipDst: |
| 1982 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1983 | "type": ipDst[ 0 ], |
| 1984 | "ip": ipDst[ 1 ] } ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1985 | if tcpSrc: |
| 1986 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1987 | "type": "TCP_SRC", |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1988 | "tcpPort": tcpSrc } ) |
| 1989 | if tcpDst: |
| 1990 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1991 | "type": "TCP_DST", |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1992 | "tcpPort": tcpDst } ) |
| 1993 | if udpSrc: |
| 1994 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1995 | "type": "UDP_SRC", |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 1996 | "udpPort": udpSrc } ) |
| 1997 | if udpDst: |
| 1998 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1999 | "type": "UDP_DST", |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2000 | "udpPort": udpDst } ) |
| 2001 | if ipProto: |
| 2002 | flowJson[ 'selector' ][ 'criteria' ].append( { |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2003 | "type": "IP_PROTO", |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2004 | "protocol": ipProto } ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2005 | # pprint(flowJson) |
| 2006 | flowJsonList.append( flowJson ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2007 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2008 | main.log.info( self.name + ": Number of flows in batch: " + str( len( flowJsonList ) ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2009 | flowJsonBatch[ 'flows' ] = flowJsonList |
| 2010 | # pprint(flowJsonBatch) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2011 | |
| 2012 | return flowJsonBatch |
| 2013 | |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2014 | def sendFlowBatch( self, batch={}, ip="DEFAULT", port="DEFAULT", debug=False ): |
| 2015 | """ |
| 2016 | Description: |
| 2017 | Sends a single flow batch through /flows REST API. |
| 2018 | Required: |
| 2019 | * The batch of flows |
| 2020 | Returns: |
| 2021 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 2022 | if error on requests; |
| 2023 | Returns None for exceptions |
| 2024 | NOTE: |
| 2025 | The ip and port option are for the requests input's ip and port |
| 2026 | of the ONOS node |
| 2027 | """ |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2028 | |
| 2029 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2030 | if debug: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2031 | main.log.debug( self.name + ": Adding flow: " + self.pprint( batch ) ) |
| 2032 | response = None |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2033 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2034 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2035 | ip = self.ip_address |
| 2036 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2037 | main.log.warn( self.name + ": No port given, reverting to port " + |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2038 | "from topo file" ) |
| 2039 | port = self.port |
| 2040 | url = "/flows/" |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 2041 | response = self.send( method="POST", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2042 | url=url, ip = ip, port = port, |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2043 | data=json.dumps( batch ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2044 | # main.log.info("Post response is: ", str(response[0])) |
| 2045 | if response[ 0 ] == 200: |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2046 | main.log.info( self.name + ": Successfully POST flow batch" ) |
| 2047 | return main.TRUE, response |
| 2048 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2049 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 2050 | ( response[ 0 ], response[ 1 ] ) ) |
You Wang | 7b5b226 | 2016-11-10 13:54:56 -0800 | [diff] [blame] | 2051 | return main.FALSE, response |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2052 | except NotImplementedError as e: |
| 2053 | raise e # Inform the caller |
| 2054 | except ( AttributeError, TypeError ): |
| 2055 | main.log.exception( self.name + ": Object not as expected" ) |
You Wang | 7b5b226 | 2016-11-10 13:54:56 -0800 | [diff] [blame] | 2056 | return None, None |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2057 | except Exception: |
| 2058 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2059 | main.cleanAndExit() |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2060 | |
| 2061 | def removeFlowBatch( self, batch={}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2062 | ip="DEFAULT", port="DEFAULT" ): |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2063 | """ |
| 2064 | Description: |
| 2065 | Remove a batch of flows |
| 2066 | Required: |
| 2067 | flow batch |
| 2068 | Return: |
| 2069 | Returns main.TRUE if successfully deletes flows, otherwise |
| 2070 | Returns main.FALSE, Returns None on error |
| 2071 | """ |
| 2072 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2073 | response = None |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2074 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2075 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2076 | ip = self.ip_address |
| 2077 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2078 | main.log.warn( self.name + ": No port given, reverting to port " + |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2079 | "from topo file" ) |
| 2080 | port = self.port |
| 2081 | # NOTE: REST url requires the intent id to be in decimal form |
| 2082 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 2083 | response = self.send( method="DELETE", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2084 | url="/flows/", ip = ip, port = port, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2085 | data = json.dumps( batch ) ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2086 | if response: |
| 2087 | if 200 <= response[ 0 ] <= 299: |
| 2088 | return main.TRUE |
| 2089 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2090 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 2091 | ( response[ 0 ], response[ 1 ] ) ) |
suibin zhang | 1730862 | 2016-04-14 15:45:30 -0700 | [diff] [blame] | 2092 | return main.FALSE |
| 2093 | except ( AttributeError, TypeError ): |
| 2094 | main.log.exception( self.name + ": Object not as expected" ) |
| 2095 | return None |
| 2096 | except Exception: |
| 2097 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2098 | main.cleanAndExit() |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2099 | |
| 2100 | def getTopology( self, topologyOutput ): |
| 2101 | """ |
| 2102 | Definition: |
| 2103 | Loads a json topology output |
| 2104 | Return: |
| 2105 | topology = current ONOS topology |
| 2106 | """ |
| 2107 | import json |
| 2108 | try: |
| 2109 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2110 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2111 | main.log.debug( topology ) |
| 2112 | return topology |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2113 | except Exception: |
| 2114 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2115 | main.cleanAndExit() |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2116 | |
| 2117 | def checkStatus( |
| 2118 | self, |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 2119 | numswitch, |
| 2120 | numlink, |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2121 | logLevel="info" ): |
| 2122 | """ |
| 2123 | Checks the number of switches & links that ONOS sees against the |
| 2124 | supplied values. By default this will report to main.log, but the |
| 2125 | log level can be specific. |
| 2126 | |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 2127 | Params: numswitch = expected number of switches |
| 2128 | numlink = expected number of links |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2129 | logLevel = level to log to. |
| 2130 | Currently accepts 'info', 'warn' and 'report' |
| 2131 | |
| 2132 | Returns: main.TRUE if the number of switches and links are correct, |
| 2133 | main.FALSE if the number of switches and links is incorrect, |
| 2134 | and main.ERROR otherwise |
| 2135 | """ |
| 2136 | try: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2137 | topology = self.getTopology( self.topology() ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2138 | # summary = self.summary() |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2139 | if topology == {}: |
| 2140 | return main.ERROR |
| 2141 | output = "" |
| 2142 | # Is the number of switches is what we expected |
| 2143 | devices = topology.get( 'devices', False ) |
| 2144 | links = topology.get( 'links', False ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 2145 | if devices is False or links is False: |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2146 | return main.ERROR |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 2147 | switchCheck = ( int( devices ) == int( numswitch ) ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2148 | # Is the number of links is what we expected |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 2149 | linkCheck = ( int( links ) == int( numlink ) ) |
| 2150 | if switchCheck and linkCheck: |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2151 | # We expected the correct numbers |
| 2152 | output = output + "The number of links and switches match "\ |
| 2153 | + "what was expected" |
| 2154 | result = main.TRUE |
| 2155 | else: |
| 2156 | output = output + \ |
| 2157 | "The number of links and switches does not match " + \ |
| 2158 | "what was expected" |
| 2159 | result = main.FALSE |
| 2160 | output = output + "\n ONOS sees %i devices" % int( devices ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 2161 | output = output + " (%i expected) " % int( numswitch ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2162 | output = output + "and %i links " % int( links ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 2163 | output = output + "(%i expected)" % int( numlink ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2164 | if logLevel == "report": |
| 2165 | main.log.report( output ) |
| 2166 | elif logLevel == "warn": |
| 2167 | main.log.warn( output ) |
| 2168 | else: |
| 2169 | main.log.info( output ) |
| 2170 | return result |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2171 | except Exception: |
| 2172 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2173 | main.cleanAndExit() |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2174 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2175 | def addGroup( self, deviceId, groupType, bucketList, appCookie, groupId, |
| 2176 | ip="DEFAULT", port="DEFAULT", debug=False ): |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2177 | """ |
| 2178 | Description: |
| 2179 | Creates a single Group for the specified device. |
| 2180 | Required: |
| 2181 | * deviceId: id of the device |
| 2182 | * type: Type of the Group |
| 2183 | * bucketList: Buckets to be added to the group |
| 2184 | * appCookie: Cookie for the Group |
| 2185 | * groupId: Id of the Group |
| 2186 | Returns: |
| 2187 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 2188 | if error on requests; |
| 2189 | Returns None for exceptions |
| 2190 | Note: |
| 2191 | The ip and port option are for the requests input's ip and port |
| 2192 | of the ONOS node |
| 2193 | """ |
| 2194 | try: |
| 2195 | groupJson = { "type": groupType, |
| 2196 | "appCookie": appCookie, |
| 2197 | "groupId": groupId, |
| 2198 | "buckets": bucketList |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2199 | } |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2200 | return self.sendGroup( deviceId=deviceId, groupJson=groupJson, ip="DEFAULT", port="DEFAULT", debug=False ) |
| 2201 | |
| 2202 | except ( AttributeError, TypeError ): |
| 2203 | main.log.exception( self.name + ": Object not as expected" ) |
| 2204 | return None |
| 2205 | except Exception: |
| 2206 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2207 | main.cleanAndExit() |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2208 | |
| 2209 | def sendGroup( self, deviceId, groupJson, ip="DEFAULT", port="DEFAULT", debug=False ): |
| 2210 | """ |
| 2211 | Description: |
| 2212 | Sends a single group to the specified device. |
| 2213 | Required: |
| 2214 | * deviceId: id of the device |
| 2215 | * groupJson: the group in json |
| 2216 | Returns: |
| 2217 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 2218 | if error on requests; |
| 2219 | Returns None for exceptions |
| 2220 | NOTE: |
| 2221 | The ip and port option are for the requests input's ip and port |
| 2222 | of the ONOS node |
| 2223 | """ |
| 2224 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2225 | if debug: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2226 | main.log.debug( self.name + ": Adding group: " + self.pprint( groupJson ) ) |
| 2227 | response = None |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2228 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2229 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2230 | ip = self.ip_address |
| 2231 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2232 | main.log.warn( self.name + ": No port given, reverting to port " + |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2233 | "from topo file" ) |
| 2234 | port = self.port |
| 2235 | url = "/groups/" + deviceId |
| 2236 | response = self.send( method="POST", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2237 | url=url, ip = ip, port = port, |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2238 | data=json.dumps( groupJson ) ) |
| 2239 | if response: |
| 2240 | if "201" in str( response[ 0 ] ): |
| 2241 | main.log.info( self.name + ": Successfully POST group " + |
| 2242 | "in device: " + str( deviceId ) ) |
| 2243 | return main.TRUE |
| 2244 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2245 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 2246 | ( response[ 0 ], response[ 1 ] ) ) |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2247 | return main.FALSE |
| 2248 | except NotImplementedError as e: |
| 2249 | raise e # Inform the caller |
| 2250 | except ( AttributeError, TypeError ): |
| 2251 | main.log.exception( self.name + ": Object not as expected" ) |
| 2252 | return None |
| 2253 | except Exception: |
| 2254 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2255 | main.cleanAndExit() |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2256 | |
| 2257 | def getGroups( self, deviceId=None, appCookie=None, ip="DEFAULT", port="DEFAULT" ): |
| 2258 | """ |
| 2259 | Description: |
| 2260 | Get all the groups or get a specific group by giving the |
| 2261 | deviceId and appCookie |
| 2262 | Optional: |
| 2263 | * deviceId: id of the Device |
| 2264 | * appCookie: Cookie of the Group |
| 2265 | Returns: |
| 2266 | Returns Groups for successful requests; Returns main.FALSE |
| 2267 | if error on requests; |
| 2268 | Returns None for exceptions |
| 2269 | NOTE: |
| 2270 | The ip and port option are for the requests input's ip and port |
| 2271 | of the ONOS node |
| 2272 | """ |
| 2273 | try: |
| 2274 | output = None |
| 2275 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2276 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2277 | ip = self.ip_address |
| 2278 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2279 | main.log.warn( self.name + ": No port given, reverting to port " + |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2280 | "from topo file" ) |
| 2281 | port = self.port |
| 2282 | url = "/groups" |
| 2283 | if deviceId: |
| 2284 | url += "/" + deviceId |
| 2285 | if appCookie: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2286 | url += "/" + appCookie |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2287 | response = self.send( url=url, ip = ip, port = port ) |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2288 | if response: |
| 2289 | if 200 <= response[ 0 ] <= 299: |
| 2290 | output = response[ 1 ] |
| 2291 | groupsJson = json.loads( output ).get( 'groups' ) |
| 2292 | assert groupsJson is not None, "Error parsing json object" |
| 2293 | groups = json.dumps( groupsJson ) |
| 2294 | return groups |
| 2295 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2296 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 2297 | ( response[ 0 ], response[ 1 ] ) ) |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2298 | return main.FALSE |
| 2299 | except ( AttributeError, AssertionError, TypeError ): |
| 2300 | main.log.exception( self.name + ": Object not as expected" ) |
| 2301 | return None |
| 2302 | except Exception: |
| 2303 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2304 | main.cleanAndExit() |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2305 | |
| 2306 | def removeGroup( self, deviceId, appCookie, |
| 2307 | ip="DEFAULT", port="DEFAULT" ): |
| 2308 | """ |
| 2309 | Description: |
| 2310 | Removes specific device group |
| 2311 | Required: |
| 2312 | * deviceId: id of the Device |
| 2313 | * appCookie: Cookie of the Group |
| 2314 | Returns: |
| 2315 | Returns main.TRUE for successful requests; Returns main.FALSE |
| 2316 | if error on requests; |
| 2317 | Returns None for exceptions |
| 2318 | NOTE: |
| 2319 | The ip and port option are for the requests input's ip and port |
| 2320 | of the ONOS node |
| 2321 | |
| 2322 | """ |
| 2323 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2324 | response = None |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2325 | if ip == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2326 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2327 | ip = self.ip_address |
| 2328 | if port == "DEFAULT": |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2329 | main.log.warn( self.name + ": No port given, reverting to port " + |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2330 | "from topo file" ) |
| 2331 | port = self.port |
| 2332 | query = "/" + str( deviceId ) + "/" + str( appCookie ) |
| 2333 | response = self.send( method="DELETE", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2334 | url="/groups" + query, ip = ip, port = port ) |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2335 | if response: |
| 2336 | if 200 <= response[ 0 ] <= 299: |
| 2337 | return main.TRUE |
| 2338 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 2339 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 2340 | ( response[ 0 ], response[ 1 ] ) ) |
kavitha Alagesan | 373e055 | 2016-11-22 05:22:05 +0530 | [diff] [blame] | 2341 | return main.FALSE |
| 2342 | except ( AttributeError, TypeError ): |
| 2343 | main.log.exception( self.name + ": Object not as expected" ) |
| 2344 | return None |
| 2345 | except Exception: |
| 2346 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2347 | main.cleanAndExit() |
Jon Hall | 50a0001 | 2021-03-08 11:06:11 -0800 | [diff] [blame] | 2348 | |
| 2349 | def portstats( self, ip="DEFAULT", port="DEFAULT" ): |
| 2350 | """ |
| 2351 | Description: |
| 2352 | Gets the portstats for each port in ONOS |
| 2353 | Returns: |
| 2354 | A list of dicts containing device id and a list of dicts containing the |
| 2355 | port statistics for each port. |
| 2356 | Returns main.FALSE if error on request; |
| 2357 | Returns None for exception |
| 2358 | """ |
| 2359 | try: |
| 2360 | output = None |
| 2361 | if ip == "DEFAULT": |
| 2362 | main.log.warn( self.name + ": No ip given, reverting to ip from topo file" ) |
| 2363 | ip = self.ip_address |
| 2364 | if port == "DEFAULT": |
| 2365 | main.log.warn( self.name + ": No port given, reverting to port " + |
| 2366 | "from topo file" ) |
| 2367 | port = self.port |
| 2368 | response = self.send( url="/statistics/ports", ip = ip, port = port ) |
| 2369 | if response: |
| 2370 | if 200 <= response[ 0 ] <= 299: |
| 2371 | output = response[ 1 ] |
| 2372 | a = json.loads( output ).get( 'statistics' ) |
| 2373 | assert a is not None, "Error parsing json object" |
| 2374 | b = json.dumps( a ) |
| 2375 | return b |
| 2376 | else: |
| 2377 | main.log.error( "Error with REST request, response was: %s: %s" % |
| 2378 | ( response[ 0 ], response[ 1 ] ) ) |
| 2379 | return main.FALSE |
| 2380 | except ( AttributeError, AssertionError, TypeError ): |
| 2381 | main.log.exception( self.name + ": Object not as expected" ) |
| 2382 | return None |
| 2383 | except Exception: |
| 2384 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2385 | main.cleanAndExit() |