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