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