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.openecomp.sdc.be.model.tosca.converters;
 
  23 import com.google.gson.Gson;
 
  24 import com.google.gson.JsonElement;
 
  25 import com.google.gson.JsonObject;
 
  26 import com.google.gson.JsonParser;
 
  27 import com.google.gson.stream.JsonReader;
 
  28 import java.io.StringReader;
 
  29 import java.util.HashMap;
 
  30 import java.util.List;
 
  32 import org.openecomp.sdc.be.model.DataTypeDefinition;
 
  33 import org.openecomp.sdc.be.model.PropertyDefinition;
 
  34 import org.openecomp.sdc.common.util.JsonUtils;
 
  36 public class DataTypePropertyConverter {
 
  38     private static final DataTypePropertyConverter INSTANCE = new DataTypePropertyConverter();
 
  39     private static final JsonParser jsonParser = new JsonParser();
 
  40     private static final Gson gson = new Gson();
 
  42     private DataTypePropertyConverter() {
 
  45     public static DataTypePropertyConverter getInstance() {
 
  51      * @param propertyDataType the data type
 
  52      * @param dataTypes all data types in the system mapped by their name
 
  53      * @return a json representation of all the given data type properties default values and recursively their data type properties
 
  55     public String getDataTypePropertiesDefaultValuesRec(String propertyDataType, Map<String, DataTypeDefinition> dataTypes) {
 
  56         JsonObject defaultValues = getDataTypePropsDefaultValuesRec(propertyDataType, dataTypes);
 
  57         return !JsonUtils.isJsonNullOrEmpty(defaultValues) ? gson.toJson(defaultValues) : null;
 
  61      * Takes a json representation of a tosca property value and merges all the default values of the property data type
 
  62      * @param value the json representation of a tosca property value
 
  63      * @param propertyDataType data type from which to merge default values
 
  64      * @param dataTypes all data types in the system mapped by their name
 
  65      * for example: for value {a: {b: c}} we could have a default value {a: {d: e}} (property a has two sub properties but only b was overridden)
 
  66      * so the complete value is {a: {b: c, d: e}}
 
  68     public void mergeDataTypeDefaultValuesWithPropertyValue(JsonObject value, String propertyDataType, Map<String, DataTypeDefinition> dataTypes) {
 
  69         JsonObject dataTypeDefaultValues = getDataTypePropsDefaultValuesRec(propertyDataType, dataTypes);
 
  70         mergeDefaultValuesRec(dataTypeDefaultValues, value);
 
  73     private void mergeDefaultValuesRec(JsonObject defaultValue, JsonObject value) {
 
  74         if (ToscaConverterUtils.isGetInputValue(value) || ToscaConverterUtils.isGetPolicyValue(value)) {
 
  77         for (Map.Entry<String, JsonElement> defVal : defaultValue.entrySet()) {
 
  78             mergeDefaultValue(value, defVal.getKey(), defVal.getValue());
 
  82     private void mergeDefaultValue(JsonObject value, String propName, JsonElement defValue) {
 
  83         if (defValueWasNotOverridden(value, propName)) {
 
  84             value.add(propName, defValue);
 
  85         } else if (notPrimitivePropValue(value, defValue, propName)) {
 
  86             JsonElement propValue = value.get(propName);
 
  87             mergeDefaultValuesRec(defValue.getAsJsonObject(), propValue.getAsJsonObject());
 
  91     private boolean notPrimitivePropValue(JsonObject value, JsonElement defValue, String propName) {
 
  92         return value.get(propName).isJsonObject() && defValue.isJsonObject();
 
  95     private boolean defValueWasNotOverridden(JsonObject value, String propName) {
 
  96         return !value.has(propName);
 
  99     private JsonObject getDataTypePropsDefaultValuesRec(String propertyDataType, Map<String, DataTypeDefinition> dataTypes) {
 
 100         Map<String, PropertyDefinition> allParentsProps = getAllDataTypeProperties(dataTypes.get(propertyDataType));
 
 101         JsonObject dataTypeDefaultsJson = new JsonObject();
 
 102         for (Map.Entry<String, PropertyDefinition> propertyEntry : allParentsProps.entrySet()) {
 
 103             PropertyDefinition propertyDefinition = propertyEntry.getValue();
 
 104             addDefaultValueToJson(dataTypes, dataTypeDefaultsJson, propertyDefinition);
 
 106         return dataTypeDefaultsJson;
 
 109     private void addDefaultValueToJson(Map<String, DataTypeDefinition> dataTypes, JsonObject dataTypePropsDefaults, PropertyDefinition propertyDefinition) {
 
 110         String propName = propertyDefinition.getName();
 
 111         JsonElement defVal = getDefaultValue(dataTypes, dataTypePropsDefaults, propertyDefinition);
 
 112         if (!JsonUtils.isEmptyJson(defVal)) {
 
 113             dataTypePropsDefaults.add(propName, defVal);
 
 117     private JsonElement getDefaultValue(Map<String, DataTypeDefinition> dataTypes, JsonObject dataTypePropsDefaults, PropertyDefinition propertyDefinition) {
 
 118         JsonElement defVal = new JsonObject();
 
 119         String propName = propertyDefinition.getName();
 
 120         String propDefaultVal = propertyDefinition.getDefaultValue();
 
 121         if(!JsonUtils.containsEntry(dataTypePropsDefaults, propName) && propDefaultVal != null){
 
 122             defVal = convertToJson(propDefaultVal);
 
 123         } else if (!JsonUtils.containsEntry(dataTypePropsDefaults, propName)) {
 
 124             defVal = getDataTypePropsDefaultValuesRec(propertyDefinition.getType(), dataTypes);
 
 129     private JsonElement convertToJson(String propDefaultVal) {
 
 130         JsonReader jsonReader = new JsonReader(new StringReader(propDefaultVal));
 
 131         jsonReader.setLenient(true);
 
 132         return jsonParser.parse(jsonReader);
 
 135     private Map<String, PropertyDefinition> getAllDataTypeProperties(DataTypeDefinition dataTypeDefinition) {
 
 136         Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
 
 137         while (dataTypeDefinition != null) {
 
 139             List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
 
 140             if (currentParentsProps != null) {
 
 141                 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
 
 144             dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
 
 146         return allParentsProps;