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.triggers import Triggers
20 from toscaparser.utils import validateutils
23 SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS) = \
24 ('type', 'metadata', 'description',
25 'properties', 'targets', 'triggers')
27 log = logging.getLogger('tosca')
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,
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))
48 return self.entity_tpl.get('targets')
51 def description(self):
52 return self.entity_tpl.get('description')
56 return self.entity_tpl.get('metadata')
58 def get_targets_type(self):
59 return self.targets_type
61 def get_targets_list(self):
62 return self.targets_list
64 def _triggers(self, triggers):
67 for name, trigger_tpl in triggers.items():
68 triggersObj = Triggers(name, trigger_tpl)
69 triggerObjs.append(triggersObj)
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,