summaryrefslogtreecommitdiffstats
path: root/tosca2heat/heat-translator/translator/hot/tosca/tosca_policies_monitoring.py
blob: 7aac1c7254de61f4470616de5bc898d1a8e8881b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import logging
from toscaparser.common.exception import InvalidPropertyValueError
from translator.hot.syntax.hot_resource import HotResource

# Name used to dynamically load appropriate map class.
TARGET_CLASS_NAME = 'ToscaMonitoring'

log = logging.getLogger('heat-translator')

ALARM_STATISTIC = {'average': 'avg', 'summary': 'sum',
                   'maximum': 'max', 'minimum': 'min'}


class ToscaMonitoring(HotResource):
    '''Translate TOSCA node type tosca.policies.Monitoring'''

    toscatype = 'tosca.policies.Monitoring'

    def __init__(self, policy, csar_dir=None):
        hot_type = "OS::Aodh::Alarm"
        super(ToscaMonitoring, self).__init__(policy,
                                              type=hot_type,
                                              csar_dir=csar_dir)
        self.policy = policy
        self.filter = list()

    def handle_expansion(self):
        '''handle monitoring resources in case of multiple triggers'''
        extra_resources = list()
        extra_triggers = self.policy.entity_tpl["triggers"]
        for trigger_name, trigger_dict in extra_triggers.items():
            if trigger_name not in self.filter:
                self.filter.append(trigger_name)
                prop = self._get_monitoring_prop(trigger_dict)
                mon_resources = HotResource(self.nodetemplate,
                                            type='OS::Aodh::Alarm',
                                            name=trigger_name,
                                            properties=prop)
                extra_resources.append(mon_resources)
        return extra_resources

    def handle_properties(self):
        self.properties = {}
        if self.policy.entity_tpl.get('triggers'):
            triggers = self.policy.entity_tpl["triggers"]
            trigger_name, trigger_dict = list(triggers.items())[0]
            self.filter.append(trigger_name)
            self.properties = self._get_monitoring_prop(trigger_dict)
            self.name = trigger_name
        return self.properties

    def _get_monitoring_prop(self, trigger):
        sample = trigger.get('condition')
        prop = dict()
        prop["description"] = sample.get('constraint')
        prop["meter_name"] = trigger.get('meter_name')
        if sample.get('method') not in ALARM_STATISTIC:
            msg = _('method should be one of given statistics')
            log.error(msg)
            raise InvalidPropertyValueError(what=msg)
        prop["statistic"] = ALARM_STATISTIC[sample["method"]]
        prop["period"] = sample.get("period")
        prop["threshold"] = sample.get("threshold")
        prop["comparison_operator"] = sample.get('comparison_operator')
        prop['evaluation_periods'] = sample.get('evaluations')
        return prop

    def embed_substack_templates(self, hot_template_version):
        pass