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 | |
| 16 | import structlog |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 17 | from twisted.internet.defer import inlineCallbacks, returnValue, succeed |
Matt Jeanneret | 72f96fc | 2019-02-11 10:53:05 -0500 | [diff] [blame] | 18 | from pyvoltha.adapters.extensions.omci.omci_me import * |
| 19 | from pyvoltha.adapters.extensions.omci.omci_defs import * |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 20 | |
| 21 | RC = ReasonCodes |
| 22 | |
| 23 | |
| 24 | class OnuTCont(object): |
| 25 | """ |
| 26 | Broadcom ONU specific implementation |
| 27 | """ |
| 28 | def __init__(self, handler, uni_id, alloc_id, q_sched_policy, traffic_descriptor): |
| 29 | |
| 30 | self.log = structlog.get_logger(device_id=handler.device_id, uni_id=uni_id, alloc_id=alloc_id) |
| 31 | self.log.debug('function-entry') |
| 32 | |
| 33 | self.uni_id = uni_id |
| 34 | self.alloc_id = alloc_id |
| 35 | self._q_sched_policy = 0 |
| 36 | self.q_sched_policy = q_sched_policy |
| 37 | self.traffic_descriptor = traffic_descriptor |
| 38 | |
| 39 | self._handler = handler |
| 40 | self._entity_id = None |
| 41 | |
| 42 | def __str__(self): |
| 43 | return "OnuTCont - uni_id: {}, entity_id {}, alloc-id: {}, q_sched_policy: {}, traffic_descriptor: {}".format( |
| 44 | self.uni_id, self._entity_id, self.alloc_id, self.q_sched_policy, self.traffic_descriptor) |
| 45 | |
| 46 | def __repr__(self): |
| 47 | return str(self) |
| 48 | |
| 49 | @property |
| 50 | def entity_id(self): |
| 51 | self.log.debug('function-entry') |
| 52 | return self._entity_id |
| 53 | |
| 54 | @property |
| 55 | def q_sched_policy(self): |
| 56 | self.log.debug('function-entry') |
| 57 | return self._q_sched_policy |
| 58 | |
| 59 | |
| 60 | @q_sched_policy.setter |
| 61 | def q_sched_policy(self, q_sched_policy): |
| 62 | sp = ('Null', 'WRR', 'StrictPriority') |
| 63 | if q_sched_policy in sp: |
| 64 | self._q_sched_policy = sp.index(q_sched_policy) |
| 65 | else: |
| 66 | self._q_sched_policy = 0 |
| 67 | |
| 68 | @staticmethod |
| 69 | def create(handler, tcont, td): |
| 70 | log = structlog.get_logger(tcont=tcont, td=td) |
| 71 | log.debug('function-entry', tcont=tcont) |
| 72 | |
| 73 | return OnuTCont(handler, |
| 74 | tcont['uni_id'], |
| 75 | tcont['alloc-id'], |
| 76 | tcont['q_sched_policy'], |
| 77 | td |
| 78 | ) |
| 79 | |
| 80 | @inlineCallbacks |
| 81 | def add_to_hardware(self, omci, tcont_entity_id): |
| 82 | self.log.debug('add-to-hardware', tcont_entity_id=tcont_entity_id) |
| 83 | |
| 84 | self._entity_id = tcont_entity_id |
| 85 | |
| 86 | try: |
| 87 | # FIXME: self.q_sched_policy seems to be READ-ONLY |
| 88 | # Ideally the READ-ONLY or NOT attribute is available from ONU-2G ME |
| 89 | #msg = TcontFrame(self.entity_id, self.alloc_id, self.q_sched_policy) |
| 90 | msg = TcontFrame(self.entity_id, self.alloc_id) |
| 91 | frame = msg.set() |
| 92 | self.log.debug('openomci-msg', omci_msg=msg) |
| 93 | results = yield omci.send(frame) |
| 94 | self.check_status_and_state(results, 'set-tcont') |
| 95 | |
| 96 | except Exception as e: |
| 97 | self.log.exception('tcont-set', e=e) |
| 98 | raise |
| 99 | |
| 100 | returnValue(results) |
| 101 | |
| 102 | @inlineCallbacks |
| 103 | def remove_from_hardware(self, omci): |
| 104 | self.log.debug('function-entry', omci=omci) |
| 105 | self.log.debug('remove-from-hardware', tcont_entity_id=self.entity_id) |
| 106 | |
| 107 | # Release tcont by setting alloc_id=0xFFFF |
| 108 | # TODO: magic number, create a named variable |
| 109 | |
| 110 | try: |
| 111 | msg = TcontFrame(self.entity_id, 0xFFFF) |
| 112 | frame = msg.set() |
| 113 | self.log.debug('openomci-msg', omci_msg=msg) |
| 114 | results = yield omci.send(frame) |
| 115 | self.check_status_and_state(results, 'delete-tcont') |
| 116 | |
| 117 | except Exception as e: |
| 118 | self.log.exception('tcont-delete', e=e) |
| 119 | raise |
| 120 | |
| 121 | returnValue(results) |
| 122 | |
| 123 | def check_status_and_state(self, results, operation=''): |
| 124 | self.log.debug('function-entry') |
| 125 | omci_msg = results.fields['omci_message'].fields |
| 126 | status = omci_msg['success_code'] |
| 127 | error_mask = omci_msg.get('parameter_error_attributes_mask', 'n/a') |
| 128 | failed_mask = omci_msg.get('failed_attributes_mask', 'n/a') |
| 129 | unsupported_mask = omci_msg.get('unsupported_attributes_mask', 'n/a') |
| 130 | |
Matt Jeanneret | e8fc53e | 2019-04-13 15:58:33 -0400 | [diff] [blame] | 131 | self.log.debug("OMCI Result", operation=operation, omci_msg=omci_msg, |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 132 | status=status, error_mask=error_mask, |
| 133 | failed_mask=failed_mask, unsupported_mask=unsupported_mask) |
| 134 | |
| 135 | if status == RC.Success: |
| 136 | return True |
| 137 | |
| 138 | elif status == RC.InstanceExists: |
| 139 | return False |