Merge "netsted template validate type error"
[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) = \
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')
32     VALID_TEMPLATE_VERSIONS = ['tosca_simple_yaml_1_0']
33     exttools = ExtTools()
34     VALID_TEMPLATE_VERSIONS.extend(exttools.get_versions())
35
36     def __init__(self, custom_types, import_def):
37         self.import_def = import_def
38         self._validate_type_keys(custom_types)
39
40     def _validate_type_keys(self, custom_type):
41         version = custom_type[self.DEFINITION_VERSION] \
42             if self.DEFINITION_VERSION in custom_type \
43             else None
44         if version:
45             self._validate_type_version(version)
46             self.version = version
47
48         for name in custom_type:
49             if name not in self.ALLOWED_TYPE_SECTIONS:
50                 ExceptionCollector.appendException(
51                     UnknownFieldError(what='Template ' + (self.import_def),
52                                       field=name))
53
54     def _validate_type_version(self, version):
55         if version not in self.VALID_TEMPLATE_VERSIONS:
56             ExceptionCollector.appendException(
57                 InvalidTemplateVersion(
58                     what=version + ' in ' + self.import_def,
59                     valid_versions=', '. join(self.VALID_TEMPLATE_VERSIONS)))