Merge "Update the nfv tosca definietion against the latest spec"
[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         ('tosca_definitions_version', 'description', 'imports',
27          'dsl_definitions', 'node_types', 'repositories',
28          'data_types', 'artifact_types', 'group_types',
29          'relationship_types', 'capability_types',
30          'interface_types', 'policy_types')
31     VALID_TEMPLATE_VERSIONS = ['tosca_simple_yaml_1_0']
32     exttools = ExtTools()
33     VALID_TEMPLATE_VERSIONS.extend(exttools.get_versions())
34
35     def __init__(self, custom_types, import_def):
36         self.import_def = import_def
37         self._validate_type_keys(custom_types)
38
39     def _validate_type_keys(self, custom_type):
40         version = custom_type[self.DEFINITION_VERSION] \
41             if self.DEFINITION_VERSION in custom_type \
42             else None
43         if version:
44             self._validate_type_version(version)
45             self.version = version
46
47         for name in custom_type:
48             if name not in self.ALLOWED_TYPE_SECTIONS:
49                 ExceptionCollector.appendException(
50                     UnknownFieldError(what='Template ' + (self.import_def),
51                                       field=name))
52
53     def _validate_type_version(self, version):
54         if version not in self.VALID_TEMPLATE_VERSIONS:
55             ExceptionCollector.appendException(
56                 InvalidTemplateVersion(
57                     what=version + ' in ' + self.import_def,
58                     valid_versions=', '. join(self.VALID_TEMPLATE_VERSIONS)))