blob: b5b1dc95cd9e1bed5d0a85b9bf1cffc2965b8e6d [file] [log] [blame]
William Kurkian6f436d02019-02-06 16:25:01 -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#
16from task import Task
17from twisted.internet import reactor
18from twisted.internet.defer import inlineCallbacks, TimeoutError, failure
19from voltha.extensions.omci.omci_me import OntGFrame
20from voltha.extensions.omci.omci_defs import ReasonCodes as RC
21from datetime import datetime
22
23
24class SyncTimeTask(Task):
25 """
26 OpenOMCI - Synchronize the ONU time with server
27 """
28 task_priority = Task.DEFAULT_PRIORITY + 10
29 name = "Sync Time Task"
30
31 def __init__(self, omci_agent, device_id, use_utc=True):
32 """
33 Class initialization
34
35 :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
36 :param device_id: (str) ONU Device ID
37 :param use_utc: (bool) Use UTC time if True, otherwise local time
38 """
39 super(SyncTimeTask, self).__init__(SyncTimeTask.name,
40 omci_agent,
41 device_id,
42 priority=SyncTimeTask.task_priority,
43 exclusive=False)
44 self._local_deferred = None
45 self._use_utc = use_utc
46
47 def cancel_deferred(self):
48 super(SyncTimeTask, self).cancel_deferred()
49
50 d, self._local_deferred = self._local_deferred, None
51 try:
52 if d is not None and not d.called:
53 d.cancel()
54 except:
55 pass
56
57 def start(self):
58 """
59 Start the tasks
60 """
61 super(SyncTimeTask, self).start()
62 self._local_deferred = reactor.callLater(0, self.perform_sync_time)
63
64 def stop(self):
65 """
66 Shutdown the tasks
67 """
68 self.log.debug('stopping')
69
70 self.cancel_deferred()
71 super(SyncTimeTask, self).stop()
72
73 @inlineCallbacks
74 def perform_sync_time(self):
75 """
76 Sync the time
77 """
78 self.log.debug('perform-sync-time')
79
80 try:
81 device = self.omci_agent.get_device(self.device_id)
82
83 #########################################
84 # ONT-G (ME #256)
85 dt = datetime.utcnow() if self._use_utc else datetime.now()
86
87 results = yield device.omci_cc.send(OntGFrame().synchronize_time(dt))
88
89 omci_msg = results.fields['omci_message'].fields
90 status = omci_msg['success_code']
91 self.log.debug('sync-time', status=status)
92
93 if status == RC.Success:
94 self.log.info('sync-time', success_info=omci_msg['success_info'] & 0x0f)
95
96 assert status == RC.Success, 'Unexpected Response Status: {}'.format(status)
97
98 # Successful if here
99 self.deferred.callback(results)
100
101 except TimeoutError as e:
102 self.log.warn('sync-time-timeout', e=e)
103 self.deferred.errback(failure.Failure(e))
104
105 except Exception as e:
106 self.log.exception('sync-time', e=e)
107 self.deferred.errback(failure.Failure(e))