Chip Boling | 8e042f6 | 2019-02-12 16:14:34 -0600 | [diff] [blame] | 1 | # Copyright 2017-present Adtran, Inc. |
| 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 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 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. |
| 14 | |
| 15 | import structlog |
| 16 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 17 | from pyvoltha.protos.common_pb2 import AdminState, OperStatus |
| 18 | from pyvoltha.protos.device_pb2 import Port |
| 19 | |
| 20 | |
| 21 | class PonPort(object): |
| 22 | """Wraps northbound-port/ANI support for ONU""" |
| 23 | MIN_GEM_ENTITY_ID = 0x4900 |
| 24 | MAX_GEM_ENTITY_ID = 0x4AFF |
| 25 | |
| 26 | def __init__(self, handler, port_no): |
| 27 | self.log = structlog.get_logger(device_id=handler.device_id, port_no=port_no) |
| 28 | |
| 29 | self._enabled = False |
| 30 | self._valid = True |
| 31 | self._handler = handler |
| 32 | self._deferred = None |
| 33 | self._port = None |
| 34 | self._port_number = port_no |
| 35 | self._entity_id = None # ANI entity ID |
| 36 | self._next_entity_id = PonPort.MIN_GEM_ENTITY_ID |
| 37 | |
| 38 | self._admin_state = AdminState.ENABLED |
| 39 | self._oper_status = OperStatus.ACTIVE |
| 40 | |
| 41 | self._gem_ports = {} # gem-id -> GemPort |
| 42 | self._tconts = {} # alloc-id -> TCont |
| 43 | |
| 44 | # OMCI resources |
| 45 | # TODO: These could be dynamically chosen (can be most any value) |
| 46 | self.ieee_mapper_service_profile_entity_id = 0x100 |
| 47 | self.mac_bridge_port_ani_entity_id = 0x100 |
| 48 | |
| 49 | def __str__(self): |
| 50 | return "PonPort" # TODO: Encode current state |
| 51 | |
| 52 | @staticmethod |
| 53 | def create(handler, port_no): |
| 54 | port = PonPort(handler, port_no) |
| 55 | return port |
| 56 | |
| 57 | def _start(self): |
| 58 | self._cancel_deferred() |
| 59 | |
| 60 | self._admin_state = AdminState.ENABLED |
| 61 | self._oper_status = OperStatus.ACTIVE |
| 62 | self._update_adapter_agent() |
| 63 | |
| 64 | def _stop(self): |
| 65 | self._cancel_deferred() |
| 66 | |
| 67 | self._admin_state = AdminState.DISABLED |
| 68 | self._oper_status = OperStatus.UNKNOWN |
| 69 | self._update_adapter_agent() |
| 70 | |
| 71 | # TODO: stop h/w sync |
| 72 | |
| 73 | def _cancel_deferred(self): |
| 74 | d1, self._deferred = self._deferred, None |
| 75 | |
| 76 | for d in [d1]: |
| 77 | try: |
| 78 | if d is not None and not d.called: |
| 79 | d.cancel() |
| 80 | except: |
| 81 | pass |
| 82 | |
| 83 | def delete(self): |
| 84 | self.enabled = False |
| 85 | self._valid = False |
| 86 | self._handler = None |
| 87 | |
| 88 | @property |
| 89 | def enabled(self): |
| 90 | return self._enabled |
| 91 | |
| 92 | @enabled.setter |
| 93 | def enabled(self, value): |
| 94 | if self._enabled != value: |
| 95 | self._enabled = value |
| 96 | |
| 97 | if value: |
| 98 | self._start() |
| 99 | else: |
| 100 | self._stop() |
| 101 | |
| 102 | @property |
| 103 | def port_number(self): |
| 104 | return self._port_number |
| 105 | |
| 106 | @property |
| 107 | def entity_id(self): |
| 108 | """ |
| 109 | OMCI ANI_G entity ID for port |
| 110 | """ |
| 111 | return self._entity_id |
| 112 | |
| 113 | @entity_id.setter |
| 114 | def entity_id(self, value): |
| 115 | assert self._entity_id is None or self._entity_id == value, 'Cannot reset the Entity ID' |
| 116 | self._entity_id = value |
| 117 | |
| 118 | @property |
| 119 | def next_gem_entity_id(self): |
| 120 | entity_id = self._next_entity_id |
| 121 | |
| 122 | self._next_entity_id = self._next_entity_id + 1 |
| 123 | if self._next_entity_id > PonPort.MAX_GEM_ENTITY_ID: |
| 124 | self._next_entity_id = PonPort.MIN_GEM_ENTITY_ID |
| 125 | |
| 126 | return entity_id |
| 127 | |
| 128 | @property |
| 129 | def tconts(self): |
| 130 | return self._tconts |
| 131 | |
| 132 | @property |
| 133 | def gem_ports(self): |
| 134 | return self._gem_ports |
| 135 | |
| 136 | def get_port(self): |
| 137 | """ |
| 138 | Get the VOLTHA PORT object for this port |
| 139 | :return: VOLTHA Port object |
| 140 | """ |
| 141 | if self._port is None: |
| 142 | device = self._handler.adapter_agent.get_device(self._handler.device_id) |
| 143 | |
| 144 | self._port = Port(port_no=self.port_number, |
| 145 | label='PON port', |
| 146 | type=Port.PON_ONU, |
| 147 | admin_state=self._admin_state, |
| 148 | oper_status=self._oper_status, |
| 149 | peers=[Port.PeerPort(device_id=device.parent_id, |
| 150 | port_no=device.parent_port_no)]) |
| 151 | return self._port |
| 152 | |
Chip Boling | d2d7a4d | 2019-03-14 14:34:56 -0500 | [diff] [blame] | 153 | @inlineCallbacks |
Chip Boling | 8e042f6 | 2019-02-12 16:14:34 -0600 | [diff] [blame] | 154 | def _update_adapter_agent(self): |
| 155 | """ |
| 156 | Update the port status and state in the core |
| 157 | """ |
| 158 | self.log.debug('update-adapter-agent', admin_state=self._admin_state, |
| 159 | oper_status=self._oper_status) |
| 160 | |
| 161 | if self._port is not None: |
| 162 | self._port.admin_state = self._admin_state |
| 163 | self._port.oper_status = self._oper_status |
| 164 | |
Chip Boling | d2d7a4d | 2019-03-14 14:34:56 -0500 | [diff] [blame] | 165 | try: |
| 166 | yield self._handler.adapter_agent.port_state_update(self._handler.device_id, |
| 167 | self._port.type, |
| 168 | self._port.port_no, |
| 169 | self._port.oper_status) |
| 170 | except Exception as e: |
| 171 | self.log.exception('update-port', e=e) |
Chip Boling | 8e042f6 | 2019-02-12 16:14:34 -0600 | [diff] [blame] | 172 | |
| 173 | def add_tcont(self, tcont, reflow=False): |
| 174 | """ |
| 175 | Creates/ a T-CONT with the given alloc-id |
| 176 | |
| 177 | :param tcont: (TCont) Object that maintains the TCONT properties |
| 178 | :param reflow: (boolean) If true, force add (used during h/w resync) |
| 179 | :return: (deferred) |
| 180 | """ |
| 181 | if not self._valid: |
| 182 | return # Deleting |
| 183 | |
| 184 | if not reflow and tcont.alloc_id in self._tconts: |
| 185 | return # already created |
| 186 | |
| 187 | self.log.info('add', tcont=tcont, reflow=reflow) |
| 188 | self._tconts[tcont.alloc_id] = tcont |
| 189 | |
| 190 | @inlineCallbacks |
| 191 | def remove_tcont(self, alloc_id): |
| 192 | tcont = self._tconts.get(alloc_id) |
| 193 | |
| 194 | if tcont is None: |
| 195 | returnValue('nop') |
| 196 | |
| 197 | try: |
| 198 | del self._tconts[alloc_id] |
| 199 | results = yield tcont.remove_from_hardware(self._handler.openomci.omci_cc) |
| 200 | returnValue(results) |
| 201 | |
| 202 | except Exception as e: |
| 203 | self.log.exception('delete', e=e) |
| 204 | raise |
| 205 | |
| 206 | def gem_port(self, gem_id): |
| 207 | return self._gem_ports.get(gem_id) |
| 208 | |
| 209 | @property |
| 210 | def gem_ids(self): |
| 211 | """Get all GEM Port IDs used by this ONU""" |
| 212 | return sorted([gem_id for gem_id, gem in self._gem_ports.items()]) |
| 213 | |
| 214 | def add_gem_port(self, gem_port, reflow=False): |
| 215 | """ |
| 216 | Add a GEM Port to this ONU |
| 217 | |
| 218 | :param gem_port: (GemPort) GEM Port to add |
| 219 | :param reflow: (boolean) If true, force add (used during h/w resync) |
| 220 | :return: (deferred) |
| 221 | """ |
| 222 | if not self._valid: |
| 223 | return # Deleting |
| 224 | |
| 225 | if not reflow and gem_port.gem_id in self._gem_ports: |
| 226 | return # nop |
| 227 | |
| 228 | self.log.info('add', gem_port=gem_port, reflow=reflow) |
| 229 | self._gem_ports[gem_port.gem_id] = gem_port |
| 230 | |
| 231 | @inlineCallbacks |
| 232 | def remove_gem_id(self, gem_id): |
| 233 | """ |
| 234 | Remove a GEM Port from this ONU |
| 235 | |
| 236 | :param gem_port: (GemPort) GEM Port to remove |
| 237 | :return: deferred |
| 238 | """ |
| 239 | gem_port = self._gem_ports.get(gem_id) |
| 240 | |
| 241 | if gem_port is None: |
| 242 | returnValue('nop') |
| 243 | |
| 244 | try: |
| 245 | del self._gem_ports[gem_id] |
| 246 | results = yield gem_port.remove_from_hardware(self._handler.openomci.omci_cc) |
| 247 | returnValue(results) |
| 248 | |
| 249 | except Exception as ex: |
| 250 | self.log.exception('gem-port-delete', e=ex) |
| 251 | raise |