blob: aecefc129f4557740f8ed58ed5c931123e98b04e [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 -050016
17from __future__ import absolute_import
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050018from pyvoltha.adapters.extensions.omci.tasks.task import Task
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050019from twisted.internet import reactor
20from twisted.internet.defer import inlineCallbacks, failure, returnValue
Matt Jeanneret72f96fc2019-02-11 10:53:05 -050021from pyvoltha.adapters.extensions.omci.omci_defs import ReasonCodes, EntityOperations
22from pyvoltha.adapters.extensions.omci.omci_me import OntGFrame
23from pyvoltha.adapters.extensions.omci.omci_me import PptpEthernetUniFrame, VeipUniFrame
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050024
25RC = ReasonCodes
26OP = EntityOperations
27
28
29class BrcmUniLockException(Exception):
30 pass
31
32
33class BrcmUniLockTask(Task):
34 """
35 Lock or unlock all discovered UNI/PPTP on the ONU
36 """
37 task_priority = 200
38 name = "Broadcom UNI Lock Task"
39
40 def __init__(self, omci_agent, device_id, lock=True, priority=task_priority):
41 """
42 Class initialization
43
44 :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
45 :param device_id: (str) ONU Device ID
46 :param lock: (bool) If true administratively lock all the UNI. If false unlock
47 :param priority: (int) OpenOMCI Task priority (0..255) 255 is the highest
48 """
49 super(BrcmUniLockTask, self).__init__(BrcmUniLockTask.name,
50 omci_agent,
51 device_id,
52 priority=priority,
53 exclusive=True)
54 self._device = omci_agent.get_device(device_id)
55 self._lock = lock
56 self._results = None
57 self._local_deferred = None
58 self._config = self._device.configuration
59
60 def cancel_deferred(self):
61 super(BrcmUniLockTask, self).cancel_deferred()
62
63 d, self._local_deferred = self._local_deferred, None
64 try:
65 if d is not None and not d.called:
66 d.cancel()
67 except:
68 pass
69
70 def start(self):
71 """
72 Start UNI/PPTP Lock/Unlock Task
73 """
74 super(BrcmUniLockTask, self).start()
75 self._local_deferred = reactor.callLater(0, self.perform_lock)
76
77
78 @inlineCallbacks
79 def perform_lock(self):
80 """
81 Perform the lock/unlock
82 """
83 self.log.info('setting-uni-lock-state', lock=self._lock)
84
85 try:
86 state = 1 if self._lock else 0
87
88 # lock the whole ont and all the pptp. some onu dont causing odd behavior.
Matt Jeanneret142a6012019-10-26 12:22:04 -040089 pptp_list = sorted(self._config.pptp_entities) if self._config.pptp_entities else []
90 veip_list = sorted(self._config.veip_entities) if self._config.veip_entities else []
91
92 for entity_id in pptp_list:
93 pptp_value = self._config.pptp_entities[entity_id]
94 msg = PptpEthernetUniFrame(entity_id,
95 attributes=dict(administrative_state=state))
96 yield self._send_uni_lock_msg(entity_id, pptp_value, msg)
97
98 for entity_id in veip_list:
99 veip_value = self._config.veip_entities[entity_id]
100 msg = VeipUniFrame(entity_id,
101 attributes=dict(administrative_state=state))
102 yield self._send_uni_lock_msg(entity_id, veip_value, msg)
103
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500104 msg = OntGFrame(attributes={'administrative_state': state})
105 frame = msg.set()
106 self.log.debug('openomci-msg', omci_msg=msg)
107 results = yield self._device.omci_cc.send(frame)
108 self.strobe_watchdog()
109
110 status = results.fields['omci_message'].fields['success_code']
111 self.log.info('response-status', status=status)
112
113 # Success?
114 if status in (RC.Success.value, RC.InstanceExists):
115 self.log.debug('set-lock-ontg', lock=self._lock)
116 else:
117 self.log.warn('cannot-set-lock-ontg', lock=self._lock)
118
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500119 self.deferred.callback(self)
120
121 except Exception as e:
122 self.log.exception('setting-uni-lock-state', e=e)
123 self.deferred.errback(failure.Failure(e))
124
125
126 @inlineCallbacks
127 def _send_uni_lock_msg(self, entity_id, value, me_message):
128 frame = me_message.set()
129 self.log.debug('openomci-msg', omci_msg=me_message)
130 results = yield self._device.omci_cc.send(frame)
131 self.strobe_watchdog()
132
133 status = results.fields['omci_message'].fields['success_code']
134 self.log.info('response-status', status=status)
135
136 # Success?
137 if status in (RC.Success.value, RC.InstanceExists):
138 self.log.debug('set-lock-uni', uni=entity_id, value=value, lock=self._lock)
139 else:
140 self.log.warn('cannot-set-lock-uni', uni=entity_id, value=value, lock=self._lock)
141
142 returnValue(None)