From 541ed451d1257f6eb72517f33f470db53fdd7fb4 Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Tue, 5 Dec 2023 11:02:17 +0000 Subject: Introduce Schema Set upgrade method - Incl. integration test - Prod code is changed to use new method that update yand module. - updated module resource to return multiple modules. Issue-ID: CPS-1806 Signed-off-by: sourabh_sourabh Change-Id: I751d9393ce78a3be9daeaff6d0252738c02115e0 Signed-off-by: sourabh_sourabh --- .../impl/inventory/sync/ModuleOperationsUtils.java | 20 ++-- .../api/impl/inventory/sync/ModuleSyncService.java | 107 +++++++++++++-------- .../api/impl/inventory/sync/ModuleSyncTasks.java | 16 +-- .../sync/ModuleOperationsUtilsSpec.groovy | 21 +++- .../inventory/sync/ModuleSyncServiceSpec.groovy | 18 ++-- 5 files changed, 115 insertions(+), 67 deletions(-) (limited to 'cps-ncmp-service') diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleOperationsUtils.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleOperationsUtils.java index 25ea3bc1e6..750be2dc86 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleOperationsUtils.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleOperationsUtils.java @@ -38,6 +38,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries; import org.onap.cps.ncmp.api.impl.inventory.CmHandleState; import org.onap.cps.ncmp.api.impl.inventory.CompositeState; @@ -64,7 +65,7 @@ public class ModuleOperationsUtils { public static final String MODULE_SET_TAG_KEY = "moduleSetTag"; public static final String MODULE_SET_TAG_MESSAGE_FORMAT = "Upgrade to ModuleSetTag: {0}"; private static final String UPGRADE_FORMAT = "Upgrade to ModuleSetTag: %s"; - private static final String UPGRADE_FAILED_FORMAT = UPGRADE_FORMAT + " Attempt #%d failed: %s"; + private static final String LOCK_REASON_DETAILS_MSG_FORMAT = UPGRADE_FORMAT + " Attempt #%d failed: %s"; private static final Pattern retryAttemptPattern = Pattern.compile("Attempt #(\\d+) failed:.+"); private static final Pattern moduleSetTagPattern = Pattern.compile("Upgrade to ModuleSetTag: (\\S+)"); @@ -127,13 +128,14 @@ public class ModuleOperationsUtils { int attempt = 1; final Map compositeStateDetails = getLockedCompositeStateDetails(compositeState.getLockReason()); - if (!compositeStateDetails.isEmpty()) { + if (!compositeStateDetails.isEmpty() && compositeStateDetails.containsKey(RETRY_ATTEMPT_KEY)) { attempt = 1 + Integer.parseInt(compositeStateDetails.get(RETRY_ATTEMPT_KEY)); } + final String moduleSetTag = compositeStateDetails.get(MODULE_SET_TAG_KEY); compositeState.setLockReason(CompositeState.LockReason.builder() - .details(String.format(UPGRADE_FAILED_FORMAT, - compositeStateDetails.get(MODULE_SET_TAG_KEY), attempt, errorMessage)) - .lockReasonCategory(lockReasonCategory).build()); + .details(String.format(LOCK_REASON_DETAILS_MSG_FORMAT, StringUtils.isNotBlank(moduleSetTag) + ? moduleSetTag : "not-specified", attempt, errorMessage)).lockReasonCategory(lockReasonCategory) + .build()); } /** @@ -174,13 +176,19 @@ public class ModuleOperationsUtils { DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")); final CompositeState.LockReason lockReason = compositeState.getLockReason(); + final boolean moduleUpgrade = LockReasonCategory.MODULE_UPGRADE == lockReason.getLockReasonCategory(); + if (moduleUpgrade) { + log.info("Locked for module upgrade"); + return true; + } + final boolean failedDuringModuleSync = LockReasonCategory.MODULE_SYNC_FAILED == lockReason.getLockReasonCategory(); final boolean failedDuringModuleUpgrade = LockReasonCategory.MODULE_UPGRADE_FAILED == lockReason.getLockReasonCategory(); if (failedDuringModuleSync || failedDuringModuleUpgrade) { - log.info("Locked for module {}.", failedDuringModuleSync ? "sync" : "upgrade"); + log.info("Locked for module {} (last attempt failed).", failedDuringModuleSync ? "sync" : "upgrade"); return isRetryDue(lockReason, time); } log.info("Locked for other reason"); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncService.java index 841368c0db..a6b85a5cab 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncService.java @@ -73,34 +73,36 @@ public class ModuleSyncService { */ public void syncAndCreateOrUpgradeSchemaSetAndAnchor(final YangModelCmHandle yangModelCmHandle) { - final String moduleSetTag; final String cmHandleId = yangModelCmHandle.getId(); final CompositeState compositeState = yangModelCmHandle.getCompositeState(); final boolean inUpgrade = ModuleOperationsUtils.isInUpgradeOrUpgradeFailed(compositeState); - - if (inUpgrade) { - moduleSetTag = ModuleOperationsUtils.getLockedCompositeStateDetails(compositeState.getLockReason()) - .get(ModuleOperationsUtils.MODULE_SET_TAG_KEY); - } else { - moduleSetTag = yangModelCmHandle.getModuleSetTag(); - } + final String moduleSetTag = getModuleSetTag(yangModelCmHandle, compositeState, inUpgrade); final Collection moduleReferencesFromCache = moduleSetTagCache.get(moduleSetTag); if (moduleReferencesFromCache == null) { - final Optional optionalExistingCmHandleWithSameModuleSetTag + final Optional existingCmHandleWithSameModuleSetTag = getFirstReadyDataNodeWithModuleSetTag(moduleSetTag); - if (optionalExistingCmHandleWithSameModuleSetTag.isPresent()) { - final String existingCmHandleAnchorName - = optionalExistingCmHandleWithSameModuleSetTag.get().getAnchorName(); - createOrUpgradeSchemaSetUsingModuleSetTag(cmHandleId, moduleSetTag, existingCmHandleAnchorName); + if (existingCmHandleWithSameModuleSetTag.isPresent()) { + final String existingAnchorName = existingCmHandleWithSameModuleSetTag.get().getAnchorName(); + final Collection moduleReferencesFromExistingCmHandle = + upgradeOrCreateSchemaSetUsingModuleSetTag(yangModelCmHandle.getId(), moduleSetTag, + existingAnchorName, inUpgrade); + updateModuleSetTagCache(moduleSetTag, moduleReferencesFromExistingCmHandle); } else { - syncAndCreateSchemaSet(yangModelCmHandle, moduleSetTag); + final Collection allModuleReferencesFromCmHandle + = syncAndCreateSchemaSet(yangModelCmHandle); + updateModuleSetTagCache(moduleSetTag, allModuleReferencesFromCmHandle); } } else { - cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, - cmHandleId, NO_NEW_MODULES, moduleReferencesFromCache); + if (inUpgrade) { + cpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, + NO_NEW_MODULES, moduleReferencesFromCache); + } else { + cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, + cmHandleId, NO_NEW_MODULES, moduleReferencesFromCache); + } } if (!inUpgrade) { cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, cmHandleId); @@ -108,25 +110,6 @@ public class ModuleSyncService { setCmHandleModuleSetTag(yangModelCmHandle, moduleSetTag); } - private void syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle, final String moduleSetTag) { - final Collection allModuleReferencesFromCmHandle = - dmiModelOperations.getModuleReferences(yangModelCmHandle); - final Collection identifiedNewModuleReferencesFromCmHandle = cpsModuleService - .identifyNewModuleReferences(allModuleReferencesFromCmHandle); - final Map newModuleNameToContentMap; - if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) { - newModuleNameToContentMap = NO_NEW_MODULES; - } else { - newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, - identifiedNewModuleReferencesFromCmHandle); - } - cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, - yangModelCmHandle.getId(), newModuleNameToContentMap, allModuleReferencesFromCmHandle); - if (StringUtils.isNotBlank(moduleSetTag)) { - moduleSetTagCache.put(moduleSetTag, allModuleReferencesFromCmHandle); - } - } - /** * Deletes the SchemaSet for schema set id if the SchemaSet Exists. * @@ -162,16 +145,56 @@ public class ModuleSyncService { jsonObjectMapper.asJsonString(dmiRegistryProperties), OffsetDateTime.now()); } - private void createOrUpgradeSchemaSetUsingModuleSetTag(final String schemaSetName, - final String moduleSetTag, - final String existingCmHandleAnchorName) { + private Collection syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle) { + final Collection allModuleReferencesFromCmHandle = + dmiModelOperations.getModuleReferences(yangModelCmHandle); + final Collection identifiedNewModuleReferencesFromCmHandle = cpsModuleService + .identifyNewModuleReferences(allModuleReferencesFromCmHandle); + final Map newModuleNameToContentMap; + if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) { + newModuleNameToContentMap = NO_NEW_MODULES; + } else { + newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, + identifiedNewModuleReferencesFromCmHandle); + } + cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, + yangModelCmHandle.getId(), newModuleNameToContentMap, allModuleReferencesFromCmHandle); + return allModuleReferencesFromCmHandle; + } + + private Collection upgradeOrCreateSchemaSetUsingModuleSetTag(final String schemaSetName, + final String moduleSetTag, + final String existingAnchorName, + final boolean inUpgrade) { log.info("Found cm handle having module set tag: {}", moduleSetTag); final Collection moduleReferencesFromExistingCmHandle = cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, - existingCmHandleAnchorName); - cpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, - schemaSetName, NO_NEW_MODULES, moduleReferencesFromExistingCmHandle); - moduleSetTagCache.put(moduleSetTag, moduleReferencesFromExistingCmHandle); + existingAnchorName); + if (inUpgrade) { + cpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName, + NO_NEW_MODULES, moduleReferencesFromExistingCmHandle); + } else { + cpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, + schemaSetName, NO_NEW_MODULES, moduleReferencesFromExistingCmHandle); + } + return moduleReferencesFromExistingCmHandle; + } + + private String getModuleSetTag(final YangModelCmHandle yangModelCmHandle, + final CompositeState compositeState, + final boolean inUpgrade) { + if (inUpgrade) { + return ModuleOperationsUtils.getLockedCompositeStateDetails(compositeState.getLockReason()) + .get(ModuleOperationsUtils.MODULE_SET_TAG_KEY); + } + return yangModelCmHandle.getModuleSetTag(); + } + + private void updateModuleSetTagCache(final String moduleSetTag, + final Collection allModuleReferencesFromCmHandle) { + if (StringUtils.isNotBlank(moduleSetTag)) { + moduleSetTagCache.putIfAbsent(moduleSetTag, allModuleReferencesFromCmHandle); + } } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncTasks.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncTasks.java index 896316a496..e214044189 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncTasks.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncTasks.java @@ -67,20 +67,20 @@ public class ModuleSyncTasks { final YangModelCmHandle yangModelCmHandle = YangDataConverter.convertCmHandleToYangModel(cmHandleAsDataNode, cmHandleId); final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId); + final boolean inUpgrade = ModuleOperationsUtils.isInUpgradeOrUpgradeFailed(compositeState); try { - moduleSyncService.deleteSchemaSetIfExists(cmHandleId); + if (!inUpgrade) { + moduleSyncService.deleteSchemaSetIfExists(cmHandleId); + } moduleSyncService.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle); yangModelCmHandle.getCompositeState().setLockReason(null); cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.READY); } catch (final Exception e) { log.warn("Processing of {} module failed due to reason {}.", cmHandleId, e.getMessage()); - if (ModuleOperationsUtils.isInUpgradeOrUpgradeFailed(compositeState)) { - moduleOperationsUtils.updateLockReasonDetailsAndAttempts(compositeState, - LockReasonCategory.MODULE_UPGRADE_FAILED, e.getMessage()); - } else { - moduleOperationsUtils.updateLockReasonDetailsAndAttempts(compositeState, - LockReasonCategory.MODULE_SYNC_FAILED, e.getMessage()); - } + final LockReasonCategory lockReasonCategory = inUpgrade ? LockReasonCategory.MODULE_UPGRADE_FAILED + : LockReasonCategory.MODULE_SYNC_FAILED; + moduleOperationsUtils.updateLockReasonDetailsAndAttempts(compositeState, + lockReasonCategory, e.getMessage()); setCmHandleStateLocked(yangModelCmHandle, compositeState.getLockReason()); cmHandelStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED); } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleOperationsUtilsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleOperationsUtilsSpec.groovy index 099fc5ac18..8f3d8d97ba 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleOperationsUtilsSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleOperationsUtilsSpec.groovy @@ -108,9 +108,24 @@ class ModuleOperationsUtilsSpec extends Specification{ assert compositeState.lockReason.lockReasonCategory == MODULE_SYNC_FAILED assert compositeState.lockReason.details.contains(expectedDetails) where: - scenario | lockReason || expectedDetails - 'does not exist' | null || 'Attempt #1 failed: new error message' - 'exists' | CompositeState.LockReason.builder().details("Attempt #2 failed: some error message").build() || 'Attempt #3 failed: new error message' + scenario | lockReason || expectedDetails + 'does not exist' | null || 'Attempt #1 failed: new error message' + 'exists' | CompositeState.LockReason.builder().details("Attempt #2 failed: some error message").build() || 'Attempt #3 failed: new error message' + } + + def 'Update lock reason details that contains #scenario'() { + given: 'A locked state' + def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.LOCKED) + .withLockReason(MODULE_UPGRADE, "Upgrade to ModuleSetTag: " + moduleSetTag).build() + when: 'update cm handle details' + objectUnderTest.updateLockReasonDetailsAndAttempts(compositeState, MODULE_UPGRADE_FAILED, 'new error message') + then: 'the composite state lock reason and details are updated' + assert compositeState.lockReason.lockReasonCategory == MODULE_UPGRADE_FAILED + assert compositeState.lockReason.details.contains("Upgrade to ModuleSetTag: " + expectedDetails) + where: + scenario | moduleSetTag || expectedDetails + 'a module set tag' | 'someModuleSetTag' || 'someModuleSetTag' + 'empty module set tag' | '' || 'not-specified' } def 'Get all locked Cm-Handle where Lock Reason is MODULE_SYNC_FAILED cm handle #scenario'() { diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncServiceSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncServiceSpec.groovy index 5384f31cc1..b68862bf2b 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncServiceSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/inventory/sync/ModuleSyncServiceSpec.groovy @@ -76,7 +76,7 @@ class ModuleSyncServiceSpec extends Specification { when: 'module sync is triggered' objectUnderTest.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle) then: 'create schema set from module is invoked with correct parameters' - 1 * mockCpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'ch-1', newModuleNameContentToMap, moduleReferences) + 1 * mockCpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'ch-1', newModuleNameContentToMap, moduleReferences) and: 'anchor is created with the correct parameters' 1 * mockCpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'ch-1', 'ch-1') where: 'the following parameters are used' @@ -110,15 +110,17 @@ class ModuleSyncServiceSpec extends Specification { mockCmHandleQueries.cmHandleHasState('otherId', CmHandleState.READY) >> true when: 'module sync is triggered' objectUnderTest.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle) - then: 'create schema set from module is invoked for the upgraded cm handle' - 1 * mockCpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'upgraded-ch', [:], moduleReferences) + then: 'update schema set from module is invoked for the upgraded cm handle' + expectedCallsToUpgradeSchemaSet * mockCpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'upgraded-ch', [:], moduleReferences) + and: 'create schema set from module is invoked for the upgraded cm handle' + expectedCallsToCeateSchemaSet * mockCpsModuleService.createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'upgraded-ch', [:], moduleReferences) and: 'No anchor is created for the upgraded cm handle' 0 * mockCpsAdminService.createAnchor(*_) where: 'the following parameters are used' - scenario | populateCache | existingCmHandlesWithSameTag - 'new' | false | [] - 'in cache' | true | [] - 'in database' | false | [cmHandleWithModuleSetTag] + scenario | populateCache | existingCmHandlesWithSameTag || expectedCallsToUpgradeSchemaSet | expectedCallsToCeateSchemaSet + 'new' | false | [] || 0 | 1 + 'in cache' | true | [] || 1 | 0 + 'in database' | false | [cmHandleWithModuleSetTag] || 1 | 0 } def 'upgrade model for a existing cm handle'() { @@ -138,7 +140,7 @@ class ModuleSyncServiceSpec extends Specification { when: 'module upgrade is triggered' objectUnderTest.syncAndCreateOrUpgradeSchemaSetAndAnchor(yangModelCmHandle) then: 'the upgrade is delegated to the module service (with the correct parameters)' - 1 * mockCpsModuleService.createOrUpgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cmHandleId-1', Collections.emptyMap(), moduleReferences) + 1 * mockCpsModuleService.upgradeSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, 'cmHandleId-1', Collections.emptyMap(), moduleReferences) } def 'Delete Schema Set for CmHandle'() { -- cgit 1.2.3-korg