blob: 2edd43cb0cc4a9532d5276ccc2b32a5cb8893637 [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 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
Matt Jeanneretf4113222019-08-14 19:44:34 -040090 if self._lock is True:
91 # lock unis first, ontg must be last
92 for entity_id in pptp_list:
93 msg = PptpEthernetUniFrame(entity_id,
94 attributes=dict(administrative_state=state))
95 self._send_omci_msg(msg)
Matt Jeanneret142a6012019-10-26 12:22:04 -040096
Matt Jeanneretf4113222019-08-14 19:44:34 -040097 for entity_id in veip_list:
98 msg = VeipUniFrame(entity_id,
99 attributes=dict(administrative_state=state))
100 self._send_omci_msg(msg)
Matt Jeanneret142a6012019-10-26 12:22:04 -0400101
Matt Jeanneretf4113222019-08-14 19:44:34 -0400102 msg = OntGFrame(attributes={'administrative_state': state})
103 self._send_omci_msg(msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500104 else:
Matt Jeanneretf4113222019-08-14 19:44:34 -0400105 # ontg must be unlocked first, then unis
106 msg = OntGFrame(attributes={'administrative_state': state})
107 self._send_omci_msg(msg)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500108
Matt Jeanneretf4113222019-08-14 19:44:34 -0400109 for entity_id in pptp_list:
110 msg = PptpEthernetUniFrame(entity_id,
111 attributes=dict(administrative_state=state))
112 self._send_omci_msg(msg)
113
114 for entity_id in veip_list:
115 msg = VeipUniFrame(entity_id,
116 attributes=dict(administrative_state=state))
117 self._send_omci_msg(msg)
118
119 self.deferred.callback('setting-uni-lock-state-finished')
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500120
121 except Exception as e:
122 self.log.exception('setting-uni-lock-state', e=e)
123 self.deferred.errback(failure.Failure(e))
124
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500125 @inlineCallbacks
Matt Jeanneretf4113222019-08-14 19:44:34 -0400126 def _send_omci_msg(self, me_message):
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500127 frame = me_message.set()
128 self.log.debug('openomci-msg', omci_msg=me_message)
129 results = yield self._device.omci_cc.send(frame)
130 self.strobe_watchdog()
131
132 status = results.fields['omci_message'].fields['success_code']
Matt Jeanneretf4113222019-08-14 19:44:34 -0400133 self.log.debug('response-status', status=status)
Matt Jeanneretf1e9c5d2019-02-08 07:41:29 -0500134
135 # Success?
Matt Jeanneretf4113222019-08-14 19:44:34 -0400136 if status not in (RC.Success.value, RC.InstanceExists):
137 raise BrcmUniLockException('openomci-set-failed')