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;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.DataTypeDefinition;
32 import org.openecomp.sdc.be.model.PropertyDefinition;
33 import org.openecomp.sdc.be.model.Resource;
34 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
35 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
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.JsonParser;
47 import com.google.gson.stream.JsonReader;
49 import fj.data.Either;
51 public class PropertyConvertor {
52 private static PropertyConvertor instance;
53 private JsonParser jsonParser = new JsonParser();
54 private static final Logger log = LoggerFactory.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, PropertyDefinition 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 log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue());
100 Object convertedObj = convertToToscaObject(property.getType(), property.getDefaultValue(), innerType, dataTypes);
101 if (convertedObj != null) {
102 prop.setDefaultp(convertedObj);
104 prop.setType(property.getType());
105 prop.setDescription(property.getDescription());
106 if (isCapabiltyProperty) {
107 prop.setStatus(property.getStatus());
108 prop.setRequired(property.isRequired());
114 public Object convertToToscaObject(String propertyType, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
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)){
123 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
124 ToscaValueConverter innerConverter = null;
125 Boolean isScalar = true;
127 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
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;
135 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
136 log.trace("This is scalar type. get suitable converter for type {}", type);
137 innerConverter = type.getValueConverter();
142 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
143 if (typeIfScalar == null) {
147 innerConverter = type.getValueConverter();
148 if (ToscaPropertyType.STRING.equals(type) && valueStartsWithNonJsonChar(value)) {
149 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
152 JsonElement jsonElement = null;
154 StringReader reader = new StringReader(value);
155 JsonReader jsonReader = new JsonReader(reader);
156 jsonReader.setLenient(true);
158 jsonElement = jsonParser.parse(jsonReader);
160 if (value.equals("")) {
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);
169 log.trace("It's data type or inputs in primitive type. convert as map");
170 Object convertedValue;
171 if (innerConverter != null && (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(type))) {
172 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
175 // complex json for scalar type
176 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
178 if (innerConverter != null) {
179 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
181 convertedValue = mapConverterInst.convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement);
185 return convertedValue;
188 } catch (Exception e) {
189 log.debug("convertToToscaValue failed to parse json value :", e);
195 private boolean valueStartsWithNonJsonChar(String value) {
196 return value.startsWith("/") || value.startsWith(":");