blob: c314dc474fc086c89a17df2683615e1262fd5f22 [file] [log] [blame]
Chip Boling8e042f62019-02-12 16:14:34 -06001# Copyright 2017-present Adtran, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import structlog
16from twisted.internet.defer import inlineCallbacks, returnValue
17
18from adapters.adtran_common.xpon.tcont import TCont
19from adapters.adtran_common.xpon.traffic_descriptor import TrafficDescriptor
20from pyvoltha.adapters.extensions.omci.omci_me import TcontFrame
21from pyvoltha.adapters.extensions.omci.omci_defs import ReasonCodes
22
23
24class OnuTCont(TCont):
25 """
26 Adtran ONU specific implementation
27 """
28 FREE_TCONT_ALLOC_ID = 0xFFFF
29 FREE_GPON_TCONT_ALLOC_ID = 0xFF # SFU may use this to indicate a free TCONT
30
31 def __init__(self, handler, alloc_id, sched_policy, tech_profile_id, uni_id, traffic_descriptor, is_mock=False):
32 super(OnuTCont, self).__init__(alloc_id, tech_profile_id, traffic_descriptor, uni_id, is_mock=is_mock)
33 self.log = structlog.get_logger(device_id=handler.device_id, alloc_id=alloc_id)
34
35 self._handler = handler
36 self.sched_policy = sched_policy
37 self._entity_id = None
38 self._free_alloc_id = OnuTCont.FREE_TCONT_ALLOC_ID
39
40 @property
41 def entity_id(self):
42 return self._entity_id
43
44 @staticmethod
45 def create(handler, tcont, td):
46 assert isinstance(tcont, dict), 'TCONT should be a dictionary'
47 assert isinstance(td, TrafficDescriptor), 'Invalid Traffic Descriptor data type'
48 return OnuTCont(handler,
49 tcont['alloc-id'],
50 tcont['q-sched-policy'],
51 tcont['tech-profile-id'],
52 tcont['uni-id'],
53 td)
54
55 @inlineCallbacks
56 def add_to_hardware(self, omci, tcont_entity_id, prev_alloc_id=FREE_TCONT_ALLOC_ID):
57 self.log.debug('add-to-hardware', tcont_entity_id=tcont_entity_id)
58 if self._is_mock:
59 returnValue('mock')
60
61 if self._entity_id == tcont_entity_id:
62 returnValue('Already set')
63
64 elif self.entity_id is not None:
65 raise KeyError('TCONT already assigned: {}'.format(self.entity_id))
66
67 try:
68 # TODO: Look up ONU2-G QoS flexibility attribute and only set this
69 # if q-sched-policy can be supported
70
71 self._free_alloc_id = prev_alloc_id
72 frame = TcontFrame(tcont_entity_id, self.alloc_id).set()
73 results = yield omci.send(frame)
74
75 status = results.fields['omci_message'].fields['success_code']
76 if status == ReasonCodes.Success:
77 self._entity_id = tcont_entity_id
78
79 failed_attributes_mask = results.fields['omci_message'].fields['failed_attributes_mask']
80 unsupported_attributes_mask = results.fields['omci_message'].fields['unsupported_attributes_mask']
81
82 self.log.debug('set-tcont', status=status,
83 failed_attributes_mask=failed_attributes_mask,
84 unsupported_attributes_mask=unsupported_attributes_mask)
85
86 except Exception as e:
87 self.log.exception('tcont-set', e=e)
88 raise
89
90 returnValue(results)
91
92 @inlineCallbacks
93 def remove_from_hardware(self, omci):
94 self.log.debug('remove-from-hardware', tcont_entity_id=self.entity_id)
95 if self._is_mock:
96 returnValue('mock')
97 try:
98 frame = TcontFrame(self.entity_id, self._free_alloc_id).set()
99 results = yield omci.send(frame)
100
101 status = results.fields['omci_message'].fields['success_code']
102 self.log.debug('delete-tcont', status=status)
103
104 if status == ReasonCodes.Success:
105 self._entity_id = None
106
107 except Exception as e:
108 self.log.exception('tcont-delete', e=e)
109 raise
110
111 returnValue(results)