blob: 65a0320d5ff74c3bdc81dd82ab152db787ba18a0 [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 *
24from adapters.brcm_openomci_onu.pon_port \
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050025 import BRDCM_DEFAULT_VLAN, TASK_PRIORITY, DEFAULT_TPID, DEFAULT_GEM_PAYLOAD
26
aishwaryarana0180594ce2019-07-26 15:31:14 -050027
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050028OP = EntityOperations
29RC = ReasonCodes
30
31
32class TechProfileDownloadFailure(Exception):
33 """
34 This error is raised by default when the download fails
35 """
36
37
38class TechProfileResourcesFailure(Exception):
39 """
40 This error is raised by when one or more resources required is not available
41 """
42
43
44class BrcmTpServiceSpecificTask(Task):
45 """
46 OpenOMCI Tech-Profile Download Task
47
48 """
49
50 name = "Broadcom Tech-Profile Download Task"
51
52 def __init__(self, omci_agent, handler, uni_id):
53 """
54 Class initialization
55
56 :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
57 :param device_id: (str) ONU Device ID
58 """
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
73 # Frame size
74 self._max_gem_payload = DEFAULT_GEM_PAYLOAD
75
76 self._uni_port = handler.uni_ports[uni_id]
77 assert self._uni_port.uni_id == uni_id
78
79 # Port numbers
80 self._input_tpid = DEFAULT_TPID
81 self._output_tpid = DEFAULT_TPID
82
83 self._vlan_tcis_1 = BRDCM_DEFAULT_VLAN
84 self._cvid = BRDCM_DEFAULT_VLAN
85 self._vlan_config_entity_id = self._vlan_tcis_1
86
87 # Entity IDs. IDs with values can probably be most anything for most ONUs,
88 # IDs set to None are discovered/set
89
90 self._mac_bridge_service_profile_entity_id = \
91 handler.mac_bridge_service_profile_entity_id
92 self._ieee_mapper_service_profile_entity_id = \
93 handler.pon_port.ieee_mapper_service_profile_entity_id
94 self._mac_bridge_port_ani_entity_id = \
95 handler.pon_port.mac_bridge_port_ani_entity_id
96 self._gal_enet_profile_entity_id = \
97 handler.gal_enet_profile_entity_id
98
99 # Extract the current set of TCONT and GEM Ports from the Handler's pon_port that are
100 # relevant to this task's UNI. It won't change. But, the underlying pon_port may change
101 # due to additional tasks on different UNIs. So, it we cannot use the pon_port affter
102 # this initializer
103 self._tconts = []
104 for tcont in handler.pon_port.tconts.itervalues():
105 if tcont.uni_id is not None and tcont.uni_id != self._uni_port.uni_id: continue
106 self._tconts.append(tcont)
107
108 self._gem_ports = []
109 for gem_port in handler.pon_port.gem_ports.itervalues():
110 if gem_port.uni_id is not None and gem_port.uni_id != self._uni_port.uni_id: continue
111 self._gem_ports.append(gem_port)
112
113 self.tcont_me_to_queue_map = dict()
114 self.uni_port_to_queue_map = dict()
115
116 def cancel_deferred(self):
117 self.log.debug('function-entry')
118 super(BrcmTpServiceSpecificTask, self).cancel_deferred()
119
120 d, self._local_deferred = self._local_deferred, None
121 try:
122 if d is not None and not d.called:
123 d.cancel()
124 except:
125 pass
126
127 def start(self):
128 """
129 Start the Tech-Profile Download
130 """
131 self.log.debug('function-entry')
132 super(BrcmTpServiceSpecificTask, self).start()
133 self._local_deferred = reactor.callLater(0, self.perform_service_specific_steps)
134
135 def stop(self):
136 """
137 Shutdown Tech-Profile download tasks
138 """
139 self.log.debug('function-entry')
140 self.log.debug('stopping')
141
142 self.cancel_deferred()
143 super(BrcmTpServiceSpecificTask, self).stop()
144
145 def check_status_and_state(self, results, operation=''):
146 """
147 Check the results of an OMCI response. An exception is thrown
148 if the task was cancelled or an error was detected.
149
150 :param results: (OmciFrame) OMCI Response frame
151 :param operation: (str) what operation was being performed
152 :return: True if successful, False if the entity existed (already created)
153 """
154 self.log.debug('function-entry')
155
156 omci_msg = results.fields['omci_message'].fields
157 status = omci_msg['success_code']
158 error_mask = omci_msg.get('parameter_error_attributes_mask', 'n/a')
159 failed_mask = omci_msg.get('failed_attributes_mask', 'n/a')
160 unsupported_mask = omci_msg.get('unsupported_attributes_mask', 'n/a')
161
Matt Jeannerete8fc53e2019-04-13 15:58:33 -0400162 self.log.debug("OMCI Result", operation=operation, omci_msg=omci_msg, status=status, error_mask=error_mask,
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500163 failed_mask=failed_mask, unsupported_mask=unsupported_mask)
164
165 if status == RC.Success:
166 self.strobe_watchdog()
167 return True
168
169 elif status == RC.InstanceExists:
170 return False
171
172 raise TechProfileDownloadFailure(
173 '{} failed with a status of {}, error_mask: {}, failed_mask: {}, unsupported_mask: {}'
174 .format(operation, status, error_mask, failed_mask, unsupported_mask))
175
176 @inlineCallbacks
177 def perform_service_specific_steps(self):
178 self.log.debug('function-entry')
179
180 omci_cc = self._onu_device.omci_cc
aishwaryarana0180594ce2019-07-26 15:31:14 -0500181 gem_pq_associativity = dict()
182 pq_to_related_port = dict()
183 is_related_ports_configurable = False
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500184
185 try:
186 ################################################################################
187 # TCONTS
188 #
189 # EntityID will be referenced by:
190 # - GemPortNetworkCtp
191 # References:
192 # - ONU created TCONT (created on ONU startup)
193
194 tcont_idents = self._onu_device.query_mib(Tcont.class_id)
195 self.log.debug('tcont-idents', tcont_idents=tcont_idents)
196
197 for tcont in self._tconts:
198 self.log.debug('tcont-loop', tcont=tcont)
199
200 if tcont.entity_id is None:
201 free_entity_id = None
202 for k, v in tcont_idents.items():
203 alloc_check = v.get('attributes', {}).get('alloc_id', 0)
204 # Some onu report both to indicate an available tcont
205 if alloc_check == 0xFF or alloc_check == 0xFFFF:
206 free_entity_id = k
207 break
208
209 self.log.debug('tcont-loop-free', free_entity_id=free_entity_id, alloc_id=tcont.alloc_id)
210
211 if free_entity_id is None:
212 self.log.error('no-available-tconts')
213 break
214
215 # Also assign entity id within tcont object
216 results = yield tcont.add_to_hardware(omci_cc, free_entity_id)
217 self.check_status_and_state(results, 'new-tcont-added')
218 else:
219 # likely already added given entity_id is set, but no harm in doing it again
220 results = yield tcont.add_to_hardware(omci_cc, tcont.entity_id)
221 self.check_status_and_state(results, 'existing-tcont-added')
222
223 ################################################################################
224 # GEMS (GemPortNetworkCtp and GemInterworkingTp)
225 #
226 # For both of these MEs, the entity_id is the GEM Port ID. The entity id of the
227 # GemInterworkingTp ME could be different since it has an attribute to specify
228 # the GemPortNetworkCtp entity id.
229 #
230 # for the GemPortNetworkCtp ME
231 #
232 # GemPortNetworkCtp
233 # EntityID will be referenced by:
234 # - GemInterworkingTp
235 # References:
236 # - TCONT
237 # - Hardcoded upstream TM Entity ID
238 # - (Possibly in Future) Upstream Traffic descriptor profile pointer
239 #
240 # GemInterworkingTp
241 # EntityID will be referenced by:
242 # - Ieee8021pMapperServiceProfile
243 # References:
244 # - GemPortNetworkCtp
245 # - Ieee8021pMapperServiceProfile
246 # - GalEthernetProfile
247 #
248
249 onu_g = self._onu_device.query_mib(OntG.class_id)
250 # If the traffic management option attribute in the ONU-G ME is 0
251 # (priority controlled) or 2 (priority and rate controlled), this
252 # pointer specifies the priority queue ME serving this GEM port
253 # network CTP. If the traffic management option attribute is 1
254 # (rate controlled), this attribute redundantly points to the
255 # T-CONT serving this GEM port network CTP.
256 traffic_mgmt_opt = \
257 onu_g.get('attributes', {}).get('traffic_management_options', 0)
258 self.log.debug("traffic-mgmt-option", traffic_mgmt_opt=traffic_mgmt_opt)
259
260 prior_q = self._onu_device.query_mib(PriorityQueueG.class_id)
261 for k, v in prior_q.items():
262 self.log.debug("prior-q", k=k, v=v)
263
264 try:
265 _ = iter(v)
266 except TypeError:
267 continue
268
269 if 'instance_id' in v:
270 related_port = v['attributes']['related_port']
aishwaryarana0180594ce2019-07-26 15:31:14 -0500271 pq_to_related_port[k] = related_port
272
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500273 if v['instance_id'] & 0b1000000000000000:
274 tcont_me = (related_port & 0xffff0000) >> 16
275 if tcont_me not in self.tcont_me_to_queue_map:
276 self.log.debug("prior-q-related-port-and-tcont-me",
277 related_port=related_port,
278 tcont_me=tcont_me)
279 self.tcont_me_to_queue_map[tcont_me] = list()
280
281 self.tcont_me_to_queue_map[tcont_me].append(k)
282 else:
283 uni_port = (related_port & 0xffff0000) >> 16
284 if uni_port == self._uni_port.entity_id:
285 if uni_port not in self.uni_port_to_queue_map:
286 self.log.debug("prior-q-related-port-and-uni-port-me",
287 related_port=related_port,
288 uni_port_me=uni_port)
289 self.uni_port_to_queue_map[uni_port] = list()
290
291 self.uni_port_to_queue_map[uni_port].append(k)
292
293
294 self.log.debug("ul-prior-q", ul_prior_q=self.tcont_me_to_queue_map)
295 self.log.debug("dl-prior-q", dl_prior_q=self.uni_port_to_queue_map)
296
297 for gem_port in self._gem_ports:
298 # TODO: Traffic descriptor will be available after meter bands are available
299 tcont = gem_port.tcont
300 if tcont is None:
301 self.log.error('unknown-tcont-reference', gem_id=gem_port.gem_id)
302 continue
303
304 ul_prior_q_entity_id = None
305 dl_prior_q_entity_id = None
306 if gem_port.direction == "upstream" or \
307 gem_port.direction == "bi-directional":
308
309 # Sort the priority queue list in order of priority.
310 # 0 is highest priority and 0x0fff is lowest.
311 self.tcont_me_to_queue_map[tcont.entity_id].sort()
312 self.uni_port_to_queue_map[self._uni_port.entity_id].sort()
aishwaryarana0180594ce2019-07-26 15:31:14 -0500313 # Get the priority queue by indexing the priority value of the gemport.
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500314 # The self.tcont_me_to_queue_map and self.uni_port_to_queue_map
315 # have priority queue entities ordered in descending order
316 # of priority
aishwaryarana0180594ce2019-07-26 15:31:14 -0500317
318 ul_prior_q_entity_id = \
319 self.tcont_me_to_queue_map[tcont.entity_id][gem_port.priority_q]
320 dl_prior_q_entity_id = \
321 self.uni_port_to_queue_map[self._uni_port.entity_id][gem_port.priority_q]
322
323 pq_attributes = dict()
324 pq_attributes["pq_entity_id"] = ul_prior_q_entity_id
325 pq_attributes["weight"] = gem_port.weight
326 pq_attributes["scheduling_policy"] = gem_port.scheduling_policy
327 pq_attributes["priority_q"] = gem_port.priority_q
328 gem_pq_associativity[gem_port.entity_id] = pq_attributes
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500329
330 assert ul_prior_q_entity_id is not None and \
331 dl_prior_q_entity_id is not None
332
333 # TODO: Need to restore on failure. Need to check status/results
334 results = yield gem_port.add_to_hardware(omci_cc,
335 tcont.entity_id,
336 self._ieee_mapper_service_profile_entity_id +
337 self._uni_port.mac_bridge_port_num,
338 self._gal_enet_profile_entity_id,
339 ul_prior_q_entity_id, dl_prior_q_entity_id)
340 self.check_status_and_state(results, 'assign-gem-port')
341 elif gem_port.direction == "downstream":
342 # Downstream is inverse of upstream
343 # TODO: could also be a case of multicast. Not supported for now
344 self.log.debug("skipping-downstream-gem", gem_port=gem_port)
345 pass
346
347 ################################################################################
aishwaryarana0180594ce2019-07-26 15:31:14 -0500348 # Update the PriorityQeue Attributes for the PQ Associated with Gemport
349 #
350 # Entityt ID was created prior to this call. This is a set
351 #
352 #
353
354 ont2g = self._onu_device.query_mib(Ont2G.class_id)
355 qos_config_flexibility_ie = ont2g.get(0, {}).get('attributes', {}).\
356 get('qos_configuration_flexibility', None)
357 self.log.debug("qos_config_flexibility",
358 qos_config_flexibility=qos_config_flexibility_ie)
359
360 if qos_config_flexibility_ie & 0b100000:
361 is_related_ports_configurable = True
362
363 for k, v in gem_pq_associativity.items():
364 if v["scheduling_policy"] == "WRR":
365 self.log.debug("updating-pq-weight")
366 msg = PriorityQueueFrame(v["pq_entity_id"], weight=v["weight"])
367 frame = msg.set()
368 results = yield omci_cc.send(frame)
369 self.check_status_and_state(results, 'set-priority-queues-weight')
370 elif v["scheduling_policy"] == "StrictPriority" and \
371 is_related_ports_configurable:
372 self.log.debug("updating-pq-priority")
373 related_port = pq_to_related_port[v["pq_entity_id"]]
374 related_port = related_port & 0xffff0000
375 related_port = related_port | v['priority_q'] # Set priority
376 msg = PriorityQueueFrame(v["pq_entity_id"], related_port=related_port)
377 frame = msg.set()
378 results = yield omci_cc.send(frame)
379 self.check_status_and_state(results, 'set-priority-queues-priority')
380
381
382 ################################################################################
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500383 # Update the IEEE 802.1p Mapper Service Profile config
384 #
385 # EntityID was created prior to this call. This is a set
386 #
387 # References:
388 # - Gem Interwork TPs are set here
389 #
390
391 gem_entity_ids = [OmciNullPointer] * 8
392 for gem_port in self._gem_ports:
393 self.log.debug("tp-gem-port", entity_id=gem_port.entity_id, uni_id=gem_port.uni_id)
394
395 if gem_port.direction == "upstream" or \
396 gem_port.direction == "bi-directional":
397 for i, p in enumerate(reversed(gem_port.pbit_map)):
398 if p == '1':
399 gem_entity_ids[i] = gem_port.entity_id
400 elif gem_port.direction == "downstream":
401 # Downstream gem port p-bit mapper is inverse of upstream
402 # TODO: Could also be a case of multicast. Not supported for now
403 pass
404
405 msg = Ieee8021pMapperServiceProfileFrame(
406 self._ieee_mapper_service_profile_entity_id + self._uni_port.mac_bridge_port_num, # 802.1p mapper Service Mapper Profile ID
407 interwork_tp_pointers=gem_entity_ids # Interworking TP IDs
408 )
409 frame = msg.set()
410 self.log.debug('openomci-msg', omci_msg=msg)
411 results = yield omci_cc.send(frame)
412 self.check_status_and_state(results, 'set-8021p-mapper-service-profile-ul')
413
414 ################################################################################
415 # Create Extended VLAN Tagging Operation config (PON-side)
416 #
417 # EntityID relates to the VLAN TCIS
418 # References:
419 # - VLAN TCIS from previously created VLAN Tagging filter data
420 # - PPTP Ethernet or VEIP UNI
421 #
422
423 # TODO: do this for all uni/ports...
424 # TODO: magic. static variable for assoc_type
425
426 # default to PPTP
Matt Jeanneretaf7cd6b2019-04-05 15:37:34 -0400427 if self._uni_port.type.value == UniType.VEIP.value:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500428 association_type = 10
Matt Jeanneretaf7cd6b2019-04-05 15:37:34 -0400429 elif self._uni_port.type.value == UniType.PPTP.value:
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500430 association_type = 2
431 else:
432 association_type = 2
433
434 attributes = dict(
435 association_type=association_type, # Assoc Type, PPTP/VEIP Ethernet UNI
436 associated_me_pointer=self._uni_port.entity_id, # Assoc ME, PPTP/VEIP Entity Id
437
438 # See VOL-1311 - Need to set table during create to avoid exception
439 # trying to read back table during post-create-read-missing-attributes
440 # But, because this is a R/W attribute. Some ONU may not accept the
441 # value during create. It is repeated again in a set below.
442 input_tpid=self._input_tpid, # input TPID
443 output_tpid=self._output_tpid, # output TPID
444 )
445
446 msg = ExtendedVlanTaggingOperationConfigurationDataFrame(
447 self._mac_bridge_service_profile_entity_id + self._uni_port.mac_bridge_port_num, # Bridge Entity ID
448 attributes=attributes
449 )
450
451 frame = msg.create()
452 self.log.debug('openomci-msg', omci_msg=msg)
453 results = yield omci_cc.send(frame)
454 self.check_status_and_state(results, 'create-extended-vlan-tagging-operation-configuration-data')
455
456 attributes = dict(
457 # Specifies the TPIDs in use and that operations in the downstream direction are
458 # inverse to the operations in the upstream direction
459 input_tpid=self._input_tpid, # input TPID
460 output_tpid=self._output_tpid, # output TPID
461 downstream_mode=0, # inverse of upstream
462 )
463
464 msg = ExtendedVlanTaggingOperationConfigurationDataFrame(
465 self._mac_bridge_service_profile_entity_id + self._uni_port.mac_bridge_port_num, # Bridge Entity ID
466 attributes=attributes
467 )
468
469 frame = msg.set()
470 self.log.debug('openomci-msg', omci_msg=msg)
471 results = yield omci_cc.send(frame)
472 self.check_status_and_state(results, 'set-extended-vlan-tagging-operation-configuration-data')
473
474 attributes = dict(
475 # parameters: Entity Id ( 0x900), Filter Inner Vlan Id(0x1000-4096,do not filter on Inner vid,
476 # Treatment Inner Vlan Id : 2
477
478 # Update uni side extended vlan filter
479 # filter for untagged
480 # probably for eapol
481 # TODO: lots of magic
482 # TODO: magic 0x1000 / 4096?
483 received_frame_vlan_tagging_operation_table=
484 VlanTaggingOperation(
485 filter_outer_priority=15, # This entry is not a double-tag rule
486 filter_outer_vid=4096, # Do not filter on the outer VID value
487 filter_outer_tpid_de=0, # Do not filter on the outer TPID field
488
489 filter_inner_priority=15,
490 filter_inner_vid=4096,
491 filter_inner_tpid_de=0,
492 filter_ether_type=0,
493
494 treatment_tags_to_remove=0,
495 treatment_outer_priority=15,
496 treatment_outer_vid=0,
497 treatment_outer_tpid_de=0,
498
499 treatment_inner_priority=0,
500 treatment_inner_vid=self._cvid,
501 treatment_inner_tpid_de=4,
502 )
503 )
504
505 msg = ExtendedVlanTaggingOperationConfigurationDataFrame(
506 self._mac_bridge_service_profile_entity_id + self._uni_port.mac_bridge_port_num, # Bridge Entity ID
507 attributes=attributes
508 )
509
510 frame = msg.set()
511 self.log.debug('openomci-msg', omci_msg=msg)
512 results = yield omci_cc.send(frame)
513 self.check_status_and_state(results, 'set-extended-vlan-tagging-operation-configuration-data-table')
514
515 self.deferred.callback("tech-profile-download-success")
516
517 except TimeoutError as e:
518 self.log.warn('rx-timeout-2', e=e)
519 self.deferred.errback(failure.Failure(e))
520
521 except Exception as e:
522 self.log.exception('omci-setup-2', e=e)
523 self.deferred.errback(failure.Failure(e))