Add relationship of node template associated with tosca template in
[parser.git] / tosca2heat / tosca-parser / toscaparser / policy.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.triggers import Triggers
20 from toscaparser.utils import validateutils
21
22
23 SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS) = \
24            ('type', 'metadata', 'description',
25             'properties', 'targets', 'triggers')
26
27 log = logging.getLogger('tosca')
28
29
30 class Policy(EntityTemplate):
31     '''Policies defined in Topology template.'''
32     def __init__(self, name, policy, targets, targets_type, custom_def=None):
33         super(Policy, self).__init__(name,
34                                      policy,
35                                      'policy_type',
36                                      custom_def)
37         self.meta_data = None
38         if self.METADATA in policy:
39             self.meta_data = policy.get(self.METADATA)
40             validateutils.validate_map(self.meta_data)
41         self.targets_list = targets
42         self.targets_type = targets_type
43         self.triggers = self._triggers(policy.get(TRIGGERS))
44         self._validate_keys()
45
46     @property
47     def targets(self):
48         return self.entity_tpl.get('targets')
49
50     @property
51     def description(self):
52         return self.entity_tpl.get('description')
53
54     @property
55     def metadata(self):
56         return self.entity_tpl.get('metadata')
57
58     def get_targets_type(self):
59         return self.targets_type
60
61     def get_targets_list(self):
62         return self.targets_list
63
64     def _triggers(self, triggers):
65         triggerObjs = []
66         if triggers:
67             for name, trigger_tpl in triggers.items():
68                 triggersObj = Triggers(name, trigger_tpl)
69                 triggerObjs.append(triggersObj)
70         return triggerObjs
71
72     def _validate_keys(self):
73         for key in self.entity_tpl.keys():
74             if key not in SECTIONS:
75                 ExceptionCollector.appendException(
76                     UnknownFieldError(what='Policy "%s"' % self.name,
77                                       field=key))