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) |
| 37 | self.log.debug('function-entry') |
| 38 | |
| 39 | self._enabled = False |
| 40 | self._valid = True |
| 41 | self._handler = handler |
| 42 | self._deferred = None |
| 43 | self._port = None |
| 44 | self._port_number = port_no |
Matt Jeanneret | 0c28789 | 2019-02-28 11:48:00 -0500 | [diff] [blame] | 45 | self._peers = [] |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 46 | self._next_entity_id = PonPort.MIN_GEM_ENTITY_ID |
| 47 | |
| 48 | self._admin_state = AdminState.ENABLED |
| 49 | self._oper_status = OperStatus.ACTIVE |
| 50 | |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 51 | self._gem_ports = {} # gem-id -> GemPort |
| 52 | self._tconts = {} # alloc-id -> TCont |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 53 | |
| 54 | self.ieee_mapper_service_profile_entity_id = 0x8001 |
| 55 | self.mac_bridge_port_ani_entity_id = 0x2102 # TODO: can we just use the entity id from the anis list? |
| 56 | |
| 57 | def __str__(self): |
| 58 | return "PonPort - port_number: {}, next_entity_id: {}, num_gem_ports: {}, num_tconts: {}".format( |
| 59 | self._port_number, self._next_entity_id, len(self._gem_ports), len(self._tconts)) |
| 60 | |
| 61 | def __repr__(self): |
| 62 | return str(self) |
| 63 | |
| 64 | @staticmethod |
| 65 | def create(handler, port_no): |
| 66 | log = structlog.get_logger(device_id=handler.device_id, port_no=port_no) |
| 67 | log.debug('function-entry') |
| 68 | port = PonPort(handler, port_no) |
| 69 | |
| 70 | return port |
| 71 | |
| 72 | def _start(self): |
| 73 | self.log.debug('function-entry') |
| 74 | self._cancel_deferred() |
| 75 | |
| 76 | self._admin_state = AdminState.ENABLED |
| 77 | self._oper_status = OperStatus.ACTIVE |
| 78 | self._update_adapter_agent() |
| 79 | |
| 80 | def _stop(self): |
| 81 | self.log.debug('function-entry') |
| 82 | self._cancel_deferred() |
| 83 | |
| 84 | self._admin_state = AdminState.DISABLED |
| 85 | self._oper_status = OperStatus.UNKNOWN |
| 86 | self._update_adapter_agent() |
| 87 | |
| 88 | # TODO: stop h/w sync |
| 89 | |
| 90 | def _cancel_deferred(self): |
| 91 | self.log.debug('function-entry') |
| 92 | d1, self._deferred = self._deferred, None |
| 93 | |
| 94 | for d in [d1]: |
| 95 | try: |
| 96 | if d is not None and not d.called: |
| 97 | d.cancel() |
| 98 | except: |
| 99 | pass |
| 100 | |
| 101 | def delete(self): |
| 102 | self.log.debug('function-entry') |
| 103 | self.enabled = False |
| 104 | self._valid = False |
| 105 | self._handler = None |
| 106 | |
| 107 | @property |
| 108 | def enabled(self): |
| 109 | self.log.debug('function-entry') |
| 110 | return self._enabled |
| 111 | |
| 112 | @enabled.setter |
| 113 | def enabled(self, value): |
| 114 | self.log.debug('function-entry') |
| 115 | if self._enabled != value: |
| 116 | self._enabled = value |
| 117 | |
| 118 | if value: |
| 119 | self._start() |
| 120 | else: |
| 121 | self._stop() |
| 122 | |
| 123 | @property |
| 124 | def port_number(self): |
| 125 | self.log.debug('function-entry') |
| 126 | return self._port_number |
| 127 | |
| 128 | @property |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 129 | def tconts(self): |
| 130 | self.log.debug('function-entry') |
| 131 | return self._tconts |
| 132 | |
| 133 | @property |
| 134 | def gem_ports(self): |
| 135 | self.log.debug('function-entry') |
| 136 | return self._gem_ports |
| 137 | |
| 138 | def get_port(self): |
| 139 | """ |
| 140 | Get the VOLTHA PORT object for this port |
| 141 | :return: VOLTHA Port object |
| 142 | """ |
| 143 | self.log.debug('function-entry') |
| 144 | |
Matt Jeanneret | 0c28789 | 2019-02-28 11:48:00 -0500 | [diff] [blame] | 145 | self._port = Port(port_no=self.port_number, |
| 146 | label='PON port', |
| 147 | type=Port.PON_ONU, |
| 148 | admin_state=self._admin_state, |
| 149 | oper_status=self._oper_status, |
| 150 | peers=self._peers) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 151 | return self._port |
| 152 | |
Matt Jeanneret | 0c28789 | 2019-02-28 11:48:00 -0500 | [diff] [blame] | 153 | def add_peer(self, parent_device_id, parent_port_no): |
| 154 | self.log.debug('add-peer-port', parent_device_id=parent_device_id, parent_port_no=parent_port_no) |
| 155 | new_peer = Port.PeerPort(device_id=parent_device_id, port_no=parent_port_no) |
| 156 | self._peers.extend([new_peer]) |
| 157 | |
Matt Jeanneret | 84e56f6 | 2019-02-26 10:48:09 -0500 | [diff] [blame] | 158 | @inlineCallbacks |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 159 | def _update_adapter_agent(self): |
| 160 | """ |
| 161 | Update the port status and state in the core |
| 162 | """ |
| 163 | self.log.debug('function-entry') |
| 164 | self.log.debug('update-adapter-agent', admin_state=self._admin_state, |
| 165 | oper_status=self._oper_status) |
| 166 | |
| 167 | if self._port is not None: |
| 168 | self._port.admin_state = self._admin_state |
| 169 | self._port.oper_status = self._oper_status |
| 170 | |
| 171 | # adapter_agent add_port also does an update of port status |
| 172 | try: |
Matt Jeanneret | a32441c | 2019-03-07 05:16:37 -0500 | [diff] [blame] | 173 | 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] | 174 | self._port.port_no, self._port.oper_status) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 175 | except Exception as e: |
| 176 | self.log.exception('update-port', e=e) |
| 177 | |
| 178 | def add_tcont(self, tcont, reflow=False): |
| 179 | """ |
| 180 | Creates/ a T-CONT with the given alloc-id |
| 181 | |
| 182 | :param tcont: (TCont) Object that maintains the TCONT properties |
| 183 | :param reflow: (boolean) If true, force add (used during h/w resync) |
| 184 | :return: (deferred) |
| 185 | """ |
| 186 | self.log.debug('function-entry', tcont=tcont.alloc_id) |
| 187 | |
| 188 | if not self._valid: |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 189 | return # Deleting |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 190 | |
| 191 | if not reflow and tcont.alloc_id in self._tconts: |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 192 | return # already created |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 193 | |
| 194 | self.log.info('add-tcont', tcont=tcont.alloc_id, reflow=reflow) |
| 195 | self._tconts[tcont.alloc_id] = tcont |
| 196 | |
| 197 | def update_tcont_td(self, alloc_id, new_td): |
| 198 | self.log.debug('function-entry') |
| 199 | |
| 200 | tcont = self._tconts.get(alloc_id) |
| 201 | |
| 202 | if tcont is None: |
| 203 | return # not-found |
| 204 | |
| 205 | tcont.traffic_descriptor = new_td |
| 206 | |
| 207 | # TODO: Not yet implemented |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 208 | # TODO: How does this affect ONU tcont settings? |
| 209 | # try: |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 210 | # results = yield tcont.add_to_hardware(self._handler.omci) |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 211 | # except Exception as e: |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 212 | # self.log.exception('tcont', tcont=tcont, e=e) |
| 213 | # # May occur with xPON provisioning, use hw-resync to recover |
| 214 | # results = 'resync needed' |
| 215 | # returnValue(results) |
| 216 | |
| 217 | @inlineCallbacks |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 218 | def remove_tcont(self, alloc_id, remove_from_hw=True): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 219 | self.log.debug('function-entry') |
| 220 | |
| 221 | tcont = self._tconts.get(alloc_id) |
| 222 | |
| 223 | if tcont is None: |
| 224 | returnValue('nop') |
| 225 | |
| 226 | try: |
| 227 | del self._tconts[alloc_id] |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 228 | if remove_from_hw: |
| 229 | results = yield tcont.remove_from_hardware(self._handler.openomci.omci_cc) |
| 230 | returnValue(results) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 231 | |
| 232 | except Exception as e: |
| 233 | self.log.exception('delete', e=e) |
| 234 | raise |
| 235 | |
| 236 | def gem_port(self, gem_id, direction): |
| 237 | self.log.debug('function-entry') |
| 238 | return self._gem_ports.get((gem_id, direction)) |
| 239 | |
| 240 | @property |
| 241 | def gem_ids(self): |
| 242 | """Get all GEM Port IDs used by this ONU""" |
| 243 | self.log.debug('function-entry') |
| 244 | return sorted([gem_id_and_direction[0] for gem_id_and_direction, gem in self._gem_ports.items()]) |
| 245 | |
| 246 | def add_gem_port(self, gem_port, reflow=False): |
| 247 | """ |
| 248 | Add a GEM Port to this ONU |
| 249 | |
| 250 | :param gem_port: (GemPort) GEM Port to add |
| 251 | :param reflow: (boolean) If true, force add (used during h/w resync) |
| 252 | :return: (deferred) |
| 253 | """ |
| 254 | self.log.debug('function-entry', gem_port=gem_port.gem_id) |
| 255 | |
| 256 | if not self._valid: |
| 257 | return # Deleting |
| 258 | |
| 259 | if not reflow and (gem_port.gem_id, gem_port.direction) in self._gem_ports: |
| 260 | return # nop |
| 261 | |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 262 | # The gem_port entity id is set to be same as gem_id |
| 263 | gem_port.entity_id = gem_port.gem_id |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 264 | self.log.info('add-gem-port', gem_port=gem_port, reflow=reflow) |
| 265 | self._gem_ports[(gem_port.gem_id, gem_port.direction)] = gem_port |
| 266 | |
| 267 | @inlineCallbacks |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 268 | def remove_gem_id(self, gem_id, direction, remove_from_hw=True): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 269 | """ |
| 270 | Remove a GEM Port from this ONU |
| 271 | |
| 272 | :param gem_id: (GemPort) GEM Port to remove |
| 273 | :param direction: Direction of the gem port |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 274 | :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] | 275 | :return: deferred |
| 276 | """ |
| 277 | self.log.debug('function-entry', gem_id=gem_id) |
| 278 | |
| 279 | gem_port = self._gem_ports.get((gem_id, direction)) |
| 280 | |
| 281 | if gem_port is None: |
| 282 | returnValue('nop') |
| 283 | |
| 284 | try: |
| 285 | del self._gem_ports[(gem_id, direction)] |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 286 | if remove_from_hw: |
| 287 | results = yield gem_port.remove_from_hardware(self._handler.openomci.omci_cc) |
| 288 | returnValue(results) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 289 | |
| 290 | except Exception as ex: |
| 291 | self.log.exception('gem-port-delete', e=ex) |
| 292 | raise |