diff options
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java | 372 |
1 files changed, 207 insertions, 165 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 4b9203de42..8b69a4575c 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 @@ -20,30 +20,28 @@ package org.openecomp.sdc.be.components.impl; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.function.Consumer; -import java.util.function.Function; - +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import fj.data.Either; import org.apache.commons.lang3.StringEscapeUtils; +import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils; +import org.openecomp.sdc.be.datatypes.elements.Annotation; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; -import org.openecomp.sdc.be.model.HeatParameterDefinition; -import org.openecomp.sdc.be.model.InputDefinition; -import org.openecomp.sdc.be.model.LifecycleStateEnum; -import org.openecomp.sdc.be.model.PropertyConstraint; -import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.model.*; import org.openecomp.sdc.be.model.heat.HeatParameterType; +import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; +import org.openecomp.sdc.be.utils.TypeUtils; +import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; import org.openecomp.sdc.common.api.ArtifactTypeEnum; +import org.openecomp.sdc.common.log.wrappers.Logger; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.YamlProcessor; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -52,11 +50,16 @@ import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.representer.Representer; import org.yaml.snakeyaml.resolver.Resolver; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; +import java.lang.reflect.Type; +import java.util.*; +import java.util.Map.Entry; +import java.util.function.Consumer; +import java.util.function.Function; + +import static org.apache.commons.collections.CollectionUtils.isEmpty; +import static org.openecomp.sdc.be.components.impl.ResourceImportManager.PROPERTY_NAME_PATTERN_IGNORE_LENGTH; +import static org.openecomp.sdc.be.datatypes.elements.Annotation.setAnnotationsName; -import fj.data.Either; public final class ImportUtils { private ImportUtils() { @@ -64,7 +67,15 @@ public final class ImportUtils { } private static CustomResolver customResolver = new CustomResolver(); - private static Yaml STRICT_MAPPING_YAML_LOADER = new YamlLoader().getStrictYamlLoader(); + private static Yaml strictYamlLoader = new YamlLoader().getStrictYamlLoader(); + + @Autowired + protected static ComponentsUtils componentsUtils; + + @Autowired + private ExceptionUtils exceptionUtils; + + private static final Logger log = Logger.getLogger(ImportUtils.class); private static class CustomResolver extends Resolver { @Override @@ -106,7 +117,7 @@ public final class ImportUtils { public static Map<String, Object> loadYamlAsStrictMap(String content){ Map<String, Object> result = new LinkedHashMap<>(); - Object map = STRICT_MAPPING_YAML_LOADER.load(content); + Object map = strictYamlLoader.load(content); buildMap(result, (Map<String, Object>)map); return result; } @@ -124,8 +135,6 @@ public final class ImportUtils { } public static class Constants { - - public static final String FIRST_CERTIFIED_VERSION_VERSION = "1.0"; public static final String FIRST_NON_CERTIFIED_VERSION = "0.1"; public static final String VENDOR_NAME = "ATT (Tosca)"; public static final String VENDOR_RELEASE = "1.0.0.wd03"; @@ -139,6 +148,8 @@ public final class ImportUtils { public static final String USER_DEFINED_RESOURCE_NAMESPACE_PREFIX = "org.openecomp.resource."; public static final String UI_JSON_PAYLOAD_NAME = "payloadName"; public static final String CVFC_DESCRIPTION = "Complex node type that is used as nested type in VF"; + + private Constants() {} } public enum ResultStatusEnum { @@ -149,37 +160,6 @@ public final class ImportUtils { BOOLEAN, STRING, MAP, LIST, ALL } - public enum ToscaTagNamesEnum { - DERIVED_FROM("derived_from"), IS_PASSWORD("is_password"), - // Properties - PROPERTIES("properties"), TYPE("type"), STATUS("status"), ENTRY_SCHEMA("entry_schema"), REQUIRED("required"), DESCRIPTION("description"), DEFAULT_VALUE("default"), VALUE("value"), CONSTRAINTS("constraints"), - // Group Types - MEMBERS("members"), METADATA("metadata"), - // Policy Types - TARGETS("targets"), - // Capabilities - CAPABILITIES("capabilities"), VALID_SOURCE_TYPES("valid_source_types"), - // Requirements - REQUIREMENTS("requirements"), NODE("node"), RELATIONSHIP("relationship"), CAPABILITY("capability"), INTERFACES("interfaces"), - // Heat env Validation - PARAMETERS("parameters"), - // Import Validations - TOSCA_VERSION("tosca_definitions_version"), TOPOLOGY_TEMPLATE("topology_template"), NODE_TYPES("node_types"), OCCURRENCES("occurrences"), NODE_TEMPLATES("node_templates"), GROUPS("groups"), INPUTS("inputs"), - SUBSTITUTION_MAPPINGS("substitution_mappings"), NODE_TYPE("node_type"), - // Attributes - ATTRIBUTES("attributes"), LABEL("label"), HIDDEN("hidden"), IMMUTABLE("immutable"), GET_INPUT("get_input"); - - private String elementName; - - private ToscaTagNamesEnum(String elementName) { - this.elementName = elementName; - } - - public String getElementName() { - return elementName; - } - } - @SuppressWarnings("unchecked") private static void handleElementNameNotFound(String elementName, Object elementValue, ToscaElementTypeEnum elementType, List<Object> returnedList) { if (elementValue instanceof Map) { @@ -190,7 +170,7 @@ public final class ImportUtils { } @SuppressWarnings("unchecked") - private static void handleElementNameFound(String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList, Object elementValue) { + private static void addFoundElementAccordingToItsType(String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList, Object elementValue) { if (elementValue instanceof Boolean) { if (elementType == ToscaElementTypeEnum.BOOLEAN || elementType == ToscaElementTypeEnum.ALL) { @@ -216,10 +196,8 @@ public final class ImportUtils { } // For Integer, Double etc... - else if (elementType == ToscaElementTypeEnum.ALL) { - if (elementValue != null) { - returnedList.add(String.valueOf(elementValue)); - } + else if (elementType == ToscaElementTypeEnum.ALL && elementValue != null) { + returnedList.add(String.valueOf(elementValue)); } } @@ -232,15 +210,13 @@ public final class ImportUtils { } - public static Either<Object, ResultStatusEnum> findToscaElement(Map<String, Object> toscaJson, ToscaTagNamesEnum elementName, ToscaElementTypeEnum elementType) { + public static Either<Object, ResultStatusEnum> findToscaElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementName, ToscaElementTypeEnum elementType) { List<Object> foundElements = new ArrayList<>(); - Either<Object, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); ImportUtils.findToscaElements(toscaJson, elementName.getElementName(), elementType, foundElements); - if (foundElements.size() > 0) { - returnedElement = Either.left(foundElements.get(0)); + if (!isEmpty(foundElements)) { + return Either.left(foundElements.get(0)); } - return returnedElement; - + return Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); } /** @@ -250,16 +226,13 @@ public final class ImportUtils { * Or ELEMENT_NOT_FOUND ActionStatus * * @param toscaJson - * @param toscaTagName * @return */ public static Either<List<Object>, ResultStatusEnum> findToscaElements(Map<String, Object> toscaJson, String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList) { Either<List<Object>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); String skipKey = null; if (toscaJson.containsKey(elementName)) { - Object elementValue = toscaJson.get(elementName); - handleElementNameFound(elementName, elementType, returnedList, elementValue); - skipKey = elementName; + skipKey = handleFoundElement(toscaJson, elementName, elementType, returnedList); } Iterator<Entry<String, Object>> keyValItr = toscaJson.entrySet().iterator(); @@ -270,15 +243,22 @@ public final class ImportUtils { } } - if (returnedList.size() > 0) { + if (!isEmpty(returnedList)) { returnedElement = Either.left(returnedList); } return returnedElement; } + private static String handleFoundElement(Map<String, Object> toscaJson, String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList) { + Object elementValue = toscaJson.get(elementName); + addFoundElementAccordingToItsType(elementName, elementType, returnedList, elementValue); + return elementName; + + } + @SuppressWarnings("unchecked") - public static <T> Either<List<T>, ResultStatusEnum> findFirstToscaListElement(Map<String, Object> toscaJson, ToscaTagNamesEnum toscaTagName) { + public static <T> Either<List<T>, ResultStatusEnum> findFirstToscaListElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { Either<List<T>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.LIST); if (findFirstToscaElement.isLeft()) { @@ -289,7 +269,7 @@ public final class ImportUtils { } @SuppressWarnings("unchecked") - public static <T> Either<Map<String, T>, ResultStatusEnum> findFirstToscaMapElement(Map<String, Object> toscaJson, ToscaTagNamesEnum toscaTagName) { + public static <T> Either<Map<String, T>, ResultStatusEnum> findFirstToscaMapElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { Either<Map<String, T>, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.MAP); if (findFirstToscaElement.isLeft()) { @@ -299,7 +279,7 @@ public final class ImportUtils { } - public static Either<String, ResultStatusEnum> findFirstToscaStringElement(Map<String, Object> toscaJson, ToscaTagNamesEnum toscaTagName) { + public static Either<String, ResultStatusEnum> findFirstToscaStringElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { Either<String, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); Either<Object, ResultStatusEnum> findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.STRING); if (findFirstToscaElements.isLeft()) { @@ -315,7 +295,7 @@ public final class ImportUtils { * @param toscaTagName * @return */ - public static Either<String, ResultStatusEnum> findFirstToscaBooleanElement(Map<String, Object> toscaJson, ToscaTagNamesEnum toscaTagName) { + public static Either<String, ResultStatusEnum> findFirstToscaBooleanElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { Either<String, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); Either<Object, ResultStatusEnum> findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.BOOLEAN); if (findFirstToscaElements.isLeft()) { @@ -325,7 +305,7 @@ public final class ImportUtils { } private static void setPropertyConstraints(Map<String, Object> propertyValue, PropertyDefinition property) { - Either<List<Object>, ResultStatusEnum> propertyFieldconstraints = findFirstToscaListElement(propertyValue, ToscaTagNamesEnum.CONSTRAINTS); + Either<List<Object>, ResultStatusEnum> propertyFieldconstraints = findFirstToscaListElement(propertyValue, TypeUtils.ToscaTagNamesEnum.CONSTRAINTS); if (propertyFieldconstraints.isLeft()) { List<Object> jsonConstraintList = propertyFieldconstraints.left().value(); @@ -345,78 +325,129 @@ public final class ImportUtils { public static PropertyDefinition createModuleProperty(Map<String, Object> propertyValue) { PropertyDefinition propertyDef = new PropertyDefinition(); - ImportUtils.setField(propertyValue, ToscaTagNamesEnum.TYPE, type -> propertyDef.setType(type)); - ImportUtils.setPropertyFieldRequired(propertyValue, propertyDef); - ImportUtils.setField(propertyValue, ToscaTagNamesEnum.DESCRIPTION, desc -> propertyDef.setDescription(desc)); + setField(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE, propertyDef::setType); + setPropertyFieldRequired(propertyValue, propertyDef); + setField(propertyValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, propertyDef::setDescription); - Either<Object, ResultStatusEnum> findToscaElement = ImportUtils.findToscaElement(propertyValue, ToscaTagNamesEnum.DEFAULT_VALUE, ToscaElementTypeEnum.ALL); - if (findToscaElement.isLeft()) { - String propertyJsonStringValue = getPropertyJsonStringValue(findToscaElement.left().value(), propertyDef.getType()); - propertyDef.setDefaultValue(propertyJsonStringValue); - } - ImportUtils.setField(propertyValue, ToscaTagNamesEnum.IS_PASSWORD, pass -> propertyDef.setPassword(Boolean.parseBoolean(pass))); - ImportUtils.setField(propertyValue, ToscaTagNamesEnum.STATUS, status -> propertyDef.setStatus(status)); - ImportUtils.setPropertyScheme(propertyValue, propertyDef); - ImportUtils.setPropertyConstraints(propertyValue, propertyDef); + setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, propertyDef.getType(), propertyDef::setDefaultValue); + setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.VALUE, propertyDef.getType(), propertyDef::setValue); + + setField(propertyValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> propertyDef.setPassword(Boolean.parseBoolean(pass))); + setField(propertyValue, TypeUtils.ToscaTagNamesEnum.STATUS, propertyDef::setStatus); + setScheme(propertyValue, propertyDef); + setPropertyConstraints(propertyValue, propertyDef); return propertyDef; } - public static InputDefinition createModuleInput(Map<String, Object> inputValue) { + + private static void setJsonStringField(Map<String, Object> propertyValue, ToscaTagNamesEnum elementName, String type, Consumer<String> setter) { + Either<Object, ResultStatusEnum> eitherValue = ImportUtils.findToscaElement(propertyValue, elementName, ToscaElementTypeEnum.ALL); + if (eitherValue.isLeft()) { + String propertyJsonStringValue = getPropertyJsonStringValue(eitherValue.left().value(), type); + setter.accept(propertyJsonStringValue); + } + } + + + + public static Annotation createModuleAnnotation(Map<String, Object> annotationMap, AnnotationTypeOperations annotationTypeOperations) { + String parsedAnnotationType = findFirstToscaStringElement(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE).left().value(); + AnnotationTypeDefinition annotationTypeObject = annotationTypeOperations.getLatestType(parsedAnnotationType); + if (annotationTypeObject != null) { + Annotation annotation = new Annotation(); + ImportUtils.setField(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE, annotation::setType); + ImportUtils.setField(annotationMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, annotation::setDescription); + Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties = getProperties(annotationMap); + modifyPropertiesKeysToProperForm(properties, annotation); + return annotation; + } + return null; + } + + private static Either<Boolean, ResponseFormat> modifyPropertiesKeysToProperForm(Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties, Annotation annotation) { + Either<Boolean, ResponseFormat> result = Either.left(true); + if (properties.isLeft()) { + List<PropertyDataDefinition> propertiesList = new ArrayList<>(); + Map<String, PropertyDefinition> value = properties.left().value(); + if (value != null) { + for (Entry<String, PropertyDefinition> entry : value.entrySet()) { + String name = entry.getKey(); + if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) { + log.debug("The property with invalid name {} occured upon import resource {}. ", name, annotation.getName()); + result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY))); + } + PropertyDefinition propertyDefinition = entry.getValue(); + propertyDefinition.setValue(propertyDefinition.getName()); + propertyDefinition.setName(name); + propertiesList.add(propertyDefinition); + } + } + annotation.setProperties(propertiesList); + } + else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) { + result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromResultStatusEnum(properties + .right() + .value(), JsonPresentationFields.PROPERTY))); + } + return result; + } + + public static InputDefinition createModuleInput(Map<String, Object> inputValue, AnnotationTypeOperations annotationTypeOperations) { InputDefinition inputDef = new InputDefinition(); - ImportUtils.setField(inputValue, ToscaTagNamesEnum.TYPE, type -> inputDef.setType(type)); - ImportUtils.setField(inputValue, ToscaTagNamesEnum.REQUIRED, req -> inputDef.setRequired(Boolean.parseBoolean(req))); - ImportUtils.setField(inputValue, ToscaTagNamesEnum.DESCRIPTION, desc -> inputDef.setDescription(desc)); - - Either<Object, ResultStatusEnum> findToscaElement = ImportUtils.findToscaElement(inputValue, ToscaTagNamesEnum.DEFAULT_VALUE, ToscaElementTypeEnum.ALL); - if (findToscaElement.isLeft()) { - String propertyJsonStringValue = getPropertyJsonStringValue(findToscaElement.left().value(), inputDef.getType()); - inputDef.setDefaultValue(propertyJsonStringValue); - } - ImportUtils.setField(inputValue, ToscaTagNamesEnum.IS_PASSWORD, pass -> inputDef.setPassword(Boolean.parseBoolean(pass))); - ImportUtils.setField(inputValue, ToscaTagNamesEnum.STATUS, status -> inputDef.setStatus(status)); - ImportUtils.setField(inputValue, ToscaTagNamesEnum.LABEL, label -> inputDef.setLabel(label)); - ImportUtils.setField(inputValue, ToscaTagNamesEnum.HIDDEN, hidden -> inputDef.setHidden(Boolean.parseBoolean(hidden))); - ImportUtils.setField(inputValue, ToscaTagNamesEnum.HIDDEN, immutable -> inputDef.setImmutable(Boolean.parseBoolean(immutable))); - ImportUtils.setField(inputValue, ToscaTagNamesEnum.LABEL, label -> inputDef.setLabel(label)); - ImportUtils.setPropertyScheme(inputValue, inputDef); + ImportUtils.setField(inputValue, TypeUtils.ToscaTagNamesEnum.TYPE, inputDef::setType); + ImportUtils.setField(inputValue, TypeUtils.ToscaTagNamesEnum.REQUIRED, req -> inputDef.setRequired(Boolean.parseBoolean(req))); + ImportUtils.setField(inputValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, inputDef::setDescription); + + setJsonStringField(inputValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, inputDef.getType(), inputDef::setDefaultValue); + + ImportUtils.setField(inputValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> inputDef.setPassword(Boolean.parseBoolean(pass))); + ImportUtils.setField(inputValue, TypeUtils.ToscaTagNamesEnum.STATUS, inputDef::setStatus); + ImportUtils.setField(inputValue, TypeUtils.ToscaTagNamesEnum.LABEL, inputDef::setLabel); + ImportUtils.setField(inputValue, TypeUtils.ToscaTagNamesEnum.HIDDEN, hidden -> inputDef.setHidden(Boolean.parseBoolean(hidden))); + ImportUtils.setField(inputValue, TypeUtils.ToscaTagNamesEnum.HIDDEN, immutable -> inputDef.setImmutable(Boolean.parseBoolean(immutable))); + ImportUtils.setField(inputValue, TypeUtils.ToscaTagNamesEnum.LABEL, inputDef::setLabel); + + ImportUtils.setScheme(inputValue, inputDef); ImportUtils.setPropertyConstraints(inputValue, inputDef); + return parseAnnotationsAndAddItToInput(inputDef, inputValue, annotationTypeOperations); + + } + + + public static InputDefinition parseAnnotationsAndAddItToInput(InputDefinition inputDef, Map<String, Object> inputValue, AnnotationTypeOperations annotationTypeOperations){ + Function<String, Annotation> elementGenByName = ImportUtils::createAnnotation; + Function<Map<String, Object>, Annotation> func = annotation -> createModuleAnnotation(annotation, annotationTypeOperations); + return getElements(inputValue, TypeUtils.ToscaTagNamesEnum.ANNOTATIONS, elementGenByName, func). + left().map( annotations -> modifyInputWithAnnotations(inputDef, annotations)). + left().on(err -> { log.error("Parsing annotations or adding them to the PropertyDataDefinition object failed"); + return inputDef;}); + } + + private static InputDefinition modifyInputWithAnnotations(InputDefinition inputDef, Map<String, Annotation> annotationsMap) { + setAnnotationsName(annotationsMap); + inputDef.setAnnotationsToInput(annotationsMap.values()); return inputDef; } + public static PropertyDefinition createModuleAttribute(Map<String, Object> attributeMap) { PropertyDefinition attributeDef = new PropertyDefinition(); - ImportUtils.setField(attributeMap, ToscaTagNamesEnum.TYPE, type -> attributeDef.setType(type)); - ImportUtils.setField(attributeMap, ToscaTagNamesEnum.DESCRIPTION, desc -> attributeDef.setDescription(desc)); - ImportUtils.setField(attributeMap, ToscaTagNamesEnum.STATUS, status -> attributeDef.setStatus(status)); - Either<Object, ResultStatusEnum> eitherDefaultValue = ImportUtils.findToscaElement(attributeMap, ToscaTagNamesEnum.DEFAULT_VALUE, ToscaElementTypeEnum.ALL); - if (eitherDefaultValue.isLeft()) { - String attributeDefaultValue = getPropertyJsonStringValue(eitherDefaultValue.left().value(), attributeDef.getType()); - attributeDef.setDefaultValue(attributeDefaultValue); - } - Either<Object, ResultStatusEnum> eitherValue = ImportUtils.findToscaElement(attributeMap, ToscaTagNamesEnum.VALUE, ToscaElementTypeEnum.ALL); - if (eitherValue.isLeft()) { - String attributeValue = getPropertyJsonStringValue(eitherValue.left().value(), attributeDef.getType()); - attributeDef.setValue(attributeValue); - } - ImportUtils.setAttributeScheme(attributeMap, attributeDef); - return attributeDef; - } + setField(attributeMap, TypeUtils.ToscaTagNamesEnum.TYPE, attributeDef::setType); + setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, attributeDef::setDescription); + setField(attributeMap, TypeUtils.ToscaTagNamesEnum.STATUS, attributeDef::setStatus); - private static void setPropertyScheme(Map<String, Object> propertyValue, PropertyDefinition propertyDefinition) { - Either<SchemaDefinition, ResultStatusEnum> eitherSchema = getSchema(propertyValue); - if (eitherSchema.isLeft()) { - SchemaDefinition schemaDef = new SchemaDefinition(); - schemaDef.setProperty(eitherSchema.left().value().getProperty()); - propertyDefinition.setSchema(schemaDef); - } + setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), attributeDef::setDefaultValue); + setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.VALUE, attributeDef.getType(), attributeDef::setValue); + setScheme(attributeMap, attributeDef); + return attributeDef; } - private static void setAttributeScheme(Map<String, Object> propertyValue, PropertyDefinition propertyDefinition) { + private static void setScheme(Map<String, Object> propertyValue, PropertyDefinition propertyDefinition) { Either<SchemaDefinition, ResultStatusEnum> eitherSchema = getSchema(propertyValue); if (eitherSchema.isLeft()) { SchemaDefinition schemaDef = new SchemaDefinition(); @@ -428,7 +459,7 @@ public final class ImportUtils { private static Either<SchemaDefinition, ResultStatusEnum> getSchema(Map<String, Object> propertyValue) { Either<SchemaDefinition, ResultStatusEnum> result = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either<Object, ResultStatusEnum> propertyFieldEntryScheme = findToscaElement(propertyValue, ToscaTagNamesEnum.ENTRY_SCHEMA, ToscaElementTypeEnum.ALL); + Either<Object, ResultStatusEnum> propertyFieldEntryScheme = findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA, ToscaElementTypeEnum.ALL); if (propertyFieldEntryScheme.isLeft()) { if (propertyFieldEntryScheme.left().value() instanceof String) { String schemaType = (String) propertyFieldEntryScheme.left().value(); @@ -443,14 +474,12 @@ public final class ImportUtils { SchemaDefinition schema = new SchemaDefinition(); schema.setProperty(schemeProperty); result = Either.left(schema); - } - } return result; } - public static void setField(Map<String, Object> toscaJson, ToscaTagNamesEnum tagName, Consumer<String> setter) { + public static void setField(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) { Either<String, ResultStatusEnum> fieldStringValue = findFirstToscaStringElement(toscaJson, tagName); if (fieldStringValue.isLeft()) { setter.accept(fieldStringValue.left().value()); @@ -459,58 +488,57 @@ public final class ImportUtils { } private static void setPropertyFieldRequired(Map<String, Object> propertyValue, PropertyDefinition dataDefinition) { - Either<String, ResultStatusEnum> propertyFieldRequired = findFirstToscaBooleanElement(propertyValue, ToscaTagNamesEnum.REQUIRED); + Either<String, ResultStatusEnum> propertyFieldRequired = findFirstToscaBooleanElement(propertyValue, TypeUtils.ToscaTagNamesEnum.REQUIRED); if (propertyFieldRequired.isLeft()) { dataDefinition.setRequired(Boolean.parseBoolean(propertyFieldRequired.left().value())); } } public static Either<Map<String, PropertyDefinition>, ResultStatusEnum> getProperties(Map<String, Object> toscaJson) { - Function<String, PropertyDefinition> elementGenByName = elementName -> createProperties(elementName); - Function<Map<String, Object>, PropertyDefinition> func = map -> createModuleProperty(map); + Function<String, PropertyDefinition> elementGenByName = ImportUtils::createProperties; + Function<Map<String, Object>, PropertyDefinition> func = ImportUtils::createModuleProperty; - return getElements(toscaJson, ToscaTagNamesEnum.PROPERTIES, elementGenByName, func); + return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.PROPERTIES, elementGenByName, func); } - public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(Map<String, Object> toscaJson) { - Function<String, InputDefinition> elementGenByName = elementName -> createInputs(elementName); - Function<Map<String, Object>, InputDefinition> func = map -> createModuleInput(map); + public static Either<Map<String, InputDefinition>, ResultStatusEnum> getInputs(Map<String, Object> toscaJson, AnnotationTypeOperations annotationTypeOperations) { + Function<String, InputDefinition> elementGenByName = ImportUtils::createInputs; + Function<Map<String, Object>, InputDefinition> func = object -> createModuleInput(object, annotationTypeOperations); - return getElements(toscaJson, ToscaTagNamesEnum.INPUTS, elementGenByName, func); + return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.INPUTS, elementGenByName, func); } public static Either<Map<String, PropertyDefinition>, ResultStatusEnum> getAttributes(Map<String, Object> toscaJson) { - Function<String, PropertyDefinition> elementGenByName = elementName -> createAttribute(elementName); - Function<Map<String, Object>, PropertyDefinition> func = map -> createModuleAttribute(map); + Function<String, PropertyDefinition> elementGenByName = ImportUtils::createAttribute; + Function<Map<String, Object>, PropertyDefinition> func = ImportUtils::createModuleAttribute; - return getElements(toscaJson, ToscaTagNamesEnum.ATTRIBUTES, elementGenByName, func); + return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.ATTRIBUTES, elementGenByName, func); } - public static <ElementDefinition> Either<Map<String, ElementDefinition>, ResultStatusEnum> getElements(Map<String, Object> toscaJson, ToscaTagNamesEnum elementTagName, Function<String, ElementDefinition> elementGenByName, - Function<Map<String, Object>, ElementDefinition> func) { - Either<Map<String, ElementDefinition>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); + public static <T> Either<Map<String, T>, ResultStatusEnum> getElements(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementTagName, Function<String, T> elementGenByName, + Function<Map<String, Object>, T> func) { + Either<Map<String, T>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(toscaJson, elementTagName); if (toscaAttributes.isLeft()) { Map<String, Object> jsonAttributes = toscaAttributes.left().value(); - Map<String, ElementDefinition> moduleAttributes = new HashMap<>(); + Map<String, T> moduleAttributes = new HashMap<>(); Iterator<Entry<String, Object>> propertiesNameValue = jsonAttributes.entrySet().iterator(); while (propertiesNameValue.hasNext()) { Entry<String, Object> attributeNameValue = propertiesNameValue.next(); if (attributeNameValue.getValue() instanceof Map) { @SuppressWarnings("unchecked") - ElementDefinition attribute = func.apply((Map<String, Object>) attributeNameValue.getValue()); - moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), attribute); - } else { - - ElementDefinition element = elementGenByName.apply(String.valueOf(attributeNameValue.getValue())); - + T attribute = func.apply((Map<String, Object>) attributeNameValue.getValue()); + if (attribute != null){ + moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), attribute); + } + } + else { + T element = elementGenByName.apply(String.valueOf(attributeNameValue.getValue())); moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), element); } - } - if (moduleAttributes.size() > 0) { eitherResult = Either.left(moduleAttributes); } @@ -534,6 +562,8 @@ public final class ImportUtils { return property; } + + private static InputDefinition createInputs(String name) { InputDefinition input = new InputDefinition(); @@ -541,10 +571,18 @@ public final class ImportUtils { return input; } + private static Annotation createAnnotation(String name) { + Annotation annotation = new Annotation(); + annotation.setName(name); + return annotation; + } + + + public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParameters(Map<String, Object> heatData, String artifactType) { Either<List<HeatParameterDefinition>, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either<Map<String, Object>, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(heatData, ToscaTagNamesEnum.PARAMETERS); + Either<Map<String, Object>, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(heatData, TypeUtils.ToscaTagNamesEnum.PARAMETERS); if (toscaProperties.isLeft()) { Map<String, Object> jsonProperties = toscaProperties.left().value(); List<HeatParameterDefinition> moduleProperties = new ArrayList<>(); @@ -570,7 +608,7 @@ public final class ImportUtils { } - if (moduleProperties.size() > 0) { + if (!isEmpty(moduleProperties)) { eitherResult = Either.left(moduleProperties); } @@ -583,7 +621,7 @@ public final class ImportUtils { HeatParameterDefinition property = new HeatParameterDefinition(); Object value = propertyNameValue.getValue(); if (value != null) { - property.setDefaultValue(isJson ? new Gson().toJson(value).toString() : StringEscapeUtils.escapeJava(String.valueOf(value))); + property.setDefaultValue(isJson ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value))); } property.setName(String.valueOf(propertyNameValue.getKey())); moduleProperties.add(property); @@ -592,31 +630,35 @@ public final class ImportUtils { private static Either<HeatParameterDefinition, ResultStatusEnum> createModuleHeatParameter(Map<String, Object> propertyValue) { HeatParameterDefinition propertyDef = new HeatParameterDefinition(); String type; - Either<String, ResultStatusEnum> propertyFieldType = findFirstToscaStringElement(propertyValue, ToscaTagNamesEnum.TYPE); + Either<String, ResultStatusEnum> propertyFieldType = findFirstToscaStringElement(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE); if (propertyFieldType.isLeft()) { type = propertyFieldType.left().value(); propertyDef.setType(type); } else { return Either.right(ResultStatusEnum.INVALID_PROPERTY_TYPE); } - Either<String, ResultStatusEnum> propertyFieldDescription = findFirstToscaStringElement(propertyValue, ToscaTagNamesEnum.DESCRIPTION); + Either<String, ResultStatusEnum> propertyFieldDescription = findFirstToscaStringElement(propertyValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION); if (propertyFieldDescription.isLeft()) { propertyDef.setDescription(propertyFieldDescription.left().value()); } - Either<Object, ResultStatusEnum> propertyFieldDefaultVal = findToscaElement(propertyValue, ToscaTagNamesEnum.DEFAULT_VALUE, ToscaElementTypeEnum.ALL); + Either<Object, ResultStatusEnum> propertyFieldDefaultVal = findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, ToscaElementTypeEnum.ALL); if (propertyFieldDefaultVal.isLeft()) { if (propertyFieldDefaultVal.left().value() == null) { return Either.right(ResultStatusEnum.INVALID_PROPERTY_VALUE); } Object value = propertyFieldDefaultVal.left().value(); - String defaultValue = type.equals(HeatParameterType.JSON.getType()) ? new Gson().toJson(value).toString() : StringEscapeUtils.escapeJava(String.valueOf(value)); + String defaultValue = type.equals(HeatParameterType.JSON.getType()) ? new Gson().toJson(value) : StringEscapeUtils.escapeJava(String.valueOf(value)); propertyDef.setDefaultValue(defaultValue); propertyDef.setCurrentValue(defaultValue); } return Either.left(propertyDef); } + public static boolean containsGetInput(Object propValue) { + String value = getPropertyJsonStringValue(propValue, ToscaPropertyType.MAP.getType()); + return value != null && value.contains(TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName()); + } public static String getPropertyJsonStringValue(Object value, String type) { Gson gson = new Gson(); |