blob: 1560c83a1289bb57ff0ffae5ab062a47eb45e91e [file] [log] [blame]
William Kurkian6f436d02019-02-06 16:25:01 -05001#
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 task import Task
17from twisted.internet import reactor
18from twisted.internet.defer import inlineCallbacks, TimeoutError, failure
19from voltha.extensions.omci.omci_me import OntDataFrame
20from voltha.extensions.omci.omci_defs import ReasonCodes as RC
21
22
23class GetMdsTask(Task):
24 """
25 OpenOMCI Get MIB Data Sync value task
26
27 On successful completion, this task will call the 'callback' method of the
28 deferred returned by the start method and return the value of the MIB
29 Data Sync attribute of the ONT Data ME
30 """
31 task_priority = Task.DEFAULT_PRIORITY
32 name = "Get MDS Task"
33
34 def __init__(self, omci_agent, device_id):
35 """
36 Class initialization
37
38 :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
39 :param device_id: (str) ONU Device ID
40 """
41 super(GetMdsTask, self).__init__(GetMdsTask.name,
42 omci_agent,
43 device_id,
44 priority=GetMdsTask.task_priority)
45 self._local_deferred = None
46
47 def cancel_deferred(self):
48 super(GetMdsTask, 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 MIB Synchronization tasks
60 """
61 super(GetMdsTask, self).start()
62 self._local_deferred = reactor.callLater(0, self.perform_get_mds)
63
64 def stop(self):
65 """
66 Shutdown MIB Synchronization tasks
67 """
68 self.log.debug('stopping')
69
70 self.cancel_deferred()
71 super(GetMdsTask, self).stop()
72
73 @inlineCallbacks
74 def perform_get_mds(self):
75 """
76 Get the 'mib_data_sync' attribute of the ONU
77 """
78 self.log.debug('perform-get-mds')
79
80 try:
81 device = self.omci_agent.get_device(self.device_id)
82
83 #########################################
84 # Request (MDS supplied value does not matter for a 'get' request)
85
86 self.strobe_watchdog()
87 results = yield device.omci_cc.send(OntDataFrame().get())
88
89 omci_msg = results.fields['omci_message'].fields
90 status = omci_msg['success_code']
91
92 # Note: Currently the data reported by the Scapy decode is 16-bits since we need
93 # the data field that large in order to support MIB and Alarm Upload Next
94 # commands. Select only the first 8-bits since that is the size of the MIB
95 # Data Sync attribute
96 mds = (omci_msg['data']['mib_data_sync'] >> 8) & 0xFF \
97 if 'data' in omci_msg and 'mib_data_sync' in omci_msg['data'] else -1
98
99 self.log.debug('ont-data-mds', status=status, mib_data_sync=mds)
100
101 assert status == RC.Success, 'Unexpected Response Status: {}'.format(status)
102
103 # Successful if here
104 self.deferred.callback(mds)
105
106 except TimeoutError as e:
107 self.log.warn('get-mds-timeout', e=e)
108 self.deferred.errback(failure.Failure(e))
109
110 except Exception as e:
111 self.log.exception('get-mds', e=e)
112 self.deferred.errback(failure.Failure(e))