blob: adf1ce24b3ff2912b5c780018eb29984c0c9c92b [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, failure
19from voltha.extensions.omci.omci_defs import ReasonCodes, EntityOperations
20from voltha.extensions.omci.omci_frame import OmciFrame
21from voltha.extensions.omci.omci_messages import OmciDelete
22
23RC = ReasonCodes
24OP = EntityOperations
25
26
27class DeletePMException(Exception):
28 pass
29
30
31class OmciDeletePMRequest(Task):
32 """
33 OpenOMCI routine to delete the requested PM Interval MEs
34 """
35 task_priority = Task.DEFAULT_PRIORITY
36 name = "ONU OMCI Delete PM ME Task"
37
38 def __init__(self, omci_agent, device_id, me_set, exclusive=False):
39 """
40 Class initialization
41
42 :param omci_agent: (OmciAdapterAgent) OMCI Adapter agent
43 :param me_set: (set) Tuples of class_id / entity_id to create
44 :param exclusive: (bool) True if this Create request Task exclusively own the
45 OMCI-CC while running. Default: False
46 """
47 super(OmciDeletePMRequest, self).__init__(OmciDeletePMRequest.name,
48 omci_agent,
49 device_id,
50 priority=OmciDeletePMRequest.task_priority,
51 exclusive=exclusive)
52 self._device = omci_agent.get_device(device_id)
53 self._me_tuples = me_set
54 self._local_deferred = None
55
56 def cancel_deferred(self):
57 super(OmciDeletePMRequest, 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 """ Start task """
68 super(OmciDeletePMRequest, self).start()
69 self._local_deferred = reactor.callLater(0, self.perform_delete)
70
71 @inlineCallbacks
72 def perform_delete(self):
73 """ Perform the delete requests """
74 self.log.debug('perform-delete')
75
76 try:
77 for me in self._me_tuples:
78 class_id = me[0]
79 entity_id = me[1]
80
81 frame = OmciFrame(
82 transaction_id=None,
83 message_type=OmciDelete.message_id,
84 omci_message=OmciDelete(
85 entity_class=class_id,
86 entity_id=entity_id
87 )
88 )
89 self.strobe_watchdog()
90 results = yield self._device.omci_cc.send(frame)
91
92 status = results.fields['omci_message'].fields['success_code']
93 self.log.debug('perform-delete-status', status=status)
94
95 # Did it fail, it instance does not exist, not an error
96 if status != RC.Success.value and status != RC.UnknownInstance.value:
97 msg = 'ME: {}, entity: {} failed with status {}'.format(class_id,
98 entity_id,
99 status)
100 raise DeletePMException(msg)
101
102 self.log.debug('delete-pm-success', class_id=class_id,
103 entity_id=entity_id)
104 self.deferred.callback(self)
105
106 except Exception as e:
107 self.log.exception('perform-create', e=e)
108 self.deferred.errback(failure.Failure(e))