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.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 java.io.StringReader;
45 import java.util.HashMap;
46 import java.util.List;
49 public class PropertyConvertor {
50 private static PropertyConvertor instance;
51 private JsonParser jsonParser = new JsonParser();
52 private static Logger log = LoggerFactory.getLogger(PropertyConvertor.class.getName());
53 Gson gson = new Gson();
54 protected PropertyConvertor() {
58 public static synchronized PropertyConvertor getInstance() {
59 if (instance == null) {
60 instance = new PropertyConvertor();
65 public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType, Map<String, DataTypeDefinition> dataTypes) {
67 if (component instanceof Resource) {
68 Resource resource = (Resource) component;
69 List<PropertyDefinition> props = resource.getProperties();
71 Map<String, ToscaProperty> properties = new HashMap<>();
73 // take only the properties of this resource
74 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
75 ToscaProperty prop = convertProperty(dataTypes, property, false);
78 properties.put(property.getName(), prop);
81 if (!properties.isEmpty()) {
82 toscaNodeType.setProperties(properties);
86 return Either.left(toscaNodeType);
89 public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, boolean isCapabiltyProperty) {
90 ToscaProperty prop = new ToscaProperty();
92 String innerType = null;
93 SchemaDefinition schema = property.getSchema();
94 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
95 innerType = schema.getProperty().getType();
96 EntrySchema eschema = new EntrySchema();
97 eschema.setType(innerType);
98 eschema.setDescription(schema.getProperty().getDescription());
99 prop.setEntry_schema(eschema);
101 log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue());
102 prop.setDefaultp(convertToToscaObject(property.getType(), property.getDefaultValue(), innerType, dataTypes));
104 if (prop.getDefaultp() == null) {
107 prop.setType(property.getType());
108 prop.setDescription(property.getDescription());
109 if (isCapabiltyProperty) {
110 prop.setStatus(property.getStatus());
111 prop.setRequired(property.isRequired());
116 public Object convertToToscaObject(String propertyType, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
117 log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
118 if (StringUtils.isEmpty(value)) {
119 value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
120 if(StringUtils.isEmpty(value)){
126 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
127 ToscaValueConverter innerConverter = null;
128 Boolean isScalar = true;
130 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
132 log.trace("isn't prederfined type, get from all data types");
133 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
134 if (innerType == null) {
135 innerType = propertyType;
138 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
139 log.trace("This is scalar type. get suitable converter for type {}", type);
140 innerConverter = type.getValueConverter();
145 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
146 if (typeIfScalar == null) {
150 innerConverter = type.getValueConverter();
151 if (ToscaPropertyType.STRING.equals(type) && value.startsWith("/")) {
152 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
155 JsonElement jsonElement = null;
157 StringReader reader = new StringReader(value);
158 JsonReader jsonReader = new JsonReader(reader);
159 jsonReader.setLenient(true);
161 jsonElement = jsonParser.parse(jsonReader);
163 if (value.equals("")) {
167 if (jsonElement.isJsonPrimitive() && isScalar) {
168 log.trace("It's well defined type. convert it");
169 ToscaValueConverter converter = type.getValueConverter();
170 return converter.convertToToscaValue(value, innerType, dataTypes);
172 log.trace("It's data type or inputs in primitive type. convert as map");
173 Object convertedValue;
174 if (innerConverter != null && (ToscaPropertyType.MAP.equals(type) || ToscaPropertyType.LIST.equals(type))) {
175 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
178 // complex json for scalar type
179 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
181 if (innerConverter != null) {
182 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
184 convertedValue = mapConverterInst.convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement);
188 return convertedValue;
191 } catch (Exception e) {
192 log.debug("convertToToscaValue failed to parse json value :", e);