Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / parameters / Input.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.parameters;
22
23 import org.onap.sdc.toscaparser.api.DataEntity;
24 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
25 import org.onap.sdc.toscaparser.api.elements.EntityType;
26 import org.onap.sdc.toscaparser.api.elements.constraints.Constraint;
27 import org.onap.sdc.toscaparser.api.elements.constraints.Schema;
28 import org.onap.sdc.toscaparser.api.elements.enums.ToscaElementNames;
29 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
30
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.LinkedHashMap;
34 import java.util.Map;
35 import java.util.stream.Collectors;
36
37 public class Input {
38
39     private static final String TYPE = "type";
40     private static final String DESCRIPTION = "description";
41     private static final String DEFAULT = "default";
42     private static final String CONSTRAINTS = "constraints";
43     private static final String REQUIRED = "required";
44     private static final String STATUS = "status";
45     private static final String ENTRY_SCHEMA = "entry_schema";
46
47     public static final String INTEGER = "integer";
48     public static final String STRING = "string";
49     public static final String BOOLEAN = "boolean";
50     public static final String FLOAT = "float";
51     public static final String LIST = "list";
52     public static final String MAP = "map";
53     public static final String JSON = "json";
54
55     private static String[] inputField = {
56             TYPE, DESCRIPTION, DEFAULT, CONSTRAINTS, REQUIRED, STATUS, ENTRY_SCHEMA
57     };
58
59     private static String[] primitiveTypes = {
60             INTEGER, STRING, BOOLEAN, FLOAT, LIST, MAP, JSON
61     };
62
63     private String name;
64     private Schema schema;
65     private LinkedHashMap<String, Object> customDefs;
66     private Map<String, Annotation> annotations;
67
68     public Input() {
69     }
70
71     public Input(String name, LinkedHashMap<String, Object> schema, LinkedHashMap<String, Object> customDefinitions) {
72         this.name = name;
73         this.schema = new Schema(name, schema);
74         customDefs = customDefinitions;
75     }
76
77     @SuppressWarnings("unchecked")
78     public void parseAnnotations() {
79         if (schema.getSchema() != null) {
80             LinkedHashMap<String, Object> annotations = (LinkedHashMap<String, Object>) schema.getSchema().get(ToscaElementNames.ANNOTATIONS.getName());
81             if (annotations != null) {
82                 setAnnotations(annotations.entrySet().stream()
83                         .map(Annotation::new)
84                         .filter(Annotation::isHeatSourceType)
85                         .collect(Collectors.toMap(Annotation::getName, a -> a)));
86             }
87         }
88     }
89
90     public String getName() {
91         return name;
92     }
93
94     public String getType() {
95         return schema.getType();
96     }
97
98     public String getDescription() {
99         return schema.getDescription();
100     }
101
102     public boolean isRequired() {
103         return schema.isRequired();
104     }
105
106     public Object getDefault() {
107         return schema.getDefault();
108     }
109
110     public ArrayList<Constraint> getConstraints() {
111         return schema.getConstraints();
112     }
113
114     public void validate(Object value) {
115         validateField();
116         validateType(getType());
117         if (value != null) {
118             validateValue(value);
119         }
120     }
121
122     private void validateField() {
123         for (String key : schema.getSchema().keySet()) {
124             boolean bFound = false;
125             for (String ifld : inputField) {
126                 if (key.equals(ifld)) {
127                     bFound = true;
128                     break;
129                 }
130             }
131             if (!bFound) {
132                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE214", String.format(
133                         "UnknownFieldError: Input \"%s\" contains unknown field \"%s\"",
134                         name, key)));
135             }
136         }
137     }
138
139     private void validateType(String inputType) {
140         boolean bFound = false;
141         for (String pt : Schema.PROPERTY_TYPES) {
142             if (pt.equals(inputType)) {
143                 bFound = true;
144                 break;
145             }
146         }
147
148         if (!bFound) {
149             if (customDefs.get(inputType) != null) {
150                 bFound = true;
151             }
152         }
153
154         if (!bFound) {
155             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE215", String.format(
156                     "ValueError: Invalid type \"%s\"", inputType)));
157         }
158     }
159
160     @SuppressWarnings("unchecked")
161     private void validateValue(Object value) {
162         Object datatype;
163         if (EntityType.TOSCA_DEF.get(getType()) != null) {
164             datatype = EntityType.TOSCA_DEF.get(getType());
165         } else if (EntityType.TOSCA_DEF.get(EntityType.DATATYPE_NETWORK_PREFIX + getType()) != null) {
166             datatype = EntityType.TOSCA_DEF.get(EntityType.DATATYPE_NETWORK_PREFIX + getType());
167         }
168
169         String type = getType();
170         // if it's one of the basic types DON'T look in customDefs
171         if (Arrays.asList(primitiveTypes).contains(type)) {
172             DataEntity.validateDatatype(getType(), value, null, customDefs, null);
173             return;
174         } else if (customDefs.get(getType()) != null) {
175             datatype = customDefs.get(getType());
176             DataEntity.validateDatatype(getType(), value, (LinkedHashMap<String, Object>) datatype, customDefs, null);
177             return;
178         }
179
180         DataEntity.validateDatatype(getType(), value, null, customDefs, null);
181     }
182
183     public Map<String, Annotation> getAnnotations() {
184         return annotations;
185     }
186
187     private void setAnnotations(Map<String, Annotation> annotations) {
188         this.annotations = annotations;
189     }
190
191     public void resetAnnotaions() {
192         annotations = null;
193     }
194
195     public LinkedHashMap<String, Object> getEntrySchema() {
196         return schema.getEntrySchema();
197     }
198
199 }