Chip Boling | 8e042f6 | 2019-02-12 16:14:34 -0600 | [diff] [blame^] | 1 | # |
| 2 | # Copyright 2018 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | from twisted.internet import reactor |
| 17 | from twisted.internet.defer import inlineCallbacks, failure, returnValue |
| 18 | from voltha.extensions.omci.omci_me import * |
| 19 | from voltha.extensions.omci.tasks.task import Task |
| 20 | from voltha.extensions.omci.omci_defs import * |
| 21 | from voltha.adapters.adtran_onu.flow.flow_entry import FlowEntry |
| 22 | |
| 23 | OP = EntityOperations |
| 24 | RC = ReasonCodes |
| 25 | |
| 26 | |
| 27 | class ServiceInstallFailure(Exception): |
| 28 | """ |
| 29 | This error is raised by default when the flow-install fails |
| 30 | """ |
| 31 | |
| 32 | |
| 33 | class AdtnInstallFlowTask(Task): |
| 34 | """ |
| 35 | OpenOMCI MIB Flow Install Task |
| 36 | |
| 37 | Currently, the only service tech profiles expected by v2.0 will be for AT&T |
| 38 | residential data service and DT residential data service. |
| 39 | """ |
| 40 | task_priority = Task.DEFAULT_PRIORITY + 10 |
| 41 | name = "ADTRAN MIB Install Flow Task" |
| 42 | |
| 43 | def __init__(self, omci_agent, handler, flow_entry): |
| 44 | """ |
| 45 | Class initialization |
| 46 | |
| 47 | :param omci_agent: (OpenOMCIAgent) OMCI Adapter agent |
| 48 | :param handler: (AdtranOnuHandler) ONU Handler |
| 49 | :param flow_entry: (FlowEntry) Flow to install |
| 50 | """ |
| 51 | super(AdtnInstallFlowTask, self).__init__(AdtnInstallFlowTask.name, |
| 52 | omci_agent, |
| 53 | handler.device_id, |
| 54 | priority=AdtnInstallFlowTask.task_priority, |
| 55 | exclusive=False) |
| 56 | self._handler = handler |
| 57 | self._onu_device = omci_agent.get_device(handler.device_id) |
| 58 | self._local_deferred = None |
| 59 | self._flow_entry = flow_entry |
| 60 | self._install_by_delete = True |
| 61 | |
| 62 | # TODO: Cleanup below that is not needed |
| 63 | is_upstream = flow_entry.flow_direction in FlowEntry.upstream_flow_types |
| 64 | uni_port = flow_entry.in_port if is_upstream else flow_entry.out_port |
| 65 | pon_port = flow_entry.out_port if is_upstream else flow_entry.in_port |
| 66 | |
| 67 | self._uni = handler.uni_port(uni_port) |
| 68 | self._pon = handler.pon_port(pon_port) |
| 69 | |
| 70 | # Entity IDs. IDs with values can probably be most anything for most ONUs, |
| 71 | # IDs set to None are discovered/set |
| 72 | # |
| 73 | # TODO: Probably need to store many of these in the appropriate object (UNI, PON,...) |
| 74 | # |
| 75 | self._ethernet_uni_entity_id = self._handler.uni_ports[0].entity_id |
| 76 | self._ieee_mapper_service_profile_entity_id = self._pon.ieee_mapper_service_profile_entity_id |
| 77 | |
| 78 | # Next to are specific |
| 79 | self._mac_bridge_service_profile_entity_id = handler.mac_bridge_service_profile_entity_id |
| 80 | |
| 81 | def cancel_deferred(self): |
| 82 | super(AdtnInstallFlowTask, self).cancel_deferred() |
| 83 | |
| 84 | d, self._local_deferred = self._local_deferred, None |
| 85 | try: |
| 86 | if d is not None and not d.called: |
| 87 | d.cancel() |
| 88 | except: |
| 89 | pass |
| 90 | |
| 91 | def start(self): |
| 92 | """ |
| 93 | Start the flow installation |
| 94 | """ |
| 95 | super(AdtnInstallFlowTask, self).start() |
| 96 | self._local_deferred = reactor.callLater(0, self.perform_flow_install) |
| 97 | |
| 98 | def stop(self): |
| 99 | """ |
| 100 | Shutdown flow install task |
| 101 | """ |
| 102 | self.log.debug('stopping') |
| 103 | |
| 104 | self.cancel_deferred() |
| 105 | super(AdtnInstallFlowTask, self).stop() |
| 106 | |
| 107 | def check_status_and_state(self, results, operation=''): |
| 108 | """ |
| 109 | Check the results of an OMCI response. An exception is thrown |
| 110 | if the task was cancelled or an error was detected. |
| 111 | |
| 112 | :param results: (OmciFrame) OMCI Response frame |
| 113 | :param operation: (str) what operation was being performed |
| 114 | :return: True if successful, False if the entity existed (already created) |
| 115 | """ |
| 116 | omci_msg = results.fields['omci_message'].fields |
| 117 | status = omci_msg['success_code'] |
| 118 | error_mask = omci_msg.get('parameter_error_attributes_mask', 'n/a') |
| 119 | failed_mask = omci_msg.get('failed_attributes_mask', 'n/a') |
| 120 | unsupported_mask = omci_msg.get('unsupported_attributes_mask', 'n/a') |
| 121 | |
| 122 | self.log.debug(operation, status=status, error_mask=error_mask, |
| 123 | failed_mask=failed_mask, unsupported_mask=unsupported_mask) |
| 124 | |
| 125 | if status == RC.Success: |
| 126 | self.strobe_watchdog() |
| 127 | return True |
| 128 | |
| 129 | elif status == RC.InstanceExists: |
| 130 | return False |
| 131 | |
| 132 | elif status == RC.UnknownInstance and operation == 'delete': |
| 133 | return True |
| 134 | |
| 135 | raise ServiceInstallFailure('{} failed with a status of {}, error_mask: {}, failed_mask: {}, unsupported_mask: {}' |
| 136 | .format(operation, status, error_mask, failed_mask, unsupported_mask)) |
| 137 | |
| 138 | @inlineCallbacks |
| 139 | def perform_flow_install(self): |
| 140 | """ |
| 141 | Send the commands to configure the flow. |
| 142 | |
| 143 | Currently this task uses the pre-installed default TCONT and GEM Port. This will |
| 144 | change when Technology Profiles are supported. |
| 145 | """ |
| 146 | self.log.info('perform-flow-install', vlan_vid=self._flow_entry.vlan_vid) |
| 147 | |
| 148 | if self._flow_entry.vlan_vid == 0: |
| 149 | return |
| 150 | |
| 151 | def resources_available(): |
| 152 | # TODO: Rework for non-xpon mode |
| 153 | return (len(self._handler.uni_ports) > 0 and |
| 154 | len(self._pon.tconts) and |
| 155 | len(self._pon.gem_ports)) |
| 156 | |
| 157 | if self._handler.enabled and resources_available(): |
| 158 | |
| 159 | omci = self._onu_device.omci_cc |
| 160 | brg_id = self._mac_bridge_service_profile_entity_id |
| 161 | vlan_vid = self._flow_entry.vlan_vid |
| 162 | |
| 163 | if self._install_by_delete: |
| 164 | # Delete any existing flow before adding this new one |
| 165 | |
| 166 | msg = ExtendedVlanTaggingOperationConfigurationDataFrame(brg_id, attributes=None) |
| 167 | frame = msg.delete() |
| 168 | |
| 169 | try: |
| 170 | results = yield omci.send(frame) |
| 171 | self.check_status_and_state(results, operation='delete') |
| 172 | |
| 173 | attributes = dict( |
| 174 | association_type=2, # Assoc Type, PPTP Ethernet UNI |
| 175 | associated_me_pointer=self._ethernet_uni_entity_id # Assoc ME, PPTP Entity Id |
| 176 | ) |
| 177 | |
| 178 | frame = ExtendedVlanTaggingOperationConfigurationDataFrame( |
| 179 | self._mac_bridge_service_profile_entity_id + self._uni.mac_bridge_port_num, |
| 180 | attributes=attributes |
| 181 | ).create() |
| 182 | results = yield omci.send(frame) |
| 183 | self.check_status_and_state(results, 'flow-recreate-before-set') |
| 184 | |
| 185 | # TODO: Any of the following needed as well |
| 186 | |
| 187 | # # Delete bridge ani side vlan filter |
| 188 | # msg = VlanTaggingFilterDataFrame(self._mac_bridge_port_ani_entity_id) |
| 189 | # frame = msg.delete() |
| 190 | # |
| 191 | # results = yield omci.send(frame) |
| 192 | # self.check_status_and_state(results, 'flow-delete-vlan-tagging-filter-data') |
| 193 | # |
| 194 | # # Re-Create bridge ani side vlan filter |
| 195 | # msg = VlanTaggingFilterDataFrame( |
| 196 | # self._mac_bridge_port_ani_entity_id, # Entity ID |
| 197 | # vlan_tcis=[vlan_vid], # VLAN IDs |
| 198 | # forward_operation=0x10 |
| 199 | # ) |
| 200 | # frame = msg.create() |
| 201 | # |
| 202 | # results = yield omci.send(frame) |
| 203 | # self.check_status_and_state(results, 'flow-create-vlan-tagging-filter-data') |
| 204 | |
| 205 | except Exception as e: |
| 206 | self.log.exception('flow-delete-before-install-failure', e=e) |
| 207 | self.deferred.errback(failure.Failure(e)) |
| 208 | returnValue(None) |
| 209 | |
| 210 | try: |
| 211 | # Now set the VLAN Tagging Operation up as we want it |
| 212 | # Update uni side extended vlan filter |
| 213 | # filter for untagged |
| 214 | # probably for eapol |
| 215 | # TODO: lots of magic |
| 216 | # attributes = dict( |
| 217 | # # This table filters and tags upstream frames |
| 218 | # received_frame_vlan_tagging_operation_table= |
| 219 | # VlanTaggingOperation( |
| 220 | # filter_outer_priority=15, # This entry is not a double-tag rule (ignore out tag rules) |
| 221 | # filter_outer_vid=4096, # Do not filter on the outer VID value |
| 222 | # filter_outer_tpid_de=0, # Do not filter on the outer TPID field |
| 223 | # |
| 224 | # filter_inner_priority=15, # This is a no-tag rule, ignore all other VLAN tag filter fields |
| 225 | # filter_inner_vid=4096, # Do not filter on the inner VID |
| 226 | # filter_inner_tpid_de=0, # Do not filter on inner TPID field |
| 227 | # filter_ether_type=0, # Do not filter on EtherType |
| 228 | # |
| 229 | # treatment_tags_to_remove=0, # Remove 0 tags |
| 230 | # |
| 231 | # treatment_outer_priority=15, # Do not add an outer tag |
| 232 | # treatment_outer_vid=0, # n/a |
| 233 | # treatment_outer_tpid_de=0, # n/a |
| 234 | # |
| 235 | # treatment_inner_priority=0, # Add an inner tag and insert this value as the priority |
| 236 | # treatment_inner_vid=vlan_vid, # Push this tag onto the frame |
| 237 | # treatment_inner_tpid_de=4 # set TPID |
| 238 | # ) |
| 239 | # ) |
| 240 | # msg = ExtendedVlanTaggingOperationConfigurationDataFrame( |
| 241 | # self._mac_bridge_service_profile_entity_id + self._uni.mac_bridge_port_num, # Bridge Entity ID |
| 242 | # attributes=attributes # See above |
| 243 | # ) |
| 244 | # frame = msg.set() |
| 245 | # |
| 246 | # results = yield omci.send(frame) |
| 247 | # self.check_status_and_state(results, |
| 248 | # 'flow-set-ext-vlan-tagging-op-config-data-untagged') |
| 249 | |
| 250 | # Update uni side extended vlan filter |
| 251 | # filter for vlan 0 |
| 252 | # TODO: lots of magic |
| 253 | |
| 254 | ################################################################################ |
| 255 | # Update Extended VLAN Tagging Operation Config Data |
| 256 | # |
| 257 | # Specifies the TPIDs in use and that operations in the downstream direction are |
| 258 | # inverse to the operations in the upstream direction |
| 259 | # TODO: Downstream mode may need to be modified once we work more on the flow rules |
| 260 | |
| 261 | attributes = dict( |
| 262 | input_tpid=0x8100, # input TPID |
| 263 | output_tpid=0x8100, # output TPID |
| 264 | downstream_mode=0, # inverse of upstream |
| 265 | ) |
| 266 | |
| 267 | msg = ExtendedVlanTaggingOperationConfigurationDataFrame( |
| 268 | self._mac_bridge_service_profile_entity_id + |
| 269 | self._uni.mac_bridge_port_num, # Bridge Entity ID |
| 270 | attributes=attributes # See above |
| 271 | ) |
| 272 | frame = msg.set() |
| 273 | |
| 274 | results = yield omci.send(frame) |
| 275 | self.check_status_and_state(results, 'set-extended-vlan-tagging-operation-configuration-data') |
| 276 | |
| 277 | attributes = dict( |
| 278 | received_frame_vlan_tagging_operation_table= |
| 279 | VlanTaggingOperation( |
| 280 | filter_outer_priority=15, # This entry is not a double-tag rule |
| 281 | filter_outer_vid=4096, # Do not filter on the outer VID value |
| 282 | filter_outer_tpid_de=0, # Do not filter on the outer TPID field |
| 283 | |
| 284 | filter_inner_priority=15, # This is a no-tag rule, ignore all other VLAN tag filter fields |
| 285 | filter_inner_vid=0x1000, # Do not filter on the inner VID |
| 286 | filter_inner_tpid_de=0, # Do not filter on inner TPID field |
| 287 | |
| 288 | filter_ether_type=0, # Do not filter on EtherType |
| 289 | treatment_tags_to_remove=0, # Remove 0 tags |
| 290 | |
| 291 | treatment_outer_priority=15, # Do not add an outer tag |
| 292 | treatment_outer_vid=0, # n/a |
| 293 | treatment_outer_tpid_de=0, # n/a |
| 294 | |
| 295 | treatment_inner_priority=0, # Add an inner tag and insert this value as the priority |
| 296 | treatment_inner_vid=vlan_vid, # use this value as the VID in the inner VLAN tag |
| 297 | treatment_inner_tpid_de=4, # set TPID |
| 298 | ) |
| 299 | ) |
| 300 | |
| 301 | msg = ExtendedVlanTaggingOperationConfigurationDataFrame( |
| 302 | self._mac_bridge_service_profile_entity_id + |
| 303 | self._uni.mac_bridge_port_num, # Bridge Entity ID |
| 304 | attributes=attributes # See above |
| 305 | ) |
| 306 | frame = msg.set() |
| 307 | |
| 308 | results = yield omci.send(frame) |
| 309 | self.check_status_and_state(results, |
| 310 | 'flow-set-ext-vlan-tagging-op-config-data-untagged') |
| 311 | self.deferred.callback('flow-install-success') |
| 312 | |
| 313 | except Exception as e: |
| 314 | # TODO: Better context info for this exception output... |
| 315 | self.log.exception('failed-to-install-flow', e=e) |
| 316 | self.deferred.errback(failure.Failure(e)) |
| 317 | |
| 318 | else: |
| 319 | # TODO: Provide better error reason, what was missing... |
| 320 | e = ServiceInstallFailure('Required resources are not available') |
| 321 | self.deferred.errback(failure.Failure(e)) |