diff options
author | danielhanrahan <daniel.hanrahan@est.tech> | 2024-08-15 14:33:39 +0100 |
---|---|---|
committer | danielhanrahan <daniel.hanrahan@est.tech> | 2024-08-20 14:47:06 +0100 |
commit | 73f044beb1fe4bd2fd30fb71357c918ab39dda38 (patch) | |
tree | 805ffc1c6bcb17f783684993fbfe36830616c5de /cps-ncmp-service/src/main/java | |
parent | c5709a649f6dce392816a69d2c481d9b7680815a (diff) |
Refactoring alternate ID checker (CPS-2366 #1)
Issue-ID: CPS-2366
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
Change-Id: I07228a130ebdab0e2782e54255b5e8cc34c8d77e
Diffstat (limited to 'cps-ncmp-service/src/main/java')
-rw-r--r-- | cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/AlternateIdChecker.java | 84 |
1 files changed, 26 insertions, 58 deletions
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 1096980e1d..f8e13b76d2 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 @@ -28,7 +28,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle; -import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle; import org.onap.cps.spi.exceptions.DataNodeNotFoundException; import org.springframework.stereotype.Service; @@ -46,59 +45,6 @@ public class AlternateIdChecker { private static final String NO_CURRENT_ALTERNATE_ID = ""; /** - * Check if the alternate can be applied to the given cm handle (id). - * Conditions: - * - proposed alternate is blank (it wil be ignored) - * - proposed alternate is same as current (no change) - * - proposed alternate is not in use for a different cm handle (in the DB) - * - * @param cmHandleId cm handle id - * @param proposedAlternateId proposed alternate id - * @return true if the new alternate id not in use or equal to current alternate id, false otherwise - */ - public boolean canApplyAlternateId(final String cmHandleId, final String proposedAlternateId) { - String currentAlternateId = ""; - try { - final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId); - currentAlternateId = yangModelCmHandle.getAlternateId(); - } catch (final DataNodeNotFoundException dataNodeNotFoundException) { - // work with blank current alternate id - } - return this.canApplyAlternateId(cmHandleId, currentAlternateId, proposedAlternateId); - } - - /** - * Check if the alternate can be applied to the given cm handle. - * Conditions: - * - proposed alternate is blank (it wil be ignored) - * - proposed alternate is same as current (no change) - * - proposed alternate is not in use for a different cm handle (in the DB) - * - * @param cmHandleId cm handle id - * @param currentAlternateId current alternate id - * @param proposedAlternateId new alternate id - * @return true if the new alternate id not in use or equal to current alternate id, false otherwise - */ - public boolean canApplyAlternateId(final String cmHandleId, - final String currentAlternateId, - final String proposedAlternateId) { - if (StringUtils.isBlank(currentAlternateId)) { - if (alternateIdAlreadyInDb(proposedAlternateId)) { - log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already " - + "assigned to a different cm handle", cmHandleId); - return false; - } - return true; - } - if (currentAlternateId.equals(proposedAlternateId)) { - return true; - } - log.warn("Alternate id update ignored, cannot update cm handle {}, already has an alternate id of {}", - cmHandleId, currentAlternateId); - return false; - } - - /** * Check all alternate ids of a batch of cm handles. * Includes cross-checks in the batch itself for duplicates. Only the first entry encountered wil be accepted. * @@ -125,7 +71,7 @@ public class AlternateIdChecker { private boolean isProposedAlternateIdAcceptable(final String proposedAlternateId, final Operation operation, final Set<String> acceptedAlternateIds, final String cmHandleId) { - if (StringUtils.isEmpty(proposedAlternateId)) { + if (StringUtils.isBlank(proposedAlternateId)) { return true; } if (acceptedAlternateIds.contains(proposedAlternateId)) { @@ -133,10 +79,21 @@ public class AlternateIdChecker { + "assigned to a different cm handle (in this batch)", cmHandleId); return false; } - if (Operation.CREATE.equals(operation)) { - return canApplyAlternateId(cmHandleId, NO_CURRENT_ALTERNATE_ID, proposedAlternateId); + final String currentAlternateId = getCurrentAlternateId(operation, cmHandleId); + if (currentAlternateId.equals(proposedAlternateId)) { + return true; + } + if (StringUtils.isNotBlank(currentAlternateId)) { + log.warn("Alternate id update ignored, cannot update cm handle {}, already has an alternate id of {}", + cmHandleId, currentAlternateId); + return false; + } + if (alternateIdAlreadyInDb(proposedAlternateId)) { + log.warn("Alternate id update ignored, cannot update cm handle {}, alternate id is already " + + "assigned to a different cm handle", cmHandleId); + return false; } - return canApplyAlternateId(cmHandleId, proposedAlternateId); + return true; } private boolean alternateIdAlreadyInDb(final String alternateId) { @@ -148,4 +105,15 @@ public class AlternateIdChecker { return true; } + private String getCurrentAlternateId(final Operation operation, final String cmHandleId) { + if (operation == Operation.UPDATE) { + try { + return inventoryPersistence.getYangModelCmHandle(cmHandleId).getAlternateId(); + } catch (final DataNodeNotFoundException dataNodeNotFoundException) { + // work with blank current alternate id + } + } + return NO_CURRENT_ALTERNATE_ID; + } + } |