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