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