netsted template validate type error
[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
20 SECTIONS = (DESCRIPTION, EVENT, SCHEDULE, TARGET_FILTER, CONDITION, ACTION) = \
21            ('description', 'event_type', 'schedule',
22             'target_filter', 'condition', 'action')
23 CONDITION_KEYNAMES = (CONTRAINT, PERIOD, EVALUATIONS, METHOD) = \
24                      ('constraint', 'period', 'evaluations', 'method')
25 log = logging.getLogger('tosca')
26
27
28 class Triggers(EntityTemplate):
29
30     '''Triggers defined in policies of topology template'''
31
32     def __init__(self, name, trigger_tpl):
33         self.name = name
34         self.trigger_tpl = trigger_tpl
35         self._validate_keys()
36         self._validate_condition()
37
38     def get_description(self):
39         return self.trigger_tpl['description']
40
41     def get_event(self):
42         return self.trigger_tpl['event_type']
43
44     def get_schedule(self):
45         return self.trigger_tpl['schedule']
46
47     def get_target_filter(self):
48         return self.trigger_tpl['target_filter']
49
50     def get_condition(self):
51         return self.trigger_tpl['condition']
52
53     def get_action(self):
54         return self.trigger_tpl['action']
55
56     def _validate_keys(self):
57         for key in self.trigger_tpl.keys():
58             if key not in SECTIONS:
59                 ExceptionCollector.appendException(
60                     UnknownFieldError(what='Triggers "%s"' % self.name,
61                                       field=key))
62
63     def _validate_condition(self):
64         for key in self.get_condition():
65             if key not in CONDITION_KEYNAMES:
66                 ExceptionCollector.appendException(
67                     UnknownFieldError(what='Triggers "%s"' % self.name,
68                                       field=key))