2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
19 package org.openecomp.sdc.be.tosca;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParseException;
24 import com.google.gson.JsonParser;
25 import com.google.gson.stream.JsonReader;
26 import java.io.StringReader;
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.sdc.tosca.datatypes.model.EntrySchema;
30 import org.openecomp.sdc.be.model.AttributeDefinition;
31 import org.openecomp.sdc.be.model.DataTypeDefinition;
32 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
33 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
34 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
35 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
36 import org.openecomp.sdc.be.tosca.model.ToscaAttribute;
37 import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
38 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
39 import org.openecomp.sdc.common.log.wrappers.Logger;
40 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
41 import org.springframework.beans.factory.config.BeanDefinition;
42 import org.springframework.context.annotation.Scope;
43 import org.springframework.stereotype.Component;
46 * Handles conversions between attribute objects.
49 @Scope(BeanDefinition.SCOPE_PROTOTYPE)
50 public class AttributeConverter {
52 private static final Logger LOGGER = Logger.getLogger(AttributeConverter.class);
53 private final Map<String, DataTypeDefinition> dataTypes;
54 private final ToscaMapValueConverter toscaMapValueConverter;
57 * Creates an {@link AttributeConverter} with all the required data types.
59 * @param dataTypes all the data types required for the conversion
61 public AttributeConverter(final Map<String, DataTypeDefinition> dataTypes) {
62 this.dataTypes = dataTypes;
63 toscaMapValueConverter = ToscaMapValueConverter.getInstance();
67 * Converts an {@link AttributeDefinition} to a {@link ToscaAttribute}.
69 * @param attributeDefinition the attribute definition to be converted
70 * @return the {@link ToscaAttribute} instance based on the the given {@link AttributeDefinition} instance
72 public ToscaAttribute convert(final AttributeDefinition attributeDefinition) {
73 final ToscaAttribute toscaAttribute = new ToscaAttribute();
74 LOGGER.trace("Converting attribute '{}' from type '{}' with default value '{}'", attributeDefinition.getName(), attributeDefinition.getType(),
75 attributeDefinition.getDefaultValue());
76 toscaAttribute.setEntrySchema(convert(attributeDefinition.getEntry_schema()));
77 toscaAttribute.setType(attributeDefinition.getType());
78 toscaAttribute.setDescription(attributeDefinition.getDescription());
79 toscaAttribute.setStatus(attributeDefinition.getStatus());
80 final Object defaultValue = convertToToscaObject(attributeDefinition, attributeDefinition.getDefaultValue(), false);
81 if (defaultValue != null) {
82 toscaAttribute.setDefault(defaultValue);
84 final Object value = convertToToscaObject(attributeDefinition, attributeDefinition.getValue(), false);
86 toscaAttribute.setValue(value);
88 return toscaAttribute;
91 private ToscaSchemaDefinition convert(final EntrySchema entrySchema) {
92 if (entrySchema == null) {
95 final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
96 toscaSchemaDefinition.setType(entrySchema.getType());
97 toscaSchemaDefinition.setDescription(entrySchema.getDescription());
98 return toscaSchemaDefinition;
101 private Object convertToToscaObject(final AttributeDefinition attributeDefinition,
103 final boolean preserveEmptyValue) {
104 final String name = attributeDefinition.getName();
105 final String attributeType = attributeDefinition.getType();
106 final EntrySchema schemaDefinition = attributeDefinition.getEntry_schema();
108 final String innerType = schemaDefinition == null ? attributeType : schemaDefinition.getType();
109 LOGGER.trace("Converting attribute '{}' of type '{}', value '{}', innerType '{}'", name, attributeType, value, innerType);
110 if (StringUtils.isEmpty(value)) {
111 value = getTypeDefaultValue(attributeType);
112 if (StringUtils.isEmpty(value)) {
117 boolean isScalar = true;
118 ToscaPropertyType predefinedType = ToscaPropertyType.isValidType(attributeType);
119 if (predefinedType == null) {
120 //not predefined, search in existing data types
121 final DataTypeDefinition dataTypeDefinition = dataTypes.get(attributeType);
122 predefinedType = toscaMapValueConverter.isScalarType(dataTypeDefinition);
123 if (predefinedType == null) {
127 isScalar = ToscaPropertyType.getTypeIfScalar(predefinedType.getType()) != null;
129 final JsonElement valueAsJson = parseToJson(value);
130 if (isValueGetAttribute(valueAsJson)) {
131 return ToscaMapValueConverter.getInstance().handleComplexJsonValue(valueAsJson.getAsJsonObject());
133 //if it has a converter
134 if (predefinedType != null && predefinedType.getValueConverter() != null) {
135 LOGGER.trace("It's well defined type. convert it");
136 return predefinedType.getValueConverter().convertToToscaValue(value, innerType, dataTypes);
138 //no converter but scalar
140 return toscaMapValueConverter.handleComplexJsonValue(valueAsJson);
142 //if it is a data type
143 return toscaMapValueConverter.convertDataTypeToToscaObject(innerType, dataTypes, null, false, valueAsJson, preserveEmptyValue);
144 } catch (final JsonParseException e) {
145 final String errorMsg = "Failed to parse json value";
146 LOGGER.error(EcompLoggerErrorCode.SCHEMA_ERROR, "Attribute Converter", errorMsg, e);
148 } catch (final Exception e) {
149 final String errorMsg = "Unexpected error occurred while converting attribute value to TOSCA";
150 LOGGER.error(EcompLoggerErrorCode.UNKNOWN_ERROR, "Attribute Converter", errorMsg, e);
155 private JsonElement parseToJson(final String value) {
156 final StringReader reader = new StringReader(value);
157 final JsonReader jsonReader = new JsonReader(reader);
158 jsonReader.setLenient(true);
159 return new JsonParser().parse(jsonReader);
162 private boolean isValueGetAttribute(final JsonElement valueAsJson) {
163 if (!valueAsJson.isJsonObject()) {
166 final JsonObject jsonObj = valueAsJson.getAsJsonObject();
167 return jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_ATTRIBUTE.getFunctionName());
170 private String getTypeDefaultValue(final String attributeType) {
171 return DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(attributeType, dataTypes);
174 public void convertAndAddValue(final Map<String, Object> attribs,
175 final AttributeDefinition attribute) {
176 final Object convertedValue = convertToToscaObject(attribute, attribute.getDefaultValue(), false);
177 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
178 attribs.put(attribute.getName(), convertedValue);