From: vasraz Date: Fri, 22 Sep 2023 13:51:31 +0000 (+0100) Subject: Fix 'Substitution Node not updated during import'-bug X-Git-Tag: 1.13.5~17 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=sdc.git;a=commitdiff_plain;h=f4668df71072c8ecd4b9d05423006265b36a6ef7 Fix 'Substitution Node not updated during import'-bug Signed-off-by: Vasyl Razinkov Change-Id: I3b99e4c60a5971916ed593b8d4161da58ddab6d6 Issue-ID: SDC-4633 --- diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java index 7837588178..8352a8d628 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java @@ -22,6 +22,7 @@ package org.openecomp.sdc.be.components.csar; import static org.openecomp.sdc.be.components.impl.ImportUtils.Constants.DEFAULT_ICON; +import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaStringElement; import static org.openecomp.sdc.be.components.impl.ImportUtils.findToscaElement; import fj.data.Either; @@ -282,16 +283,18 @@ public class ServiceCsarInfo extends CsarInfo { } @SuppressWarnings("unchecked") - private Set getNodeTypesUsedInToscaTemplate(Map mappedToscaTemplate) { - final Either nodeTemplatesEither = findToscaElement(mappedToscaTemplate, - TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP); - final Set nodeTypesUsedInNodeTemplates = new HashSet<>(); + private Set getNodeTypesUsedInToscaTemplate(final Map mappedToscaTemplate) { + final var nodeTemplatesEither = findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP); + final Set nodeTypesUsedInToscaTemplate = new HashSet<>(); if (nodeTemplatesEither.isLeft()) { - final Map> nodeTemplates = - (Map>) nodeTemplatesEither.left().value(); - nodeTypesUsedInNodeTemplates.addAll(findNodeTypesUsedInNodeTemplates(nodeTemplates)); + final var nodeTemplates = (Map>) nodeTemplatesEither.left().value(); + nodeTypesUsedInToscaTemplate.addAll(findNodeTypesUsedInNodeTemplates(nodeTemplates)); } - return nodeTypesUsedInNodeTemplates; + final var substitutionMappingsNodeType = findFirstToscaStringElement(mappedToscaTemplate, ToscaTagNamesEnum.NODE_TYPE); + if (substitutionMappingsNodeType.isLeft()){ + nodeTypesUsedInToscaTemplate.add(substitutionMappingsNodeType.left().value()); + } + return nodeTypesUsedInToscaTemplate; } private NodeTypeMetadata getMetaDataFromTemplate(Map mappedResourceTemplate, String nodeTemplateType) { @@ -320,7 +323,6 @@ public class ServiceCsarInfo extends CsarInfo { category.setName((String) metadata.get("category")); category.setNormalizedName(((String) metadata.get("category")).toLowerCase()); category.setIcons(List.of(DEFAULT_ICON)); - category.setNormalizedName(((String) metadata.get("category")).toLowerCase()); category.addSubCategory(subCategory); List categories = new ArrayList<>(); categories.add(category); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java index 3b0d19f03c..2d29f80017 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java @@ -109,7 +109,6 @@ import org.openecomp.sdc.be.datatypes.elements.SubPropertyToscaFunction; import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterPropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ToscaFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.datatypes.enums.FilterValueType; @@ -201,14 +200,12 @@ public class YamlTemplateParsingHandler { if (substitutionMappings != null) { if (component.isService()) { if (interfaceTemplateYaml.isEmpty()) { - Resource resource = serviceBusinessLogic.fetchDerivedFromGenericType(component, null); - List properties = resource.getProperties(); + List properties = serviceBusinessLogic.fetchDerivedFromGenericType(component, null).getProperties(); parsedToscaYamlInfo.setProperties(properties.stream().collect(Collectors.toMap(PropertyDefinition::getName, prop -> prop))); - parsedToscaYamlInfo.setSubstitutionFilterProperties(getSubstitutionFilterProperties(mappedToscaTemplate)); } else { parsedToscaYamlInfo.setProperties(getProperties(loadYamlAsStrictMap(interfaceTemplateYaml))); - parsedToscaYamlInfo.setSubstitutionFilterProperties(getSubstitutionFilterProperties(mappedToscaTemplate)); } + parsedToscaYamlInfo.setSubstitutionFilterProperties(getSubstitutionFilterProperties(mappedToscaTemplate)); } if (substitutionMappings.get("properties") != null) { parsedToscaYamlInfo.setSubstitutionMappingProperties((Map>) substitutionMappings.get("properties")); @@ -1442,7 +1439,7 @@ public class YamlTemplateParsingHandler { for (Object objValue : propValueList) { if (objValue instanceof Map) { Map objMap = (Map) objValue; - Map propValueMap = new HashMap(); + Map propValueMap = new HashMap<>(); propValueMap.put(String.valueOf(index), objValue); final Collection subPropertyToscaFunctions = buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>()); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CommonCsarGenerator.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CommonCsarGenerator.java index 633dd6e483..3fa8745f01 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CommonCsarGenerator.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CommonCsarGenerator.java @@ -643,7 +643,7 @@ public class CommonCsarGenerator { final List modelDefaultImportList = modelOperation.findAllModelImports(modelName, true); final Set writtenEntryPathList = new HashSet<>(); final var defsPath = Path.of(definitionsPath); - Map contentToMerge = new HashMap(); + Map contentToMerge = new HashMap<>(); for (final ToscaImportByModel toscaImportByModel : modelDefaultImportList) { var importPath = Path.of(toscaImportByModel.getFullPath()); if (!isSingleImportsFile) { @@ -675,15 +675,15 @@ public class CommonCsarGenerator { } if (!isSingleImportsFile) { byte[] mergingContent = new byte[0]; - for (Map.Entry entry : contentToMerge.entrySet()) { + for (Map.Entry entry : contentToMerge.entrySet()) { if (ADDITIONAL_TYPE_DEFINITIONS.equals(Paths.get(String.valueOf(entry.getKey())).normalize().toString())) { - mergingContent = (byte[]) entry.getValue(); + mergingContent = Bytes.concat(mergingContent, entry.getValue()); } else { final var zipEntry = new ZipEntry(entry.getKey().toString()); zipOutputStream.putNextEntry(zipEntry); - writtenEntryPathList.add((Path) entry.getKey()); - zipOutputStream.write(Bytes.concat(mergingContent, (byte[]) entry.getValue()), 0, - ((byte[]) entry.getValue()).length); + writtenEntryPathList.add(entry.getKey()); + final var concat = Bytes.concat(mergingContent, entry.getValue()); + zipOutputStream.write(concat, 0, concat.length); zipOutputStream.closeEntry(); } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/ResourceImportManagerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/ResourceImportManagerTest.java index 0d6922e3f2..d64aaf8cd0 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/ResourceImportManagerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/ResourceImportManagerTest.java @@ -477,7 +477,7 @@ class ResourceImportManagerTest { NodeTypesMetadataList nodeTypesMetadataList = new NodeTypesMetadataList(); List nodeTypeMetadataList = new ArrayList<>(); Map allTypesToCreate = new HashMap<>(); - ServiceCsarInfo csarInfo= getCsarInfo(); + ServiceCsarInfo csarInfo = getCsarInfo(); List nodeTypesToCreate = csarInfo.getNodeTypesUsed(); nodeTypesToCreate.stream().forEach(nodeType -> { allTypesToCreate.put(nodeType.getMappedNodeType().getKey(), nodeType.getMappedNodeType().getValue()); @@ -487,15 +487,26 @@ class ResourceImportManagerTest { when(toscaOperationFacade.getLatestByName(any(), any())).thenReturn(Either.left(null)).thenReturn(Either.left(null)); when(toscaOperationFacade.getLatestByToscaResourceName("org.openecomp.resource.VFC-root", "ETSI SOL001 v2.5.1")) - .thenReturn(Either.left(null)); - when(resourceBusinessLogic - .createOrUpdateResourceByImport(any(Resource.class), any(User.class), eq(true), eq(true), eq(false), eq(null), eq(null), eq(false))) - .thenReturn(new ImmutablePair<>(new Resource(), ActionStatus.OK)).thenReturn(new ImmutablePair<>(new Resource(), ActionStatus.OK)); + .thenReturn(Either.left(null)); + when(toscaOperationFacade.getLatestByToscaResourceName("tosca.nodes.nfv.NS", "")) + .thenReturn(Either.left(createResourceNS())); + when(toscaOperationFacade.getLatestByToscaResourceName("org.openecomp.service.Etsiwithchild", "")) + .thenReturn(Either.right(null)); + when(resourceBusinessLogic.createOrUpdateResourceByImport(any(Resource.class), any(User.class), eq(true), eq(true), eq(false), eq(null), + eq(null), eq(false))).thenReturn(new ImmutablePair<>(new Resource(), ActionStatus.OK)) + .thenReturn(new ImmutablePair<>(new Resource(), ActionStatus.OK)); + when(responseFormatManager.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(mock(ResponseFormat.class)); importManager.importAllNormativeResource(allTypesToCreate, nodeTypesMetadataList, null, user, "", false, false); verify(janusGraphDao).commit(); } + private Resource createResourceNS() { + Resource ns = new Resource(); + ns.setName("tosca.nodes.nfv.NS"); + return ns; + } + private void setResourceBusinessLogicMock() { when(resourceBusinessLogic.getUserAdmin()).thenReturn(userAdmin); when(resourceBusinessLogic.createOrUpdateResourceByImport(any(Resource.class), any(User.class), anyBoolean(), anyBoolean(), anyBoolean(), @@ -768,7 +779,8 @@ class ResourceImportManagerTest { assertNotNull(mainTemplateService); final String mainTemplateContent = new String(mainTemplateService); - return new ServiceCsarInfo(user, csarUuid, csar, vfReousrceName, null, mainTemplateName, mainTemplateContent, false, mock(ModelOperation.class)); + return new ServiceCsarInfo(user, csarUuid, csar, vfReousrceName, null, mainTemplateName, mainTemplateContent, false, + mock(ModelOperation.class)); } catch (URISyntaxException | ZipException e) { fail(e); } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java index fc709ceaea..c6ba0ba9ac 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogicTest.java @@ -212,11 +212,13 @@ class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTest when(csarBusinessLogic.getCsarInfo(any(Service.class), any(), any(User.class), any(Map.class), anyString(), any())).thenReturn(csarInfo); when(serviceImportParseLogic.findNodeTypesArtifactsToHandle(any(Map.class), any(CsarInfo.class), any(Service.class))) .thenReturn(Either.left(new HashMap>>())); - doReturn(getParsedToscaYamlInfo()).when(csarBusinessLogic).getParsedToscaYamlInfo(anyString(), anyString(), any(), any(CsarInfo.class), any(), any(Service.class)); + doReturn(getParsedToscaYamlInfo()).when(csarBusinessLogic) + .getParsedToscaYamlInfo(anyString(), anyString(), any(), any(CsarInfo.class), any(), any(Service.class)); doReturn(getParsedToscaYamlInfo()).when(csarBusinessLogic).getParsedToscaYamlInfo(any(ServiceCsarInfo.class), any(Service.class)); when(serviceBusinessLogic.lockComponentByName(newService.getSystemName(), oldService, CREATE_RESOURCE)).thenReturn(Either.left(true)); when(toscaOperationFacade.getLatestResourceByToscaResourceName(anyString())).thenReturn(Either.left(createOldResource())); - when(serviceImportParseLogic.createServiceTransaction(oldService, csarInfo.getModifier(), false, AuditingActionEnum.CREATE_RESOURCE)).thenReturn(newService); + when(serviceImportParseLogic.createServiceTransaction(oldService, csarInfo.getModifier(), false, + AuditingActionEnum.CREATE_RESOURCE)).thenReturn(newService); when(serviceImportParseLogic.createInputsOnService(eq(oldService), anyMap())).thenReturn(newService); Assertions.assertDoesNotThrow(() -> { when(serviceImportParseLogic.createSubstitutionFilterOnService(eq(oldService), any())).thenReturn(newService); @@ -258,14 +260,16 @@ class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTest properties.add(versionProperty); typeToBeUpdated.setProperties(properties); when(applicationDataTypeCache.get(any(), eq("onap.datatypes.ToscaConceptIdentifier.datatype"))).thenReturn(Either.left(typeToBeUpdated)); - when(applicationDataTypeCache.get(any(), matches("^((?!(tosca.datatypes.test_|onap.datatypes.ToscaConceptIdentifier)).)*$"))).thenReturn(Either.left(new DataTypeDefinition())); + when(applicationDataTypeCache.get(any(), matches("^((?!(tosca.datatypes.test_|onap.datatypes.ToscaConceptIdentifier)).)*$"))).thenReturn( + Either.left(new DataTypeDefinition())); - when(artifactTypeOperation.getArtifactTypeByUid(contains("tosca.testartifacts.Name"))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + when(artifactTypeOperation.getArtifactTypeByUid(contains("tosca.testartifacts.Name"))).thenReturn( + Either.right(StorageOperationStatus.NOT_FOUND)); when(artifactTypeOperation.getArtifactTypeByUid(contains("tosca.artifacts"))).thenReturn(Either.left(null)); when(interfaceLifecycleTypeOperation.getInterface(contains("tosca.interfaces"))).thenReturn(Either.left(new InterfaceDefinition())); - when(interfaceLifecycleTypeOperation.getInterface(contains("tosca.interfaces.test"))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); - + when(interfaceLifecycleTypeOperation.getInterface(contains("tosca.interfaces.test"))).thenReturn( + Either.right(StorageOperationStatus.NOT_FOUND)); when(capabilityTypeOperation.getCapabilityType(anyString())) .thenReturn(Either.left(new CapabilityTypeDefinition())); @@ -273,12 +277,15 @@ class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTest .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); when(toscaOperationFacade.getLatestByToscaResourceName(contains("org.openecomp.resource"), isNull())) - .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); when(toscaOperationFacade.getLatestByToscaResourceName(contains("tosca.nodes."), isNull())) - .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); when(toscaOperationFacade.getLatestByToscaResourceName(updatedNodeType, null)).thenReturn(Either.left(resource)); - when(artifactsBusinessLogic.handleDownloadRequestById(resourceUniqueId, artifactUniqueId, user.getUserId(), ComponentTypeEnum.RESOURCE, null, null)) - .thenReturn(resourceTemplate); + when(toscaOperationFacade.getLatestByToscaResourceName(contains("org.openecomp.service."), isNull())) + .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); + when(artifactsBusinessLogic.handleDownloadRequestById(resourceUniqueId, artifactUniqueId, user.getUserId(), ComponentTypeEnum.RESOURCE, null, + null)) + .thenReturn(resourceTemplate); when(toscaOperationFacade.updatePropertyOfComponent(eq(oldService), any(PropertyDefinition.class))).thenReturn(Either.left(null)); when(toscaOperationFacade.updateComponentInstancePropsToComponent(anyMap(), anyString())).thenReturn(Either.left(null)); when(groupTypeOperation.getGroupTypeByUid(anyString())).thenReturn(Either.left(new GroupTypeDefinition())); @@ -304,7 +311,7 @@ class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTest assertNotNull(yamlMap.get("onap.datatypes.ToscaConceptIdentifier")); ArgumentCaptor artifactTypes = ArgumentCaptor.forClass(String.class); - verify(artifactTypeImportManager).createArtifactTypes(artifactTypes.capture(),isNull(), anyBoolean()); + verify(artifactTypeImportManager).createArtifactTypes(artifactTypes.capture(), isNull(), anyBoolean()); Map artifactTypesMap = new Yaml().load(artifactTypes.getValue()); assertEquals(1, artifactTypesMap.size()); assertNotNull(artifactTypesMap.get("tosca.testartifacts.Name")); @@ -320,7 +327,7 @@ class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTest ArgumentCaptor> nodeTypes = ArgumentCaptor.forClass(Map.class); verify(resourceImportManager).importAllNormativeResource(nodeTypes.capture(), any(), any(), any(), any(), - anyBoolean(), anyBoolean()); + anyBoolean(), anyBoolean()); Map nodeTypesMap = nodeTypes.getValue(); Map newUpdatedNodeType = (Map) nodeTypesMap.get(updatedNodeType); assertEquals(8, ((Map) newUpdatedNodeType.get("properties")).size()); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/TopologyTemplateOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/TopologyTemplateOperation.java index a253d815ad..f8665b7845 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/TopologyTemplateOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsonjanusgraph/operations/TopologyTemplateOperation.java @@ -1329,6 +1329,11 @@ public class TopologyTemplateOperation extends ToscaElementOperation { log.debug("Failed to disassociate capabilities for {} error {}", toscaElementVertex.getUniqueId(), status); return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); } + status = janusGraphDao.disassociateAndDeleteLast(toscaElementVertex, Direction.OUT, EdgeLabelEnum.SUBSTITUTION_FILTER_TEMPLATE); + if (status != JanusGraphOperationStatus.OK) { + log.debug("Failed to disassociate substitution filter template for {} error {}", toscaElementVertex.getUniqueId(), status); + return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)); + } toscaElementVertex.getVertex().remove(); log.trace("Tosca element vertex for {} was removed", toscaElementVertex.getUniqueId());