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,
31 self.custom_def = custom_def
35 if self.METADATA in self.defs:
36 self.meta_data = self.defs[self.METADATA]
37 self._validate_metadata(self.meta_data)
39 self.properties = None
40 if self.PROPERTIES in self.defs:
41 self.properties = self.defs[self.PROPERTIES]
42 self.parent_policies = self._get_parent_policies()
44 self.policy_version = None
45 if self.VERSION in self.defs:
46 self.policy_version = TOSCAVersionProperty(
47 self.defs[self.VERSION]).get_version()
49 self.policy_description = self.defs[self.DESCRIPTION] \
50 if self.DESCRIPTION in self.defs else None
52 self.targets_list = None
53 if self.TARGETS in self.defs:
54 self.targets_list = self.defs[self.TARGETS]
55 self._validate_targets(self.targets_list, custom_def)
57 def _get_parent_policies(self):
59 parent_policy = self.parent_type.type if self.parent_type else None
61 while parent_policy != 'tosca.policies.Root':
62 policies[parent_policy] = self.TOSCA_DEF[parent_policy]
63 parent_policy = policies[parent_policy]['derived_from']
67 def parent_type(self):
68 '''Return a policy statefulentity of this node is derived from.'''
69 if not hasattr(self, 'defs'):
71 ppolicy_entity = self.derived_from(self.defs)
73 return PolicyType(ppolicy_entity, self.custom_def)
75 def get_policy(self, name):
76 '''Return the definition of a policy field by name.'''
78 return self.defs[name]
83 return self.targets_list
86 def description(self):
87 return self.policy_description
91 return self.policy_version
93 def _validate_keys(self):
94 for key in self.defs.keys():
95 if key not in self.SECTIONS:
96 ExceptionCollector.appendException(
97 UnknownFieldError(what='Policy "%s"' % self.type,
100 def _validate_targets(self, targets_list, custom_def):
101 for nodetype in targets_list:
102 if nodetype not in custom_def:
103 ExceptionCollector.appendException(
104 InvalidTypeError(what='"%s" defined in targets for '
105 'policy "%s"' % (nodetype, self.type)))
107 def _validate_metadata(self, meta_data):
108 if not meta_data.get('type') in ['map', 'tosca:map']:
109 ExceptionCollector.appendException(
110 InvalidTypeError(what='"%s" defined in policy for '
111 'metadata' % (meta_data.get('type'))))
113 for entry_schema, entry_schema_type in meta_data.items():
114 if isinstance(entry_schema_type, dict) and not \
115 entry_schema_type.get('type') == 'string':
116 ExceptionCollector.appendException(
117 InvalidTypeError(what='"%s" defined in policy for '
119 % (entry_schema_type.get('type'),