From dfc9aa481470129710746d3e614f0b394548312a Mon Sep 17 00:00:00 2001 From: "halil.cakal" Date: Tue, 18 Feb 2025 10:24:54 +0000 Subject: Handle duplicated yang resource exception when creating schema set - 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 --- .../main/java/org/onap/cps/init/AbstractModelLoader.java | 13 ++++++++++--- .../groovy/org/onap/cps/init/AbstractModelLoaderSpec.groovy | 13 ++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'cps-service/src') 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 a80239039a..df068c68a6 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 @@ -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); + } } diff --git a/cps-service/src/test/groovy/org/onap/cps/init/AbstractModelLoaderSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/init/AbstractModelLoaderSpec.groovy index 0618cad951..c3cb4f205b 100644 --- a/cps-service/src/test/groovy/org/onap/cps/init/AbstractModelLoaderSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/init/AbstractModelLoaderSpec.groovy @@ -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) } -- cgit