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