Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / TypeValidation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api.elements;
22
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
25
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
28
29 import org.onap.sdc.toscaparser.api.extensions.ExtTools;
30
31 public class TypeValidation {
32
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";
47     //Pavel
48     private static final String METADATA = "metadata";
49
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
57     };
58
59     private static ArrayList<String> VALID_TEMPLATE_VERSIONS = _getVTV();
60
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());
67         return vtv;
68     }
69
70     //private LinkedHashMap<String,Object> customTypes;
71     private Object importDef;
72     //private String version;
73
74     public TypeValidation(LinkedHashMap<String, Object> _customTypes,
75                           Object _importDef) {
76         importDef = _importDef;
77         _validateTypeKeys(_customTypes);
78     }
79
80     private void _validateTypeKeys(LinkedHashMap<String, Object> customTypes) {
81
82         String sVersion = (String) customTypes.get(DEFINITION_VERSION);
83         if (sVersion != null) {
84             _validateTypeVersion(sVersion);
85             //version = sVersion;
86         }
87         for (String name : customTypes.keySet()) {
88             boolean bFound = false;
89             for (String ats : ALLOWED_TYPE_SECTIONS) {
90                 if (name.equals(ats)) {
91                     bFound = true;
92                     break;
93                 }
94             }
95             if (!bFound) {
96                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE138", String.format(
97                         "UnknownFieldError: Template \"%s\" contains unknown field \"%s\"",
98                         importDef.toString(), name)));
99             }
100         }
101     }
102
103     private void _validateTypeVersion(String sVersion) {
104         boolean bFound = false;
105         String allowed = "";
106         for (String atv : VALID_TEMPLATE_VERSIONS) {
107             allowed += "\"" + atv + "\" ";
108             if (sVersion.equals(atv)) {
109                 bFound = true;
110                 break;
111             }
112         }
113         if (!bFound) {
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)));
118         }
119     }
120 }
121
122 /*python
123
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
128
129
130 class TypeValidation(object):
131
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())
146
147     def __init__(self, custom_types, import_def):
148         self.import_def = import_def
149         self._validate_type_keys(custom_types)
150
151     def _validate_type_keys(self, custom_type):
152         version = custom_type[self.DEFINITION_VERSION] \
153             if self.DEFINITION_VERSION in custom_type \
154             else None
155         if version:
156             self._validate_type_version(version)
157             self.version = version
158
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),
164                                       field=name))
165
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)))    
173 */