Handle duplicated yang resource exception when creating schema set 42/140242/5
authorhalil.cakal <halil.cakal@est.tech>
Tue, 18 Feb 2025 10:24:54 +0000 (10:24 +0000)
committerhalil.cakal <halil.cakal@est.tech>
Thu, 20 Feb 2025 16:41:23 +0000 (16:41 +0000)
- catch and log duplicated yang resource during schema set creation
- there may be other exception when the app started however there will
  be a tech. dept ticket for them thus please review this commit for
   only duplicated yang resource exception

Issue-ID: CPS-2647

Change-Id: Idf6063cb8328efc667516f09d25ad6c4c6fd8186
Signed-off-by: halil.cakal <halil.cakal@est.tech>
cps-service/src/main/java/org/onap/cps/init/AbstractModelLoader.java
cps-service/src/test/groovy/org/onap/cps/init/AbstractModelLoaderSpec.groovy

index a802390..df068c6 100644 (file)
@@ -34,6 +34,7 @@ import org.onap.cps.api.CpsDataService;
 import org.onap.cps.api.CpsDataspaceService;
 import org.onap.cps.api.CpsModuleService;
 import org.onap.cps.api.exceptions.AlreadyDefinedException;
+import org.onap.cps.api.exceptions.DuplicatedYangResourceException;
 import org.onap.cps.api.exceptions.ModelOnboardingException;
 import org.onap.cps.api.parameters.CascadeDeleteAllowed;
 import org.onap.cps.utils.JsonObjectMapper;
@@ -57,10 +58,10 @@ public abstract class AbstractModelLoader implements ModelLoader {
     public void onApplicationEvent(final ApplicationStartedEvent applicationStartedEvent) {
         try {
             onboardOrUpgradeModel();
-        } catch (final Exception modelOnboardUpException) {
+        } catch (final Exception exception) {
             log.error("Exiting application due to failure in onboarding model: {} ",
-                    modelOnboardUpException.getMessage());
-            SpringApplication.exit(applicationStartedEvent.getApplicationContext(), () -> EXIT_CODE_ON_ERROR);
+                exception.getMessage());
+            exitApplication(applicationStartedEvent);
         }
     }
 
@@ -76,6 +77,8 @@ public abstract class AbstractModelLoader implements ModelLoader {
             cpsModuleService.createSchemaSet(dataspaceName, schemaSetName, yangResourcesContentByResourceName);
         } catch (final AlreadyDefinedException alreadyDefinedException) {
             log.warn("Creating new schema set failed as schema set already exists");
+        } catch (final DuplicatedYangResourceException duplicatedYangResourceException) {
+            log.warn("Ignoring yang resource duplication exception. Assuming model was created by another instance");
         } catch (final Exception exception) {
             log.error("Creating schema set {} failed: {} ", schemaSetName, exception.getMessage());
             throw new ModelOnboardingException("Creating schema set failed", exception.getMessage());
@@ -180,4 +183,8 @@ public abstract class AbstractModelLoader implements ModelLoader {
             throw new ModelOnboardingException(message, exception.getMessage());
         }
     }
+
+    private void exitApplication(final ApplicationStartedEvent applicationStartedEvent) {
+        SpringApplication.exit(applicationStartedEvent.getApplicationContext(), () -> EXIT_CODE_ON_ERROR);
+    }
 }
index 0618cad..c3cb4f2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2023-2024 Nordix Foundation
+ *  Copyright (C) 2023-2025 Nordix Foundation
  *  Modification Copyright (C) 2024 TechMahindra Ltd.
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -28,6 +28,7 @@ import org.onap.cps.api.CpsAnchorService
 import org.onap.cps.api.CpsDataService
 import org.onap.cps.api.CpsDataspaceService
 import org.onap.cps.api.CpsModuleService
+import org.onap.cps.api.exceptions.DuplicatedYangResourceException
 import org.onap.cps.api.exceptions.ModelOnboardingException
 import org.onap.cps.api.parameters.CascadeDeleteAllowed
 import org.onap.cps.api.exceptions.AlreadyDefinedException
@@ -117,6 +118,16 @@ class AbstractModelLoaderSpec extends Specification {
             1 * mockCpsModuleService.createSchemaSet('some dataspace','new name',_)
     }
 
+    def 'Creating a schema set handles duplicated yang resource exception'() {
+        given: 'module service throws duplicated yang resource exception'
+            mockCpsModuleService.createSchemaSet(*_) >> { throw new DuplicatedYangResourceException('my-yang-resource', 'my-yang-resource-checksum', null) }
+        when: 'attempt to create a schema set'
+            objectUnderTest.createSchemaSet('some dataspace','some schema set','cps-notification-subscriptions@2024-07-03.yang')
+        then: 'exception is ignored, and correct exception message is logged'
+            noExceptionThrown()
+            assertLogContains('Ignoring yang resource duplication exception. Assuming model was created by another instance')
+    }
+
     def 'Creating a schema set handles already defined exception.'() {
         given: 'the module service throws an already defined exception'
             mockCpsModuleService.createSchemaSet(*_) >>  { throw AlreadyDefinedException.forSchemaSet('name','context',null) }