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 |
ozgecanetsia | b7f7309 | 2020-02-16 23:36:25 +0300 | [diff] [blame^] | 19 | from pyvoltha.adapters.extensions.omci.omci_me import GemInterworkingTpFrame, GemPortNetworkCtpFrame, MulticastGemInterworkingTPFrame |
| 20 | from pyvoltha.adapters.extensions.omci.omci_entities import IPv4MulticastAddressTable |
Matt Jeanneret | 2e3cb8d | 2019-11-16 09:22:41 -0500 | [diff] [blame] | 21 | from pyvoltha.adapters.extensions.omci.omci_defs import ReasonCodes |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 22 | |
| 23 | RC = ReasonCodes |
| 24 | |
| 25 | |
| 26 | class OnuGemPort(object): |
| 27 | """ |
| 28 | Broadcom ONU specific implementation |
| 29 | """ |
| 30 | |
| 31 | def __init__(self, gem_id, uni_id, alloc_id, |
| 32 | entity_id=None, |
| 33 | direction="BIDIRECTIONAL", |
| 34 | encryption=False, |
| 35 | discard_config=None, |
| 36 | discard_policy=None, |
| 37 | max_q_size="auto", |
| 38 | pbit_map="0b00000011", |
| 39 | priority_q=3, |
| 40 | scheduling_policy="WRR", |
| 41 | weight=8, |
| 42 | omci_transport=False, |
| 43 | multicast=False, |
| 44 | tcont_ref=None, |
| 45 | traffic_class=None, |
| 46 | intf_ref=None, |
| 47 | untagged=False, |
| 48 | name=None, |
| 49 | handler=None): |
| 50 | |
| 51 | self.log = structlog.get_logger(device_id=handler.device_id, uni_id=uni_id, gem_id=gem_id) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 52 | |
| 53 | self.name = name |
| 54 | self.gem_id = gem_id |
| 55 | self.uni_id = uni_id |
| 56 | self._alloc_id = alloc_id |
| 57 | self.tcont_ref = tcont_ref |
| 58 | self.intf_ref = intf_ref |
| 59 | self.traffic_class = traffic_class |
| 60 | self._direction = None |
| 61 | self._encryption = encryption |
| 62 | self._discard_config = None |
| 63 | self._discard_policy = None |
| 64 | self._max_q_size = None |
| 65 | self._pbit_map = None |
| 66 | self._scheduling_policy = None |
| 67 | self._omci_transport = omci_transport |
| 68 | self.multicast = multicast |
| 69 | self.untagged = untagged |
| 70 | self._handler = handler |
| 71 | |
| 72 | self.direction = direction |
| 73 | self.encryption = encryption |
| 74 | self.discard_config = discard_config |
| 75 | self.discard_policy = discard_policy |
| 76 | self.max_q_size = max_q_size |
| 77 | self.pbit_map = pbit_map |
| 78 | self.priority_q = priority_q |
| 79 | self.scheduling_policy = scheduling_policy |
| 80 | self.weight = weight |
| 81 | |
| 82 | self._pon_id = None |
| 83 | self._onu_id = None |
| 84 | self._entity_id = entity_id |
| 85 | |
| 86 | # Statistics |
| 87 | self.rx_packets = 0 |
| 88 | self.rx_bytes = 0 |
| 89 | self.tx_packets = 0 |
| 90 | self.tx_bytes = 0 |
| 91 | |
| 92 | def __str__(self): |
Matt Jeanneret | 3789d0d | 2020-01-19 09:03:42 -0500 | [diff] [blame] | 93 | return "OnuGemPort - entity_id {}, alloc-id: {}, gem-id: {}".format(self.entity_id, self.alloc_id, |
Girish Gowdra | e933cd3 | 2019-11-21 21:04:41 +0530 | [diff] [blame] | 94 | self.gem_id) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 95 | |
| 96 | def __repr__(self): |
| 97 | return str(self) |
| 98 | |
| 99 | @property |
| 100 | def pon_id(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 101 | return self._pon_id |
| 102 | |
| 103 | @pon_id.setter |
| 104 | def pon_id(self, pon_id): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 105 | assert self._pon_id is None or self._pon_id == pon_id, 'PON-ID can only be set once' |
| 106 | self._pon_id = pon_id |
| 107 | |
| 108 | @property |
| 109 | def onu_id(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 110 | return self._onu_id |
| 111 | |
| 112 | @onu_id.setter |
| 113 | def onu_id(self, onu_id): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 114 | assert self._onu_id is None or self._onu_id == onu_id, 'ONU-ID can only be set once' |
| 115 | self._onu_id = onu_id |
| 116 | |
| 117 | @property |
| 118 | def alloc_id(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 119 | return self._alloc_id |
| 120 | |
| 121 | @property |
| 122 | def direction(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 123 | return self._direction |
| 124 | |
| 125 | @direction.setter |
| 126 | def direction(self, direction): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 127 | # GEM Port CTP are configured separately in UPSTREAM and DOWNSTREAM. |
| 128 | # BIDIRECTIONAL is not supported. |
| 129 | assert direction == "UPSTREAM" or direction == "DOWNSTREAM" or \ |
| 130 | direction == "BIDIRECTIONAL", "invalid-direction" |
| 131 | |
| 132 | # OMCI framework expects string in lower-case. Tech-Profile sends in upper-case. |
| 133 | if direction == "UPSTREAM": |
| 134 | self._direction = "upstream" |
| 135 | elif direction == "DOWNSTREAM": |
| 136 | self._direction = "downstream" |
| 137 | elif direction == "BIDIRECTIONAL": |
| 138 | self._direction = "bi-directional" |
| 139 | |
| 140 | @property |
| 141 | def tcont(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 142 | tcont_item = self._handler.pon_port.tconts.get(self.alloc_id) |
| 143 | return tcont_item |
| 144 | |
| 145 | @property |
| 146 | def omci_transport(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 147 | return self._omci_transport |
| 148 | |
| 149 | def to_dict(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 150 | return { |
| 151 | 'port-id': self.gem_id, |
| 152 | 'alloc-id': self.alloc_id, |
| 153 | 'encryption': self._encryption, |
| 154 | 'omci-transport': self.omci_transport |
| 155 | } |
| 156 | |
| 157 | @property |
| 158 | def entity_id(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 159 | return self._entity_id |
| 160 | |
| 161 | @entity_id.setter |
| 162 | def entity_id(self, value): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 163 | self._entity_id = value |
| 164 | |
| 165 | @property |
| 166 | def encryption(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 167 | return self._encryption |
| 168 | |
| 169 | @encryption.setter |
| 170 | def encryption(self, value): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 171 | # FIXME The encryption should come as boolean by default |
| 172 | value = eval(value) |
| 173 | assert isinstance(value, bool), 'encryption is a boolean' |
| 174 | |
| 175 | if self._encryption != value: |
| 176 | self._encryption = value |
| 177 | |
| 178 | @property |
| 179 | def discard_config(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 180 | return self._discard_config |
| 181 | |
| 182 | @discard_config.setter |
| 183 | def discard_config(self, discard_config): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 184 | assert isinstance(discard_config, dict), "discard_config not dict" |
| 185 | assert 'max_probability' in discard_config, "max_probability missing" |
| 186 | assert 'max_threshold' in discard_config, "max_threshold missing" |
| 187 | assert 'min_threshold' in discard_config, "min_threshold missing" |
| 188 | self._discard_config = discard_config |
| 189 | |
| 190 | @property |
| 191 | def discard_policy(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 192 | return self._discard_policy |
| 193 | |
| 194 | @discard_policy.setter |
| 195 | def discard_policy(self, discard_policy): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 196 | dp = ("TailDrop", "WTailDrop", "RED", "WRED") |
| 197 | assert (isinstance(discard_policy, str)) |
| 198 | assert (discard_policy in dp) |
| 199 | self._discard_policy = discard_policy |
| 200 | |
| 201 | @property |
| 202 | def max_q_size(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 203 | return self._max_q_size |
| 204 | |
| 205 | @max_q_size.setter |
| 206 | def max_q_size(self, max_q_size): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 207 | if isinstance(max_q_size, str): |
| 208 | assert (max_q_size == "auto") |
| 209 | else: |
| 210 | assert (isinstance(max_q_size, int)) |
| 211 | |
| 212 | self._max_q_size = max_q_size |
| 213 | |
| 214 | @property |
| 215 | def pbit_map(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 216 | return self._pbit_map |
| 217 | |
| 218 | @pbit_map.setter |
| 219 | def pbit_map(self, pbit_map): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 220 | assert (isinstance(pbit_map, str)) |
| 221 | assert (len(pbit_map[2:]) == 8) # Example format of pbit_map: "0b00000101" |
| 222 | try: |
| 223 | _ = int(pbit_map[2], 2) |
| 224 | except ValueError: |
| 225 | raise Exception("pbit_map-not-binary-string-{}".format(pbit_map)) |
| 226 | |
| 227 | # remove '0b' |
| 228 | self._pbit_map = pbit_map[2:] |
| 229 | |
| 230 | @property |
| 231 | def scheduling_policy(self): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 232 | return self._scheduling_policy |
| 233 | |
| 234 | @scheduling_policy.setter |
| 235 | def scheduling_policy(self, scheduling_policy): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 236 | sp = ("WRR", "StrictPriority") |
| 237 | assert (isinstance(scheduling_policy, str)) |
| 238 | assert (scheduling_policy in sp) |
| 239 | self._scheduling_policy = scheduling_policy |
| 240 | |
| 241 | @staticmethod |
| 242 | def create(handler, gem_port): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 243 | |
| 244 | return OnuGemPort(gem_id=gem_port['gemport_id'], |
| 245 | uni_id=gem_port['uni_id'], |
| 246 | alloc_id=gem_port['alloc_id_ref'], |
| 247 | direction=gem_port['direction'], |
| 248 | encryption=gem_port['encryption'], # aes_indicator, |
| 249 | discard_config=gem_port['discard_config'], |
| 250 | discard_policy=gem_port['discard_policy'], |
| 251 | max_q_size=gem_port['max_q_size'], |
| 252 | pbit_map=gem_port['pbit_map'], |
| 253 | priority_q=gem_port['priority_q'], |
| 254 | scheduling_policy=gem_port['scheduling_policy'], |
| 255 | weight=gem_port['weight'], |
| 256 | handler=handler, |
| 257 | untagged=False) |
| 258 | |
| 259 | @inlineCallbacks |
| 260 | def add_to_hardware(self, omci, |
| 261 | tcont_entity_id, |
| 262 | ieee_mapper_service_profile_entity_id, |
| 263 | gal_enet_profile_entity_id, |
| 264 | ul_prior_q_entity_id, |
| 265 | dl_prior_q_entity_id): |
| 266 | |
| 267 | self.log.debug('add-to-hardware', entity_id=self.entity_id, gem_id=self.gem_id, |
| 268 | tcont_entity_id=tcont_entity_id, |
| 269 | ieee_mapper_service_profile_entity_id=ieee_mapper_service_profile_entity_id, |
| 270 | gal_enet_profile_entity_id=gal_enet_profile_entity_id, |
| 271 | ul_prior_q_entity_id=ul_prior_q_entity_id, |
| 272 | dl_prior_q_entity_id=dl_prior_q_entity_id) |
| 273 | |
| 274 | try: |
| 275 | direction = "downstream" if self.multicast else "bi-directional" |
ozgecanetsia | b7f7309 | 2020-02-16 23:36:25 +0300 | [diff] [blame^] | 276 | entity_id = self.gem_id if self.multicast else self.entity_id |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 277 | assert not self.multicast, 'MCAST is not supported yet' |
| 278 | |
| 279 | attributes = dict() |
| 280 | attributes['priority_queue_pointer_downstream'] = dl_prior_q_entity_id |
| 281 | msg = GemPortNetworkCtpFrame( |
| 282 | self.entity_id, # same entity id as GEM port |
| 283 | port_id=self.gem_id, |
| 284 | tcont_id=tcont_entity_id, |
| 285 | direction=direction, |
| 286 | upstream_tm=ul_prior_q_entity_id, |
| 287 | attributes=attributes |
| 288 | ) |
| 289 | frame = msg.create() |
| 290 | self.log.debug('openomci-msg', omci_msg=msg) |
| 291 | results = yield omci.send(frame) |
| 292 | self.check_status_and_state(results, 'create-gem-port-network-ctp') |
| 293 | |
| 294 | except Exception as e: |
| 295 | self.log.exception('gemport-create', e=e) |
| 296 | raise |
| 297 | |
| 298 | try: |
ozgecanetsia | b7f7309 | 2020-02-16 23:36:25 +0300 | [diff] [blame^] | 299 | if self.multicast: |
| 300 | # 224.0.0.0 - 239.255.255.255 is valid multicast ip range |
| 301 | mcast_ip_list= IPv4MulticastAddressTable( gem_port_id=self.gem_id, |
| 302 | secondary_key=0, |
| 303 | multicast_ip_range_start="224.0.0.0", |
| 304 | multicast_ip_range_stop="239.255.255.255") |
| 305 | msg = MulticastGemInterworkingTPFrame( |
| 306 | self.gem_id, |
| 307 | gem_port_network_ctp_pointer=self.gem_id, |
| 308 | interworking_option=0, # Mac Bridge |
| 309 | service_profile_pointer=0, |
| 310 | gal_profile_pointer=gal_enet_profile_entity_id, |
| 311 | ipv4_multicast_address_table= mcast_ip_list |
| 312 | ) |
| 313 | else: |
| 314 | msg = GemInterworkingTpFrame( |
| 315 | self.entity_id, # same entity id as GEM port |
| 316 | gem_port_network_ctp_pointer=self.entity_id, |
| 317 | interworking_option=5, # IEEE 802.1 |
| 318 | service_profile_pointer=ieee_mapper_service_profile_entity_id, |
| 319 | interworking_tp_pointer=0x0, |
| 320 | pptp_counter=1, |
| 321 | gal_profile_pointer=gal_enet_profile_entity_id |
| 322 | ) |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 323 | frame = msg.create() |
| 324 | self.log.debug('openomci-msg', omci_msg=msg) |
| 325 | results = yield omci.send(frame) |
| 326 | self.check_status_and_state(results, 'create-gem-interworking-tp') |
| 327 | |
| 328 | except Exception as e: |
| 329 | self.log.exception('interworking-create', e=e) |
| 330 | raise |
| 331 | |
| 332 | returnValue(results) |
| 333 | |
| 334 | @inlineCallbacks |
| 335 | def remove_from_hardware(self, omci): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 336 | self.log.debug('remove-from-hardware', gem_id=self.gem_id) |
| 337 | |
| 338 | try: |
| 339 | msg = GemInterworkingTpFrame(self.entity_id) |
| 340 | frame = msg.delete() |
| 341 | self.log.debug('openomci-msg', omci_msg=msg) |
| 342 | results = yield omci.send(frame) |
| 343 | self.check_status_and_state(results, 'delete-gem-port-network-ctp') |
| 344 | except Exception as e: |
| 345 | self.log.exception('interworking-delete', e=e) |
| 346 | raise |
| 347 | |
| 348 | try: |
| 349 | msg = GemPortNetworkCtpFrame(self.entity_id) |
| 350 | frame = msg.delete() |
| 351 | self.log.debug('openomci-msg', omci_msg=msg) |
| 352 | results = yield omci.send(frame) |
| 353 | self.check_status_and_state(results, 'delete-gem-interworking-tp') |
| 354 | except Exception as e: |
| 355 | self.log.exception('gemport-delete', e=e) |
| 356 | raise |
| 357 | |
| 358 | returnValue(results) |
| 359 | |
| 360 | def check_status_and_state(self, results, operation=''): |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 361 | omci_msg = results.fields['omci_message'].fields |
| 362 | status = omci_msg['success_code'] |
| 363 | error_mask = omci_msg.get('parameter_error_attributes_mask', 'n/a') |
| 364 | failed_mask = omci_msg.get('failed_attributes_mask', 'n/a') |
| 365 | unsupported_mask = omci_msg.get('unsupported_attributes_mask', 'n/a') |
| 366 | |
Matt Jeanneret | e8fc53e | 2019-04-13 15:58:33 -0400 | [diff] [blame] | 367 | self.log.debug("OMCI Result", operation=operation, omci_msg=omci_msg, |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 368 | status=status, error_mask=error_mask, |
| 369 | failed_mask=failed_mask, unsupported_mask=unsupported_mask) |
| 370 | |
| 371 | if status == RC.Success: |
| 372 | return True |
| 373 | |
| 374 | elif status == RC.InstanceExists: |
| 375 | return False |