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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
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
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')
32 class Triggers(EntityTemplate):
34 '''Triggers defined in policies of topology template'''
36 def __init__(self, name, trigger_tpl):
38 self.trigger_tpl = trigger_tpl
40 self._validate_condition()
41 self._validate_input()
43 def get_description(self):
44 return self.trigger_tpl['description']
47 return self.trigger_tpl['event_type']
49 def get_schedule(self):
50 return self.trigger_tpl['schedule']
52 def get_target_filter(self):
53 return self.trigger_tpl['target_filter']
55 def get_condition(self):
56 return self.trigger_tpl['condition']
59 return self.trigger_tpl['action']
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,
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,
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)