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.Map.Entry;
29 import org.apache.commons.lang3.StringUtils;
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.ToscaMapValueConverter;
37 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
38 import org.openecomp.sdc.be.tosca.model.EntrySchema;
39 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
40 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import com.google.gson.Gson;
45 import com.google.gson.JsonElement;
46 import com.google.gson.JsonObject;
47 import com.google.gson.JsonParser;
48 import com.google.gson.JsonSyntaxException;
49 import com.google.gson.stream.JsonReader;
51 import fj.data.Either;
53 public class PropertyConvertor {
54 private static PropertyConvertor instance;
55 private JsonParser jsonParser = new JsonParser();
56 private static Logger log = LoggerFactory.getLogger(PropertyConvertor.class.getName());
57 Gson gson = new Gson();
58 protected PropertyConvertor() {
62 public static synchronized PropertyConvertor getInstance() {
63 if (instance == null) {
64 instance = new PropertyConvertor();
69 public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType, Map<String, DataTypeDefinition> dataTypes) {
71 if (component instanceof Resource) {
72 Resource resource = (Resource) component;
73 List<PropertyDefinition> props = resource.getProperties();
75 Map<String, ToscaProperty> properties = new HashMap<>();
77 // take only the properties of this resource
78 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
79 ToscaProperty prop = convertProperty(dataTypes, property, false);
81 properties.put(property.getName(), prop);
83 if (!properties.isEmpty()) {
84 toscaNodeType.setProperties(properties);
88 return Either.left(toscaNodeType);
91 public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, boolean isCapabiltyProperty) {
92 ToscaProperty prop = new ToscaProperty();
94 String innerType = null;
95 SchemaDefinition schema = property.getSchema();
96 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
97 innerType = schema.getProperty().getType();
98 EntrySchema eschema = new EntrySchema();
99 eschema.setType(innerType);
100 eschema.setDescription(schema.getProperty().getDescription());
101 prop.setEntry_schema(eschema);
103 log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue());
104 prop.setDefaultp(convertToToscaObject(property.getType(), property.getName(), property.getDefaultValue(), innerType, dataTypes));
105 prop.setType(property.getType());
106 prop.setDescription(property.getDescription());
107 if (isCapabiltyProperty) {
108 prop.setStatus(property.getStatus());
109 prop.setRequired(property.isRequired());
114 public Object convertToToscaObject(String propertyType, String propertyName, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
115 log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
117 value = getDataTypeDefaultValue(propertyName, dataTypes.get(propertyType));
118 if(StringUtils.isEmpty(value)){
124 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
125 ToscaValueConverter innerConverter = null;
126 Boolean isScalar = true;
128 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
130 log.trace("isn't prederfined type, get from all data types");
131 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
132 if (innerType == null) {
133 innerType = propertyType;
136 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
137 log.trace("This is scalar type. get suitable converter for type {}", type);
138 innerConverter = type.getValueConverter();
143 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
144 if (typeIfScalar == null) {
148 innerConverter = type.getValueConverter();
149 if (ToscaPropertyType.STRING.equals(type) && value.startsWith("/")) {
150 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
153 JsonElement jsonElement = null;
155 StringReader reader = new StringReader(value);
156 JsonReader jsonReader = new JsonReader(reader);
157 jsonReader.setLenient(true);
159 jsonElement = jsonParser.parse(jsonReader);
161 if (value.equals("")) {
165 if (jsonElement.isJsonPrimitive() && isScalar) {
166 log.trace("It's well defined type. convert it");
167 ToscaValueConverter converter = type.getValueConverter();
168 return converter.convertToToscaValue(value, innerType, dataTypes);
170 log.trace("It's data type or inputs in primitive type. convert as map");
171 Object convertedValue;
172 if (innerConverter != null && (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(type))) {
173 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
176 // complex json for scalar type
177 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
179 if (innerConverter != null) {
180 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
182 convertedValue = mapConverterInst.convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement);
186 return convertedValue;
189 } catch (Exception e) {
190 log.debug("convertToToscaValue failed to parse json value :", e);
196 private String getDataTypeDefaultValue(String propertyName, DataTypeDefinition dataTypeDefinition) {
198 String delaultValue = null;
199 JsonObject asJsonObjectIn = new JsonObject();
200 Map<String, PropertyDefinition> allParentsProps = new HashMap<>();
201 while (dataTypeDefinition != null) {
203 List<PropertyDefinition> currentParentsProps = dataTypeDefinition.getProperties();
204 if (currentParentsProps != null) {
205 currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p));
208 dataTypeDefinition = dataTypeDefinition.getDerivedFrom();
210 for (Entry<String, PropertyDefinition> entry : allParentsProps.entrySet()) {
211 String propName = entry.getKey();
212 PropertyDefinition propertyDefinition = entry.getValue();
213 JsonElement elementValue = asJsonObjectIn.get(propName);
214 if(elementValue == null && propertyDefinition.getDefaultValue() != null){
215 JsonReader jsonReader = new JsonReader(new StringReader(propertyDefinition.getDefaultValue()));
216 jsonReader.setLenient(true);
217 elementValue = jsonParser.parse(jsonReader);
218 asJsonObjectIn.add(propName, elementValue);
221 if(!asJsonObjectIn.isJsonNull()){
222 delaultValue = gson.toJson(asJsonObjectIn);