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.datatypes.elements.SchemaDefinition;
31 import org.openecomp.sdc.be.model.AttributeDefinition;
32 import org.openecomp.sdc.be.model.DataTypeDefinition;
33 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
34 import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter;
35 import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter;
36 import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter;
37 import org.openecomp.sdc.be.tosca.model.ToscaAttribute;
38 import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition;
39 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
42 import org.springframework.beans.factory.config.BeanDefinition;
43 import org.springframework.context.annotation.Scope;
44 import org.springframework.stereotype.Component;
47 * Handles conversions between attribute objects.
50 @Scope(BeanDefinition.SCOPE_PROTOTYPE)
51 public class AttributeConverter {
53 private static final Logger LOGGER = Logger.getLogger(AttributeConverter.class);
54 private final Map<String, DataTypeDefinition> dataTypes;
55 private final ToscaMapValueConverter toscaMapValueConverter;
58 * Creates an {@link AttributeConverter} with all the required data types.
60 * @param dataTypes all the data types required for the conversion
62 public AttributeConverter(final Map<String, DataTypeDefinition> dataTypes) {
63 this.dataTypes = dataTypes;
64 toscaMapValueConverter = ToscaMapValueConverter.getInstance();
68 * Converts an {@link AttributeDefinition} to a {@link ToscaAttribute}.
70 * @param attributeDefinition the attribute definition to be converted
71 * @return the {@link ToscaAttribute} instance based on the the given {@link AttributeDefinition} instance
73 public ToscaAttribute convert(final AttributeDefinition attributeDefinition) {
74 final ToscaAttribute toscaAttribute = new ToscaAttribute();
75 LOGGER.trace("Converting attribute '{}' from type '{}' with default value '{}'", attributeDefinition.getName(), attributeDefinition.getType(),
76 attributeDefinition.getDefaultValue());
77 SchemaDefinition schema = attributeDefinition.getSchema();
78 if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null
79 && StringUtils.isNotEmpty(schema.getProperty().getType())) {
80 final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
81 toscaSchemaDefinition.setType(schema.getProperty().getType());
82 toscaSchemaDefinition.setDescription(schema.getProperty().getDescription());
83 toscaAttribute.setEntrySchema(toscaSchemaDefinition);
85 toscaAttribute.setType(attributeDefinition.getType());
86 toscaAttribute.setDescription(attributeDefinition.getDescription());
87 toscaAttribute.setStatus(attributeDefinition.getStatus());
88 final Object defaultValue = convertToToscaObject(attributeDefinition, attributeDefinition.getDefaultValue(), false);
89 if (defaultValue != null) {
90 toscaAttribute.setDefault(defaultValue);
92 final Object value = convertToToscaObject(attributeDefinition, attributeDefinition.getValue(), false);
94 toscaAttribute.setValue(value);
96 return toscaAttribute;
99 private ToscaSchemaDefinition convert(final EntrySchema entrySchema) {
100 if (entrySchema == null) {
103 final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition();
104 toscaSchemaDefinition.setType(entrySchema.getType());
105 toscaSchemaDefinition.setDescription(entrySchema.getDescription());
106 return toscaSchemaDefinition;
109 private Object convertToToscaObject(final AttributeDefinition attributeDefinition,
111 final boolean preserveEmptyValue) {
112 final String name = attributeDefinition.getName();
113 final String attributeType = attributeDefinition.getType();
114 final EntrySchema schemaDefinition = attributeDefinition.getEntry_schema();
116 final String innerType = schemaDefinition == null ? attributeType : schemaDefinition.getType();
117 LOGGER.trace("Converting attribute '{}' of type '{}', value '{}', innerType '{}'", name, attributeType, value, innerType);
118 if (StringUtils.isEmpty(value)) {
119 value = getTypeDefaultValue(attributeType);
120 if (StringUtils.isEmpty(value)) {
125 boolean isScalar = true;
126 ToscaPropertyType predefinedType = ToscaPropertyType.isValidType(attributeType);
127 if (predefinedType == null) {
128 //not predefined, search in existing data types
129 final DataTypeDefinition dataTypeDefinition = dataTypes.get(attributeType);
130 predefinedType = toscaMapValueConverter.isScalarType(dataTypeDefinition);
131 if (predefinedType == null) {
135 isScalar = ToscaPropertyType.getTypeIfScalar(predefinedType.getType()) != null;
137 final JsonElement valueAsJson = parseToJson(value);
138 if (isValueGetAttribute(valueAsJson)) {
139 return ToscaMapValueConverter.getInstance().handleComplexJsonValue(valueAsJson.getAsJsonObject());
141 //if it has a converter
142 if (predefinedType != null && predefinedType.getValueConverter() != null) {
143 LOGGER.trace("It's well defined type. convert it");
144 return predefinedType.getValueConverter().convertToToscaValue(value, innerType, dataTypes);
146 //no converter but scalar
148 return toscaMapValueConverter.handleComplexJsonValue(valueAsJson);
150 //if it is a data type
151 return toscaMapValueConverter.convertDataTypeToToscaObject(innerType, dataTypes, null, false, valueAsJson,
152 preserveEmptyValue, false);
153 } catch (final JsonParseException e) {
154 final String errorMsg = "Failed to parse json value";
155 LOGGER.error(EcompLoggerErrorCode.SCHEMA_ERROR, "Attribute Converter", errorMsg, e);
157 } catch (final Exception e) {
158 final String errorMsg = "Unexpected error occurred while converting attribute value to TOSCA";
159 LOGGER.error(EcompLoggerErrorCode.UNKNOWN_ERROR, "Attribute Converter", errorMsg, e);
164 private JsonElement parseToJson(final String value) {
165 final StringReader reader = new StringReader(value);
166 final JsonReader jsonReader = new JsonReader(reader);
167 jsonReader.setLenient(true);
168 return new JsonParser().parse(jsonReader);
171 private boolean isValueGetAttribute(final JsonElement valueAsJson) {
172 if (!valueAsJson.isJsonObject()) {
175 final JsonObject jsonObj = valueAsJson.getAsJsonObject();
176 return jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_ATTRIBUTE.getFunctionName());
179 private String getTypeDefaultValue(final String attributeType) {
180 return DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(attributeType, dataTypes);
183 public void convertAndAddValue(final Map<String, Object> attribs,
184 final AttributeDefinition attribute) {
185 final Object convertedValue = convertToToscaObject(attribute, attribute.getValue(), false);
186 if (!ToscaValueBaseConverter.isEmptyObjectValue(convertedValue)) {
187 attribs.put(attribute.getName(), convertedValue);