Updated onboardOrUpgradeModel in InventoryModelLoader to check if module revision... 90/141990/1
authorsourabh_sourabh <sourabh.sourabh@est.tech>
Thu, 4 Sep 2025 16:44:47 +0000 (17:44 +0100)
committersourabh_sourabh <sourabh.sourabh@est.tech>
Thu, 4 Sep 2025 16:52:16 +0000 (17:52 +0100)
 - Simplified conditional flow by removing duplicate revision checks.

Issue-ID: CPS-2974
Change-Id: I8e5ca62c60a33eb01abffbd917a98e18fefb22f0
Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
cps-ncmp-service/src/main/java/org/onap/cps/ncmp/init/InventoryModelLoader.java
cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/init/InventoryModelLoaderSpec.groovy
cps-service/pom.xml
cps-service/src/main/java/org/onap/cps/init/AbstractModelLoader.java
cps-service/src/main/java/org/onap/cps/init/actuator/ReadinessHealthIndicator.java

index 7ca52f1..a9d4b73 100644 (file)
@@ -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();
+    }
 }
index 2ae66c9..0a38949 100644 (file)
@@ -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'
index 8b7de2c..9264d95 100644 (file)
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-aop</artifactId>
     </dependency>
-      <dependency>
-          <groupId>org.springframework.boot</groupId>
-          <artifactId>spring-boot-starter-actuator</artifactId>
-      </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-actuator</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-validation</artifactId>
index f1bf78a..52a942e 100644 (file)
@@ -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<ModuleDefinition> moduleDefinitions =
+                cpsModuleService.getModuleDefinitionsByAnchorAndModule(dataspaceName, anchorName, moduleName,
+                        moduleRevision);
+        return !moduleDefinitions.isEmpty();
+    }
+
     Map<String, String> mapYangResourcesToContent(final String... resourceNames) {
         final Map<String, String> 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<ModuleDefinition> moduleDefinitions =
-                cpsModuleService.getModuleDefinitionsByAnchorAndModule(dataspaceName, anchorName, moduleName,
-                        moduleRevision);
-        return !moduleDefinitions.isEmpty();
-    }
-
     private void exitApplication(final ApplicationStartedEvent applicationStartedEvent) {
         SpringApplication.exit(applicationStartedEvent.getApplicationContext(), () -> EXIT_CODE_ON_ERROR);
     }
index caf430b..e40769a 100644 (file)
@@ -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();
     }
 }