blob: dd61b7e98276c879230a1f4c57417b139f8d3f27 [file] [log] [blame]
kesavand5a2f61d2020-11-23 21:49:50 -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#
16
17from __future__ import absolute_import
18import structlog
19from pyvoltha.adapters.extensions.omci.tasks.task import Task
20from twisted.internet import reactor
21from twisted.internet.defer import inlineCallbacks, failure, returnValue, DeferredQueue
22from pyvoltha.adapters.extensions.omci.omci_defs import ReasonCodes, EntityOperations
23from pyvoltha.adapters.extensions.omci.omci_me import OntGFrame
24from pyvoltha.adapters.extensions.omci.omci_me import PptpEthernetUniFrame, VeipUniFrame
25from voltha_protos.extensions_pb2 import SingleGetValueResponse, GetValueResponse, GetOnuUniInfoResponse
26
27RC = ReasonCodes
28OP = EntityOperations
29
30
31class BrcmUniStatusTask(Task):
32 """
33 Get the status of the UNI Port
34 """
35 task_priority = Task.DEFAULT_PRIORITY + 20
36 name = "Broadcom UNI Status Task"
37
38 def __init__(self, omci_agent, device_id, get_val_req, entity_id, msg_queue, priority=task_priority):
39 """
40 Class initialization
41
42 :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
43 :param device_id: (str) ONU Device ID
44 :param get_val_req: (voltha_pb2.SingleGetValueResponse)
45 :param priority: (int) OpenOMCI Task priority (0..255) 255 is the highest
46 """
47 super(BrcmUniStatusTask, self).__init__(BrcmUniStatusTask.name,
48 omci_agent,
49 device_id,
50 priority=priority,
51 exclusive=True)
52
53 self.log = structlog.get_logger(device_id=device_id,
54 name=BrcmUniStatusTask.name,
55 task_id=self._task_id)
56
57 self._device = omci_agent.get_device(device_id)
58 self._req = get_val_req
59 self._results = SingleGetValueResponse()
60 self._local_deferred = None
61 self._config = self._device.configuration
62 self._entity_id = entity_id
63 self.uni_status_response_queue = msg_queue
64
65 self.log.info("get-uni-staus-deviceid",deviceid=device_id)
66
67 def cancel_deferred(self):
68 super(BrcmUniStatusTask, self).cancel_deferred()
69
70 d, self._local_deferred = self._local_deferred, None
71 try:
72 if d is not None and not d.called:
73 d.cancel()
74 except:
75 pass
76
77 def start(self):
78 """
79 Start UNI/PPTP Get Status Task
80 """
81 super(BrcmUniStatusTask, self).start()
82 self._local_deferred = reactor.callLater(0, self.perform_get_uni_status)
83 def stop(self):
84 self.cancel_deferred()
85 super(BrcmUniStatusTask, self).stop()
86
87
88 @inlineCallbacks
89 def perform_get_uni_status(self):
90 """
91 Perform the Get UNI Status
92 """
93 self.log.info('get-uni-status-uni-index-is ',uni_index=self._req.uniInfo.uniIndex)
94
95
96 try:
97 pptp_list = sorted(self._config.pptp_entities) if self._config.pptp_entities else []
98 pptp_items = ['administrative_state', 'operational_state', 'config_ind', 'max_frame_size', 'sensed_type', 'bridged_ip_ind']
99 for entity_id in pptp_list:
100 self.log.info('entity-id',entity_id)
101 msg = PptpEthernetUniFrame(self._entity_id, attributes=pptp_items)
102 yield self._send_omci_msg(msg)
103 except Exception as e:
104 self._results.response.status = GetValueResponse.ERROR
105 self._results.response.errReason = GetValueResponse.REASON_UNDEFINED
106 self.log.exception('get-uni-status', e=e)
107 finally:
108 self.log.info('uni-status-response',self._results)
109 yield self.uni_status_response_queue.put(self._results)
110
111 @inlineCallbacks
112 def _send_omci_msg(self, me_message):
113 frame = me_message.get()
114 results = yield self._device.omci_cc.send(frame)
115 if self._check_status_and_state(results, 'get-uni-status'):
116 omci_msg = results.fields['omci_message'].fields
117 self._results.response.status = GetValueResponse.OK
118 self._collect_uni_admin_state(results)
119 self._collect_uni_operational_state(results)
120 self._collect_uni_config_ind(results)
121 else:
122 self._results.response.status = GetValueResponse.ERROR
123 self._results.response.errReason = GetValueResponse.UNSUPPORTED
124
125 def _collect_uni_admin_state(self, results):
126 omci_msg = results.fields['omci_message'].fields
127 admin_state = omci_msg.get("data").get("administrative_state")
128 if admin_state == 0 :
129 self._results.response.uniInfo.admState = GetOnuUniInfoResponse.UNLOCKED
130 elif admin_state == 1 :
131 self._results.response.uniInfo.admState = GetOnuUniInfoResponse.LOCKED
132 else:
133 self._results.response.uniInfo.admState = GetOnuUniInfoResponse.ADMSTATE_UNDEFINED
134
135 def _collect_uni_operational_state(self, results):
136 self.log.info('collect-uni-oper-state')
137 omci_msg = results.fields['omci_message'].fields
138 oper_state=omci_msg.get("data").get("operational_state")
139 if oper_state == 0 :
140 self._results.response.uniInfo.operState = GetOnuUniInfoResponse.ENABLED
141 elif oper_state == 1 :
142 self._results.response.uniInfo.operState = GetOnuUniInfoResponse.DISABLED
143 else:
144 self._results.response.uniInfo.operState = GetOnuUniInfoResponse.OPERSTATE_UNDEFINED
145
146 def _collect_uni_config_ind(self, results):
147 config_ind_map = {0:GetOnuUniInfoResponse.UNKOWN,
148 1:GetOnuUniInfoResponse.TEN_BASE_T_FDX,
149 2:GetOnuUniInfoResponse.HUNDRED_BASE_T_FDX,
150 3:GetOnuUniInfoResponse.GIGABIT_ETHERNET_FDX,
151 4:GetOnuUniInfoResponse.TEN_G_ETHERNET_FDX,
152 17:GetOnuUniInfoResponse.TEN_BASE_T_HDX,
153 18:GetOnuUniInfoResponse.HUNDRED_BASE_T_HDX,
154 19:GetOnuUniInfoResponse.GIGABIT_ETHERNET_HDX,
155 }
156 self.log.info('collect-config-ind')
157 omci_msg = results.fields['omci_message'].fields
158 config_ind =omci_msg.get("data").get("config_ind", GetOnuUniInfoResponse.UNKOWN)
159 self._results.response.uniInfo.configInd = config_ind_map.get(config_ind, GetOnuUniInfoResponse.UNKOWN)
160
161 def _check_status_and_state(self, results, operation=''):
162 """
163 Check the results of an OMCI response. An exception is thrown
164 if the task was cancelled or an error was detected.
165
166 :param results: (OmciFrame) OMCI Response frame
167 :param operation: (str) what operation was being performed
168 :return: True if successful, False if the entity existed (already created)
169 """
170 omci_msg = results.fields['omci_message'].fields
171 status = omci_msg['success_code']
172 error_mask = omci_msg.get('parameter_error_attributes_mask', 'n/a')
173 failed_mask = omci_msg.get('failed_attributes_mask', 'n/a')
174 unsupported_mask = omci_msg.get('unsupported_attributes_mask', 'n/a')
175
176 self.log.debug("omci-response", operation=operation,
177 omci_msg=omci_msg, status=status,
178 error_mask=error_mask, failed_mask=failed_mask,
179 unsupported_mask=unsupported_mask)
180
181 self.strobe_watchdog()
182 if status == RC.Success:
183 return True
184 else:
185 self.log.info("omci-reponse-failed", status, "error-mask-is",
186 error_mask, "failed-mask-is ", failed_mask, "unsupported-mask-is ", unsupported_mask)
187 return False
188