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=None, targets_type=None,
34 super(Policy, self).__init__(name,
39 if self.METADATA in policy:
40 self.meta_data = policy.get(self.METADATA)
41 validateutils.validate_map(self.meta_data)
42 self.targets_list = targets
43 self.targets_type = targets_type
44 self.triggers = self._triggers(policy.get(TRIGGERS))
45 self.properties = None
46 if 'properties' in policy:
47 self.properties = policy['properties']
52 return self.entity_tpl.get('targets')
55 def description(self):
56 return self.entity_tpl.get('description')
60 return self.entity_tpl.get('metadata')
62 def get_targets_type(self):
63 return self.targets_type
65 def get_targets_list(self):
66 return self.targets_list
68 def _triggers(self, triggers):
71 for name, trigger_tpl in triggers.items():
72 triggersObj = Triggers(name, trigger_tpl)
73 triggerObjs.append(triggersObj)
76 def _validate_keys(self):
77 for key in self.entity_tpl.keys():
78 if key not in SECTIONS:
79 ExceptionCollector.appendException(
80 UnknownFieldError(what='Policy "%s"' % self.name,