From 38c6faa738abe6e0acdd24df2364d725d36fca40 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Tue, 31 Aug 2021 10:04:26 +0100 Subject: Fix additional types import file generation The additional_type_definitions.yaml is missing types entries and header. Also, the solution is not considering the types entries, i.e. 'data_types:', 'policy_types:', etc., when replacing types and generating the new file, which renders the solution broken. This change aims to fix the related problems. Change-Id: I412683b49966c09dd067ecbf8a1d778155b23fa6 Issue-ID: SDC-3703 Signed-off-by: andre.schmid --- .../sdc/be/model/normatives/ElementTypeEnum.java | 38 +++++++ .../be/model/operations/impl/ModelOperation.java | 109 +++++++++------------ 2 files changed, 86 insertions(+), 61 deletions(-) create mode 100644 catalog-model/src/main/java/org/openecomp/sdc/be/model/normatives/ElementTypeEnum.java (limited to 'catalog-model/src/main/java/org') diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/normatives/ElementTypeEnum.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/normatives/ElementTypeEnum.java new file mode 100644 index 0000000000..57ca559615 --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/normatives/ElementTypeEnum.java @@ -0,0 +1,38 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.model.normatives; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter +public enum ElementTypeEnum { + POLICY_TYPE("policy_types"), + GROUP_TYPE("group_types"), + DATA_TYPE("data_types"), + CAPABILITY_TYPE("capability_types"), + INTERFACE_LIFECYCLE_TYPE("interface_types"), + RELATIONSHIP_TYPE("relationship_types"); + + private final String toscaEntryName; +} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java index 87ab3fc79d..967ffdc711 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java @@ -20,15 +20,15 @@ package org.openecomp.sdc.be.model.operations.impl; import fj.data.Either; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.EnumMap; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -51,6 +51,7 @@ import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.model.Model; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException; +import org.openecomp.sdc.be.model.normatives.ElementTypeEnum; import org.openecomp.sdc.be.model.operations.api.DerivedFromOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.resources.data.ModelData; @@ -64,7 +65,7 @@ import org.yaml.snakeyaml.Yaml; public class ModelOperation { private static final Logger log = Logger.getLogger(ModelOperation.class); - private static final String ADDITIONAL_TYPE_DEFINITIONS = "additional_type_definitions.yml"; + static final Path ADDITIONAL_TYPE_DEFINITIONS_PATH = Path.of("additional_type_definitions.yaml"); private final JanusGraphGenericDao janusGraphGenericDao; private final JanusGraphDao janusGraphDao; @@ -173,7 +174,7 @@ public class ModelOperation { toscaImportByModel.setContent(content); return toscaImportByModel; }).collect(Collectors.toList()); - toscaModelImportCassandraDao.importAll(modelId, toscaImportByModelList); + toscaModelImportCassandraDao.replaceImports(modelId, toscaImportByModelList); } /** @@ -257,75 +258,61 @@ public class ModelOperation { } } - public void addTypesToDefaultImports(final String typesYaml, final String modelName) { - final List allSchemaImportsByModel = toscaModelImportCassandraDao.findAllByModel(modelName); - final Optional additionalTypeDefinitionsOptional = allSchemaImportsByModel.stream() - .filter(t -> ADDITIONAL_TYPE_DEFINITIONS.equals(t.getFullPath())).findAny(); - final ToscaImportByModel toscaImportByModelAdditionalTypeDefinitions; - final List schemaImportsByModel; - if (additionalTypeDefinitionsOptional.isPresent()) { - toscaImportByModelAdditionalTypeDefinitions = additionalTypeDefinitionsOptional.get(); - schemaImportsByModel = allSchemaImportsByModel.stream() - .filter(toscaImportByModel -> !ADDITIONAL_TYPE_DEFINITIONS.equals(toscaImportByModel.getFullPath())) + public void addTypesToDefaultImports(final ElementTypeEnum elementTypeEnum, final String typesYaml, final String modelName) { + final List modelImportList = toscaModelImportCassandraDao.findAllByModel(modelName); + final Optional additionalTypeDefinitionsImportOptional = modelImportList.stream() + .filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny(); + final ToscaImportByModel additionalTypeDefinitionsImport; + final List rebuiltModelImportList; + if (additionalTypeDefinitionsImportOptional.isPresent()) { + additionalTypeDefinitionsImport = additionalTypeDefinitionsImportOptional.get(); + rebuiltModelImportList = modelImportList.stream() + .filter(toscaImportByModel -> !ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(toscaImportByModel.getFullPath()))) .collect(Collectors.toList()); } else { - toscaImportByModelAdditionalTypeDefinitions = new ToscaImportByModel(); - toscaImportByModelAdditionalTypeDefinitions.setModelId(modelName); - toscaImportByModelAdditionalTypeDefinitions.setFullPath(ADDITIONAL_TYPE_DEFINITIONS); - toscaImportByModelAdditionalTypeDefinitions.setContent(typesYaml); - schemaImportsByModel = new ArrayList<>(allSchemaImportsByModel); + additionalTypeDefinitionsImport = new ToscaImportByModel(); + additionalTypeDefinitionsImport.setModelId(modelName); + additionalTypeDefinitionsImport.setFullPath(ADDITIONAL_TYPE_DEFINITIONS_PATH.toString()); + additionalTypeDefinitionsImport.setContent(createAdditionalTypeDefinitionsHeader()); + rebuiltModelImportList = new ArrayList<>(modelImportList); } - final List toscaImportByModels = removeExistingDefaultImports(typesYaml, schemaImportsByModel); + final Map typesYamlMap = new Yaml().load(typesYaml); + removeExistingTypesFromDefaultImports(elementTypeEnum, typesYamlMap, rebuiltModelImportList); - final Map originalContent = (Map) new Yaml().load(toscaImportByModelAdditionalTypeDefinitions.getContent()); - toscaImportByModelAdditionalTypeDefinitions.setContent(buildAdditionalTypeDefinitionsContent(typesYaml, originalContent).toString()); - toscaImportByModels.add(toscaImportByModelAdditionalTypeDefinitions); + final Map originalContent = new Yaml().load(additionalTypeDefinitionsImport.getContent()); + additionalTypeDefinitionsImport.setContent(buildAdditionalTypeDefinitionsContent(elementTypeEnum, typesYamlMap, originalContent)); + rebuiltModelImportList.add(additionalTypeDefinitionsImport); - toscaModelImportCassandraDao.importOnly(modelName, toscaImportByModels); + toscaModelImportCassandraDao.saveAll(modelName, rebuiltModelImportList); } - private List removeExistingDefaultImports(final String typesYaml, final List schemaImportsByModel) { - final List toscaImportByModels = new ArrayList<>(); - schemaImportsByModel.forEach(toscaImportByModel -> { - final ToscaImportByModel toscaImportByModelNew = new ToscaImportByModel(); - toscaImportByModelNew.setModelId(toscaImportByModel.getModelId()); - toscaImportByModelNew.setFullPath(toscaImportByModel.getFullPath()); - - final Map existingImportYamlMap = (Map) new Yaml().load(toscaImportByModel.getContent()); - - ((Map) new Yaml().load(typesYaml)).keySet().forEach(existingImportYamlMap::remove); - - final StringBuilder stringBuilder = new StringBuilder(); - existingImportYamlMap.forEach((key, value) -> { - final Map hashMap = new HashMap<>(); - hashMap.put(key, value); - stringBuilder.append("\n").append(new YamlUtil().objectToYaml(hashMap)); - }); - - toscaImportByModelNew.setContent(stringBuilder.toString()); - toscaImportByModels.add(toscaImportByModelNew); + private void removeExistingTypesFromDefaultImports(final ElementTypeEnum elementTypeEnum, final Map typesYaml, + final List defaultImportList) { + defaultImportList.forEach(toscaImportByModel -> { + final Map existingImportYamlMap = new Yaml().load(toscaImportByModel.getContent()); + final Map currentTypeYamlMap = (Map) existingImportYamlMap.get(elementTypeEnum.getToscaEntryName()); + if (MapUtils.isNotEmpty(currentTypeYamlMap)) { + typesYaml.keySet().forEach(currentTypeYamlMap::remove); + } + toscaImportByModel.setContent(new YamlUtil().objectToYaml(existingImportYamlMap)); }); - return toscaImportByModels; } - private StringBuilder buildAdditionalTypeDefinitionsContent(final String typesYaml, final Map originalContent) { - final var stringBuilder = new StringBuilder(); - - final Map typesYamlMap = (Map) new Yaml().load(typesYaml); - final Set typeYmlKeySet = typesYamlMap.keySet(); + private String buildAdditionalTypeDefinitionsContent(final ElementTypeEnum elementTypeEnum, final Map typesYamlMap, + final Map originalContent) { + final Map originalTypeContent = (Map) originalContent.get(elementTypeEnum.getToscaEntryName()); + if (MapUtils.isEmpty(originalTypeContent)) { + originalContent.put(elementTypeEnum.getToscaEntryName(), new LinkedHashMap<>(typesYamlMap)); + } else { + originalTypeContent.putAll(typesYamlMap); + } + return new YamlUtil().objectToYaml(originalContent); + } - originalContent.forEach((key, value) -> { - final Map hashMap = new HashMap<>(); - if (typeYmlKeySet.contains(key)) { - hashMap.put(key, typesYamlMap.get(key)); - } else { - hashMap.put(key, value); - } - final String newContent = new YamlUtil().objectToYaml(hashMap); - stringBuilder.append("\n").append(newContent); - }); - return stringBuilder; + private String createAdditionalTypeDefinitionsHeader() { + return "tosca_definitions_version: tosca_simple_yaml_1_3" + "\n" + + "description: Auto-generated file that contains package custom types or types added after system installation." + "\n"; } } -- cgit 1.2.3-korg