toscaparser: Support deriving from capability types of no property
[parser.git] / tosca2heat / tosca-parser / toscaparser / triggers.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13
14 import logging
15
16 from toscaparser.common.exception import ExceptionCollector
17 from toscaparser.common.exception import UnknownFieldError
18 from toscaparser.entity_template import EntityTemplate
19 from toscaparser.utils import validateutils
20
21 SECTIONS = (DESCRIPTION, EVENT, SCHEDULE, METER_NAME, METADATA,
22             TARGET_FILTER, CONDITION, ACTION) = \
23            ('description', 'event_type', 'schedule', 'meter_name',
24             'metadata', 'target_filter', 'condition', 'action')
25 CONDITION_KEYNAMES = (CONSTRAINT, PERIOD, EVALUATIONS, METHOD,
26                       THRESHOLD, COMPARISON_OPERATOR) = \
27                      ('constraint', 'period', 'evaluations',
28                       'method', 'threshold', 'comparison_operator')
29 log = logging.getLogger('tosca')
30
31
32 class Triggers(EntityTemplate):
33
34     '''Triggers defined in policies of topology template'''
35
36     def __init__(self, name, trigger_tpl):
37         self.name = name
38         self.trigger_tpl = trigger_tpl
39         self._validate_keys()
40         self._validate_condition()
41         self._validate_input()
42
43     def get_description(self):
44         return self.trigger_tpl['description']
45
46     def get_event(self):
47         return self.trigger_tpl['event_type']
48
49     def get_schedule(self):
50         return self.trigger_tpl['schedule']
51
52     def get_target_filter(self):
53         return self.trigger_tpl['target_filter']
54
55     def get_condition(self):
56         return self.trigger_tpl['condition']
57
58     def get_action(self):
59         return self.trigger_tpl['action']
60
61     def _validate_keys(self):
62         for key in self.trigger_tpl.keys():
63             if key not in SECTIONS:
64                 ExceptionCollector.appendException(
65                     UnknownFieldError(what='Triggers "%s"' % self.name,
66                                       field=key))
67
68     def _validate_condition(self):
69         for key in self.get_condition():
70             if key not in CONDITION_KEYNAMES:
71                 ExceptionCollector.appendException(
72                     UnknownFieldError(what='Triggers "%s"' % self.name,
73                                       field=key))
74
75     def _validate_input(self):
76         for key, value in self.get_condition().items():
77             if key in [PERIOD, EVALUATIONS]:
78                 validateutils.validate_integer(value)
79             elif key == THRESHOLD:
80                 validateutils.validate_numeric(value)
81             elif key in [METER_NAME, METHOD]:
82                 validateutils.validate_string(value)