89a6a0319ea38604ddee9ab31804a6e69609e464
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / tosca_type_validation.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 InvalidTemplateVersion
15 from toscaparser.common.exception import UnknownFieldError
16 from toscaparser.extensions.exttools import ExtTools
17
18
19 class TypeValidation(object):
20
21     ALLOWED_TYPE_SECTIONS = (DEFINITION_VERSION, DESCRIPTION, IMPORTS,
22                              DSL_DEFINITIONS, NODE_TYPES, REPOSITORIES,
23                              DATA_TYPES, ARTIFACT_TYPES, GROUP_TYPES,
24                              RELATIONSHIP_TYPES, CAPABILITY_TYPES,
25                              INTERFACE_TYPES, POLICY_TYPES,
26                              TOPOLOGY_TEMPLATE, METADATA) = \
27         ('tosca_definitions_version', 'description', 'imports',
28          'dsl_definitions', 'node_types', 'repositories',
29          'data_types', 'artifact_types', 'group_types',
30          'relationship_types', 'capability_types',
31          'interface_types', 'policy_types', 'topology_template', 'metadata')
32     VALID_TEMPLATE_VERSIONS = ['tosca_simple_yaml_1_0',
33                                'tosca_simple_yaml_1_1']
34     exttools = ExtTools()
35     VALID_TEMPLATE_VERSIONS.extend(exttools.get_versions())
36
37     def __init__(self, custom_types, import_def):
38         self.import_def = import_def
39         self._validate_type_keys(custom_types)
40
41     def _validate_type_keys(self, custom_type):
42         version = custom_type[self.DEFINITION_VERSION] \
43             if self.DEFINITION_VERSION in custom_type \
44             else None
45         if version:
46             self._validate_type_version(version)
47             self.version = version
48
49         for name in custom_type:
50             if name not in self.ALLOWED_TYPE_SECTIONS:
51                 ExceptionCollector.appendException(
52                     UnknownFieldError(what='Template ' + (self.import_def),
53                                       field=name))
54
55     def _validate_type_version(self, version):
56         if version not in self.VALID_TEMPLATE_VERSIONS:
57             ExceptionCollector.appendException(
58                 InvalidTemplateVersion(
59                     what=version + ' in ' + self.import_def,
60                     valid_versions=', '. join(self.VALID_TEMPLATE_VERSIONS)))