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.JsonParser;
26 import com.google.gson.stream.JsonReader;
27 import fj.data.Either;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
31 import org.openecomp.sdc.be.model.Component;
32 import org.openecomp.sdc.be.model.DataTypeDefinition;
33 import org.openecomp.sdc.be.model.PropertyDefinition;
34 import org.openecomp.sdc.be.model.Resource;
35 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
36 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
37 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
38 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
39 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
40 import org.openecomp.sdc.be.tosca.model.EntrySchema;
41 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
42 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
43 import org.openecomp.sdc.common.log.wrappers.Logger;
45 import java.io.StringReader;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.function.Supplier;
51 public class PropertyConvertor {
52 private static PropertyConvertor instance;
53 private JsonParser jsonParser = new JsonParser();
54 private static final Logger log = Logger.getLogger(PropertyConvertor.class);
55 Gson gson = new Gson();
56 protected PropertyConvertor() {
60 public static synchronized PropertyConvertor getInstance() {
61 if (instance == null) {
62 instance = new PropertyConvertor();
67 public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType, Map<String, DataTypeDefinition> dataTypes) {
69 if (component instanceof Resource) {
70 Resource resource = (Resource) component;
71 List<PropertyDefinition> props = resource.getProperties();
73 Map<String, ToscaProperty> properties = new HashMap<>();
75 // take only the properties of this resource
76 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
77 properties.put(property.getName(), convertProperty(dataTypes, property, false));
79 if (!properties.isEmpty()) {
80 toscaNodeType.setProperties(properties);
84 return Either.left(toscaNodeType);
87 public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDataDefinition property, boolean isCapabiltyProperty) {
88 ToscaProperty prop = new ToscaProperty();
90 String innerType = null;
91 SchemaDefinition schema = property.getSchema();
92 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
93 innerType = schema.getProperty().getType();
94 EntrySchema eschema = new EntrySchema();
95 eschema.setType(innerType);
96 eschema.setDescription(schema.getProperty().getDescription());
97 prop.setEntry_schema(eschema);
99 return getToscaProperty(dataTypes, property, isCapabiltyProperty, prop, innerType);
103 private ToscaProperty getToscaProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDataDefinition property, boolean isCapabiltyProperty, ToscaProperty prop, String innerType) {
104 log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue());
105 Object convertedObj = convertToToscaObject(property.getType(), property.getDefaultValue(), innerType, dataTypes, false);
106 if (convertedObj != null) {
107 prop.setDefaultp(convertedObj);
109 prop.setType(property.getType());
110 prop.setDescription(property.getDescription());
111 if (isCapabiltyProperty) {
112 prop.setStatus(property.getStatus());
113 prop.setRequired(property.isRequired());
119 public Object convertToToscaObject(String propertyType, String value, String innerType, Map<String, DataTypeDefinition> dataTypes, boolean preserveEmptyValue) {
120 log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
121 if (StringUtils.isEmpty(value)) {
122 value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
123 if(StringUtils.isEmpty(value)){
128 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
129 ToscaValueConverter innerConverter = null;
130 Boolean isScalar = true;
132 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
134 log.trace("isn't prederfined type, get from all data types");
135 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
136 if (innerType == null) {
137 innerType = propertyType;
140 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
141 log.trace("This is scalar type. get suitable converter for type {}", type);
142 innerConverter = type.getValueConverter();
147 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
148 if (typeIfScalar == null) {
152 innerConverter = type.getValueConverter();
153 if (ToscaPropertyType.STRING.equals(type) && valueStartsWithNonJsonChar(value)) {
154 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
157 JsonElement jsonElement = null;
159 StringReader reader = new StringReader(value);
160 JsonReader jsonReader = new JsonReader(reader);
161 jsonReader.setLenient(true);
163 jsonElement = jsonParser.parse(jsonReader);
165 if (value.equals("")) {
169 if (jsonElement.isJsonPrimitive() && isScalar) {
170 log.trace("It's well defined type. convert it");
171 ToscaValueConverter converter = type.getValueConverter();
172 return converter.convertToToscaValue(value, innerType, dataTypes);
174 log.trace("It's data type or inputs in primitive type. convert as map");
175 Object convertedValue;
176 if (innerConverter != null && (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(type))) {
177 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
180 // complex json for scalar type
181 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
183 if (innerConverter != null) {
184 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
186 convertedValue = mapConverterInst.convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
190 return convertedValue;
193 } catch (Exception e) {
194 log.debug("convertToToscaValue failed to parse json value :", e);
200 private boolean valueStartsWithNonJsonChar(String value) {
201 return value.startsWith("/") || value.startsWith(":");
204 public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes,
205 Map<String, Object> props, PropertyDataDefinition prop, Supplier<String> supplier) {
206 Object convertedValue = convertValue(dataTypes, prop, supplier);
207 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
208 props.put(prop.getName(), convertedValue);
212 private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes,
213 T input, Supplier<String> supplier) {
214 String propertyType = input.getType();
215 String innerType = null;
216 if (input.getSchema() != null && input.getSchema().getProperty() != null) {
217 innerType = input.getSchema().getProperty().getType();
219 return convertToToscaObject(propertyType, supplier.get(), innerType, dataTypes, false);