blob: 7bafc17c95e956cb8160ac51ea230d177616b1c0 [file] [log] [blame]
Chip Boling67b674a2019-02-08 11:42:18 -06001#
2# Copyright 2017 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#
Zack Williams84a71e92019-11-15 09:00:19 -070016from __future__ import absolute_import
Chip Boling67b674a2019-02-08 11:42:18 -060017from pyvoltha.adapters.extensions.omci.tasks.task import Task
18from pyvoltha.common.utils.asleep import asleep
Zack Williams84a71e92019-11-15 09:00:19 -070019from twisted.internet.defer import inlineCallbacks, failure, AlreadyCalledError, CancelledError
Chip Boling67b674a2019-02-08 11:42:18 -060020from twisted.internet import reactor
21
22
23class SimpleTask(Task):
24 def __init__(self, omci_agent, device_id,
25 exclusive=True,
26 success=True,
27 delay=0,
28 value=None,
29 priority=Task.DEFAULT_PRIORITY,
30 watchdog_timeout=Task.DEFAULT_WATCHDOG_SECS):
31 """
32 Class initialization
33
34 :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
35 :param device_id: (str) ONU Device ID
36 :param exclusive: (bool) True if the task should run by itself
37 :param success: (bool) True if the task should complete successfully
38 :param delay: (int/float) Time it takes the task to complete
39 :param priority (int) Priority of the task
40 :param watchdog_timeout (int or float) Watchdog timeout after task start
41 :param value: (various) The value (string, int, ...) to return if successful
42 or an Exception to send to the errBack if 'success'
43 is False
44 """
45 super(SimpleTask, self).__init__('Simple Mock Task',
46 omci_agent,
47 device_id,
48 exclusive=exclusive,
49 priority=priority,
50 watchdog_timeout=watchdog_timeout)
51 self._delay = delay
52 self._success = success
53 self._value = value
54 self._local_deferred = None
55
56 def cancel_deferred(self):
57 super(SimpleTask, self).cancel_deferred()
58
59 d, self._local_deferred = self._local_deferred, None
60 try:
61 if d is not None and not d.called:
62 d.cancel()
63 except:
64 pass
65
66 def start(self):
67 """
68 Start MIB Synchronization tasks
69 """
70 super(SimpleTask, self).start()
71 self._local_deferred = reactor.callLater(0, self.perform_task)
72
73 def stop(self):
74 """
75 Shutdown MIB Synchronization tasks
76 """
77 self.cancel_deferred()
78 super(SimpleTask, self).stop()
79
80 @inlineCallbacks
81 def perform_task(self):
82 """
83 Get the 'mib_data_sync' attribute of the ONU
84 """
85 try:
86 if self._delay > 0:
87 yield asleep(self._delay)
88
89 if self._success:
90 self.deferred.callback(self._value)
Zack Williams84a71e92019-11-15 09:00:19 -070091 else:
92 self.deferred.errback(failure.Failure(self._value))
Chip Boling67b674a2019-02-08 11:42:18 -060093
Zack Williams84a71e92019-11-15 09:00:19 -070094 except AlreadyCalledError as all:
95 pass
Chip Boling67b674a2019-02-08 11:42:18 -060096 except Exception as e:
97 self.deferred.errback(failure.Failure(e))