diff options
Diffstat (limited to 'catalog-be/src/main/java')
3 files changed, 225 insertions, 101 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 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<String, Object> result = new LinkedHashMap<>(); buildMap(result, (Map) value); output.put(key, result); - } - else if (value instanceof Collection) { + } else if (value instanceof Collection) { Map<String, Object> result = new LinkedHashMap<>(); int i = 0; - for(Object item : (Collection<Object>) value) { + for (Object item : (Collection<Object>) value) { buildMap(result, Collections.singletonMap("[" + (i++) + "]", item)); } output.put(key, new ArrayList<>(result.values())); - } - else { + } else { output.put(key, value); } } } - public static Map<String, Object> loadYamlAsStrictMap(String content){ + public static Map<String, Object> loadYamlAsStrictMap(String content) { Map<String, Object> result = new LinkedHashMap<>(); Object map = strictYamlLoader.load(content); - buildMap(result, (Map<String, Object>)map); + buildMap(result, (Map<String, Object>) map); return result; } private static class YamlLoader extends YamlProcessor { + public Yaml getStrictYamlLoader() { return createYaml(); } } @SuppressWarnings("unchecked") - public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParamsWithoutImplicitTypes(String heatDecodedPayload, String artifactType) { - Map<String, Object> heatData = (Map<String, Object>) new Yaml(new Constructor(), new Representer(), new DumperOptions(), customResolver).load(heatDecodedPayload); + public static Either<List<HeatParameterDefinition>, ResultStatusEnum> getHeatParamsWithoutImplicitTypes( + String heatDecodedPayload, String artifactType) { + Map<String, Object> heatData = (Map<String, Object>) 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<Object> returnedList) { + private static void handleElementNameNotFound(String elementName, Object elementValue, + ToscaElementTypeEnum elementType, List<Object> returnedList) { if (elementValue instanceof Map) { findToscaElements((Map<String, Object>) 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<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) { 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<Object> list, String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList) { + private static void findAllToscaElementsInList(List<Object> list, String elementName, + ToscaElementTypeEnum elementType, List<Object> returnedList) { list.forEach(elementValue -> handleElementNameNotFound(elementName, elementValue, elementType, returnedList)); } - public static Either<Object, ResultStatusEnum> findToscaElement(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum elementName, ToscaElementTypeEnum elementType) { + public static Either<Object, ResultStatusEnum> findToscaElement(Map<String, Object> toscaJson, + TypeUtils.ToscaTagNamesEnum elementName, + ToscaElementTypeEnum elementType) { List<Object> 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. <br> - * Returns Either element with:<br> - * List with all value if values found<br> - * Or ELEMENT_NOT_FOUND ActionStatus + * Returns Either element with:<br> List with all value if values found<br> Or ELEMENT_NOT_FOUND ActionStatus * * @param toscaJson * @return */ - public static Either<List<Object>, ResultStatusEnum> findToscaElements(Map<String, Object> toscaJson, String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList) { + 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)) { skipKey = handleFoundElement(toscaJson, elementName, elementType, returnedList); } - Iterator<Entry<String, Object>> keyValItr = toscaJson.entrySet().iterator(); while (keyValItr.hasNext()) { Entry<String, Object> keyValEntry = keyValItr.next(); @@ -272,7 +284,8 @@ public final class ImportUtils { return returnedElement; } - private static String handleFoundElement(Map<String, Object> toscaJson, String elementName, ToscaElementTypeEnum elementType, List<Object> returnedList) { + 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; @@ -280,9 +293,11 @@ public final class ImportUtils { } @SuppressWarnings("unchecked") - public static <T> Either<List<T>, ResultStatusEnum> findFirstToscaListElement(Map<String, Object> toscaJson, TypeUtils.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); + Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, + ToscaElementTypeEnum.LIST); if (findFirstToscaElement.isLeft()) { returnedElement = Either.left((List<T>) findFirstToscaElement.left().value()); } @@ -291,9 +306,11 @@ public final class ImportUtils { } @SuppressWarnings("unchecked") - public static <T> Either<Map<String, T>, ResultStatusEnum> findFirstToscaMapElement(Map<String, Object> toscaJson, TypeUtils.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); + Either<Object, ResultStatusEnum> findFirstToscaElement = findToscaElement(toscaJson, toscaTagName, + ToscaElementTypeEnum.MAP); if (findFirstToscaElement.isLeft()) { returnedElement = Either.left((Map<String, T>) findFirstToscaElement.left().value()); } @@ -301,9 +318,11 @@ public final class ImportUtils { } - public static Either<String, ResultStatusEnum> findFirstToscaStringElement(Map<String, Object> toscaJson, TypeUtils.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); + Either<Object, ResultStatusEnum> 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<String, ResultStatusEnum> findFirstToscaBooleanElement(Map<String, Object> toscaJson, TypeUtils.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); + Either<Object, ResultStatusEnum> 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<PropertyConstraint> getPropertyConstraints(Map<String, Object> propertyValue, String propertyType) { + private static List<PropertyConstraint> getPropertyConstraints(Map<String, Object> propertyValue, + String propertyType) { List<Object> propertyFieldConstraints = findCurrentLevelConstraintsElement(propertyValue); if (CollectionUtils.isNotEmpty(propertyFieldConstraints)) { List<PropertyConstraint> constraintList = new ArrayList<>(); Type constraintType = new TypeToken<PropertyConstraint>() { }.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<Object>) 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<String, Object> propertyValue, ToscaTagNamesEnum elementName, String type, Consumer<String> setter) { - Either<Object, ResultStatusEnum> eitherValue = findToscaElement(propertyValue, elementName, ToscaElementTypeEnum.ALL); + private static void setJsonStringField(Map<String, Object> propertyValue, ToscaTagNamesEnum elementName, + String type, Consumer<String> setter) { + Either<Object, ResultStatusEnum> eitherValue = 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(); + 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(); @@ -423,7 +460,8 @@ public final class ImportUtils { return null; } - private static Either<Boolean, ResponseFormat> modifyPropertiesKeysToProperForm(Either<Map<String, PropertyDefinition>, ResultStatusEnum> properties, Annotation annotation) { + 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<>(); @@ -432,8 +470,11 @@ public final class ImportUtils { 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 {} 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<String, Object> inputValue, AnnotationTypeOperations annotationTypeOperations) { + public static InputDefinition createModuleInput(Map<String, Object> 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<String, Object> inputValue, AnnotationTypeOperations 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); + 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;}); + 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) { + private static InputDefinition modifyInputWithAnnotations(InputDefinition inputDef, + Map<String, Annotation> 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<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) { + private 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()); @@ -536,7 +587,8 @@ public final class ImportUtils { } - public static void setFieldBoolean(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer<String> setter) { + public static void setFieldBoolean(Map<String, Object> toscaJson, TypeUtils.ToscaTagNamesEnum tagName, + Consumer<String> setter) { Either<String, ResultStatusEnum> 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<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, TypeUtils.ToscaTagNamesEnum.PARAMETERS); + Either<List<HeatParameterDefinition>, ResultStatusEnum> eitherResult = Either + .right(ResultStatusEnum.ELEMENT_NOT_FOUND); + 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<>(); @@ -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<HeatParameterDefinition, ResultStatusEnum> propertyStatus = createModuleHeatParameter((Map<String, Object>) propertyNameValue.getValue()); + Either<HeatParameterDefinition, ResultStatusEnum> propertyStatus = createModuleHeatParameter( + (Map<String, Object>) propertyNameValue.getValue()); if (propertyStatus.isRight()) { return Either.right(propertyStatus.right().value()); } @@ -670,44 +724,52 @@ public final class ImportUtils { } - private static void addHeatParamDefinition(List<HeatParameterDefinition> moduleProperties, Entry<String, Object> propertyNameValue, boolean isJson) { + private static void addHeatParamDefinition(List<HeatParameterDefinition> moduleProperties, + Entry<String, Object> 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<HeatParameterDefinition, ResultStatusEnum> createModuleHeatParameter(Map<String, Object> propertyValue) { + private static Either<HeatParameterDefinition, ResultStatusEnum> createModuleHeatParameter( + Map<String, Object> propertyValue) { HeatParameterDefinition propertyDef = new HeatParameterDefinition(); String type; - Either<String, ResultStatusEnum> propertyFieldType = findFirstToscaStringElement(propertyValue, TypeUtils.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, TypeUtils.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, TypeUtils.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) : 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<Object> foundElements = new ArrayList<>(); + final Either<List<Object>, ResultStatusEnum> toscaElements = ImportUtils.findToscaElements(toscaJsonAll, + ToscaTagNamesEnum.DATA_TYPES.getElementName(), ToscaElementTypeEnum.MAP, foundElements); + if (toscaElements.isLeft()) { + final Map<String, Object> toscaAttributes = (Map<String, Object>) 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<String, Object> 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<String, Object> 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<String>) 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<String, Object> mappedToscaTemplate = decodePayload(payloadData); final Either<String, ResultStatusEnum> findFirstToscaStringElement = ImportUtils.findFirstToscaStringElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.TOSCA_VERSION); - return findFirstToscaStringElement.left().value(); + if (findFirstToscaStringElement.isLeft()) { + return findFirstToscaStringElement.left().value(); + } else { + return null; + } + } + + private Map<String, Object> getDataTypes(final String payloadData) { + final Map<String, Object> mappedToscaTemplate = decodePayload(payloadData); + final Either<Map<String, Object>, 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<CategoryDefinition> categories) { @@ -1172,4 +1204,29 @@ public class ResourceImportManager { this.auditingManager = auditingManager; } + private List<DataTypeDefinition> extractDataTypeFromJson(final ResourceBusinessLogic resourceBusinessLogic, + final Map<String, Object> foundElements) { + final List<DataTypeDefinition> dataTypeDefinitionList = new ArrayList<>(); + if (MapUtils.isNotEmpty(foundElements)) { + final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeCacheAll = + resourceBusinessLogic.dataTypeCache.getAll(); + if (dataTypeCacheAll.isLeft()) { + for (final Entry<String, Object> 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; + } } |