Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [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 | |
Matt Jeanneret | 2e3cb8d | 2019-11-16 09:22:41 -0500 | [diff] [blame] | 16 | from __future__ import absolute_import |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 17 | import structlog |
| 18 | from twisted.internet.defer import inlineCallbacks, returnValue |
William Kurkian | 8235c1e | 2019-03-05 12:58:28 -0500 | [diff] [blame] | 19 | from voltha_protos.common_pb2 import AdminState, OperStatus |
| 20 | from voltha_protos.device_pb2 import Port |
Matt Jeanneret | 72f96fc | 2019-02-11 10:53:05 -0500 | [diff] [blame] | 21 | from pyvoltha.adapters.extensions.omci.tasks.task import Task |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 22 | |
| 23 | BRDCM_DEFAULT_VLAN = 4091 |
| 24 | TASK_PRIORITY = Task.DEFAULT_PRIORITY + 10 |
| 25 | DEFAULT_TPID = 0x8100 |
| 26 | DEFAULT_GEM_PAYLOAD = 48 |
| 27 | |
| 28 | |
| 29 | class PonPort(object): |
| 30 | """Wraps northbound-port/ANI support for ONU""" |
| 31 | # TODO: possibly get from olt |
| 32 | MIN_GEM_ENTITY_ID = 0x408 |
| 33 | MAX_GEM_ENTITY_ID = 0x4FF # TODO: This limits is internal to specific ONU. It should be more "discoverable"? |
| 34 | |
| 35 | def __init__(self, handler, port_no): |
| 36 | self.log = structlog.get_logger(device_id=handler.device_id, port_no=port_no) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 37 | |
| 38 | self._enabled = False |
| 39 | self._valid = True |
| 40 | self._handler = handler |
| 41 | self._deferred = None |
| 42 | self._port = None |
| 43 | self._port_number = port_no |
Matt Jeanneret | 0c28789 | 2019-02-28 11:48:00 -0500 | [diff] [blame] | 44 | self._peers = [] |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 45 | self._next_entity_id = PonPort.MIN_GEM_ENTITY_ID |
| 46 | |
| 47 | self._admin_state = AdminState.ENABLED |
| 48 | self._oper_status = OperStatus.ACTIVE |
| 49 | |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 50 | self._gem_ports = {} # gem-id -> GemPort |
| 51 | self._tconts = {} # alloc-id -> TCont |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 52 | |
| 53 | self.ieee_mapper_service_profile_entity_id = 0x8001 |
| 54 | self.mac_bridge_port_ani_entity_id = 0x2102 # TODO: can we just use the entity id from the anis list? |
| 55 | |
| 56 | def __str__(self): |
| 57 | return "PonPort - port_number: {}, next_entity_id: {}, num_gem_ports: {}, num_tconts: {}".format( |
| 58 | self._port_number, self._next_entity_id, len(self._gem_ports), len(self._tconts)) |
| 59 | |
| 60 | def __repr__(self): |
| 61 | return str(self) |
| 62 | |
| 63 | @staticmethod |
| 64 | def create(handler, port_no): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 65 | port = PonPort(handler, port_no) |
| 66 | |
| 67 | return port |
| 68 | |
| 69 | def _start(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 70 | self._cancel_deferred() |
| 71 | |
| 72 | self._admin_state = AdminState.ENABLED |
| 73 | self._oper_status = OperStatus.ACTIVE |
| 74 | self._update_adapter_agent() |
| 75 | |
| 76 | def _stop(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 77 | self._cancel_deferred() |
| 78 | |
| 79 | self._admin_state = AdminState.DISABLED |
| 80 | self._oper_status = OperStatus.UNKNOWN |
| 81 | self._update_adapter_agent() |
| 82 | |
| 83 | # TODO: stop h/w sync |
| 84 | |
| 85 | def _cancel_deferred(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 86 | d1, self._deferred = self._deferred, None |
| 87 | |
| 88 | for d in [d1]: |
| 89 | try: |
| 90 | if d is not None and not d.called: |
| 91 | d.cancel() |
| 92 | except: |
| 93 | pass |
| 94 | |
| 95 | def delete(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 96 | self.enabled = False |
| 97 | self._valid = False |
| 98 | self._handler = None |
| 99 | |
| 100 | @property |
| 101 | def enabled(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 102 | return self._enabled |
| 103 | |
| 104 | @enabled.setter |
| 105 | def enabled(self, value): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 106 | if self._enabled != value: |
| 107 | self._enabled = value |
| 108 | |
| 109 | if value: |
| 110 | self._start() |
| 111 | else: |
| 112 | self._stop() |
| 113 | |
| 114 | @property |
| 115 | def port_number(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 116 | return self._port_number |
| 117 | |
| 118 | @property |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 119 | def tconts(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 120 | return self._tconts |
| 121 | |
| 122 | @property |
| 123 | def gem_ports(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 124 | return self._gem_ports |
| 125 | |
| 126 | def get_port(self): |
| 127 | """ |
| 128 | Get the VOLTHA PORT object for this port |
| 129 | :return: VOLTHA Port object |
| 130 | """ |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 131 | |
Matt Jeanneret | 0c28789 | 2019-02-28 11:48:00 -0500 | [diff] [blame] | 132 | self._port = Port(port_no=self.port_number, |
| 133 | label='PON port', |
| 134 | type=Port.PON_ONU, |
| 135 | admin_state=self._admin_state, |
| 136 | oper_status=self._oper_status, |
| 137 | peers=self._peers) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 138 | return self._port |
| 139 | |
Matt Jeanneret | 0c28789 | 2019-02-28 11:48:00 -0500 | [diff] [blame] | 140 | def add_peer(self, parent_device_id, parent_port_no): |
| 141 | self.log.debug('add-peer-port', parent_device_id=parent_device_id, parent_port_no=parent_port_no) |
| 142 | new_peer = Port.PeerPort(device_id=parent_device_id, port_no=parent_port_no) |
| 143 | self._peers.extend([new_peer]) |
| 144 | |
Matt Jeanneret | 84e56f6 | 2019-02-26 10:48:09 -0500 | [diff] [blame] | 145 | @inlineCallbacks |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 146 | def _update_adapter_agent(self): |
| 147 | """ |
| 148 | Update the port status and state in the core |
| 149 | """ |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 150 | self.log.debug('update-adapter-agent', admin_state=self._admin_state, |
| 151 | oper_status=self._oper_status) |
| 152 | |
| 153 | if self._port is not None: |
| 154 | self._port.admin_state = self._admin_state |
| 155 | self._port.oper_status = self._oper_status |
| 156 | |
| 157 | # adapter_agent add_port also does an update of port status |
| 158 | try: |
Matt Jeanneret | a32441c | 2019-03-07 05:16:37 -0500 | [diff] [blame] | 159 | yield self._handler.core_proxy.port_state_update(self._handler.device_id, self._port.type, |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 160 | self._port.port_no, self._port.oper_status) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 161 | except Exception as e: |
| 162 | self.log.exception('update-port', e=e) |
| 163 | |
| 164 | def add_tcont(self, tcont, reflow=False): |
| 165 | """ |
| 166 | Creates/ a T-CONT with the given alloc-id |
| 167 | |
| 168 | :param tcont: (TCont) Object that maintains the TCONT properties |
| 169 | :param reflow: (boolean) If true, force add (used during h/w resync) |
| 170 | :return: (deferred) |
| 171 | """ |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 172 | |
| 173 | if not self._valid: |
Matt Jeanneret | 2ca384f | 2020-03-06 13:49:31 -0500 | [diff] [blame] | 174 | return False # Deleting |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 175 | |
| 176 | if not reflow and tcont.alloc_id in self._tconts: |
Matt Jeanneret | 2ca384f | 2020-03-06 13:49:31 -0500 | [diff] [blame] | 177 | return False # already created |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 178 | |
| 179 | self.log.info('add-tcont', tcont=tcont.alloc_id, reflow=reflow) |
| 180 | self._tconts[tcont.alloc_id] = tcont |
Matt Jeanneret | 2ca384f | 2020-03-06 13:49:31 -0500 | [diff] [blame] | 181 | return True |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 182 | |
| 183 | @inlineCallbacks |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 184 | def remove_tcont(self, alloc_id, remove_from_hw=True): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 185 | |
| 186 | tcont = self._tconts.get(alloc_id) |
| 187 | |
| 188 | if tcont is None: |
| 189 | returnValue('nop') |
| 190 | |
| 191 | try: |
| 192 | del self._tconts[alloc_id] |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 193 | if remove_from_hw: |
| 194 | results = yield tcont.remove_from_hardware(self._handler.openomci.omci_cc) |
| 195 | returnValue(results) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 196 | |
| 197 | except Exception as e: |
| 198 | self.log.exception('delete', e=e) |
| 199 | raise |
| 200 | |
| 201 | def gem_port(self, gem_id, direction): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 202 | return self._gem_ports.get((gem_id, direction)) |
| 203 | |
| 204 | @property |
| 205 | def gem_ids(self): |
| 206 | """Get all GEM Port IDs used by this ONU""" |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 207 | return sorted([gem_id_and_direction[0] for gem_id_and_direction, gem in self._gem_ports.items()]) |
| 208 | |
| 209 | def add_gem_port(self, gem_port, reflow=False): |
| 210 | """ |
| 211 | Add a GEM Port to this ONU |
| 212 | |
| 213 | :param gem_port: (GemPort) GEM Port to add |
| 214 | :param reflow: (boolean) If true, force add (used during h/w resync) |
| 215 | :return: (deferred) |
| 216 | """ |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 217 | |
| 218 | if not self._valid: |
Matt Jeanneret | 2ca384f | 2020-03-06 13:49:31 -0500 | [diff] [blame] | 219 | return False # Deleting |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 220 | |
| 221 | if not reflow and (gem_port.gem_id, gem_port.direction) in self._gem_ports: |
Matt Jeanneret | 2ca384f | 2020-03-06 13:49:31 -0500 | [diff] [blame] | 222 | return False # nop |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 223 | |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 224 | # The gem_port entity id is set to be same as gem_id |
| 225 | gem_port.entity_id = gem_port.gem_id |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 226 | self.log.info('add-gem-port', gem_port=gem_port, reflow=reflow) |
| 227 | self._gem_ports[(gem_port.gem_id, gem_port.direction)] = gem_port |
Matt Jeanneret | 2ca384f | 2020-03-06 13:49:31 -0500 | [diff] [blame] | 228 | return True |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 229 | |
| 230 | @inlineCallbacks |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 231 | def remove_gem_id(self, gem_id, direction, remove_from_hw=True): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 232 | """ |
| 233 | Remove a GEM Port from this ONU |
| 234 | |
| 235 | :param gem_id: (GemPort) GEM Port to remove |
| 236 | :param direction: Direction of the gem port |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 237 | :param remove_from_hw: Remove the GemPort from hardware (remove if True else not) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 238 | :return: deferred |
| 239 | """ |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 240 | |
| 241 | gem_port = self._gem_ports.get((gem_id, direction)) |
| 242 | |
| 243 | if gem_port is None: |
| 244 | returnValue('nop') |
| 245 | |
| 246 | try: |
| 247 | del self._gem_ports[(gem_id, direction)] |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 248 | if remove_from_hw: |
| 249 | results = yield gem_port.remove_from_hardware(self._handler.openomci.omci_cc) |
| 250 | returnValue(results) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 251 | |
| 252 | except Exception as ex: |
| 253 | self.log.exception('gem-port-delete', e=ex) |
| 254 | raise |