Support functions in TOSCA Simple Profile in YAML
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / PropertyConvertor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.tosca;
22
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
26 import com.google.gson.stream.JsonReader;
27 import fj.data.Either;
28 import java.io.StringReader;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Objects;
33 import java.util.function.Supplier;
34 import org.apache.commons.lang3.StringUtils;
35 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.DataTypeDefinition;
39 import org.openecomp.sdc.be.model.PropertyDefinition;
40 import org.openecomp.sdc.be.model.Resource;
41 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
42 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
43 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
44 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
45 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
46 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
47 import org.openecomp.sdc.be.tosca.model.EntrySchema;
48 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
49 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
50 import org.openecomp.sdc.common.log.wrappers.Logger;
51 import org.springframework.stereotype.Service;
52
53 @Service
54 public class PropertyConvertor {
55     private JsonParser jsonParser = new JsonParser();
56     private static final Logger log = Logger.getLogger(PropertyConvertor.class);
57     public  enum PropertyType  {
58         CAPABILITY,
59         INPUT,
60         PROPERTY
61     }
62
63
64     public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType, Map<String, DataTypeDefinition> dataTypes) {
65
66         if (component instanceof Resource) {
67             Resource resource = (Resource) component;
68             List<PropertyDefinition> props = resource.getProperties();
69             if (props != null) {
70                 Map<String, ToscaProperty> properties = new HashMap<>();
71
72                 // take only the properties of this resource
73                 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
74                     properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY));
75                  });
76                 if (!properties.isEmpty()) {
77                     toscaNodeType.setProperties(properties);
78                 }
79             }
80         }
81         return Either.left(toscaNodeType);
82     }
83
84     public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
85         ToscaProperty prop = new ToscaProperty();
86         log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue());
87         SchemaDefinition schema = property.getSchema();
88         if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
89             EntrySchema eschema = new EntrySchema();
90             eschema.setType(schema.getProperty().getType());
91             eschema.setDescription(schema.getProperty().getDescription());
92             prop.setEntry_schema(eschema);
93         }
94         String defaultValue = property.getDefaultValue();
95         if(Objects.isNull(defaultValue)) {
96             defaultValue = property.getValue();
97         }
98         Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false);
99         if (convertedObj != null) {
100             prop.setDefaultp(convertedObj);
101         }
102         prop.setType(property.getType());
103         prop.setDescription(property.getDescription());
104         prop.setRequired(property.isRequired());
105         if(propertyType.equals(PropertyType.CAPABILITY)) {
106             prop.setStatus(property.getStatus());
107         }
108         return prop;
109     }
110     
111
112     public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes, boolean preserveEmptyValue) {
113         String propertyType = property.getType();
114         String innerType = property.getSchemaType();
115         log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
116         if (StringUtils.isEmpty(value)) {
117             value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
118             if(StringUtils.isEmpty(value)){
119                 return null;
120             }
121         }
122         try {
123             ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
124             ToscaValueConverter innerConverter = null;
125             boolean isScalar = true;
126
127             ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
128             if (type == null) {
129                 log.trace("isn't prederfined type, get from all data types");
130                 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
131                 if (innerType == null) {
132                     innerType = propertyType;
133                 }
134
135                 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
136                     log.trace("This is scalar type. get suitable converter for type {}", type);
137                     innerConverter = type.getValueConverter();
138                 } else {
139                     isScalar = false;
140                 }
141             } else {
142                 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
143                 if (typeIfScalar == null) {
144                     isScalar = false;
145                 }
146
147                 innerConverter = type.getValueConverter();
148                 if (ToscaPropertyType.STRING == type && valueStartsWithNonJsonChar(value)) {
149                     return innerConverter.convertToToscaValue(value, innerType, dataTypes);
150                 }
151             }
152             JsonElement jsonElement = null;
153
154             StringReader reader = new StringReader(value);
155             JsonReader jsonReader = new JsonReader(reader);
156             jsonReader.setLenient(true);
157
158             jsonElement = jsonParser.parse(jsonReader);
159
160             if (value.equals("")) {
161                 return value;
162             }
163
164             if (jsonElement.isJsonPrimitive() && isScalar) {
165                 log.trace("It's well defined type. convert it");
166                 ToscaValueConverter converter = type.getValueConverter();
167                 return converter.convertToToscaValue(value, innerType, dataTypes);
168             }
169             log.trace("It's data type or inputs in primitive type. convert as map");
170             if (jsonElement.isJsonObject()) {
171                 JsonObject jsonObj = jsonElement.getAsJsonObject();
172                 // check if value is a get_input function
173                 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
174                     Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
175                     log.debug("It's get_input function. obj={}", obj);
176                     return obj;
177                 }
178             }
179             Object convertedValue;
180                 if (innerConverter != null && (ToscaPropertyType.MAP == type || ToscaPropertyType.LIST == type)) {
181                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
182             } else if (isScalar) {
183                 // complex json for scalar type
184                 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
185             } else if (innerConverter != null) {
186                 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
187             } else {
188                 convertedValue = mapConverterInst.convertDataTypeToToscaObject(
189                     innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
190             }
191
192             return convertedValue;
193
194         } catch (Exception e) {
195             log.debug("convertToToscaValue failed to parse json value :", e);
196             return null;
197         }
198
199     }
200
201     private boolean valueStartsWithNonJsonChar(String value) {
202         return value.startsWith("/") || value.startsWith(":");
203     }
204
205     public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes,
206             Map<String, Object> props, PropertyDataDefinition prop, Supplier<String> supplier) {
207         Object convertedValue = convertValue(dataTypes, prop, supplier);
208         if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
209             props.put(prop.getName(), convertedValue);
210         }
211     }
212
213     private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes,
214              T input, Supplier<String> supplier) {
215         return convertToToscaObject(input, supplier.get(), dataTypes, false);
216     }
217 }