Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 14 | |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 15 | import xmltodict |
| 16 | import re |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 17 | from enum import Enum |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 18 | from twisted.internet import reactor |
| 19 | from twisted.internet.defer import inlineCallbacks, returnValue, succeed |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 20 | from voltha.core.flow_decomposer import * |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 21 | |
| 22 | log = structlog.get_logger() |
| 23 | |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 24 | EVC_NAME_FORMAT = 'VOLTHA-{}' # format(flow.id) |
| 25 | EVC_NAME_REGEX_ALL = EVC_NAME_FORMAT.format('*') |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 26 | DEFAULT_STPID = 0x8100 |
| 27 | |
| 28 | |
| 29 | class EVC(object): |
| 30 | """ |
| 31 | Class to wrap EVC functionality |
| 32 | """ |
| 33 | class SwitchingMethod(Enum): |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 34 | SINGLE_TAGGED = 1 |
| 35 | DOUBLE_TAGGED = 2 |
| 36 | MAC_SWITCHED = 3 |
| 37 | DOUBLE_TAGGED_MAC_SWITCHED = 4 |
| 38 | DEFAULT = SINGLE_TAGGED |
| 39 | |
| 40 | @staticmethod |
| 41 | def xml(value): |
| 42 | if value is None: |
| 43 | value = EVC.SwitchingMethod.DEFAULT |
| 44 | if value == EVC.SwitchingMethod.SINGLE_TAGGED: |
| 45 | return '<single-tag-switched/>' |
| 46 | elif value == EVC.SwitchingMethod.DOUBLE_TAGGED: |
| 47 | return '<double-tag-switched/>' |
| 48 | elif value == EVC.SwitchingMethod.MAC_SWITCHED: |
| 49 | return '<mac-switched/>' |
| 50 | elif value == EVC.SwitchingMethod.DOUBLE_TAGGED_MAC_SWITCHED: |
| 51 | return '<double-tag-mac-switched/>' |
| 52 | raise ValueError('Invalid SwitchingMethod enumeration') |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 53 | |
| 54 | class Men2UniManipulation(Enum): |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 55 | SYMMETRIC = 1 |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 56 | POP_OUT_TAG_ONLY = 2 |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 57 | DEFAULT = SYMMETRIC |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 58 | |
| 59 | @staticmethod |
| 60 | def xml(value): |
| 61 | if value is None: |
| 62 | value = EVC.Men2UniManipulation.DEFAULT |
| 63 | fmt = '<men-to-uni-tag-manipulation>{}</men-to-uni-tag-manipulation>' |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 64 | if value == EVC.Men2UniManipulation.SYMMETRIC: |
| 65 | return fmt.format('<symmetric/>') |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 66 | elif value == EVC.Men2UniManipulation.POP_OUT_TAG_ONLY: |
| 67 | return fmt.format('<pop-outer-tag-only/>') |
| 68 | raise ValueError('Invalid Men2UniManipulation enumeration') |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 69 | |
| 70 | class ElineFlowType(Enum): |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 71 | NNI_TO_UNI = 1 |
| 72 | UNI_TO_NNI = 2 |
| 73 | NNI_TO_NNI = 3 |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 74 | UNI_TO_UNI = 4 |
| 75 | ACL_FILTER = 5 |
| 76 | UNKNOWN = 6 |
| 77 | UNSUPPORTED = 7 # Or Invalid |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 78 | |
| 79 | def __init__(self, flow_entry): |
| 80 | self._installed = False |
| 81 | self._status_message = None |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 82 | self._flow = flow_entry |
| 83 | self._name = self._create_name() |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 84 | self._evc_maps = {} # Map Name -> evc-map |
| 85 | self._install_deferred = None |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 86 | |
| 87 | self._flow_type = EVC.ElineFlowType.UNKNOWN |
| 88 | |
| 89 | # EVC related properties |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 90 | self._enabled = True |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 91 | self._men_ports = [] |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 92 | self._s_tag = None |
| 93 | self._stpid = None |
| 94 | self._switching_method = None |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 95 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 96 | self._ce_vlan_preservation = None |
| 97 | self._men_to_uni_tag_manipulation = None |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 98 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 99 | try: |
| 100 | self._valid = self._decode() |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 101 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 102 | except Exception as e: |
| 103 | log.exception('Failure during EVC decode', e=e) |
| 104 | self._valid = False |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 105 | |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 106 | def __str__(self): |
| 107 | return "EVC-{}: MEN: {}, S-Tag: {}".format(self._name, self._men_ports, self._s_tag) |
| 108 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 109 | def _create_name(self): |
| 110 | # |
| 111 | # TODO: Take into account selection criteria and output to make the name |
| 112 | # |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 113 | return EVC_NAME_FORMAT.format(self._flow.flow_id) |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 114 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 115 | @property |
| 116 | def name(self): |
| 117 | return self._name |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 118 | |
| 119 | @property |
| 120 | def valid(self): |
| 121 | return self._valid |
| 122 | |
| 123 | @property |
| 124 | def installed(self): |
| 125 | return self._installed |
| 126 | |
| 127 | @property |
| 128 | def status(self): |
| 129 | return self._status_message |
| 130 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 131 | @status.setter |
| 132 | def status(self, value): |
| 133 | self._status_message = value |
| 134 | |
| 135 | @property |
| 136 | def s_tag(self): |
| 137 | return self._s_tag |
| 138 | |
| 139 | @property |
| 140 | def stpid(self): |
| 141 | return self._stpid |
| 142 | |
| 143 | @stpid.setter |
| 144 | def stpid(self, value): |
| 145 | assert self._stpid is None or self._stpid == value |
| 146 | self._stpid = value |
| 147 | |
| 148 | @property |
| 149 | def switching_method(self): |
| 150 | return self._switching_method |
| 151 | |
| 152 | @switching_method.setter |
| 153 | def switching_method(self, value): |
| 154 | assert self._switching_method is None or self._switching_method == value |
| 155 | self._switching_method = value |
| 156 | |
| 157 | @property |
| 158 | def ce_vlan_preservation(self): |
| 159 | return self._ce_vlan_preservation |
| 160 | |
| 161 | @ce_vlan_preservation.setter |
| 162 | def ce_vlan_preservation(self, value): |
| 163 | assert self._ce_vlan_preservation is None or self._ce_vlan_preservation == value |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 164 | self._ce_vlan_preservation = value |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 165 | |
| 166 | @property |
| 167 | def men_to_uni_tag_manipulation(self): |
| 168 | return self._men_to_uni_tag_manipulation |
| 169 | |
| 170 | @men_to_uni_tag_manipulation.setter |
| 171 | def men_to_uni_tag_manipulation(self, value): |
| 172 | assert self._men_to_uni_tag_manipulation is None or self._men_to_uni_tag_manipulation == value |
| 173 | self._men_to_uni_tag_manipulation = value |
| 174 | |
| 175 | @property |
| 176 | def flow_entry(self): |
| 177 | return self._flow |
| 178 | |
| 179 | @property |
| 180 | def evc_maps(self): |
| 181 | """ |
| 182 | Get all EVC Maps that reference this EVC |
| 183 | :return: list of EVCMap |
| 184 | """ |
| 185 | return self._evc_maps.values() |
| 186 | |
| 187 | def add_evc_map(self, evc_map): |
| 188 | if self._evc_maps is not None: |
| 189 | self._evc_maps[evc_map.name] = evc_map |
| 190 | |
| 191 | def remove_evc_map(self, evc_map): |
| 192 | if self._evc_maps is not None and evc_map.name in self._evc_maps: |
| 193 | del self._evc_maps[evc_map.name] |
| 194 | |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 195 | def schedule_install(self): |
| 196 | """ |
| 197 | Try to install EVC and all MAPs in a single operational sequence |
| 198 | """ |
| 199 | if self._valid and self._install_deferred is None: |
| 200 | self._install_deferred = reactor.callLater(0, self._do_install) |
| 201 | |
| 202 | return self._install_deferred |
| 203 | |
| 204 | @staticmethod |
| 205 | def _xml_header(operation=None): |
| 206 | return '<evcs xmlns="http://www.adtran.com/ns/yang/adtran-evcs"><evc{}>'.\ |
| 207 | format('' if operation is None else ' operation="{}"'.format(operation)) |
| 208 | |
| 209 | @staticmethod |
| 210 | def _xml_trailer(): |
| 211 | return '</evc></evcs>' |
| 212 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 213 | @inlineCallbacks |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 214 | def _do_install(self): |
| 215 | self._install_deferred = None |
| 216 | |
| 217 | # Install the EVC if needed |
| 218 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 219 | if self._valid and not self._installed: |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 220 | # TODO: Currently install EVC and then MAPs. Can do it all in a single edit-config operation |
| 221 | |
| 222 | xml = EVC._xml_header() |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 223 | xml += '<name>{}</name>'.format(self.name) |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 224 | xml += '<enabled>{}</enabled>'.format('true' if self._enabled else 'false') |
| 225 | |
| 226 | if self._ce_vlan_preservation is not None: |
| 227 | xml += '<ce-vlan-preservation>{}</ce-vlan-preservation>'.\ |
| 228 | format('true' if self._ce_vlan_preservation else 'false') |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 229 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 230 | if self._s_tag is not None: |
| 231 | xml += '<stag>{}</stag>'.format(self._s_tag) |
| 232 | xml += '<stag-tpid>{:#x}</stag-tpid>'.format(self._stpid or DEFAULT_STPID) |
| 233 | else: |
| 234 | xml += 'no-stag/' |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 235 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 236 | for port in self._men_ports: |
| 237 | xml += '<men-ports>{}</men-ports>'.format(port) |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 238 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 239 | xml += EVC.Men2UniManipulation.xml(self._men_to_uni_tag_manipulation) |
| 240 | xml += EVC.SwitchingMethod.xml(self._switching_method) |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 241 | xml += EVC._xml_trailer() |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 242 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 243 | log.debug("Creating EVC {}: '{}'".format(self.name, xml)) |
| 244 | |
| 245 | try: |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 246 | # Set installed to true while request is in progress |
| 247 | self._installed = True |
| 248 | results = yield self._flow.handler.netconf_client.edit_config(xml, lock_timeout=30) |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 249 | self._installed = results.ok |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 250 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 251 | if results.ok: |
| 252 | self.status = '' |
| 253 | else: |
| 254 | self.status = results.error # TODO: Save off error status |
| 255 | |
| 256 | except Exception as e: |
| 257 | log.exception('Failed to install EVC', name=self.name, e=e) |
| 258 | raise |
| 259 | |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 260 | # Install any associated EVC Maps |
| 261 | |
| 262 | if self._installed: |
| 263 | for evc_map in self.evc_maps: |
| 264 | try: |
| 265 | results = yield evc_map.install() |
| 266 | pass # TODO: What to do on error? |
| 267 | |
| 268 | except Exception as e: |
| 269 | evc_map.status = 'Exception during EVC-MAP Install: {}'.format(e.message) |
| 270 | log.exception(evc_map.status, e=e) |
| 271 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 272 | returnValue(self._installed and self._valid) |
| 273 | |
| 274 | @inlineCallbacks |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 275 | def remove(self): |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 276 | d, self._install_deferred = self._install_deferred, None |
| 277 | if d is not None: |
| 278 | d.cancel() |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 279 | |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 280 | if self._installed: |
| 281 | xml = EVC._xml_header('delete') + '<name>{}</name>'.format(self.name) + EVC._xml_trailer() |
| 282 | |
| 283 | log.debug('removing', evc=self.name, xml=xml) |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 284 | |
| 285 | try: |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 286 | results = yield self._flow.handler.netconf_client.edit_config(xml, lock_timeout=30) |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 287 | self._installed = not results.ok |
| 288 | if results.ok: |
| 289 | self.status = '' |
| 290 | else: |
| 291 | self.status = results.error # TODO: Save off error status |
| 292 | |
| 293 | except Exception as e: |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 294 | log.exception('removing', name=self.name, e=e) |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 295 | raise |
| 296 | |
| 297 | # TODO: Do we remove evc-maps as well reference here or maybe have a 'delete' function? |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 298 | pass |
| 299 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 300 | returnValue(not self._installed) |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 301 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 302 | @inlineCallbacks |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 303 | def enable(self): |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 304 | if self.installed and not self._enabled: |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 305 | xml = EVC._xml_header() + '<name>{}</name>'.format(self.name) |
| 306 | xml += '<enabled>true</enabled>' + EVC._xml_trailer() |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 307 | |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 308 | log.debug('enabling', evc=self.name, xml=xml) |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 309 | |
| 310 | try: |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 311 | results = yield self._flow.handler.netconf_client.edit_config(xml, lock_timeout=30) |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 312 | self._enabled = results.ok |
| 313 | if results.ok: |
| 314 | self.status = '' |
| 315 | else: |
| 316 | self.status = results.error # TODO: Save off error status |
| 317 | |
| 318 | except Exception as e: |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 319 | log.exception('enabling', name=self.name, e=e) |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 320 | raise |
| 321 | |
| 322 | returnValue(self.installed and self._enabled) |
| 323 | |
| 324 | @inlineCallbacks |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 325 | def disable(self): |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 326 | if self.installed and self._enabled: |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 327 | xml = EVC._xml_header() + '<name>{}</name>'.format(self.name) |
| 328 | xml += '<enabled>false</enabled>' + EVC._xml_trailer() |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 329 | |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 330 | log.debug('disabling', evc=self.name, xml=xml) |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 331 | |
| 332 | try: |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 333 | results = yield self._flow.handler.netconf_client.edit_config(xml, lock_timeout=30) |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 334 | self._enabled = not results.ok |
| 335 | if results.ok: |
| 336 | self.status = '' |
| 337 | else: |
| 338 | self.status = results.error # TODO: Save off error status |
| 339 | |
| 340 | except Exception as e: |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 341 | log.exception('disabling', name=self.name, e=e) |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 342 | raise |
| 343 | |
| 344 | returnValue(self.installed and not self._enabled) |
| 345 | |
| 346 | @inlineCallbacks |
| 347 | def delete(self): |
| 348 | """ |
| 349 | Remove from hardware and delete/clean-up |
| 350 | """ |
| 351 | try: |
| 352 | self._valid = False |
| 353 | succeeded = yield self.remove() |
| 354 | # TODO: On timeout or other NETCONF error, should we schedule cleanup later? |
| 355 | |
| 356 | except Exception: |
| 357 | succeeded = False |
| 358 | |
| 359 | finally: |
| 360 | self._flow = None |
| 361 | self._evc_maps = None |
| 362 | |
| 363 | returnValue(succeeded) |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 364 | |
| 365 | def _decode(self): |
| 366 | """ |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 367 | Examine flow rules and extract appropriate settings for this EVC |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 368 | """ |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 369 | if self._flow.handler.is_nni_port(self._flow.in_port): |
| 370 | self._men_ports.append(self._flow.handler.get_port_name(self._flow.in_port)) |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 371 | else: |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 372 | self._status_message = 'EVCs with UNI ports are not supported' |
| 373 | return False # UNI Ports handled in the EVC Maps |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 374 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 375 | self._s_tag = self._flow.vlan_id |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 376 | |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 377 | # if self._flow.inner_vid is not None: |
| 378 | # self._switching_method = EVC.SwitchingMethod.DOUBLE_TAGGED TODO: Future support |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 379 | |
Chip Boling | 5561d55 | 2017-07-07 15:11:26 -0500 | [diff] [blame] | 380 | # Note: The following fields will get set when the first EVC-MAP |
| 381 | # is associated with this object. Once set, they cannot be changed to |
| 382 | # another value. |
| 383 | # self._stpid |
| 384 | # self._switching_method |
| 385 | # self._ce_vlan_preservation |
| 386 | # self._men_to_uni_tag_manipulation |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 387 | return True |
| 388 | |
| 389 | # BULK operations |
| 390 | |
| 391 | @staticmethod |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 392 | def remove_all(client, regex_=EVC_NAME_REGEX_ALL): |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 393 | """ |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 394 | Remove all matching EVCs from hardware |
| 395 | :param client: (ncclient) NETCONF Client to use |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 396 | :param regex_: (String) Regular expression for name matching |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 397 | :return: (deferred) |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 398 | """ |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 399 | # Do a 'get' on the evc config an you should get the names |
| 400 | get_xml = """ |
| 401 | <filter xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 402 | <evcs xmlns="http://www.adtran.com/ns/yang/adtran-evcs"> |
| 403 | <evc><name/></evc> |
| 404 | </evcs> |
| 405 | </filter> |
| 406 | """ |
| 407 | log.debug('query', xml=get_xml) |
Chip Boling | 7294b25 | 2017-06-15 16:16:55 -0500 | [diff] [blame] | 408 | |
Chip Boling | 252c777 | 2017-08-16 10:13:17 -0500 | [diff] [blame] | 409 | def request_failed(results, operation): |
| 410 | log.error('{}-failed'.format(operation), results=results) |
| 411 | # No further actions. Periodic poll later on will scrub any old EVCs if needed |
| 412 | |
| 413 | def delete_complete(results): |
| 414 | log.debug('delete-complete', results=results) |
| 415 | |
| 416 | def do_delete(rpc_reply, regexpr): |
| 417 | log.debug('query-complete', rpc_reply=rpc_reply) |
| 418 | |
| 419 | if rpc_reply.ok: |
| 420 | result_dict = xmltodict.parse(rpc_reply.data_xml) |
| 421 | entries = result_dict['data']['evcs'] if 'evcs' in result_dict['data'] else {} |
| 422 | |
| 423 | if 'evc' in entries: |
| 424 | p = re.compile(regexpr) |
| 425 | |
| 426 | if isinstance(entries['evc'], list): |
| 427 | names = {entry['name'] for entry in entries['evc'] if 'name' in entry |
| 428 | and p.match(entry['name'])} |
| 429 | else: |
| 430 | names = set() |
| 431 | for item in entries['evc-map'].items(): |
| 432 | if isinstance(item, tuple) and item[0] == 'name': |
| 433 | names.add(item[1]) |
| 434 | break |
| 435 | |
| 436 | if len(names) > 0: |
| 437 | del_xml = EVC._xml_header('delete') |
| 438 | for name in names: |
| 439 | del_xml += '<name>{}</name>'.format(name) |
| 440 | del_xml += EVC._xml_trailer() |
| 441 | |
| 442 | log.debug('removing', xml=del_xml) |
| 443 | return client.edit_config(del_xml, lock_timeout=30) |
| 444 | |
| 445 | return succeed('no entries') |
| 446 | |
| 447 | d = client.get(get_xml) |
| 448 | d.addCallbacks(do_delete, request_failed, callbackArgs=[regex_], errbackArgs=['get']) |
| 449 | d.addCallbacks(delete_complete, request_failed, errbackArgs=['edit-config']) |
| 450 | return d |