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.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.Objects;
34 import java.util.function.Supplier;
35 import org.apache.commons.collections.CollectionUtils;
36 import org.apache.commons.lang3.StringUtils;
37 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
38 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
39 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
40 import org.openecomp.sdc.be.model.Component;
41 import org.openecomp.sdc.be.model.DataTypeDefinition;
42 import org.openecomp.sdc.be.model.PropertyConstraint;
43 import org.openecomp.sdc.be.model.PropertyDefinition;
44 import org.openecomp.sdc.be.model.Resource;
45 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
46 import org.openecomp.sdc.be.model.tosca.ToscaType;
47 import org.openecomp.sdc.be.model.tosca.constraints.EqualConstraint;
48 import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint;
49 import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint;
50 import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
51 import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint;
52 import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint;
53 import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint;
54 import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint;
55 import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint;
56 import org.openecomp.sdc.be.model.tosca.constraints.PatternConstraint;
57 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
58 import org.openecomp.sdc.be.model.tosca.constraints.exception.ConstraintValueDoNotMatchPropertyTypeException;
59 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
60 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
61 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
62 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
63 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
64 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
65 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint;
66 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintEqual;
67 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintGreaterOrEqual;
68 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintGreaterThan;
69 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintInRange;
70 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLength;
71 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessOrEqual;
72 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessThan;
73 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMaxLength;
74 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMinLength;
75 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintPattern;
76 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
77 import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
78 import org.openecomp.sdc.common.log.wrappers.Logger;
79 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
80 import org.springframework.stereotype.Service;
81 import org.yaml.snakeyaml.Yaml;
84 public class PropertyConvertor {
86 private static final Logger log = Logger.getLogger(PropertyConvertor.class);
88 public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType,
89 Map<String, DataTypeDefinition> dataTypes) {
90 if (component instanceof Resource) {
91 Resource resource = (Resource) component;
92 List<PropertyDefinition> props = resource.getProperties();
94 Map<String, ToscaProperty> properties = new HashMap<>();
95 // take only the properties of this resource
96 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property ->
97 properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY))
99 if (!properties.isEmpty()) {
100 toscaNodeType.setProperties(properties);
104 return Either.left(toscaNodeType);
107 public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
108 ToscaProperty prop = new ToscaProperty();
109 log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(),
110 property.getDefaultValue());
111 SchemaDefinition schema = property.getSchema();
112 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
113 final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
114 toscaSchemaDefinition.setType(schema.getProperty().getType());
115 toscaSchemaDefinition.setDescription(schema.getProperty().getDescription());
116 prop.setEntry_schema(toscaSchemaDefinition);
118 String defaultValue = property.getDefaultValue();
119 if (Objects.isNull(defaultValue)) {
120 defaultValue = property.getValue();
122 Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false);
123 if (convertedObj != null) {
124 prop.setDefaultp(convertedObj);
126 prop.setType(property.getType());
127 prop.setDescription(property.getDescription());
128 prop.setRequired(property.isRequired());
129 if (propertyType.equals(PropertyType.CAPABILITY)) {
130 prop.setStatus(property.getStatus());
132 prop.setMetadata(property.getMetadata());
134 if (CollectionUtils.isNotEmpty(property.getConstraints())) {
136 prop.setConstraints(convertConstraints(property.getConstraints(), property.getType()));
137 } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
138 log.error(e.getMessage());
144 private List<ToscaPropertyConstraint> convertConstraints(List<PropertyConstraint> constraints, String propertyType)
145 throws ConstraintValueDoNotMatchPropertyTypeException {
146 List<ToscaPropertyConstraint> convertedConstraints = new ArrayList<>();
147 for (PropertyConstraint constraint : constraints) {
148 if (constraint instanceof EqualConstraint) {
149 EqualConstraint equalConstraint = ((EqualConstraint) constraint);
151 if (doesPropertyTypeNeedConverted(propertyType)) {
152 equalConstraint.changeConstraintValueTypeTo(propertyType);
155 ToscaPropertyConstraintEqual prop = new ToscaPropertyConstraintEqual(equalConstraint.getEqual());
156 convertedConstraints.add(prop);
158 if (constraint instanceof GreaterThanConstraint) {
159 GreaterThanConstraint greaterThanConstraint = ((GreaterThanConstraint) constraint);
161 if (doesPropertyTypeNeedConverted(propertyType)) {
162 greaterThanConstraint.changeConstraintValueTypeTo(propertyType);
165 ToscaPropertyConstraintGreaterThan prop = new ToscaPropertyConstraintGreaterThan(greaterThanConstraint.getGreaterThan());
166 convertedConstraints.add(prop);
168 if (constraint instanceof GreaterOrEqualConstraint) {
169 GreaterOrEqualConstraint greaterOrEqualConstraint = ((GreaterOrEqualConstraint) constraint);
171 if (doesPropertyTypeNeedConverted(propertyType)) {
172 greaterOrEqualConstraint.changeConstraintValueTypeTo(propertyType);
175 ToscaPropertyConstraintGreaterOrEqual prop = new ToscaPropertyConstraintGreaterOrEqual(greaterOrEqualConstraint.getGreaterOrEqual());
176 convertedConstraints.add(prop);
178 if (constraint instanceof LessThanConstraint) {
179 LessThanConstraint lessThanConstraint = ((LessThanConstraint) constraint);
181 if (doesPropertyTypeNeedConverted(propertyType)) {
182 lessThanConstraint.changeConstraintValueTypeTo(propertyType);
185 ToscaPropertyConstraintLessThan prop = new ToscaPropertyConstraintLessThan(lessThanConstraint.getLessThan());
186 convertedConstraints.add(prop);
188 if (constraint instanceof LessOrEqualConstraint) {
189 LessOrEqualConstraint lessOrEqualConstraint = ((LessOrEqualConstraint) constraint);
191 if (doesPropertyTypeNeedConverted(propertyType)) {
192 lessOrEqualConstraint.changeConstraintValueTypeTo(propertyType);
195 ToscaPropertyConstraintLessOrEqual prop = new ToscaPropertyConstraintLessOrEqual(lessOrEqualConstraint.getLessOrEqual());
196 convertedConstraints.add(prop);
198 if (constraint instanceof InRangeConstraint) {
199 InRangeConstraint inRangeConstraint = (InRangeConstraint) constraint;
201 if (doesPropertyTypeNeedConverted(propertyType)) {
202 inRangeConstraint.changeConstraintValueTypeTo(propertyType);
205 List<Object> range = new ArrayList<>();
206 range.add(inRangeConstraint.getMin());
207 range.add(inRangeConstraint.getMax());
208 convertedConstraints.add(new ToscaPropertyConstraintInRange(range));
210 if (constraint instanceof ValidValuesConstraint) {
211 ValidValuesConstraint validValues = ((ValidValuesConstraint) constraint);
213 if (propertyType.equals(ToscaType.INTEGER.toString()) || propertyType.equals(ToscaType.FLOAT.toString())) {
214 validValues.changeConstraintValueTypeTo(propertyType);
217 List prop = validValues.getValidValues();
218 convertedConstraints.add(new ToscaPropertyConstraintValidValues(prop));
220 if (constraint instanceof LengthConstraint) {
221 convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength()));
223 if (constraint instanceof MinLengthConstraint) {
224 convertedConstraints.add(new ToscaPropertyConstraintMinLength(((MinLengthConstraint) constraint).getMinLength()));
226 if (constraint instanceof MaxLengthConstraint) {
227 convertedConstraints.add(new ToscaPropertyConstraintMaxLength(((MaxLengthConstraint) constraint).getMaxLength()));
229 if (constraint instanceof PatternConstraint) {
230 convertedConstraints.add(new ToscaPropertyConstraintPattern(((PatternConstraint) constraint).getPattern()));
233 return convertedConstraints;
236 private boolean doesPropertyTypeNeedConverted(String propertyType) {
237 return propertyType.equals(ToscaType.INTEGER.getType()) || propertyType.equals(ToscaType.FLOAT.getType());
240 public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes,
241 boolean preserveEmptyValue) {
242 String propertyType = property.getType();
243 String innerType = property.getSchemaType();
244 log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
245 if (StringUtils.isEmpty(value)) {
246 value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
247 if (StringUtils.isEmpty(value)) {
251 if (property.isToscaFunction() && property.getToscaFunction().getType() == ToscaFunctionType.YAML) {
252 return new Yaml().load(property.getValue());
255 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
256 ToscaValueConverter innerConverter = null;
257 boolean isScalar = true;
258 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
260 log.trace("isn't prederfined type, get from all data types");
261 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
262 if (innerType == null) {
263 innerType = propertyType;
265 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
266 log.trace("This is scalar type. get suitable converter for type {}", type);
267 innerConverter = type.getValueConverter();
272 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
273 if (typeIfScalar == null) {
276 innerConverter = type.getValueConverter();
277 if (ToscaPropertyType.STRING == type && valueStartsWithNonJsonChar(value)) {
278 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
281 JsonElement jsonElement = null;
282 StringReader reader = new StringReader(value);
283 JsonReader jsonReader = new JsonReader(reader);
284 jsonReader.setLenient(true);
285 jsonElement = JsonParser.parseReader(jsonReader);
286 if (value.equals("")) {
289 if (jsonElement.isJsonPrimitive() && isScalar) {
290 log.trace("It's well defined type. convert it");
291 ToscaValueConverter converter = type.getValueConverter();
292 return converter.convertToToscaValue(value, innerType, dataTypes);
294 log.trace("It's data type or inputs in primitive type. convert as map");
295 if (jsonElement.isJsonObject()) {
296 JsonObject jsonObj = jsonElement.getAsJsonObject();
297 // check if value is a get_input function
298 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
299 Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
300 log.debug("It's get_input function. obj={}", obj);
304 Object convertedValue;
305 if (innerConverter != null && (ToscaPropertyType.MAP == type || ToscaPropertyType.LIST == type)) {
306 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
307 } else if (isScalar) {
308 // complex json for scalar type
309 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
310 } else if (innerConverter != null) {
311 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
313 convertedValue = mapConverterInst
314 .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
316 return convertedValue;
318 } catch (JsonParseException e) {
319 log.trace("{} not parsable as JSON. Convert as YAML instead", value);
320 return new Yaml().load(value);
321 } catch (Exception e) {
322 log.debug("convertToToscaValue failed to parse json value :", e);
327 private boolean valueStartsWithNonJsonChar(String value) {
328 return value.startsWith("/") || value.startsWith(":");
331 public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, Map<String, Object> props, PropertyDataDefinition prop,
332 Supplier<String> supplier) {
333 Object convertedValue = convertValue(dataTypes, prop, supplier);
334 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
335 props.put(prop.getName(), convertedValue);
339 private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes, T input, Supplier<String> supplier) {
340 return convertToToscaObject(input, supplier.get(), dataTypes, false);
343 public enum PropertyType {CAPABILITY, INPUT, PROPERTY}