Merge "Support version of tosca_simple_yaml_1_1"
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / policytype.py
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
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
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
11 #    under the License.
12
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
18
19
20 class PolicyType(StatefulEntityType):
21
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')
27
28     def __init__(self, ptype, custom_def=None):
29         super(PolicyType, self).__init__(ptype, self.POLICY_PREFIX,
30                                          custom_def)
31         self.type = ptype
32         self.custom_def = custom_def
33         self._validate_keys()
34
35         self.meta_data = None
36         if self.METADATA in self.defs:
37             self.meta_data = self.defs[self.METADATA]
38             self._validate_metadata(self.meta_data)
39
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()
44
45         self.policy_version = None
46         if self.VERSION in self.defs:
47             self.policy_version = TOSCAVersionProperty(
48                 self.defs[self.VERSION]).get_version()
49
50         self.policy_description = self.defs[self.DESCRIPTION] \
51             if self.DESCRIPTION in self.defs else None
52
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)
57
58     def _get_parent_policies(self):
59         policies = {}
60         parent_policy = self.parent_type.type if self.parent_type else None
61         if parent_policy:
62             while parent_policy != 'tosca.policies.Root':
63                 policies[parent_policy] = self.TOSCA_DEF[parent_policy]
64                 parent_policy = policies[parent_policy]['derived_from']
65         return policies
66
67     @property
68     def parent_type(self):
69         '''Return a policy statefulentity of this node is derived from.'''
70         if not hasattr(self, 'defs'):
71             return None
72         ppolicy_entity = self.derived_from(self.defs)
73         if ppolicy_entity:
74             return PolicyType(ppolicy_entity, self.custom_def)
75
76     def get_policy(self, name):
77         '''Return the definition of a policy field by name.'''
78         if name in self.defs:
79             return self.defs[name]
80
81     @property
82     def targets(self):
83         '''Return targets.'''
84         return self.targets_list
85
86     @property
87     def description(self):
88         return self.policy_description
89
90     @property
91     def version(self):
92         return self.policy_version
93
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,
99                                       field=key))
100
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)))
107
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'))))
113
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 '
119                                      'metadata "%s"'
120                                      % (entry_schema_type.get('type'),
121                                         entry_schema)))