2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END============================================
\r
20 * ===================================================================
\r
24 package org.onap.clamp.clds.tosca.update.elements;
\r
26 import com.google.gson.JsonArray;
\r
27 import com.google.gson.JsonObject;
\r
28 import java.util.ArrayList;
\r
29 import java.util.LinkedHashMap;
\r
30 import java.util.Map.Entry;
\r
31 import org.onap.clamp.clds.tosca.update.templates.JsonTemplate;
\r
33 public class Constraint {
\r
35 private LinkedHashMap<String, Object> constraints;
\r
36 private JsonTemplate jsonTemplate;
\r
38 public Constraint(LinkedHashMap<String, Object> constraints, JsonTemplate jsonTemplate) {
\r
39 this.jsonTemplate = jsonTemplate;
\r
40 this.constraints = constraints;
\r
44 * Deploy the linkedhashmap which contains the constraints, to extract them one to one.
\r
46 * @param jsonSchema The json Schema
\r
47 * @param typeProperty The ype property
\r
48 * @return the json object
\r
50 public JsonObject deployConstraints(JsonObject jsonSchema, String typeProperty) {
\r
51 for (Entry<String, Object> constraint : constraints.entrySet()) {
\r
52 this.parseConstraint(jsonSchema, constraint.getKey(), constraint.getValue(), typeProperty);
\r
58 * Each case of Tosca constraints below parse specifically the field in the JsonObject.
\r
60 * @param jsonSchema Json Schema
\r
61 * @param nameConstraint Name constraint
\r
62 * @param valueConstraint value constraint
\r
63 * @param typeProperty Type Property
\r
65 @SuppressWarnings("unchecked")
\r
66 public void parseConstraint(JsonObject jsonSchema, String nameConstraint, Object valueConstraint,
\r
67 String typeProperty) {
\r
68 switch (nameConstraint) {
\r
70 checkTemplateField("const", jsonSchema, valueConstraint);
\r
72 case "greater_than":
\r
73 checkTemplateField("exclusiveMinimum", jsonSchema, valueConstraint);
\r
75 case "greater_or_equal":
\r
76 checkTemplateField("minimum", jsonSchema, valueConstraint);
\r
79 checkTemplateField("exclusiveMaximum", jsonSchema, valueConstraint);
\r
81 case "less_or_equal":
\r
82 checkTemplateField("maximum", jsonSchema, valueConstraint);
\r
85 ArrayList<Integer> limitValues = (ArrayList<Integer>) valueConstraint;
\r
86 checkTemplateField("minimum", jsonSchema, limitValues.get(0));
\r
87 checkTemplateField("maximum", jsonSchema, limitValues.get(1));
\r
90 jsonSchema.addProperty(nameConstraint, (String) valueConstraint);
\r
93 this.getSpecificLength(jsonSchema, valueConstraint, typeProperty);
\r
96 String[] prefixValues = nameConstraint.split("_");
\r
97 this.getLimitValue(jsonSchema, valueConstraint, typeProperty, prefixValues[0]);
\r
100 String[] maxtab = nameConstraint.split("_");
\r
101 this.getLimitValue(jsonSchema, valueConstraint, typeProperty, maxtab[0]);
\r
103 default://valid_value
\r
104 this.getValueArray(jsonSchema, valueConstraint, typeProperty);
\r
113 * @param jsonSchema json schema
\r
114 * @param fieldValue field value
\r
115 * @param typeProperty For the complex components, get a specific number of items/properties
\r
117 public void getSpecificLength(JsonObject jsonSchema, Object fieldValue, String typeProperty) {
\r
118 switch (typeProperty.toLowerCase()) {
\r
120 checkTemplateField("minLength", jsonSchema, fieldValue);
\r
121 checkTemplateField("maxLength", jsonSchema, fieldValue);
\r
124 if (fieldValue.equals(1) && jsonTemplate.hasFields("uniqueItems")) {
\r
125 jsonSchema.addProperty("uniqueItems", true);
\r
127 checkTemplateField("minItems", jsonSchema, fieldValue);
\r
128 checkTemplateField("maxItems", jsonSchema, fieldValue);
\r
131 default:// Map && List
\r
132 checkTemplateField("minProperties", jsonSchema, fieldValue);
\r
133 checkTemplateField("maxProperties", jsonSchema, fieldValue);
\r
142 * @param jsonSchema json schema
\r
143 * @param fieldValue field value
\r
144 * @param typeProperty type property
\r
145 * @param side Get the limits fieldValue for the properties : depend of the type of the component
\r
147 public void getLimitValue(JsonObject jsonSchema, Object fieldValue, String typeProperty, String side) {
\r
148 switch (typeProperty) {
\r
150 if (side.equals("min")) {
\r
151 checkTemplateField("minLength", jsonSchema, fieldValue);
\r
153 checkTemplateField("maxLength", jsonSchema, fieldValue);
\r
157 if (side.equals("min")) {
\r
158 checkTemplateField("minItems", jsonSchema, fieldValue);
\r
160 checkTemplateField("maxItems", jsonSchema, fieldValue);
\r
170 * @param jsonSchema Json schema
\r
171 * @param fieldValue field value
\r
172 * @param typeProperty Get as Enum the valid values for the property
\r
174 public void getValueArray(JsonObject jsonSchema, Object fieldValue, String typeProperty) {
\r
175 if (jsonTemplate.hasFields("enum")) {
\r
176 JsonArray enumeration = new JsonArray();
\r
177 if (typeProperty.equals("string") || typeProperty.equals("String")) {
\r
178 ArrayList<String> arrayValues = (ArrayList<String>) fieldValue;
\r
179 for (String arrayItem : arrayValues) {
\r
180 enumeration.add(arrayItem);
\r
182 jsonSchema.add("enum", enumeration);
\r
184 ArrayList<Number> arrayValues = (ArrayList<Number>) fieldValue;
\r
185 for (Number arrayItem : arrayValues) {
\r
186 enumeration.add(arrayItem);
\r
188 jsonSchema.add("enum", enumeration);
\r
196 * @param field Field
\r
197 * @param jsonSchema Json schema
\r
198 * @param fieldValue Simple way to avoid code duplication
\r
200 public void checkTemplateField(String field, JsonObject jsonSchema, Object fieldValue) {
\r
201 if (jsonTemplate.hasFields(field)) {
\r
202 String typeField = fieldValue.getClass().getSimpleName();
\r
203 switch (typeField) {
\r
205 jsonSchema.addProperty(field, (String) fieldValue);
\r
208 jsonSchema.addProperty(field, (Integer) fieldValue);
\r
211 jsonSchema.addProperty(field, (Number) fieldValue);
\r
214 jsonSchema.addProperty(field, (Boolean) fieldValue);
\r