From 0dcf847d0df57209eb2a723256c6d84501d62e0e Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Thu, 4 Sep 2025 17:44:47 +0100 Subject: [PATCH] Updated onboardOrUpgradeModel in InventoryModelLoader to check if module revision already installed for all scenario - Simplified conditional flow by removing duplicate revision checks. Issue-ID: CPS-2974 Change-Id: I8e5ca62c60a33eb01abffbd917a98e18fefb22f0 Signed-off-by: sourabh_sourabh --- .../onap/cps/ncmp/init/InventoryModelLoader.java | 44 +++++++--------------- .../cps/ncmp/init/InventoryModelLoaderSpec.groovy | 7 +++- cps-service/pom.xml | 8 ++-- .../org/onap/cps/init/AbstractModelLoader.java | 22 +++++------ .../init/actuator/ReadinessHealthIndicator.java | 7 ++-- 5 files changed, 36 insertions(+), 52 deletions(-) diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/InventoryModelLoader.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/InventoryModelLoader.java index 7ca52f1cf0..a9d4b7364a 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/InventoryModelLoader.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/InventoryModelLoader.java @@ -66,22 +66,17 @@ public class InventoryModelLoader extends AbstractModelLoader { @Override public void onboardOrUpgradeModel() { final String schemaToInstall = newRevisionEnabled ? NEW_INVENTORY_SCHEMA_SET_NAME : PREVIOUS_SCHEMA_SET_NAME; - if (newRevisionEnabled) { - if (doesAnchorExist(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR)) { - final String moduleRevision = getModuleRevision(schemaToInstall); - if (isModuleRevisionInstalled(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, INVENTORY_YANG_MODULE_NAME, - moduleRevision)) { - log.info("Revision {} is already installed.", moduleRevision); - } else { - upgradeInventoryModel(); - performInventoryDataMigration(); - } - } else { - installInventoryModel(schemaToInstall); - } + final String moduleRevision = getModuleRevision(schemaToInstall); + + if (isModuleRevisionInstalled(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, INVENTORY_YANG_MODULE_NAME, + moduleRevision)) { + log.info("Revision {} is already installed.", moduleRevision); + } else if (newRevisionEnabled && doesAnchorExist(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR)) { + upgradeAndMigrateInventoryModel(); } else { installInventoryModel(schemaToInstall); } + applicationEventPublisher.publishEvent(new NcmpInventoryModelOnboardingFinishedEvent(this)); } @@ -114,24 +109,6 @@ public class InventoryModelLoader extends AbstractModelLoader { // TODO further implementation is pending //1. Load all the cm handles (in batch) //2. Copy the state and known properties - log.info("Starting inventory module data migration..."); - - // Simulate a 4-minute migration (240 seconds total) - final int totalSeconds = 240; - final int stepSeconds = 30; // log progress every 30 seconds - final int steps = totalSeconds / stepSeconds; - - for (int i = 1; i <= steps; i++) { - try { - Thread.sleep(stepSeconds * 1000L); - } catch (final InterruptedException e) { - Thread.currentThread().interrupt(); - log.warn("Migration interrupted!", e); - return; - } - final int progress = (i * 100) / steps; - log.info("Migration progress: {}%", progress); - } log.info("Inventory module data migration is completed successfully."); } @@ -143,4 +120,9 @@ public class InventoryModelLoader extends AbstractModelLoader { // Extract the revision part ( for example: 2024-02-23) return schemaSetName.substring(INVENTORY_YANG_MODULE_NAME.length() + 1); } + + private void upgradeAndMigrateInventoryModel() { + upgradeInventoryModel(); + performInventoryDataMigration(); + } } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/InventoryModelLoaderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/InventoryModelLoaderSpec.groovy index 2ae66c91a9..0a3894928e 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/InventoryModelLoaderSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/InventoryModelLoaderSpec.groovy @@ -74,9 +74,11 @@ class InventoryModelLoaderSpec extends Specification { } def 'Onboard subscription model via application ready event.'() { - given: 'dataspace is ready for use' + given: 'dataspace is ready for use with default newRevisionEnabled flag' objectUnderTest.newRevisionEnabled = false mockCpsAdminService.getDataspace(NCMP_DATASPACE_NAME) >> new Dataspace('') + and: 'module revision does not exist' + mockCpsModuleService.getModuleDefinitionsByAnchorAndModule(_, _, _, _) >> Collections.emptyList() when: 'the application is started' objectUnderTest.onApplicationEvent(Mock(ApplicationStartedEvent)) then: 'the module service is used to create the new schema set from the correct resource' @@ -88,8 +90,9 @@ class InventoryModelLoaderSpec extends Specification { } def 'Install new model revision'() { - given: 'the anchor does not exist' + given: 'the anchor and module revision does not exist' mockCpsAnchorService.getAnchor(_, _) >> { throw new AnchorNotFoundException('', '') } + mockCpsModuleService.getModuleDefinitionsByAnchorAndModule(_, _, _, _) >> Collections.emptyList() when: 'the inventory model loader is triggered' objectUnderTest.onboardOrUpgradeModel() then: 'a new schema set for the 2025-07-22 revision is installed' diff --git a/cps-service/pom.xml b/cps-service/pom.xml index 8b7de2c336..9264d955fa 100644 --- a/cps-service/pom.xml +++ b/cps-service/pom.xml @@ -53,10 +53,10 @@ org.springframework.boot spring-boot-starter-aop - - org.springframework.boot - spring-boot-starter-actuator - + + org.springframework.boot + spring-boot-starter-actuator + org.springframework.boot spring-boot-starter-validation diff --git a/cps-service/src/main/java/org/onap/cps/init/AbstractModelLoader.java b/cps-service/src/main/java/org/onap/cps/init/AbstractModelLoader.java index f1bf78a30a..52a942e6b9 100644 --- a/cps-service/src/main/java/org/onap/cps/init/AbstractModelLoader.java +++ b/cps-service/src/main/java/org/onap/cps/init/AbstractModelLoader.java @@ -192,6 +192,17 @@ public abstract class AbstractModelLoader implements ModelLoader { } } + /** + * Checks if the specified revision of a module is installed. + */ + protected boolean isModuleRevisionInstalled(final String dataspaceName, final String anchorName, + final String moduleName, final String moduleRevision) { + final Collection moduleDefinitions = + cpsModuleService.getModuleDefinitionsByAnchorAndModule(dataspaceName, anchorName, moduleName, + moduleRevision); + return !moduleDefinitions.isEmpty(); + } + Map mapYangResourcesToContent(final String... resourceNames) { final Map yangResourceContentByName = new HashMap<>(); for (final String resourceName: resourceNames) { @@ -210,17 +221,6 @@ public abstract class AbstractModelLoader implements ModelLoader { } } - /** - * Checks if the specified revision of a module is installed. - */ - protected boolean isModuleRevisionInstalled(final String dataspaceName, final String anchorName, - final String moduleName, final String moduleRevision) { - final Collection moduleDefinitions = - cpsModuleService.getModuleDefinitionsByAnchorAndModule(dataspaceName, anchorName, moduleName, - moduleRevision); - return !moduleDefinitions.isEmpty(); - } - private void exitApplication(final ApplicationStartedEvent applicationStartedEvent) { SpringApplication.exit(applicationStartedEvent.getApplicationContext(), () -> EXIT_CODE_ON_ERROR); } diff --git a/cps-service/src/main/java/org/onap/cps/init/actuator/ReadinessHealthIndicator.java b/cps-service/src/main/java/org/onap/cps/init/actuator/ReadinessHealthIndicator.java index caf430b1d5..e40769aa07 100644 --- a/cps-service/src/main/java/org/onap/cps/init/actuator/ReadinessHealthIndicator.java +++ b/cps-service/src/main/java/org/onap/cps/init/actuator/ReadinessHealthIndicator.java @@ -37,10 +37,9 @@ public class ReadinessHealthIndicator implements HealthIndicator { return Health.up() .withDetail("Startup Processes", "All startup processes completed") .build(); - } else { - return Health.down() - .withDetail("Startup Processes active", readinessManager.getStartupProcessesAsString()) - .build(); } + return Health.down() + .withDetail("Startup Processes active", readinessManager.getStartupProcessesAsString()) + .build(); } } -- 2.16.6