Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 1 | # |
| 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 Jeanneret | 72f96fc | 2019-02-11 10:53:05 -0500 | [diff] [blame] | 16 | from pyvoltha.adapters.extensions.omci.tasks.task import Task |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 17 | from twisted.internet import reactor |
| 18 | from twisted.internet.defer import inlineCallbacks, failure, returnValue |
Matt Jeanneret | 72f96fc | 2019-02-11 10:53:05 -0500 | [diff] [blame] | 19 | from pyvoltha.adapters.extensions.omci.omci_defs import ReasonCodes, EntityOperations |
| 20 | from pyvoltha.adapters.extensions.omci.omci_me import OntGFrame |
| 21 | from pyvoltha.adapters.extensions.omci.omci_me import PptpEthernetUniFrame, VeipUniFrame |
Matt Jeanneret | f1e9c5d | 2019-02-08 07:41:29 -0500 | [diff] [blame] | 22 | |
| 23 | RC = ReasonCodes |
| 24 | OP = EntityOperations |
| 25 | |
| 26 | |
| 27 | class BrcmUniLockException(Exception): |
| 28 | pass |
| 29 | |
| 30 | |
| 31 | class 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. |
| 87 | msg = OntGFrame(attributes={'administrative_state': state}) |
| 88 | frame = msg.set() |
| 89 | self.log.debug('openomci-msg', omci_msg=msg) |
| 90 | results = yield self._device.omci_cc.send(frame) |
| 91 | self.strobe_watchdog() |
| 92 | |
| 93 | status = results.fields['omci_message'].fields['success_code'] |
| 94 | self.log.info('response-status', status=status) |
| 95 | |
| 96 | # Success? |
| 97 | if status in (RC.Success.value, RC.InstanceExists): |
| 98 | self.log.debug('set-lock-ontg', lock=self._lock) |
| 99 | else: |
| 100 | self.log.warn('cannot-set-lock-ontg', lock=self._lock) |
| 101 | |
| 102 | pptp_list = sorted(self._config.pptp_entities) if self._config.pptp_entities else [] |
| 103 | veip_list = sorted(self._config.veip_entities) if self._config.veip_entities else [] |
| 104 | |
| 105 | for entity_id in pptp_list: |
| 106 | pptp_value = self._config.pptp_entities[entity_id] |
| 107 | msg = PptpEthernetUniFrame(entity_id, |
| 108 | attributes=dict(administrative_state=state)) |
| 109 | self._send_uni_lock_msg(entity_id, pptp_value, msg) |
| 110 | |
| 111 | for entity_id in veip_list: |
| 112 | veip_value = self._config.veip_entities[entity_id] |
| 113 | msg = VeipUniFrame(entity_id, |
| 114 | attributes=dict(administrative_state=state)) |
| 115 | self._send_uni_lock_msg(entity_id, veip_value, msg) |
| 116 | |
| 117 | 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) |