From 7347bbd9655ba6f713241af1ad3667ee22c85016 Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Tue, 20 Aug 2024 14:58:49 +0100 Subject: Faster alternate-id checks during registration (CPS-2366 #2) This improves performance when using alternate ID in registration. Instead of one CPS path query for each alternate ID, it sends one query for the whole batch using OR operator, e.g. /dmi-registry/cm-handles[@alternate-id='A' or @alternate-id='B'] Issue-ID: CPS-2366 Signed-off-by: danielhanrahan Change-Id: I5b10437f4a01c886b3c84e46ac727e0e79917589 --- .../ncmp/impl/inventory/AlternateIdChecker.java | 32 ++++++++++------------ .../ncmp/impl/inventory/InventoryPersistence.java | 8 ++++++ .../impl/inventory/InventoryPersistenceImpl.java | 15 ++++++++++ 3 files changed, 37 insertions(+), 18 deletions(-) (limited to 'cps-ncmp-service/src/main') diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/AlternateIdChecker.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/AlternateIdChecker.java index f8e13b76d2..3600d6da32 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/AlternateIdChecker.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/AlternateIdChecker.java @@ -22,8 +22,8 @@ package org.onap.cps.ncmp.impl.inventory; import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; import java.util.Set; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -55,13 +55,13 @@ public class AlternateIdChecker { public Collection getIdsOfCmHandlesWithRejectedAlternateId( final Collection newNcmpServiceCmHandles, final Operation operation) { - final Set acceptedAlternateIds = new HashSet<>(newNcmpServiceCmHandles.size()); + final Set assignedAlternateIds = getAlternateIdsAlreadyInDb(newNcmpServiceCmHandles); final Collection rejectedCmHandleIds = new ArrayList<>(); for (final NcmpServiceCmHandle ncmpServiceCmHandle : newNcmpServiceCmHandles) { final String cmHandleId = ncmpServiceCmHandle.getCmHandleId(); final String proposedAlternateId = ncmpServiceCmHandle.getAlternateId(); - if (isProposedAlternateIdAcceptable(proposedAlternateId, operation, acceptedAlternateIds, cmHandleId)) { - acceptedAlternateIds.add(proposedAlternateId); + if (isProposedAlternateIdAcceptable(proposedAlternateId, operation, assignedAlternateIds, cmHandleId)) { + assignedAlternateIds.add(proposedAlternateId); } else { rejectedCmHandleIds.add(cmHandleId); } @@ -70,15 +70,10 @@ public class AlternateIdChecker { } private boolean isProposedAlternateIdAcceptable(final String proposedAlternateId, final Operation operation, - final Set acceptedAlternateIds, final String cmHandleId) { + final Set assignedAlternateIds, final String cmHandleId) { if (StringUtils.isBlank(proposedAlternateId)) { return true; } - if (acceptedAlternateIds.contains(proposedAlternateId)) { - log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already " - + "assigned to a different cm handle (in this batch)", cmHandleId); - return false; - } final String currentAlternateId = getCurrentAlternateId(operation, cmHandleId); if (currentAlternateId.equals(proposedAlternateId)) { return true; @@ -88,7 +83,7 @@ public class AlternateIdChecker { cmHandleId, currentAlternateId); return false; } - if (alternateIdAlreadyInDb(proposedAlternateId)) { + if (assignedAlternateIds.contains(proposedAlternateId)) { log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already " + "assigned to a different cm handle", cmHandleId); return false; @@ -96,13 +91,14 @@ public class AlternateIdChecker { return true; } - private boolean alternateIdAlreadyInDb(final String alternateId) { - try { - inventoryPersistence.getCmHandleDataNodeByAlternateId(alternateId); - } catch (final DataNodeNotFoundException dataNodeNotFoundException) { - return false; - } - return true; + private Set getAlternateIdsAlreadyInDb(final Collection ncmpServiceCmHandles) { + final Set alternateIdsToCheck = ncmpServiceCmHandles.stream() + .map(NcmpServiceCmHandle::getAlternateId) + .filter(StringUtils::isNotBlank) + .collect(Collectors.toSet()); + return inventoryPersistence.getCmHandleDataNodesByAlternateIds(alternateIdsToCheck).stream() + .map(dataNode -> (String) dataNode.getLeaves().get("alternate-id")) + .collect(Collectors.toSet()); } private String getCurrentAlternateId(final Operation operation, final String cmHandleId) { diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistence.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistence.java index beef752ef1..a0d3a3eaee 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistence.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistence.java @@ -129,6 +129,14 @@ public interface InventoryPersistence extends NcmpPersistence { */ DataNode getCmHandleDataNodeByAlternateId(String alternateId); + /** + * Get data nodes for the given batch of alternate ids. + * + * @param alternateIds alternate IDs + * @return data nodes + */ + Collection getCmHandleDataNodesByAlternateIds(Collection alternateIds); + /** * Get collection of data nodes of given cm handles. * diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistenceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistenceImpl.java index 083b25db3d..42123f1a26 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistenceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/InventoryPersistenceImpl.java @@ -32,6 +32,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.onap.cps.api.CpsAnchorService; import org.onap.cps.api.CpsDataService; @@ -183,6 +184,15 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv return dataNodes.iterator().next(); } + @Override + public Collection getCmHandleDataNodesByAlternateIds(final Collection alternateIds) { + if (alternateIds.isEmpty()) { + return Collections.emptyList(); + } + final String cpsPathForCmHandlesByAlternateIds = getCpsPathForCmHandlesByAlternateIds(alternateIds); + return cmHandleQueryService.queryNcmpRegistryByCpsPath(cpsPathForCmHandlesByAlternateIds, OMIT_DESCENDANTS); + } + @Override public Collection getCmHandleDataNodes(final Collection cmHandleIds) { final Collection xpaths = new ArrayList<>(cmHandleIds.size()); @@ -212,6 +222,11 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv return NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@alternate-id='" + alternateId + "']"; } + private static String getCpsPathForCmHandlesByAlternateIds(final Collection alternateIds) { + return alternateIds.stream().collect(Collectors.joining("' or @alternate-id='", + NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@alternate-id='", "']")); + } + private static String createStateJsonData(final String state) { return "{\"state\":" + state + "}"; } -- cgit 1.2.3-korg