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.JsonParseException;
25 import com.google.gson.JsonParser;
26 import com.google.gson.stream.JsonReader;
27 import fj.data.Either;
28 import java.io.StringReader;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Objects;
33 import java.util.function.Supplier;
34 import org.apache.commons.lang3.StringUtils;
35 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
37 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
38 import org.openecomp.sdc.be.model.Component;
39 import org.openecomp.sdc.be.model.DataTypeDefinition;
40 import org.openecomp.sdc.be.model.PropertyDefinition;
41 import org.openecomp.sdc.be.model.Resource;
42 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
43 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
44 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
45 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
46 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
47 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
48 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
49 import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
50 import org.openecomp.sdc.common.log.wrappers.Logger;
51 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
52 import org.springframework.stereotype.Service;
53 import org.yaml.snakeyaml.Yaml;
56 public class PropertyConvertor {
58 private static final Logger log = Logger.getLogger(PropertyConvertor.class);
60 public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType,
61 Map<String, DataTypeDefinition> dataTypes) {
62 if (component instanceof Resource) {
63 Resource resource = (Resource) component;
64 List<PropertyDefinition> props = resource.getProperties();
66 Map<String, ToscaProperty> properties = new HashMap<>();
67 // take only the properties of this resource
68 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
69 properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY));
71 if (!properties.isEmpty()) {
72 toscaNodeType.setProperties(properties);
76 return Either.left(toscaNodeType);
79 public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
80 ToscaProperty prop = new ToscaProperty();
81 log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(),
82 property.getDefaultValue());
83 SchemaDefinition schema = property.getSchema();
84 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
85 final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
86 toscaSchemaDefinition.setType(schema.getProperty().getType());
87 toscaSchemaDefinition.setDescription(schema.getProperty().getDescription());
88 prop.setEntry_schema(toscaSchemaDefinition);
90 String defaultValue = property.getDefaultValue();
91 if (Objects.isNull(defaultValue)) {
92 defaultValue = property.getValue();
94 Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false);
95 if (convertedObj != null) {
96 prop.setDefaultp(convertedObj);
98 prop.setType(property.getType());
99 prop.setDescription(property.getDescription());
100 prop.setRequired(property.isRequired());
101 if (propertyType.equals(PropertyType.CAPABILITY)) {
102 prop.setStatus(property.getStatus());
104 prop.setMetadata(property.getMetadata());
108 public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes,
109 boolean preserveEmptyValue) {
110 String propertyType = property.getType();
111 String innerType = property.getSchemaType();
112 log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
113 if (StringUtils.isEmpty(value)) {
114 value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
115 if (StringUtils.isEmpty(value)) {
119 if (property.isToscaFunction() && property.getToscaFunction().getType() == ToscaFunctionType.YAML) {
120 return new Yaml().load(property.getValue());
123 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
124 ToscaValueConverter innerConverter = null;
125 boolean isScalar = true;
126 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
128 log.trace("isn't prederfined type, get from all data types");
129 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
130 if (innerType == null) {
131 innerType = propertyType;
133 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
134 log.trace("This is scalar type. get suitable converter for type {}", type);
135 innerConverter = type.getValueConverter();
140 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
141 if (typeIfScalar == null) {
144 innerConverter = type.getValueConverter();
145 if (ToscaPropertyType.STRING == type && valueStartsWithNonJsonChar(value)) {
146 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
149 JsonElement jsonElement = null;
150 StringReader reader = new StringReader(value);
151 JsonReader jsonReader = new JsonReader(reader);
152 jsonReader.setLenient(true);
153 jsonElement = JsonParser.parseReader(jsonReader);
154 if (value.equals("")) {
157 if (jsonElement.isJsonPrimitive() && isScalar) {
158 log.trace("It's well defined type. convert it");
159 ToscaValueConverter converter = type.getValueConverter();
160 return converter.convertToToscaValue(value, innerType, dataTypes);
162 log.trace("It's data type or inputs in primitive type. convert as map");
163 if (jsonElement.isJsonObject()) {
164 JsonObject jsonObj = jsonElement.getAsJsonObject();
165 // check if value is a get_input function
166 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
167 Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
168 log.debug("It's get_input function. obj={}", obj);
172 Object convertedValue;
173 if (innerConverter != null && (ToscaPropertyType.MAP == type || ToscaPropertyType.LIST == type)) {
174 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
175 } else if (isScalar) {
176 // complex json for scalar type
177 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
178 } else if (innerConverter != null) {
179 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
181 convertedValue = mapConverterInst
182 .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
184 return convertedValue;
186 } catch (JsonParseException e) {
187 log.trace("{} not parsable as JSON. Convert as YAML instead", value);
188 return new Yaml().load(value);
189 } catch (Exception e) {
190 log.debug("convertToToscaValue failed to parse json value :", e);
195 private boolean valueStartsWithNonJsonChar(String value) {
196 return value.startsWith("/") || value.startsWith(":");
199 public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, Map<String, Object> props, PropertyDataDefinition prop,
200 Supplier<String> supplier) {
201 Object convertedValue = convertValue(dataTypes, prop, supplier);
202 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
203 props.put(prop.getName(), convertedValue);
207 private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes, T input, Supplier<String> supplier) {
208 return convertToToscaObject(input, supplier.get(), dataTypes, false);
211 public enum PropertyType {CAPABILITY, INPUT, PROPERTY}