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,
24 TARGETS, TRIGGERS, TYPE) = \
25 ('derived_from', 'metadata', 'properties', 'version',
26 'description', 'targets', 'triggers', 'type')
28 def __init__(self, ptype, custom_def=None):
29 super(PolicyType, self).__init__(ptype, self.POLICY_PREFIX,
32 self.custom_def = custom_def
36 if self.METADATA in self.defs:
37 self.meta_data = self.defs[self.METADATA]
38 self._validate_metadata(self.meta_data)
40 self.properties = None
41 if self.PROPERTIES in self.defs:
42 self.properties = self.defs[self.PROPERTIES]
43 self.parent_policies = self._get_parent_policies()
45 self.policy_version = None
46 if self.VERSION in self.defs:
47 self.policy_version = TOSCAVersionProperty(
48 self.defs[self.VERSION]).get_version()
50 self.policy_description = self.defs[self.DESCRIPTION] \
51 if self.DESCRIPTION in self.defs else None
53 self.targets_list = None
54 if self.TARGETS in self.defs:
55 self.targets_list = self.defs[self.TARGETS]
56 self._validate_targets(self.targets_list, custom_def)
58 def _get_parent_policies(self):
60 parent_policy = self.parent_type.type if self.parent_type else None
62 while parent_policy != 'tosca.policies.Root':
63 policies[parent_policy] = self.TOSCA_DEF[parent_policy]
64 parent_policy = policies[parent_policy]['derived_from']
68 def parent_type(self):
69 '''Return a policy statefulentity of this node is derived from.'''
70 if not hasattr(self, 'defs'):
72 ppolicy_entity = self.derived_from(self.defs)
74 return PolicyType(ppolicy_entity, self.custom_def)
76 def get_policy(self, name):
77 '''Return the definition of a policy field by name.'''
79 return self.defs[name]
84 return self.targets_list
87 def description(self):
88 return self.policy_description
92 return self.policy_version
94 def _validate_keys(self):
95 for key in self.defs.keys():
96 if key not in self.SECTIONS:
97 ExceptionCollector.appendException(
98 UnknownFieldError(what='Policy "%s"' % self.type,
101 def _validate_targets(self, targets_list, custom_def):
102 for nodetype in targets_list:
103 if nodetype not in custom_def:
104 ExceptionCollector.appendException(
105 InvalidTypeError(what='"%s" defined in targets for '
106 'policy "%s"' % (nodetype, self.type)))
108 def _validate_metadata(self, meta_data):
109 if not meta_data.get('type') in ['map', 'tosca:map']:
110 ExceptionCollector.appendException(
111 InvalidTypeError(what='"%s" defined in policy for '
112 'metadata' % (meta_data.get('type'))))
114 for entry_schema, entry_schema_type in meta_data.items():
115 if isinstance(entry_schema_type, dict) and not \
116 entry_schema_type.get('type') == 'string':
117 ExceptionCollector.appendException(
118 InvalidTypeError(what='"%s" defined in policy for '
120 % (entry_schema_type.get('type'),