Performance fix for Module search outputting Alternate IDs 40/140640/3
authordanielhanrahan <daniel.hanrahan@est.tech>
Tue, 1 Apr 2025 15:04:39 +0000 (16:04 +0100)
committerdanielhanrahan <daniel.hanrahan@est.tech>
Wed, 2 Apr 2025 11:01:35 +0000 (12:01 +0100)
Use OMIT_DESCENDANTS instead of INCLUDE_ALL_DESCENDANTS.

Issue-ID: CPS-2738
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
Change-Id: If0ee84baa297137f7275e611905e4183a16c7ee1

cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistence.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistenceImpl.java
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/ParameterizedCmHandleQueryServiceImpl.java
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/impl/inventory/InventoryPersistenceImplSpec.groovy

index 6bb1bfc..aeeb865 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022-2025 Nordix Foundation
+ *  Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
  *  Modifications Copyright (C) 2023 TechMahindra Ltd.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -118,7 +118,7 @@ public interface InventoryPersistence extends NcmpPersistence {
      * Get data node with the given cm handle id.
      *
      * @param cmHandleId cmHandle ID
-     * @param fetchDescendantsOption fetchDescendantsOption
+     * @param fetchDescendantsOption fetch descendants option
      * @return data node
      */
     Collection<DataNode> getCmHandleDataNodeByCmHandleId(String cmHandleId,
@@ -144,9 +144,11 @@ public interface InventoryPersistence extends NcmpPersistence {
      * Get collection of data nodes of given cm handles.
      *
      * @param cmHandleIds collection of cmHandle IDs
+     * @param fetchDescendantsOption fetch descendants option
      * @return collection of data nodes
      */
-    Collection<DataNode> getCmHandleDataNodes(Collection<String> cmHandleIds);
+    Collection<DataNode> getCmHandleDataNodes(Collection<String> cmHandleIds,
+                                              FetchDescendantsOption fetchDescendantsOption);
 
     /**
      * get CM handles that has given module names.
index 02e7112..8832290 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022-2025 Nordix Foundation
+ *  Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
  *  Modifications Copyright (C) 2022 Bell Canada
  *  Modifications Copyright (C) 2024 TechMahindra Ltd.
  *  ================================================================================
@@ -34,8 +34,8 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
-import java.util.stream.Stream;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.onap.cps.api.CpsAnchorService;
 import org.onap.cps.api.CpsDataService;
 import org.onap.cps.api.CpsModuleService;
@@ -134,7 +134,7 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv
                         dataValidationException.getMessage());
             }
         });
-        return YangDataConverter.toYangModelCmHandles(getCmHandleDataNodes(validCmHandleIds));
+        return YangDataConverter.toYangModelCmHandles(getCmHandleDataNodes(validCmHandleIds, INCLUDE_ALL_DESCENDANTS));
     }
 
     @Override
@@ -201,22 +201,22 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv
     }
 
     @Override
-    public Collection<DataNode> getCmHandleDataNodes(final Collection<String> cmHandleIds) {
+    public Collection<DataNode> getCmHandleDataNodes(final Collection<String> cmHandleIds,
+                                                     final FetchDescendantsOption fetchDescendantsOption) {
         final Collection<String> xpaths = new ArrayList<>(cmHandleIds.size());
         cmHandleIds.forEach(cmHandleId -> xpaths.add(getXPathForCmHandleById(cmHandleId)));
-        return this.getDataNodes(xpaths);
+        return this.getDataNodes(xpaths, fetchDescendantsOption);
     }
 
     @Override
     public Collection<String> getCmHandleReferencesWithGivenModules(final Collection<String> moduleNamesForQuery,
                                                                     final boolean outputAlternateId) {
-        if (outputAlternateId) {
-            final Collection<String> cmHandleIds =
+        final Collection<String> cmHandleIds =
                 cpsAnchorService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
-            return getAlternateIdsFromDataNodes(getCmHandleDataNodes(cmHandleIds));
-        } else {
-            return cpsAnchorService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
+        if (outputAlternateId) {
+            return getAlternateIdsForCmHandleIds(cmHandleIds);
         }
+        return cmHandleIds;
     }
 
     @Override
@@ -241,12 +241,6 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv
                 NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@alternate-id='", "']"));
     }
 
-    private String getCpsPathForCmHandlesByReferences(final Collection<String> cmHandleReferences) {
-        return cmHandleReferences.stream()
-            .flatMap(id -> Stream.of("@id='" + id + "'", "@alternate-id='" + id + "'"))
-            .collect(Collectors.joining(" or ", NCMP_DMI_REGISTRY_PARENT + "/cm-handles[", "]"));
-    }
-
     private static String createStateJsonData(final String state) {
         return "{\"state\":" + state + "}";
     }
@@ -255,8 +249,13 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv
         return "{\"cm-handles\":" + jsonObjectMapper.asJsonString(yangModelCmHandles) + "}";
     }
 
-    private Collection<String> getAlternateIdsFromDataNodes(final Collection<DataNode> dataNodes) {
-        return dataNodes.stream().map(dataNode ->
-            (String) dataNode.getLeaves().get("alternate-id")).collect(Collectors.toSet());
+
+    private Collection<String> getAlternateIdsForCmHandleIds(final Collection<String> cmHandleIds) {
+        final Collection<DataNode> dataNodes = getCmHandleDataNodes(cmHandleIds, OMIT_DESCENDANTS);
+        return dataNodes.stream()
+                .map(DataNode::getLeaves)
+                .map(leaves -> (String) leaves.get("alternate-id"))
+                .filter(StringUtils::isNotBlank)
+                .collect(Collectors.toSet());
     }
 }
index 6076895..bafb065 100644 (file)
@@ -288,14 +288,4 @@ public class ParameterizedCmHandleQueryServiceImpl implements ParameterizedCmHan
         }
     }
 
-    private Collection<String> collectCmHandleReferencesFromDataNodes(final Collection<DataNode> dataNodes,
-                                                                      final boolean outputAlternateId) {
-        if (outputAlternateId) {
-            return dataNodes.stream().map(dataNode ->
-                    (String) dataNode.getLeaves().get("alternate-id")).collect(Collectors.toSet());
-        } else {
-            return dataNodes.stream().map(dataNode ->
-                    (String) dataNode.getLeaves().get("id")).collect(Collectors.toSet());
-        }
-    }
 }
index 2b0997b..bc21360 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022-2025 Nordix Foundation
+ *  Copyright (C) 2022-2025 OpenInfra Foundation Europe. All rights reserved.
  *  Modifications Copyright (C) 2022 Bell Canada
  *  Modifications Copyright (C) 2024 TechMahindra Ltd.
  *  ================================================================================
@@ -336,15 +336,15 @@ class InventoryPersistenceImplSpec extends Specification {
     }
 
     def 'Get Alternate Ids for CM Handles that has given module names'() {
-        given: 'A Collection of data nodes'
-            def dataNodes = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='ch-1']", leaves: ['id': 'ch-1', 'alternate-id': 'alt-1'])]
-        when: 'the methods to get dataNodes is called and returns correct values'
+        given: 'cps anchor service returns a CM-handle ID for the given module name'
             mockCpsAnchorService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, ['sample-module-name']) >> ['ch-1']
-            mockCpsDataService.getDataNodesForMultipleXpaths(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, ["/dmi-registry/cm-handles[@id='ch-1']"], INCLUDE_ALL_DESCENDANTS) >> dataNodes
-        and: 'the method returns a result'
+        and: 'cps data service returns some data nodes for the given CM-handle ID'
+            def dataNodes = [new DataNode(xpath: "/dmi-registry/cm-handles[@id='ch-1']", leaves: ['id': 'ch-1', 'alternate-id': 'alt-1'])]
+            mockCpsDataService.getDataNodesForMultipleXpaths(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, ["/dmi-registry/cm-handles[@id='ch-1']"], OMIT_DESCENDANTS) >> dataNodes
+        when: 'the method to get cm-handle references by modules is called (outputting alternate IDs)'
             def result = objectUnderTest.getCmHandleReferencesWithGivenModules(['sample-module-name'], true)
         then: 'the result contains the correct alternate Id'
-            assert result == ['alt-1'] as HashSet
+            assert result == ['alt-1'] as Set
     }
 
     def 'Replace list content'() {