diff options
author | andre.schmid <andre.schmid@est.tech> | 2021-02-02 19:18:22 +0000 |
---|---|---|
committer | Christophe Closset <christophe.closset@intl.att.com> | 2021-02-25 12:20:17 +0000 |
commit | 0aa493c70aebd63244beeddf0ef22147196c5489 (patch) | |
tree | 6557463275234f84d05d40c89c1bcfc7134298fc /catalog-be/src/main | |
parent | 70f93dfdd2b3a7c682b341223b6f63af43feb280 (diff) |
Improve import and export VFC TOSCA attributes
Improve the import and export VFC TOSCA attributes, addressing the
following concerns:
- brings the import and export logic very close to the properties logic,
as they are very similar structures
- fix import/export default values for complex types and list of simple
or complex types
- fix export of unnecessary/empty attribute entries
- creation of attributes during the initialization (base types)
- inheritance of parent attributes
Change-Id: Ic733e3455fc256595b5c2b1f48e19a13be27b5cc
Issue-ID: SDC-3466
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'catalog-be/src/main')
13 files changed, 249 insertions, 138 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java index 614b261b4a..ed26eade32 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java @@ -43,15 +43,14 @@ import java.util.function.Consumer; import java.util.function.Function; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringEscapeUtils; +import org.onap.sdc.tosca.datatypes.model.EntrySchema; import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.elements.Annotation; -import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; -import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.AnnotationTypeDefinition; import org.openecomp.sdc.be.model.AttributeDefinition; @@ -182,7 +181,8 @@ public final class ImportUtils { } public enum ResultStatusEnum { - ELEMENT_NOT_FOUND, GENERAL_ERROR, OK, INVALID_PROPERTY_DEFAULT_VALUE, INVALID_PROPERTY_TYPE, INVALID_PROPERTY_VALUE, MISSING_ENTRY_SCHEMA_TYPE, INVALID_PROPERTY_NAME + ELEMENT_NOT_FOUND, GENERAL_ERROR, OK, INVALID_PROPERTY_DEFAULT_VALUE, INVALID_PROPERTY_TYPE, + INVALID_PROPERTY_VALUE, MISSING_ENTRY_SCHEMA_TYPE, INVALID_PROPERTY_NAME, INVALID_ATTRIBUTE_NAME } public enum ToscaElementTypeEnum { @@ -422,7 +422,7 @@ public final class ImportUtils { setFieldBoolean(propertyValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> propertyDef.setPassword(Boolean.parseBoolean(pass))); setField(propertyValue, TypeUtils.ToscaTagNamesEnum.STATUS, propertyDef::setStatus); - setScheme(propertyValue, propertyDef); + setSchema(propertyValue, propertyDef); setPropertyConstraints(propertyValue, propertyDef); return propertyDef; @@ -506,7 +506,7 @@ public final class ImportUtils { setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.HIDDEN, hidden -> inputDef.setHidden(Boolean.parseBoolean(hidden))); setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IMMUTABLE, immutable -> inputDef.setImmutable(Boolean.parseBoolean(immutable))); - setScheme(inputValue, inputDef); + setSchema(inputValue, inputDef); setPropertyConstraints(inputValue, inputDef); return inputDef; } @@ -542,20 +542,27 @@ public final class ImportUtils { setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), attributeDef::set_default); - setScheme(attributeMap, attributeDef); + setEntrySchema(attributeMap, attributeDef); return attributeDef; } - private static void setScheme(Map<String, Object> propertyValue, - ToscaDataDefinition toscaDataDefinition) { - Either<Object, ResultStatusEnum> schemaElementRes = findSchemaElement(propertyValue); + private static void setSchema(final Map<String, Object> propertyValue, + final PropertyDefinition propertyDefinition) { + final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(propertyValue); if (schemaElementRes.isLeft()) { - SchemaDefinition schemaDef = getSchema(schemaElementRes.left().value()); - toscaDataDefinition.setSchema(schemaDef); + propertyDefinition.setSchema(getSchema(schemaElementRes.left().value())); } } - private static Either<Object, ResultStatusEnum> findSchemaElement(Map<String, Object> propertyValue) { + private static void setEntrySchema(final Map<String, Object> toscaJsonMap, + final AttributeDefinition attributeDefinition) { + final Either<Object, ResultStatusEnum> schemaElementRes = findEntrySchemaElement(toscaJsonMap); + if (schemaElementRes.isLeft()) { + attributeDefinition.setEntry_schema(createEntrySchema(schemaElementRes.left().value())); + } + } + + private static Either<Object, ResultStatusEnum> findEntrySchemaElement(final Map<String, Object> propertyValue) { return findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA, ToscaElementTypeEnum.ALL); } @@ -573,6 +580,18 @@ public final class ImportUtils { return schema; } + private static EntrySchema createEntrySchema(final Object toscaEntrySchemaObj) { + final EntrySchema entrySchema = new EntrySchema(); + if (toscaEntrySchemaObj instanceof String) { + entrySchema.setType((String) toscaEntrySchemaObj); + } else if (toscaEntrySchemaObj instanceof Map) { + final PropertyDefinition schemeProperty = createModuleProperty((Map<String, Object>) toscaEntrySchemaObj); + entrySchema.setType(schemeProperty.getType()); + entrySchema.setDescription(schemeProperty.getDescription()); + } + return entrySchema; + } + private static void setField(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) { Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaStringElement(toscaJson, tagName); @@ -600,6 +619,13 @@ public final class ImportUtils { } + public static Either<Map<String, AttributeDefinition>, ResultStatusEnum> getAttributes(final Map<String, Object> toscaJson) { + final Function<String, AttributeDefinition> elementGenByName = ImportUtils::createAttribute; + final Function<Map<String, Object>, AttributeDefinition> func = ImportUtils::createModuleAttribute; + + return getElements(toscaJson, ToscaTagNamesEnum.ATTRIBUTES, elementGenByName, func); + } + public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(Map<String, Object> toscaJson, AnnotationTypeOperations annotationTypeOperations) { Function<String, InputDefinition> elementGenByName = ImportUtils::createInputs; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java index 7e5e1b9568..383d7cda95 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java @@ -353,6 +353,7 @@ public class ResourceImportManager { } setCapabilities(toscaJson, resource, parentResource); setProperties(toscaJson, resource); + setAttributes(toscaJson, resource); setRequirements(toscaJson, resource, parentResource); setInterfaceLifecycle(toscaJson, resource); } else { @@ -537,6 +538,42 @@ public class ResourceImportManager { } } + private void setAttributes(final Map<String, Object> originalToscaJsonMap, final Resource resource) { + final Map<String, Object> toscaJsonMap = new HashMap<>(originalToscaJsonMap); + ImportUtils.removeElementFromJsonMap(toscaJsonMap, "capabilities"); + final Either<Map<String, AttributeDefinition>, ResultStatusEnum> getAttributeEither = ImportUtils + .getAttributes(toscaJsonMap); + + if (getAttributeEither.isRight()) { + final ResultStatusEnum resultStatus = getAttributeEither.right().value(); + if (resultStatus == ResultStatusEnum.ELEMENT_NOT_FOUND) { + return; + } + throw new ByActionStatusComponentException( + componentsUtils.convertFromResultStatusEnum(resultStatus, JsonPresentationFields.ATTRIBUTES)); + } + + final List<AttributeDefinition> attributeDefinitionList = new ArrayList<>(); + final Map<String, AttributeDefinition> attributeMap = getAttributeEither.left().value(); + if (MapUtils.isEmpty(attributeMap)) { + return; + } + for (final Entry<String, AttributeDefinition> entry : attributeMap.entrySet()) { + final String name = entry.getKey(); + if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) { + log.debug("Detected attribute with invalid name '{}' during resource '{}' import. ", + name, resource.getName()); + throw new ByActionStatusComponentException(componentsUtils + .convertFromResultStatusEnum(ResultStatusEnum.INVALID_ATTRIBUTE_NAME, + JsonPresentationFields.ATTRIBUTES)); + } + final AttributeDefinition attributeDefinition = entry.getValue(); + attributeDefinition.setName(name); + attributeDefinitionList.add(attributeDefinition); + } + resource.setAttributes(attributeDefinitionList); + } + private Resource setDerivedFrom(Map<String, Object> toscaJson, Resource resource) { Either<String, ResultStatusEnum> toscaDerivedFromElement = ImportUtils .findFirstToscaStringElement(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM); @@ -831,52 +868,12 @@ public class ResourceImportManager { final String payloadData = resourceMetaData.getPayloadData(); if (payloadData != null) { resource.setToscaVersion(getToscaVersion(payloadData)); - resource.setAttributes(getAttributes(payloadData)); } final List<CategoryDefinition> categories = resourceMetaData.getCategories(); calculateResourceIsAbstract(resource, categories); } - private List<AttributeDefinition> getAttributes(final String payloadData) { - final Map<String, Object> mappedToscaTemplate = decodePayload(payloadData); - - final List<AttributeDefinition> attributeDataDefinitionList = new ArrayList<>(); - - final Either<Map<String, Object>, ResultStatusEnum> firstToscaMapElement = ImportUtils - .findFirstToscaMapElement(mappedToscaTemplate, ToscaTagNamesEnum.ATTRIBUTES); - if (firstToscaMapElement.isRight()) { - return attributeDataDefinitionList; - } - final Map<String, Object> attributes = firstToscaMapElement.left().value(); - - final Iterator<Entry<String, Object>> propertiesNameValue = attributes.entrySet().iterator(); - while (propertiesNameValue.hasNext()) { - final Entry<String, Object> attributeNameValue = propertiesNameValue.next(); - final Object value = attributeNameValue.getValue(); - final String key = attributeNameValue.getKey(); - if (value instanceof Map) { - - final Map<String, Object> attributeMap = (Map<String, Object>) value; - - final AttributeDefinition attributeDefinition = new AttributeDefinition(); - attributeDefinition.setName(key); - - setField(attributeMap, ToscaTagNamesEnum.DESCRIPTION, attributeDefinition::setDescription); - setField(attributeMap, ToscaTagNamesEnum.TYPE, attributeDefinition::setType); - setField(attributeMap, ToscaTagNamesEnum.DEFAULT_VALUE, attributeDefinition::set_default); - setField(attributeMap, ToscaTagNamesEnum.STATUS, attributeDefinition::setStatus); - setField(attributeMap, ToscaTagNamesEnum.ENTRY_SCHEMA, attributeDefinition::setSchema); - attributeDataDefinitionList.add(attributeDefinition); - } else { - final AttributeDefinition attributeDefinition = new AttributeDefinition(); - attributeDefinition.setName(key); - attributeDataDefinitionList.add(attributeDefinition); - } - } - return attributeDataDefinitionList; - } - private Map<String, Object> decodePayload(final String payloadData) { final String decodedPayload = new String(Base64.decodeBase64(payloadData)); return (Map<String, Object>) new Yaml().load(decodedPayload); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java index 8f1f2c1c21..98b3c84081 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java @@ -1384,6 +1384,9 @@ public class ComponentsUtils { case MISSING_ENTRY_SCHEMA_TYPE: responseEnum = ActionStatus.INVALID_PROPERTY; break; + case INVALID_ATTRIBUTE_NAME: + responseEnum = ActionStatus.INVALID_ATTRIBUTE; + break; default: responseEnum = ActionStatus.GENERAL_ERROR; break; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AttributeServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AttributeServlet.java index a1500a4204..227627304a 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AttributeServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AttributeServlet.java @@ -130,7 +130,8 @@ public class AttributeServlet extends AbstractValidationsServlet { if (errorWrapper.isEmpty()) { AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class); - Either<AttributeDefinition, ResponseFormat> createAttribute = businessLogic.createAttribute(resourceId, attributeDataDefinition, userId); + Either<AttributeDefinition, ResponseFormat> createAttribute = businessLogic. + createAttribute(resourceId, attributeDataDefinition, userId); if (createAttribute.isRight()) { errorWrapper.setInnerElement(createAttribute.right().value()); } else { @@ -283,27 +284,6 @@ public class AttributeServlet extends AbstractValidationsServlet { } } - private void buildAttributeFromString(String data, Wrapper<AttributeDataDefinition> attributesWrapper, - Wrapper<ResponseFormat> errorWrapper) { - try { - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - final AttributeDataDefinition attribute = gson.fromJson(data, AttributeDataDefinition.class); - if (attribute == null) { - log.info(ATTRIBUTE_CONTENT_IS_INVALID, data); - ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT); - errorWrapper.setInnerElement(responseFormat); - } else { - attributesWrapper.setInnerElement(attribute); - } - - } catch (Exception e) { - ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT); - errorWrapper.setInnerElement(responseFormat); - log.debug(ATTRIBUTE_CONTENT_IS_INVALID, data, e); - log.info(ATTRIBUTE_CONTENT_IS_INVALID, data); - } - } - private AttributeDefinition convertJsonToObject(final String data, final Wrapper<ResponseFormat> errorWrapper) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java index 085c075add..a0f8d8b44f 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.tosca; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; import java.io.StringReader; @@ -32,7 +33,10 @@ import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter; +import org.openecomp.sdc.be.tosca.exception.ToscaConversionException; import org.openecomp.sdc.be.tosca.model.ToscaAttribute; +import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; import org.springframework.beans.factory.config.BeanDefinition; @@ -61,15 +65,16 @@ public class AttributeConverter { } /** - * Converts and {@link AttributeDefinition} to a {@link ToscaAttribute}. + * Converts an {@link AttributeDefinition} to a {@link ToscaAttribute}. * * @param attributeDefinition the attribute definition to be converted * @return the {@link ToscaAttribute} instance based on the the given {@link AttributeDefinition} instance */ - public ToscaAttribute convert(final AttributeDefinition attributeDefinition) { + public ToscaAttribute convert(final AttributeDefinition attributeDefinition) throws ToscaConversionException { final ToscaAttribute toscaAttribute = new ToscaAttribute(); LOGGER.trace("Converting attribute '{}' from type '{}' with default value '{}'", attributeDefinition.getName(), attributeDefinition.getType(), attributeDefinition.getDefaultValue()); + toscaAttribute.setEntrySchema(convert(attributeDefinition.getEntry_schema())); toscaAttribute.setType(attributeDefinition.getType()); toscaAttribute.setDescription(attributeDefinition.getDescription()); toscaAttribute.setStatus(attributeDefinition.getStatus()); @@ -87,8 +92,19 @@ public class AttributeConverter { return toscaAttribute; } + private ToscaSchemaDefinition convert(final EntrySchema entrySchema) { + if (entrySchema == null) { + return null; + } + + final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition(); + toscaSchemaDefinition.setType(entrySchema.getType()); + toscaSchemaDefinition.setDescription(entrySchema.getDescription()); + return toscaSchemaDefinition; + } + private Object convertToToscaObject(final String name, final String attributeType, String value, - final EntrySchema schemaDefinition, final boolean preserveEmptyValue) { + final EntrySchema schemaDefinition, final boolean preserveEmptyValue) throws ToscaConversionException { final String innerType = schemaDefinition == null ? attributeType : schemaDefinition.getType(); LOGGER.trace("Converting attribute '{}' of type '{}', value '{}', innerType '{}'", name, attributeType, value, innerType); @@ -114,12 +130,10 @@ public class AttributeConverter { isScalar = ToscaPropertyType.getTypeIfScalar(predefinedType.getType()) != null; } final JsonElement valueAsJson = parseToJson(value); - if (valueAsJson.isJsonObject()) { - final JsonObject jsonObj = valueAsJson.getAsJsonObject(); - if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_ATTRIBUTE.getFunctionName())) { - return ToscaMapValueConverter.getInstance().handleComplexJsonValue(valueAsJson); - } + if (isValueGetAttribute(valueAsJson)) { + return ToscaMapValueConverter.getInstance().handleComplexJsonValue(valueAsJson.getAsJsonObject()); } + //if it has a converter if (predefinedType != null && predefinedType.getValueConverter() != null) { LOGGER.trace("It's well defined type. convert it"); @@ -134,9 +148,16 @@ public class AttributeConverter { return toscaMapValueConverter.convertDataTypeToToscaObject( innerType, dataTypes, null, false, valueAsJson, preserveEmptyValue); + } catch (final JsonParseException e) { + final String errorMsg = "Failed to parse json value"; + LOGGER.error(EcompLoggerErrorCode.SCHEMA_ERROR, "Attribute Converter", + errorMsg, e); + throw new ToscaConversionException(errorMsg, e); } catch (final Exception e) { - LOGGER.debug("convertToToscaValue failed to parse json value :", e); - return null; + final String errorMsg = "Unexpected error occurred while converting attribute value to TOSCA"; + LOGGER.error(EcompLoggerErrorCode.UNKNOWN_ERROR, "Attribute Converter", + errorMsg, e); + throw new ToscaConversionException(errorMsg, e); } } @@ -148,8 +169,16 @@ public class AttributeConverter { return new JsonParser().parse(jsonReader); } + private boolean isValueGetAttribute(final JsonElement valueAsJson) { + if (!valueAsJson.isJsonObject()) { + return false; + } + final JsonObject jsonObj = valueAsJson.getAsJsonObject(); + return jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_ATTRIBUTE.getFunctionName()); + } + private String getTypeDefaultValue(final String attributeType) { return DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(attributeType, dataTypes); } -}
\ No newline at end of file +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java index 4c4f19bb87..59b859dae2 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java @@ -44,7 +44,7 @@ import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter; -import org.openecomp.sdc.be.tosca.model.EntrySchema; +import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaProperty; import org.openecomp.sdc.common.log.wrappers.Logger; @@ -86,10 +86,10 @@ public class PropertyConvertor { log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue()); SchemaDefinition schema = property.getSchema(); if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) { - EntrySchema eschema = new EntrySchema(); - eschema.setType(schema.getProperty().getType()); - eschema.setDescription(schema.getProperty().getDescription()); - prop.setEntry_schema(eschema); + final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition(); + toscaSchemaDefinition.setType(schema.getProperty().getType()); + toscaSchemaDefinition.setDescription(schema.getProperty().getDescription()); + prop.setEntry_schema(toscaSchemaDefinition); } String defaultValue = property.getDefaultValue(); if(Objects.isNull(defaultValue)) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index 0459b5d1c6..1f0270fee9 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -28,9 +28,7 @@ import static org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_ATTRIBUTE; import static org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_INPUT; import static org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_PROPERTY; -import com.fasterxml.jackson.databind.ObjectMapper; import fj.data.Either; -import java.beans.IntrospectionException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -102,9 +100,11 @@ import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; import org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType; import org.openecomp.sdc.be.tosca.builder.ToscaRelationshipBuilder; +import org.openecomp.sdc.be.tosca.exception.ToscaConversionException; import org.openecomp.sdc.be.tosca.model.CapabilityFilter; import org.openecomp.sdc.be.tosca.model.NodeFilter; import org.openecomp.sdc.be.tosca.model.SubstitutionMapping; +import org.openecomp.sdc.be.tosca.model.ToscaAttribute; import org.openecomp.sdc.be.tosca.model.ToscaCapability; import org.openecomp.sdc.be.tosca.model.ToscaDataType; import org.openecomp.sdc.be.tosca.model.ToscaGroupTemplate; @@ -122,6 +122,7 @@ import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate; import org.openecomp.sdc.be.tosca.utils.ForwardingPathToscaUtil; import org.openecomp.sdc.be.tosca.utils.InputConverter; import org.openecomp.sdc.be.tosca.utils.OutputConverter; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.yaml.snakeyaml.DumperOptions; @@ -324,7 +325,15 @@ public class ToscaExportHandler { topologyTemplate.setInputs(inputs); } - final Map<String, ToscaProperty> outputs = outputConverter.convert(component.getOutputs(), dataTypes); + final Map<String, ToscaProperty> outputs; + try { + outputs = outputConverter.convert(component.getOutputs(), dataTypes); + } catch (final ToscaConversionException e) { + log.error(EcompLoggerErrorCode.SCHEMA_ERROR, ToscaExportHandler.class.getName(), + "Could not parse component '{}' outputs. Component unique id '{}'.", + new Object[]{component.getName(), component.getUniqueId(), e}); + return Either.right(ToscaError.GENERAL_ERROR); + } if (!outputs.isEmpty()) { topologyTemplate.setOutputs(outputs); } @@ -748,6 +757,18 @@ public class ToscaExportHandler { .addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent); addInputsToProperties(dataTypes, inputDef, mergedProperties); + final Map<String, ToscaAttribute> toscaAttributeMap; + try { + toscaAttributeMap = convertToToscaAttributes(component.getAttributes(), dataTypes); + } catch (final ToscaConversionException e) { + log.error(EcompLoggerErrorCode.SCHEMA_ERROR, ToscaExportHandler.class.getName(), + "Could not parse component '{}' attributes. Component unique id '{}'.", + new Object[]{component.getName(), component.getUniqueId(), e}); + return Either.right(ToscaError.GENERAL_ERROR); + } + if (!toscaAttributeMap.isEmpty()) { + toscaNodeType.setAttributes(toscaAttributeMap); + } if (CollectionUtils.isNotEmpty(component.getProperties())) { List<PropertyDefinition> properties = component.getProperties(); Map<String, ToscaProperty> convertedProperties = properties.stream() @@ -790,6 +811,19 @@ public class ToscaExportHandler { return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes); } + private Map<String, ToscaAttribute> convertToToscaAttributes(final List<AttributeDefinition> attributeList, + final Map<String, DataTypeDefinition> dataTypes) throws ToscaConversionException { + if (CollectionUtils.isEmpty(attributeList)) { + return Collections.emptyMap(); + } + final AttributeConverter attributeConverter = new AttributeConverter(dataTypes); + final Map<String, ToscaAttribute> toscaAttributeMap = new HashMap<>(); + for (final AttributeDefinition attributeDefinition : attributeList) { + toscaAttributeMap.put(attributeDefinition.getName(), attributeConverter.convert(attributeDefinition)); + } + return toscaAttributeMap; + } + private Either<ToscaTemplate, ToscaError> convertReqCapAndTypeName(Map<String, Component> componentsCache, Component component, ToscaTemplate toscaNode, Map<String, ToscaNodeType> nodeTypes, @@ -1050,31 +1084,9 @@ public class ToscaExportHandler { : NATIVE_ROOT; toscaNodeType.setDerived_from(derivedFrom); } - if (component instanceof Resource) { - final List<AttributeDefinition> attributes = ((Resource) component).getAttributes(); - if (CollectionUtils.isNotEmpty(attributes)) { - final Map<String, Object> attributeDataDefinitionMap = new HashMap<>(); - attributes.forEach(attributeDataDefinition -> - buildAttributeData(attributeDataDefinition, attributeDataDefinitionMap)); - - toscaNodeType.setAttributes(attributeDataDefinitionMap); - } - } return toscaNodeType; } - private void buildAttributeData(final AttributeDefinition originalAttributeDefinition, - final Map<String, Object> attributeDataDefinitionMap) { - - attributeDataDefinitionMap.put(originalAttributeDefinition.getName(), - new ObjectMapper().convertValue(new org.onap.sdc.tosca.datatypes.model.AttributeDefinition( - originalAttributeDefinition.getType(), - originalAttributeDefinition.getDescription(), - originalAttributeDefinition.get_default(), - originalAttributeDefinition.getStatus(), - originalAttributeDefinition.getEntry_schema()), Object.class)); - } - private Either<Map<String, Object>, ToscaError> createProxyInterfaceTypes(Component container) { Map<String, Object> proxyInterfaceTypes = new HashMap<>(); @@ -1679,12 +1691,22 @@ public class ToscaExportHandler { CustomRepresenter() { super(); this.representers.put(ToscaPropertyAssignment.class, new RepresentToscaPropertyAssignment()); + this.representers.put(ToscaAttribute.class, new RepresentToscaAttribute()); // null representer is exceptional and it is stored as an instance // variable. this.nullRepresenter = new RepresentNull(); } + private class RepresentToscaAttribute implements Represent { + + @Override + public Node representData(Object data) { + final ToscaAttribute toscaAttribute = (ToscaAttribute) data; + return represent(toscaAttribute.asToscaMap()); + } + } + private class RepresentToscaPropertyAssignment implements Represent { public Node representData(Object data) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/exception/ToscaConversionException.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/exception/ToscaConversionException.java new file mode 100644 index 0000000000..2d37a35a96 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/exception/ToscaConversionException.java @@ -0,0 +1,27 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.exception; + +public class ToscaConversionException extends Exception { + + public ToscaConversionException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java index 891347d191..ea56e60a17 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java @@ -19,9 +19,12 @@ package org.openecomp.sdc.be.tosca.model; +import java.util.LinkedHashMap; +import java.util.Map; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; /** * Represents a TOSCA Attribute Definition (see TOSCA 1.3, Section 3.6.12 Attribute definition) @@ -42,6 +45,12 @@ public class ToscaAttribute { private String description; @Getter @Setter + private ToscaSchemaDefinition keySchema; + @Getter + @Setter + private ToscaSchemaDefinition entrySchema; + @Getter + @Setter private String status; public Object getDefault() { @@ -52,4 +61,33 @@ public class ToscaAttribute { this.defaultValue = defaultValue; } + /** + * Converts this object to Map, ignoring null fields. The entries of the map are based on the TOSCA Attribute Definition. + * + * @return a Map representing the TOSCA Attribute Definition. + */ + public Map<String, Object> asToscaMap() { + final Map<String, Object> toscaAttributeAsMap = new LinkedHashMap<>(); + if (getType() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.TYPE.getElementName(), getType()); + } + if (getDescription() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.DESCRIPTION.getElementName(), getDescription()); + } + if (getKeySchema() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.KEY_SCHEMA.getElementName(), getKeySchema()); + } + if (getEntrySchema() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.ENTRY_SCHEMA.getElementName(), getEntrySchema()); + } + if (getDefault() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.DEFAULT.getElementName(), getDefault()); + } + if (getStatus() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.STATUS.getElementName(), getStatus()); + } + + return toscaAttributeAsMap; + } + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeType.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeType.java index 856c430f65..c43486fab8 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeType.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeType.java @@ -40,6 +40,6 @@ public class ToscaNodeType { private Map<String, ToscaCapability> capabilities; private List<Map<String, ToscaRequirement>> requirements; - private Map<String, Object> attributes; + private Map<String, ToscaAttribute> attributes; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaProperty.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaProperty.java index 7ebb2cb00d..fb5c544511 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaProperty.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaProperty.java @@ -44,7 +44,7 @@ public class ToscaProperty { private Boolean required; @Getter @Setter - private EntrySchema entry_schema; + private ToscaSchemaDefinition entry_schema; @Getter @Setter private List<ToscaPropertyConstraint> constraints; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/EntrySchema.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaSchemaDefinition.java index f1a7dcb9e5..d02e9da6bd 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/EntrySchema.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaSchemaDefinition.java @@ -20,24 +20,12 @@ package org.openecomp.sdc.be.tosca.model; -public class EntrySchema { +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ToscaSchemaDefinition { private String type; private String description; - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/OutputConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/OutputConverter.java index 85400a747a..1e7fc81a3b 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/OutputConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/OutputConverter.java @@ -27,6 +27,7 @@ import org.apache.commons.collections.CollectionUtils; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.OutputDefinition; import org.openecomp.sdc.be.tosca.AttributeConverter; +import org.openecomp.sdc.be.tosca.exception.ToscaConversionException; import org.openecomp.sdc.be.tosca.model.ToscaAttribute; import org.openecomp.sdc.be.tosca.model.ToscaOutput; import org.openecomp.sdc.be.tosca.model.ToscaProperty; @@ -45,17 +46,17 @@ public class OutputConverter { } public Map<String, ToscaProperty> convert(final List<OutputDefinition> outputDefinitionList, - final Map<String, DataTypeDefinition> dataTypes) { + final Map<String, DataTypeDefinition> dataTypes) throws ToscaConversionException { final AttributeConverter attributeConverter = this.attributeConverterProvider.getObject(dataTypes); final Map<String, ToscaProperty> outputMap = new HashMap<>(); if (CollectionUtils.isEmpty(outputDefinitionList)) { return Collections.emptyMap(); } - outputDefinitionList.forEach(outputDefinition -> { + for (final OutputDefinition outputDefinition : outputDefinitionList) { final ToscaAttribute toscaAttribute = attributeConverter.convert(outputDefinition); final ToscaProperty toscaProperty = new ToscaOutput(toscaAttribute); outputMap.put(outputDefinition.getName(), toscaProperty); - }); + } return outputMap; } } |