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 java.io.StringReader;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.function.Supplier;
30 import org.apache.commons.lang3.StringUtils;
31 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.DataTypeDefinition;
35 import org.openecomp.sdc.be.model.PropertyDefinition;
36 import org.openecomp.sdc.be.model.Resource;
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.ToscaStringConvertor;
41 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
42 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
43 import org.openecomp.sdc.be.tosca.model.EntrySchema;
44 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
45 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
46 import org.openecomp.sdc.common.log.wrappers.Logger;
48 import com.google.gson.Gson;
49 import com.google.gson.JsonElement;
50 import com.google.gson.JsonParser;
51 import com.google.gson.stream.JsonReader;
53 import fj.data.Either;
55 public class PropertyConvertor {
56 private static PropertyConvertor instance;
57 private JsonParser jsonParser = new JsonParser();
58 private static final Logger log = Logger.getLogger(PropertyConvertor.class);
59 public enum PropertyType {
64 Gson gson = new Gson();
65 public PropertyConvertor() {
69 public static synchronized PropertyConvertor getInstance() {
70 if (instance == null) {
71 instance = new PropertyConvertor();
76 public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType, Map<String, DataTypeDefinition> dataTypes) {
78 if (component instanceof Resource) {
79 Resource resource = (Resource) component;
80 List<PropertyDefinition> props = resource.getProperties();
82 Map<String, ToscaProperty> properties = new HashMap<>();
84 // take only the properties of this resource
85 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
86 properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY));
88 if (!properties.isEmpty()) {
89 toscaNodeType.setProperties(properties);
93 return Either.left(toscaNodeType);
96 public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
97 ToscaProperty prop = new ToscaProperty();
99 String innerType = null;
100 SchemaDefinition schema = property.getSchema();
101 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
102 innerType = schema.getProperty().getType();
103 EntrySchema eschema = new EntrySchema();
104 eschema.setType(innerType);
105 eschema.setDescription(schema.getProperty().getDescription());
106 prop.setEntry_schema(eschema);
108 return getToscaProperty(dataTypes, property, prop, innerType, propertyType);
112 private ToscaProperty getToscaProperty(Map<String, DataTypeDefinition> dataTypes,
113 PropertyDataDefinition property,
116 PropertyType propertyType) {
117 log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue());
118 String defaultValue = property.getDefaultValue();
119 if(Objects.isNull(defaultValue)) {
120 defaultValue = property.getValue();
122 Object convertedObj =
123 convertToToscaObject(property.getType(), defaultValue, innerType, dataTypes, false);
124 if (convertedObj != null) {
125 prop.setDefaultp(convertedObj);
127 prop.setType(property.getType());
128 prop.setDescription(property.getDescription());
129 prop.setRequired(property.isRequired());
130 switch (propertyType) {
132 prop.setStatus(property.getStatus());
141 public Object convertToToscaObject(String propertyType, String value, String innerType, Map<String, DataTypeDefinition> dataTypes, boolean preserveEmptyValue) {
142 log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
143 if (StringUtils.isEmpty(value)) {
144 value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
145 if(StringUtils.isEmpty(value)){
150 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
151 ToscaValueConverter innerConverter = null;
152 Boolean isScalar = true;
154 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
156 log.trace("isn't prederfined type, get from all data types");
157 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
158 if (innerType == null) {
159 innerType = propertyType;
162 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
163 log.trace("This is scalar type. get suitable converter for type {}", type);
164 innerConverter = type.getValueConverter();
169 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
170 if (typeIfScalar == null) {
174 innerConverter = type.getValueConverter();
175 if (ToscaPropertyType.STRING.equals(type) && valueStartsWithNonJsonChar(value)) {
176 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
179 JsonElement jsonElement = null;
181 StringReader reader = new StringReader(value);
182 JsonReader jsonReader = new JsonReader(reader);
183 jsonReader.setLenient(true);
185 jsonElement = jsonParser.parse(jsonReader);
187 if (value.equals("")) {
191 if (jsonElement.isJsonPrimitive() && isScalar) {
192 log.trace("It's well defined type. convert it");
193 ToscaValueConverter converter = type.getValueConverter();
194 return converter.convertToToscaValue(value, innerType, dataTypes);
196 log.trace("It's data type or inputs in primitive type. convert as map");
197 Object convertedValue;
198 if (innerConverter != null && (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(type))) {
199 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
202 // complex json for scalar type
203 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
205 if (innerConverter != null) {
206 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
208 convertedValue = mapConverterInst.convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
212 return convertedValue;
215 } catch (Exception e) {
216 log.debug("convertToToscaValue failed to parse json value :", e);
222 private boolean valueStartsWithNonJsonChar(String value) {
223 return value.startsWith("/") || value.startsWith(":");
226 public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes,
227 Map<String, Object> props, PropertyDataDefinition prop, Supplier<String> supplier) {
228 Object convertedValue = convertValue(dataTypes, prop, supplier);
229 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
230 props.put(prop.getName(), convertedValue);
234 private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes,
235 T input, Supplier<String> supplier) {
236 String propertyType = input.getType();
237 String innerType = null;
238 if (input.getSchema() != null && input.getSchema().getProperty() != null) {
239 innerType = input.getSchema().getProperty().getType();
241 return convertToToscaObject(propertyType, supplier.get(), innerType, dataTypes, false);