2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.sdc.be.model.tosca.validators;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Map.Entry;
29 import org.apache.commons.lang3.tuple.ImmutablePair;
30 import org.openecomp.sdc.be.model.DataTypeDefinition;
31 import org.openecomp.sdc.be.model.PropertyDefinition;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
34 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
35 import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import com.google.gson.Gson;
40 import com.google.gson.JsonElement;
41 import com.google.gson.JsonObject;
42 import com.google.gson.JsonParser;
43 import com.google.gson.JsonPrimitive;
44 import com.google.gson.JsonSyntaxException;
46 public class DataTypeValidatorConverter {
48 private static DataTypeValidatorConverter dataTypeValidatorConverter = new DataTypeValidatorConverter();
50 public static DataTypeValidatorConverter getInstance() {
51 return dataTypeValidatorConverter;
54 private DataTypeValidatorConverter() {
58 private static Logger log = LoggerFactory.getLogger(DataTypeValidatorConverter.class.getName());
60 JsonParser jsonParser = new JsonParser();
62 Gson gson = new Gson();
64 ImmutablePair<JsonElement, Boolean> falseResult = new ImmutablePair<JsonElement, Boolean>(null, false);
65 ImmutablePair<JsonElement, Boolean> trueEmptyResult = new ImmutablePair<JsonElement, Boolean>(null, true);
67 ImmutablePair<String, Boolean> trueStringEmptyResult = new ImmutablePair<String, Boolean>(null, true);
68 ImmutablePair<String, Boolean> falseStringEmptyResult = new ImmutablePair<String, Boolean>(null, true);
70 private ToscaPropertyType isDataTypeDerviedFromScalarType(DataTypeDefinition dataTypeDef) {
72 ToscaPropertyType result = null;
74 DataTypeDefinition dataType = dataTypeDef;
76 while (dataType != null) {
78 String name = dataType.getName();
79 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(name);
80 if (typeIfScalar != null) {
81 result = typeIfScalar;
85 dataType = dataType.getDerivedFrom();
91 private ImmutablePair<JsonElement, Boolean> validateAndUpdate(JsonElement jsonElement, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) {
93 Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
95 ToscaPropertyType toscaPropertyType = null;
96 if ((toscaPropertyType = isDataTypeDerviedFromScalarType(dataTypeDefinition)) != null) {
98 PropertyTypeValidator validator = toscaPropertyType.getValidator();
99 PropertyValueConverter converter = toscaPropertyType.getConverter();
100 if (jsonElement == null || true == jsonElement.isJsonNull()) {
101 boolean valid = validator.isValid(null, null, allDataTypes);
103 log.trace("Failed in validation of property {} from type {}", dataTypeDefinition.getName(), dataTypeDefinition.getName());
106 return new ImmutablePair<JsonElement, Boolean>(jsonElement, true);
109 if (jsonElement.isJsonPrimitive()) {
111 if (jsonElement != null) {
112 if (jsonElement.toString().isEmpty()) {
115 value = jsonElement.toString();
118 boolean valid = validator.isValid(value, null, null);
120 log.trace("Failed in validation of property {} from type {}. Json primitive value is {}", dataTypeDefinition.getName(), dataTypeDefinition.getName(), value);
124 String convertedValue = converter.convert(value, null, allDataTypes);
125 JsonElement element = null;
127 element = jsonParser.parse(convertedValue);
128 } catch (JsonSyntaxException e) {
129 log.debug("Failed to parse value {} of property {} {}", convertedValue, dataTypeDefinition.getName(), e);
133 return new ImmutablePair<JsonElement, Boolean>(element, true);
136 // MAP, LIST, OTHER types cannot be applied data type
137 // definition scalar type. We currently cannot derived from
138 // map/list. (cannot add the entry schema to it)
139 log.debug("We cannot derive from list/map. Thus, the value cannot be not primitive since the data type {} is scalar one", dataTypeDefinition.getName());
146 if (jsonElement == null || jsonElement.isJsonNull()) {
148 return new ImmutablePair<JsonElement, Boolean>(jsonElement, true);
152 if (jsonElement.isJsonObject()) {
154 JsonObject buildJsonObject = new JsonObject();
156 JsonObject asJsonObject = jsonElement.getAsJsonObject();
157 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
159 for (Entry<String, JsonElement> entry : entrySet) {
160 String propName = entry.getKey();
162 JsonElement elementValue = entry.getValue();
164 PropertyDefinition propertyDefinition = allProperties.get(propName);
165 if (propertyDefinition == null) {
166 log.debug("The property {} was not found under data type {}" ,propName, dataTypeDefinition.getName());
169 String type = propertyDefinition.getType();
170 boolean isScalarType = ToscaPropertyType.isScalarType(type);
173 ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
174 if (propertyType == null) {
175 log.debug("cannot find the {} under default tosca property types", type);
178 PropertyTypeValidator validator = propertyType.getValidator();
179 String innerType = null;
180 if (propertyType == ToscaPropertyType.LIST || propertyType == ToscaPropertyType.MAP) {
181 if (propertyDefinition.getSchema() != null && propertyDefinition.getSchema().getProperty() != null) {
182 innerType = propertyDefinition.getSchema().getProperty().getType();
183 if (innerType == null) {
184 log.debug("Property type {} must have inner type in its declaration.", propertyType);
191 if (elementValue != null) {
192 if (elementValue.isJsonPrimitive() && elementValue.getAsString().isEmpty()) {
195 value = elementValue.toString();
199 boolean isValid = validator.isValid(value, innerType, allDataTypes);
200 if (false == isValid) {
201 log.debug("Failed to validate the value {} from type {}", value, propertyType);
205 PropertyValueConverter converter = propertyType.getConverter();
206 String convertedValue = converter.convert(value, innerType, allDataTypes);
208 JsonElement element = null;
209 if (convertedValue != null) {
210 if (convertedValue.isEmpty()) {
211 element = new JsonPrimitive("");
214 element = jsonParser.parse(convertedValue);
215 } catch (JsonSyntaxException e) {
216 log.debug("Failed to parse value {} of type {}", convertedValue, propertyType, e);
221 buildJsonObject.add(propName, element);
225 DataTypeDefinition typeDefinition = allDataTypes.get(type);
226 if (typeDefinition == null) {
227 log.debug("The data type {} cannot be found in the given data type list.", type);
231 ImmutablePair<JsonElement, Boolean> isValid = validateAndUpdate(elementValue, typeDefinition, allDataTypes);
233 if (!isValid.getRight().booleanValue()) {
234 log.debug("Failed in validation of value {} from type {}", (elementValue != null ? elementValue.toString() : null), typeDefinition.getName());
238 buildJsonObject.add(propName, isValid.getLeft());
243 return new ImmutablePair<JsonElement, Boolean>(buildJsonObject, true);
245 log.debug("The value {} of type {} should be json object", (jsonElement != null ? jsonElement.toString() : null), dataTypeDefinition.getName());
254 public ImmutablePair<JsonElement, Boolean> validateAndUpdate(String value, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) {
256 ImmutablePair<JsonElement, Boolean> result = falseResult;
258 if (value == null || value.isEmpty()) {
259 return trueEmptyResult;
262 JsonElement jsonElement = null;
264 jsonElement = jsonParser.parse(value);
265 } catch (JsonSyntaxException e) {
269 result = validateAndUpdate(jsonElement, dataTypeDefinition, allDataTypes);
274 private Map<String, PropertyDefinition> getAllProperties(DataTypeDefinition dataTypeDefinition) {
276 Map<String, PropertyDefinition> allParentsProps = new HashMap<String, PropertyDefinition>();
278 while (dataTypeDefinition != null) {
280 List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
281 if (currentParentsProps != null) {
282 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
285 dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
288 return allParentsProps;
291 private String getValueFromJsonElement(JsonElement jsonElement) {
294 if (jsonElement == null || jsonElement.isJsonNull()) {
295 value = PropertyOperation.EMPTY_VALUE;
297 if (jsonElement.toString().isEmpty()) {
300 value = jsonElement.toString();
307 public boolean isValid(String value, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) {
309 boolean result = false;
311 if (value == null || value.isEmpty()) {
315 JsonElement jsonElement = null;
317 jsonElement = jsonParser.parse(value);
318 } catch (JsonSyntaxException e) {
319 log.debug("Failed to parse the value {} from type {}", value, dataTypeDefinition, e);
323 result = isValid(jsonElement, dataTypeDefinition, allDataTypes);
328 private boolean isValid(JsonElement jsonElement, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> allDataTypes) {
330 Map<String, PropertyDefinition> allProperties = getAllProperties(dataTypeDefinition);
332 ToscaPropertyType toscaPropertyType = null;
333 if ((toscaPropertyType = isDataTypeDerviedFromScalarType(dataTypeDefinition)) != null) {
335 PropertyTypeValidator validator = toscaPropertyType.getValidator();
336 if (jsonElement == null || true == jsonElement.isJsonNull()) {
337 boolean valid = validator.isValid(null, null, allDataTypes);
338 if (false == valid) {
339 log.trace("Failed in validation of property {} from type {}", dataTypeDefinition.getName(), dataTypeDefinition.getName());
346 if (true == jsonElement.isJsonPrimitive()) {
348 if (jsonElement != null) {
349 if (jsonElement.toString().isEmpty()) {
352 value = jsonElement.toString();
355 boolean valid = validator.isValid(value, null, allDataTypes);
356 if (false == valid) {
357 log.trace("Failed in validation of property {} from type {}. Json primitive value is {}", dataTypeDefinition.getName(), dataTypeDefinition.getName(), value);
364 // MAP, LIST, OTHER types cannot be applied data type
365 // definition scalar type. We currently cannot derived from
366 // map/list. (cannot add the entry schema to it)
367 log.debug("We cannot derive from list/map. Thus, the value cannot be not primitive since the data type {} is scalar one", dataTypeDefinition.getName());
374 if (jsonElement == null || jsonElement.isJsonNull()) {
380 if (jsonElement.isJsonObject()) {
382 JsonObject asJsonObject = jsonElement.getAsJsonObject();
383 Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
385 for (Entry<String, JsonElement> entry : entrySet) {
386 String propName = entry.getKey();
388 JsonElement elementValue = entry.getValue();
390 PropertyDefinition propertyDefinition = allProperties.get(propName);
391 if (propertyDefinition == null) {
392 log.debug("The property {} was not found under data type {}", propName, dataTypeDefinition.getName());
395 String type = propertyDefinition.getType();
396 boolean isScalarType = ToscaPropertyType.isScalarType(type);
398 if (true == isScalarType) {
399 ToscaPropertyType propertyType = ToscaPropertyType.isValidType(type);
400 if (propertyType == null) {
401 log.debug("cannot find the {} under default tosca property types", type);
404 PropertyTypeValidator validator = propertyType.getValidator();
405 String innerType = null;
406 if (propertyType == ToscaPropertyType.LIST || propertyType == ToscaPropertyType.MAP) {
407 if (propertyDefinition.getSchema() != null && propertyDefinition.getSchema().getProperty() != null) {
408 innerType = propertyDefinition.getSchema().getProperty().getType();
409 if (innerType == null) {
410 log.debug("Property type {} must have inner type in its declaration.", propertyType);
417 if (elementValue != null) {
418 if (elementValue.isJsonPrimitive() && elementValue.getAsString().isEmpty()) {
421 value = elementValue.toString();
425 boolean isValid = validator.isValid(value, innerType, allDataTypes);
426 if (false == isValid) {
427 log.debug("Failed to validate the value {} from type {}", value, propertyType);
433 DataTypeDefinition typeDefinition = allDataTypes.get(type);
434 if (typeDefinition == null) {
435 log.debug("The data type {} cannot be found in the given data type list.", type);
439 boolean isValid = isValid(elementValue, typeDefinition, allDataTypes);
441 if (false == isValid) {
442 log.debug("Failed in validation of value {} from type {}", (elementValue != null ? elementValue.toString() : null), typeDefinition.getName());
452 log.debug("The value {} of type {} should be json object", (jsonElement != null ? jsonElement.toString() : null), dataTypeDefinition.getName());