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