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=========================================================
20 package org.openecomp.sdc.be.tosca;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25 import com.google.gson.stream.JsonReader;
26 import fj.data.Either;
27 import java.io.StringReader;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.Objects;
32 import java.util.function.Supplier;
33 import org.apache.commons.lang3.StringUtils;
34 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
36 import org.openecomp.sdc.be.model.Component;
37 import org.openecomp.sdc.be.model.DataTypeDefinition;
38 import org.openecomp.sdc.be.model.PropertyDefinition;
39 import org.openecomp.sdc.be.model.Resource;
40 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
41 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
42 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
43 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
44 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
45 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
46 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
47 import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
48 import org.openecomp.sdc.common.log.wrappers.Logger;
49 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
50 import org.springframework.stereotype.Service;
53 public class PropertyConvertor {
55 private static final Logger log = Logger.getLogger(PropertyConvertor.class);
57 public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType,
58 Map<String, DataTypeDefinition> dataTypes) {
59 if (component instanceof Resource) {
60 Resource resource = (Resource) component;
61 List<PropertyDefinition> props = resource.getProperties();
63 Map<String, ToscaProperty> properties = new HashMap<>();
64 // take only the properties of this resource
65 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
66 properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY));
68 if (!properties.isEmpty()) {
69 toscaNodeType.setProperties(properties);
73 return Either.left(toscaNodeType);
76 public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
77 ToscaProperty prop = new ToscaProperty();
78 log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(),
79 property.getDefaultValue());
80 SchemaDefinition schema = property.getSchema();
81 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
82 final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
83 toscaSchemaDefinition.setType(schema.getProperty().getType());
84 toscaSchemaDefinition.setDescription(schema.getProperty().getDescription());
85 prop.setEntry_schema(toscaSchemaDefinition);
87 String defaultValue = property.getDefaultValue();
88 if (Objects.isNull(defaultValue)) {
89 defaultValue = property.getValue();
91 Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false);
92 if (convertedObj != null) {
93 prop.setDefaultp(convertedObj);
95 prop.setType(property.getType());
96 prop.setDescription(property.getDescription());
97 prop.setRequired(property.isRequired());
98 if (propertyType.equals(PropertyType.CAPABILITY)) {
99 prop.setStatus(property.getStatus());
101 prop.setMetadata(property.getMetadata());
105 public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes,
106 boolean preserveEmptyValue) {
107 String propertyType = property.getType();
108 String innerType = property.getSchemaType();
109 log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
110 if (StringUtils.isEmpty(value)) {
111 value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
112 if (StringUtils.isEmpty(value)) {
117 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
118 ToscaValueConverter innerConverter = null;
119 boolean isScalar = true;
120 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
122 log.trace("isn't prederfined type, get from all data types");
123 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
124 if (innerType == null) {
125 innerType = propertyType;
127 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
128 log.trace("This is scalar type. get suitable converter for type {}", type);
129 innerConverter = type.getValueConverter();
134 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
135 if (typeIfScalar == null) {
138 innerConverter = type.getValueConverter();
139 if (ToscaPropertyType.STRING == type && valueStartsWithNonJsonChar(value)) {
140 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
143 JsonElement jsonElement = null;
144 StringReader reader = new StringReader(value);
145 JsonReader jsonReader = new JsonReader(reader);
146 jsonReader.setLenient(true);
147 jsonElement = JsonParser.parseReader(jsonReader);
148 if (value.equals("")) {
151 if (jsonElement.isJsonPrimitive() && isScalar) {
152 log.trace("It's well defined type. convert it");
153 ToscaValueConverter converter = type.getValueConverter();
154 return converter.convertToToscaValue(value, innerType, dataTypes);
156 log.trace("It's data type or inputs in primitive type. convert as map");
157 if (jsonElement.isJsonObject()) {
158 JsonObject jsonObj = jsonElement.getAsJsonObject();
159 // check if value is a get_input function
160 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
161 Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
162 log.debug("It's get_input function. obj={}", obj);
166 Object convertedValue;
167 if (innerConverter != null && (ToscaPropertyType.MAP == type || ToscaPropertyType.LIST == type)) {
168 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
169 } else if (isScalar) {
170 // complex json for scalar type
171 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
172 } else if (innerConverter != null) {
173 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
175 convertedValue = mapConverterInst
176 .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
178 return convertedValue;
179 } catch (Exception e) {
180 log.debug("convertToToscaValue failed to parse json value :", e);
185 private boolean valueStartsWithNonJsonChar(String value) {
186 return value.startsWith("/") || value.startsWith(":");
189 public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, Map<String, Object> props, PropertyDataDefinition prop,
190 Supplier<String> supplier) {
191 Object convertedValue = convertValue(dataTypes, prop, supplier);
192 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
193 props.put(prop.getName(), convertedValue);
197 private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes, T input, Supplier<String> supplier) {
198 return convertToToscaObject(input, supplier.get(), dataTypes, false);
201 public enum PropertyType {CAPABILITY, INPUT, PROPERTY}