Added oparent to sdc main
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / converters / DataTypePropertyConverter.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.openecomp.sdc.be.model.tosca.converters;
22
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;
31 import java.util.Map;
32 import org.openecomp.sdc.be.model.DataTypeDefinition;
33 import org.openecomp.sdc.be.model.PropertyDefinition;
34 import org.openecomp.sdc.common.util.JsonUtils;
35
36 public class DataTypePropertyConverter {
37
38     private static final DataTypePropertyConverter INSTANCE = new DataTypePropertyConverter();
39     private static final JsonParser jsonParser = new JsonParser();
40     private static final Gson gson = new Gson();
41
42     private DataTypePropertyConverter() {
43     }
44
45     public static DataTypePropertyConverter getInstance() {
46         return INSTANCE;
47     }
48
49     /**
50      *
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
54      */
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;
58     }
59
60     /**
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}}
67      */
68     public void mergeDataTypeDefaultValuesWithPropertyValue(JsonObject value, String propertyDataType, Map<String, DataTypeDefinition> dataTypes) {
69         JsonObject dataTypeDefaultValues = getDataTypePropsDefaultValuesRec(propertyDataType, dataTypes);
70         mergeDefaultValuesRec(dataTypeDefaultValues, value);
71     }
72
73     private void mergeDefaultValuesRec(JsonObject defaultValue, JsonObject value) {
74         if (ToscaConverterUtils.isGetInputValue(value) || ToscaConverterUtils.isGetPolicyValue(value)) {
75             return;
76         }
77         for (Map.Entry<String, JsonElement> defVal : defaultValue.entrySet()) {
78             mergeDefaultValue(value, defVal.getKey(), defVal.getValue());
79         }
80     }
81
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());
88         }
89     }
90
91     private boolean notPrimitivePropValue(JsonObject value, JsonElement defValue, String propName) {
92         return value.get(propName).isJsonObject() && defValue.isJsonObject();
93     }
94
95     private boolean defValueWasNotOverridden(JsonObject value, String propName) {
96         return !value.has(propName);
97     }
98
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);
105         }
106         return dataTypeDefaultsJson;
107     }
108
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);
114         }
115     }
116
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);
125         }
126         return defVal;
127     }
128
129     private JsonElement convertToJson(String propDefaultVal) {
130         JsonReader jsonReader = new JsonReader(new StringReader(propDefaultVal));
131         jsonReader.setLenient(true);
132         return jsonParser.parse(jsonReader);
133     }
134
135     private Map<String, PropertyDefinition> getAllDataTypeProperties(DataTypeDefinition dataTypeDefinition) {
136         Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
137         while (dataTypeDefinition != null) {
138
139             List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
140             if (currentParentsProps != null) {
141                 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
142             }
143
144             dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
145         }
146         return allParentsProps;
147     }
148
149 }