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
13 from toscaparser.common.exception import ExceptionCollector
14 from toscaparser.common.exception import InvalidTypeError
15 from toscaparser.common.exception import UnknownFieldError
16 from toscaparser.elements.statefulentitytype import StatefulEntityType
17 from toscaparser.utils.validateutils import TOSCAVersionProperty
20 class PolicyType(StatefulEntityType):
22 '''TOSCA built-in policies type.'''
23 SECTIONS = (DERIVED_FROM, METADATA, PROPERTIES, VERSION, DESCRIPTION, TARGETS) = \
24 ('derived_from', 'metadata', 'properties', 'version',
25 'description', 'targets')
27 def __init__(self, ptype, custom_def=None):
28 super(PolicyType, self).__init__(ptype, self.POLICY_PREFIX,
34 if self.METADATA in self.defs:
35 self.meta_data = self.defs[self.METADATA]
36 self._validate_metadata(self.meta_data)
38 self.properties = None
39 if self.PROPERTIES in self.defs:
40 self.properties = self.defs[self.PROPERTIES]
41 self.parent_policies = self._get_parent_policies()
43 self.policy_version = None
44 if self.VERSION in self.defs:
45 self.policy_version = TOSCAVersionProperty(
46 self.defs[self.VERSION]).get_version()
48 self.policy_description = self.defs[self.DESCRIPTION] \
49 if self.DESCRIPTION in self.defs else None
51 self.targets_list = None
52 if self.TARGETS in self.defs:
53 self.targets_list = self.defs[self.TARGETS]
54 self._validate_targets(self.targets_list, custom_def)
56 def _get_parent_policies(self):
58 parent_policy = self.parent_type
60 while parent_policy != 'tosca.policies.Root':
61 policies[parent_policy] = self.TOSCA_DEF[parent_policy]
62 parent_policy = policies[parent_policy]['derived_from']
66 def parent_type(self):
67 '''Return a policy this policy is derived from.'''
68 return self.derived_from(self.defs)
70 def get_policy(self, name):
71 '''Return the definition of a policy field by name.'''
73 return self.defs[name]
78 return self.targets_list
81 def description(self):
82 return self.policy_description
86 return self.policy_version
88 def _validate_keys(self):
89 for key in self.defs.keys():
90 if key not in self.SECTIONS:
91 ExceptionCollector.appendException(
92 UnknownFieldError(what='Policy "%s"' % self.type,
95 def _validate_targets(self, targets_list, custom_def):
96 for nodetype in targets_list:
97 if nodetype not in custom_def:
98 ExceptionCollector.appendException(
99 InvalidTypeError(what='"%s" defined in targets for '
100 'policy "%s"' % (nodetype, self.type)))
102 def _validate_metadata(self, meta_data):
103 if not meta_data.get('type') in ['map', 'tosca:map']:
104 ExceptionCollector.appendException(
105 InvalidTypeError(what='"%s" defined in policy for '
106 'metadata' % (meta_data.get('type'))))
108 for entry_schema, entry_schema_type in meta_data.items():
109 if isinstance(entry_schema_type, dict) and not \
110 entry_schema_type.get('type') == 'string':
111 ExceptionCollector.appendException(
112 InvalidTypeError(what='"%s" defined in policy for '
114 % (entry_schema_type.get('type'),