blob: 8e9a2e10079e3133bb4af2ecb161ab32e3ff4ffd [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,
Girish Gowdrae933cd32019-11-21 21:04:41 +053050 omci_agent,
51 device_id,
52 priority=priority,
53 exclusive=True)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050054 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
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -050077 @inlineCallbacks
78 def perform_lock(self):
79 """
80 Perform the lock/unlock
81 """
82 self.log.info('setting-uni-lock-state', lock=self._lock)
83
84 try:
85 state = 1 if self._lock else 0
86
87 # lock the whole ont and all the pptp. some onu dont causing odd behavior.
Matt Jeanneret142a6012019-10-26 12:22:04 -040088 pptp_list = sorted(self._config.pptp_entities) if self._config.pptp_entities else []
89 veip_list = sorted(self._config.veip_entities) if self._config.veip_entities else []
90
91 for entity_id in pptp_list:
92 pptp_value = self._config.pptp_entities[entity_id]
93 msg = PptpEthernetUniFrame(entity_id,
94 attributes=dict(administrative_state=state))
95 yield self._send_uni_lock_msg(entity_id, pptp_value, msg)
96
97 for entity_id in veip_list:
98 veip_value = self._config.veip_entities[entity_id]
99 msg = VeipUniFrame(entity_id,
Girish Gowdrae933cd32019-11-21 21:04:41 +0530100 attributes=dict(administrative_state=state))
Matt Jeanneret142a6012019-10-26 12:22:04 -0400101 yield self._send_uni_lock_msg(entity_id, veip_value, msg)
102
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500103 msg = OntGFrame(attributes={'administrative_state': state})
104 frame = msg.set()
105 self.log.debug('openomci-msg', omci_msg=msg)
106 results = yield self._device.omci_cc.send(frame)
107 self.strobe_watchdog()
108
109 status = results.fields['omci_message'].fields['success_code']
110 self.log.info('response-status', status=status)
111
112 # Success?
113 if status in (RC.Success.value, RC.InstanceExists):
114 self.log.debug('set-lock-ontg', lock=self._lock)
115 else:
116 self.log.warn('cannot-set-lock-ontg', lock=self._lock)
117
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500118 self.deferred.callback(self)
119
120 except Exception as e:
121 self.log.exception('setting-uni-lock-state', e=e)
122 self.deferred.errback(failure.Failure(e))
123
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500124 @inlineCallbacks
125 def _send_uni_lock_msg(self, entity_id, value, me_message):
126 frame = me_message.set()
127 self.log.debug('openomci-msg', omci_msg=me_message)
128 results = yield self._device.omci_cc.send(frame)
129 self.strobe_watchdog()
130
131 status = results.fields['omci_message'].fields['success_code']
132 self.log.info('response-status', status=status)
133
134 # Success?
135 if status in (RC.Success.value, RC.InstanceExists):
136 self.log.debug('set-lock-uni', uni=entity_id, value=value, lock=self._lock)
137 else:
138 self.log.warn('cannot-set-lock-uni', uni=entity_id, value=value, lock=self._lock)
139
140 returnValue(None)