blob: 7ba4523a451b700a1cf3f23f59307377023d9941 [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
16import structlog
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050017from twisted.internet import reactor
18from twisted.internet.defer import inlineCallbacks, returnValue, TimeoutError, failure
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050019from pyvoltha.adapters.extensions.omci.omci_me import *
20from pyvoltha.adapters.extensions.omci.tasks.task import Task
21from pyvoltha.adapters.extensions.omci.omci_defs import *
aishwaryarana0180594ce2019-07-26 15:31:14 -050022from pyvoltha.adapters.extensions.omci.omci_entities import *
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050023from adapters.brcm_openomci_onu.uni_port import *
Matt Jeanneret810148b2019-09-29 12:44:01 -040024from adapters.brcm_openomci_onu.pon_port import TASK_PRIORITY, DEFAULT_GEM_PAYLOAD
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050025
aishwaryarana0180594ce2019-07-26 15:31:14 -050026
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050027OP = EntityOperations
28RC = ReasonCodes
29
30
31class TechProfileDownloadFailure(Exception):
32 """
33 This error is raised by default when the download fails
34 """
35
36
37class TechProfileResourcesFailure(Exception):
38 """
39 This error is raised by when one or more resources required is not available
40 """
41
42
43class BrcmTpServiceSpecificTask(Task):
44 """
45 OpenOMCI Tech-Profile Download Task
46
47 """
48
49 name = "Broadcom Tech-Profile Download Task"
50
51 def __init__(self, omci_agent, handler, uni_id):
52 """
53 Class initialization
54
55 :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
Matt Jeanneret810148b2019-09-29 12:44:01 -040056 :param handler: (BrcmOpenomciOnuHandler) ONU Device Handler Instance
57 :param uni_id: (int) numeric id of the uni port on the onu device, starts at 0
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050058 """
59 log = structlog.get_logger(device_id=handler.device_id, uni_id=uni_id)
60 log.debug('function-entry')
61
62 super(BrcmTpServiceSpecificTask, self).__init__(BrcmTpServiceSpecificTask.name,
63 omci_agent,
64 handler.device_id,
65 priority=TASK_PRIORITY,
66 exclusive=True)
67
68 self.log = log
69
70 self._onu_device = omci_agent.get_device(handler.device_id)
71 self._local_deferred = None
72
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050073 self._uni_port = handler.uni_ports[uni_id]
74 assert self._uni_port.uni_id == uni_id
75
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050076 # Entity IDs. IDs with values can probably be most anything for most ONUs,
77 # IDs set to None are discovered/set
78
79 self._mac_bridge_service_profile_entity_id = \
80 handler.mac_bridge_service_profile_entity_id
81 self._ieee_mapper_service_profile_entity_id = \
82 handler.pon_port.ieee_mapper_service_profile_entity_id
83 self._mac_bridge_port_ani_entity_id = \
84 handler.pon_port.mac_bridge_port_ani_entity_id
85 self._gal_enet_profile_entity_id = \
86 handler.gal_enet_profile_entity_id
87
88 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
89 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
90 # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
91 # this initializer
92 self._tconts = []
93 for tcont in handler.pon_port.tconts.itervalues():
94 if tcont.uni_id is not None and tcont.uni_id != self._uni_port.uni_id: continue
95 self._tconts.append(tcont)
96
97 self._gem_ports = []
98 for gem_port in handler.pon_port.gem_ports.itervalues():
99 if gem_port.uni_id is not None and gem_port.uni_id != self._uni_port.uni_id: continue
100 self._gem_ports.append(gem_port)
101
102 self.tcont_me_to_queue_map = dict()
103 self.uni_port_to_queue_map = dict()
104
105 def cancel_deferred(self):
106 self.log.debug('function-entry')
107 super(BrcmTpServiceSpecificTask, self).cancel_deferred()
108
109 d, self._local_deferred = self._local_deferred, None
110 try:
111 if d is not None and not d.called:
112 d.cancel()
113 except:
114 pass
115
116 def start(self):
117 """
118 Start the Tech-Profile Download
119 """
120 self.log.debug('function-entry')
121 super(BrcmTpServiceSpecificTask, self).start()
122 self._local_deferred = reactor.callLater(0, self.perform_service_specific_steps)
123
124 def stop(self):
125 """
126 Shutdown Tech-Profile download tasks
127 """
128 self.log.debug('function-entry')
129 self.log.debug('stopping')
130
131 self.cancel_deferred()
132 super(BrcmTpServiceSpecificTask, self).stop()
133
134 def check_status_and_state(self, results, operation=''):
135 """
136 Check the results of an OMCI response. An exception is thrown
137 if the task was cancelled or an error was detected.
138
139 :param results: (OmciFrame) OMCI Response frame
140 :param operation: (str) what operation was being performed
141 :return: True if successful, False if the entity existed (already created)
142 """
143 self.log.debug('function-entry')
144
145 omci_msg = results.fields['omci_message'].fields
146 status = omci_msg['success_code']
147 error_mask = omci_msg.get('parameter_error_attributes_mask', 'n/a')
148 failed_mask = omci_msg.get('failed_attributes_mask', 'n/a')
149 unsupported_mask = omci_msg.get('unsupported_attributes_mask', 'n/a')
150
Matt Jeannerete8fc53e2019-04-13 15:58:33 -0400151 self.log.debug("OMCI Result", operation=operation, omci_msg=omci_msg, status=status, error_mask=error_mask,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500152 failed_mask=failed_mask, unsupported_mask=unsupported_mask)
153
154 if status == RC.Success:
155 self.strobe_watchdog()
156 return True
157
158 elif status == RC.InstanceExists:
159 return False
160
161 raise TechProfileDownloadFailure(
162 '{} failed with a status of {}, error_mask: {}, failed_mask: {}, unsupported_mask: {}'
163 .format(operation, status, error_mask, failed_mask, unsupported_mask))
164
165 @inlineCallbacks
166 def perform_service_specific_steps(self):
167 self.log.debug('function-entry')
168
169 omci_cc = self._onu_device.omci_cc
aishwaryarana0180594ce2019-07-26 15:31:14 -0500170 gem_pq_associativity = dict()
171 pq_to_related_port = dict()
172 is_related_ports_configurable = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500173
174 try:
175 ################################################################################
176 # TCONTS
177 #
178 # EntityID will be referenced by:
179 # - GemPortNetworkCtp
180 # References:
181 # - ONU created TCONT (created on ONU startup)
182
183 tcont_idents = self._onu_device.query_mib(Tcont.class_id)
184 self.log.debug('tcont-idents', tcont_idents=tcont_idents)
185
186 for tcont in self._tconts:
187 self.log.debug('tcont-loop', tcont=tcont)
188
189 if tcont.entity_id is None:
190 free_entity_id = None
191 for k, v in tcont_idents.items():
192 alloc_check = v.get('attributes', {}).get('alloc_id', 0)
193 # Some onu report both to indicate an available tcont
194 if alloc_check == 0xFF or alloc_check == 0xFFFF:
195 free_entity_id = k
196 break
197
198 self.log.debug('tcont-loop-free', free_entity_id=free_entity_id, alloc_id=tcont.alloc_id)
199
200 if free_entity_id is None:
201 self.log.error('no-available-tconts')
202 break
203
204 # Also assign entity id within tcont object
205 results = yield tcont.add_to_hardware(omci_cc, free_entity_id)
206 self.check_status_and_state(results, 'new-tcont-added')
207 else:
208 # likely already added given entity_id is set, but no harm in doing it again
209 results = yield tcont.add_to_hardware(omci_cc, tcont.entity_id)
210 self.check_status_and_state(results, 'existing-tcont-added')
211
212 ################################################################################
213 # GEMS (GemPortNetworkCtp and GemInterworkingTp)
214 #
215 # For both of these MEs, the entity_id is the GEM Port ID. The entity id of the
216 # GemInterworkingTp ME could be different since it has an attribute to specify
217 # the GemPortNetworkCtp entity id.
218 #
219 # for the GemPortNetworkCtp ME
220 #
221 # GemPortNetworkCtp
222 # EntityID will be referenced by:
223 # - GemInterworkingTp
224 # References:
225 # - TCONT
226 # - Hardcoded upstream TM Entity ID
227 # - (Possibly in Future) Upstream Traffic descriptor profile pointer
228 #
229 # GemInterworkingTp
230 # EntityID will be referenced by:
231 # - Ieee8021pMapperServiceProfile
232 # References:
233 # - GemPortNetworkCtp
234 # - Ieee8021pMapperServiceProfile
235 # - GalEthernetProfile
236 #
237
238 onu_g = self._onu_device.query_mib(OntG.class_id)
239 # If the traffic management option attribute in the ONU-G ME is 0
240 # (priority controlled) or 2 (priority and rate controlled), this
241 # pointer specifies the priority queue ME serving this GEM port
242 # network CTP. If the traffic management option attribute is 1
243 # (rate controlled), this attribute redundantly points to the
244 # T-CONT serving this GEM port network CTP.
245 traffic_mgmt_opt = \
246 onu_g.get('attributes', {}).get('traffic_management_options', 0)
247 self.log.debug("traffic-mgmt-option", traffic_mgmt_opt=traffic_mgmt_opt)
248
249 prior_q = self._onu_device.query_mib(PriorityQueueG.class_id)
250 for k, v in prior_q.items():
251 self.log.debug("prior-q", k=k, v=v)
252
253 try:
254 _ = iter(v)
255 except TypeError:
256 continue
257
258 if 'instance_id' in v:
259 related_port = v['attributes']['related_port']
aishwaryarana0180594ce2019-07-26 15:31:14 -0500260 pq_to_related_port[k] = related_port
261
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500262 if v['instance_id'] & 0b1000000000000000:
263 tcont_me = (related_port & 0xffff0000) >> 16
264 if tcont_me not in self.tcont_me_to_queue_map:
265 self.log.debug("prior-q-related-port-and-tcont-me",
266 related_port=related_port,
267 tcont_me=tcont_me)
268 self.tcont_me_to_queue_map[tcont_me] = list()
269
270 self.tcont_me_to_queue_map[tcont_me].append(k)
271 else:
272 uni_port = (related_port & 0xffff0000) >> 16
273 if uni_port == self._uni_port.entity_id:
274 if uni_port not in self.uni_port_to_queue_map:
275 self.log.debug("prior-q-related-port-and-uni-port-me",
276 related_port=related_port,
277 uni_port_me=uni_port)
278 self.uni_port_to_queue_map[uni_port] = list()
279
280 self.uni_port_to_queue_map[uni_port].append(k)
281
282
283 self.log.debug("ul-prior-q", ul_prior_q=self.tcont_me_to_queue_map)
284 self.log.debug("dl-prior-q", dl_prior_q=self.uni_port_to_queue_map)
285
286 for gem_port in self._gem_ports:
287 # TODO: Traffic descriptor will be available after meter bands are available
288 tcont = gem_port.tcont
289 if tcont is None:
290 self.log.error('unknown-tcont-reference', gem_id=gem_port.gem_id)
291 continue
292
293 ul_prior_q_entity_id = None
294 dl_prior_q_entity_id = None
295 if gem_port.direction == "upstream" or \
296 gem_port.direction == "bi-directional":
297
298 # Sort the priority queue list in order of priority.
299 # 0 is highest priority and 0x0fff is lowest.
300 self.tcont_me_to_queue_map[tcont.entity_id].sort()
301 self.uni_port_to_queue_map[self._uni_port.entity_id].sort()
aishwaryarana0180594ce2019-07-26 15:31:14 -0500302 # Get the priority queue by indexing the priority value of the gemport.
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500303 # The self.tcont_me_to_queue_map and self.uni_port_to_queue_map
304 # have priority queue entities ordered in descending order
305 # of priority
aishwaryarana0180594ce2019-07-26 15:31:14 -0500306
307 ul_prior_q_entity_id = \
308 self.tcont_me_to_queue_map[tcont.entity_id][gem_port.priority_q]
309 dl_prior_q_entity_id = \
310 self.uni_port_to_queue_map[self._uni_port.entity_id][gem_port.priority_q]
311
312 pq_attributes = dict()
313 pq_attributes["pq_entity_id"] = ul_prior_q_entity_id
314 pq_attributes["weight"] = gem_port.weight
315 pq_attributes["scheduling_policy"] = gem_port.scheduling_policy
316 pq_attributes["priority_q"] = gem_port.priority_q
317 gem_pq_associativity[gem_port.entity_id] = pq_attributes
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500318
319 assert ul_prior_q_entity_id is not None and \
320 dl_prior_q_entity_id is not None
321
322 # TODO: Need to restore on failure. Need to check status/results
323 results = yield gem_port.add_to_hardware(omci_cc,
324 tcont.entity_id,
325 self._ieee_mapper_service_profile_entity_id +
326 self._uni_port.mac_bridge_port_num,
327 self._gal_enet_profile_entity_id,
328 ul_prior_q_entity_id, dl_prior_q_entity_id)
329 self.check_status_and_state(results, 'assign-gem-port')
330 elif gem_port.direction == "downstream":
331 # Downstream is inverse of upstream
332 # TODO: could also be a case of multicast. Not supported for now
333 self.log.debug("skipping-downstream-gem", gem_port=gem_port)
334 pass
335
336 ################################################################################
aishwaryarana0180594ce2019-07-26 15:31:14 -0500337 # Update the PriorityQeue Attributes for the PQ Associated with Gemport
338 #
339 # Entityt ID was created prior to this call. This is a set
340 #
341 #
342
343 ont2g = self._onu_device.query_mib(Ont2G.class_id)
344 qos_config_flexibility_ie = ont2g.get(0, {}).get('attributes', {}).\
345 get('qos_configuration_flexibility', None)
346 self.log.debug("qos_config_flexibility",
347 qos_config_flexibility=qos_config_flexibility_ie)
348
349 if qos_config_flexibility_ie & 0b100000:
350 is_related_ports_configurable = True
351
352 for k, v in gem_pq_associativity.items():
353 if v["scheduling_policy"] == "WRR":
354 self.log.debug("updating-pq-weight")
355 msg = PriorityQueueFrame(v["pq_entity_id"], weight=v["weight"])
356 frame = msg.set()
357 results = yield omci_cc.send(frame)
358 self.check_status_and_state(results, 'set-priority-queues-weight')
359 elif v["scheduling_policy"] == "StrictPriority" and \
360 is_related_ports_configurable:
361 self.log.debug("updating-pq-priority")
362 related_port = pq_to_related_port[v["pq_entity_id"]]
363 related_port = related_port & 0xffff0000
364 related_port = related_port | v['priority_q'] # Set priority
365 msg = PriorityQueueFrame(v["pq_entity_id"], related_port=related_port)
366 frame = msg.set()
367 results = yield omci_cc.send(frame)
368 self.check_status_and_state(results, 'set-priority-queues-priority')
369
370
371 ################################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500372 # Update the IEEE 802.1p Mapper Service Profile config
373 #
374 # EntityID was created prior to this call. This is a set
375 #
376 # References:
377 # - Gem Interwork TPs are set here
378 #
379
380 gem_entity_ids = [OmciNullPointer] * 8
381 for gem_port in self._gem_ports:
382 self.log.debug("tp-gem-port", entity_id=gem_port.entity_id, uni_id=gem_port.uni_id)
383
384 if gem_port.direction == "upstream" or \
385 gem_port.direction == "bi-directional":
386 for i, p in enumerate(reversed(gem_port.pbit_map)):
387 if p == '1':
388 gem_entity_ids[i] = gem_port.entity_id
389 elif gem_port.direction == "downstream":
390 # Downstream gem port p-bit mapper is inverse of upstream
391 # TODO: Could also be a case of multicast. Not supported for now
392 pass
393
394 msg = Ieee8021pMapperServiceProfileFrame(
395 self._ieee_mapper_service_profile_entity_id + self._uni_port.mac_bridge_port_num, # 802.1p mapper Service Mapper Profile ID
396 interwork_tp_pointers=gem_entity_ids # Interworking TP IDs
397 )
398 frame = msg.set()
399 self.log.debug('openomci-msg', omci_msg=msg)
400 results = yield omci_cc.send(frame)
401 self.check_status_and_state(results, 'set-8021p-mapper-service-profile-ul')
402
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500403 self.deferred.callback("tech-profile-download-success")
404
405 except TimeoutError as e:
Matt Jeanneret810148b2019-09-29 12:44:01 -0400406 self.log.warn('rx-timeout-tech-profile', e=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500407 self.deferred.errback(failure.Failure(e))
408
409 except Exception as e:
Matt Jeanneret810148b2019-09-29 12:44:01 -0400410 self.log.exception('omci-setup-tech-profile', e=e)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500411 self.deferred.errback(failure.Failure(e))