2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.toscaparser.api.elements;
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
29 import org.onap.sdc.toscaparser.api.extensions.ExtTools;
31 public class TypeValidation {
33 private static final String DEFINITION_VERSION = "tosca_definitions_version";
34 private static final String DESCRIPTION = "description";
35 private static final String IMPORTS = "imports";
36 private static final String DSL_DEFINITIONS = "dsl_definitions";
37 private static final String NODE_TYPES = "node_types";
38 private static final String REPOSITORIES = "repositories";
39 private static final String DATA_TYPES = "data_types";
40 private static final String ARTIFACT_TYPES = "artifact_types";
41 private static final String GROUP_TYPES = "group_types";
42 private static final String RELATIONSHIP_TYPES = "relationship_types";
43 private static final String CAPABILITY_TYPES = "capability_types";
44 private static final String INTERFACE_TYPES = "interface_types";
45 private static final String POLICY_TYPES = "policy_types";
46 private static final String TOPOLOGY_TEMPLATE = "topology_template";
48 private static final String METADATA = "metadata";
50 private String ALLOWED_TYPE_SECTIONS[] = {
51 DEFINITION_VERSION, DESCRIPTION, IMPORTS,
52 DSL_DEFINITIONS, NODE_TYPES, REPOSITORIES,
53 DATA_TYPES, ARTIFACT_TYPES, GROUP_TYPES,
54 RELATIONSHIP_TYPES, CAPABILITY_TYPES,
55 INTERFACE_TYPES, POLICY_TYPES,
56 TOPOLOGY_TEMPLATE, METADATA
59 private static ArrayList<String> VALID_TEMPLATE_VERSIONS = _getVTV();
61 private static ArrayList<String> _getVTV() {
62 ArrayList<String> vtv = new ArrayList<>();
63 vtv.add("tosca_simple_yaml_1_0");
64 vtv.add("tosca_simple_yaml_1_1");
65 ExtTools exttools = new ExtTools();
66 vtv.addAll(exttools.getVersions());
70 //private LinkedHashMap<String,Object> customTypes;
71 private Object importDef;
72 //private String version;
74 public TypeValidation(LinkedHashMap<String, Object> _customTypes,
76 importDef = _importDef;
77 _validateTypeKeys(_customTypes);
80 private void _validateTypeKeys(LinkedHashMap<String, Object> customTypes) {
82 String sVersion = (String) customTypes.get(DEFINITION_VERSION);
83 if (sVersion != null) {
84 _validateTypeVersion(sVersion);
87 for (String name : customTypes.keySet()) {
88 boolean bFound = false;
89 for (String ats : ALLOWED_TYPE_SECTIONS) {
90 if (name.equals(ats)) {
96 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE138", String.format(
97 "UnknownFieldError: Template \"%s\" contains unknown field \"%s\"",
98 importDef.toString(), name)));
103 private void _validateTypeVersion(String sVersion) {
104 boolean bFound = false;
106 for (String atv : VALID_TEMPLATE_VERSIONS) {
107 allowed += "\"" + atv + "\" ";
108 if (sVersion.equals(atv)) {
114 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE139", String.format(
115 "InvalidTemplateVersion: version \"%s\" in \"%s\" is not supported\n" +
116 "Allowed versions: [%s]",
117 sVersion, importDef.toString(), allowed)));
124 from toscaparser.common.exception import ValidationIssueCollector
125 from toscaparser.common.exception import InvalidTemplateVersion
126 from toscaparser.common.exception import UnknownFieldError
127 from toscaparser.extensions.exttools import ExtTools
130 class TypeValidation(object):
132 ALLOWED_TYPE_SECTIONS = (DEFINITION_VERSION, DESCRIPTION, IMPORTS,
133 DSL_DEFINITIONS, NODE_TYPES, REPOSITORIES,
134 DATA_TYPES, ARTIFACT_TYPES, GROUP_TYPES,
135 RELATIONSHIP_TYPES, CAPABILITY_TYPES,
136 INTERFACE_TYPES, POLICY_TYPES,
137 TOPOLOGY_TEMPLATE) = \
138 ('tosca_definitions_version', 'description', 'imports',
139 'dsl_definitions', 'node_types', 'repositories',
140 'data_types', 'artifact_types', 'group_types',
141 'relationship_types', 'capability_types',
142 'interface_types', 'policy_types', 'topology_template')
143 VALID_TEMPLATE_VERSIONS = ['tosca_simple_yaml_1_0']
144 exttools = ExtTools()
145 VALID_TEMPLATE_VERSIONS.extend(exttools.get_versions())
147 def __init__(self, custom_types, import_def):
148 self.import_def = import_def
149 self._validate_type_keys(custom_types)
151 def _validate_type_keys(self, custom_type):
152 version = custom_type[self.DEFINITION_VERSION] \
153 if self.DEFINITION_VERSION in custom_type \
156 self._validate_type_version(version)
157 self.version = version
159 for name in custom_type:
160 if name not in self.ALLOWED_TYPE_SECTIONS:
161 ValidationIssueCollector.appendException(
162 # UnknownFieldError(what='Template ' + (self.import_def),
163 UnknownFieldError(what= (self.import_def),
166 def _validate_type_version(self, version):
167 if version not in self.VALID_TEMPLATE_VERSIONS:
168 ValidationIssueCollector.appendException(
169 InvalidTemplateVersion(
170 # what=version + ' in ' + self.import_def,
171 what=self.import_def,
172 valid_versions=', '. join(self.VALID_TEMPLATE_VERSIONS)))