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.parameters;
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;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.LinkedHashMap;
35 import java.util.stream.Collectors;
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";
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";
55 private static String[] inputField = {
56 TYPE, DESCRIPTION, DEFAULT, CONSTRAINTS, REQUIRED, STATUS, ENTRY_SCHEMA
59 private static String[] primitiveTypes = {
60 INTEGER, STRING, BOOLEAN, FLOAT, LIST, MAP, JSON
64 private Schema schema;
65 private LinkedHashMap<String, Object> customDefs;
66 private Map<String, Annotation> annotations;
71 public Input(String name, LinkedHashMap<String, Object> schema, LinkedHashMap<String, Object> customDefinitions) {
73 this.schema = new Schema(name, schema);
74 customDefs = customDefinitions;
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()
84 .filter(Annotation::isHeatSourceType)
85 .collect(Collectors.toMap(Annotation::getName, a -> a)));
90 public String getName() {
94 public String getType() {
95 return schema.getType();
98 public String getDescription() {
99 return schema.getDescription();
102 public boolean isRequired() {
103 return schema.isRequired();
106 public Object getDefault() {
107 return schema.getDefault();
110 public ArrayList<Constraint> getConstraints() {
111 return schema.getConstraints();
114 public void validate(Object value) {
116 validateType(getType());
118 validateValue(value);
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)) {
132 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE214", String.format(
133 "UnknownFieldError: Input \"%s\" contains unknown field \"%s\"",
139 private void validateType(String inputType) {
140 boolean bFound = false;
141 for (String pt : Schema.PROPERTY_TYPES) {
142 if (pt.equals(inputType)) {
149 if (customDefs.get(inputType) != null) {
155 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE215", String.format(
156 "ValueError: Invalid type \"%s\"", inputType)));
160 @SuppressWarnings("unchecked")
161 private void validateValue(Object value) {
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());
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);
174 } else if (customDefs.get(getType()) != null) {
175 datatype = customDefs.get(getType());
176 DataEntity.validateDatatype(getType(), value, (LinkedHashMap<String, Object>) datatype, customDefs, null);
180 DataEntity.validateDatatype(getType(), value, null, customDefs, null);
183 public Map<String, Annotation> getAnnotations() {
187 private void setAnnotations(Map<String, Annotation> annotations) {
188 this.annotations = annotations;
191 public void resetAnnotaions() {
195 public LinkedHashMap<String, Object> getEntrySchema() {
196 return schema.getEntrySchema();