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.constraints.EqualConstraint;
47 import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint;
48 import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint;
49 import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
50 import org.openecomp.sdc.be.model.tosca.constraints.LengthConstraint;
51 import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint;
52 import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint;
53 import org.openecomp.sdc.be.model.tosca.constraints.MaxLengthConstraint;
54 import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint;
55 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
56 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
57 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
58 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
59 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter;
60 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
61 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
62 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint;
63 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintEqual;
64 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintGreaterOrEqual;
65 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintGreaterThan;
66 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintInRange;
67 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLength;
68 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessOrEqual;
69 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintLessThan;
70 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMaxLength;
71 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintMinLength;
72 import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues;
73 import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
74 import org.openecomp.sdc.common.log.wrappers.Logger;
75 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
76 import org.springframework.stereotype.Service;
77 import org.yaml.snakeyaml.Yaml;
81 public class PropertyConvertor {
83 private static final Logger log = Logger.getLogger(PropertyConvertor.class);
85 public Either<ToscaNodeType, ToscaError> convertProperties(Component component, ToscaNodeType toscaNodeType,
86 Map<String, DataTypeDefinition> dataTypes) {
87 if (component instanceof Resource) {
88 Resource resource = (Resource) component;
89 List<PropertyDefinition> props = resource.getProperties();
91 Map<String, ToscaProperty> properties = new HashMap<>();
92 // take only the properties of this resource
93 props.stream().filter(p -> p.getOwnerId() == null || p.getOwnerId().equals(component.getUniqueId())).forEach(property -> {
94 properties.put(property.getName(), convertProperty(dataTypes, property, PropertyType.PROPERTY));
96 if (!properties.isEmpty()) {
97 toscaNodeType.setProperties(properties);
101 return Either.left(toscaNodeType);
104 public ToscaProperty convertProperty(Map<String, DataTypeDefinition> dataTypes, PropertyDefinition property, PropertyType propertyType) {
105 ToscaProperty prop = new ToscaProperty();
106 log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(),
107 property.getDefaultValue());
108 SchemaDefinition schema = property.getSchema();
109 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) {
110 final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
111 toscaSchemaDefinition.setType(schema.getProperty().getType());
112 toscaSchemaDefinition.setDescription(schema.getProperty().getDescription());
113 prop.setEntry_schema(toscaSchemaDefinition);
115 String defaultValue = property.getDefaultValue();
116 if (Objects.isNull(defaultValue)) {
117 defaultValue = property.getValue();
119 Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false);
120 if (convertedObj != null) {
121 prop.setDefaultp(convertedObj);
123 prop.setType(property.getType());
124 prop.setDescription(property.getDescription());
125 prop.setRequired(property.isRequired());
126 if (propertyType.equals(PropertyType.CAPABILITY)) {
127 prop.setStatus(property.getStatus());
129 prop.setMetadata(property.getMetadata());
131 List<ToscaPropertyConstraint> constraints = new ArrayList<>();
132 if (CollectionUtils.isNotEmpty(property.getConstraints())) {
133 constraints = convertConstraints(property.getConstraints());
134 prop.setConstraints(constraints);
139 private List<ToscaPropertyConstraint> convertConstraints(List<PropertyConstraint> constraints) {
140 List<ToscaPropertyConstraint> convertedConstraints = new ArrayList<>();
141 for (PropertyConstraint constraint: constraints){
142 if (constraint instanceof EqualConstraint) {
143 convertedConstraints.add(new ToscaPropertyConstraintEqual(((EqualConstraint) constraint).getEqual()));
145 if (constraint instanceof GreaterThanConstraint) {
146 convertedConstraints.add(new ToscaPropertyConstraintGreaterThan(((GreaterThanConstraint) constraint).getGreaterThan()));
148 if (constraint instanceof GreaterOrEqualConstraint) {
149 convertedConstraints.add(new ToscaPropertyConstraintGreaterOrEqual(((GreaterOrEqualConstraint) constraint).getGreaterOrEqual()));
151 if (constraint instanceof LessThanConstraint) {
152 convertedConstraints.add(new ToscaPropertyConstraintLessThan(((LessThanConstraint) constraint).getLessThan()));
154 if (constraint instanceof LessOrEqualConstraint) {
155 convertedConstraints.add(new ToscaPropertyConstraintLessOrEqual(((LessOrEqualConstraint) constraint).getLessOrEqual()));
157 if (constraint instanceof InRangeConstraint) {
158 InRangeConstraint inRangeConstraint = (InRangeConstraint) constraint;
159 List<String> range = new ArrayList<>();
160 range.add(inRangeConstraint.getRangeMinValue());
161 range.add(inRangeConstraint.getRangeMaxValue());
162 convertedConstraints.add(new ToscaPropertyConstraintInRange(range));
164 if (constraint instanceof ValidValuesConstraint) {
165 convertedConstraints.add(new ToscaPropertyConstraintValidValues(((ValidValuesConstraint) constraint).getValidValues()));
167 if (constraint instanceof LengthConstraint) {
168 convertedConstraints.add(new ToscaPropertyConstraintLength(((LengthConstraint) constraint).getLength().toString()));
170 if (constraint instanceof MinLengthConstraint) {
171 convertedConstraints.add(new ToscaPropertyConstraintMinLength(((MinLengthConstraint) constraint).getMinLength()));
173 if (constraint instanceof MaxLengthConstraint) {
174 convertedConstraints.add(new ToscaPropertyConstraintMaxLength(((MaxLengthConstraint) constraint).getMaxLength()));
177 return convertedConstraints;
180 public Object convertToToscaObject(PropertyDataDefinition property, String value, Map<String, DataTypeDefinition> dataTypes,
181 boolean preserveEmptyValue) {
182 String propertyType = property.getType();
183 String innerType = property.getSchemaType();
184 log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType);
185 if (StringUtils.isEmpty(value)) {
186 value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes);
187 if (StringUtils.isEmpty(value)) {
191 if (property.isToscaFunction() && property.getToscaFunction().getType() == ToscaFunctionType.YAML) {
192 return new Yaml().load(property.getValue());
195 ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance();
196 ToscaValueConverter innerConverter = null;
197 boolean isScalar = true;
198 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
200 log.trace("isn't prederfined type, get from all data types");
201 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
202 if (innerType == null) {
203 innerType = propertyType;
205 if ((type = mapConverterInst.isScalarType(dataTypeDefinition)) != null) {
206 log.trace("This is scalar type. get suitable converter for type {}", type);
207 innerConverter = type.getValueConverter();
212 ToscaPropertyType typeIfScalar = ToscaPropertyType.getTypeIfScalar(type.getType());
213 if (typeIfScalar == null) {
216 innerConverter = type.getValueConverter();
217 if (ToscaPropertyType.STRING == type && valueStartsWithNonJsonChar(value)) {
218 return innerConverter.convertToToscaValue(value, innerType, dataTypes);
221 JsonElement jsonElement = null;
222 StringReader reader = new StringReader(value);
223 JsonReader jsonReader = new JsonReader(reader);
224 jsonReader.setLenient(true);
225 jsonElement = JsonParser.parseReader(jsonReader);
226 if (value.equals("")) {
229 if (jsonElement.isJsonPrimitive() && isScalar) {
230 log.trace("It's well defined type. convert it");
231 ToscaValueConverter converter = type.getValueConverter();
232 return converter.convertToToscaValue(value, innerType, dataTypes);
234 log.trace("It's data type or inputs in primitive type. convert as map");
235 if (jsonElement.isJsonObject()) {
236 JsonObject jsonObj = jsonElement.getAsJsonObject();
237 // check if value is a get_input function
238 if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_INPUT.getFunctionName())) {
239 Object obj = mapConverterInst.handleComplexJsonValue(jsonElement);
240 log.debug("It's get_input function. obj={}", obj);
244 Object convertedValue;
245 if (innerConverter != null && (ToscaPropertyType.MAP == type || ToscaPropertyType.LIST == type)) {
246 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
247 } else if (isScalar) {
248 // complex json for scalar type
249 convertedValue = mapConverterInst.handleComplexJsonValue(jsonElement);
250 } else if (innerConverter != null) {
251 convertedValue = innerConverter.convertToToscaValue(value, innerType, dataTypes);
253 convertedValue = mapConverterInst
254 .convertDataTypeToToscaObject(innerType, dataTypes, innerConverter, isScalar, jsonElement, preserveEmptyValue);
256 return convertedValue;
258 } catch (JsonParseException e) {
259 log.trace("{} not parsable as JSON. Convert as YAML instead", value);
260 return new Yaml().load(value);
261 } catch (Exception e) {
262 log.debug("convertToToscaValue failed to parse json value :", e);
267 private boolean valueStartsWithNonJsonChar(String value) {
268 return value.startsWith("/") || value.startsWith(":");
271 public void convertAndAddValue(Map<String, DataTypeDefinition> dataTypes, Map<String, Object> props, PropertyDataDefinition prop,
272 Supplier<String> supplier) {
273 Object convertedValue = convertValue(dataTypes, prop, supplier);
274 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
275 props.put(prop.getName(), convertedValue);
279 private <T extends PropertyDataDefinition> Object convertValue(Map<String, DataTypeDefinition> dataTypes, T input, Supplier<String> supplier) {
280 return convertToToscaObject(input, supplier.get(), dataTypes, false);
283 public enum PropertyType {CAPABILITY, INPUT, PROPERTY}