Chip Boling | 32aab30 | 2019-01-23 10:50:18 -0600 | [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 | # |
| 16 | from task import Task |
| 17 | from enum import IntEnum |
| 18 | from twisted.internet import reactor |
| 19 | from twisted.internet.defer import inlineCallbacks, failure, TimeoutError |
| 20 | from voltha.extensions.omci.omci_defs import ReasonCodes, EntityOperations |
| 21 | from voltha.extensions.omci.omci_me import OntGFrame |
| 22 | from voltha.extensions.omci.omci_cc import DEFAULT_OMCI_TIMEOUT |
| 23 | |
| 24 | RC = ReasonCodes |
| 25 | OP = EntityOperations |
| 26 | |
| 27 | |
| 28 | class RebootException(Exception): |
| 29 | pass |
| 30 | |
| 31 | |
| 32 | class DeviceBusy(Exception): |
| 33 | pass |
| 34 | |
| 35 | |
| 36 | class RebootFlags(IntEnum): |
| 37 | Reboot_Unconditionally = 0, |
| 38 | Reboot_If_No_POTS_VoIP_In_Progress = 1, |
| 39 | Reboot_If_No_Emergency_Call_In_Progress = 2 |
| 40 | |
| 41 | |
| 42 | class OmciRebootRequest(Task): |
| 43 | """ |
| 44 | OpenOMCI routine to request reboot of an ONU |
| 45 | """ |
| 46 | task_priority = Task.MAX_PRIORITY |
| 47 | name = "ONU OMCI Reboot Task" |
| 48 | # adopt the global default |
| 49 | DEFAULT_REBOOT_TIMEOUT = DEFAULT_OMCI_TIMEOUT |
| 50 | |
| 51 | def __init__(self, omci_agent, device_id, |
| 52 | flags=RebootFlags.Reboot_Unconditionally, |
| 53 | timeout=DEFAULT_REBOOT_TIMEOUT): |
| 54 | """ |
| 55 | Class initialization |
| 56 | |
| 57 | :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent |
| 58 | :param flags: (RebootFlags) Reboot condition |
| 59 | """ |
| 60 | super(OmciRebootRequest, self).__init__(OmciRebootRequest.name, |
| 61 | omci_agent, |
| 62 | device_id, |
| 63 | priority=OmciRebootRequest.task_priority, |
| 64 | exclusive=True) |
| 65 | self._device = omci_agent.get_device(device_id) |
| 66 | self._flags = flags |
| 67 | self._timeout = timeout |
| 68 | self._local_deferred = None |
| 69 | |
| 70 | def cancel_deferred(self): |
| 71 | super(OmciRebootRequest, self).cancel_deferred() |
| 72 | |
| 73 | d, self._local_deferred = self._local_deferred, None |
| 74 | try: |
| 75 | if d is not None and not d.called: |
| 76 | d.cancel() |
| 77 | except: |
| 78 | pass |
| 79 | |
| 80 | def start(self): |
| 81 | """ Start task """ |
| 82 | super(OmciRebootRequest, self).start() |
| 83 | self._local_deferred = reactor.callLater(0, self.perform_reboot) |
| 84 | |
| 85 | @inlineCallbacks |
| 86 | def perform_reboot(self): |
| 87 | """ |
| 88 | Perform the reboot requests |
| 89 | |
| 90 | Depending on the ONU implementation, a response may not be returned. For this |
| 91 | reason, a timeout is considered successful. |
| 92 | """ |
| 93 | self.log.info('perform-reboot') |
| 94 | |
| 95 | try: |
| 96 | frame = OntGFrame().reboot(reboot_code=self._flags) |
| 97 | self.strobe_watchdog() |
| 98 | results = yield self._device.omci_cc.send(frame, timeout=self._timeout) |
| 99 | |
| 100 | status = results.fields['omci_message'].fields['success_code'] |
| 101 | self.log.debug('reboot-status', status=status) |
| 102 | |
| 103 | # Did it fail |
| 104 | if status != RC.Success.value: |
| 105 | if self._flags != RebootFlags.Reboot_Unconditionally and\ |
| 106 | status == RC.DeviceBusy.value: |
| 107 | raise DeviceBusy('ONU is busy, try again later') |
| 108 | else: |
| 109 | msg = 'Reboot request failed with status {}'.format(status) |
| 110 | raise RebootException(msg) |
| 111 | |
| 112 | self.log.info('reboot-success') |
| 113 | self.deferred.callback(self) |
| 114 | |
| 115 | except TimeoutError: |
| 116 | self.log.info('timeout', msg='Request timeout is not considered an error') |
| 117 | self.deferred.callback(None) |
| 118 | |
| 119 | except DeviceBusy as e: |
| 120 | self.log.warn('perform-reboot', msg=e) |
| 121 | self.deferred.errback(failure.Failure(e)) |
| 122 | |
| 123 | except Exception as e: |
| 124 | self.log.exception('perform-reboot', e=e) |
| 125 | self.deferred.errback(failure.Failure(e)) |