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.tosca;
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 fj.data.Either;
29 import org.apache.commons.lang3.StringUtils;
30 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
32 import org.openecomp.sdc.be.model.Component;
33 import org.openecomp.sdc.be.model.DataTypeDefinition;
34 import org.openecomp.sdc.be.model.PropertyDefinition;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.tosca.ToscaFunctions;
37 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
38 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
39 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
40 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
41 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
42 import org.openecomp.sdc.be.tosca.model.EntrySchema;
43 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
44 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
45 import org.openecomp.sdc.common.log.wrappers.Logger;
47 import java.io.StringReader;
48 import java.util.HashMap;
49 import java.util.List;
51 import java.util.Objects;
52 import java.util.function.Supplier;
54 public class PropertyConvertor {
55 private static PropertyConvertor instance;
56 private JsonParser jsonParser = new JsonParser();
57 private static final Logger log = Logger.getLogger(PropertyConvertor.class);
58 public enum PropertyType {
63 Gson gson = new Gson();
64 public PropertyConvertor() {
68 public static synchronized PropertyConvertor getInstance() {
69 if (instance == null) {
70 instance = new PropertyConvertor();
75 public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType, Map<String, DataTypeDefinition> dataTypes) {
77 if (component instanceof Resource) {
78 Resource resource = (Resource) component;
79 List<PropertyDefinition> props = resource.getProperties();
81 Map<String, ToscaProperty> properties = new HashMap<>();
83 // take only the properties of this resource
84 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
85 properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY));
87 if (!properties.isEmpty()) {
88 toscaNodeType.setProperties(properties);
92 return Either.left(toscaNodeType);
95 public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
96 ToscaProperty prop = new ToscaProperty();
98 String innerType = null;
99 SchemaDefinition schema = property.getSchema();
100 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
101 innerType = schema.getProperty().getType();
102 EntrySchema eschema = new EntrySchema();
103 eschema.setType(innerType);
104 eschema.setDescription(schema.getProperty().getDescription());
105 prop.setEntry_schema(eschema);
107 return getToscaProperty(dataTypes, property, prop, innerType, propertyType);
111 private ToscaProperty getToscaProperty(Map<String, DataTypeDefinition> dataTypes,
112 PropertyDataDefinition property,
115 PropertyType propertyType) {
116 log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue());
117 String defaultValue = property.getDefaultValue();
118 if(Objects.isNull(defaultValue)) {
119 defaultValue = property.getValue();
121 Object convertedObj =
122 convertToToscaObject(property.getType(), defaultValue, innerType, dataTypes, false);
123 if (convertedObj != null) {
124 prop.setDefaultp(convertedObj);
126 prop.setType(property.getType());
127 prop.setDescription(property.getDescription());
128 prop.setRequired(property.isRequired());
129 if(propertyType.equals(PropertyType.CAPABILITY)) {
130 prop.setStatus(property.getStatus());
136 public Object convertToToscaObject(String propertyType, String value, String innerType, Map<String, DataTypeDefinition> dataTypes, boolean preserveEmptyValue) {
137 log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
138 if (StringUtils.isEmpty(value)) {
139 value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
140 if(StringUtils.isEmpty(value)){
145 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
146 ToscaValueConverter innerConverter = null;
147 Boolean isScalar = true;
149 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
151 log.trace("isn't prederfined type, get from all data types");
152 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
153 if (innerType == null) {
154 innerType = propertyType;
157 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
158 log.trace("This is scalar type. get suitable converter for type {}", type);
159 innerConverter = type.getValueConverter();
164 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
165 if (typeIfScalar == null) {
169 innerConverter = type.getValueConverter();
170 if (ToscaPropertyType.STRING.equals(type) && valueStartsWithNonJsonChar(value)) {
171 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
174 JsonElement jsonElement = null;
176 StringReader reader = new StringReader(value);
177 JsonReader jsonReader = new JsonReader(reader);
178 jsonReader.setLenient(true);
180 jsonElement = jsonParser.parse(jsonReader);
182 if (value.equals("")) {
186 if (jsonElement.isJsonPrimitive() && isScalar) {
187 log.trace("It's well defined type. convert it");
188 ToscaValueConverter converter = type.getValueConverter();
189 return converter.convertToToscaValue(value, innerType, dataTypes);
191 log.trace("It's data type or inputs in primitive type. convert as map");
192 if (jsonElement.isJsonObject()) {
193 JsonObject jsonObj = jsonElement.getAsJsonObject();
194 // check if value is a get_input function
195 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
196 Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
197 log.debug("It's get_input function. obj={}", obj);
201 Object convertedValue;
202 if (innerConverter != null && (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(type))) {
203 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
204 } else if (isScalar) {
205 // complex json for scalar type
206 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
207 } else if (innerConverter != null) {
208 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
210 convertedValue = mapConverterInst.convertDataTypeToToscaObject(
211 innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
214 return convertedValue;
216 } catch (Exception e) {
217 log.debug("convertToToscaValue failed to parse json value :", e);
223 private boolean valueStartsWithNonJsonChar(String value) {
224 return value.startsWith("/") || value.startsWith(":");
227 public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes,
228 Map<String, Object> props, PropertyDataDefinition prop, Supplier<String> supplier) {
229 Object convertedValue = convertValue(dataTypes, prop, supplier);
230 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
231 props.put(prop.getName(), convertedValue);
235 private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes,
236 T input, Supplier<String> supplier) {
237 String propertyType = input.getType();
238 String innerType = null;
239 if (input.getSchema() != null && input.getSchema().getProperty() != null) {
240 innerType = input.getSchema().getProperty().getType();
242 return convertToToscaObject(propertyType, supplier.get(), innerType, dataTypes, false);