From 26cba80ddf5e97e1df856ed013ea69c45c5c0976 Mon Sep 17 00:00:00 2001 From: vasraz Date: Thu, 30 Jul 2020 13:06:42 +0100 Subject: Add 'data_types' during import VFCs Change-Id: Ib9974bbc1eb88633e7e6834cc7ef8ae95deda539 Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3191 --- .../sdc/be/components/impl/ImportUtils.java | 240 +++++++++++++-------- .../be/components/impl/ResourceBusinessLogic.java | 13 +- .../be/components/impl/ResourceImportManager.java | 73 ++++++- 3 files changed, 225 insertions(+), 101 deletions(-) (limited to 'catalog-be') 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 01698fa662..4db44d3a76 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 @@ -7,9 +7,9 @@ * 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. @@ -26,11 +26,13 @@ import com.google.gson.JsonParseException; import com.google.gson.reflect.TypeToken; import fj.data.Either; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.tinkerpop.gremlin.structure.T; 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.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.datatypes.elements.Annotation; import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; @@ -40,6 +42,7 @@ 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; +import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.HeatParameterDefinition; import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.LifecycleStateEnum; @@ -83,12 +86,14 @@ 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 static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataType; +import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataTypeDefinitionWithName; @Component public final class ImportUtils { private static final CustomResolver customResolver = new CustomResolver(); - private static final Yaml strictYamlLoader = new YamlLoader().getStrictYamlLoader(); + private static final Yaml strictYamlLoader = new YamlLoader().getStrictYamlLoader(); private static ComponentsUtils componentsUtils; @@ -103,6 +108,7 @@ public final class ImportUtils { } private static class CustomResolver extends Resolver { + @Override protected void addImplicitResolvers() { // avoid implicit resolvers for strings that can be interpreted as boolean values @@ -124,41 +130,43 @@ public final class ImportUtils { Map result = new LinkedHashMap<>(); buildMap(result, (Map) value); output.put(key, result); - } - else if (value instanceof Collection) { + } else if (value instanceof Collection) { Map result = new LinkedHashMap<>(); int i = 0; - for(Object item : (Collection) value) { + for (Object item : (Collection) value) { buildMap(result, Collections.singletonMap("[" + (i++) + "]", item)); } output.put(key, new ArrayList<>(result.values())); - } - else { + } else { output.put(key, value); } } } - public static Map loadYamlAsStrictMap(String content){ + public static Map loadYamlAsStrictMap(String content) { Map result = new LinkedHashMap<>(); Object map = strictYamlLoader.load(content); - buildMap(result, (Map)map); + buildMap(result, (Map) map); return result; } private static class YamlLoader extends YamlProcessor { + public Yaml getStrictYamlLoader() { return createYaml(); } } @SuppressWarnings("unchecked") - public static Either, ResultStatusEnum> getHeatParamsWithoutImplicitTypes(String heatDecodedPayload, String artifactType) { - Map heatData = (Map) new Yaml(new Constructor(), new Representer(), new DumperOptions(), customResolver).load(heatDecodedPayload); + public static Either, ResultStatusEnum> getHeatParamsWithoutImplicitTypes( + String heatDecodedPayload, String artifactType) { + Map heatData = (Map) new Yaml(new Constructor(), new Representer(), + new DumperOptions(), customResolver).load(heatDecodedPayload); return getHeatParameters(heatData, artifactType); } public static class Constants { + public static final String FIRST_NON_CERTIFIED_VERSION = "0.1"; public static final String VENDOR_NAME = "ONAP (Tosca)"; public static final String VENDOR_RELEASE = "1.0.0.wd03"; @@ -175,7 +183,8 @@ public final class ImportUtils { public static final String ESCAPED_DOUBLE_QUOTE = "\""; public static final String QUOTE = "'"; - private Constants() {} + private Constants() { + } } public enum ResultStatusEnum { @@ -187,7 +196,8 @@ public final class ImportUtils { } @SuppressWarnings("unchecked") - private static void handleElementNameNotFound(String elementName, Object elementValue, ToscaElementTypeEnum elementType, List returnedList) { + private static void handleElementNameNotFound(String elementName, Object elementValue, + ToscaElementTypeEnum elementType, List returnedList) { if (elementValue instanceof Map) { findToscaElements((Map) elementValue, elementName, elementType, returnedList); } else if (elementValue instanceof List) { @@ -196,15 +206,14 @@ public final class ImportUtils { } @SuppressWarnings("unchecked") - private static void addFoundElementAccordingToItsType(String elementName, ToscaElementTypeEnum elementType, List returnedList, Object elementValue) { + private static void addFoundElementAccordingToItsType(String elementName, ToscaElementTypeEnum elementType, + List returnedList, Object elementValue) { if (elementValue instanceof Boolean) { if (elementType == ToscaElementTypeEnum.BOOLEAN || elementType == ToscaElementTypeEnum.ALL) { returnedList.add(elementValue); } - } - - else if (elementValue instanceof String) { + } else if (elementValue instanceof String) { if (elementType == ToscaElementTypeEnum.STRING || elementType == ToscaElementTypeEnum.ALL) { returnedList.add(elementValue); } @@ -227,11 +236,14 @@ public final class ImportUtils { } } - private static void findAllToscaElementsInList(List list, String elementName, ToscaElementTypeEnum elementType, List returnedList) { + private static void findAllToscaElementsInList(List list, String elementName, + ToscaElementTypeEnum elementType, List returnedList) { list.forEach(elementValue -> handleElementNameNotFound(elementName, elementValue, elementType, returnedList)); } - public static Either findToscaElement(Map toscaJson, TypeUtils.ToscaTagNamesEnum elementName, ToscaElementTypeEnum elementType) { + public static Either findToscaElement(Map toscaJson, + TypeUtils.ToscaTagNamesEnum elementName, + ToscaElementTypeEnum elementType) { List foundElements = new ArrayList<>(); findToscaElements(toscaJson, elementName.getElementName(), elementType, foundElements); if (!isEmpty(foundElements)) { @@ -242,21 +254,21 @@ public final class ImportUtils { /** * Recursively searches for all tosca elements with key equals to elementName and value equals to elementType.
- * Returns Either element with:
- * List with all value if values found
- * Or ELEMENT_NOT_FOUND ActionStatus + * Returns Either element with:
List with all value if values found
Or ELEMENT_NOT_FOUND ActionStatus * * @param toscaJson * @return */ - public static Either, ResultStatusEnum> findToscaElements(Map toscaJson, String elementName, ToscaElementTypeEnum elementType, List returnedList) { + public static Either, ResultStatusEnum> findToscaElements(Map toscaJson, + String elementName, + ToscaElementTypeEnum elementType, + List returnedList) { Either, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); String skipKey = null; if (toscaJson.containsKey(elementName)) { skipKey = handleFoundElement(toscaJson, elementName, elementType, returnedList); } - Iterator> keyValItr = toscaJson.entrySet().iterator(); while (keyValItr.hasNext()) { Entry keyValEntry = keyValItr.next(); @@ -272,7 +284,8 @@ public final class ImportUtils { return returnedElement; } - private static String handleFoundElement(Map toscaJson, String elementName, ToscaElementTypeEnum elementType, List returnedList) { + private static String handleFoundElement(Map toscaJson, String elementName, + ToscaElementTypeEnum elementType, List returnedList) { Object elementValue = toscaJson.get(elementName); addFoundElementAccordingToItsType(elementName, elementType, returnedList, elementValue); return elementName; @@ -280,9 +293,11 @@ public final class ImportUtils { } @SuppressWarnings("unchecked") - public static Either, ResultStatusEnum> findFirstToscaListElement(Map toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { + public static Either, ResultStatusEnum> findFirstToscaListElement(Map toscaJson, + TypeUtils.ToscaTagNamesEnum toscaTagName) { Either, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.LIST); + Either findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, + ToscaElementTypeEnum.LIST); if (findFirstToscaElement.isLeft()) { returnedElement = Either.left((List) findFirstToscaElement.left().value()); } @@ -291,9 +306,11 @@ public final class ImportUtils { } @SuppressWarnings("unchecked") - public static Either, ResultStatusEnum> findFirstToscaMapElement(Map toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { + public static Either, ResultStatusEnum> findFirstToscaMapElement(Map toscaJson, + TypeUtils.ToscaTagNamesEnum toscaTagName) { Either, ResultStatusEnum> returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.MAP); + Either findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, + ToscaElementTypeEnum.MAP); if (findFirstToscaElement.isLeft()) { returnedElement = Either.left((Map) findFirstToscaElement.left().value()); } @@ -301,9 +318,11 @@ public final class ImportUtils { } - public static Either findFirstToscaStringElement(Map toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { + public static Either findFirstToscaStringElement(Map toscaJson, + TypeUtils.ToscaTagNamesEnum toscaTagName) { Either returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.STRING); + Either findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, + ToscaElementTypeEnum.STRING); if (findFirstToscaElements.isLeft()) { returnedElement = Either.left((String) findFirstToscaElements.left().value()); } @@ -311,15 +330,18 @@ public final class ImportUtils { } /** - * searches for first Tosca in Json map (toscaJson) boolean element by name (toscaTagName) returns found element or ELEMENT_NOT_FOUND status + * searches for first Tosca in Json map (toscaJson) boolean element by name (toscaTagName) returns found element or + * ELEMENT_NOT_FOUND status * * @param toscaJson * @param toscaTagName * @return */ - public static Either findFirstToscaBooleanElement(Map toscaJson, TypeUtils.ToscaTagNamesEnum toscaTagName) { + public static Either findFirstToscaBooleanElement(Map toscaJson, + TypeUtils.ToscaTagNamesEnum toscaTagName) { Either returnedElement = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, ToscaElementTypeEnum.BOOLEAN); + Either findFirstToscaElements = findToscaElement(toscaJson, toscaTagName, + ToscaElementTypeEnum.BOOLEAN); if (findFirstToscaElements.isLeft()) { returnedElement = Either.left(String.valueOf(findFirstToscaElements.left().value())); } @@ -333,16 +355,19 @@ public final class ImportUtils { } } - private static List getPropertyConstraints(Map propertyValue, String propertyType) { + private static List getPropertyConstraints(Map propertyValue, + String propertyType) { List propertyFieldConstraints = findCurrentLevelConstraintsElement(propertyValue); if (CollectionUtils.isNotEmpty(propertyFieldConstraints)) { List constraintList = new ArrayList<>(); Type constraintType = new TypeToken() { }.getType(); - Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create(); + Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()) + .create(); for (Object constraintJson : propertyFieldConstraints) { - PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, gson, constraintJson); + PropertyConstraint propertyConstraint = validateAndGetPropertyConstraint(propertyType, constraintType, + gson, constraintJson); constraintList.add(propertyConstraint); } return constraintList; @@ -355,29 +380,33 @@ public final class ImportUtils { if (toscaJson.containsKey(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName())) { try { constraints = (List) toscaJson.get(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName()); - } catch (ClassCastException e){ - throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT, toscaJson.get(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName()).toString()); + } catch (ClassCastException e) { + throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT, + toscaJson.get(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName()).toString()); } } return constraints; } - private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, Gson gson, Object constraintJson) { + private static PropertyConstraint validateAndGetPropertyConstraint(String propertyType, Type constraintType, + Gson gson, Object constraintJson) { PropertyConstraint propertyConstraint; - try{ + try { propertyConstraint = gson.fromJson(gson.toJson(constraintJson), constraintType); - } catch (ClassCastException|JsonParseException e){ - throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT, constraintJson.toString()); + } catch (ClassCastException | JsonParseException e) { + throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS_FORMAT, + constraintJson.toString()); } - if(propertyConstraint!= null && propertyConstraint instanceof ValidValuesConstraint){ + if (propertyConstraint != null && propertyConstraint instanceof ValidValuesConstraint) { try { - ((ValidValuesConstraint)propertyConstraint).validateType(propertyType); + ((ValidValuesConstraint) propertyConstraint).validateType(propertyType); } catch (ConstraintValueDoNotMatchPropertyTypeException e) { BeEcompErrorManager.getInstance().logInternalFlowError("GetInitializedPropertyConstraint", - e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR); - throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, ConstraintType.VALID_VALUES.name(), - ((ValidValuesConstraint) propertyConstraint).getValidValues().toString(), propertyType); + e.getMessage(), BeEcompErrorManager.ErrorSeverity.ERROR); + throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_CONSTRAINTS, + ConstraintType.VALID_VALUES.name(), + ((ValidValuesConstraint) propertyConstraint).getValidValues().toString(), propertyType); } } return propertyConstraint; @@ -387,13 +416,17 @@ public final class ImportUtils { PropertyDefinition propertyDef = new PropertyDefinition(); setField(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE, propertyDef::setType); - setFieldBoolean(propertyValue, ToscaTagNamesEnum.REQUIRED, req -> propertyDef.setRequired(Boolean.parseBoolean(req))); + setFieldBoolean(propertyValue, ToscaTagNamesEnum.REQUIRED, + req -> propertyDef.setRequired(Boolean.parseBoolean(req))); setField(propertyValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, propertyDef::setDescription); - setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, propertyDef.getType(), propertyDef::setDefaultValue); - setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.VALUE, propertyDef.getType(), propertyDef::setValue); + setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, propertyDef.getType(), + propertyDef::setDefaultValue); + setJsonStringField(propertyValue, TypeUtils.ToscaTagNamesEnum.VALUE, propertyDef.getType(), + propertyDef::setValue); - setFieldBoolean(propertyValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> propertyDef.setPassword(Boolean.parseBoolean(pass))); + setFieldBoolean(propertyValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, + pass -> propertyDef.setPassword(Boolean.parseBoolean(pass))); setField(propertyValue, TypeUtils.ToscaTagNamesEnum.STATUS, propertyDef::setStatus); setScheme(propertyValue, propertyDef); setPropertyConstraints(propertyValue, propertyDef); @@ -401,16 +434,20 @@ public final class ImportUtils { return propertyDef; } - private static void setJsonStringField(Map propertyValue, ToscaTagNamesEnum elementName, String type, Consumer setter) { - Either eitherValue = findToscaElement(propertyValue, elementName, ToscaElementTypeEnum.ALL); + private static void setJsonStringField(Map propertyValue, ToscaTagNamesEnum elementName, + String type, Consumer setter) { + Either eitherValue = findToscaElement(propertyValue, elementName, + ToscaElementTypeEnum.ALL); if (eitherValue.isLeft()) { String propertyJsonStringValue = getPropertyJsonStringValue(eitherValue.left().value(), type); setter.accept(propertyJsonStringValue); } } - public static Annotation createModuleAnnotation(Map annotationMap, AnnotationTypeOperations annotationTypeOperations) { - String parsedAnnotationType = findFirstToscaStringElement(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE).left().value(); + public static Annotation createModuleAnnotation(Map annotationMap, + AnnotationTypeOperations annotationTypeOperations) { + String parsedAnnotationType = findFirstToscaStringElement(annotationMap, TypeUtils.ToscaTagNamesEnum.TYPE) + .left().value(); AnnotationTypeDefinition annotationTypeObject = annotationTypeOperations.getLatestType(parsedAnnotationType); if (annotationTypeObject != null) { Annotation annotation = new Annotation(); @@ -423,7 +460,8 @@ public final class ImportUtils { return null; } - private static Either modifyPropertiesKeysToProperForm(Either, ResultStatusEnum> properties, Annotation annotation) { + private static Either modifyPropertiesKeysToProperForm( + Either, ResultStatusEnum> properties, Annotation annotation) { Either result = Either.left(true); if (properties.isLeft()) { List propertiesList = new ArrayList<>(); @@ -432,8 +470,11 @@ public final class ImportUtils { for (Entry entry : value.entrySet()) { String name = entry.getKey(); if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) { - log.debug("The property with invalid name {} occurred upon import resource {}. ", name, annotation.getName()); - result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY))); + log.debug("The property with invalid name {} occurred 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()); @@ -442,23 +483,25 @@ public final class ImportUtils { } } annotation.setProperties(propertiesList); - } - else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) { - result = Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromResultStatusEnum(properties + } 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 inputValue, AnnotationTypeOperations annotationTypeOperations) { + public static InputDefinition createModuleInput(Map inputValue, + AnnotationTypeOperations annotationTypeOperations) { InputDefinition inputDef = new InputDefinition(); setField(inputValue, TypeUtils.ToscaTagNamesEnum.TYPE, inputDef::setType); setFieldBoolean(inputValue, ToscaTagNamesEnum.REQUIRED, req -> inputDef.setRequired(Boolean.parseBoolean(req))); setField(inputValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, inputDef::setDescription); - setJsonStringField(inputValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, inputDef.getType(), inputDef::setDefaultValue); + setJsonStringField(inputValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, inputDef.getType(), + inputDef::setDefaultValue); setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> inputDef.setPassword(Boolean.parseBoolean(pass))); setField(inputValue, TypeUtils.ToscaTagNamesEnum.STATUS, inputDef::setStatus); @@ -473,16 +516,22 @@ public final class ImportUtils { } - public static InputDefinition parseAnnotationsAndAddItToInput(InputDefinition inputDef, Map inputValue, AnnotationTypeOperations annotationTypeOperations){ + public static InputDefinition parseAnnotationsAndAddItToInput(InputDefinition inputDef, + Map inputValue, + AnnotationTypeOperations annotationTypeOperations) { Function elementGenByName = ImportUtils::createAnnotation; - Function, Annotation> func = annotation -> createModuleAnnotation(annotation, annotationTypeOperations); + Function, 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;}); + 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 annotationsMap) { + private static InputDefinition modifyInputWithAnnotations(InputDefinition inputDef, + Map annotationsMap) { setAnnotationsName(annotationsMap); inputDef.setAnnotationsToInput(annotationsMap.values()); return inputDef; @@ -495,7 +544,8 @@ public final class ImportUtils { setField(attributeMap, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, attributeDef::setDescription); setField(attributeMap, TypeUtils.ToscaTagNamesEnum.STATUS, attributeDef::setStatus); - setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), attributeDef::set_default); + setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), + attributeDef::set_default); setScheme(attributeMap, attributeDef); return attributeDef; @@ -528,7 +578,8 @@ public final class ImportUtils { return schema; } - public static void setField(Map toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer setter) { + private static void setField(Map toscaJson, TypeUtils.ToscaTagNamesEnum tagName, + Consumer setter) { Either fieldStringValue = findFirstToscaStringElement(toscaJson, tagName); if (fieldStringValue.isLeft()) { setter.accept(fieldStringValue.left().value()); @@ -536,7 +587,8 @@ public final class ImportUtils { } - public static void setFieldBoolean(Map toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer setter) { + public static void setFieldBoolean(Map toscaJson, TypeUtils.ToscaTagNamesEnum tagName, + Consumer setter) { Either fieldStringValue = findFirstToscaBooleanElement(toscaJson, tagName); if (fieldStringValue.isLeft()) { setter.accept(fieldStringValue.left().value()); @@ -590,8 +642,7 @@ public final class ImportUtils { if (attribute != null) { moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), attribute); } - } - else { + } else { T element = elementGenByName.apply(String.valueOf(attributeNameValue.getValue())); moduleAttributes.put(String.valueOf(attributeNameValue.getKey()), element); } @@ -634,8 +685,10 @@ public final class ImportUtils { public static Either, ResultStatusEnum> getHeatParameters(Map heatData, String artifactType) { - Either, ResultStatusEnum> eitherResult = Either.right(ResultStatusEnum.ELEMENT_NOT_FOUND); - Either, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(heatData, TypeUtils.ToscaTagNamesEnum.PARAMETERS); + Either, ResultStatusEnum> eitherResult = Either + .right(ResultStatusEnum.ELEMENT_NOT_FOUND); + Either, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(heatData, + TypeUtils.ToscaTagNamesEnum.PARAMETERS); if (toscaProperties.isLeft()) { Map jsonProperties = toscaProperties.left().value(); List moduleProperties = new ArrayList<>(); @@ -645,7 +698,8 @@ public final class ImportUtils { if (propertyNameValue.getValue() instanceof Map || propertyNameValue.getValue() instanceof List) { if (!artifactType.equals(ArtifactTypeEnum.HEAT_ENV.getType())) { @SuppressWarnings("unchecked") - Either propertyStatus = createModuleHeatParameter((Map) propertyNameValue.getValue()); + Either propertyStatus = createModuleHeatParameter( + (Map) propertyNameValue.getValue()); if (propertyStatus.isRight()) { return Either.right(propertyStatus.right().value()); } @@ -670,44 +724,52 @@ public final class ImportUtils { } - private static void addHeatParamDefinition(List moduleProperties, Entry propertyNameValue, boolean isJson) { + private static void addHeatParamDefinition(List moduleProperties, + Entry propertyNameValue, boolean isJson) { HeatParameterDefinition property = new HeatParameterDefinition(); Object value = propertyNameValue.getValue(); if (value != null) { - property.setDefaultValue(isJson ? new Gson().toJson(value) : 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); } - private static Either createModuleHeatParameter(Map propertyValue) { + private static Either createModuleHeatParameter( + Map propertyValue) { HeatParameterDefinition propertyDef = new HeatParameterDefinition(); String type; - Either propertyFieldType = findFirstToscaStringElement(propertyValue, TypeUtils.ToscaTagNamesEnum.TYPE); + Either 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 propertyFieldDescription = findFirstToscaStringElement(propertyValue, TypeUtils.ToscaTagNamesEnum.DESCRIPTION); + Either propertyFieldDescription = findFirstToscaStringElement(propertyValue, + TypeUtils.ToscaTagNamesEnum.DESCRIPTION); if (propertyFieldDescription.isLeft()) { propertyDef.setDescription(propertyFieldDescription.left().value()); } - Either propertyFieldDefaultVal = findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, ToscaElementTypeEnum.ALL); + Either 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) : 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()); @@ -719,14 +781,16 @@ public final class ImportUtils { return null; } ToscaPropertyType validType = ToscaPropertyType.isValidType(type); - if (validType == null || validType == ToscaPropertyType.JSON || validType == ToscaPropertyType.MAP || validType == ToscaPropertyType.LIST) { + if (validType == null || validType == ToscaPropertyType.JSON || validType == ToscaPropertyType.MAP + || validType == ToscaPropertyType.LIST) { return gson.toJson(value); } return value.toString(); } /** - * removes from Json map (toscaJson) first element found by name (elementName) note that this method could update the received argument toscaJson + * removes from Json map (toscaJson) first element found by name (elementName) note that this method could update + * the received argument toscaJson * * @param toscaJson * @param elementName diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java index 35a0761ff1..8164bad477 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java @@ -4015,22 +4015,25 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } newResource.setAbstract(oldResource.isAbstract()); - if (CollectionUtils.isEmpty(newResource.getDerivedFrom())) { + if (CollectionUtils.isEmpty(newResource.getDerivedFrom())){ newResource.setDerivedFrom(oldResource.getDerivedFrom()); } - if (StringUtils.isEmpty(newResource.getDerivedFromGenericType())) { + if (CollectionUtils.isEmpty(newResource.getDataTypes())){ + newResource.setDataTypes(oldResource.getDataTypes()); + } + if (StringUtils.isEmpty(newResource.getDerivedFromGenericType())){ newResource.setDerivedFromGenericType(oldResource.getDerivedFromGenericType()); } - if (StringUtils.isEmpty(newResource.getDerivedFromGenericVersion())) { + if (StringUtils.isEmpty(newResource.getDerivedFromGenericVersion())){ newResource.setDerivedFromGenericVersion(oldResource.getDerivedFromGenericVersion()); } // add for new) // created without tosca artifacts - add the placeholders - if (MapUtils.isEmpty(newResource.getToscaArtifacts())) { + if (MapUtils.isEmpty(newResource.getToscaArtifacts())){ setToscaArtifactsPlaceHolders(newResource, user); } - if (MapUtils.isEmpty(newResource.getInterfaces())) { + if (MapUtils.isEmpty(newResource.getInterfaces())){ newResource.setInterfaces(oldResource.getInterfaces()); } if (CollectionUtils.isEmpty(newResource.getAttributes())) { 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 1a0dfaa9fd..dbcaee4d30 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 @@ -23,11 +23,16 @@ package org.openecomp.sdc.be.components.impl; import static org.openecomp.sdc.be.components.impl.ImportUtils.Constants.QUOTE; +import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataType; +import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataTypeDefinitionWithName; import static org.openecomp.sdc.be.utils.TypeUtils.setField; import fj.data.Either; +import java.util.Collections; import java.util.LinkedHashMap; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.auditing.api.AuditEventFactory; @@ -37,12 +42,14 @@ import org.openecomp.sdc.be.components.csar.CsarInfo; import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.ArtifactOperationEnum; import org.openecomp.sdc.be.components.impl.ImportUtils.Constants; import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum; +import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum; import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity; import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; @@ -59,6 +66,7 @@ import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.ComponentInstanceAttribute; import org.openecomp.sdc.be.model.ComponentInstanceProperty; +import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.LifecycleStateEnum; import org.openecomp.sdc.be.model.PropertyDefinition; @@ -241,7 +249,7 @@ public class ResourceImportManager { componentsUtils .getResponseFormat(((ComponentException) e).getActionStatus(), ((ComponentException) e).getParams()) : - ((ComponentException) e).getResponseFormat(); + ((ComponentException) e).getResponseFormat(); } return null; } @@ -327,6 +335,15 @@ public class ResourceImportManager { toscaJson.put(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName(), toscaJsonAll.get(TypeUtils.ToscaTagNamesEnum.NODE_TYPES.getElementName())); } + final List foundElements = new ArrayList<>(); + final Either, ResultStatusEnum> toscaElements = ImportUtils.findToscaElements(toscaJsonAll, + ToscaTagNamesEnum.DATA_TYPES.getElementName(), ToscaElementTypeEnum.MAP, foundElements); + if (toscaElements.isLeft()) { + final Map toscaAttributes = (Map) foundElements.get(0); + if (MapUtils.isNotEmpty(toscaAttributes)) { + resource.setDataTypes(extractDataTypeFromJson(resourceBusinessLogic, toscaAttributes)); + } + } // Derived From Resource parentResource = setDerivedFrom(toscaJson, resource); if (StringUtils.isEmpty(resource.getToscaResourceName())) { @@ -482,7 +499,7 @@ public class ResourceImportManager { } private boolean entryIsInterfaceType(final Entry entry) { - if (entry.getKey().equals(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) { + if(entry.getKey().equals(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName())) { if (entry.getValue() instanceof String) { return true; } @@ -493,8 +510,8 @@ public class ResourceImportManager { private boolean entryContainsImplementationForAKnownOperation(final Entry entry, final String interfaceType) { - if (entry.getValue() instanceof Map && ((Map) entry.getValue()).containsKey(IMPLEMENTATION)) { - if (isAKnownOperation(interfaceType, entry.getKey())) { + if (entry.getValue() instanceof Map && ((Map)entry.getValue()).containsKey(IMPLEMENTATION)) { + if (isAKnownOperation(interfaceType, entry.getKey())){ return true; } throw new ByActionStatusComponentException(ActionStatus.INTERFACE_OPERATION_NOT_FOUND); @@ -832,8 +849,8 @@ public class ResourceImportManager { log.debug("Couldn't check whether imported resource capability derives from its parent's capability"); throw new ByActionStatusComponentException( componentsUtils.convertFromStorageResponse(capabilityTypeDerivedFrom - .right() - .value())); + .right() + .value())); } return capabilityTypeDerivedFrom.left().value(); } @@ -858,7 +875,7 @@ public class ResourceImportManager { if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES.getElementName())) { capabilityDefinition.setValidSourceTypes( (List) capabilityJsonMap.get(TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES - .getElementName())); + .getElementName())); } // ValidSourceTypes if (capabilityJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName())) { @@ -1023,7 +1040,22 @@ public class ResourceImportManager { final Map mappedToscaTemplate = decodePayload(payloadData); final Either findFirstToscaStringElement = ImportUtils.findFirstToscaStringElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION); - return findFirstToscaStringElement.left().value(); + if (findFirstToscaStringElement.isLeft()) { + return findFirstToscaStringElement.left().value(); + } else { + return null; + } + } + + private Map getDataTypes(final String payloadData) { + final Map mappedToscaTemplate = decodePayload(payloadData); + final Either, ResultStatusEnum> findFirstToscaStringElement = + ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, ToscaTagNamesEnum.DATA_TYPES); + if (findFirstToscaStringElement.isLeft()) { + return findFirstToscaStringElement.left().value(); + } else { + return Collections.EMPTY_MAP; + } } private void calculateResourceIsAbstract(Resource resource, List categories) { @@ -1172,4 +1204,29 @@ public class ResourceImportManager { this.auditingManager = auditingManager; } + private List extractDataTypeFromJson(final ResourceBusinessLogic resourceBusinessLogic, + final Map foundElements) { + final List dataTypeDefinitionList = new ArrayList<>(); + if (MapUtils.isNotEmpty(foundElements)) { + final Either, JanusGraphOperationStatus> dataTypeCacheAll = + resourceBusinessLogic.dataTypeCache.getAll(); + if (dataTypeCacheAll.isLeft()) { + for (final Entry attributeNameValue : foundElements.entrySet()) { + final Object value = attributeNameValue.getValue(); + if (value instanceof Map) { + final DataTypeDefinition dataTypeDefinition = + createDataTypeDefinitionWithName(attributeNameValue); + final DataTypeDefinition dataTypeDefinitionParent = + dataTypeCacheAll.left().value().get(dataTypeDefinition.getDerivedFromName()); + dataTypeDefinition.setDerivedFrom(dataTypeDefinitionParent); + + dataTypeDefinitionList.add(dataTypeDefinition); + } else { + dataTypeDefinitionList.add(createDataType(String.valueOf(value))); + } + } + } + } + return dataTypeDefinitionList; + } } -- cgit 1.2.3-korg