Merge "Update tosca lib to version 0.5"
[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, TARGETS) = \
24                ('derived_from', 'metadata', 'properties', 'version',
25                 'description', 'targets')
26
27     def __init__(self, ptype, custom_def=None):
28         super(PolicyType, self).__init__(ptype, self.POLICY_PREFIX,
29                                          custom_def)
30         self.type = ptype
31         self._validate_keys()
32
33         self.meta_data = None
34         if self.METADATA in self.defs:
35             self.meta_data = self.defs[self.METADATA]
36             self._validate_metadata(self.meta_data)
37
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()
42
43         self.policy_version = None
44         if self.VERSION in self.defs:
45             self.policy_version = TOSCAVersionProperty(
46                 self.defs[self.VERSION]).get_version()
47
48         self.policy_description = self.defs[self.DESCRIPTION] \
49             if self.DESCRIPTION in self.defs else None
50
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)
55
56     def _get_parent_policies(self):
57         policies = {}
58         parent_policy = self.parent_type
59         if parent_policy:
60             while parent_policy != 'tosca.policies.Root':
61                 policies[parent_policy] = self.TOSCA_DEF[parent_policy]
62                 parent_policy = policies[parent_policy]['derived_from']
63         return policies
64
65     @property
66     def parent_type(self):
67         '''Return a policy this policy is derived from.'''
68         return self.derived_from(self.defs)
69
70     def get_policy(self, name):
71         '''Return the definition of a policy field by name.'''
72         if name in self.defs:
73             return self.defs[name]
74
75     @property
76     def targets(self):
77         '''Return targets.'''
78         return self.targets_list
79
80     @property
81     def description(self):
82         return self.policy_description
83
84     @property
85     def version(self):
86         return self.policy_version
87
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,
93                                       field=key))
94
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)))
101
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'))))
107
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 '
113                                      'metadata "%s"'
114                                      % (entry_schema_type.get('type'),
115                                         entry_schema)))